summaryrefslogtreecommitdiff
path: root/bin/manage-slaves
blob: cfa8c4dff3fad5e9aaca3327f59dc882ca44e033 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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