toolbox-run 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env bash
  2. #
  3. # This program will execute commands in the specified toolbox container
  4. #
  5. # Author:
  6. # Bryan Roessler
  7. # Source:
  8. # https://git.bryanroessler.com/bryan/scripts/src/master/toolboxrun
  9. #
  10. toolboxrun () {
  11. _printHelpAndExit () {
  12. cat <<-'EOF'
  13. USAGE
  14. toolboxRun -c NAME [-i NAME] [-r RELEASE] [--no-sh] [-h] [-s] [-d] [COMMANDS [ARGS...]]
  15. COMMANDS
  16. COMMANDS to run in the container (e.g. the current active file, an external
  17. build script, a program residing in the container, etc.)
  18. Can be empty (default entrypoint)
  19. OPTIONS
  20. --container NAME, -c NAME
  21. Assign a different NAME to the toolbox container. This is useful for creating multiple
  22. toolbox containers from the same base image, or for entirely customized containers from
  23. custom-built base images.
  24. If the toolbox container NAME already exists, the command passed to toolboxRun will be
  25. executed in the existing toolbox. If toolbox container NAME does not exist, it will be
  26. created and the COMMAND will then be run in it.
  27. --image NAME, -i NAME
  28. Change the NAME of the base image used to create the toolbox container. This is useful for
  29. creating containers from custom-built base images.
  30. --release RELEASE, -r RELEASE
  31. Create a toolbox container for a different operating system RELEASE than the host.
  32. --ephemeral
  33. The toolbox will be removed after the COMMAND is executed
  34. --recreate
  35. If the toolbox NAME already exists, it will first be removed and recreated
  36. --no-sh, -n
  37. Do not wrap COMMANDS in 'sh -c'
  38. --help, -h
  39. Print this help message and exit (overrides --silent)
  40. EOF
  41. # Exit using passed exit code
  42. [[ -z $1 ]] && exit 0 || exit "$1"
  43. }
  44. _parseInput () {
  45. # Parse input and set switches using getopt
  46. if _input=$(getopt -o +c:i:r:nh -l container:,image:,release:,ephemeral,recreate,no-sh,help -- "$@"); then
  47. eval set -- "$_input"
  48. while true; do
  49. case "$1" in
  50. --container|-c)
  51. shift && export _cname="$1"
  52. ;;
  53. --image|-i)
  54. shift && export _image=("-i" "$1")
  55. ;;
  56. --release|-r)
  57. shift && export _release=("-r" "$1")
  58. ;;
  59. --ephemeral)
  60. export _ephemeral="true"
  61. ;;
  62. --recreate)
  63. export _recreate="true"
  64. ;;
  65. --no-sh|-n)
  66. export _no_sh="true"
  67. ;;
  68. --help|-h)
  69. _printHelpAndExit 0
  70. ;;
  71. --)
  72. shift
  73. break
  74. ;;
  75. esac
  76. shift
  77. done
  78. else
  79. echo "Incorrect options provided"
  80. _printHelpAndExit 1
  81. fi
  82. # Create _pre_commands_array from remaining arguments
  83. # shift getopt parameters away
  84. shift $((OPTIND - 1))
  85. # Assume program name is first argument
  86. export _program="$1"
  87. # create command array
  88. declare -ga _cmd_array=("$@")
  89. }
  90. _shWrap () { [[ -z $_no_sh ]] && _cmd_array=("sh" "-c" "${_cmd_array[*]}"); }
  91. _toolboxExists () { toolbox list -c | cut -d ' ' -f 3 | grep -w "$1" > /dev/null 2>&1; }
  92. _toolboxCreate () { toolbox create -c "$1" "${_image[@]}" "${_release[@]}"; }
  93. _toolboxRemove () { toolbox rm -f "$1"; }
  94. _toolboxRun () { toolbox run -c "$1" "${_cmd_array[@]}"; }
  95. __main () {
  96. # Get input
  97. _parseInput "$@"
  98. # Wrap command with `sh -c` by default
  99. [[ -z $_no_sh ]] && _shWrap
  100. # Check if container exists
  101. if _toolboxExists "$_cname"; then
  102. if [[ -n $_recreate || -n $_ephemeral ]]; then
  103. _toolboxRemove "$_cname"
  104. fi
  105. else
  106. _toolboxCreate "$_cname"
  107. fi
  108. _toolboxRun "$_cname" && export _ec="$?"
  109. [[ -n $_ephemeral ]] && _toolboxRemove "$_cname"
  110. }
  111. # Allow this function to be executed directly
  112. __main "$@"
  113. }
  114. if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
  115. toolboxrun "$@"
  116. exit 0
  117. fi