Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cba250f

Browse files
committed
Check whether locksmithctl is installed
When running tasks that depend on it. The package is uninstalled by Puppet when `govuk_unattended_reboots::enabled` is false (default). This results in the following output when a task is run: (fabric)➜ fabric-scripts git:(master) fab preview locksmith.status [etcd-1.management] run: /usr/bin/locksmithctl -endpoint='http://etcd-1.management:4001,http://etcd-2.management:4001,http://etcd-3.management:4001' status [etcd-1.management] out: /bin/bash: /usr/bin/locksmithctl: No such file or directory [etcd-1.management] out: Fatal error: run() received nonzero return code 127 while executing! Requested: /usr/bin/locksmithctl -endpoint='http://etcd-1.management:4001,http://etcd-2.management:4001,http://etcd-3.management:4001' status Executed: /bin/bash -l -c "/usr/bin/locksmithctl -endpoint='http://etcd-1.management:4001,http://etcd-2.management:4001,http://etcd-3.management:4001' status" Aborting. Disconnecting from etcd-1.management... done. Disconnecting from jumpbox.preview.alphagov.co.uk... done. By checking whether the command is present first we can provide a simpler error with more context: (fabric)➜ fabric-scripts git:(check_locksmithctl_installed) fab preview locksmith.status Fatal error: locksmithctl is not installed. Perhaps unattended_reboots are disabled? Aborting. Disconnecting from etcd-1.management... done. Disconnecting from jumpbox.preview.alphagov.co.uk... done. I thought about decorating this with `@runs_once` but actually we probably won't call locksmith multiple times in the same run, we'd want to check for every machine it's run on, and it's pretty cheap to check.
1 parent 43c8289 commit cba250f

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

locksmith.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
from fabric.api import run, task
2+
from fabric.utils import error
3+
import fabric.contrib.files
24
import util
35

46
etcd_cluster = 'http://etcd-1.management:4001,http://etcd-2.management:4001,http://etcd-3.management:4001'
7+
locksmithctl = '/usr/bin/locksmithctl'
8+
9+
def check_locksmithctl():
10+
if not fabric.contrib.files.exists(locksmithctl):
11+
error('locksmithctl is not installed. Perhaps unattended_reboots are disabled?')
512

613
@task
714
def status():
815
"""Get the status of locksmith"""
916
util.use_random_host('class-etcd')
10-
run("/usr/bin/locksmithctl -endpoint='{0}' status".format(etcd_cluster))
17+
check_locksmithctl()
18+
run("{0} -endpoint='{1}' status".format(locksmithctl, etcd_cluster))
1119

1220
@task
1321
def unlock(machine_name):
1422
"""Unlock a machine with locksmith"""
1523
util.use_random_host('class-etcd')
16-
run("/usr/bin/locksmithctl -endpoint='{0}' unlock '{1}'".format(etcd_cluster, machine_name))
24+
check_locksmithctl()
25+
run("{0} -endpoint='{1}' unlock '{2}'".format(locksmithctl, etcd_cluster, machine_name))

0 commit comments

Comments
 (0)