Skip to content

Instantly share code, notes, and snippets.

@magn2o
Created February 10, 2020 02:25
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save magn2o/58f106a50f2fe77379bf7862071e4649 to your computer and use it in GitHub Desktop.
Save magn2o/58f106a50f2fe77379bf7862071e4649 to your computer and use it in GitHub Desktop.
init.d style service script for wpa_supplicant on the UDMPro.
#!/bin/sh
set -eu -o pipefail
CONTAINER_NAME="wpa_supplicant"
IMAGE_NAME="pbrah/wpa_supplicant-udmpro"
IMAGE_TAG="v1.0"
MAC_ADDRESS=""
ETH_IFACE="eth8"
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}")
}
set_mac() {
ifconfig "${1}" hw ether "${2}"
}
run() {
echo "podman run ${1} -it ${2} -Dwired -i${ETH_IFACE} -c/etc/wpa_supplicant/conf/wpa_supplicant.conf"
podman run ${1} -it ${2} -Dwired -i${ETH_IFACE} -c/etc/wpa_supplicant/conf/wpa_supplicant.conf
}
start() {
local args
local container_id
local image_version
local container_version
image_version=$(get_version "${IMAGE_NAME}:v1.0")
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.0"
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}"
[ ! -z "${MAC_ADDRESS}" ] && set_mac "${ETH_IFACE}" "${MAC_ADDRESS}"
args="-d --name ${CONTAINER_NAME} --label version=${image_version} --network=host --privileged -v /mnt/data/${CONTAINER_NAME}/:/etc/wpa_supplicant/conf/"
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.0" &&
{
podman rm "${CONTAINER_NAME}" --force &>/dev/null
run "$args" "${IMAGE_NAME}:v1.0" ||
echo "Starting container from ${IMAGE_NAME}:v1.0 failed" >&2
}
}
else
run "$args" "${IMAGE_NAME}:v1.0" ||
echo "Starting container from ${IMAGE_NAME}:v1.0 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