Allow non-root users to restart the Aegir queue daemon

I can no longer imagine using Aegir regularly without the Hosting Queue Daemon. For those who aren't aware, this was a contrib module (hosting_queue_runner) for the 1.x version of Aegir, but improves the user experience so much that we've moved it into core for the 2.x series.

It's installed by default with the 2.x Debian packages, and the accompanying README does a good job explaining how to install it,. It certainly deserves a handbook page too, though I haven't been able to track one down...

One challenge with it, though, is that it's prone to crashing. There are a number of work-arounds for this, including using Supervisord, Puppet or cron to ensure the daemon is running or restarted regularly. We've also made lots of progress recently in improving its resilience, but it can still be crashed by introducing PHP errors into Drush, for example.

So, I had a need recently to allow non-root users to restart the queue daemon. These users have access to operate as the 'aegir' user, and so I added an entry in the sudoers file to allow aegir to restart the daemon:

# cat /etc/sudoers.d/queued-aegir
aegir ALL=NOPASSWD: /etc/init.d/hosting-queued

However, when trying to run the command as the aegir user, we get the following error:

$ /etc/init.d/hosting-queued restart
Restarting Aegir queue daemon... /etc/init.d/hosting-queued: 57: kill: Operation not permitted
rm: cannot remove `/var/run/': Permission denied
/etc/init.d/hosting-queued: 47: /etc/init.d/hosting-queued: cannot create /var/run/ Permission denied
su: must be run from a terminal

The problem is just that the aegir user doesn't have write access to /var/run. This stumped me for a while, as the PID file is created and destroyed from this script, but broadening write access to /var/run is a bad idea. The trick is to create a directory under /var/run that aegir will have write access to:

# mkdir /var/run/aegir
chown aegir:aegir /var/run/aegir
chmod 755 /var/run/aegir

Then we need to have the init script use the new directory. This is simply a matter of editing /etc/init.d/hosting-queued:

DAEMON_ARGS="--quiet @hostmaster $NAME" # Arguments to run the daemon with

Now aegir should be able to restart the queue daemon properly:

$ /etc/init.d/hosting-queued restart
Restarting Aegir queue daemon: hosting-queued.