diff --git a/contrib/containerized-pbench/pbench b/contrib/containerized-pbench/pbench new file mode 100755 index 0000000000..e6671a35a7 --- /dev/null +++ b/contrib/containerized-pbench/pbench @@ -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} [...]" >&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} "${@}"