summaryrefslogtreecommitdiff
path: root/lib/intentions-queue
blob: f7c9a4f214951aa434bb8a2040c3d79972d6a1d2 (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
#!/bin/sh

# contains functions related to the intentions-queue

# shellcheck disable=SC2039

if [ -z "${base_dir}" ]; then
  # just to make shellcheck happy
  . '../lib/load-configuration'
fi

# TODO: Create an intentions-queue: It should handle all mysql and
# master mirror interactions which interact with each other or other
# local actions (repo-add, repo-remove, ...).
# Design idea: Create enumerated shell scripts which do the right thing
# and remove themself after successful execution.
# return-assignment would create the following scripts:
# 1. download database from mirror
# 2. update package database
# 3. update mysql database
# 4. upload database and package to mirror
# 5. clean up temporary files
# db-update would create the following scripts (after getting the usual
# database lock and determining what needs to be done):
# 1. download database from mirror
# 2. update package database
# 3. update mysql database
# 4. upload database to mirror
# 5. update packages on mirror
# 6. clean up temporary files

# intent_something
# create an intention, provided on stdin
# note, that this function is not thread-safe - use the lock
# $package_database_lock_file externally
intent_something() {
  local next_number
  next_number=$((
    $(
      find "${intentions_directory}" \
        -maxdepth 1 \
        -type f \
        -name 'intention.*' \
        -printf '%f\n' \
      | sed '
        s/^intention\.//
        t
        d
      ' \
      | sort -nr \
      | grep -xm1 '[0-9]\+' \
      || echo 0
    )+1
  ))
  {
    printf '%s\n' \
      '#!/bin/sh' \
      'set -e'
    cat
    printf 'rm "%s"\n' "${intentions_directory}/intention.${next_number}"
  } > "${intentions_directory}/intention.${next_number}"
  chmod +x "${intentions_directory}/intention.${next_number}"
}

# execute_intention
# executes the next intention
execute_intention() {
  local next_number
  next_number=$(
    find "${intentions_directory}" \
      -maxdepth 1 \
      -type f \
      -name 'intention.*' \
      -printf '%f\n' \
    | sed '
      s/^intention\.//
      t
      d
    ' \
    | sort -n \
    | grep -xm1 '[0-9]\+'
  ) || return 0
  "${intentions_directory}/intention.${next_number}"
}

# intentions_left
# check if there are undone intentions left
# return 0 if there is something left to do
# return 1 if nothing is queued
intentions_left() {
  find "${intentions_directory}" \
    -maxdepth 1 \
    -type f \
    -name 'intention.*' \
    -printf '%f\n' \
  | sed '
    s/^intention\.//
    t
    d
  ' \
  | sort -n \
  | grep -qxm1 '[0-9]\+'
}

# execute_all_intentions
# executes all intentions
execute_all_intentions() {
  while intentions_left; do
    execute_intention
  done
}