| 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 | VERSION="$(${PROGRAM} version)" |
| 32 | |
| 33 | Parametersscip-shell shell shell-project 0.0.0 Parameters(). () |
| 34 | { |
| 35 | GETOPT_LONGOPTIONS="all,csv-separator:,format:,host:,other,started,stopped," |
| 36 | GETOPT_OPTIONS="a,f:,h:,o,s,t," |
| 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 | -a|--all) |
| 52 | LIST="${LIST} all" |
| 53 | shift 1 |
| 54 | ;; |
| 55 | |
| 56 | --csv-separator) |
| 57 | CSV_SEPARATOR="${2}" |
| 58 | shift 2 |
| 59 | ;; |
| 60 | |
| 61 | -f|--format) |
| 62 | FORMAT="${2}" |
| 63 | shift 2 |
| 64 | ;; |
| 65 | |
| 66 | -h|--host) |
| 67 | HOST="${2}" |
| 68 | shift 2 |
| 69 | ;; |
| 70 | |
| 71 | -o|--other) |
| 72 | LIST="${LIST} other" |
| 73 | shift 1 |
| 74 | ;; |
| 75 | |
| 76 | -s|--started) |
| 77 | LIST="${LIST} started" |
| 78 | shift 1 |
| 79 | ;; |
| 80 | |
| 81 | -t|--stopped) |
| 82 | LIST="${LIST} stopped" |
| 83 | shift 1 |
| 84 | ;; |
| 85 | |
| 86 | --) |
| 87 | shift 1 |
| 88 | break |
| 89 | ;; |
| 90 | |
| 91 | *) |
| 92 | echoscip-shell system . . echo. "'${COMMAND}': getopt error" >&2 |
| 93 | exit 1 |
| 94 | ;; |
| 95 | esac |
| 96 | done |
| 97 | } |
| 98 | |
| 99 | Usagescip-shell shell shell-project 0.0.0 Usage(). () |
| 100 | { |
| 101 | echoscip-shell system . . echo. "Usage: ${PROGRAM} ${COMMAND} [-a|--all] [--csv-separator SEPARATOR] [--format FORMAT] [-h|--host HOSTNAME] [-o|--other] [-s|--started] [-t|--stopped]" >&2 |
| 102 | echoscip-shell system . . echo. |
| 103 | echoscip-shell system . . echo. "See ${COMMAND}(1), ${PROGRAM}(1) and ${PROJECT}(7) for more information." |
| 104 | |
| 105 | exit 1 |
| 106 | } |
| 107 | |
| 108 | Parametersscip-shell shell shell-project 0.0.0 Parameters(). "${@}" |
| 109 | |
| 110 | LIST="${LIST:-started stopped}" |
| 111 | FORMAT="${FORMAT:-cli}" |
| 112 | HOST="${HOST:-$(hostnamescip-shell system . . hostname. -f 2> /dev/nullscip-shell filesystem . . `/dev/null`. || hostnamescip-shell system . . hostname.)}" |
| 113 | |
| 114 | CSV_SEPARATOR="${CSV_SEPARATOR:-,}" |
| 115 | |
| 116 | # Pre hooks |
| 117 | for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}" |
| 118 | do |
| 119 | if [scip-shell system . . `[`. -x "${FILE}" ] |
| 120 | then |
| 121 | "${FILE}" |
| 122 | fi |
| 123 | done |
| 124 | |
| 125 | # Run |
| 126 | Listscip-shell shell shell-project 0.0.0 List(). () |
| 127 | { |
| 128 | case "${FORMAT}" in |
| 129 | cli) |
| 130 | printfscip-shell system . . printf. "%1s %-74s %-19s\n" "${STATUS}" "${BLUE}${CONTAINER}${NORMAL}" "${YELLOW}${ADDRESS}${NORMAL}" |
| 131 | ;; |
| 132 | |
| 133 | csv) |
| 134 | echoscip-shell system . . echo. "${HOST}${CSV_SEPARATOR}${CONTAINER}${CSV_SEPARATOR}${STATE}${CSV_SEPARATOR}${ADDRESS}" |
| 135 | ;; |
| 136 | |
| 137 | json) |
| 138 | |
| 139 | catscip-shell system . . cat. << EOF |
| 140 | { |
| 141 | "name": "${CONTAINER}", |
| 142 | "status": "${STATE}", |
| 143 | "address": "${ADDRESS}", |
| 144 | }, |
| 145 | EOF |
| 146 | |
| 147 | ;; |
| 148 | |
| 149 | shell|sh) |
| 150 | echoscip-shell system . . echo. "${CONTAINER}" |
| 151 | ;; |
| 152 | |
| 153 | yaml) |
| 154 | |
| 155 | catscip-shell system . . cat. << EOF |
| 156 | - name: ${CONTAINER} |
| 157 | address: ${ADDRESS} |
| 158 | EOF |
| 159 | |
| 160 | ;; |
| 161 | |
| 162 | xml) |
| 163 | |
| 164 | catscip-shell system . . cat. << EOF |
| 165 | <container> |
| 166 | <name>${CONTAINER}</name> |
| 167 | <address>${ADDRESS}</address> |
| 168 | </container> |
| 169 | EOF |
| 170 | |
| 171 | ;; |
| 172 | esac |
| 173 | } |
| 174 | |
| 175 | case "${FORMAT}" in |
| 176 | cli) |
| 177 | RED="$(tputscip-shell system . . tput. setaf 1)$(tputscip-shell system . . tput. bold)" |
| 178 | GREEN="$(tputscip-shell system . . tput. setaf 2)$(tputscip-shell system . . tput. bold)" |
| 179 | YELLOW="$(tputscip-shell system . . tput. setaf 3)$(tputscip-shell system . . tput. bold)" |
| 180 | BLUE="$(tputscip-shell system . . tput. setaf 4)$(tputscip-shell system . . tput. bold)" |
| 181 | WHITE="$(tputscip-shell system . . tput. setaf 7)$(tputscip-shell system . . tput. bold)" |
| 182 | NORMAL="$(tputscip-shell system . . tput. sgr0)" |
| 183 | |
| 184 | catscip-shell system . . cat. << EOF |
| 185 | ${WHITE} ${NORMAL} Container IP Address |
| 186 | -------------------------------------------------------------------------------- |
| 187 | EOF |
| 188 | |
| 189 | ;; |
| 190 | |
| 191 | csv) |
| 192 | |
| 193 | catscip-shell system . . cat. << EOF |
| 194 | # compute-tools ${VERSION} |
| 195 | Host${CSV_SEPARATOR}Container${CSV_SEPARATOR}Status${CSV_SEPARATOR}IPv4-Address |
| 196 | EOF |
| 197 | |
| 198 | ;; |
| 199 | |
| 200 | json) |
| 201 | |
| 202 | catscip-shell system . . cat. << EOF |
| 203 | { |
| 204 | "compute-tools": { |
| 205 | "version": "${VERSION}", |
| 206 | }, |
| 207 | "host": { |
| 208 | "name": "${HOST}", |
| 209 | }, |
| 210 | "container": [ |
| 211 | EOF |
| 212 | |
| 213 | ;; |
| 214 | |
| 215 | yaml) |
| 216 | |
| 217 | catscip-shell system . . cat. << EOF |
| 218 | --- |
| 219 | compute_tools: |
| 220 | version: ${VERSION} |
| 221 | |
| 222 | host: |
| 223 | name: ${HOST} |
| 224 | |
| 225 | container: |
| 226 | EOF |
| 227 | |
| 228 | ;; |
| 229 | |
| 230 | xml) |
| 231 | |
| 232 | catscip-shell system . . cat. << EOF |
| 233 | <compute-tools> |
| 234 | <version>${VERSION}</version> |
| 235 | </compute-tools> |
| 236 | <host> |
| 237 | <name>${HOST}</name> |
| 238 | </host> |
| 239 | <containers> |
| 240 | EOF |
| 241 | |
| 242 | ;; |
| 243 | esac |
| 244 | |
| 245 | if lsscip-shell system . . ls. "${MACHINES}"/* > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 246 | then |
| 247 | CONTAINERS="$(cd "${MACHINES}" 2>/dev/nullscip-shell filesystem . . `/dev/null`. && findscip-shell system . . find. -maxdepth 1 \( -type d -or -type l \) -and -not -name 'lost+found' -printf '%P\n' | sortscip-shell system . . sort. -V)" |
| 248 | fi |
| 249 | |
| 250 | for CONTAINER in ${CONTAINERS} |
| 251 | do |
| 252 | STATE="$(machinectl show ${CONTAINER} 2>&1 | awkscip-shell system . . awk. -FState= '/^State=/ { print $2 }scip-shell filesystem . . `/^State=/ { print $2 }`.')" |
| 253 | |
| 254 | if [scip-shell system . . `[`. -e "${CONFIG}/${CONTAINER}.conf" ] |
| 255 | then |
| 256 | CONTAINER_SERVER="$(awkscip-shell system . . awk. -Fcnt.container-server= '/^cnt.container-server=/ { print $2 }scip-shell filesystem . . `/^cnt.container-server=/ { print $2 }`.' ${CONFIG}/${CONTAINER}.conf)" |
| 257 | CONTAINER_SERVER="${CONTAINER_SERVER:-false}" |
| 258 | |
| 259 | case "${CONTAINER_SERVER}" in |
| 260 | ${HOST}|true) |
| 261 | ;; |
| 262 | |
| 263 | *) |
| 264 | STATE="other" |
| 265 | ;; |
| 266 | esac |
| 267 | else |
| 268 | STATE="other" |
| 269 | fi |
| 270 | |
| 271 | case "${STATE}" in |
| 272 | running) |
| 273 | STATE="started" |
| 274 | STATUS="${GREEN}●${NORMAL}" |
| 275 | ;; |
| 276 | |
| 277 | other) |
| 278 | STATUS="${WHITE}○${NORMAL}" |
| 279 | ;; |
| 280 | |
| 281 | *) |
| 282 | STATE="stopped" |
| 283 | STATUS="${RED}●${NORMAL}" |
| 284 | ;; |
| 285 | esac |
| 286 | |
| 287 | ADDRESS="" |
| 288 | |
| 289 | case "${STATE}" in |
| 290 | started) |
| 291 | case "${FORMAT}" in |
| 292 | shell|sh) |
| 293 | ;; |
| 294 | |
| 295 | *) |
| 296 | LEADER="$(machinectl status ${CONTAINER} | awkscip-shell system . . awk. '/Leader: / { print $2 }scip-shell filesystem . . `/Leader: / { print $2 }`.')" |
| 297 | ADDRESS="$(nsenterscip-shell system . . nsenter. --all --target "${LEADER}" /bin/hostnamescip-shell filesystem . . `/bin/hostname`. -I)" |
| 298 | ;; |
| 299 | esac |
| 300 | |
| 301 | ADDRESS="${ADDRESS:-none}" |
| 302 | ;; |
| 303 | |
| 304 | *) |
| 305 | if lsscip-shell system . . ls. "${MACHINES}/${CONTAINER}/etc/systemd/network"/*.network > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 306 | then |
| 307 | ADDRESS="$(for IP in $(awkscip-shell system . . awk. -FAddress= '/^Address/ { print $2 }scip-shell filesystem . . `/^Address/ { print $2 }`.' ${MACHINES}/${CONTAINER}/etc/systemd/network/*.network); do echoscip-shell system . . echo. -n "${IP} "; done)" |
| 308 | elif [scip-shell system . . `[`. -e "${MACHINES}/${CONTAINER}/etc/network/interfaces" ] |
| 309 | then |
| 310 | ADDRESS="$(for IP in $(awkscip-shell system . . awk. '/address/ { print $2 }scip-shell filesystem . . `/address/ { print $2 }`.' ${MACHINES}/${CONTAINER}/etc/network/interfaces); do echoscip-shell system . . echo. -n "${IP} "; done)" |
| 311 | fi |
| 312 | |
| 313 | ADDRESS="${ADDRESS:-n/a}" |
| 314 | ;; |
| 315 | esac |
| 316 | |
| 317 | if echoscip-shell system . . echo. ${LIST} | grepscip-shell system . . grep. -qs all |
| 318 | then |
| 319 | Listscip-shell shell shell-project 0.0.0 List(). |
| 320 | fi |
| 321 | |
| 322 | for ITEM in other started stopped |
| 323 | do |
| 324 | if echoscip-shell system . . echo. ${LIST} | grepscip-shell system . . grep. -qs ${ITEM} |
| 325 | then |
| 326 | case "${STATE}" in |
| 327 | ${ITEM}) |
| 328 | Listscip-shell shell shell-project 0.0.0 List(). |
| 329 | ;; |
| 330 | esac |
| 331 | fi |
| 332 | done |
| 333 | done |
| 334 | |
| 335 | case "${FORMAT}" in |
| 336 | cli) |
| 337 | |
| 338 | catscip-shell system . . cat. << EOF |
| 339 | -------------------------------------------------------------------------------- |
| 340 | EOF |
| 341 | |
| 342 | ;; |
| 343 | |
| 344 | json) |
| 345 | |
| 346 | catscip-shell system . . cat. << EOF |
| 347 | ], |
| 348 | } |
| 349 | EOF |
| 350 | |
| 351 | ;; |
| 352 | |
| 353 | xml) |
| 354 | |
| 355 | catscip-shell system . . cat. << EOF |
| 356 | </containers> |
| 357 | EOF |
| 358 | |
| 359 | ;; |
| 360 | esac |
| 361 | |
| 362 | # Post hooks |
| 363 | for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}" |
| 364 | do |
| 365 | if [scip-shell system . . `[`. -x "${FILE}" ] |
| 366 | then |
| 367 | "${FILE}" |
| 368 | fi |
| 369 | done |