Debian Codegraph @

libexec/container/info

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 VERSION="$(${PROGRAM} version)"
32
33 Parameters ()
34 {
35 GETOPT_LONGOPTIONS="name:,status,os,ip,"
36 GETOPT_OPTIONS="n:,"
37
38 PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${COMMAND} --options ${GETOPT_OPTIONS} --shell sh -- ${@})"
39
40 if [ "${?}" != "0" ]
41 then
42 echo "'${COMMAND}': getopt exit" >&2
43 exit 1
44 fi
45
46 eval set -- "${PARAMETERS}"
47
48 while true
49 do
50 case "${1}" in
51 -n|--name)
52 NAME="${2}"
53 shift 2
54 ;;
55
56 --status)
57 ACTIONS="${ACTIONS} status"
58 shift 1
59 ;;
60
61 --os)
62 ACTIONS="${ACTIONS} os"
63 shift 1
64 ;;
65
66 --ip)
67 ACTIONS="${ACTIONS} ip"
68 shift 1
69 ;;
70
71 --)
72 shift 1
73 break
74 ;;
75
76 *)
77 echo "'${COMMAND}': getopt error" >&2
78 exit 1
79 ;;
80 esac
81 done
82 }
83
84 Usage ()
85 {
86 echo "Usage: ${PROGRAM} ${COMMAND} -n|--name NAME [--status] [--os] [--ip]" >&2
87 echo
88 echo "See ${COMMAND}(1), ${PROGRAM}(1) and ${PROJECT}(7) for more information."
89
90 exit 1
91 }
92
93 Parameters "${@}"
94
95 # Pre hooks
96 for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}"
97 do
98 if [ -x "${FILE}" ]
99 then
100 "${FILE}"
101 fi
102 done
103
104 ACTIONS="${ACTIONS:-status os ip}"
105 HOST="$(cat /etc/hostname)"
106
107 # Run
108
109 # Status
110 STATUS="$(machinectl show ${NAME} 2>&1 | awk -FState= '/^State=/ { print $2 }')"
111
112 if [ -e "${CONFIG}/${NAME}.conf" ]
113 then
114 CONTAINER_SERVER="$(awk -Fcnt.container-server= '/^cnt.container-server=/ { print $2 }' ${CONFIG}/${NAME}.conf)"
115 CONTAINER_SERVER="${CONTAINER_SERVER:-false}"
116
117 case "${CONTAINER_SERVER}" in
118 ${HOST}|true)
119 ;;
120
121 *)
122 STATUS="other"
123 ;;
124 esac
125 else
126 STATUS="other"
127 fi
128
129 case "${STATUS}" in
130 running)
131 STATUS="started"
132 ;;
133
134 other)
135 ;;
136
137 *)
138 STATUS="stopped"
139 ;;
140 esac
141
142 # OS
143 VERSION_BASH="$(chroot ${MACHINES}/${NAME} apt-cache policy bash | awk '/Installed: / { print $2 }')"
144
145 case "${VERSION_BASH}" in
146 4.1-*|4.1.[0-9]*)
147 OS="Debian 6 (squeeze)"
148 ;;
149
150 4.2-*|4.2.[0-9]*)
151 OS="Debian 7 (wheezy)"
152 ;;
153
154 4.3-*|4.3.[0-9]*)
155 OS="Debian 8 (jessie)"
156 ;;
157
158 4.4-*|4.4.[0-9]*)
159 OS="Debian 9 (stretch)"
160 ;;
161
162 5.0-*|5.0.[0-9]*)
163 OS="Debian 10 (buster)"
164 ;;
165
166 5.1-*|5.1.[0-9]*)
167 OS="Debian 11 (bullseye)"
168 ;;
169
170 5.2-*|5.2.[0-9]*)
171 if [ "$(echo "${VERSION_BASH}" | cut -d. -f 3 | cut -d- -f1)" -lt 37 ]
172 then
173 OS="Debian 12 (bookworm)"
174 else
175 OS="Debian 13 (trixie)"
176 fi
177 ;;
178
179 5.3-*|5.3.[0-9]*)
180 OS="Debian 14 (forky)"
181 ;;
182
183 *)
184 OS="n/a"
185 ;;
186 esac
187
188 case "${STATUS}" in
189 started)
190 IP="$(cnt run -n ${NAME} -- hostname -I)"
191 ;;
192
193 *)
194 if ls "${MACHINES}/${NAME}/etc/systemd/network"/*.network > /dev/null 2>&1
195 then
196 IP="$(awk -FAddress= '/^Address/ { printf "%s ", $2 }' ${MACHINES}/${NAME}/etc/systemd/network/*.network)"
197 elif [ -e "${MACHINES}/${NAME}/etc/network/interfaces" ]
198 then
199 IP="$(awk '/address/ { printf "%s ", $2 }' ${MACHINES}/${NAME}/etc/network/interfaces)"
200 fi
201
202 IP="${IP:-n/a}"
203 ;;
204 esac
205
206 for ACTION in ${ACTIONS}
207 do
208 case "${ACTION}" in
209 status)
210 echo "${STATUS}"
211 ;;
212
213 os)
214 echo "${OS}"
215 ;;
216
217 ip)
218 echo "${IP}"
219 ;;
220 esac
221 done
222
223 # Post hooks
224 for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}"
225 do
226 if [ -x "${FILE}" ]
227 then
228 "${FILE}"
229 fi
230 done