Commit 251c4860 authored by Martin Peres's avatar Martin Peres
Browse files

initscript: add information about the status of the pipeline

This information is shared to the container using environment
variables.

Closes: #1
parent 64385ebe
......@@ -71,7 +71,22 @@ Here is the list of available options:
* **b2c.shutdown_cmd**: Once the execution of all the containers is over,
execute this command. Default: `poweroff -f`
## How to?
## Environment variables provided to containers
You may provide environment variables to your container by simply specifying it
in the command line of the container:
b2c.container="-e VAR=VALUE -e VAR2=VALUE2 docker://hello-world"
Additionally, the following environment variables will be set, to help containers
decide what they want to do if a previous container failed:
* `B2C_PIPELINE_STATUS`: `0` if all the containers' exit codes were 0, otherwise the exit code of the first container than failed. **NOTE**: `b2c.post_container`'s do not affect this variable;
* `B2C_PIPELINE_FAILED_BY`: Command line of the first container that failed, or empty if `B2C_PIPELINE_STATUS` == 0. **NOTE**: `b2c.post_container`'s do not affect this variable;
* `B2C_PIPELINE_PREV_CONTAINER`: Command line of the previous container in the pipeline, or empty if this is the first container;
* `B2C_PIPELINE_PREV_CONTAINER_EXIT_CODE`: Exit code of the previous command, or empty if this is the first container.
## How to generate an initramfs?
Generating an initramfs is pretty simple, provided you have already setup
docker/podman. Just run the following command to generate the initramfs (
......
......@@ -295,9 +295,14 @@ function create_container {
# container, so just try multiple times, each time increasing the size of
# the hammer!
for i in 0 1 2 3 4; do
cmdline="podman create --rm --privileged --pull=always --network=host \
--runtime /bin/crun-no-pivot -e B2C_PIPELINE_STATUS=$B2C_PIPELINE_STATUS \
-e B2C_PIPELINE_FAILED_BY=\"$B2C_PIPELINE_FAILED_BY\" \
-e B2C_PIPELINE_PREV_CONTAINER=\"$B2C_PIPELINE_PREV_CONTAINER\" \
-e B2C_PIPELINE_PREV_CONTAINER_EXIT_CODE=\"$B2C_PIPELINE_PREV_CONTAINER_EXIT_CODE\""
# Set up the wanted container
container_id=`eval "podman create --rm --privileged --pull=always --network=host --runtime /bin/crun-no-pivot $@"` \
&& podman init "$container_id" && return 0
container_id=`eval "$cmdline $@"` && podman init "$container_id" && return 0
# The command failed... Ignore the first 3 times, as we want to check it
# is not a shortlived-network error
......@@ -330,6 +335,10 @@ function start_container {
exit_code=0
podman start -a "$container_id" || exit_code=$?
# Store the results of the execution
B2C_PIPELINE_PREV_CONTAINER="$@"
B2C_PIPELINE_PREV_CONTAINER_EXIT_CODE="$exit_code"
return $exit_code
}
......@@ -342,7 +351,13 @@ function start_containers {
if [ $exit_code -eq 0 ] ; then
log "The container run successfully, load the next one!"
else
log "The container exited with error code $?, aborting..."
# If this is the first container that failed, store that information
if [ $B2C_PIPELINE_STATUS -eq 0 ]; then
B2C_PIPELINE_STATUS="$exit_code"
B2C_PIPELINE_FAILED_BY="$container_params"
fi
log "The container exited with error code $exit_code, aborting..."
return 0
fi
done
......@@ -378,6 +393,18 @@ function container_cleanup {
podman image prune -f
}
function run_containers {
B2C_PIPELINE_STATUS="0"
B2C_PIPELINE_FAILED_BY=""
B2C_PIPELINE_PREV_CONTAINER=""
B2C_PIPELINE_PREV_CONTAINER_EXIT_CODE=""
setup_container_runtime
start_containers "$ARG_CONTAINER"
start_post_containers "$ARG_POST_CONTAINER"
container_cleanup
}
# Do not print all the early commands
set +x
......@@ -405,10 +432,7 @@ connect
ntp_set $ARG_NTP_PEER
# Start the containers
setup_container_runtime
start_containers "$ARG_CONTAINER"
start_post_containers "$ARG_POST_CONTAINER"
container_cleanup
run_containers
# Shutdown command
sync
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment