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