Initial commit

This commit is contained in:
2020-01-25 12:51:42 -05:00
commit 7944dcf5d4
3 changed files with 204 additions and 0 deletions

8
LICENSE Normal file
View File

@@ -0,0 +1,8 @@
MIT License
Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

3
README.md Normal file
View File

@@ -0,0 +1,3 @@
# run-with-podman
Execute a script in a podman container

193
run-with-podman.sh Executable file
View File

@@ -0,0 +1,193 @@
#!/usr/bin/env bash
# README; print this help message
print_help () {
cat <<-'EOF'
Usage: run-with-podman.sh --file FILE [--file-path PATH] [--mode [0,1,2]]
[--mask-dir PATH] [--image IMAGE_NAME] [--force-systemd]
[--help] [--] $OPTIONS
--file,-f FILE
The local script to execute in the container (typically sent from your IDE)
--file-path PATH
Path that the script operates on (Default: the --file directory)
--mode,-m 0,1,2
0. Nonpersistent container (always recreate) (Default)
1. Persistent container
2. Recreate persistent container
--mask-dir PATH
Hide this directory from the host OS, store contents in the container only (Default: unset)
(Useful for capturing output in the container only for easy reset)
--image,-i IMAGE_NAME
The name of the image to execute the script (Default: fedora:latest)
--force-systemd
Force container to init with systemd support
--help,-h
Print this help message and exit
-- [additional arguments to pass to --file FILE]
Parsed as "quoted string"
EOF
}
# DEFAULTS
MODE="0"
IMAGE="fedora:latest"
SYSTEMD="on" # "on" is the podman default; "always" forces systemd init
# Parse input
function parse_input () {
if options=$(getopt -o fmih -l file:,file-path:,mode:,mask-dir:,image:,force-systemd,help -- "$@"); then
eval set -- "$options"
while true; do
case "$1" in
--file| -f)
shift
FILE_ACTIVE="$1"
;;
--file-path)
shift
FILE_ACTIVE_PATH="$1"
;;
--mode| -m)
shift
MODE="$1"
;;
--mask-dir)
shift
MASK_DIR="$1"
;;
--image| -i)
shift
IMAGE="$1"
;;
--force-systemd)
SYSTEMD="always" # force systemd init
;;
--help |-h)
print_help
exit $?
;;
--)
shift
break
;;
esac
shift
done
else
echo "Incorrect options provided"
exit 1
fi
[[ -z $FILE_ACTIVE ]] && echo "You must provide a --file" && exit 1
# If --file-path not set, extract FILE_ACTIVE_PATH from FILE_ACTIVE
[[ -z $FILE_ACTIVE_PATH ]] && FILE_ACTIVE_PATH=${FILE_ACTIVE%/*}
! [[ -d "$FILE_ACTIVE_PATH" ]] &&
# Pass any remaining positional arguments as script options
OPTIONS=${*:$OPTIND}
}
# Get input
parse_input "${@}"
# Sanitize filename for unique container name
CLEAN="${FILE_ACTIVE//_/}" && CLEAN="${CLEAN// /_}" &&
CLEAN="${CLEAN//[^a-zA-Z0-9_]/}" && CLEAN="${CLEAN,,}"
# Allow container access to the pwd
chcon -t container_file_t -R "${FILE_ACTIVE_PATH}"
# Nonpersistent container (always recreate)
if [[ $MODE == "0" ]]; then
if podman container exists "atom-${CLEAN}-nonpersistent"; then
podman rm -v -f "atom-${CLEAN}-nonpersistent"
fi
echo "Building in nonpersistent container: atom-${CLEAN}-nonpersistent"
if [[ -n $MASK_DIR ]]; then
podman run \
-it \
--systemd="${SYSTEMD}" \
--name "atom-${CLEAN}-nonpersistent" \
-v "${FILE_ACTIVE_PATH}:${FILE_ACTIVE_PATH}" \
-v "${FILE_ACTIVE_PATH}/${MASK_DIR}" \
-w "${FILE_ACTIVE_PATH}" \
"${IMAGE}" \
/bin/bash -c "chmod 755 ${FILE_ACTIVE} && ${FILE_ACTIVE} ${OPTIONS}"
else
podman run \
-it \
--systemd="${SYSTEMD}" \
--name "atom-${CLEAN}-nonpersistent" \
-v "${FILE_ACTIVE_PATH}:${FILE_ACTIVE_PATH}" \
-w "${FILE_ACTIVE_PATH}" \
"${IMAGE}" \
/bin/bash -c "chmod 755 ${FILE_ACTIVE} && ${FILE_ACTIVE} ${OPTIONS}"
fi
# Persistent container
elif [[ $MODE == "1" ]]; then
echo "Reusing container: atom-${CLEAN}-persistent"
if podman container exists "atom-${CLEAN}-persistent"; then
echo "Using existing container!"
podman exec "atom-${CLEAN}-persistent" \
/bin/bash -c "chmod 755 {FILE_ACTIVE} && {FILE_ACTIVE}"
else
if [[ -n $MASK_DIR ]]; then
podman run \
-it \
--systemd="${SYSTEMD}" \
--name "atom-${CLEAN}-persistent" \
-v "{FILE_ACTIVE_PATH}:{FILE_ACTIVE_PATH}" \
-v "{FILE_ACTIVE_PATH}/${MASK_DIR}" \
-w "{FILE_ACTIVE_PATH}" \
"${IMAGE}" \
/bin/bash -c "chmod 755 {FILE_ACTIVE} && {FILE_ACTIVE} ${OPTIONS}"
else
podman run \
-it \
--systemd="${SYSTEMD}" \
--name "atom-${CLEAN}-persistent" \
-v "{FILE_ACTIVE_PATH}:{FILE_ACTIVE_PATH}" \
-w "{FILE_ACTIVE_PATH}" \
"${IMAGE}" \
/bin/bash -c "chmod 755 {FILE_ACTIVE} && {FILE_ACTIVE} ${OPTIONS}"
fi
fi
# Recreate persistent container
elif [[ $MODE == "2" ]]; then
echo "Building in container: atom-${CLEAN}-persistent"
if podman container exists "atom-${CLEAN}-persistent"; then
echo "Container exists! Resetting container."
podman rm -v -f "atom-${CLEAN}-persistent"
fi
if [[ -n $MASK_DIR ]]; then
podman run \
-it \
--systemd="${SYSTEMD}" \
--name "atom-${CLEAN}-persistent" \
-v "{FILE_ACTIVE_PATH}:{FILE_ACTIVE_PATH}" \
-v "{FILE_ACTIVE_PATH}/${MASK_DIR}" \
-w "{FILE_ACTIVE_PATH}" \
"${IMAGE}" \
/bin/bash -c "chmod 755 {FILE_ACTIVE} && {FILE_ACTIVE} ${OPTIONS}"
else
podman run \
-it \
--systemd="${SYSTEMD}" \
--name "atom-${CLEAN}-persistent" \
-v "{FILE_ACTIVE_PATH}:{FILE_ACTIVE_PATH}" \
-w "{FILE_ACTIVE_PATH}" \
"${IMAGE}" \
/bin/bash -c "chmod 755 {FILE_ACTIVE} && {FILE_ACTIVE} ${OPTIONS}"
fi
fi