toolboxrun 4.2 KB

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