diff options
Diffstat (limited to 'bin/manage-slaves')
-rwxr-xr-x | bin/manage-slaves | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/bin/manage-slaves b/bin/manage-slaves new file mode 100755 index 0000000..cfa8c4d --- /dev/null +++ b/bin/manage-slaves @@ -0,0 +1,123 @@ +#!/bin/sh + +# shellcheck source=../lib/load-configuration +. "${0%/*}/../lib/load-configuration" + +# shellcheck disable=SC2016 +usage() { + >&2 echo '' + >&2 echo 'manage-slaves $action [parameters]: manage the list of build slaves' + >&2 echo '' + >&2 echo 'possible actions:' + >&2 echo ' add $name $owner $ssh-key-fingerprint' + >&2 echo ' disable $name' + >&2 echo ' enable $name' + exit 1 +} + +if [ $# -eq 0 ]; then + usage +fi + +if errors=$( + printf '%s\n' "$@" | \ + grep -vx '[-0-9a-zA-Z=+/]\+' +); then + >&2 echo 'The following parameters contain invalid characters:' + printf '%s\n' "${errors}" | \ + sed ' + s/^/"/ + s/$/"/ + ' >&2 + usage +fi + +case "$1" in + 'add') + shift + if [ $# -ne 3 ]; then + >&2 printf '"add" expects 3 parameters, %s were given\n' "$#" + usage + fi + person_id=$( + # shellcheck disable=SC2016 + { + printf 'SELECT `persons`.`id`' + printf ' FROM `persons`' + printf ' WHERE `persons`.`name`="%s"' \ + "${2}" + } | \ + mysql_run_query + ) + if [ -z "${person_id}" ]; then + >&2 printf 'Cannot find person "%s" in the database.\n' \ + "${2}" + usage + fi + duplicate=$( + # shellcheck disable=SC2016 + { + printf 'SELECT CONCAT(' + printf '"ssh-key: ",' + printf '`ssh_keys`.`fingerprint`' + printf ')' + printf ' FROM `ssh_keys`' + printf ' WHERE `ssh_keys`.`fingerprint`="%s";\n' \ + "${3}" + printf 'SELECT CONCAT(' + printf '"build-slave: ",' + printf '`build_slaves`.`name`' + printf ')' + printf ' FROM `build_slaves`' + printf ' WHERE `build_slaves`.`name`="%s";\n' \ + "${1}" + } | \ + mysql_run_query + ) + if [ -n "${duplicate}" ]; then + >&2 printf 'Some entry already existed in the database:\n%s\n' \ + "${duplicate}" + usage + fi + # shellcheck disable=SC2016 + { + printf 'INSERT INTO `ssh_keys`(`owner`,`fingerprint`)' + printf ' VALUES' + printf ' (%s,"%s");\n' \ + "${person_id}" \ + "${3}" + printf 'INSERT INTO `build_slaves`(' + printf '`name`,' + printf '`ssh_key`,' + printf '`is_sane`,' + printf '`access_allowed`' + printf ') VALUES' + printf '("%s",LAST_INSERT_ID(),1,1);\n' \ + "${1}" + } | \ + mysql_run_query + printf 'command="%s/bin/slave-build-connect %s" ssh-rsa %s %s@%s\n' \ + "${base_dir}" \ + "${1}" \ + "${3}" \ + "${2}" \ + "${1}" >> \ + ~/".ssh/authorized_keys" + ;; + 'disable'|'enable') + action="$1" + shift + if [ $# -ne 1 ]; then + >&2 printf '"%s" expects one parameter, %s were given\n' \ + "${action}" \ + "$#" + usage + fi + >&2 echo 'not yet implemented' + exit 1 + ;; + *) + >&2 printf 'unknown action "%s"\n' "$1" + usage + ;; +esac |