Fifth Commit

This commit is contained in:
Andrew Hurley 2023-02-27 23:01:25 +08:00
parent 91db28899d
commit a2020c989d
1 changed files with 124 additions and 68 deletions

192
podmanctl
View File

@ -5,7 +5,7 @@
set -u set -u
set -e set -e
# --------------------------------------------------------- usage # sub ----------------------------------------------------- usage
usage () { usage () {
printf "Usage:\n" printf "Usage:\n"
printf " podmanctl [<options>] <project> [<project>...]\n" printf " podmanctl [<options>] <project> [<project>...]\n"
@ -34,8 +34,10 @@ else
fi fi
# --------------------------------------------------------- args # --------------------------------------------------------- args
# ADD PULL OPTION
ARGS_ENV="" ARGS_ENV=""
while getopts ':e:' OPT; do PULL=""
while getopts ':pe:' OPT; do
case $OPT in case $OPT in
e) e)
if [[ "${COMMAND}" =~ ^(create|run)$ ]]; then if [[ "${COMMAND}" =~ ^(create|run)$ ]]; then
@ -45,6 +47,14 @@ while getopts ':e:' OPT; do
exit 1 exit 1
fi fi
;; ;;
p)
if [[ "${COMMAND}" =~ ^(create|run)$ ]]; then
PULL="--pull"
else
printf "option 'p' does not apply to command '${COMMAND}'\n"
exit 1
fi
;;
\?) \?)
usage usage
exit 1 exit 1
@ -60,6 +70,73 @@ if [[ $# -lt 1 ]]; then
exit 2 exit 2
fi fi
# sub ----------------------------------------------------- stop
stop-pod () {
if [[ "$(systemctl is-active podman-${PODNAME}.service)" == "active" ]]; then
if systemctl list-units --full -all \
|grep -Fq "podman-${PODNAME}.service"; then
systemctl stop podman-${PODNAME}.service || return 1
fi
else
if podman pod exists "${PODNAME}"; then
podman pod stop -t 70 "${PODNAME}" || return 2
fi
fi
}
# sub ----------------------------------------------------- start
start-pod () {
if systemctl list-units --full -all \
|grep -Fq "podman-${PODNAME}.service"; then
systemctl start podman-${PODNAME}.service || return 1
else
printf ":: ${COMMAND} ${PROJECT} - service not found!\n"; return 2
fi
}
# sub ----------------------------------------------------- restart
restart-pod () {
if systemctl list-units --full -all \
|grep -Fq "podman-${PODNAME}.service"; then
systemctl restart podman-${PODNAME}.service || return 1
else
printf ":: ${COMMAND} ${PROJECT} - service not found!\n"; return 2
fi
}
# sub ----------------------------------------------------- remove services
remove-services () {
if [[ -f "${SYSTEMD}"/podman-${PODNAME}.service ]]; then
systemctl disable podman-${PODNAME}.service
fi
find "${SYSTEMD}" \
-maxdepth 1 \
-name "podman-${PROJECT}-*.service" \
-exec rm {} +
systemctl daemon-reload
}
# sub ----------------------------------------------------- install services
install-services () {
(cd "${SYSTEMD}" && \
podman generate systemd \
--name \
--new \
--stop-timeout=70 \
--pod-prefix=podman \
--restart-sec=10 \
--container-prefix=podman \
--files ${PODNAME} \
) && systemctl daemon-reload \
&& systemctl enable podman-${PODNAME}
}
# sub ----------------------------------------------------- clean
clean-pod () {
stop-pod
remove-services
}
# --------------------------------------------------------- project loop # --------------------------------------------------------- project loop
for PROJECT in $*; do for PROJECT in $*; do
@ -114,6 +191,7 @@ for PROJECT in $*; do
# --------------------------------------------------------- spec vars # --------------------------------------------------------- spec vars
SPEC_IP="--ip ${SUBNET_PREFIX}" SPEC_IP="--ip ${SUBNET_PREFIX}"
SPEC_POD="--pod ${PODNAME}" SPEC_POD="--pod ${PODNAME}"
SPEC_PULL="${PULL}"
SPEC_DNS="--dns ${SUBNET_PREFIX}.254" SPEC_DNS="--dns ${SUBNET_PREFIX}.254"
SPEC_ENV="--env-file ${ENVFILE}" SPEC_ENV="--env-file ${ENVFILE}"
SPEC_ENV="${SPEC_ENV} --env SUBNET=${SUBNET}" SPEC_ENV="${SPEC_ENV} --env SUBNET=${SUBNET}"
@ -140,101 +218,79 @@ for PROJECT in $*; do
SPEC_RUN="${RUNTIME} create" SPEC_RUN="${RUNTIME} create"
fi fi
# --------------------------------------------------------- remove pod # --------------------------------------------------------- pod
if [[ "$(systemctl is-active podman-${PODNAME}.service)" == "active" ]]; then stop-pod
if systemctl list-units --full -all \
|grep -Fq "podman-${PODNAME}.service"; then
systemctl stop podman-${PODNAME}.service
fi
else
if podman pod exists "${PODNAME}"; then
podman pod rm -f -t 70 "${PODNAME}"
fi
fi
# --------------------------------------------------------- create pod
podman pod create \ podman pod create \
${SPEC_NAME}-pod \ ${SPEC_NAME}-pod \
${SPEC_INFRA_NAME} ${SPEC_INFRA_NAME} \
--replace
# --------------------------------------------------------- create|run containers # --------------------------------------------------------- create|run containers
source ${PROJECT} source ${PROJECT}
# --------------------------------------------------------- create services # --------------------------------------------------------- install services
(cd /etc/systemd/system && \ install-services
podman generate systemd \
--name \
--new \
--stop-timeout=70 \
--pod-prefix=podman \
--restart-sec=10 \
--container-prefix=podman \
--files ${PODNAME} \
) && systemctl daemon-reload \
&& systemctl enable podman-${PODNAME}
printf " ${COMMAND} ${PROJECT} - success\n" printf ":: ${COMMAND} ${PROJECT} - success\n"
;; ;;
(stop|start|restart) (stop)
# --------------------------------------------------------- start pod service if stop-pod; then
if [[ "$(systemctl is-active podman-${PODNAME}.service)" == "active" ]]; then printf ":: ${COMMAND} ${PROJECT} - success\n"
if systemctl list-units --full -all \
|grep -Fq "podman-${PODNAME}.service"; then
systemctl "${COMMAND}" podman-${PODNAME}.service
fi
else else
if podman pod exists "${PODNAME}"; then printf ":: ${COMMAND} ${PROJECT} - failure\n"
if [[ $COMMAND = "stop" ]]; then
podman pod stop -t 70 "${PODNAME}"
else
podman pod "${COMMAND}" "${PODNAME}"
fi
else
printf " ${COMMAND} ${PROJECT} - pod not found!\n"
continue
fi
fi fi
;;
(start)
printf " ${COMMAND} ${PROJECT} - success\n" if start-pod; then
printf ":: ${COMMAND} ${PROJECT} - success\n"
else
printf ":: ${COMMAND} ${PROJECT} - failure\n"
fi
sleep 1
;;
(restart)
if restart-pod; then
printf ":: ${COMMAND} ${PROJECT} - success\n"
else
printf ":: ${COMMAND} ${PROJECT} - failure\n"
fi
sleep 1
;; ;;
(clean) (clean)
# --------------------------------------------------------- clean if clean-pod; then
if [[ "$(systemctl is-active podman-${PODNAME}.service)" == "active" ]]; then printf ":: ${COMMAND} ${PROJECT} - success\n"
if systemctl list-units --full -all \
|grep -Fq "podman-${PODNAME}.service"; then
systemctl stop podman-${PODNAME}.service
fi
else else
if podman pod exists "${PODNAME}"; then printf ":: ${COMMAND} ${PROJECT} - failure\n"
podman pod rm -f -t 70 "${PODNAME}"
fi
fi fi
if [[ -f /etc/systemd/system/podman-${PODNAME}.service ]]; then
systemctl disable podman-${PODNAME}.service
fi
find /etc/systemd/system \
-maxdepth 1 \
-name "podman-${PROJECT}-*.service" \
-exec rm {} +
systemctl daemon-reload
printf " ${COMMAND} ${PROJECT} - success\n"
;; ;;
(backup) (backup)
echo " ${COMMAND} ${PROJECT} - not implemented\n"; continue if stop-pod; then
# start backup here ,,,
printf ":: ${COMMAND} ${PROJECT} - success\n"
else
printf ":: ${COMMAND} ${PROJECT} - failure\n"
fi
;; ;;
(restore) (restore)
echo " ${COMMAND} ${PROJECT} - not implemented\n"; continue echo ":: ${COMMAND} ${PROJECT} - not implemented\n"; continue
;; ;;
esac esac
${RUNTIME} ps -a --filter "pod=${PODNAME}" --format '{{.Names}}\t\t{{.Image}}\t\t{{.ExitCode}}\t\t{{.Status}}'
done done