Building robust commandline tools with click and flask - Wojtek BurakiewiczΒΆ

Tags: python, django

(Summary of a talk at a Dutch Python meetup).

Wojtek likes simple tools. His presentation is about a recent tool they build: an internal tool that deploys python applications to servers with virtualenvs. It also configures nginx.

They based the architecture around microservices. So you basically write a simple script that talks to a couple of APIs that in turn talk to other APIs.

They started out with stash, artifactory, ansible, jenkins, supervisord and JIRA. All except JIRA have a nice API. Their deploy tool talked to those APIs. One problem was authentication. One tool needs user/pass, the other an auth token, the next an ssh key... Another problem was network segmentation. You don’t want every laptop to talk to your production environment...

The solution was to use one intermediary API. So the command line tool talks to the intermediary API which in turns talks to the APIs mentioned above.

Another advantage of an intermediary API is that you can unify the concepts. You can just talk about “host” and “application”, even though in Jenkins/ansible/etc it might be a “job” or a “machine.

You can also exchange components! You can switch from Stash to Bitbucket without the user of the deploy tool noticing.

They used flask. If you compare it to django:

  • When you install django, you get a house.
  • When you install Flask, you get a single brick.

There are all sorts of libraries you can use to get all the functionality you would get with django. Only you get to install and compose it yourself.

For the command line, he used click, a nice alternative to argparse (he dislikes argparse). From the homepage: Click is a Python package for creating beautiful command line interfaces in a composable way with as little code as necessary.

When you distribute command line tools, people will have older versions. So you need to get the users to upgrade in some way.

They did it in three ways:

  • By returning a special header from the API with the desired command line version. The command line tool can then ask the user to upgrade when needed.
  • The commnad line tool also passes its version to the API. If the API needs a newer version, it can return an error code.
  • If an update was needed, it would also print out the necessary pip command as an extra service.

Ongerelateerde reclame: bij ons python programmeren in hartje Utrecht? Mail me even :-)

blog comments powered by Disqus
 
vanrees.org logo

About me

My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):