Compare commits

..

4 Commits

Author SHA1 Message Date
cryobry
1d3bbe441d Simplify __main() logic 2020-05-09 14:18:56 -04:00
cryobry
903eb8dcdb Simplify options array 2020-05-09 13:47:32 -04:00
cryobry
55aea4774a Cleanup debugging and generalize function 2020-05-09 10:26:00 -04:00
cryobry
3e07284a4e Remove systemd hack & improve permissions handling 2020-05-08 14:53:07 -04:00
4 changed files with 94 additions and 101 deletions

View File

@@ -105,3 +105,15 @@ 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 "$@"
}

View File

@@ -51,7 +51,7 @@ podmanRunEasy () {
cat <<-'EOF' cat <<-'EOF'
USAGE USAGE
podman-run-easy [-m _mode] [-w PATH] [-d PATH] [-i _image] [--systemd] [--mkexec] [--help] podmanRunEasy [-m _mode] [-w PATH] [-d PATH] [-i _image] [--userfix] [--mkexec] [--help]
[--silent] [--debug] [COMMANDS [ARGS...]] [--silent] [--debug] [COMMANDS [ARGS...]]
COMMANDS COMMANDS
@@ -86,11 +86,6 @@ 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
@@ -117,7 +112,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:xsh -l mode:,workdir:,maskdir:,image:,array:,name:,mkexec,systemd,silent,debug,help -- "$@"); then if _input=$(getopt -o +m:w:d:i:a:n:xh -l mode:,workdir:,maskdir:,image:,array:,name:,mkexec,silent,debug,help -- "$@"); then
eval set -- "$_input" eval set -- "$_input"
while true; do while true; do
case "$1" in case "$1" in
@@ -149,9 +144,6 @@ EOF
--mkexec|-x) --mkexec|-x)
_mkexec="true" _mkexec="true"
;; ;;
--systemd|-s)
_systemd="true"
;;
--silent) --silent)
_silent="true" _silent="true"
;; ;;
@@ -201,10 +193,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}") _pre_options_array+=("-v" "${_workdir}:${_workdir}:Z")
_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[@]}"
} }

View File

@@ -13,17 +13,17 @@ podmanRunWrapper () {
cat <<-'EOF' cat <<-'EOF'
USAGE USAGE
Argument mode: Argument mode:
podman-run-wrapper -m MODE -o OPTIONS -i IMAGE [-n CONTAINER_NAME] podmanRunWrapper -m MODE -o OPTIONS -i IMAGE [-n CONTAINER_NAME] [--help]
[--help] [--silent] [--debug] [COMMANDS [ARGS...]] [--debug] [COMMANDS [ARGS...]]
Array mode (bash >= 4.3): Array mode (bash >= 4.3):
podman-run-wrapper -a ARRAY podmanRunWrapper -a ARRAY
EXAMPLE EXAMPLE
podman-run-wrapper -m ephemeral -o "-it -v $PWD:$PWD -w $PWD" -i "php:latest" -c "php ./script.php" podmanRunWrapper -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")
podman-run-wrapper -a ARRAY podmanRunWrapper -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,7 +37,6 @@ 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
@@ -65,9 +64,6 @@ 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
@@ -88,7 +84,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:sdh -l mode:,options:,image:,name:,array:,optionsarray:,commandsarray:,selinuxfix,silent,debug,help -- "$@"); then if INPUT=$(getopt -o +m:o:i:x:n:a:dh -l mode:,options:,image:,name:,array:,optionsarray:,commandsarray:,selinuxfix,debug,help -- "$@"); then
eval set -- "$INPUT" eval set -- "$INPUT"
while true; do while true; do
case "$1" in case "$1" in
@@ -98,7 +94,7 @@ EOF
;; ;;
--options|-o) --options|-o)
shift shift
_options="$1" _options+=("$1")
;; ;;
--image|-i) --image|-i)
shift shift
@@ -126,11 +122,8 @@ EOF
--help|-h) --help|-h)
_printHelpAndExit 0 _printHelpAndExit 0
;; ;;
--silent|-s)
_silent="1"
;;
--debug|-d) --debug|-d)
_debug="1" export _debug="1"
echo "Debugging on!" echo "Debugging on!"
;; ;;
--) --)
@@ -155,16 +148,13 @@ EOF
return return
fi fi
# Parse podman options # Parse podman options from --optionsarray
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 not array mode optionally load podman options from input string # If no array given, parse input from options
elif [[ -n $_options ]]; then elif [[ ${#_options[@]} -ge 1 ]]; then
declare -ga _prw_opts_arr declare -ga _prw_opts_arr=("${_options[@]}")
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
@@ -181,12 +171,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
[[ -z $_silent ]] && echo "Warning: running container without any commands" debug "Running container without any commands"
fi fi
fi fi
[[ -n $_debug ]] && echo "_prw_opts_arr:" "${_prw_opts_arr[@]}" debug "_prw_opts_arr:" "${_prw_opts_arr[@]}"
[[ -n $_debug ]] && echo "_prw_cmds_arr:" "${_prw_cmds_arr[@]}" debug "_prw_cmds_arr:" "${_prw_cmds_arr[@]}"
} }
@@ -211,10 +201,9 @@ EOF
_removeContainer() { _removeContainer() {
if podman container exists "$_cname"; then if podman container exists "$1"; then
[[ -z $_silent ]] && echo "Removing container: $_cname" debug "podman rm -v -f $1"
[[ -n $_debug ]] && echo "podman rm -v -f $_cname" podman rm -v -f "$1"
podman rm -v -f "$_cname"
fi fi
} }
@@ -222,14 +211,12 @@ 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 "${_cname}"; then if podman container exists "$1"; then
[[ -z $_silent ]] && echo "Reusing container: $_cname" debug podman exec "$1" sh -c "${_prw_cmds_arr[@]}"
[[ -n $_debug ]] && echo podman exec "$_cname" sh -c "${_prw_cmds_arr[@]}" podman exec "$1" sh -c "${_prw_cmds_arr[@]}"
podman exec "$_cname" sh -c "${_prw_cmds_arr[@]}"
exit $? exit $?
else else
[[ -z $_silent ]] && echo "Running in container: $_cname" debug "Command: podman run" "${_prw_opts_arr[@]}" "$_image" sh -c "${_prw_cmds_arr[@]}"
[[ -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
@@ -240,7 +227,7 @@ EOF
####### EXECUTE ######### ####### EXECUTE #########
######################### #########################
_execute () { __main() {
# Get input # Get input
_parseInput "$@" _parseInput "$@"
@@ -252,26 +239,40 @@ EOF
[[ -n $_selinux_fix ]] && fixPermissions "$PWD" [[ -n $_selinux_fix ]] && fixPermissions "$PWD"
# Execute podman # Execute podman
if [[ $_mode == "ephemeral" || $_mode == "recreate-persistent" ]]; then if [[ "$_mode" =~ ^(ephemeral|recreate-persistent)$ ]]; then
_removeContainer _removeContainer "$_cname"
_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
_execute "$@" __main "$@"
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
#source-functions _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
podmanRunWrapper "$@" podmanRunWrapper "$@"
fi fi

View File

@@ -49,10 +49,6 @@ OPTIONS
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
--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 --help, -h
Print this help message and exit (overrides --silent) Print this help message and exit (overrides --silent)
@@ -63,22 +59,21 @@ EOF
} }
_runDebug () { debug () {
[[ -n $_debug ]] && echo "Running: " "$@" [[ -n $_debug ]] && echo "debug: " "$@"
} }
_parseInput () { _parseInput () {
_runDebug "${FUNCNAME[0]}" "$@" debug "${FUNCNAME[0]}" "$@"
# Unset vars # Unset vars
unset _array unset _array
declare -a _quiet=(">" "/dev/null" "2>&1")
# Parse input and set switches using getopt # Parse input and set switches using getopt
if _input=$(getopt -o +c:i:r:a:ndsh -l container:,image:,release:,ephemeral,recreate,no-sh,debug,array:,silent,help -- "$@"); then if _input=$(getopt -o +c:i:r:nda:h -l container:,image:,release:,ephemeral,recreate,no-sh,debug,array:,help -- "$@"); then
eval set -- "$_input" eval set -- "$_input"
while true; do while true; do
case "$1" in case "$1" in
@@ -97,21 +92,17 @@ EOF
--recreate) --recreate)
_recreate="true" _recreate="true"
;; ;;
--no-sh) --no-sh|-n)
_no_sh="true" _no_sh="true"
;; ;;
--debug) --debug|-d)
_debug="true" _debug="true"
unset _quiet
echo "Debugging on!" echo "Debugging on!"
;; ;;
--array|-a) --array|-a)
shift && _array="$1" shift && _array="$1"
break break
;; ;;
--silent)
#_silent="true"
;;
--help|-h) --help|-h)
_printHelpAndExit 0 _printHelpAndExit 0
;; ;;
@@ -139,16 +130,16 @@ EOF
# Create _pre_commands_array from remaining arguments # Create _pre_commands_array from remaining arguments
# shift getopt parameters away # shift getopt parameters away
shift $((OPTIND - 1)) shift $((OPTIND - 1))
# create array # Assume program name is first argument
declare -a _cmd_array
_program="$1" _program="$1"
# create command array
declare -ga _cmd_array=("$@") declare -ga _cmd_array=("$@")
} }
_shWrap () { _shWrap () {
_runDebug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
if [[ -z $_no_sh ]]; then if [[ -z $_no_sh ]]; then
_cmd_array=("sh" "-c" "${_cmd_array[*]}") _cmd_array=("sh" "-c" "${_cmd_array[*]}")
@@ -158,36 +149,33 @@ EOF
_toolboxExists () { _toolboxExists () {
_runDebug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}" "$1"
toolbox list -c | cut -d ' ' -f 3 | grep -w "$_cname" toolbox list -c | cut -d ' ' -f 3 | grep -w "$1" > /dev/null 2>&1
} }
_toolboxCreate () { _toolboxCreate () {
_runDebug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}" "$1" "${_image[@]}" "${_release[@]}"
toolbox create -c "$_cname" "${_image[@]}" "${_release[@]}" "${_quiet[@]}"
toolbox create -c "$1" "${_image[@]}" "${_release[@]}"
} }
_toolboxRemove () { _toolboxRemove () {
_runDebug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}" "$1"
toolbox rm -f "$_cname" "${_quiet[@]}" toolbox rm -f "$1"
} }
_toolboxRun () { _toolboxRun () {
_runDebug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}" "$1" "${_cmd_array[@]}"
[[ -n $_debug ]] && echo "_cmd_array:" "${_cmd_array[@]}" toolbox run -c "$1" "${_cmd_array[@]}"
toolbox run -c "$_cname" "${_cmd_array[@]}"
} }
@@ -203,18 +191,18 @@ EOF
_shWrap _shWrap
# Check if container exists # Check if container exists
if _toolboxExists; then if _toolboxExists "$_cname"; then
if [[ -n $_recreate || -n $_ephemeral ]]; then if [[ -n $_recreate || -n $_ephemeral ]]; then
_toolboxRemove _toolboxRemove "$_cname"
fi fi
else else
_toolboxCreate _toolboxCreate "$_cname"
fi fi
_toolboxRun _toolboxRun "$_cname"
if [[ -n $_ephemeral ]]; then if [[ -n $_ephemeral ]]; then
_toolboxRemove _toolboxRemove "$_cname"
fi fi
} }