swmodd: Fix AutoStart, RequestedState for OCI containers

This commit is contained in:
Suvendhu Hansa 2022-11-25 08:36:13 +00:00 committed by Vivek Kumar Dutta
parent 1ba2d5ab89
commit 0d076595f8
2 changed files with 66 additions and 31 deletions

View file

@ -5,12 +5,12 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=swmodd
PKG_VERSION:=2.1.17
PKG_VERSION:=2.1.18
LOCAL_DEV:=0
ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_VERSION:=5124ea8051c06e8c3563b384660b08db199c93eb
PKG_SOURCE_VERSION:=0617f1017511854b0e3d384799f2bf63800a27c0
PKG_SOURCE_URL:=https://dev.iopsys.eu/iopsys/swmodd.git
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz
PKG_MIRROR_HASH:=skip

View file

@ -16,11 +16,12 @@ is_container_running() {
configure_crun_container() {
local name type autostart du_status requested_state url username password
local BRIDGE BUNDLE
local BRIDGE BUNDLE BOOT
local RUNNER="/etc/swmodd/run.sh"
BUNDLE="${2}"
BRIDGE="${3}"
BOOT="${4}"
config_get name ${1} name ""
config_get type ${1} type ""
@ -102,36 +103,59 @@ configure_crun_container() {
return 0;
fi
if [ "${autostart}" -eq 0 ]; then
ubus call service delete "{\"name\":\"crun\",\"instance\":\"${name}\"}"
# stop the container if not enabled
crun kill --all "${name}" 9
return 0;
fi
if [ "${BOOT}" -eq "1" ]; then
if [ "${autostart}" -eq 1 ]; then
${RUNNER} -U -b "${BUNDLE}" -n "${name}"
result=$(cat ${BUNDLE}/${name}/config.json |jq ".annotations.org_opencontainers_image_description")
if [ "${result}" != "null" ]; then
uci_set ocicontainer ${1} description "${result}"
fi
if is_container_running "${name}"; then
log "Container [$name] req_status [${requested_state}]"
if [ "${requested_state}" == "Idle" ]; then
crun pause "${name}"
elif [ "${requested_state}" == "Active" ]; then
${RUNNER} -u -n "${name}" -i "${BRIDGE}"
crun resume "${name}"
result=$(cat ${BUNDLE}/${name}/config.json |jq ".annotations.org_opencontainers_image_vendor")
if [ "${result}" != "null" ]; then
uci_set ocicontainer ${1} vendor "${result}"
fi
result=$(cat ${BUNDLE}/${name}/config.json |jq ".annotations.org_opencontainers_image_version")
if [ "${result}" != "null" ]; then
uci_set ocicontainer ${1} version "${result}"
fi
uci_set ocicontainer ${1} requested_state "Active"
else
uci_set ocicontainer ${1} requested_state "Idle"
return 0;
fi
else
${RUNNER} -U -b "${BUNDLE}" -n "${name}"
result=$(cat ${BUNDLE}/${name}/config.json |jq ".annotations.org_opencontainers_image_description")
if [ "${result}" != "null" ]; then
uci_set ocicontainer ${1} description "${result}"
fi
log "Container [$name] req_status [${requested_state}]"
if [ "${requested_state}" == "Idle" ]; then
if is_container_running "${name}"; then
crun pause "${name}"
else
return 0;
fi
elif [ "${requested_state}" == "Active" ]; then
if is_container_running "${name}"; then
${RUNNER} -u -n "${name}" -i "${BRIDGE}"
crun resume "${name}"
else
${RUNNER} -U -b "${BUNDLE}" -n "${name}"
result=$(cat ${BUNDLE}/${name}/config.json |jq ".annotations.org_opencontainers_image_description")
if [ "${result}" != "null" ]; then
uci_set ocicontainer ${1} description "${result}"
fi
result=$(cat ${BUNDLE}/${name}/config.json |jq ".annotations.org_opencontainers_image_vendor")
if [ "${result}" != "null" ]; then
uci_set ocicontainer ${1} vendor "${result}"
fi
result=$(cat ${BUNDLE}/${name}/config.json |jq ".annotations.org_opencontainers_image_vendor")
if [ "${result}" != "null" ]; then
uci_set ocicontainer ${1} vendor "${result}"
fi
result=$(cat ${BUNDLE}/${name}/config.json |jq ".annotations.org_opencontainers_image_version")
if [ "${result}" != "null" ]; then
uci_set ocicontainer ${1} version "${result}"
result=$(cat ${BUNDLE}/${name}/config.json |jq ".annotations.org_opencontainers_image_version")
if [ "${result}" != "null" ]; then
uci_set ocicontainer ${1} version "${result}"
fi
fi
else
return 0;
fi
fi
@ -144,8 +168,12 @@ configure_crun_container() {
procd_close_instance "${name}"
}
boot() {
start "bootstrap"
}
start_service() {
local bundle bridge
local bundle bridge boot
# Check if crun present
if ! command -v crun >/dev/null 2>&1; then
@ -153,6 +181,12 @@ start_service() {
return 0;
fi
if [ "${1}" == "bootstrap" ]; then
boot=1
else
boot=0
fi
config_load swmodd
config_get bundle globals oci_bundle_root ""
config_get bridge globals lan_bridge "br-lan"
@ -165,8 +199,9 @@ start_service() {
if [ -f "${bundle}/ocicontainer" ]; then
UCI_CONFIG_DIR="${bundle}"
config_load ocicontainer
config_foreach configure_crun_container du_eu_assoc "${bundle}" "${bridge}"
uci_commit
config_foreach configure_crun_container du_eu_assoc "${bundle}" "${bridge}" "${boot}"
uci_commit ocicontainer
# Add a timer for DuStateChange!
(sleep 5 && ubus -t 5 call swmodules reload) &
fi