run-with-podman.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #!/usr/bin/env bash
  2. # README; print this help message
  3. print_help () {
  4. cat <<-'EOF'
  5. Usage: run-with-podman.sh --file FILE [--file-path PATH] [--mode [0,1,2]]
  6. [--mask-dir PATH] [--image IMAGE_NAME] [--force-systemd]
  7. [--help] [--] $OPTIONS
  8. --file,-f FILE
  9. The local script to execute in the container (typically sent from your IDE)
  10. --file-path PATH
  11. Path that the script operates on (Default: the --file directory)
  12. --mode,-m 0,1,2
  13. 0. Nonpersistent container (always recreate) (Default)
  14. 1. Persistent container
  15. 2. Recreate persistent container
  16. --mask-dir PATH
  17. Hide this directory from the host OS, store contents in the container only (Default: unset)
  18. (Useful for capturing output in the container only for easy reset)
  19. --image,-i IMAGE_NAME
  20. The name of the image to execute the script (Default: fedora:latest)
  21. --force-systemd
  22. Force container to init with systemd support
  23. --help,-h
  24. Print this help message and exit
  25. -- [additional arguments to pass to --file FILE]
  26. Parsed as "quoted string"
  27. EOF
  28. }
  29. # DEFAULTS
  30. MODE="0"
  31. IMAGE="fedora:latest"
  32. SYSTEMD="on" # "on" is the podman default; "always" forces systemd init
  33. # Parse input
  34. function parse_input () {
  35. if options=$(getopt -o fmih -l file:,file-path:,mode:,mask-dir:,image:,force-systemd,help -- "$@"); then
  36. eval set -- "$options"
  37. while true; do
  38. case "$1" in
  39. --file| -f)
  40. shift
  41. FILE_ACTIVE="$1"
  42. ;;
  43. --file-path)
  44. shift
  45. FILE_ACTIVE_PATH="$1"
  46. ;;
  47. --mode| -m)
  48. shift
  49. MODE="$1"
  50. ;;
  51. --mask-dir)
  52. shift
  53. MASK_DIR="$1"
  54. ;;
  55. --image| -i)
  56. shift
  57. IMAGE="$1"
  58. ;;
  59. --force-systemd)
  60. SYSTEMD="always" # force systemd init
  61. ;;
  62. --help |-h)
  63. print_help
  64. exit $?
  65. ;;
  66. --)
  67. shift
  68. break
  69. ;;
  70. esac
  71. shift
  72. done
  73. else
  74. echo "Incorrect options provided"
  75. exit 1
  76. fi
  77. [[ -z $FILE_ACTIVE ]] && echo "You must provide a --file" && exit 1
  78. # If --file-path not set, extract FILE_ACTIVE_PATH from FILE_ACTIVE
  79. [[ -z $FILE_ACTIVE_PATH ]] && FILE_ACTIVE_PATH=${FILE_ACTIVE%/*}
  80. ! [[ -d "$FILE_ACTIVE_PATH" ]] &&
  81. # Pass any remaining positional arguments as script options
  82. OPTIONS=${*:$OPTIND}
  83. }
  84. # Get input
  85. parse_input "${@}"
  86. # Sanitize filename for unique container name
  87. CLEAN="${FILE_ACTIVE//_/}" && CLEAN="${CLEAN// /}" &&
  88. CLEAN="${CLEAN//[^a-zA-Z0-9_]/}" && CLEAN="${CLEAN,,}"
  89. # Allow container access to the pwd
  90. chcon -t container_file_t -R "${FILE_ACTIVE_PATH}"
  91. # Nonpersistent container (always recreate)
  92. if [[ $MODE == "0" ]]; then
  93. if podman container exists "atom-${CLEAN}-nonpersistent"; then
  94. podman rm -v -f "atom-${CLEAN}-nonpersistent"
  95. fi
  96. echo "Building in nonpersistent container: atom-${CLEAN}-nonpersistent"
  97. if [[ -n $MASK_DIR ]]; then
  98. podman run \
  99. -it \
  100. --systemd="${SYSTEMD}" \
  101. --name "atom-${CLEAN}-nonpersistent" \
  102. -v "${FILE_ACTIVE_PATH}:${FILE_ACTIVE_PATH}" \
  103. -v "${FILE_ACTIVE_PATH}/${MASK_DIR}" \
  104. -w "${FILE_ACTIVE_PATH}" \
  105. "${IMAGE}" \
  106. /bin/bash -c "chmod 755 ${FILE_ACTIVE} && ${FILE_ACTIVE} ${OPTIONS}"
  107. else
  108. podman run \
  109. -it \
  110. --systemd="${SYSTEMD}" \
  111. --name "atom-${CLEAN}-nonpersistent" \
  112. -v "${FILE_ACTIVE_PATH}:${FILE_ACTIVE_PATH}" \
  113. -w "${FILE_ACTIVE_PATH}" \
  114. "${IMAGE}" \
  115. /bin/bash -c "chmod 755 ${FILE_ACTIVE} && ${FILE_ACTIVE} ${OPTIONS}"
  116. fi
  117. # Persistent container
  118. elif [[ $MODE == "1" ]]; then
  119. echo "Reusing container: atom-${CLEAN}-persistent"
  120. if podman container exists "atom-${CLEAN}-persistent"; then
  121. echo "Using existing container!"
  122. podman exec "atom-${CLEAN}-persistent" \
  123. /bin/bash -c "chmod 755 ${FILE_ACTIVE} && ${FILE_ACTIVE}"
  124. else
  125. if [[ -n $MASK_DIR ]]; then
  126. podman run \
  127. -it \
  128. --systemd="${SYSTEMD}" \
  129. --name "atom-${CLEAN}-persistent" \
  130. -v "${FILE_ACTIVE_PATH}:${FILE_ACTIVE_PATH}" \
  131. -v "${FILE_ACTIVE_PATH}/${MASK_DIR}" \
  132. -w "${FILE_ACTIVE_PATH}" \
  133. "${IMAGE}" \
  134. /bin/bash -c "chmod 755 ${FILE_ACTIVE} && ${FILE_ACTIVE} ${OPTIONS}"
  135. else
  136. podman run \
  137. -it \
  138. --systemd="${SYSTEMD}" \
  139. --name "atom-${CLEAN}-persistent" \
  140. -v "${FILE_ACTIVE_PATH}:${FILE_ACTIVE_PATH}" \
  141. -w "${FILE_ACTIVE_PATH}" \
  142. "${IMAGE}" \
  143. /bin/bash -c "chmod 755 ${FILE_ACTIVE} && ${FILE_ACTIVE} ${OPTIONS}"
  144. fi
  145. fi
  146. # Recreate persistent container
  147. elif [[ $MODE == "2" ]]; then
  148. echo "Building in container: atom-${CLEAN}-persistent"
  149. if podman container exists "atom-${CLEAN}-persistent"; then
  150. echo "Container exists! Resetting container."
  151. podman rm -v -f "atom-${CLEAN}-persistent"
  152. fi
  153. if [[ -n $MASK_DIR ]]; then
  154. podman run \
  155. -it \
  156. --systemd="${SYSTEMD}" \
  157. --name "atom-${CLEAN}-persistent" \
  158. -v "${FILE_ACTIVE_PATH}:${FILE_ACTIVE_PATH}" \
  159. -v "${FILE_ACTIVE_PATH}/${MASK_DIR}" \
  160. -w "${FILE_ACTIVE_PATH}" \
  161. "${IMAGE}" \
  162. /bin/bash -c "chmod 755 ${FILE_ACTIVE} && ${FILE_ACTIVE} ${OPTIONS}"
  163. else
  164. podman run \
  165. -it \
  166. --systemd="${SYSTEMD}" \
  167. --name "atom-${CLEAN}-persistent" \
  168. -v "${FILE_ACTIVE_PATH}:${FILE_ACTIVE_PATH}" \
  169. -w "${FILE_ACTIVE_PATH}" \
  170. "${IMAGE}" \
  171. /bin/bash -c "chmod 755 ${FILE_ACTIVE} && ${FILE_ACTIVE} ${OPTIONS}"
  172. fi
  173. fi