Skip to content

Commit

Permalink
Add a wrapper script for running Agent commands in a container
Browse files Browse the repository at this point in the history
  • Loading branch information
webbnh committed Mar 11, 2023
1 parent e3a98da commit 5a6c281
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions contrib/containerized-pbench/pbench
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#! /bin/bash
#
# This script is a wrapper to facilitate the invocation of a Pbench Agent
# command using a containerized deployment of the Pbench Agent. Simply prefix
# a Pbench Agent command line with the path to this script to run it inside a
# container, without needing to install the Agent on the host system.
#
# Invocation options are provided as environment variables:
# PB_AGENT_IMAGE_NAME: the full image name for the containerized Pbench Agent
# _PBENCH_AGENT_CONFIG: the location of the Pbench Agent configuration file
# PB_AGENT_RUN_DIR: the directory for use as the Pbench Agent "run directory"
# PB_AGENT_SERVER_LOC: the host and port for the Pbench Server
# PB_AGENT_PODMAN_OPTIONS: Additional options to be supplied to Podman run
#
# In all cases, reasonable defaults are supplied if the environment variables
# are not defined.
#
# This script checks for the presence of a `~/.ssh` directory, an existing
# Pbench Agent configuration file, and a Pbench Agent "run directory" and maps
# them into the container if they exist. If the configuration file is missing
# but the location of the Pbench Server is available, then this script will
# generate the configuration file, and the script creates the run directory if
# it does not exist. The script then invokes the Pbench Agent container with
# these options and any others which the user has specified and passes in the
# command to be executed.

image_name=${PB_AGENT_IMAGE_NAME:-quay.io/pbench/pbench-agent-all-centos-8:latest}
config_file=${_PBENCH_AGENT_CONFIG:-${HOME}/.config/pbench/pbench-agent.cfg}
pbench_run_dir=${PB_AGENT_RUN_DIR:-/var/tmp/${USER}/pbench-agent/run}
pbench_server=${PB_AGENT_SERVER_LOC}
other_options=${PB_AGENT_PODMAN_OPTIONS}

if [[ $# == 0 || $1 == "help" || $1 == "-h" || $1 == "--help" ]]; then
echo "Usage: ${0} <Pbench Agent Command> [<arg>...]" >&2
exit 2
fi

if [[ -d "${HOME}/.ssh" && -r "${HOME}/.ssh" ]]; then
other_options="-v ${HOME}/.ssh:/root/.ssh:z ${other_options}"
fi

if [[ -f "${config_file}" && -r "${config_file}" ]]; then
other_options="-v ${config_file}:/opt/pbench-agent/config/pbench-agent.cfg:z ${other_options}"
elif [[ -n "${pbench_server}" ]]; then
echo "Warning: the Pbench Agent config file is missing; attempting to generate one in ${config_file}" >&2
# TODO: this should be handled by a separate Pbench Agent command which
# provides a "configuration wizard".
mkdir -p $(dirname ${config_file})
cat > ${config_file} <<- EOF
[DEFAULT]
pbench_install_dir = /opt/pbench-agent
pbench_web_server = ${pbench_server}
[config]
path = %(pbench_install_dir)s/config
files = pbench-agent-default.cfg
EOF
else
echo "Warning: the Pbench Agent config file (e.g., ${config_file}) is missing or inaccessible -- using default configuration." >&2
fi

mkdir -p ${pbench_run_dir}
other_options="-v ${pbench_run_dir}:/var/lib/pbench-agent:z ${other_options}"

podman run \
-it \
--rm \
--network host \
--name pbench-agent \
${other_options} \
${image_name} "${@}"

0 comments on commit 5a6c281

Please sign in to comment.