Sunday, October 16, 2011

Upstart job for PostgreSQL 9.1 on Ubuntu 11.10

In line with the recent Upstart theme, he's a script for PostgreSQL 9.1 on Ubuntu 11.10:

Save this to /etc/init/postgresql.conf and delete the symlinks from /etc/rc#.d/ to disable the SysV scripts.

Using Upstart with RabbitMQ on Ubuntu 11.10

I've been tinkering with the idea of using Upstart to control celery processes for a Web site. I plan on using user jobs and hooking the rabbitmq-server and postgresql events to start and stop my celery instances. Unfortunately, on Ubuntu 11.10 RabbitMQ does not come with an Upstart job, but rather a SysV script.

Here's some instructions I've put together to convert it to using Upstart.

Install rabbitmq:

$ sudo apt-get install rabbitmq-server

It will automatically be started, so we first want to shut it down:

$ sudo /etc/init.d/rabbitmq-server stop

Now swap out the built-in /etc/init.d scripts for the Upstart job:

$ sudo rm /etc/rc0.d/K20rabbitmq-server \
/etc/rc1.d/K20rabbitmq-server \
/etc/rc2.d/S20rabbitmq-server \
/etc/rc3.d/S20rabbitmq-server \
/etc/rc4.d/S20rabbitmq-server \
/etc/rc5.d/S20rabbitmq-server \
/etc/rc6.d/K20rabbitmq-server

Put the following in /etc/init/rabbitmq-server.conf:

description "RabbitMQ Server"
author  "RabbitMQ"

start on runlevel [2345]
stop on runlevel [016]
respawn

exec /usr/sbin/rabbitmq-server > /var/log/rabbitmq/startup_log \
                              2> /var/log/rabbitmq/startup_err
post-start exec /usr/sbin/rabbitmqctl wait >/dev/null 2>&1

And you're done. You can now use:

sudo start rabbitmq-server

Notable differences between this job and the SysV script:
  • A lock file is not support (it was disabled by default in the SysV script anyway)
  • Shutdown is achieved via SIGTERM, rather than using rabbitmqctl stop. As a side effect, the /var/log/rabbitmq/shutdown_{log, err} files are not used.

Saturday, October 15, 2011

Upstart user jobs on Ubuntu 11.10

Recently I've been exploring Upstart's user jobs functionality. User jobs allow non-root users to have their own jobs in ~/.init/ that they can control.

The first thing I did was to create a simple job:

task

script
    sleep 5
end script

This job simply blocks for five seconds, which allows me to test whether user jobs are working properly. To use it I saved it to ~/.init/my-test-job.conf, and then started it via start my-test-job.

Unfortunately by default on Ubuntu 11.10, user jobs are disabled, which meant the start command failed with the error:

start: Rejected send message, 1 matched rules; type="method_call", sender=":1.5" (uid=1000 pid=1655 comm="start my-test-job ") interface="com.ubuntu.Upstart0_6.Job" member="Start" error name="(unset)" requested_reply="0" destination="com.ubuntu.Upstart" (uid=0 pid=1 comm="/sbin/init")

To enable user jobs, I had to edit /etc/dbus-1/system.d/Upstart.conf and change it to:


The original DBus configuration is far more restrictive in what messages it allows to reach Upstart (basically read/write for root and read for everyone else). The above configuration (taken from Upstart's source code) allows any user to control Upstart. This does seem like a security problem, but apparently Upstart can handle user permissions internally.

After making this change, I was able to start my job successfully:

$ start my-test-job
my-test-job stop/waiting

Documentation is pretty scarce, but there's a small section in the man page that's worth checking out (search for User Jobs).

Edit:

If you want user job start on stanzas to be honored, check out my more recent blog post about it