Compare commits
1 Commits
master
...
7e5e445df2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e5e445df2 |
12
functions
12
functions
@@ -105,15 +105,3 @@ fixPermissions () {
|
|||||||
# Allow container access to the workdir (SELinux)
|
# Allow container access to the workdir (SELinux)
|
||||||
chcon -t container_file_t -R "$1"
|
chcon -t container_file_t -R "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
debug () {
|
|
||||||
|
|
||||||
[[ -n $_debug ]] && echo "debug: " "$@"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
silent () {
|
|
||||||
|
|
||||||
[[ -z $_silent ]] && echo "$@"
|
|
||||||
}
|
|
||||||
|
|||||||
304
plugins/podman/podmanRunEasy
Executable file
304
plugins/podman/podmanRunEasy
Executable file
@@ -0,0 +1,304 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# shellcheck disable=SC1090,SC2004
|
||||||
|
|
||||||
|
podmanRunEasy () {
|
||||||
|
|
||||||
|
sourcePlugin "podman/podmanRunWrapper"
|
||||||
|
|
||||||
|
|
||||||
|
########################
|
||||||
|
####### DEFAULTS #######
|
||||||
|
########################
|
||||||
|
|
||||||
|
# Can be overridden using environment variables
|
||||||
|
[[ -z $_image ]] && _image="fedora:latest"
|
||||||
|
[[ -z $_mode ]] && _mode="ephemeral"
|
||||||
|
[[ -z $_workdir ]] && _workdir="$PWD"
|
||||||
|
|
||||||
|
|
||||||
|
_smartDefaults () {
|
||||||
|
echo ""
|
||||||
|
### UNDER CONSTRUCTION ###
|
||||||
|
# I would like to handle some options automatically to reduce the number of configuration
|
||||||
|
# options necessary to run podman and also make it easy for users to define defaults based
|
||||||
|
# on file extension, command, shebang, etc
|
||||||
|
|
||||||
|
# check the basename for defaults
|
||||||
|
#[[ -z $COMMAND_DEF ]] \
|
||||||
|
# && COMMAND_BASENAME="${COMMAND_ARR[0]##*/}" \
|
||||||
|
# && _set_defaults "${COMMAND_BASENAME}"
|
||||||
|
|
||||||
|
# check the extension for defaults
|
||||||
|
#[[ -z $COMMAND_DEF ]] \
|
||||||
|
# && COMMAND_EXT="${COMMAND_BASENAME##*.}" \
|
||||||
|
# && _set_defaults "${COMMAND_EXT}"
|
||||||
|
|
||||||
|
# add defaults to the string
|
||||||
|
#if [[ -n $COMMAND_DEF ]]; then
|
||||||
|
# [[ -z $_silent ]] && echo "Loaded command defaults, executing with \"${COMMAND_DEF}\""
|
||||||
|
# COMMAND_STR="${COMMAND_DEF} ${COMMAND_STR}"
|
||||||
|
#else
|
||||||
|
# [[ -z $_silent ]] && echo "Could not find command defaults from name or extension"
|
||||||
|
# [[ -z $_silent ]] && echo "Executing script directly"
|
||||||
|
#fi
|
||||||
|
}
|
||||||
|
|
||||||
|
########################
|
||||||
|
###### FUNCTIONS #######
|
||||||
|
########################
|
||||||
|
|
||||||
|
_printHelpAndExit () {
|
||||||
|
|
||||||
|
cat <<-'EOF'
|
||||||
|
USAGE
|
||||||
|
podman-run-easy [-m _mode] [-w PATH] [-d PATH] [-i _image] [--systemd] [--mkexec] [--help]
|
||||||
|
[--silent] [--debug] [COMMANDS [ARGS...]]
|
||||||
|
|
||||||
|
COMMANDS
|
||||||
|
COMMANDS to run in the container (e.g. the current active file, an external
|
||||||
|
build script, a program residing in the container, etc.)
|
||||||
|
Can be empty (default entrypoint)
|
||||||
|
|
||||||
|
OPTIONS
|
||||||
|
--mode, -m MODE
|
||||||
|
MODE can be any of the following:
|
||||||
|
|
||||||
|
1. ephemeral
|
||||||
|
2. persistent
|
||||||
|
3. recreate-persistent
|
||||||
|
4. remove-persistent
|
||||||
|
|
||||||
|
--workdir, -w PATH
|
||||||
|
The directory in the container where we execute the COMMANDS
|
||||||
|
Default: The present working directory
|
||||||
|
|
||||||
|
--maskdir, -d PATH
|
||||||
|
Hide this directory from the host OS, store contents in the container only
|
||||||
|
|
||||||
|
--image, -i IMAGE
|
||||||
|
IMAGE used to create the container
|
||||||
|
Default: fedora:latest
|
||||||
|
|
||||||
|
--mkexec, -x
|
||||||
|
Makes the first argument of COMMANDS executable
|
||||||
|
|
||||||
|
--name, -n CONTAINER_NAME
|
||||||
|
This will form the base of the container name and should be unique to each project
|
||||||
|
Default: Container name will be set based on a concatenation of the image and commands
|
||||||
|
|
||||||
|
--systemd
|
||||||
|
Force container to init with systemd (--systemd=always)
|
||||||
|
Default: --systemd=true (systemd will only start if CMD is systemd, /usr/sbin/init or
|
||||||
|
/sbin/init)
|
||||||
|
|
||||||
|
--array, -a ARRAY
|
||||||
|
Read arguments from an existing or new ARRAY (bash >= 4.3)
|
||||||
|
This is useful to reduce parsing errors and recommended for build-wrapper plugins
|
||||||
|
|
||||||
|
--silent, -s
|
||||||
|
Don't output anything from this program (container output will still be passed to stdout
|
||||||
|
if -it option is used instead of -d, see `man podman run` for more information)
|
||||||
|
|
||||||
|
--help, -h
|
||||||
|
Print this help message and exit (overrides --silent)
|
||||||
|
|
||||||
|
EOF
|
||||||
|
|
||||||
|
# Exit using passed exit code
|
||||||
|
[[ -z $1 ]] && exit 0 || exit "$1"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
_parseInput () {
|
||||||
|
|
||||||
|
[[ -n $_debug ]] && echo "$@"
|
||||||
|
|
||||||
|
# Unset array variable when reparsing array
|
||||||
|
unset _array
|
||||||
|
|
||||||
|
# Parse input and set switches using getopt
|
||||||
|
if _input=$(getopt -o +m:w:d:i:a:n:xsh -l mode:,workdir:,maskdir:,image:,array:,name:,mkexec,systemd,silent,debug,help -- "$@"); then
|
||||||
|
eval set -- "$_input"
|
||||||
|
while true; do
|
||||||
|
case "$1" in
|
||||||
|
--mode|-m)
|
||||||
|
shift
|
||||||
|
_mode="$1"
|
||||||
|
;;
|
||||||
|
--workdir|-w)
|
||||||
|
shift
|
||||||
|
_workdir="$1"
|
||||||
|
;;
|
||||||
|
--maskdir|-d)
|
||||||
|
shift
|
||||||
|
_maskdir="$1"
|
||||||
|
;;
|
||||||
|
--image|-i)
|
||||||
|
shift
|
||||||
|
_image="$1"
|
||||||
|
;;
|
||||||
|
--name|-n)
|
||||||
|
shift
|
||||||
|
_name="$1"
|
||||||
|
;;
|
||||||
|
--array|-a)
|
||||||
|
shift
|
||||||
|
_array="$1"
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
--mkexec|-x)
|
||||||
|
_mkexec="true"
|
||||||
|
;;
|
||||||
|
--systemd|-s)
|
||||||
|
_systemd="true"
|
||||||
|
;;
|
||||||
|
--silent)
|
||||||
|
_silent="true"
|
||||||
|
;;
|
||||||
|
--debug)
|
||||||
|
_debug="true"
|
||||||
|
echo "Debugging on!"
|
||||||
|
;;
|
||||||
|
--help|-h)
|
||||||
|
_printHelpAndExit 0
|
||||||
|
;;
|
||||||
|
--)
|
||||||
|
shift
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
else
|
||||||
|
echo "Incorrect options provided"
|
||||||
|
_printHelpAndExit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# If array mode, load input array, reparse input, and return
|
||||||
|
if [[ -n $_array ]]; then
|
||||||
|
checkBashVersion
|
||||||
|
local _n_array
|
||||||
|
declare -n _n_array="$_array"
|
||||||
|
_parseInput "${_n_array[@]}"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Create _pre_commands_array from remaining arguments
|
||||||
|
# shift getopt parameters away
|
||||||
|
shift $((OPTIND - 1))
|
||||||
|
# create array
|
||||||
|
declare -ga _pre_commands_array
|
||||||
|
_pre_commands_array=("$@")
|
||||||
|
[[ -n $_debug ]] && echo "_pre_commands_array:" "${_pre_commands_array[@]}"
|
||||||
|
|
||||||
|
# Sanitize mode numbers
|
||||||
|
[[ "$_mode" == "1" ]] && _mode="ephemeral"
|
||||||
|
[[ "$_mode" == "2" ]] && _mode="persistent"
|
||||||
|
[[ "$_mode" == "3" ]] && _mode="recreate-persistent"
|
||||||
|
[[ "$_mode" == "4" ]] && _mode="remove-persistent"
|
||||||
|
|
||||||
|
# build the podman options array
|
||||||
|
declare -ga _pre_options_array
|
||||||
|
_pre_options_array+=("-it")
|
||||||
|
#[[ "$_mode" == "ephemeral" ]] && _pre_options_array+=("--rm")
|
||||||
|
_pre_options_array+=("-v" "${_workdir}:${_workdir}")
|
||||||
|
_pre_options_array+=("-w" "${_workdir}")
|
||||||
|
[[ -n $_maskdir ]] && _pre_options_array+=("-v" "${_maskdir}")
|
||||||
|
[[ -n $_systemd ]] && _pre_options_array+=("--systemd=always")
|
||||||
|
[[ -n $_debug ]] && echo "_pre_options_array:" "${_pre_options_array[@]}"
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
_makeExec () {
|
||||||
|
|
||||||
|
# make executable
|
||||||
|
if [[ -n $_mkexec ]]; then
|
||||||
|
# assume script/program is the first argument in the command and break on whitespace
|
||||||
|
_program="${_pre_commands_array[0]}"
|
||||||
|
# make executable on the host
|
||||||
|
chmod +x "${_program}"
|
||||||
|
# make executable in the container
|
||||||
|
#COMMAND="chmod 755 ${_program} && $COMMAND"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
_sanityChecks () {
|
||||||
|
|
||||||
|
# If missing, create workdir
|
||||||
|
if ! [[ -d "$_workdir" ]]; then
|
||||||
|
[[ -z $_silent ]] && echo "--workdir does not exist!"
|
||||||
|
[[ -z $_silent ]] && echo "Creating ${_workdir} now..."
|
||||||
|
mkdir -p "${_workdir}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Grant container SELinux access to the _workdir
|
||||||
|
fixPermissions "${_workdir}"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
_runPodmanRunWrapper() {
|
||||||
|
|
||||||
|
_makeExec
|
||||||
|
local _prw_array
|
||||||
|
_prw_array=("-m" "${_mode}" "--optionsarray" "_pre_options_array" "-i" "${_image}" "-n" "${_name}" "--commandsarray" "_pre_commands_array")
|
||||||
|
[[ -n $_debug ]] && _prw_array+=("--debug")
|
||||||
|
[[ -n $_silent ]] && _prw_array+=("--silent")
|
||||||
|
if [[ -n $_debug ]]; then
|
||||||
|
echo -n "PRE->PRW argument array: "
|
||||||
|
printf '"%s" ' "${_prw_array[@]}"
|
||||||
|
echo ""
|
||||||
|
fi
|
||||||
|
|
||||||
|
podmanRunWrapper -a _prw_array
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#########################
|
||||||
|
####### EXECUTE #########
|
||||||
|
#########################
|
||||||
|
|
||||||
|
_execute () {
|
||||||
|
|
||||||
|
# Get input
|
||||||
|
_parseInput "$@"
|
||||||
|
|
||||||
|
# Call podman-run-wrapper
|
||||||
|
_runPodmanRunWrapper
|
||||||
|
}
|
||||||
|
|
||||||
|
# Allow this function to be executed directly
|
||||||
|
_execute "$@"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Allow this file to be executed directly if not being sourced
|
||||||
|
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
||||||
|
|
||||||
|
# The following functions are usually handled by build-wrapper
|
||||||
|
|
||||||
|
_getBaseDir () {
|
||||||
|
# Get base directory name of where this script resides
|
||||||
|
# https://stackoverflow.com/questions/59895/how-to-get-the-source-directory-of-a-bash-script-from-within-the-script-itself#comment54598418_246128
|
||||||
|
_basedir=$(dirname "$(readlink -f "$0")")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
_sourceFunctions () {
|
||||||
|
# Get the location of this file
|
||||||
|
_getBaseDir
|
||||||
|
# Go up two directories
|
||||||
|
ff="${_basedir%/*/*}/functions"
|
||||||
|
# Source functions file
|
||||||
|
if [[ -f "$ff" ]]; then
|
||||||
|
source "$ff"
|
||||||
|
else
|
||||||
|
echo "Cannot find functions file: ${ff}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
_sourceFunctions
|
||||||
|
podmanRunEasy "$@"
|
||||||
|
fi
|
||||||
277
plugins/podman/podmanRunWrapper
Executable file
277
plugins/podman/podmanRunWrapper
Executable file
@@ -0,0 +1,277 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# shellcheck disable=SC1090,SC2004
|
||||||
|
|
||||||
|
podmanRunWrapper () {
|
||||||
|
|
||||||
|
########################
|
||||||
|
###### FUNCTIONS #######
|
||||||
|
########################
|
||||||
|
|
||||||
|
_printHelpAndExit () {
|
||||||
|
|
||||||
|
if [[ -z $_debug ]]; then
|
||||||
|
cat <<-'EOF'
|
||||||
|
USAGE
|
||||||
|
Argument mode:
|
||||||
|
podman-run-wrapper -m MODE -o OPTIONS -i IMAGE [-n CONTAINER_NAME]
|
||||||
|
[--help] [--silent] [--debug] [COMMANDS [ARGS...]]
|
||||||
|
|
||||||
|
Array mode (bash >= 4.3):
|
||||||
|
podman-run-wrapper -a ARRAY
|
||||||
|
|
||||||
|
EXAMPLE
|
||||||
|
podman-run-wrapper -m ephemeral -o "-it -v $PWD:$PWD -w $PWD" -i "php:latest" -c "php ./script.php"
|
||||||
|
|
||||||
|
ARRAY=( "-m" "ephemeral" "-o" "--rm -it -v $PWD:$PWD -w $PWD" "-i" "php:latest" "-c" "php ./script.php")
|
||||||
|
podman-run-wrapper -a ARRAY
|
||||||
|
|
||||||
|
COMMANDS
|
||||||
|
COMMANDS to run in the container (e.g. the current active file, an external build script, a
|
||||||
|
program residing in the container, etc.)
|
||||||
|
Can be empty (run default container entrypoint)
|
||||||
|
|
||||||
|
OPTIONS
|
||||||
|
--mode, -m MODE
|
||||||
|
MODE can be any of the following:
|
||||||
|
|
||||||
|
1. ephemeral
|
||||||
|
2. persistent
|
||||||
|
3. recreate-persistent
|
||||||
|
4. remove-persistent
|
||||||
|
|
||||||
|
--options, -o OPTIONS
|
||||||
|
OPTIONS to pass directly to `podman run` or `podman exec` depending on the mode or
|
||||||
|
container state
|
||||||
|
|
||||||
|
--image, -i IMAGE
|
||||||
|
IMAGE used to create the container
|
||||||
|
|
||||||
|
--array, -a ARRAY
|
||||||
|
Read arguments array named ARRAY (bash >= 4.3)
|
||||||
|
This is useful to reduce parsing errors and recommended for build-wrapper plugins
|
||||||
|
|
||||||
|
--optionsarray OPTIONS_ARRAY
|
||||||
|
Read podman options from array named OPTIONS_ARRAY (bash >= 4.3)
|
||||||
|
This is useful to reduce parsing errors and recommended for build-wrapper plugins
|
||||||
|
|
||||||
|
--commandsarray COMMANDS_ARRAY
|
||||||
|
Read container commands from array named COMMANDS_ARRAY (bash >= 4.3)
|
||||||
|
This is useful to reduce parsing errors and recommended for build-wrapper plugins
|
||||||
|
|
||||||
|
--name, -n CONTAINER_NAME
|
||||||
|
Use the CONTAINER_NAME base to name containers (should be unique to each project)
|
||||||
|
Default: Container name will be set based on a concatenation of the image and commands
|
||||||
|
|
||||||
|
--selinuxfix
|
||||||
|
A temporary hack to grant SELinux write access on $PWD until a better fix is found
|
||||||
|
|
||||||
|
--silent, -s
|
||||||
|
Only print errors
|
||||||
|
|
||||||
|
--debug, -d
|
||||||
|
Print debugging
|
||||||
|
|
||||||
|
--help, -h
|
||||||
|
Print this help message and exit
|
||||||
|
|
||||||
|
EOF
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Exit using passed exit code
|
||||||
|
[[ -z $1 ]] && exit 0 || exit "$1"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Parse input
|
||||||
|
_parseInput () {
|
||||||
|
|
||||||
|
unset _mode _cmds_arr _opts_arr _options _prw_opts_arr _image _name _array _selinux_fix
|
||||||
|
|
||||||
|
# Use getopt to print help
|
||||||
|
if INPUT=$(getopt -o +m:o:i:x:n:a:sdh -l mode:,options:,image:,name:,array:,optionsarray:,commandsarray:,selinuxfix,silent,debug,help -- "$@"); then
|
||||||
|
eval set -- "$INPUT"
|
||||||
|
while true; do
|
||||||
|
case "$1" in
|
||||||
|
--mode|-m)
|
||||||
|
shift
|
||||||
|
_mode="$1"
|
||||||
|
;;
|
||||||
|
--options|-o)
|
||||||
|
shift
|
||||||
|
_options="$1"
|
||||||
|
;;
|
||||||
|
--image|-i)
|
||||||
|
shift
|
||||||
|
_image="$1"
|
||||||
|
;;
|
||||||
|
--name|-n)
|
||||||
|
shift
|
||||||
|
_name="$1"
|
||||||
|
;;
|
||||||
|
--array|-a)
|
||||||
|
shift
|
||||||
|
_array="$1"
|
||||||
|
;;
|
||||||
|
--optionsarray)
|
||||||
|
shift
|
||||||
|
_opts_arr="$1"
|
||||||
|
;;
|
||||||
|
--commandsarray)
|
||||||
|
shift
|
||||||
|
_cmds_arr="$1"
|
||||||
|
;;
|
||||||
|
--selinuxfix)
|
||||||
|
_selinux_fix="1"
|
||||||
|
;;
|
||||||
|
--help|-h)
|
||||||
|
_printHelpAndExit 0
|
||||||
|
;;
|
||||||
|
--silent|-s)
|
||||||
|
_silent="1"
|
||||||
|
;;
|
||||||
|
--debug|-d)
|
||||||
|
_debug="1"
|
||||||
|
echo "Debugging on!"
|
||||||
|
;;
|
||||||
|
--)
|
||||||
|
shift
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
else
|
||||||
|
echo "Incorrect options provided!"
|
||||||
|
_printHelpAndExit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# If array mode, load and parse input array
|
||||||
|
if [[ -n $_array ]]; then
|
||||||
|
checkBashVersion
|
||||||
|
local _n_array
|
||||||
|
declare -n _n_array="$_array"
|
||||||
|
_parseInput "${_n_array[@]}"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Parse podman options
|
||||||
|
if [[ -n $_opts_arr ]]; then
|
||||||
|
# namerefs are awesome
|
||||||
|
declare -gn _prw_opts_arr="$_opts_arr"
|
||||||
|
# If not array mode optionally load podman options from input string
|
||||||
|
elif [[ -n $_options ]]; then
|
||||||
|
declare -ga _prw_opts_arr
|
||||||
|
for _option in $_options; do
|
||||||
|
_prw_opts_arr+=("$_option")
|
||||||
|
done
|
||||||
|
else
|
||||||
|
echo "Must provide --options or the name of an existing --optionsarray"
|
||||||
|
_printHelpAndExit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Parse commands
|
||||||
|
if [[ -n $_cmds_arr ]]; then
|
||||||
|
declare -gn _prw_cmds_arr="$_cmds_arr"
|
||||||
|
else
|
||||||
|
# Create COMMANDS array from remaining arguments
|
||||||
|
# shift getopt parameters away
|
||||||
|
shift $((OPTIND - 1))
|
||||||
|
# create array
|
||||||
|
declare -ga _prw_cmds_arr
|
||||||
|
_prw_cmds_arr=("$@")
|
||||||
|
if [[ ${#_prw_cmds_arr[@]} -lt 1 ]]; then
|
||||||
|
[[ -z $_silent ]] && echo "Warning: running container without any commands"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
[[ -n $_debug ]] && echo "_prw_opts_arr:" "${_prw_opts_arr[@]}"
|
||||||
|
[[ -n $_debug ]] && echo "_prw_cmds_arr:" "${_prw_cmds_arr[@]}"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
_addCName () {
|
||||||
|
|
||||||
|
# autogenerate _name if missing
|
||||||
|
[[ -z $_name ]] && _name="${_image}${_prw_cmds_arr[*]}"
|
||||||
|
|
||||||
|
# sanitize container name
|
||||||
|
_name="${_name//_/}" && _name="${_name// /}" && \
|
||||||
|
_name="${_name//[^a-zA-Z0-9_]/}" && _name="${_name,,}"
|
||||||
|
|
||||||
|
# append flag
|
||||||
|
if [[ "$_mode" == "ephemeral" ]]; then
|
||||||
|
_cname="prw-e-$_name"
|
||||||
|
else
|
||||||
|
_cname="prw-p-$_name"
|
||||||
|
fi
|
||||||
|
_prw_opts_arr+=("--name" "$_cname")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
_removeContainer () {
|
||||||
|
|
||||||
|
if podman container exists "$_cname"; then
|
||||||
|
[[ -z $_silent ]] && echo "Removing container: $_cname"
|
||||||
|
[[ -n $_debug ]] && echo "podman rm -v -f $_cname"
|
||||||
|
podman rm -v -f "$_cname"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
_runContainer () {
|
||||||
|
|
||||||
|
# Run _remove_container first to not run in existing container
|
||||||
|
if podman container exists "${_cname}"; then
|
||||||
|
[[ -z $_silent ]] && echo "Reusing container: $_cname"
|
||||||
|
[[ -n $_debug ]] && echo podman exec "$_cname" sh -c "${_prw_cmds_arr[@]}"
|
||||||
|
podman exec "$_cname" sh -c "${_prw_cmds_arr[@]}"
|
||||||
|
exit $?
|
||||||
|
else
|
||||||
|
[[ -z $_silent ]] && echo "Running in container: $_cname"
|
||||||
|
[[ -n $_debug ]] && echo "Command: podman run" "${_prw_opts_arr[@]}" "$_image" sh -c "${_prw_cmds_arr[@]}"
|
||||||
|
podman run "${_prw_opts_arr[@]}" "$_image" "${_prw_cmds_arr[@]}"
|
||||||
|
exit $?
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#########################
|
||||||
|
####### EXECUTE #########
|
||||||
|
#########################
|
||||||
|
|
||||||
|
_execute () {
|
||||||
|
|
||||||
|
# Get input
|
||||||
|
_parseInput "$@"
|
||||||
|
|
||||||
|
# Set container name
|
||||||
|
_addCName
|
||||||
|
|
||||||
|
# SELinux fix
|
||||||
|
[[ -n $_selinux_fix ]] && fixPermissions "$PWD"
|
||||||
|
|
||||||
|
# Execute podman
|
||||||
|
if [[ $_mode == "ephemeral" || $_mode == "recreate-persistent" ]]; then
|
||||||
|
_removeContainer
|
||||||
|
_runContainer
|
||||||
|
elif [[ $_mode == "remove-persistent" ]]; then
|
||||||
|
_removeContainer
|
||||||
|
elif [[ $_mode == "persistent" ]]; then
|
||||||
|
_runContainer
|
||||||
|
else
|
||||||
|
echo "Unknown mode!"
|
||||||
|
_printHelpAndExit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Allow this function to be executed directly
|
||||||
|
_execute "$@"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Allow script to be called directly
|
||||||
|
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
|
||||||
|
# No imported functions
|
||||||
|
#source-functions
|
||||||
|
podmanRunWrapper "$@"
|
||||||
|
fi
|
||||||
@@ -51,7 +51,7 @@ podmanRunEasy () {
|
|||||||
|
|
||||||
cat <<-'EOF'
|
cat <<-'EOF'
|
||||||
USAGE
|
USAGE
|
||||||
podmanRunEasy [-m _mode] [-w PATH] [-d PATH] [-i _image] [--userfix] [--mkexec] [--help]
|
podman-run-easy [-m _mode] [-w PATH] [-d PATH] [-i _image] [--systemd] [--mkexec] [--help]
|
||||||
[--silent] [--debug] [COMMANDS [ARGS...]]
|
[--silent] [--debug] [COMMANDS [ARGS...]]
|
||||||
|
|
||||||
COMMANDS
|
COMMANDS
|
||||||
@@ -86,6 +86,11 @@ OPTIONS
|
|||||||
This will form the base of the container name and should be unique to each project
|
This will form the base of the container name and should be unique to each project
|
||||||
Default: Container name will be set based on a concatenation of the image and commands
|
Default: Container name will be set based on a concatenation of the image and commands
|
||||||
|
|
||||||
|
--systemd
|
||||||
|
Force container to init with systemd (--systemd=always)
|
||||||
|
Default: --systemd=true (systemd will only start if CMD is systemd, /usr/sbin/init or
|
||||||
|
/sbin/init)
|
||||||
|
|
||||||
--array, -a ARRAY
|
--array, -a ARRAY
|
||||||
Read arguments from an existing or new ARRAY (bash >= 4.3)
|
Read arguments from an existing or new ARRAY (bash >= 4.3)
|
||||||
This is useful to reduce parsing errors and recommended for build-wrapper plugins
|
This is useful to reduce parsing errors and recommended for build-wrapper plugins
|
||||||
@@ -112,7 +117,7 @@ EOF
|
|||||||
unset _array
|
unset _array
|
||||||
|
|
||||||
# Parse input and set switches using getopt
|
# Parse input and set switches using getopt
|
||||||
if _input=$(getopt -o +m:w:d:i:a:n:xh -l mode:,workdir:,maskdir:,image:,array:,name:,mkexec,silent,debug,help -- "$@"); then
|
if _input=$(getopt -o +m:w:d:i:a:n:xsh -l mode:,workdir:,maskdir:,image:,array:,name:,mkexec,systemd,silent,debug,help -- "$@"); then
|
||||||
eval set -- "$_input"
|
eval set -- "$_input"
|
||||||
while true; do
|
while true; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
@@ -144,6 +149,9 @@ EOF
|
|||||||
--mkexec|-x)
|
--mkexec|-x)
|
||||||
_mkexec="true"
|
_mkexec="true"
|
||||||
;;
|
;;
|
||||||
|
--systemd|-s)
|
||||||
|
_systemd="true"
|
||||||
|
;;
|
||||||
--silent)
|
--silent)
|
||||||
_silent="true"
|
_silent="true"
|
||||||
;;
|
;;
|
||||||
@@ -193,10 +201,10 @@ EOF
|
|||||||
declare -ga _pre_options_array
|
declare -ga _pre_options_array
|
||||||
_pre_options_array+=("-it")
|
_pre_options_array+=("-it")
|
||||||
#[[ "$_mode" == "ephemeral" ]] && _pre_options_array+=("--rm")
|
#[[ "$_mode" == "ephemeral" ]] && _pre_options_array+=("--rm")
|
||||||
_pre_options_array+=("-v" "${_workdir}:${_workdir}:Z")
|
_pre_options_array+=("-v" "${_workdir}:${_workdir}")
|
||||||
_pre_options_array+=("-w" "${_workdir}")
|
_pre_options_array+=("-w" "${_workdir}")
|
||||||
_pre_options_array+=("--userns=keep-id") # Fix user permission problems by default
|
|
||||||
[[ -n $_maskdir ]] && _pre_options_array+=("-v" "${_maskdir}")
|
[[ -n $_maskdir ]] && _pre_options_array+=("-v" "${_maskdir}")
|
||||||
|
[[ -n $_systemd ]] && _pre_options_array+=("--systemd=always")
|
||||||
[[ -n $_debug ]] && echo "_pre_options_array:" "${_pre_options_array[@]}"
|
[[ -n $_debug ]] && echo "_pre_options_array:" "${_pre_options_array[@]}"
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,17 +13,17 @@ podmanRunWrapper() {
|
|||||||
cat <<-'EOF'
|
cat <<-'EOF'
|
||||||
USAGE
|
USAGE
|
||||||
Argument mode:
|
Argument mode:
|
||||||
podmanRunWrapper -m MODE -o OPTIONS -i IMAGE [-n CONTAINER_NAME] [--help]
|
podman-run-wrapper -m MODE -o OPTIONS -i IMAGE [-n CONTAINER_NAME]
|
||||||
[--debug] [COMMANDS [ARGS...]]
|
[--help] [--silent] [--debug] [COMMANDS [ARGS...]]
|
||||||
|
|
||||||
Array mode (bash >= 4.3):
|
Array mode (bash >= 4.3):
|
||||||
podmanRunWrapper -a ARRAY
|
podman-run-wrapper -a ARRAY
|
||||||
|
|
||||||
EXAMPLE
|
EXAMPLE
|
||||||
podmanRunWrapper -m ephemeral -o "-it -v $PWD:$PWD -w $PWD" -i "php:latest" -c "php ./script.php"
|
podman-run-wrapper -m ephemeral -o "-it -v $PWD:$PWD -w $PWD" -i "php:latest" -c "php ./script.php"
|
||||||
|
|
||||||
ARRAY=( "-m" "ephemeral" "-o" "--rm -it -v $PWD:$PWD -w $PWD" "-i" "php:latest" "-c" "php ./script.php")
|
ARRAY=( "-m" "ephemeral" "-o" "--rm -it -v $PWD:$PWD -w $PWD" "-i" "php:latest" "-c" "php ./script.php")
|
||||||
podmanRunWrapper -a ARRAY
|
podman-run-wrapper -a ARRAY
|
||||||
|
|
||||||
COMMANDS
|
COMMANDS
|
||||||
COMMANDS to run in the container (e.g. the current active file, an external build script, a
|
COMMANDS to run in the container (e.g. the current active file, an external build script, a
|
||||||
@@ -37,6 +37,7 @@ OPTIONS
|
|||||||
1. ephemeral
|
1. ephemeral
|
||||||
2. persistent
|
2. persistent
|
||||||
3. recreate-persistent
|
3. recreate-persistent
|
||||||
|
4. remove-persistent
|
||||||
|
|
||||||
--options, -o OPTIONS
|
--options, -o OPTIONS
|
||||||
OPTIONS to pass directly to `podman run` or `podman exec` depending on the mode or
|
OPTIONS to pass directly to `podman run` or `podman exec` depending on the mode or
|
||||||
@@ -64,6 +65,9 @@ OPTIONS
|
|||||||
--selinuxfix
|
--selinuxfix
|
||||||
A temporary hack to grant SELinux write access on $PWD until a better fix is found
|
A temporary hack to grant SELinux write access on $PWD until a better fix is found
|
||||||
|
|
||||||
|
--silent, -s
|
||||||
|
Only print errors
|
||||||
|
|
||||||
--debug, -d
|
--debug, -d
|
||||||
Print debugging
|
Print debugging
|
||||||
|
|
||||||
@@ -84,7 +88,7 @@ EOF
|
|||||||
unset _mode _cmds_arr _opts_arr _options _prw_opts_arr _image _name _array _selinux_fix
|
unset _mode _cmds_arr _opts_arr _options _prw_opts_arr _image _name _array _selinux_fix
|
||||||
|
|
||||||
# Use getopt to print help
|
# Use getopt to print help
|
||||||
if INPUT=$(getopt -o +m:o:i:x:n:a:dh -l mode:,options:,image:,name:,array:,optionsarray:,commandsarray:,selinuxfix,debug,help -- "$@"); then
|
if INPUT=$(getopt -o +m:o:i:x:n:a:sdh -l mode:,options:,image:,name:,array:,optionsarray:,commandsarray:,selinuxfix,silent,debug,help -- "$@"); then
|
||||||
eval set -- "$INPUT"
|
eval set -- "$INPUT"
|
||||||
while true; do
|
while true; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
@@ -94,7 +98,7 @@ EOF
|
|||||||
;;
|
;;
|
||||||
--options|-o)
|
--options|-o)
|
||||||
shift
|
shift
|
||||||
_options+=("$1")
|
_options="$1"
|
||||||
;;
|
;;
|
||||||
--image|-i)
|
--image|-i)
|
||||||
shift
|
shift
|
||||||
@@ -122,8 +126,11 @@ EOF
|
|||||||
--help|-h)
|
--help|-h)
|
||||||
_printHelpAndExit 0
|
_printHelpAndExit 0
|
||||||
;;
|
;;
|
||||||
|
--silent|-s)
|
||||||
|
_silent="1"
|
||||||
|
;;
|
||||||
--debug|-d)
|
--debug|-d)
|
||||||
export _debug="1"
|
_debug="1"
|
||||||
echo "Debugging on!"
|
echo "Debugging on!"
|
||||||
;;
|
;;
|
||||||
--)
|
--)
|
||||||
@@ -148,13 +155,16 @@ EOF
|
|||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Parse podman options from --optionsarray
|
# Parse podman options
|
||||||
if [[ -n $_opts_arr ]]; then
|
if [[ -n $_opts_arr ]]; then
|
||||||
# namerefs are awesome
|
# namerefs are awesome
|
||||||
declare -gn _prw_opts_arr="$_opts_arr"
|
declare -gn _prw_opts_arr="$_opts_arr"
|
||||||
# If no array given, parse input from options
|
# If not array mode optionally load podman options from input string
|
||||||
elif [[ ${#_options[@]} -ge 1 ]]; then
|
elif [[ -n $_options ]]; then
|
||||||
declare -ga _prw_opts_arr=("${_options[@]}")
|
declare -ga _prw_opts_arr
|
||||||
|
for _option in $_options; do
|
||||||
|
_prw_opts_arr+=("$_option")
|
||||||
|
done
|
||||||
else
|
else
|
||||||
echo "Must provide --options or the name of an existing --optionsarray"
|
echo "Must provide --options or the name of an existing --optionsarray"
|
||||||
_printHelpAndExit 1
|
_printHelpAndExit 1
|
||||||
@@ -171,12 +181,12 @@ EOF
|
|||||||
declare -ga _prw_cmds_arr
|
declare -ga _prw_cmds_arr
|
||||||
_prw_cmds_arr=("$@")
|
_prw_cmds_arr=("$@")
|
||||||
if [[ ${#_prw_cmds_arr[@]} -lt 1 ]]; then
|
if [[ ${#_prw_cmds_arr[@]} -lt 1 ]]; then
|
||||||
debug "Running container without any commands"
|
[[ -z $_silent ]] && echo "Warning: running container without any commands"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
debug "_prw_opts_arr:" "${_prw_opts_arr[@]}"
|
[[ -n $_debug ]] && echo "_prw_opts_arr:" "${_prw_opts_arr[@]}"
|
||||||
debug "_prw_cmds_arr:" "${_prw_cmds_arr[@]}"
|
[[ -n $_debug ]] && echo "_prw_cmds_arr:" "${_prw_cmds_arr[@]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -201,9 +211,10 @@ EOF
|
|||||||
|
|
||||||
_removeContainer () {
|
_removeContainer () {
|
||||||
|
|
||||||
if podman container exists "$1"; then
|
if podman container exists "$_cname"; then
|
||||||
debug "podman rm -v -f $1"
|
[[ -z $_silent ]] && echo "Removing container: $_cname"
|
||||||
podman rm -v -f "$1"
|
[[ -n $_debug ]] && echo "podman rm -v -f $_cname"
|
||||||
|
podman rm -v -f "$_cname"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -211,12 +222,14 @@ EOF
|
|||||||
_runContainer () {
|
_runContainer () {
|
||||||
|
|
||||||
# Run _remove_container first to not run in existing container
|
# Run _remove_container first to not run in existing container
|
||||||
if podman container exists "$1"; then
|
if podman container exists "${_cname}"; then
|
||||||
debug podman exec "$1" sh -c "${_prw_cmds_arr[@]}"
|
[[ -z $_silent ]] && echo "Reusing container: $_cname"
|
||||||
podman exec "$1" sh -c "${_prw_cmds_arr[@]}"
|
[[ -n $_debug ]] && echo podman exec "$_cname" sh -c "${_prw_cmds_arr[@]}"
|
||||||
|
podman exec "$_cname" sh -c "${_prw_cmds_arr[@]}"
|
||||||
exit $?
|
exit $?
|
||||||
else
|
else
|
||||||
debug "Command: podman run" "${_prw_opts_arr[@]}" "$_image" sh -c "${_prw_cmds_arr[@]}"
|
[[ -z $_silent ]] && echo "Running in container: $_cname"
|
||||||
|
[[ -n $_debug ]] && echo "Command: podman run" "${_prw_opts_arr[@]}" "$_image" sh -c "${_prw_cmds_arr[@]}"
|
||||||
podman run "${_prw_opts_arr[@]}" "$_image" "${_prw_cmds_arr[@]}"
|
podman run "${_prw_opts_arr[@]}" "$_image" "${_prw_cmds_arr[@]}"
|
||||||
exit $?
|
exit $?
|
||||||
fi
|
fi
|
||||||
@@ -227,7 +240,7 @@ EOF
|
|||||||
####### EXECUTE #########
|
####### EXECUTE #########
|
||||||
#########################
|
#########################
|
||||||
|
|
||||||
__main() {
|
_execute () {
|
||||||
|
|
||||||
# Get input
|
# Get input
|
||||||
_parseInput "$@"
|
_parseInput "$@"
|
||||||
@@ -239,40 +252,26 @@ EOF
|
|||||||
[[ -n $_selinux_fix ]] && fixPermissions "$PWD"
|
[[ -n $_selinux_fix ]] && fixPermissions "$PWD"
|
||||||
|
|
||||||
# Execute podman
|
# Execute podman
|
||||||
if [[ "$_mode" =~ ^(ephemeral|recreate-persistent)$ ]]; then
|
if [[ $_mode == "ephemeral" || $_mode == "recreate-persistent" ]]; then
|
||||||
_removeContainer "$_cname"
|
_removeContainer
|
||||||
|
_runContainer
|
||||||
|
elif [[ $_mode == "remove-persistent" ]]; then
|
||||||
|
_removeContainer
|
||||||
|
elif [[ $_mode == "persistent" ]]; then
|
||||||
|
_runContainer
|
||||||
|
else
|
||||||
|
echo "Unknown mode!"
|
||||||
|
_printHelpAndExit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
_runContainer "$_cname"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Allow this function to be executed directly
|
# Allow this function to be executed directly
|
||||||
__main "$@"
|
_execute "$@"
|
||||||
exit $?
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Allow script to be called directly
|
# Allow script to be called directly
|
||||||
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
|
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
|
||||||
|
# No imported functions
|
||||||
_getBaseDir () {
|
#source-functions
|
||||||
# Get base directory name of where this script resides
|
|
||||||
# https://stackoverflow.com/questions/59895/how-to-get-the-source-directory-of-a-bash-script-from-within-the-script-itself#comment54598418_246128
|
|
||||||
_basedir=$(dirname "$(readlink -f "$0")")
|
|
||||||
}
|
|
||||||
|
|
||||||
_sourceFunctions () {
|
|
||||||
# Get the location of this file
|
|
||||||
_getBaseDir
|
|
||||||
# Go up two directories
|
|
||||||
ff="${_basedir%/*/*}/functions"
|
|
||||||
# Source functions file
|
|
||||||
if [[ -f "$ff" ]]; then
|
|
||||||
source "$ff"
|
|
||||||
else
|
|
||||||
echo "Cannot find functions file: ${ff}"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
_sourceFunctions
|
|
||||||
podmanRunWrapper "$@"
|
podmanRunWrapper "$@"
|
||||||
fi
|
fi
|
||||||
|
|||||||
@@ -1,241 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
# shellcheck disable=SC1090
|
|
||||||
|
|
||||||
toolboxRun () {
|
|
||||||
|
|
||||||
_printHelpAndExit () {
|
|
||||||
|
|
||||||
cat <<-'EOF'
|
|
||||||
USAGE
|
|
||||||
toolboxRun -c NAME [-i NAME] [-r RELEASE] [--no-sh] [-h] [-s] [-d] [COMMANDS [ARGS...]]
|
|
||||||
|
|
||||||
COMMANDS
|
|
||||||
|
|
||||||
COMMANDS to run in the container (e.g. the current active file, an external
|
|
||||||
build script, a program residing in the container, etc.)
|
|
||||||
Can be empty (default entrypoint)
|
|
||||||
|
|
||||||
OPTIONS
|
|
||||||
|
|
||||||
--container NAME, -c NAME
|
|
||||||
Assign a different NAME to the toolbox container. This is useful for creating multiple
|
|
||||||
toolbox containers from the same base image, or for entirely customized containers from
|
|
||||||
custom-built base images.
|
|
||||||
|
|
||||||
If the toolbox container NAME already exists, the command passed to toolboxRun will be
|
|
||||||
executed in the existing toolbox. If toolbox container NAME does not exist, it will be
|
|
||||||
created and the COMMAND will then be run in it.
|
|
||||||
|
|
||||||
--image NAME, -i NAME
|
|
||||||
Change the NAME of the base image used to create the toolbox container. This is useful for
|
|
||||||
creating containers from custom-built base images.
|
|
||||||
|
|
||||||
--release RELEASE, -r RELEASE
|
|
||||||
Create a toolbox container for a different operating system RELEASE than the host.
|
|
||||||
|
|
||||||
--ephemeral
|
|
||||||
The toolbox will be removed after the COMMAND is executed
|
|
||||||
|
|
||||||
--recreate
|
|
||||||
If the toolbox NAME already exists, it will first be removed and recreated
|
|
||||||
|
|
||||||
--no-sh, -n
|
|
||||||
Makes the first argument of COMMANDS executable and runs it directly
|
|
||||||
|
|
||||||
--debug, -d
|
|
||||||
Display debugging output
|
|
||||||
|
|
||||||
--array, -a ARRAY
|
|
||||||
Read arguments from an existing or new ARRAY (bash >= 4.3)
|
|
||||||
This is useful to reduce parsing errors and recommended for build-wrapper plugins
|
|
||||||
|
|
||||||
--help, -h
|
|
||||||
Print this help message and exit (overrides --silent)
|
|
||||||
|
|
||||||
EOF
|
|
||||||
|
|
||||||
# Exit using passed exit code
|
|
||||||
[[ -z $1 ]] && exit 0 || exit "$1"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
debug () {
|
|
||||||
|
|
||||||
[[ -n $_debug ]] && echo "debug: " "$@"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
_parseInput () {
|
|
||||||
|
|
||||||
debug "${FUNCNAME[0]}" "$@"
|
|
||||||
|
|
||||||
# Unset vars
|
|
||||||
unset _array
|
|
||||||
|
|
||||||
# Parse input and set switches using getopt
|
|
||||||
if _input=$(getopt -o +c:i:r:nda:h -l container:,image:,release:,ephemeral,recreate,no-sh,debug,array:,help -- "$@"); then
|
|
||||||
eval set -- "$_input"
|
|
||||||
while true; do
|
|
||||||
case "$1" in
|
|
||||||
--container|-c)
|
|
||||||
shift && _cname="$1"
|
|
||||||
;;
|
|
||||||
--image|-i)
|
|
||||||
shift && _image=("-i" "$1")
|
|
||||||
;;
|
|
||||||
--release|-r)
|
|
||||||
shift && _release=("-r" "$1")
|
|
||||||
;;
|
|
||||||
--ephemeral)
|
|
||||||
_ephemeral="true"
|
|
||||||
;;
|
|
||||||
--recreate)
|
|
||||||
_recreate="true"
|
|
||||||
;;
|
|
||||||
--no-sh|-n)
|
|
||||||
_no_sh="true"
|
|
||||||
;;
|
|
||||||
--debug|-d)
|
|
||||||
_debug="true"
|
|
||||||
echo "Debugging on!"
|
|
||||||
;;
|
|
||||||
--array|-a)
|
|
||||||
shift && _array="$1"
|
|
||||||
break
|
|
||||||
;;
|
|
||||||
--help|-h)
|
|
||||||
_printHelpAndExit 0
|
|
||||||
;;
|
|
||||||
--)
|
|
||||||
shift
|
|
||||||
break
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
shift
|
|
||||||
done
|
|
||||||
else
|
|
||||||
echo "Incorrect options provided"
|
|
||||||
_printHelpAndExit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# If array mode, load input array, reparse input, and return
|
|
||||||
if [[ -n $_array ]]; then
|
|
||||||
checkBashVersion
|
|
||||||
local _n_array
|
|
||||||
declare -n _n_array="$_array"
|
|
||||||
_parseInput "${_n_array[@]}"
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Create _pre_commands_array from remaining arguments
|
|
||||||
# shift getopt parameters away
|
|
||||||
shift $((OPTIND - 1))
|
|
||||||
# Assume program name is first argument
|
|
||||||
_program="$1"
|
|
||||||
# create command array
|
|
||||||
declare -ga _cmd_array=("$@")
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
_shWrap () {
|
|
||||||
|
|
||||||
debug "${FUNCNAME[0]}"
|
|
||||||
|
|
||||||
if [[ -z $_no_sh ]]; then
|
|
||||||
_cmd_array=("sh" "-c" "${_cmd_array[*]}")
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
_toolboxExists () {
|
|
||||||
|
|
||||||
debug "${FUNCNAME[0]}" "$1"
|
|
||||||
|
|
||||||
toolbox list -c | cut -d ' ' -f 3 | grep -w "$1" > /dev/null 2>&1
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
_toolboxCreate () {
|
|
||||||
|
|
||||||
debug "${FUNCNAME[0]}" "$1" "${_image[@]}" "${_release[@]}"
|
|
||||||
|
|
||||||
toolbox create -c "$1" "${_image[@]}" "${_release[@]}"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
_toolboxRemove () {
|
|
||||||
|
|
||||||
debug "${FUNCNAME[0]}" "$1"
|
|
||||||
|
|
||||||
toolbox rm -f "$1"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
_toolboxRun () {
|
|
||||||
|
|
||||||
debug "${FUNCNAME[0]}" "$1" "${_cmd_array[@]}"
|
|
||||||
|
|
||||||
toolbox run -c "$1" "${_cmd_array[@]}"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
__main () {
|
|
||||||
|
|
||||||
# Get input
|
|
||||||
_parseInput "$@"
|
|
||||||
|
|
||||||
# Make sure the first argument is executable
|
|
||||||
chmod +x "$_program"
|
|
||||||
|
|
||||||
# Wrap command with `sh -c` by default
|
|
||||||
_shWrap
|
|
||||||
|
|
||||||
# Check if container exists
|
|
||||||
if _toolboxExists "$_cname"; then
|
|
||||||
if [[ -n $_recreate || -n $_ephemeral ]]; then
|
|
||||||
_toolboxRemove "$_cname"
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
_toolboxCreate "$_cname"
|
|
||||||
fi
|
|
||||||
|
|
||||||
_toolboxRun "$_cname"
|
|
||||||
|
|
||||||
if [[ -n $_ephemeral ]]; then
|
|
||||||
_toolboxRemove "$_cname"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# Allow this function to be executed directly
|
|
||||||
__main "$@"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
# Allow this file to be executed directly if not being sourced
|
|
||||||
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
|
||||||
|
|
||||||
# The following functions are usually handled by build-wrapper
|
|
||||||
|
|
||||||
_getBaseDir () {
|
|
||||||
# Get base directory name of where this script resides
|
|
||||||
# https://stackoverflow.com/questions/59895/how-to-get-the-source-directory-of-a-bash-script-from-within-the-script-itself#comment54598418_246128
|
|
||||||
_basedir=$(dirname "$(readlink -f "$0")")
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
_sourceFunctions () {
|
|
||||||
# Get the location of this file
|
|
||||||
_getBaseDir
|
|
||||||
# Go up two directories
|
|
||||||
ff="${_basedir%/*/*}/functions"
|
|
||||||
# Source functions file
|
|
||||||
if [[ -f "$ff" ]]; then
|
|
||||||
source "$ff"
|
|
||||||
else
|
|
||||||
echo "Cannot find functions file: ${ff}"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
_sourceFunctions
|
|
||||||
toolboxRun "$@"
|
|
||||||
fi
|
|
||||||
Reference in New Issue
Block a user