To start with configuring my NAS to use the new blinkenlights, I thought I'd start with a really easy job: I plug in my iPod, a script runs to back it up, then the iPod gets unmounted. It's one of the simpler jobs to start with because the iPod is a simple block device and there's no encryption in play. For now, I'm also going to assume the LED Is going to be used exclusively for this job. In the future I will want many independent jobs to perhaps use the LED to signal things and figuring out how that will work is going to be much harder.
I'll skip over the journey and go straight to the working solution. I have a systemd job that is used to invoke a sync from the iPod as follows:
[Service] Type=oneshot ExecStart=/bin/mount /media/ipod ExecStart=/usr/local/bin/blinkstick --index 1 --limit 10 --set-color 33c280 ExecStart=/usr/bin/rsync ... ExecStop=/bin/umount /media/ipod ExecStop=/usr/local/bin/blinkstick --index 1 --limit 10 --set-color green [Install] WantedBy=dev-disk-by\x2duuid-A2EA\x2d96ED.device [Unit] OnFailure=blinkstick-fail.service
/media/ipod is a classic mount configured in
/etc/fstab. I've done this
rather than use the newer systemd
.mount units which sadly don't give you
enough hooks for running things after unmount or in the failure case. This
feels quite unnatural, much more "systemdy" would be to
Requires= the mount
unit, but I couldn't figure out an easy way to set the LED to green after the unmount.
I'm sure it's possible, but convoluted.
blinkstick command sets the LED to a colour to indicate "in progress". I explored some of the
blinkstick tool's options for a fading or throbbing colour but they didn't work very well. I'll take another look in the future. After the LED is set, the
backup job itself runs. The last
blinkstick command, which is only run if the
umount has succeeded, sets the LED to indicate "safe to unplug".
WantedBy here instructs systemd that when the iPod device-unit is
activated, it should activate my backup service. I can refer to the iPod
device-unit using this name based on the partition's UUID; this is not the
canonical device name that you see if you run
systemctl but it's much shorter
and crucially its stable, the canonical name depends on exactly where you plugged
it in and what other devices might have been connected at the same time.
If something fails, a second unit
blinkstick-fail.service gets activated. This
is very short:
[Service] ExecStart=/usr/local/bin/blinkstick --index 1 --limit 50 --set-color red
This simply sets the LED to be red.
Again it's a bit awkward that in 2 cases I'm setting the LED with a simple
Exec but in the third I have to activate a separate systemd service: this seems to be the nature of the beast. At least when I come to look at concurrent jobs all interacting with the LED, the failure case should be simple: red trumps any other activity, user must go and check what's up.