summaryrefslogtreecommitdiff
path: root/bin/build-master-status
blob: db118e61fd100a7f12936324dd6b53b8387c9566 (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
203
204
205
206
207
208
209
#!/bin/sh

# report about status of build master

# shellcheck source=../conf/default.conf
. "${0%/*}/../conf/default.conf"

# TODO: replace by build-master-status-from-mysql

usage() {
  >&2 echo ''
  >&2 echo 'build-master-status: report about status of build master'
  >&2 echo ''
  >&2 echo 'possible options:'
  >&2 echo '  -h|--help:'
  >&2 echo '    Show this help and exit.'
  [ -z "$1" ] && exit 1 || exit "$1"
}

eval set -- "$(
  getopt -o h \
    --long help \
    -n "$(basename "$0")" -- "$@" || \
  echo usage
)"

while true
do
  case "$1" in
    -h|--help)
      usage 0
    ;;
    --)
      shift
      break
    ;;
    *)
      >&2 echo 'Whoops, forgot to implement option "'"$1"'" internally.'
      exit 42
    ;;
  esac
  shift
done

if [ $# -ne 0 ]; then
  >&2 echo 'Too many arguments.'
  usage
fi

if [ -s "${work_dir}/build-master-sanity" ]; then
  >&2 echo 'Build master is not sane.'
  exit
fi

tmp_dir=$(mktemp -d 'tmp.build-master-status.XXXXXXXXXX' --tmpdir)
trap 'rm -rf --one-file-system "${tmp_dir}"' EXIT

find "${base_dir}/bin/" "${base_dir}/conf/" -type f \
  -exec grep -nHF '' '{}' \; | \
  awk '
    { print $0 }
    /^[^:]+:[0-9]+:\s*#\s*TODO:/{print ++i}
  ' | \
  sed -n '
    s/^\([^:]\+\):\([0-9]\+\):\s*#\s*TODO:\s*/\1\n\2\n/
    T
    N
    s/\n\(.*\)\n\([0-9]\+\)$/\n\2\n\1/
    :a
      N
      s/\n[^:\n]\+:[0-9]\+:[ \t]*#[ \t]*\(\S[^\n]*\)$/\n\1/
      ta
    s/\n[^:\n]\+:[0-9]\+:[^\n]*$/\n/
    p
  ' > "${tmp_dir}/todos"

if [ -s "${tmp_dir}/todos" ]; then
  sed '
    :a
    N
    /\n$/!ba
    s|^[^\n]*/\([^/\n]\+/[^/\n]\+\)\n\([0-9]\+\)\n\([0-9]\+\)\n|\1 \3 |
    s/\n$//
    s/\n/\\n/g
  ' -i "${tmp_dir}/todos"
  while read -r file line desc; do
    printf '%s %s %s\n' \
      "$(printf '%s' "${file}" | base64 -w0)" \
      "$(printf '%s' "${line}" | base64 -w0)" \
      "$(printf '%s' "${desc}" | base64 -w0)"
  done < \
    "${tmp_dir}/todos" | \
    sponge "${tmp_dir}/todos"
  # update todos
  # shellcheck disable=SC2016
  while read -r file line desc; do
    printf 'UPDATE IGNORE `todos`'
    printf ' SET `todos`.`line`=from_base64("%s")' \
      "${line}"
    printf ' WHERE `todos`.`file`=from_base64("%s")' \
      "${file}"
    printf ' AND `todos`.`description`=from_base64("%s");\n' \
      "${desc}"

    printf 'UPDATE IGNORE `todos`'
    printf ' SET `todos`.`description`=from_base64("%s")' \
      "${desc}"
    printf ' WHERE `todos`.`file`=from_base64("%s")' \
      "${file}"
    printf ' AND `todos`.`line`=from_base64("%s");\n' \
      "${line}"
  done < \
    "${tmp_dir}/todos" | \
    mysql_run_query
  # insert unfound todos
  # shellcheck disable=SC2016
  {
    printf 'SHOW CREATE TABLE `todos`' | \
      mysql_run_query | \
      sed '
        1s/^\S\+\s\+CREATE TABLE `todos` /CREATE TEMPORARY TABLE `td` /
      '
    printf ';\n'
    printf 'INSERT INTO `td` (`file`,`line`,`description`) VALUES '
    while read -r file line desc; do
      printf '('
      printf 'from_base64("%s"),' \
        "${file}" \
        "${line}" \
        "${desc}" | \
        sed 's/,$/),/'
    done < \
      "${tmp_dir}/todos" | \
      sed '
        s/,$//
      '
    printf ';\n'
    printf 'INSERT IGNORE INTO `todos` (`file`,`line`,`description`) '
    printf 'SELECT `td`.`file`,`td`.`line`,`td`.`description` '
    printf 'FROM `td` '
    printf 'WHERE NOT EXISTS ('
    printf 'SELECT * FROM `todos`'
    printf ' AND `td`.`%s`=`todos`.`%s`' \
      'file' 'file' \
      'line' 'line' \
      'description' 'description' | \
      sed 's/^ AND / WHERE /'
    printf ');\n'

    printf 'DELETE FROM `todos` WHERE NOT EXISTS ('
    printf 'SELECT * FROM `td`'
    printf ' AND `td`.`%s`=`todos`.`%s`' \
      'file' 'file' \
      'line' 'line' \
      'description' 'description' | \
      sed 's/^ AND / WHERE /'
    printf ');'
    printf 'DROP TABLE `td`;\n'
    printf 'DELETE FROM `todo_links` WHERE NOT EXISTS ('
    printf 'SELECT * FROM `todos` '
    printf 'WHERE `todos`.`id`=`todo_links`.`depending_on`'
    printf ') OR NOT EXISTS ('
    printf 'SELECT * FROM `todos` '
    printf 'WHERE `todos`.`id`=`todo_links`.`dependent`'
    printf ');\n'
  } | \
    mysql_run_query
  rm -f "${tmp_dir}/todos"
fi

{
  printf '%s\n' \
    '<html>' \
    '<head>' \
    '<title>log of ssh connections from build slaves</title>' \
    '</head>' \
    '<body>' \
    '<table>'
  printf '<tr>'
  printf '<th>%s</th>' \
    'time' \
    'build slave' \
    'command' \
    'arguments'
  printf '</tr>\n'
  if [ -r "${work_dir}/ssh-log" ]; then
    tac "${work_dir}/ssh-log" | \
      while read -r date time slave command arguments; do
        printf '<tr>'
        printf '<td>%s</td>' \
          "${date} ${time}" \
          "${slave}" \
          "${command}" \
          "${arguments}"
        printf '</tr>\n'
      done
  fi
  printf '%s\n' \
    '</table>' \
    '</body>' \
    '</html>'
} > \
  "${tmp_dir}/ssh-log.html"

find "${tmp_dir}" -maxdepth 1 -type f | \
  while read -r file; do
    cat "${file}" > \
      "${webserver_directory}/${file##*/}"
  done