tripleo-ansible/tripleo_ansible/roles/tripleo_container_manage/files/tripleo-start-podman-container

84 lines
3.1 KiB
Bash

#!/usr/bin/env bash
PODMAN=/usr/bin/podman
NAME=$1
if [ -z "$NAME" ]; then
echo "No name provided, cannot start container. Aborting" >&2
exit 1
fi
# Start container. Podman does not fail if container is already started
$PODMAN start $NAME
rc=$?
if [ $rc -ne 0 ]; then
echo "Error starting podman container $NAME: $rc" >&2
exit $rc
fi
# The environment can ben configured to create additional drop-in
# dependencies for the scopes associated with the container. This is
# done to prevent systemd from stopping the scopes early and break the
# configured dependencies in tripleo_*.services
# Stop here otherwise.
if [ ! -f "/etc/sysconfig/podman_drop_in" ]; then
exit 0
fi
# Retrieve the container's ID
# Note: currently the only API to retrieve the CID is either
# 1) via "podman inspect" but we don't want to use it because it can be
# very slow under IO load.
# 2) by running "podman start $NAME" but that command only returns the CID
# if the container is already running. Otherwise it returns the container
# name, which would break us.
# The only other means is via "podman ps". ps option "--filter" cannot
# enforce full name matches, so use grep instead and stop at first match.
CID=$($PODMAN ps --no-trunc --format '{{.ID}} {{.Names}}' | grep -F -w -m1 "$NAME" | cut -d' ' -f1)
if [ -z "$CID" ]; then
echo "Container ID not found for \"$NAME\". Not creating drop-in dependency" 2>&1
exit 1
else
echo "Creating additional drop-in dependency for \"$NAME\" ($CID)"
fi
# Note: a tripleo-ansible container has three systemd files associated with it:
# 1. tripleo_*.service - the regular systemd service generated by tripleo-ansible
# 2. libpod-conmon*.scope - created dynamically by podman. runs a conmon
# process that creates a pidfile for tripleo_*.service and monitor it.
# 3. libpod-*.scope - created dynamically by runc. for cgroups accounting
#
# tripleo-ansible can only set start/stop dependencies on 1., not 2. and 3.
# On reboot, systemd is allowed to stop 2. or 3. at any time, which can
# cause 1. to stop before its deps as set up by tripleo-ansible.
#
# To prevent an unexpected stop of 1. from happening, inject a dependency
# in 2. and 3. so that systemd is forbidden to stop those scopes
# automatically until tripleo-container-shutdown.service is stopped.
# That way, when systemd stops 1., the two scopes 2. and 3. will
# finish in sequence and tripleo-ansible dependencies will be respected.
for scope in "libpod-$CID.scope.d" "libpod-conmon-$CID.scope.d"; do
if [ $rc -eq 0 ] && [ ! -d /run/systemd/transient/"$scope" ]; then
mkdir -p /run/systemd/transient/"$scope" && \
echo -e "[Unit]\nBefore=tripleo-container-shutdown.service" > /run/systemd/transient/"$scope"/dep.conf && \
chmod ago+r /run/systemd/transient/"$scope" /run/systemd/transient/"$scope"/dep.conf
rc=$?
fi
done
if [ $rc -ne 0 ]; then
echo "Could not create drop-in dependency for \"$NAME\" ($CID)" >&2
exit 1
fi
systemctl daemon-reload
rc=$?
if [ $rc -ne 0 ]; then
echo "Could not refresh service definition after creating drop-in for \"$NAME\": $rc" >&2
exit 1
fi