Compare commits

..

1 Commits

Author SHA1 Message Date
cryobry
f2143633c5 Add toolboxRun plugin 2020-04-16 14:50:26 -04:00
4 changed files with 107 additions and 106 deletions

View File

@@ -105,15 +105,3 @@ fixPermissions () {
# Allow container access to the workdir (SELinux)
chcon -t container_file_t -R "$1"
}
debug () {
[[ -n $_debug ]] && echo "debug: " "$@"
}
silent () {
[[ -z $_silent ]] && echo "$@"
}

View File

@@ -51,7 +51,7 @@ podmanRunEasy () {
cat <<-'EOF'
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...]]
COMMANDS
@@ -86,6 +86,11 @@ OPTIONS
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
@@ -112,7 +117,7 @@ EOF
unset _array
# 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"
while true; do
case "$1" in
@@ -144,6 +149,9 @@ EOF
--mkexec|-x)
_mkexec="true"
;;
--systemd|-s)
_systemd="true"
;;
--silent)
_silent="true"
;;
@@ -193,10 +201,10 @@ EOF
declare -ga _pre_options_array
_pre_options_array+=("-it")
#[[ "$_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+=("--userns=keep-id") # Fix user permission problems by default
[[ -n $_maskdir ]] && _pre_options_array+=("-v" "${_maskdir}")
[[ -n $_systemd ]] && _pre_options_array+=("--systemd=always")
[[ -n $_debug ]] && echo "_pre_options_array:" "${_pre_options_array[@]}"
}

View File

@@ -1,29 +1,29 @@
#!/usr/bin/env bash
# shellcheck disable=SC1090,SC2004
podmanRunWrapper() {
podmanRunWrapper () {
########################
###### FUNCTIONS #######
########################
_printHelpAndExit() {
_printHelpAndExit () {
if [[ -z $_debug ]]; then
cat <<-'EOF'
USAGE
Argument mode:
podmanRunWrapper -m MODE -o OPTIONS -i IMAGE [-n CONTAINER_NAME] [--help]
[--debug] [COMMANDS [ARGS...]]
podman-run-wrapper -m MODE -o OPTIONS -i IMAGE [-n CONTAINER_NAME]
[--help] [--silent] [--debug] [COMMANDS [ARGS...]]
Array mode (bash >= 4.3):
podmanRunWrapper -a ARRAY
podman-run-wrapper -a ARRAY
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")
podmanRunWrapper -a ARRAY
podman-run-wrapper -a ARRAY
COMMANDS
COMMANDS to run in the container (e.g. the current active file, an external build script, a
@@ -37,6 +37,7 @@ OPTIONS
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
@@ -64,6 +65,9 @@ OPTIONS
--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
@@ -79,12 +83,12 @@ EOF
# Parse input
_parseInput() {
_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: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"
while true; do
case "$1" in
@@ -94,7 +98,7 @@ EOF
;;
--options|-o)
shift
_options+=("$1")
_options="$1"
;;
--image|-i)
shift
@@ -122,8 +126,11 @@ EOF
--help|-h)
_printHelpAndExit 0
;;
--silent|-s)
_silent="1"
;;
--debug|-d)
export _debug="1"
_debug="1"
echo "Debugging on!"
;;
--)
@@ -148,13 +155,16 @@ EOF
return
fi
# Parse podman options from --optionsarray
# Parse podman options
if [[ -n $_opts_arr ]]; then
# namerefs are awesome
declare -gn _prw_opts_arr="$_opts_arr"
# If no array given, parse input from options
elif [[ ${#_options[@]} -ge 1 ]]; then
declare -ga _prw_opts_arr=("${_options[@]}")
# 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
@@ -171,16 +181,16 @@ EOF
declare -ga _prw_cmds_arr
_prw_cmds_arr=("$@")
if [[ ${#_prw_cmds_arr[@]} -lt 1 ]]; then
debug "Running container without any commands"
[[ -z $_silent ]] && echo "Warning: running container without any commands"
fi
fi
debug "_prw_opts_arr:" "${_prw_opts_arr[@]}"
debug "_prw_cmds_arr:" "${_prw_cmds_arr[@]}"
[[ -n $_debug ]] && echo "_prw_opts_arr:" "${_prw_opts_arr[@]}"
[[ -n $_debug ]] && echo "_prw_cmds_arr:" "${_prw_cmds_arr[@]}"
}
_addCName() {
_addCName () {
# autogenerate _name if missing
[[ -z $_name ]] && _name="${_image}${_prw_cmds_arr[*]}"
@@ -199,24 +209,27 @@ EOF
}
_removeContainer() {
_removeContainer () {
if podman container exists "$1"; then
debug "podman rm -v -f $1"
podman rm -v -f "$1"
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() {
_runContainer () {
# Run _remove_container first to not run in existing container
if podman container exists "$1"; then
debug podman exec "$1" sh -c "${_prw_cmds_arr[@]}"
podman exec "$1" sh -c "${_prw_cmds_arr[@]}"
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
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[@]}"
exit $?
fi
@@ -227,7 +240,7 @@ EOF
####### EXECUTE #########
#########################
__main() {
_execute () {
# Get input
_parseInput "$@"
@@ -239,40 +252,26 @@ EOF
[[ -n $_selinux_fix ]] && fixPermissions "$PWD"
# Execute podman
if [[ "$_mode" =~ ^(ephemeral|recreate-persistent)$ ]]; then
_removeContainer "$_cname"
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
_runContainer "$_cname"
}
# Allow this function to be executed directly
__main "$@"
exit $?
_execute "$@"
}
# Allow script to be called directly
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
_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
# No imported functions
#source-functions
podmanRunWrapper "$@"
fi

View File

@@ -49,6 +49,10 @@ OPTIONS
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)
@@ -59,21 +63,21 @@ EOF
}
debug () {
_runDebug () {
[[ -n $_debug ]] && echo "debug: " "$@"
[[ -n $_debug ]] && echo "Running: " "$@"
}
_parseInput () {
debug "${FUNCNAME[0]}" "$@"
_runDebug "${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
if _input=$(getopt -o +c:i:r:a:ndsh -l container:,image:,release:,ephemeral,recreate,no-sh,debug,array:,silent,help -- "$@"); then
eval set -- "$_input"
while true; do
case "$1" in
@@ -92,10 +96,10 @@ EOF
--recreate)
_recreate="true"
;;
--no-sh|-n)
--no-sh)
_no_sh="true"
;;
--debug|-d)
--debug)
_debug="true"
echo "Debugging on!"
;;
@@ -103,6 +107,9 @@ EOF
shift && _array="$1"
break
;;
--silent)
#_silent="true"
;;
--help|-h)
_printHelpAndExit 0
;;
@@ -130,52 +137,54 @@ EOF
# Create _pre_commands_array from remaining arguments
# shift getopt parameters away
shift $((OPTIND - 1))
# Assume program name is first argument
# create array
declare -a _cmd_array
_program="$1"
# create command array
declare -ga _cmd_array=("$@")
_cmd_array=("$@")
[[ -n $_debug ]] && echo "_cmd_array:" "${_cmd_array[@]}"
}
_shWrap () {
debug "${FUNCNAME[0]}"
_runDebug "${FUNCNAME[0]}"
if [[ -z $_no_sh ]]; then
_cmd_array=("sh" "-c" "${_cmd_array[*]}")
_cmd_array=("sh" "-c" "${_cmd_array[@]}")
fi
}
_toolboxExists () {
debug "${FUNCNAME[0]}" "$1"
_runDebug "${FUNCNAME[0]}"
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[@]}"
toolbox list -c | cut -d ' ' -f 3 | grep -w "$_cname"
}
_toolboxRemove () {
debug "${FUNCNAME[0]}" "$1"
_runDebug "${FUNCNAME[0]}"
toolbox rm -f "$1"
toolbox rm "$_cname"
}
_toolboxRun () {
debug "${FUNCNAME[0]}" "$1" "${_cmd_array[@]}"
_runDebug "${FUNCNAME[0]}"
toolbox run -c "$1" "${_cmd_array[@]}"
toolbox run -c "$_cname" "${_cmd_array[@]}"
}
_toolboxCreate () {
_runDebug "${FUNCNAME[0]}"
toolbox create -c "$_cname" "${_image[@]}" "${_release[@]}"
}
@@ -191,19 +200,16 @@ EOF
_shWrap
# Check if container exists
if _toolboxExists "$_cname"; then
if [[ -n $_recreate || -n $_ephemeral ]]; then
_toolboxRemove "$_cname"
fi
if _toolboxExists; then
[[ -n $_recreate ]] && _toolboxRemove
else
_toolboxCreate "$_cname"
_toolboxCreate
fi
_toolboxRun "$_cname"
_toolboxRun
[[ -n $_ephemeral ]] && _toolboxRemove
if [[ -n $_ephemeral ]]; then
_toolboxRemove "$_cname"
fi
}
# Allow this function to be executed directly