123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- podmanRunEasy () {
- sourcePlugin "podmanRun/podmanRunWrapper"
-
-
-
-
- [[ -z $_image ]] && _image="fedora:latest"
- [[ -z $_mode ]] && _mode="ephemeral"
- [[ -z $_workdir ]] && _workdir="$PWD"
- _smartDefaults () {
- echo ""
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
-
-
-
- _printHelpAndExit () {
- cat <<-'EOF'
- USAGE
- podmanRunEasy [-m _mode] [-w PATH] [-d PATH] [-i _image] [--userfix] [--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
- --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
-
- [[ -z $1 ]] && exit 0 || exit "$1"
- }
- _parseInput () {
- [[ -n $_debug ]] && echo "$@"
-
- unset _array
-
- 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"
- 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"
- ;;
- --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 [[ -n $_array ]]; then
- checkBashVersion
- local _n_array
- declare -n _n_array="$_array"
- _parseInput "${_n_array[@]}"
- return
- fi
-
-
- shift $((OPTIND - 1))
-
- declare -ga _pre_commands_array
- _pre_commands_array=("$@")
- [[ -n $_debug ]] && echo "_pre_commands_array:" "${_pre_commands_array[@]}"
-
- [[ "$_mode" == "1" ]] && _mode="ephemeral"
- [[ "$_mode" == "2" ]] && _mode="persistent"
- [[ "$_mode" == "3" ]] && _mode="recreate-persistent"
- [[ "$_mode" == "4" ]] && _mode="remove-persistent"
-
- declare -ga _pre_options_array
- _pre_options_array+=("-it")
-
- _pre_options_array+=("-v" "${_workdir}:${_workdir}:Z")
- _pre_options_array+=("-w" "${_workdir}")
- _pre_options_array+=("--userns=keep-id")
- [[ -n $_maskdir ]] && _pre_options_array+=("-v" "${_maskdir}")
- [[ -n $_debug ]] && echo "_pre_options_array:" "${_pre_options_array[@]}"
- }
- _makeExec () {
-
- if [[ -n $_mkexec ]]; then
-
- _program="${_pre_commands_array[0]}"
-
- chmod +x "${_program}"
-
-
- fi
- }
- _sanityChecks () {
-
- if ! [[ -d "$_workdir" ]]; then
- [[ -z $_silent ]] && echo "--workdir does not exist!"
- [[ -z $_silent ]] && echo "Creating ${_workdir} now..."
- mkdir -p "${_workdir}"
- fi
-
- 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 () {
-
- _parseInput "$@"
-
- _runPodmanRunWrapper
- }
-
- _execute "$@"
- }
- if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
-
- _getBaseDir () {
-
-
- _basedir=$(dirname "$(readlink -f "$0")")
- }
- _sourceFunctions () {
-
- _getBaseDir
-
- ff="${_basedir%/*/*}/functions"
-
- if [[ -f "$ff" ]]; then
- source "$ff"
- else
- echo "Cannot find functions file: ${ff}"
- fi
- }
- _sourceFunctions
- podmanRunEasy "$@"
- fi
|