You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

.docker_shims.zsh 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. function info {
  2. echo "=== ${@}" >&2
  3. }
  4. function debug {
  5. if [[ "${DEBUG}" = *'docker-shim'* ]] || [[ "${DEBUG}" = '*' ]] || [[ "${DEBUG:l}" = 'true' ]]; then
  6. echo "=== ${@}" >&2
  7. fi
  8. }
  9. function docker-into {
  10. local input="${@}"
  11. local dopts="${input% // *}"
  12. local dopts="${dopts% // *}"
  13. local _path="${input% // *}"
  14. local _path="${_path#* // }"
  15. local args="${input#* // }"
  16. local args="${args#* // }"
  17. debug "RUNNING: /usr/bin/env docker ${dopts} run -it --rm --volume \"${_path}:${_path}\" --workdir "${_path}" ${args}"
  18. eval "/usr/bin/env docker ${dopts} run -it --rm --volume \"${_path}:${_path}\" --workdir \"${_path}\" ${args}"
  19. }
  20. function docker-run {
  21. local input="${@}"
  22. local dopts="${input% // *}"
  23. local args="${input#* // }"
  24. if [[ "${args}" = *'--no-rm'* ]]; then
  25. # --no-rm isn't a real flag so we remove it
  26. args=$(echo "${args}" | sed -e 's/--no-rm//g')
  27. elif [[ "${args}" != *'--rm'* ]]; then
  28. args="--rm ${args}"
  29. fi
  30. debug "RUNNING: /usr/bin/env docker ${dopts} run ${args}"
  31. eval "/usr/bin/env docker ${dopts} run ${args}"
  32. }
  33. function docker-build {
  34. local input="${@}"
  35. local dopts="${input% // *}"
  36. local args="${input#* // }"
  37. local default_tag="$(basename ${PWD})"
  38. if [[ "${args}" = *'--help'* ]] || [[ "${args}" = *'-h'* ]]; then
  39. debug "RUNNING: /usr/bin/env docker ${dopts} build ${args}"
  40. eval "/usr/bin/env docker ${dopts} build ${args}"
  41. return $?
  42. fi
  43. if [[ "${args}" != *'--tag'* ]] && [[ "${args}" != *'-t'* ]]; then
  44. args="--tag ${default_tag} ${args}"
  45. fi
  46. if [[ "${args}" = *'--no-force-rm'* ]]; then
  47. # --no-force-rm isn't a real flag so we remove it
  48. args=$(echo "${args}" | sed -e 's/--no-force-rm//g')
  49. elif [[ "${args}" != *'--force-rm'* ]]; then
  50. args="--force-rm ${args}"
  51. fi
  52. local targets=$(cat Dockerfile | grep -E '^FROM .* AS .*$' | sed -e 's/FROM .* AS //')
  53. local caches=''
  54. local base_tag=$(echo "${args}" | grep -oE ' (--tag|-t)(=| )?([^ ])*')
  55. if [[ "${args}" != *'--no-progressive-build'* ]]; then
  56. info 'Automatically build multistages in a progressive way'
  57. info 'Each named targets will be built independently and used to cache next targets'
  58. for t in $(echo ${targets}); do
  59. echo "=== Building target '${t}'" >&2
  60. local target_tag="${default_tag}:${t}"
  61. local args_changes=" --tag ${target_tag} --target ${t} ${caches}"
  62. debug "RUNNING: /usr/bin/env docker ${dopts} build $(echo ${args} | sed -e 's/'${base_tag}'/'${args_changes}'/')"
  63. eval "/usr/bin/env docker ${dopts} build $(echo ${args} | sed -e 's/'${base_tag}'/'${args_changes}'/')"
  64. local s=$?
  65. if [[ $s -ne 0 ]]; then
  66. echo "=== Docker failed to build target '${t}'" >&2
  67. return $s
  68. fi
  69. caches="${caches} --cache-from ${target_tag}"
  70. done
  71. else
  72. # --no-progressive-build isn't a real flag so we remove it
  73. args=$(echo "${args}" | sed -e 's/--no-progressive-build//g')
  74. fi
  75. debug "RUNNING: /usr/bin/env docker ${dopts} build ${caches} ${args}"
  76. eval "/usr/bin/env docker ${dopts} build ${caches} ${args}"
  77. }
  78. function docker-remove-dangling {
  79. local input="${@}"
  80. local dopts="${input% // *}"
  81. local args="${input#* // }"
  82. debug "RUNNING: /usr/bin/env docker ${dopts} images --filter dangling=true --quiet"
  83. local dangling_images=$(eval "/usr/bin/env docker ${dopts} images --filter dangling=true --quiet" | tr '\n' ' ')
  84. local s=$?
  85. if [[ $s -ne 0 ]]; then
  86. info 'Docker failed to query dangling images'
  87. echo "${dangling_images}" >&2
  88. return $s
  89. fi
  90. debug "RUNNING: /usr/bin/env docker ${dopts} rmi ${args} ${dangling_images}"
  91. eval "/usr/bin/env docker ${dopts} rmi ${args} ${dangling_images}"
  92. }
  93. function docker {
  94. if [[ $# -eq 0 ]]; then
  95. docker-build " // ${PWD}"
  96. return $?
  97. fi
  98. ## Docker top level CLI options
  99. local dopts=''
  100. while true; do
  101. case "${1}" in
  102. --config\
  103. |-c|--context\
  104. |-H|--host\
  105. |-l|--log-level\
  106. |--tlscacert\
  107. |--tlscert\
  108. |--tlskey\
  109. )
  110. dopts="${dopts} ${1} ${2}"; shift 2
  111. ;;
  112. -D|--debug\
  113. |-v|--version\
  114. |--tls\
  115. |--tlsverify\
  116. )
  117. dopts="${dopts} ${1}"; shift
  118. ;;
  119. --no-shim)
  120. local __no_shim='__true__'; shift
  121. info 'The command will be run as is without Shim function intervening'
  122. ;;
  123. *)
  124. break
  125. ;;
  126. esac
  127. done
  128. if [[ "${__no_shim}" = '__true__' ]]; then
  129. debug "RUNNING: /usr/bin/env docker ${dopts} ${@}"
  130. eval "/usr/bin/env docker ${dopts} ${@}"
  131. return $?
  132. fi
  133. if [[ $# -eq 0 ]]; then
  134. debug "RUNNING: /usr/bin/env docker ${dopts} --help"
  135. eval "/usr/bin/env docker ${dopts} --help"
  136. return $?
  137. fi
  138. local cmd="${1}"; shift
  139. case "${cmd}" in
  140. into)
  141. debug "ALIAS: ${cmd}"
  142. local _path="${1}"; shift
  143. docker-into "${dopts} // ${_path} // ${@}"
  144. ;;
  145. here)
  146. debug "ALIAS: ${cmd}"
  147. docker-into "${dopts} // ${PWD} // ${@}"
  148. ;;
  149. dive)
  150. debug "ALIAS: ${cmd}"
  151. debug "RUNNING: /usr/bin/env docker ${dopts} run --rm -it -v /var/run/docker.sock:/var/run/docker.sock wagoodman/dive -- ${@}"
  152. eval "/usr/bin/env docker ${dopts} run --rm -it -v /var/run/docker.sock:/var/run/docker.sock wagoodman/dive -- ${@}"
  153. ;;
  154. nsenter|enter)
  155. debug "ALIAS: ${cmd}"
  156. debug "RUNNING: /usr/bin/env docker ${dopts} run --rm -it --privileged --pid=host justincormack/nsenter1"
  157. eval "/usr/bin/env docker ${dopts} run --rm -it --privileged --pid=host justincormack/nsenter1"
  158. ;;
  159. bld|build) docker-build "${dopts} // ${@}"
  160. ;;
  161. run) docker-run "${dopts} // ${@}"
  162. ;;
  163. rmd|rmdangling) docker-remove-dangling "${dopts} // ${@}"
  164. ;;
  165. net)
  166. debug "ALIAS: ${cmd}"
  167. debug "RUNNING: /usr/bin/env docker ${dopts} network ${@}"
  168. eval "/usr/bin/env docker ${dopts} network ${@}"
  169. ;;
  170. ctx)
  171. debug "ALIAS: ${cmd}"
  172. debug "RUNNING: /usr/bin/env docker ${dopts} context ${@}"
  173. eval "/usr/bin/env docker ${dopts} context ${@}"
  174. ;;
  175. ls)
  176. debug "ALIAS: ${cmd}"
  177. debug "RUNNING: /usr/bin/env docker ${dopts} images ${@}"
  178. eval "/usr/bin/env docker ${dopts} images ${@}"
  179. ;;
  180. *)
  181. debug "PASSTHRU TO DOCKER: ${cmd}"
  182. debug "RUNNING: /usr/bin/env docker ${dopts} ${cmd} ${@}"
  183. eval "/usr/bin/env docker ${dopts} ${cmd} ${@}"
  184. ;;
  185. esac
  186. return $?
  187. }