summaryrefslogtreecommitdiffstatshomepage
path: root/scripts/maintenance-lib.sh
blob: 33e7e0760be25beba5505ca4649006a5385fa96a (plain) (blame)
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
#!/usr/bin/env bash

# Set args and maintenance_run for one domain of a maintenance request.
# shellcheck disable=SC2034
_maintenance_select() {
  local scope=$1 domain=$2 role raw target
  shift 2
  role=$(_machine_role) || return 1
  args=()
  maintenance_run=false
  if [ "$scope" = host ]; then
    _require_host || return 1
    args=("$@")
    maintenance_run=true
    return
  fi
  for raw in "$@"; do
    case "$raw" in
      /etc/* | etc/*)
        [ "$role" = host ] || {
          echo 'error: /etc paths require the host role' >&2
          return 1
        }
        target=etc
        ;;
      */*) target=home ;;
      *)
        echo "error: expected a file path: $raw" >&2
        return 1
        ;;
    esac
    [ "$target" != "$domain" ] || args+=("$raw")
  done
  if [ ${#args[@]} -gt 0 ] || { [ $# -eq 0 ] && { [ "$domain" = home ] || [ "$role" = host ]; }; }; then
    maintenance_run=true
  fi
  return 0
}