toolboxRun 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #!/usr/bin/env bash
  2. # shellcheck disable=SC1090
  3. toolboxRun () {
  4. _printHelpAndExit () {
  5. cat <<-'EOF'
  6. USAGE
  7. toolboxRun -c NAME [-i NAME] [-r RELEASE] [--no-sh] [-h] [-s] [-d] [COMMANDS [ARGS...]]
  8. COMMANDS
  9. COMMANDS to run in the container (e.g. the current active file, an external
  10. build script, a program residing in the container, etc.)
  11. Can be empty (default entrypoint)
  12. OPTIONS
  13. --container NAME, -c NAME
  14. Assign a different NAME to the toolbox container. This is useful for creating multiple
  15. toolbox containers from the same base image, or for entirely customized containers from
  16. custom-built base images.
  17. If the toolbox container NAME already exists, the command passed to toolboxRun will be
  18. executed in the existing toolbox. If toolbox container NAME does not exist, it will be
  19. created and the COMMAND will then be run in it.
  20. --image NAME, -i NAME
  21. Change the NAME of the base image used to create the toolbox container. This is useful for
  22. creating containers from custom-built base images.
  23. --release RELEASE, -r RELEASE
  24. Create a toolbox container for a different operating system RELEASE than the host.
  25. --ephemeral
  26. The toolbox will be removed after the COMMAND is executed
  27. --recreate
  28. If the toolbox NAME already exists, it will first be removed and recreated
  29. --no-sh, -n
  30. Makes the first argument of COMMANDS executable and runs it directly
  31. --debug, -d
  32. Display debugging output
  33. --array, -a ARRAY
  34. Read arguments from an existing or new ARRAY (bash >= 4.3)
  35. This is useful to reduce parsing errors and recommended for build-wrapper plugins
  36. --help, -h
  37. Print this help message and exit (overrides --silent)
  38. EOF
  39. # Exit using passed exit code
  40. [[ -z $1 ]] && exit 0 || exit "$1"
  41. }
  42. debug () {
  43. [[ -n $_debug ]] && echo "debug: " "$@"
  44. }
  45. _parseInput () {
  46. debug "${FUNCNAME[0]}" "$@"
  47. # Unset vars
  48. unset _array
  49. # Parse input and set switches using getopt
  50. if _input=$(getopt -o +c:i:r:nda:h -l container:,image:,release:,ephemeral,recreate,no-sh,debug,array:,help -- "$@"); then
  51. eval set -- "$_input"
  52. while true; do
  53. case "$1" in
  54. --container|-c)
  55. shift && _cname="$1"
  56. ;;
  57. --image|-i)
  58. shift && _image=("-i" "$1")
  59. ;;
  60. --release|-r)
  61. shift && _release=("-r" "$1")
  62. ;;
  63. --ephemeral)
  64. _ephemeral="true"
  65. ;;
  66. --recreate)
  67. _recreate="true"
  68. ;;
  69. --no-sh|-n)
  70. _no_sh="true"
  71. ;;
  72. --debug|-d)
  73. _debug="true"
  74. echo "Debugging on!"
  75. ;;
  76. --array|-a)
  77. shift && _array="$1"
  78. break
  79. ;;
  80. --help|-h)
  81. _printHelpAndExit 0
  82. ;;
  83. --)
  84. shift
  85. break
  86. ;;
  87. esac
  88. shift
  89. done
  90. else
  91. echo "Incorrect options provided"
  92. _printHelpAndExit 1
  93. fi
  94. # If array mode, load input array, reparse input, and return
  95. if [[ -n $_array ]]; then
  96. checkBashVersion
  97. local _n_array
  98. declare -n _n_array="$_array"
  99. _parseInput "${_n_array[@]}"
  100. return
  101. fi
  102. # Create _pre_commands_array from remaining arguments
  103. # shift getopt parameters away
  104. shift $((OPTIND - 1))
  105. # Assume program name is first argument
  106. _program="$1"
  107. # create command array
  108. declare -ga _cmd_array=("$@")
  109. }
  110. _shWrap () {
  111. debug "${FUNCNAME[0]}"
  112. if [[ -z $_no_sh ]]; then
  113. _cmd_array=("sh" "-c" "${_cmd_array[*]}")
  114. fi
  115. }
  116. _toolboxExists () {
  117. debug "${FUNCNAME[0]}" "$1"
  118. toolbox list -c | cut -d ' ' -f 3 | grep -w "$1" > /dev/null 2>&1
  119. }
  120. _toolboxCreate () {
  121. debug "${FUNCNAME[0]}" "$1" "${_image[@]}" "${_release[@]}"
  122. toolbox create -c "$1" "${_image[@]}" "${_release[@]}"
  123. }
  124. _toolboxRemove () {
  125. debug "${FUNCNAME[0]}" "$1"
  126. toolbox rm -f "$1"
  127. }
  128. _toolboxRun () {
  129. debug "${FUNCNAME[0]}" "$1" "${_cmd_array[@]}"
  130. toolbox run -c "$1" "${_cmd_array[@]}"
  131. }
  132. __main () {
  133. # Get input
  134. _parseInput "$@"
  135. # Make sure the first argument is executable
  136. chmod +x "$_program"
  137. # Wrap command with `sh -c` by default
  138. _shWrap
  139. # Check if container exists
  140. if _toolboxExists "$_cname"; then
  141. if [[ -n $_recreate || -n $_ephemeral ]]; then
  142. _toolboxRemove "$_cname"
  143. fi
  144. else
  145. _toolboxCreate "$_cname"
  146. fi
  147. _toolboxRun "$_cname"
  148. if [[ -n $_ephemeral ]]; then
  149. _toolboxRemove "$_cname"
  150. fi
  151. }
  152. # Allow this function to be executed directly
  153. __main "$@"
  154. }
  155. # Allow this file to be executed directly if not being sourced
  156. if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
  157. # The following functions are usually handled by build-wrapper
  158. _getBaseDir () {
  159. # Get base directory name of where this script resides
  160. # https://stackoverflow.com/questions/59895/how-to-get-the-source-directory-of-a-bash-script-from-within-the-script-itself#comment54598418_246128
  161. _basedir=$(dirname "$(readlink -f "$0")")
  162. }
  163. _sourceFunctions () {
  164. # Get the location of this file
  165. _getBaseDir
  166. # Go up two directories
  167. ff="${_basedir%/*/*}/functions"
  168. # Source functions file
  169. if [[ -f "$ff" ]]; then
  170. source "$ff"
  171. else
  172. echo "Cannot find functions file: ${ff}"
  173. fi
  174. }
  175. _sourceFunctions
  176. toolboxRun "$@"
  177. fi