File: debci-worker

package info (click to toggle)
debci 3.13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,656 kB
  • sloc: ruby: 6,516; sh: 2,437; javascript: 100; makefile: 92; perl: 11
file content (177 lines) | stat: -rwxr-xr-x 4,245 bytes parent folder | download | duplicates (2)
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
#!/bin/sh
# Consume test requests from AMQP queue and feed them to debci-test
set -eu

short_options='c:t:'
long_options='count:,tag:,do-request'

usage() {
  cat <<EOF
usage: debci-worker [OPTIONS]

Options:
  -c COUNT, --count COUNT   Exit after processing COUNT requests
  -t TAG, --tag TAG         listen to queues which require this platform tag
                            (can be specified multiple times)

$@
EOF
}

debci_base_dir=$(readlink -f $(dirname $(readlink -f $0))/..)
. $debci_base_dir/lib/environment.sh
. $debci_base_dir/lib/functions.sh

tags=''
count=''

# Process one request. Read the AMQP message from stdin.
do_request() {
  local request
  local pkg
  local suite
  local opts

  read request || true  # we expect EOF and thus read to fail
  set -- $request
  if [ $# -eq 0 ]; then
    return
  fi
  pkg="$1"
  suite="${2:-}"
  if [ -n "$suite" ]; then
    shift 2
  else
    suite="$debci_suite"
    shift 1
  fi

  releases=""
  opts=""
  run_id=0
  for param in $@; do
    case "$param" in
      trigger:*)
        opts="$opts --trigger=${param#trigger:}"
        ;;
      pin-packages:*)
        arg=${param#pin-packages:}
        opts="$opts --pin-packages=$arg"
        ;;
      run-id:*)
        arg=${param#run-id:}
        run_id="${arg}"
        opts="$opts --run-id=${run_id}"
        ;;
      extra-apt-source:*)
        arg=${param#extra-apt-source:}
        opts="$opts --extra-apt-source=$arg"
        ;;
      signing-key:*)
        arg=${param#signing-key:}
        opts="$opts --signing-key=$arg"
        ;;
      *)
        echo "Unknown test parameter: $param" >&2
        ;;
    esac
  done

  if ! (echo "$pkg" | grep -q '^[a-z0-9.+-]\+$'); then
    log "W: invalid package name: $pkg, ignoring"
    return
  fi

  log "$pkg $suite/$debci_arch/$debci_backend (${run_id}) started"

  tmp_dir=$(mktemp --directory --tmpdir debci-worker-${run_id}-XXXXXXXXXX)
  trap 'rm -rf ${tmp_dir}' INT TERM EXIT

  # run the test
  local result_dir="$(
    debci-test \
      --quiet \
      --data-dir "$tmp_dir" \
      --suite "$suite" \
      --arch "${debci_arch}" \
      --backend "${debci_backend}" \
      --print-output \
      $opts \
      "$pkg"
  )"

  if [ ! -s "$result_dir/exitcode" ]; then
    log_error "E: Test for package $pkg produced no exit code, aborting"
    exit 2
  fi

  # debci_suite has to passed in to report_status because the worker is running
  # using the "default" suite which is usually unstable, which might be
  # different from the suite in which the test has actually just been executed.
  case $(cat "$result_dir/exitcode") in
      0|2)
        status=pass
        ;;
      4|6|12|14)
        status=fail
        ;;
      8)
        status=neutral
        ;;
      *)
        status=tmpfail
        ;;
  esac
  log "$pkg $suite/$debci_arch/$debci_backend (${run_id}) ${status}"


  # move result into results dir and let debci-publisher send the results
  result_dir=${result_dir##$tmp_dir/}
  result_file="${tmp_dir}/${run_id}.${debci_backend}.tar.gz"
  ( cd $tmp_dir && tar czf "${result_file}" "$result_dir" )
  mkdir -p "${debci_results_dir}"
  mv "${result_file}" "${debci_results_dir}/"
  stamp_file="$(basename "${result_file}" .tar.gz).stamp"
  touch "${debci_results_dir}/${stamp_file}"
}

# parse CLI arguments
while true; do
  case "$1" in
    -t|--tag)
      tags="${tags}_$2"
      shift 2
      ;;
    -c|--count)
      count="--count $2"
      shift 2
      ;;
    --do-request)
      do_request
      exit 0
      ;;
    *)
      break
      ;;
  esac
done

amqp-declare-queue \
  --url="$debci_amqp_server" \
  $debci_amqp_tools_options \
  --durable \
  --queue="$debci_amqp_results_queue" > /dev/null

# if the user calls this, we run forever with consuming messages;
# amqp-consume calls ourselves with the (hidden) --do-request option
amqp_queue="${debci_amqp_queue}${tags}"
log "I: Connecting to AMQP queue $amqp_queue on ${debci_amqp_server_display}"
debci amqp declare-queue --arch="${debci_arch}" --backend="${debci_backend}"
exec amqp-consume \
  --url ${debci_amqp_server} \
  $debci_amqp_tools_options \
  --queue=$amqp_queue \
  --prefetch-count 1 \
  $count \
  -- \
  $0 --do-request --arch="${debci_arch}" --backend="${debci_backend}"