This file is indexed.

/usr/share/debci/bin/debci-batch is in debci 1.0.2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/bin/sh

set -eu

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

Options:

  -f, --force               Force test run on packages, even if no package in
                            its dependency chain changed. A package will still
                            not be tested if it was already tested today.
  --offline                 Puts debci-batch offline. New test runs will not be
                            started.
  --online                  Puts debci-batch online. New test runs will be
                            started normally.

$@
EOF
}

short_options='f'
long_options='force,offline,online'

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

tmp_dir=$(mktemp -d)
cleanup() {
  if [ -d "$tmp_dir" ]; then
    rm -rf "$tmp_dir"
  fi
}
trap cleanup INT TERM EXIT

one_month_ago="${tmp_dir}/one_month_ago"
touch -d '1 month ago' "${one_month_ago}"


run() {
  log "I: debci-batch started $(date)"

  log "I: building/updating chdist for $debci_suite"
  run_with_exclusive_lock "$debci_chdist_lock" debci-setup-chdist

  log "I: start processing of all packages"

  process_all_packages

  log "I: debci-batch finished $(date)"
}

all_packages_with_fastest_first() {
  # list of all packages, sorted by duration of their last test suite run
  #
  # new package have "unknown" as duration, and will sort first due to how
  # sort(1) works. We acknowledge that behavior and give new packages a chance
  # to be efficient. If they are not, they will be placed at the end of the
  # queue for the next run.
  debci-status --field duration_seconds --all | sort -k 2,2 -n | cut -d ' ' -f 1
}

process_all_packages() {
  local start=$(date +%s)

  # TODO: we need something more flexible than $debci_backend here -- look at
  # tests' isolation restrictions
  amqp-declare-queue --url ${debci_amqp_server} --durable -q $debci_amqp_queue >/dev/null

  # determine packages which need to be tested and request tests
  for pkg in $(all_packages_with_fastest_first); do
    if (! already_enqueued $pkg) && needs_processing $pkg; then
      record_enqueued "$pkg"
      debci-enqueue "$pkg"
    else
      report_status "$pkg" "skip"
    fi
  done
}

needs_processing() {
  status_dir=$(status_dir_for_package "$pkg")
  last_status=$(debci-status "$pkg")
  mkdir -p "${status_dir}"

  debci-list-dependencies "$pkg" > "$tmp_dir/${pkg}-deps.txt"
  reason="$status_dir/reason.txt"

  run=1

  if [ "$last_status" = 'tmpfail' ]; then
    run=0
    echo "∙ Retrying run since last attempt failed" >> $reason
  fi

  if [ -n "$force" ]; then
    run=0
    echo "∙ Forced test run for $pkg" >> $reason
  fi

  if [ -f "${status_dir}/latest.json" -a "${status_dir}/latest.json" -ot "${one_month_ago}" ]; then
    run=0
    echo '∙ Forcing test run after 1 month without one' >> $reason
  fi

  if [ -f "$status_dir/dependencies.txt" ]; then
    if diff -u --label last-run/dependencies.txt "$status_dir/dependencies.txt" --label current-run/dependencies.txt "$tmp_dir/${pkg}-deps.txt" > "$tmp_dir/${pkg}-changed-deps.diff"; then
      : # no need to run tests
    else
      run=0
      echo "∙ There were changes in the dependency chain since last test run:" >> $reason
      cat "$tmp_dir/${pkg}-changed-deps.diff" >> $reason
    fi
  else
    run=0
    echo "∙ First test run for $pkg" >> $reason
  fi

  if [ "$run" -eq 0 ]; then
    cp "$tmp_dir/${pkg}-deps.txt" "${status_dir}/dependencies.txt"
  fi

  return $run
}

already_enqueued() {
  local pkg="$1"
  # XXX if you change the line below also change in record_enqueued()
  local queue_marker="$(status_dir_for_package "$pkg")/queue.txt"
  local last_result="$(status_dir_for_package "$pkg")/latest.json"
  if [ -e "$queue_marker" ]; then
    if [ -e "$last_result" ]; then
      # already enqueued if last result is older than last request
      test "$last_result" -ot "$queue_marker"
    else
      # already enqueued, just not finished yet
      return 0
    fi
  else
    # never enqueued before
    return 1
  fi
}

record_enqueued() {
  local pkg="$1"
  # XXX if you change the line below also change in already_enqueued()
  local queue_marker="$(status_dir_for_package "$pkg")/queue.txt"
  mkdir -p "$(dirname "$queue_marker")"
  echo "Enqueued at $(date)" > "$queue_marker"
}

# default configuration
force=''
offline_marker="$debci_data_basedir/offline"

while true; do
  arg="$1"
  shift
  case "$arg" in
    -f|--force)
      force="$arg"
      ;;
    --offline)
      touch "${offline_marker}"
      exit 0
      ;;
    --online)
      rm -f "${offline_marker}"
      exit 0
      ;;
    --)
      break
      ;;
  esac
done

if [ -e "${offline_marker}" ]; then
  exit 0
fi

run_with_lock_or_exit "$debci_batch_lock" run "$@"