Debian Codegraph @

libexec/container/update

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 HOOKS="/etc/${SOFTWARE}/hooks"
28
29 Parameters ()
30 {
31 GETOPT_LONGOPTIONS="name:,full-upgrade,interactive,autoremove,purge,yes,"
32 GETOPT_OPTIONS="n:,f,i,r,p,y,"
33
34 PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${COMMAND} --options ${GETOPT_OPTIONS} --shell sh -- ${@})"
35
36 if [ "${?}" != "0" ]
37 then
38 echo "'${COMMAND}': getopt exit" >&2
39 exit 1
40 fi
41
42 eval set -- "${PARAMETERS}"
43
44 while true
45 do
46 case "${1}" in
47 -n|--name)
48 NAME="${2}"
49 shift 2
50 ;;
51
52 -f|--full-upgrade)
53 FULL_UPGRADE="true"
54 shift 1
55 ;;
56
57 -i|--interactive)
58 INTERACTIVE="true"
59 shift 1
60 ;;
61
62 -r|--autoremove)
63 AUTOREMOVE="true"
64 shift 1
65 ;;
66
67 -p|--purge)
68 PURGE="--purge"
69 shift 1
70 ;;
71
72 -y|--yes)
73 YES="-y"
74 shift 1
75 ;;
76
77 --)
78 shift 1
79 break
80 ;;
81
82 *)
83 echo "'${COMMAND}': getopt error" >&2
84 exit 1
85 ;;
86 esac
87 done
88 }
89
90 Usage ()
91 {
92 echo "Usage: ${PROGRAM} ${COMMAND} -n|--name NAME [-f|--full-upgrade] [-i|--interactive] [-r|--autoremove] [-p|--purge] [-y|--yes]" >&2
93 echo
94 echo "See ${COMMAND}(1), ${PROGRAM}(1) and ${PROJECT}(7) for more information."
95
96 exit 1
97 }
98
99 Parameters "${@}"
100
101 if [ -z "${NAME}" ]
102 then
103 Usage
104 fi
105
106 Notification ()
107 {
108 TYPE="${1}"
109 NUMBER="${2}"
110 PACKAGES="${3}"
111
112 if [ -z "${PACKAGES}" ]
113 then
114 return
115 fi
116
117 CONTAINER_USER="${SUDO_USER:-${USER}}"
118
119 DATE="$(date +%Y-%m-%d\ %H:%M:%S)"
120 HOST="$(hostname -f 2> /dev/null || hostname)"
121
122 # logfile
123 echo "${DATE} ${HOST} ${CONTAINER_USER} ${NAME} ${NUMBER} ${TYPE}: ${PACKAGES}" >> "/var/log/${SOFTWARE}/${PROGRAM}.log"
124
125 # irc
126 if [ -e /usr/bin/irk ]
127 then
128 for FILE in "/etc/${SOFTWARE}/${PROGRAM}.conf" "/etc/${SOFTWARE}/${PROGRAM}.conf.d"/*.conf
129 do
130 if [ -e "${FILE}" ]
131 then
132 . "${FILE}"
133 fi
134 done
135
136 if [ -n "${IRK_TARGETS}" ]
137 then
138 for TARGET in ${IRK_TARGETS}
139 do
140 irk "${TARGET}" "\x0300${CONTAINER_USER}\x03@\x0312${HOST}:\x03 \x0303${NAME}\x03 \x0307${NUMBER} ${TYPE}\x03: ${PACKAGES}"
141 done
142 fi
143 fi
144 }
145
146 case "${NAME}" in
147 ALL)
148 NAMES="$(container list --started --format shell)"
149 ;;
150
151 *)
152 NAMES="${NAME}"
153 ;;
154 esac
155
156 # Pre hooks
157 for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}"
158 do
159 if [ -x "${FILE}" ]
160 then
161 "${FILE}"
162 fi
163 done
164
165 if [ $(echo ${NAMES} | wc -w) -gt 1 ]
166 then
167 NAME_LOOP="true"
168 else
169 NAME_LOOP="false"
170 fi
171
172 # Run
173 for NAME in ${NAMES}
174 do
175 case "${INTERACTIVE}" in
176 true)
177 case "${NAME_LOOP}" in
178 true)
179 echo
180 ;;
181 esac
182
183 echo -n "'${NAME}': update container '${NAME}' [y|N|a]? "
184 read UPDATE
185
186 UPDATE="$(echo ${UPDATE} | tr '[A-Z]' '[a-z]')"
187
188 case "${UPDATE}" in
189 a|all)
190 INTERACTIVE="false"
191 ;;
192
193 y|yes)
194 ;;
195
196 *)
197 case "${NAME_LOOP}" in
198 true)
199 continue
200 ;;
201
202 *)
203 exit 1
204 ;;
205 esac
206 ;;
207 esac
208 ;;
209 esac
210
211 echo "################################################################################"
212 echo "Updating ${NAME}"
213 echo "################################################################################"
214
215 container run -n ${NAME} -- "apt update"
216
217 UPDATE_NUMBER="$(container run -n ${NAME} -- "apt \-\-simulate full-upgrade" | awk '/^[0-9]* upgraded, / { print $1 }')"
218
219 case "${UPDATE_NUMBER}" in
220 0)
221 ;;
222
223 *)
224 # usefull use of grep to de-colorize apt output
225 UPDATE_PACKAGES="$(for PACKAGE in $(container run -n ${NAME} -- "apt list \-\-upgradable 2>/dev/null | grep '\/'" | awk -F/ '{ print $1 }'); do echo -n "${PACKAGE} "; done | sed -e 's| $||'; echo)"
226
227 case "${FULL_UPGRADE}" in
228 true)
229 container run -n ${NAME} -- "DEBCONF_FRONTEND='noninteractive' DEBCONF_PRIORITY='critical' DEBCONF_NONINTERACTIVE_SEEN='true' DEBCONF_NOWARNINGS='true' apt \-o Dpkg::Options::=\-\-force-confold -f ${YES} full-upgrade"
230 ;;
231
232 *)
233 container run -n ${NAME} -- "DEBCONF_FRONTEND='noninteractive' DEBCONF_PRIORITY='critical' DEBCONF_NONINTERACTIVE_SEEN='true' DEBCONF_NOWARNINGS='true' apt \-o Dpkg::Options::=\-\-force-confold -f ${YES} upgrade"
234 ;;
235 esac
236
237 Notification "update(s)" "${UPDATE_NUMBER}" "${UPDATE_PACKAGES}"
238 ;;
239 esac
240
241 case "${AUTOREMOVE}" in
242 true)
243 REMOVE_NUMBER="$(container run -n ${NAME} -- "apt \-\-simulate autoremove" | awk '/^[0-9]* upgraded, / { print $6 }')"
244
245 case "${REMOVE_NUMBER}" in
246 0)
247 ;;
248
249 *)
250 REMOVE_PACKAGES="$(for LINE in $(container run -n ${NAME} -- "apt \-\-simulate autoremove" | grep '^ '); do echo ${LINE}; done | sed -e 's|^ ||' -e 's| $||'; echo)"
251
252 container run -n ${NAME} -- "apt ${YES} autoremove ${PURGE}"
253
254 Notification "removal(s)" "${REMOVE_NUMBER}" "$(echo ${REMOVE_PACKAGES})"
255 ;;
256 esac
257 ;;
258 esac
259
260 echo "'${NAME}': container updated."
261 done
262
263 # Post hooks
264 for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}"
265 do
266 if [ -x "${FILE}" ]
267 then
268 "${FILE}"
269 fi
270 done