Skip to content

Commit 69acd21

Browse files
authored
Merge pull request #1386 from kernelkit/container-restart-on-env
Container restart on env
2 parents 8e69973 + eeabb44 commit 69acd21

File tree

7 files changed

+50
-7
lines changed

7 files changed

+50
-7
lines changed

board/common/rootfs/usr/sbin/container

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ calc_sha()
6363
sha256sum "$1" 2>/dev/null | awk '{print $1}'
6464
}
6565

66+
# Calculate a combined SHA256 over the container script and its
67+
# optional env file. Environment variables are stored separately
68+
# from the script, so both must be included in the checksum to
69+
# detect configuration changes such as added/changed env vars.
70+
calc_config_sha()
71+
{
72+
_envfile="/run/containers/args/${name}.env"
73+
74+
if [ -f "$_envfile" ]; then
75+
cat "$1" "$_envfile" | sha256sum | awk '{print $1}'
76+
else
77+
calc_sha "$1"
78+
fi
79+
}
80+
6681
# Check image transport, return 0 if remote, 1 if local
6782
is_remote()
6883
{
@@ -124,7 +139,7 @@ is_uptodate()
124139
# If SHA matches, check container instance
125140
if [ "$stored_sha" = "$current_sha" ]; then
126141
if podman container exists "$name"; then
127-
config_sha=$(calc_sha "$script")
142+
config_sha=$(calc_config_sha "$script")
128143
container_sha=$(podman inspect "$name" --format '{{index .Config.Labels "config-sha256"}}' 2>/dev/null)
129144
container_img_sha=$(podman inspect "$name" --format '{{index .Config.Labels "meta-image-sha256"}}' 2>/dev/null)
130145

@@ -143,7 +158,7 @@ is_uptodate()
143158
else
144159
# Remote image optimization: check config-sha256 only
145160
if podman container exists "$name"; then
146-
config_sha=$(calc_sha "$script")
161+
config_sha=$(calc_config_sha "$script")
147162
container_sha=$(podman inspect "$name" --format '{{index .Config.Labels "config-sha256"}}' 2>/dev/null)
148163

149164
if [ "$container_sha" = "$config_sha" ]; then
@@ -459,7 +474,7 @@ create()
459474
fi
460475

461476
# Add config checksum label
462-
args="$args --label config-sha256=$(calc_sha "$script")"
477+
args="$args --label config-sha256=$(calc_config_sha "$script")"
463478
fi
464479

465480
# shellcheck disable=SC2048

doc/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Noteworthy changes and additions in this release are marked below in bold text.
7171
similar to IPv4, correctly mapping to the ietf-ip.yang model semantics
7272
- Fix #1082: Wi-Fi interfaces always scanned, introduce a `scan-mode` to the
7373
Wi-Fi concept in Infix
74+
- Fix #1313: Container is not restarted if environment variable is changed
7475
- Fix #1314: Raspberry Pi 4B with 1 or 8 GiB RAM does not boot. This was due
7576
newer EEPROM firmware in newer boards require a newer rpi-firmware package
7677
- Fix #1345: firewall not updating when interfaces become bridge/lag ports

doc/cli/keybindings.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ CLI has several keybindings, most significant first:
3131
| Ctrl-q | Ctrl-v | Insert next character literally |
3232
| Ctrl-r | | History, reversed interactive search (i-search) |
3333
| Ctrl-t | | Transpose/Swap characters before and at cursor |
34+
| Meta-# | Alt-Shift-3 | Prepend # to current line and submit to history |
3435

3536
## What is Meta?
3637

package/klish/klish.hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Locally calculated
22
sha256 9d9d33b873917ca5d0bdcc47a36d2fd385971ab0c045d1472fcadf95ee5bcf5b LICENCE
3-
sha256 39a73fdaa7e41001e804e2bbdebdc885da79d540f0246777e8fd1c0dd9fc9475 klish-1c31f50ab775d467fa18f2e0a798006949536a2a-git4.tar.gz
3+
sha256 cd9bc969350b8b30d9a7a31b0f19fb3218c602f04fe7e6a1d80682a75ed26d18 klish-3ae496c43d90354ffa94d364d7775c089f0e119a-git4.tar.gz

package/klish/klish.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
################################################################################
66

7-
KLISH_VERSION = 1c31f50ab775d467fa18f2e0a798006949536a2a
7+
KLISH_VERSION = 3ae496c43d90354ffa94d364d7775c089f0e119a
88
KLISH_SITE = https://github.com/kernelkit/klish.git
99
#KLISH_VERSION = tags/3.0.0
1010
#KLISH_SITE = https://src.libcode.org/pkun/klish.git

test/case/containers/environment/test.adoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ ifdef::topdoc[:imagesdir: {topdoc}../../test/case/containers/environment]
55
==== Description
66

77
Verify that environment variables can be set in container configuration
8-
and are available inside the running container.
8+
and are available inside the running container. Also verify that
9+
changing an environment variable triggers a container restart.
910

1011
1 Set up a container config with multiple environment variables
1112
2. Serve variables back to host using a CGI script in container
1213
3. Verify served content against environment variables
14+
4. Change an environment variable and verify the container restarts
1315

1416
==== Topology
1517

@@ -23,5 +25,7 @@ image::topology.svg[Container environment variables topology, align=center, scal
2325
. Verify container has started
2426
. Verify basic connectivity to data interface
2527
. Verify environment variables in CGI response
28+
. Change environment variable and verify container restarts
29+
. Verify container has restarted with updated env
2630

2731

test/case/containers/environment/test.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
Container environment variables
44
55
Verify that environment variables can be set in container configuration
6-
and are available inside the running container.
6+
and are available inside the running container. Also verify that
7+
changing an environment variable triggers a container restart.
78
89
1 Set up a container config with multiple environment variables
910
2. Serve variables back to host using a CGI script in container
1011
3. Verify served content against environment variables
12+
4. Change an environment variable and verify the container restarts
1113
"""
1214
import infamy
1315
from infamy.util import until, to_binary, curl
@@ -105,4 +107,24 @@
105107

106108
until(lambda: all(string in ns.call(lambda: curl(URL)) for string in expected_strings))
107109

110+
with test.step("Change environment variable and verify container restarts"):
111+
UPDATED_ENV_VARS = [
112+
{"key": "TEST_VAR", "value": "updated-value"},
113+
{"key": "APP_PORT", "value": "8080"},
114+
{"key": "DEBUG_MODE", "value": "true"},
115+
{"key": "PATH_WITH_SPACES", "value": "/path with spaces/test"}
116+
]
117+
118+
target.put_config_dict("infix-containers", {
119+
"containers": {
120+
"container": [{
121+
"name": f"{NAME}",
122+
"env": UPDATED_ENV_VARS,
123+
}]
124+
}
125+
})
126+
127+
with test.step("Verify container has restarted with updated env"):
128+
until(lambda: "TEST_VAR=updated-value" in ns.call(lambda: curl(URL)), attempts=60)
129+
108130
test.succeed()

0 commit comments

Comments
 (0)