Debian Codegraph @

libexec/container/remove

raw symbols SCIP
1 #!/bin/shscip-shell filesystem . . `/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="$(basenamescip-shell system . . basename. ${0})"
26
27 CONFIG="/etc/${SOFTWARE}/config"
28 HOOKS="/etc/${SOFTWARE}/hooks"
29 MACHINES="/var/lib/machinesscip-shell filesystem . . `/var/lib/machines`."
30
31 Parametersscip-shell shell shell-project 0.0.0 Parameters(). ()
32 {
33 OPTIONS_ALL=""
34
35 GETOPT_LONGOPTIONS="name:,allow-stop,force,verbose,"
36 GETOPT_OPTIONS="n:,f,v,"
37
38 PARAMETERS="$(getoptscip-shell system . . getopt. --longoptions ${GETOPT_LONGOPTIONS} --name=${COMMAND} --options ${GETOPT_OPTIONS} --shell sh -- ${@})"
39
40 if [scip-shell system . . `[`. "${?}" != "0" ]
41 then
42 echoscip-shell system . . echo. "'${COMMAND}': getopt exit" >&2
43 exit 1
44 fi
45
46 eval set -- "${PARAMETERS}"
47
48 while truescip-shell system . . true.
49 do
50 case "${1}" in
51 -n|--name)
52 NAME="${2}"
53 shift 2
54 ;;
55
56 --allow-stop)
57 ALLOW_STOP="true"
58 shift 1
59
60 OPTIONS_ALL="${OPTIONS_ALL} --allow-stop"
61 ;;
62
63 -f|--force)
64 FORCE="true"
65 shift 1
66
67 OPTIONS_ALL="${OPTIONS_ALL} --force"
68 ;;
69
70 -v|--verbose)
71 VERBOSE="true"
72 shift 1
73
74 OPTIONS_ALL="${OPTIONS_ALL} --verbose"
75 ;;
76
77 --)
78 shift 1
79 break
80 ;;
81
82 *)
83 echoscip-shell system . . echo. "'${COMMAND}': getopt error" >&2
84 exit 1
85 ;;
86 esac
87 done
88 }
89
90 Usagescip-shell shell shell-project 0.0.0 Usage(). ()
91 {
92 echoscip-shell system . . echo. "Usage: ${PROGRAM} ${COMMAND} -n|--name NAME [--allow-stop] [-f|--force] [-v|--verbose]" >&2
93 echoscip-shell system . . echo.
94 echoscip-shell system . . echo. "See ${COMMAND}(1), ${PROGRAM}(1) and ${PROJECT}(7) for more information."
95
96 exit 1
97 }
98
99 Rmdirscip-shell shell shell-project 0.0.0 Rmdir(). ()
100 {
101 DIRECTORIES="${@}"
102
103 for DIRECTORY in ${DIRECTORIES}
104 do
105 PARENT_DIRECTORY="/$(echoscip-shell system . . echo. ${DIRECTORY} | cutscip-shell system . . cut. -d /scip-shell filesystem . . `/`. -f 2)"
106
107 if [scip-shell system . . `[`. "${PARENT_DIRECTORY}" != "${DIRECTORY}" ]
108 then
109 # the directory is at least two levels down from /
110 cd "${PARENT_DIRECTORY}"
111
112 CRUFT="$(echoscip-shell system . . echo. ${DIRECTORY} | cutscip-shell system . . cut. -d /scip-shell filesystem . . `/`. -f 3-)"
113 rmdirscip-shell system . . rmdir. --ignore-fail-on-non-empty --parents "${CRUFT}" > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || truescip-shell system . . true.
114
115 cd "${OLDPWD}"
116 fi
117 done
118 }
119
120 Parametersscip-shell shell shell-project 0.0.0 Parameters(). "${@}"
121
122 if [scip-shell system . . `[`. -z "${NAME}" ]
123 then
124 Usagescip-shell shell shell-project 0.0.0 Usage().
125 fi
126
127 case "${NAME}" in
128 ALL)
129 NAMES="$(${PROGRAM} list --format shell --stopped)"
130
131 for NAME in ${NAMES}
132 do
133 ${PROGRAM} remove --name ${NAME} ${OPTIONS_ALL} || truescip-shell system . . true.
134 done
135
136 exit 0
137 ;;
138 esac
139
140 if [scip-shell system . . `[`. ! -e "${MACHINES}/${NAME}" ] && [scip-shell system . . `[`. ! -e "${CONFIG}/${NAME}.conf" ]
141 then
142 echoscip-shell system . . echo. "'${NAME}': no such container" >&2
143 exit 1
144 fi
145
146 STATE="$(machinectl show ${NAME} 2>&1 | awkscip-shell system . . awk. -FState= '/^State=/ { print $2 }scip-shell filesystem . . `/^State=/ { print $2 }`.')"
147
148 case "${STATE}" in
149 running)
150 case "${ALLOW_STOP}" in
151 true)
152 echoscip-shell system . . echo. "'${NAME}': container is started, stopping it now" >&2
153 ${PROGRAM} stop -n ${NAME} -f
154 ;;
155
156 *)
157 echoscip-shell system . . echo. "'${NAME}': container is started" >&2
158 exit 1
159 ;;
160 esac
161 ;;
162 esac
163
164 case "${FORCE}" in
165 true)
166 ;;
167
168 *)
169 echoscip-shell system . . echo. -n "'${NAME}': remove container '${NAME}' [y|N]? "
170 read FORCE
171
172 FORCE="$(echoscip-shell system . . echo. ${FORCE} | trscip-shell system . . tr. '[A-Z]' '[a-z]')"
173
174 case "${FORCE}" in
175 y|yes)
176 ;;
177
178 *)
179 exit 1
180 ;;
181 esac
182 ;;
183 esac
184
185 case "${VERBOSE}" in
186 true)
187 RM_OPTIONS="--verbose"
188 ;;
189
190 *)
191 RM_OPTIONS=""
192 ;;
193 esac
194
195 # Pre hooks
196 for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}"
197 do
198 if [scip-shell system . . `[`. -x "${FILE}" ]
199 then
200 "${FILE}"
201 fi
202 done
203
204 # data
205 if [scip-shell system . . `[`. -e "${CONFIG}/${NAME}.conf" ]
206 then
207 # Removing rw bind mounts
208 BIND="$(awkscip-shell system . . awk. -Fbind= '/^bind=/ { print $2 }scip-shell filesystem . . `/^bind=/ { print $2 }`.' ${CONFIG}/${NAME}.conf)"
209
210 if [scip-shell system . . `[`. -n "${BIND}" ]
211 then
212 BINDS="$(echoscip-shell system . . echo. ${BIND} | sedscip-shell system . . sed. -e 's|;| |g')"
213
214 for BIND in ${BINDS}
215 do
216 DIRECTORY="$(echoscip-shell system . . echo. ${BIND} | awkscip-shell system . . awk. -F: '{ print $1 }')"
217
218 Rmdirscip-shell shell shell-project 0.0.0 Rmdir(). "${DIRECTORY}"
219 done
220 fi
221
222 # Removing ro bind mounts
223 BIND_RO="$(awkscip-shell system . . awk. -Fbind-ro= '/^bind-ro=/ { print $2 }scip-shell filesystem . . `/^bind-ro=/ { print $2 }`.' ${CONFIG}/${NAME}.conf)"
224
225 if [scip-shell system . . `[`. -n "${BIND_RO}" ]
226 then
227 BINDS_RO="$(echoscip-shell system . . echo. ${BIND_RO} | sedscip-shell system . . sed. -e 's|;| |g')"
228
229 for BIND_RO in ${BINDS_RO}
230 do
231 DIRECTORY="$(echoscip-shell system . . echo. ${BIND_RO} | awkscip-shell system . . awk. -F: '{ print $1 }')"
232
233 Rmdirscip-shell shell shell-project 0.0.0 Rmdir(). "${DIRECTORY}"
234 done
235 fi
236 fi
237
238 # Run
239 case "${VERBOSE}" in
240 true)
241 echoscip-shell system . . echo. -n "Removing container ${NAME}..."
242 ;;
243 esac
244
245 rmscip-shell system . . rm. --preserve-root --one-file-system -rf ${RM_OPTIONS} "${MACHINES}/${NAME}"
246 rmscip-shell system . . rm. -f ${RM_OPTIONS} "${CONFIG}/${NAME}.conf"
247
248 case "${VERBOSE}" in
249 true)
250 echoscip-shell system . . echo. " done."
251 ;;
252 esac
253
254 # Post hooks
255 for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}"
256 do
257 if [scip-shell system . . `[`. -x "${FILE}" ]
258 then
259 "${FILE}"
260 fi
261 done