Initial commit
This commit is contained in:
193
run-with-podman.sh
Executable file
193
run-with-podman.sh
Executable 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
|
||||
Reference in New Issue
Block a user