| 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,verbose," |
| 35 | GETOPT_OPTIONS="n:,f,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 | -v|--verbose) |
| 63 | VERBOSE="true" |
| 64 | shift 1 |
| 65 | |
| 66 | OPTIONS_ALL="${OPTIONS_ALL} --verbose" |
| 67 | ;; |
| 68 | |
| 69 | --) |
| 70 | shift 1 |
| 71 | break |
| 72 | ;; |
| 73 | |
| 74 | *) |
| 75 | echo "'${COMMAND}': getopt error" >&2 |
| 76 | exit 1 |
| 77 | ;; |
| 78 | esac |
| 79 | done |
| 80 | } |
| 81 | |
| 82 | Usage () |
| 83 | { |
| 84 | echo "Usage: ${PROGRAM} ${COMMAND} -n|--name NAME [-f|--force] [-v|--verbose]" >&2 |
| 85 | echo |
| 86 | echo "See ${COMMAND}(1), ${PROGRAM}(1) and ${PROJECT}(7) for more information." |
| 87 | |
| 88 | exit 1 |
| 89 | } |
| 90 | |
| 91 | Parameters "${@}" |
| 92 | |
| 93 | if [ -z "${NAME}" ] |
| 94 | then |
| 95 | Usage |
| 96 | fi |
| 97 | |
| 98 | case "${NAME}" in |
| 99 | ALL) |
| 100 | NAMES="$(${PROGRAM} list --format shell --started)" |
| 101 | |
| 102 | for NAME in ${NAMES} |
| 103 | do |
| 104 | ${PROGRAM} rebuild,start --name ${NAME} ${OPTIONS_ALL} || true |
| 105 | done |
| 106 | |
| 107 | exit 0 |
| 108 | ;; |
| 109 | esac |
| 110 | |
| 111 | if [ ! -e "${MACHINES}/${NAME}" ] |
| 112 | then |
| 113 | echo "'${NAME}': no such container" >&2 |
| 114 | exit 1 |
| 115 | fi |
| 116 | |
| 117 | # Pre hooks |
| 118 | for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}" |
| 119 | do |
| 120 | if [ -x "${FILE}" ] |
| 121 | then |
| 122 | "${FILE}" |
| 123 | fi |
| 124 | done |
| 125 | |
| 126 | # Run |
| 127 | case "${VERBOSE}" in |
| 128 | true) |
| 129 | echo -n "Rebuilding container ${NAME}..." |
| 130 | ;; |
| 131 | esac |
| 132 | |
| 133 | ${PROGRAM} stop ${OPTIONS_ALL} --name ${NAME} || true |
| 134 | sleep 0.5 |
| 135 | ${PROGRAM} remove ${OPTIONS_ALL} --name ${NAME} || true |
| 136 | sleep 0.5 |
| 137 | ${PROGRAM} build --name ${NAME} || true |
| 138 | |
| 139 | case "${VERBOSE}" in |
| 140 | true) |
| 141 | echo " done." |
| 142 | ;; |
| 143 | esac |
| 144 | |
| 145 | # Post hooks |
| 146 | for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}" |
| 147 | do |
| 148 | if [ -x "${FILE}" ] |
| 149 | then |
| 150 | "${FILE}" |
| 151 | fi |
| 152 | done |