| 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 | MACHINES="/var/lib/machines" |
| 29 | |
| 30 | Parameters () |
| 31 | { |
| 32 | OPTIONS_ALL="" |
| 33 | |
| 34 | GETOPT_LONGOPTIONS="name:,force,interactive,verbose," |
| 35 | GETOPT_OPTIONS="n:,f,i,v," |
| 36 | |
| 37 | PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${COMMAND} --options ${GETOPT_OPTIONS} --shell sh -- ${@})" |
| 38 | |
| 39 | if [ "${?}" != "0" ] |
| 40 | then |
| 41 | echo "'${COMMAND}': getopt exit" >&2 |
| 42 | exit 1 |
| 43 | fi |
| 44 | |
| 45 | eval set -- "${PARAMETERS}" |
| 46 | |
| 47 | while true |
| 48 | do |
| 49 | case "${1}" in |
| 50 | -n|--name) |
| 51 | NAME="${2}" |
| 52 | shift 2 |
| 53 | ;; |
| 54 | |
| 55 | -f|--force) |
| 56 | FORCE="true" |
| 57 | shift 1 |
| 58 | |
| 59 | OPTIONS_ALL="${OPTIONS_ALL} --force" |
| 60 | ;; |
| 61 | |
| 62 | -i|--interactive) |
| 63 | INTERACTIVE="true" |
| 64 | shift 1 |
| 65 | |
| 66 | OPTIONS_ALL="${OPTIONS_ALL} --interactive" |
| 67 | ;; |
| 68 | |
| 69 | -v|--verbose) |
| 70 | VERBOSE="true" |
| 71 | shift 1 |
| 72 | |
| 73 | OPTIONS_ALL="${OPTIONS_ALL} --verbose" |
| 74 | ;; |
| 75 | |
| 76 | --) |
| 77 | shift 1 |
| 78 | break |
| 79 | ;; |
| 80 | |
| 81 | *) |
| 82 | echo "'${COMMAND}': getopt error" >&2 |
| 83 | exit 1 |
| 84 | ;; |
| 85 | esac |
| 86 | done |
| 87 | } |
| 88 | |
| 89 | Usage () |
| 90 | { |
| 91 | echo "Usage: ${PROGRAM} ${COMMAND} -n|--name NAME [-f|--force] [-i|--interactive] [-v|--verbose]" >&2 |
| 92 | echo |
| 93 | echo "See ${COMMAND}(1), ${PROGRAM}(1) and ${PROJECT}(7) for more information." |
| 94 | |
| 95 | exit 1 |
| 96 | } |
| 97 | |
| 98 | Parameters "${@}" |
| 99 | |
| 100 | if [ -z "${NAME}" ] |
| 101 | then |
| 102 | Usage |
| 103 | fi |
| 104 | |
| 105 | case "${NAME}" in |
| 106 | ALL) |
| 107 | NAMES="$(${PROGRAM} list --format shell --started)" |
| 108 | |
| 109 | for NAME in ${NAMES} |
| 110 | do |
| 111 | ${PROGRAM} restart --name ${NAME} ${OPTIONS_ALL} || true |
| 112 | done |
| 113 | |
| 114 | exit 0 |
| 115 | ;; |
| 116 | esac |
| 117 | |
| 118 | if [ ! -e "${MACHINES}/${NAME}" ] |
| 119 | then |
| 120 | echo "'${NAME}': no such container" >&2 |
| 121 | exit 1 |
| 122 | fi |
| 123 | |
| 124 | if [ "${FORCE}" != "true" ] || [ "${INTERACTIVE}" = "true" ] |
| 125 | then |
| 126 | echo -n "'${NAME}': restart container '${NAME}' [y|N]? " |
| 127 | read STOP |
| 128 | |
| 129 | STOP="$(echo ${STOP} | tr '[A-Z]' '[a-z]')" |
| 130 | |
| 131 | case "${STOP}" in |
| 132 | y|yes) |
| 133 | ;; |
| 134 | |
| 135 | *) |
| 136 | exit 1 |
| 137 | ;; |
| 138 | esac |
| 139 | fi |
| 140 | |
| 141 | # Pre hooks |
| 142 | for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}" |
| 143 | do |
| 144 | if [ -x "${FILE}" ] |
| 145 | then |
| 146 | "${FILE}" |
| 147 | fi |
| 148 | done |
| 149 | |
| 150 | # Run |
| 151 | case "${VERBOSE}" in |
| 152 | true) |
| 153 | echo -n "Restarting container ${NAME}..." |
| 154 | ;; |
| 155 | esac |
| 156 | |
| 157 | machinectl reboot ${NAME} |
| 158 | |
| 159 | case "${VERBOSE}" in |
| 160 | true) |
| 161 | echo " done." |
| 162 | ;; |
| 163 | esac |
| 164 | |
| 165 | # Post hooks |
| 166 | for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}" |
| 167 | do |
| 168 | if [ -x "${FILE}" ] |
| 169 | then |
| 170 | "${FILE}" |
| 171 | fi |
| 172 | done |