Debian Codegraph @

libexec/container/auto

raw symbols SCIP
1 #!/bin/sh
2
3 # Copyright (C) 2014-2026 Daniel Baumann <[email protected]>
4 #
5 # SPDX-License-Identifier: GPL-3.0+
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19
20 set -e
21
22 PROJECT="open-infrastructure"
23 SOFTWARE="compute-tools"
24 PROGRAM="container"
25 COMMAND="$(basename ${0})"
26
27 CONFIG="/etc/${SOFTWARE}/config"
28 HOOKS="/etc/${SOFTWARE}/hooks"
29
30 Parameters ()
31 {
32 GETOPT_LONGOPTIONS="force,start,stop,"
33 GETOPT_OPTIONS="f,s,t,"
34
35 PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${COMMAND} --options ${GETOPT_OPTIONS} --shell sh -- ${@})"
36
37 if [ "${?}" != "0" ]
38 then
39 echo "'${COMMAND}': getopt exit" >&2
40 exit 1
41 fi
42
43 eval set -- "${PARAMETERS}"
44
45 while true
46 do
47 case "${1}" in
48 -f|--force)
49 FORCE="true"
50 shift 1
51 ;;
52
53 -s|--start)
54 ACTION="start"
55 shift 1
56 ;;
57
58 -t|--stop)
59 ACTION="stop"
60 shift 1
61 ;;
62
63 --)
64 shift 1
65 break
66 ;;
67
68 *)
69 echo "'${COMMAND}': getopt error" >&2
70 exit 1
71 ;;
72 esac
73 done
74 }
75
76 Usage ()
77 {
78 echo "Usage: ${PROGRAM} ${COMMAND} -f|--force -s|--start -t|--stop" >&2
79 echo
80 echo "See ${COMMAND}(1), ${PROGRAM}(1) and ${PROJECT}(7) for more information."
81
82 exit 1
83 }
84
85 Parameters "${@}"
86
87 if [ -z "${ACTION}" ]
88 then
89 Usage
90 fi
91
92 HOST="$(hostname -f 2> /dev/null || hostname)"
93
94 # Pre hooks
95 for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}"
96 do
97 if [ -x "${FILE}" ]
98 then
99 "${FILE}"
100 fi
101 done
102
103 # Run
104 OPTIONS=""
105
106 case "${FORCE}" in
107 true)
108 OPTIONS="${OPTIONS} -f"
109 ;;
110 esac
111
112 for FILE in "${CONFIG}"/*.conf
113 do
114 if ! grep -Eqs "^ *cnt.container-server=${HOST}" "${FILE}"
115 then
116 continue
117 fi
118
119 CONTAINER="$(basename ${FILE} .conf)"
120 CNT_AUTO="$(grep -Es "^ *cnt.auto=" ${FILE} | awk -F= '{ print $2 }')"
121
122 case "${ACTION}" in
123 start)
124 case "${CNT_AUTO}" in
125 force-true)
126 OPTIONS="${OPTIONS} -f"
127
128 cnt ${ACTION} -n ${CONTAINER} ${OPTIONS} || true
129 ;;
130
131 last-on)
132 if grep -qs start "/var/lib/${SOFTWARE}/state/${CONTAINER}.run" || \
133 [ ! -e "/var/lib/${SOFTWARE}/state/${CONTAINER}.run" ]
134 then
135 cnt start -n ${CONTAINER} ${OPTIONS} -f || true
136 fi
137 ;;
138
139 last-off)
140 if grep -qs start "/var/lib/${SOFTWARE}/state/${CONTAINER}.run"
141 then
142 cnt start -n ${CONTAINER} ${OPTIONS} -f || true
143 fi
144 ;;
145
146 true)
147 cnt ${ACTION} -n ${CONTAINER} ${OPTIONS} || true
148 ;;
149 esac
150 ;;
151
152 stop)
153 OPTIONS="${OPTIONS} -f --stateless"
154
155 cnt ${ACTION} -n ${CONTAINER} ${OPTIONS} || true
156 ;;
157 esac
158 done
159
160 # Post hooks
161 for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}"
162 do
163 if [ -x "${FILE}" ]
164 then
165 "${FILE}"
166 fi
167 done