Debian Codegraph @

libexec/container/move

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 MACHINES="/var/lib/machines"
30
31 Parameters ()
32 {
33 GETOPT_LONGOPTIONS="force,new:,old:,"
34 GETOPT_OPTIONS="f,n:,o:,"
35
36 PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${COMMAND} --options ${GETOPT_OPTIONS} --shell sh -- ${@})"
37
38 if [ "${?}" != "0" ]
39 then
40 echo "'${COMMAND}': getopt exit" >&2
41 exit 1
42 fi
43
44 eval set -- "${PARAMETERS}"
45
46 while true
47 do
48 case "${1}" in
49 -f|--force)
50 FORCE="true"
51 shift 1
52 ;;
53
54 -n|--new)
55 NEW="${2}"
56 shift 2
57 ;;
58
59 -o|--old)
60 OLD="${2}"
61 shift 2
62 ;;
63
64 --)
65 shift 1
66 break
67 ;;
68
69 *)
70 echo "'${COMMAND}': getopt error" >&2
71 exit 1
72 ;;
73 esac
74 done
75 }
76
77 Usage ()
78 {
79 echo "Usage: ${PROGRAM} ${COMMAND} [-f|--force] -n|--new NAME -o|--old NAME" >&2
80 echo
81 echo "See ${COMMAND}(1), ${PROGRAM}(1) and ${PROJECT}(7) for more information."
82
83 exit 1
84 }
85
86 Parameters "${@}"
87
88 if [ -z "${OLD}" ] || [ -z "${NEW}" ]
89 then
90 Usage
91 fi
92
93 if [ ! -e "${MACHINES}/${OLD}" ]
94 then
95 echo "'${OLD}': no such container" >&2
96 exit 1
97 fi
98
99 if [ -e "${MACHINES}/${NEW}" ]
100 then
101 echo "'${NEW}': container already exists" >&2
102 exit 1
103 fi
104
105 STATE="$(machinectl show ${OLD} 2>&1 | awk -FState= '/^State=/ { print $2 }')"
106
107 case "${STATE}" in
108 running)
109 echo "'${OLD}': container is started" >&2
110 exit 1
111 ;;
112 esac
113
114 case "${FORCE}" in
115 true)
116 ;;
117
118 *)
119 if ${PROGRAM} list --other | grep -qs "^${OLD}$"
120 then
121 echo -n "'${OLD}': rename container to '${NEW}' [y|N]? "
122 read FORCE
123
124 FORCE="$(echo ${FORCE} | tr '[A-Z]' '[a-z]')"
125
126 case "${FORCE}" in
127 y|yes)
128 ;;
129
130 *)
131 exit 1
132 ;;
133 esac
134 fi
135 ;;
136 esac
137
138 # Pre hooks
139 for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${OLD}.pre-${COMMAND}"
140 do
141 if [ -x "${FILE}" ]
142 then
143 "${FILE}"
144 fi
145 done
146
147 # Run
148 mv "${CONFIG}/${OLD}.conf" "${CONFIG}/${NEW}.conf"
149 mv "${MACHINES}/${OLD}" "${MACHINES}/${NEW}"
150
151 # rw bind mounts
152 BIND="$(awk -Fbind= '/^bind=/ { print $2 }' ${CONFIG}/${NEW}.conf)"
153
154 if [ -n "${BIND}" ]
155 then
156 BINDS="$(echo ${BIND} | sed -e 's|;| |g')"
157
158 for BIND in ${BINDS}
159 do
160 SOURCE_OLD="$(echo ${BIND} | awk -F: '{ print $1 }')"
161 SOURCE_NEW="$(echo ${SOURCE_OLD} | sed -e "s|${OLD}|${NEW}|g")"
162
163 if [ "${SOURCE_OLD}" != "${SOURCE_NEW}" ]
164 then
165 mv "${SOURCE_OLD}" "${SOURCE_NEW}"
166 fi
167
168 TARGET_OLD="$(echo ${BIND} | awk -F: '{ print $2 }')"
169 TARGET_NEW="$(echo ${TARGET_OLD} | sed -e "s|${OLD}|${NEW}|g")"
170
171 if [ "${TARGET_OLD}" != "${TARGET_NEW}" ]
172 then
173 mv "${MACHINES}/${NEW}/${TARGET_OLD}" "${MACHINES}/${NEW}/${TARGET_NEW}"
174 fi
175 done
176 fi
177
178 # ro bind mounts
179 BIND_RO="$(awk -Fbind-ro= '/^bind-ro=/ { print $2 }' ${CONFIG}/${NEW}.conf)"
180
181 if [ -n "${BIND_RO}" ]
182 then
183 BINDS_RO="$(echo ${BIND_RO} | sed -e 's|;| |g')"
184
185 for BIND_RO in ${BINDS_RO}
186 do
187 SOURCE_OLD="$(echo ${BIND_RO} | awk -F: '{ print $1 }')"
188 SOURCE_NEW="$(echo ${SOURCE_OLD} | sed -e "s|${OLD}|${NEW}|g")"
189
190 if [ "${SOURCE_OLD}" != "${SOURCE_NEW}" ]
191 then
192 mv "${SOURCE_OLD}" "${SOURCE_NEW}"
193 fi
194
195 TARGET_OLD="$(echo ${BIND_RO} | awk -F: '{ print $2 }')"
196 TARGET_NEW="$(echo ${TARGET_OLD} | sed -e "s|${OLD}|${NEW}|g")"
197
198 if [ "${TARGET_OLD}" != "${TARGET_NEW}" ]
199 then
200 mv "${MACHINES}/${NEW}/${TARGET_OLD}" "${MACHINES}/${NEW}/${TARGET_NEW}"
201 fi
202 done
203 fi
204
205 # config
206 sed -i -e "s|${OLD}|${NEW}|g" "${CONFIG}/${NEW}.conf"
207
208 # Post hooks
209 for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NEW}.post-${COMMAND}"
210 do
211 if [ -x "${FILE}" ]
212 then
213 "${FILE}"
214 fi
215 done