| 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 | HOOKS="/etc/${SOFTWARE}/hooks" |
| 28 | MACHINES="/var/lib/machinesscip-shell filesystem . . `/var/lib/machines`." |
| 29 | |
| 30 | Parametersscip-shell shell shell-project 0.0.0 Parameters(). () |
| 31 | { |
| 32 | GETOPT_LONGOPTIONS="name:," |
| 33 | GETOPT_OPTIONS="n:," |
| 34 | |
| 35 | PARAMETERS="$(getoptscip-shell system . . getopt. --longoptions ${GETOPT_LONGOPTIONS} --name=${COMMAND} --options ${GETOPT_OPTIONS} --shell sh -- ${@})" |
| 36 | |
| 37 | if [scip-shell system . . `[`. "${?}" != "0" ] |
| 38 | then |
| 39 | echoscip-shell system . . echo. "'${COMMAND}': getopt exit" >&2 |
| 40 | exit 1 |
| 41 | fi |
| 42 | |
| 43 | eval set -- "${PARAMETERS}" |
| 44 | |
| 45 | while truescip-shell system . . true. |
| 46 | do |
| 47 | case "${1}" in |
| 48 | -n|--name) |
| 49 | NAME="${2}" |
| 50 | shift 2 |
| 51 | ;; |
| 52 | |
| 53 | --) |
| 54 | shift 1 |
| 55 | break |
| 56 | ;; |
| 57 | |
| 58 | *) |
| 59 | echoscip-shell system . . echo. "'${COMMAND}': getopt error" >&2 |
| 60 | exit 1 |
| 61 | ;; |
| 62 | esac |
| 63 | done |
| 64 | } |
| 65 | |
| 66 | Usagescip-shell shell shell-project 0.0.0 Usage(). () |
| 67 | { |
| 68 | echoscip-shell system . . echo. "Usage: ${PROGRAM} ${COMMAND} -n|--name NAME" >&2 |
| 69 | echoscip-shell system . . echo. |
| 70 | echoscip-shell system . . echo. "See ${COMMAND}(1), ${PROGRAM}(1) and ${PROJECT}(7) for more information." |
| 71 | |
| 72 | exit 1 |
| 73 | } |
| 74 | |
| 75 | Parametersscip-shell shell shell-project 0.0.0 Parameters(). "${@}" |
| 76 | |
| 77 | if [scip-shell system . . `[`. -z "${NAME}" ] |
| 78 | then |
| 79 | Usagescip-shell shell shell-project 0.0.0 Usage(). |
| 80 | fi |
| 81 | |
| 82 | if [scip-shell system . . `[`. ! -e "${MACHINES}/${NAME}" ] |
| 83 | then |
| 84 | echoscip-shell system . . echo. "'${NAME}': no such container" >&2 |
| 85 | exit 1 |
| 86 | fi |
| 87 | |
| 88 | STATE="$(machinectl show ${NAME} 2>&1 | awkscip-shell system . . awk. -FState= '/^State=/ { print $2 }scip-shell filesystem . . `/^State=/ { print $2 }`.')" |
| 89 | |
| 90 | case "${STATE}" in |
| 91 | running) |
| 92 | ;; |
| 93 | |
| 94 | *) |
| 95 | echoscip-shell system . . echo. "'${NAME}': container is not started" >&2 |
| 96 | exit 1 |
| 97 | ;; |
| 98 | esac |
| 99 | |
| 100 | # Pre hooks |
| 101 | for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}" |
| 102 | do |
| 103 | if [scip-shell system . . `[`. -x "${FILE}" ] |
| 104 | then |
| 105 | "${FILE}" |
| 106 | fi |
| 107 | done |
| 108 | |
| 109 | # Run |
| 110 | machinectl login ${NAME} |
| 111 | |
| 112 | # Post hooks |
| 113 | for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}" |
| 114 | do |
| 115 | if [scip-shell system . . `[`. -x "${FILE}" ] |
| 116 | then |
| 117 | "${FILE}" |
| 118 | fi |
| 119 | done |