Last active
February 16, 2021 07:54
-
-
Save magn2o/05c7f18e5e48b131b48924abadd8cbcd to your computer and use it in GitHub Desktop.
init.d style service script for eap_proxy on the UDMPro.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -eu -o pipefail | |
CONTAINER_NAME="eap_proxy" | |
IMAGE_NAME="pbrah/eap_proxy-udmpro" | |
IMAGE_TAG="v1.1" | |
container_exists() { | |
podman container inspect "${1}" &>/dev/null | |
} | |
image_exists() { | |
podman inspect --type=image "${1}" &>/dev/null | |
} | |
container_is_running() { | |
[ "$(podman inspect -f '{{.State.Running}}' "${1}" 2>&1)" = "true" ] | |
} | |
container_get_status() { | |
local name="${1}" | |
container_exists "${name}" || { echo "container ${name} doesn't exist" >&2; return 1; } | |
docker inspect -f '{{.State.Status}}' "${name}" | |
} | |
get_version() { | |
echo $(podman inspect --format '{{ index .Config.Labels "version"}}' "${1}") | |
} | |
run() { | |
echo "podman run ${1} -it ${2} --update-mongodb --ping-gateway --ignore-when-wan-up --ignore-start --ignore-logoff --set-mac eth8 eth9" | |
podman run ${1} -it ${2} --update-mongodb --ping-gateway --ignore-when-wan-up --ignore-start --ignore-logoff --set-mac eth8 eth9 | |
} | |
start() { | |
local args | |
local container_id | |
local image_version | |
local container_version | |
image_version=$(get_version "${IMAGE_NAME}:v1.1") | |
if container_exists "${CONTAINER_NAME}"; then | |
if container_is_running "${CONTAINER_NAME}"; then | |
echo "${CONTAINER_NAME} is already running" | |
return 0 | |
fi | |
if [ "$(container_get_status "${CONTAINER_NAME}")" != "exited" ]; then | |
echo "WARNING: ${CONTAINER_NAME} didn't initialize successfully last time. Recreating..." >&2 | |
podman rm --force "${CONTAINER_NAME}" &>/dev/null | |
start | |
return $? | |
fi | |
container_version=$(get_version "${CONTAINER_NAME}") | |
if [ "$container_version" != "$image_version" ]; then | |
echo "version mismatch, container has version ${container_version}, wanted ${image_version}, recreating container" | |
podman rm "${CONTAINER_NAME}" --force | |
podman rmi "${IMAGE_NAME}:v1.1" | |
podman rmi "${IMAGE_NAME}:${IMAGE_TAG}" | |
start | |
return $? | |
else | |
echo "podman commit ${CONTAINER_NAME} ${IMAGE_NAME}:${IMAGE_TAG}" | |
podman commit "${CONTAINER_NAME}" "${IMAGE_NAME}:${IMAGE_TAG}" | |
fi | |
fi | |
echo "Starting ${CONTAINER_NAME}" | |
args="-d --name ${CONTAINER_NAME} --label version=${image_version} --network=host --privileged" | |
podman rm "${CONTAINER_NAME}" --force &>/dev/null || true | |
if image_exists "${IMAGE_NAME}:${IMAGE_TAG}"; then | |
run "$args" "${IMAGE_NAME}:${IMAGE_TAG}" || | |
{ | |
echo "Starting container from ${IMAGE_NAME}:${IMAGE_TAG} failed, falling back to v1.1" && | |
{ | |
podman rm "${CONTAINER_NAME}" --force &>/dev/null | |
run "$args" "${IMAGE_NAME}:v1.1" || | |
echo "Starting container from ${IMAGE_NAME}:v1.1 failed" >&2 | |
} | |
} | |
else | |
run "$args" "${IMAGE_NAME}:v1.1" || | |
echo "Starting container from ${IMAGE_NAME}:v1.1 failed" >&2 | |
fi | |
} | |
stop() { | |
if ! container_is_running "${CONTAINER_NAME}"; then | |
echo "${CONTAINER_NAME} is not running" | |
else | |
echo "Stopping ${CONTAINER_NAME}" | |
podman stop "${CONTAINER_NAME}" | |
fi | |
} | |
restart() { | |
stop | |
start | |
} | |
reset() { | |
stop | |
podman rm "${CONTAINER_NAME}" --force &>/dev/null || true | |
podman rmi "${IMAGE_NAME}:${IMAGE_TAG}" || true | |
start | |
} | |
update() { | |
local remote_image="${1:-}" | |
[ -z "${remote_image}" ] && { echo "missing URL parameter" >&2; exit 1; } | |
podman pull --tls-verify=false "${remote_image}" | |
stop | |
podman rm "${CONTAINER_NAME}" --force | |
start | |
} | |
status() { | |
if container_is_running "${CONTAINER_NAME}"; then | |
echo "Running" | |
else | |
echo "Stopped" | |
fi | |
} | |
cmd="${1:-start}" | |
case "$cmd" in | |
stop) | |
stop 2>&1 | logger -st "${CONTAINER_NAME}" | |
;; | |
start) | |
start 2>&1 | logger -st "${CONTAINER_NAME}" | |
;; | |
reset) | |
reset 2>&1 | logger -st "${CONTAINER_NAME}" | |
;; | |
restart) | |
restart 2>&1 | logger -st "${CONTAINER_NAME}" | |
;; | |
shell) | |
podman exec -ti ${CONTAINER_NAME} /bin/sh | |
;; | |
update) | |
update "${2}" 2>&1 | logger -st "${CONTAINER_NAME}" | |
;; | |
status) | |
status 2>&1 | logger -st "${CONTAINER_NAME}" | |
;; | |
*) | |
echo "Usage: $0 [stop start reset restart shell status 'update url']" | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Were you able to persist this on the UDM Pro?