Debian Codegraph @

tools/cloud-init-per

raw symbols SCIP
1 #!/bin/shscip-shell filesystem . . `/bin/sh`.
2 # This file is part of cloud-init. See LICENSE file for license information.
3
4 DATA_PRE="/var/lib/cloud/sem/bootperscip-shell filesystem . . `/var/lib/cloud/sem/bootper`."
5 INST_PRE="/var/lib/cloud/instance/sem/bootperscip-shell filesystem . . `/var/lib/cloud/instance/sem/bootper`."
6
7 Usagescip-shell shell shell-project 0.0.0 Usage().() {
8 catscip-shell system . . cat. << EOF
9 Usage: ${0##*/} frequency name cmd [ arg1 [ arg2 [ ... ] ]
10 run cmd with arguments provided.
11
12 This utility can make it easier to use boothooks or bootcmd
13 on a per "once" or "always" basis.
14
15 If frequency is:
16 * once: run only once (do not re-run for new instance-id)
17 * instance: run only the first boot for a given instance-id
18 * always: run every boot
19
20 EOF
21 }
22 errorscip-shell shell shell-project 0.0.0 error().() { echoscip-shell system . . echo. "$@" 1>&2; }
23 failscip-shell shell shell-project 0.0.0 fail().() {
24 [scip-shell system . . `[`. $# -eq 0 ] || errorscip-shell shell shell-project 0.0.0 error(). "$@"
25 exit 1
26 }
27
28 # support the old 'cloud-init-run-module freq name "execute" cmd arg1'
29 # if < 3 arguments, it will fail below on usage.
30 if [scip-shell system . . `[`. "${0##*/}" = "cloud-init-run-module" ]; then
31 if [scip-shell system . . `[`. $# -le 2 -o "$3" = "execute" ]; then
32 errorscip-shell shell shell-project 0.0.0 error(). "Warning: ${0##*/} is deprecated. Please use cloud-init-per."
33 freq=$1
34 name=$2
35 [scip-shell system . . `[`. $# -le 2 ] || shift 3
36 set -- "$freq" "$name" "$@"
37 else
38 failscip-shell shell shell-project 0.0.0 fail(). "legacy cloud-init-run-module only supported with module 'execute'"
39 fi
40 fi
41
42 [scip-shell system . . `[`. "$1" = "-h" -o "$1" = "--help" ] && {
43 Usagescip-shell shell shell-project 0.0.0 Usage().
44 exit 0
45 }
46 [scip-shell system . . `[`. $# -ge 3 ] || {
47 Usagescip-shell shell shell-project 0.0.0 Usage(). 1>&2
48 exit 1
49 }
50 freq=$1
51 name=$(echoscip-shell system . . echo. $2 | sedscip-shell system . . sed. 's/-/_/g')
52 shift 2
53
54 [scip-shell system . . `[`. "${name#*/}" = "${name}" ] || failscip-shell shell shell-project 0.0.0 fail(). "name cannot contain a /"
55 [scip-shell system . . `[`. "$(idscip-shell system . . id. -u)" = "0" ] || failscip-shell shell shell-project 0.0.0 fail(). "must be root"
56
57 case "$freq" in
58 once | always) sem="${DATA_PRE}.$name.$freq" ;;
59 instance) sem="${INST_PRE}.$name.$freq" ;;
60 *)
61 Usagescip-shell shell shell-project 0.0.0 Usage(). 1>&2
62 failscip-shell shell shell-project 0.0.0 fail(). "invalid frequency: $freq"
63 ;;
64 esac
65
66 [scip-shell system . . `[`. -d "${sem%/*}" ] || mkdirscip-shell system . . mkdir. -p "${sem%/*}" ||
67 failscip-shell shell shell-project 0.0.0 fail(). "failed to make directory for ${sem}"
68
69 # Rename legacy sem files with dashes in their names. Do not overwrite existing
70 # sem files to prevent clobbering those which may have been created from calls
71 # outside of cloud-init.
72 sem_legacy=$(echoscip-shell system . . echo. $sem | sedscip-shell system . . sed. 's/_/-/g')
73 [scip-shell system . . `[`. "$sem" != "$sem_legacy" -a -e "$sem_legacy" ] && mvscip-shell system . . mv. -n "$sem_legacy" "$sem"
74
75 [scip-shell system . . `[`. "$freq" != "always" -a -e "$sem" ] && exit 0
76 "$@"
77 ret=$?
78 printfscip-shell system . . printf. "%s\t%s\n" "$ret" "$(datescip-shell system . . date. +%s)" > "$sem" ||
79 failscip-shell shell shell-project 0.0.0 fail(). "failed to write to $sem"
80 exit $ret