I have given some thought this weekend to the following problem: automate the update process for an application installed on several servers. I am considering the following elements:

  • an update has 2 parts: database update (via sql scripts) and application binaries updates
  • all servers are accessible via ssh

I’ve decided to write some thoughts regarding the process.

Database update

There are a few elements which have to be considered:

  • when connecting to a database identify the current version
  • determine which are the updates to apply
  • keep some record of success and most important of failure

Considering the above elements I think a “version” table should consist of at least the following fields: version, updated, status, comments. With this data the following pseudocode could lead to a successfull update:

  • find the last version updated
  • if the status is not ok then drop to manual update
  • else create a new version and set status as begin update
  • find all the commands to migrate to a new version
  • execute them
  • if error set status to error and log info in comments
  • else set status to ok

The commands can either be found in various directories or even in a single file with various separators identifying the version. This approach also has the advantage that you can migrate from time to time the alter commands in the base create script and just modify it’s version.

Application update

I think the first step is to create the ssh keys required for public key based access. There are a lot of resources showing how to do that using the ssh-keygen and copying the id_rsa.pub content to the ~/.ssh/authorized_keys on the remote host.

Execution of remote commands

The second step whould be to find a way to execute automatize the execution of commands via ssh. After some tests I’ve decided on using python-pexpect for that. The distribution commes with a ssh example /usr/share/doc/python-pexpect/examples/sshls.py for executing the ls command. This example has just to be slightly modified to work with public key ssh access.

Execution of local commands

In order to execute local commands the commands python module can be easily used in case you decide for a python script as it was my case but also some basic shell scripting should be enough.

File transfer

File transfer can be done by scp in an easy way but if the application contains large files which do not change very often (such as library files) and the bandwidth is of concern the rsync over ssh can be used to speed up this process. The rsync command can very good look like this:

rsync -axv --delete -e 'ssh -p 22' /opt/my-app/ app@remote-server:/opt/my-app/

It will use the public key access of ssh and it will be much faster than copying everything with scp.

In conclusion considering these simple aspects a simple update mechanism can simplify the work a lot when dealing with lots of servers.

Comments:

James Crissilv -

Si tot mi se pare o limba sau un limbaj pe care nu cred ca il voi invata vreodata. O zi minunata iti doresc


len -

Fiecare cu limbajul lui. Apropo de limbaje din astea cel mai important mi se pare nu omul care cunoaste limbajul ci cel care stie sa-l traduca.