Compare commits

...

1 Commits

Author SHA1 Message Date
cryobry
400e45e01f Simplify __main() logic 2020-05-09 14:16:31 -04:00

View File

@@ -13,17 +13,17 @@ podmanRunWrapper () {
cat <<-'EOF'
USAGE
Argument mode:
podman-run-wrapper -m MODE -o OPTIONS -i IMAGE [-n CONTAINER_NAME]
[--help] [--silent] [--debug] [COMMANDS [ARGS...]]
podmanRunWrapper -m MODE -o OPTIONS -i IMAGE [-n CONTAINER_NAME] [--help]
[--debug] [COMMANDS [ARGS...]]
Array mode (bash >= 4.3):
podman-run-wrapper -a ARRAY
podmanRunWrapper -a ARRAY
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")
podman-run-wrapper -a ARRAY
podmanRunWrapper -a ARRAY
COMMANDS
COMMANDS to run in the container (e.g. the current active file, an external build script, a
@@ -37,7 +37,6 @@ 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
@@ -65,9 +64,6 @@ 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
@@ -88,7 +84,7 @@ EOF
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
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"
while true; do
case "$1" in
@@ -126,11 +122,8 @@ EOF
--help|-h)
_printHelpAndExit 0
;;
--silent|-s)
_silent="1"
;;
--debug|-d)
_debug="1"
export _debug="1"
echo "Debugging on!"
;;
--)
@@ -178,12 +171,12 @@ EOF
declare -ga _prw_cmds_arr
_prw_cmds_arr=("$@")
if [[ ${#_prw_cmds_arr[@]} -lt 1 ]]; then
[[ -z $_silent ]] && echo "Warning: running container without any commands"
debug "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[@]}"
debug "_prw_opts_arr:" "${_prw_opts_arr[@]}"
debug "_prw_cmds_arr:" "${_prw_cmds_arr[@]}"
}
@@ -208,10 +201,9 @@ EOF
_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"
if podman container exists "$1"; then
debug "podman rm -v -f $1"
podman rm -v -f "$1"
fi
}
@@ -219,14 +211,12 @@ EOF
_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[@]}"
if podman container exists "$1"; then
debug podman exec "$1" sh -c "${_prw_cmds_arr[@]}"
podman exec "$1" 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[@]}"
debug "Command: podman run" "${_prw_opts_arr[@]}" "$_image" sh -c "${_prw_cmds_arr[@]}"
podman run "${_prw_opts_arr[@]}" "$_image" "${_prw_cmds_arr[@]}"
exit $?
fi
@@ -237,7 +227,7 @@ EOF
####### EXECUTE #########
#########################
_execute () {
__main() {
# Get input
_parseInput "$@"
@@ -249,26 +239,40 @@ EOF
[[ -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
if [[ "$_mode" =~ ^(ephemeral|recreate-persistent)$ ]]; then
_removeContainer "$_cname"
fi
_runContainer "$_cname"
}
# Allow this function to be executed directly
_execute "$@"
__main "$@"
exit $?
}
# Allow script to be called directly
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 "$@"
fi