Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/bin/manage-slaves
blob: 3dbd83ac0c53a05cee615fac698bf94b945ced70 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/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 '  list'
  >&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
  'list')
    infos=$(
      # shellcheck disable=SC2016
      {
        printf 'SELECT'
        printf ' name, CASE WHEN access_allowed = 1 THEN "enabled" ELSE "disabled" END '
        printf ' name, CASE WHEN is_sane = 1 THEN "sane" ELSE "not sane" END '
        printf ' FROM build_slaves'
        printf ';\n'
      } | \
        mysql_run_query | \
        tr '\t' ' '
    )
    echo "${infos[@]}"
    ;;
  '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
    infos=$(
      # shellcheck disable=SC2016
      {
        printf 'SELECT'
        printf ' `build_slaves`.`id`,'
        printf 'CONCAT('
          printf '"command=\\\"%s/bin/slave-build-connect ",' \
            "${base_dir}"
          printf '`build_slaves`.`name`,'
          printf '"\\\" ssh-rsa ",'
          printf '`ssh_keys`.`fingerprint`,'
          printf '" ",'
          printf '`persons`.`name`,'
          printf '"@",'
          printf '`build_slaves`.`name`'
        printf ')'
        printf ' FROM `build_slaves`'
        mysql_join_build_slaves_ssh_keys
        mysql_join_ssh_keys_persons
        printf ' WHERE `build_slaves`.`name`="%s"' \
          "${1}"
        printf ' AND `build_slaves`.`access_allowed`='
        if [ "${action}" = 'disable' ]; then
          printf 1
        else
          printf 0
        fi
        printf ';\n'
      } | \
        mysql_run_query | \
        tr '\t' ' '
    )
    if [ -z "${infos}" ]; then
      >&2 printf 'Cannot find build slave "%s" in the database to %s.\n' \
        "${1}" \
        "${action}"
      usage
    fi
    if [ "${action}" = 'disable' ]; then
      grep -vF "$(
        printf '%s\n' "${infos}" | \
          sed '
            s/^\S\+ //
            s/\s\+\S\+$//
          '
      )" ~/".ssh/authorized_keys" | \
        sponge ~/".ssh/authorized_keys"
    else
      printf '%s\n' "${infos}" | \
        sed 's/^\S\+ //' >> \
        ~/".ssh/authorized_keys"
    fi
    # shellcheck disable=SC2016
    {
      printf 'UPDATE `build_slaves`'
      printf ' SET `build_slaves`.`access_allowed`='
      if [ "${action}" = 'disable' ]; then
        printf 0
      else
        printf 1
      fi
      printf ' WHERE `build_slaves`.`id`=%s;\n' \
        "${infos%% *}"
    } | \
      mysql_run_query
  ;;
  *)
    >&2 printf 'unknown action "%s"\n' "$1"
    usage
  ;;
esac