Ant for Web Developers II - Restart Apache

Following on from yesterdays first useful ant task, here’s another commonly used task - restarting a remote service. I’ve used apache in this example, but it could be any service running on your remote machine and it doesn’t have to be the restart command.

In order to do this we’ll use the sshexec target which has a third party library dependency. This is the same third party library needed for the scp task in yesterdays post

You need first to download JSCH and then compile the source using ant. Just run ant dist in the downloaded folder and you should get a .jar file in the /dist/lib folder. Save this .jar file as as jsch.jar to a folder in your home directory ~/.ant/lib where ant can automatically load it. Alternatively you can run ant with the -lib command to load libraries from a different location.

pre. ant -lib /tools/ant-libraries

With that out of the way lets have a look at the task.

pre.

<?xml version=“1.0” encoding=“UTF-8”?>

And running it is as simple as:

pre. ant restart-apache

One potential issue with tasks like this is storing the password in the build file in plain text. The target we’re using can also use key authentication is you’re happy using ssh keys. Alternatively you can set properties on the command line each time you run ant like so.

pre. ant restart-apache -Dpassword={password}

Ant for Web Developers I - Backup Config File

I occasionally get carried away with Apache Ant. For those that haven’t come across it, Ant is a build tool written in Java, using an XML syntax to describe a series of repeatable tasks. In your typical web standards savvy, dynamic language favouring, web developer types that description is probably all they (think they) need to know. It’s Java. It’s XML. It’s only really useful in the context of building software (dull).

But I think Ant is a particularly handy tool to have around for anyone working on even simple websites. A couple of strong use cases come to mind:

  • If you’re working in a team environment then build files are hugely useful when introducing new people to the team, or when moving people around. Getting code up and running at the start of a project, or if you join the team part way through, can be tricky. A well written build file can automate this.
  • Even when working on projects on my own I tend to write simple build files. The main reason is so I don’t forget how to do something. How do you deploy this particular site? How do you run the test suite or generate the documentation? Build files can encapsulate this, and rather than documentation that might be out of date the build file will be executed to do that job in question.

So with all that in mind I’m going to try and do a series of posts each covering a single task, aiming to cover things that your regular web developer will find useful. With that in mind if anyone has any requests or questions let me know either by email or in the comments.

Out first task lets us backup a file from our remote web server, in this case it’s the apache2.conf file used to setup apache. Obviously it could be any file you want to get hold of. The example below has a couple of properties for the username and hostname of the remote machine. Save the following snippet into a file called build.xml and place it anywhere you like on your machine.

pre.

<?xml version=“1.0” encoding=“UTF-8”?>

Running the task, once you have ant installed (it comes already installed on OS X and is generally available in whatever linux package management system you prefer), is as simple as typing the following into a console.

pre. ant backup-apacheconf

This should download the apache2.conf file to you local machine, into the same directory as your build file.

The above task requires that you have scp installed on your machine, which is pretty likely if you’re using OS X or Linux. Ant comes with an inbuilt scp task, but it requires you to install a separate java library. If you’re happy doing that then you can write tasks like:

pre.

<?xml version=“1.0” encoding=“UTF-8”?>

Less CSS

Ruby people really don’t like CSS do they? But Less is actually pretty cool. It’s basically an attempt to bootstrap features, specifically Variables, Mixins, Operations and Nested Rules, into CSS. The best part about this is it uses CSS syntax and a simple one step compiler. I’d be interested to know what the folks at the W3C think about this.

So for instance you can do:

pre. /* LESS */ brand_color: #4D926F; #header { color:brand_color; } h2 { color: @brand_color; }

and compile it down to:

pre. /* CSS */ #header { color: #4D926F; } h2 { color: #4D926F; }

Message Queues at Cambridge Geek Night

Last night was the first Cambridge Geek Night and saw 35 people or so fill a room above a pub to listen to a few short talks and converse with fellow geeks. I had the pleasure of giving the first talk, a short introduction to using message queues for web developers.

I got lots of good questions from interested people and by the sounds of things it had the desired effect - for people unfamiliar with using a message queue to go out and have a play with some of the cool software available to solve your problems.

Overall the night was definitely a success. Suitably geeky conversations. A chance to meet new people as well as old friends. Good job Vero and David for organising the event and here’s to the next one.

How to Decide on Your Next Programming Language

Neil Crosby got me thinking yesterday about which language to learn/play with next by tweeting

so, lovely people of the interwebs. What webly language should I be spending my time learning then? Right now, I’m all about the PHP.

Neil appears to have gone for Python, but more specifically I’m interested in how you decide what to learn next? And improving the likelihood of you seeing it through and being able to add it to you toolbox. Personally I’ve messed around with a wide range of languages but I would say I’m proficient in only a few of those.

So here goes with a list of questions to ask yourself.

  • Do you have a small pet project you can use the new language on straight away?
  • Is the language increasing or decreasing in popularity?
  • Does the language overlap with what you already know in terms of applicability?
  • Is it a different style of programming to what you’re used to? Say a pure functional language if all you normally do is object orientated.
  • Is the language in demand in the jobs market? If not why not?
  • Could you use the language in your current job if you were allowed to?
  • What are the learning materials like? Are there books or websites that teach the language without passing on bad practices.
  • What is the community around a language like? Do they have an IRC room where beginners can ask questions without being mocked?
  • Does the language have an interactive mode? Sorry but I find anything without to be a chore to learn.
  • What is it going to cost you in terms of time? Remember some languages are bigger that others.
  • What is it going to cost you in terms of money? Do you need specialist software, or hardware, or licenses?
  • Do you have friends or acquaintances who use the language and who will help you out with pointing you in the right direction of resources or helping review your code?
  • Do you have somewhere you can go to meet other people who write the language. That might be a formal user group or it might just be a more general pub meetup that you know a few people attend.
  • How is the language represented on GitHub? Seeing what other people build and being able to read working code is hugely useful when learning anything.

I don’t think all of these apply to everyone or apply all the time, but it’s worth considering and rejecting them when they don’t.

Feel free to disagree in the comments or, even better, add extra ones. Or alternatively just cut to the chase and tell me what I should learn next.

garethrushgrove.com update

I’ve updated my vanity domain at garethrushgrove.com with a bit of information in case anyone might be interested in my services.

my personal site

The short version is I’m on the lookout for future projects, probably of a freelance or contract basis but if it’s particularly interesting then maybe a full time position. Basically I’m in quite a nice position and able to wander about a bit looking for something cool to do.

If you reading this site then you know what floats my boat. Python, testing, automation, system design, maybe get into Ruby or another language properly, etc. I’d particularly like to help people get started with testing, continuous integration or automated deployment and the like.

Cambridge Geek Night

It’s the first Cambridge Geek Night next week, on Wednesday the 10th to be precise. You can find more information on Upcoming or on the Cambridge Geek Night blog. The event also has a twitter feed to keep up with goings on. If you’re in Cambridge you don’t want to miss the perfect combination of geeks and beer.

Testing WSGI Application with Urltest

I found myself wanted something to make writing high level, functional tests for WSGI application easier and quicker. If I liked the term I’d call it a domain specific language for testing URLs. Basically I found myself writing a lot of tests like:

pre. def test_404_handler(self): response = self.app.get(‘/does-not-exist’, expect_errors=True) self.assertEquals(“404 Not Found”, response.status)

Testing more than a view URLs like this got boring quickly. What I wanted was a short hand syntax for defining this sort of simple test and then running them all individually. So was born Urltest. It uses the rather nifty Webtest module and hooks into unittest from the standard library. You’re test script then looks a little like:

pre. #!/usr/bin/env python from example_app import application from urltest import verify_urls if name == “main“: urls = ( {‘url’:“/”, ‘code’:200}, {‘url’:“/bob”, ‘code’:200}, {‘url’:“/jim”, ‘code’:404}, {‘url’:“/jim”, ‘method’: “POST”, ‘code’:405}, ) verify_urls(urls, application)

Let me know if you use it as at the moment this is works for me ware, although it’s reasonably well tested and commented.

Another Glue Python Framework - MNML

Although still a big fan of Django, but for some problems I’m finding more and more cases where I prefer less code and more freedom. My biggest issue for some types of problems being Django’s assumption that you’ll be using a relational database, or a database at all. Django wasn’t the reason I started using webapp for App Engine stuff, but in doing so I found that webapp often did all that I needed.

So when I small, non appengine project cropped up I started looking at the different options available and played with a few of them.

  • I played with Pylons but again got lost in code. I’ll probably play with Pylons more in the future and for bigger, team based, projects it looks a good mix of component parts and shared conventions.
  • Web.py - I’d used web.py before I started with Django (I even wrote a very basic PHP clone) and although I still like somethings about it, it felt like more code than was required for what I wanted.
  • Juno is similar in design to Sinatra but again it wasn’t really what I was after this time. I prefer separating my routing from my code and I’m not sure I like that it comes with it’s own templating engine.
  • Newf was more like it. Basically a hugely stripped down WSGI framework which provides the very basic building blocks. Something to build on perhaps?
  • MNML (by an ex-colleague Brad Wright) is build atop Newf adding a few more features and cleaning up some of the interfaces. My only problems here were that I prefer regex based routes and wanted individual methods for each HTTP verb. The former was a specific design decision Brad had made in order to be able to reverse routes, the later was on the todo list.

So, I set about forking MNML to create my own branch. I added extra comments as I was making my way through the code, wrote a few tests to checks thinks worked and allowed for pluggable routing mechanisms. MNML applications look a bit like the following:

pre. from mnml import RegexBasedApplication, RequestHandler, HttpResponse, development_server class HelloWorld(RequestHandler): def GET (self): return HttpResponse(”

Hello World

“) routes = ( (r’^/$‘, HelloWorld), ) application = RegexBasedApplication(routes) if name == ‘main‘: development_server(application)

If you want to use the token based routing you would substitute in something like the following:

pre. routes = ( (‘/’, Foo), (‘/myview/:stuff/‘, Bar) ) application = TokenBasedApplication(routes)

The best bit is that it’s only about 350 lines of code, a great deal of which is accounted for by comments. It’s also really quite fast - especially using something like spawning to run the WSGI application. The other thing I like is the ease with which you can add WSGI middleware into the mix.

So, if you have a small scale problem where simple and fast beats everything else then have a look and let me know what you think. It will take less time to read the code and tests than it will be read the introductory chapter on whichever larger framework you choose to look at.

Beyond Basic Web Development

I did a talk at the recent barcamp North East on web development tools. Specifically I wanted to talk about the fact that an awful lot of people just use the basic stack of tools they are familiar with. So Microsoft people will just use C#, MSSQL and ISS and lots of people just use PHP, MySQL and Apache. I’m not saying their is anything wrong with those tools, but if they are all you have in your tool box you’re limited how well designed your software can be.

I’d knocked the presentation together in my hotel room pretty quickly before heading down to barcamp and the lack of an internet connection meant I didn’t have links and didn’t cover a few tools I should have. It did seem to have the desired result in any case as several people spoke to me afterwards about wanting to use one of the tools I mentioned for something specific.

We also had a good discussion afterwards and people mentioned a few other tools.

Now it’s possible to spend too much time playing with small tools that are likely to be peripheral to the bulk of your application. But the number of stories I’ve head of people writing their own messages queuing systems in PHP, or using PHPMyAdmin as an application admin interface or ignoring the fact that their fancy new application only supports a few people at once.