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.

1009 lines
28 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. #!/bin/bash
  2. ## Bash wrap script to launch the ``compose`` docker with right options.
  3. ##
  4. ##
  5. ## Launcher
  6. ## - should need minimum requirement to run
  7. ## - no shell libs
  8. ##
  9. [[ "${BASH_SOURCE[0]}" != "${0}" ]] && SOURCED=true
  10. ##
  11. ## From kal-shlib
  12. ##
  13. ANSI_ESC=$'\e['
  14. md5_compat() {
  15. if get_path md5sum >/dev/null; then
  16. md5_compat() { md5sum | cut -c -32; }
  17. elif get_path md5 >/dev/null; then
  18. md5_compat() { md5; }
  19. else
  20. die "$exname: required GNU or BSD date not found"
  21. fi
  22. md5_compat
  23. }
  24. ## output on stdout the next record on stdin separated by a '\0'
  25. next-0() {
  26. local ans IFS=''
  27. read -r -d '' ans &&
  28. echo -n "$ans"
  29. }
  30. array_read-0() {
  31. local elt aname
  32. while true; do
  33. for aname in "$@"; do
  34. declare -n cur="$aname"
  35. elt="$(next-0)" || return 0
  36. cur+=("$elt")
  37. done
  38. done
  39. }
  40. str_pattern_matches() {
  41. local str="$1"
  42. shift
  43. for pattern in "$@"; do
  44. eval "[[ \"$str\" == $pattern ]]" && return 0
  45. done
  46. return 1
  47. }
  48. ansi_color() {
  49. local choice="$1"
  50. if [ "$choice" == "tty" ]; then
  51. if [ -t 1 ]; then
  52. choice="yes"
  53. else
  54. choice="no"
  55. fi
  56. fi
  57. if [ "$choice" != "no" ]; then
  58. SET_COL_CHAR="${ANSI_ESC}${COL_CHAR}G"
  59. SET_COL_STATUS="${ANSI_ESC}${COL_STATUS}G"
  60. SET_COL_INFO="${ANSI_ESC}${COL_INFO}G"
  61. SET_COL_ELT="${ANSI_ESC}${COL_ELT}G"
  62. SET_BEGINCOL="${ANSI_ESC}0G"
  63. UP="${ANSI_ESC}1A"
  64. DOWN="${ANSI_ESC}1B"
  65. LEFT="${ANSI_ESC}1D"
  66. RIGHT="${ANSI_ESC}1C"
  67. SAVE="${ANSI_ESC}7"
  68. RESTORE="${ANSI_ESC}8"
  69. NORMAL="${ANSI_ESC}0m"
  70. GRAY="${ANSI_ESC}1;30m"
  71. RED="${ANSI_ESC}1;31m"
  72. GREEN="${ANSI_ESC}1;32m"
  73. YELLOW="${ANSI_ESC}1;33m"
  74. BLUE="${ANSI_ESC}1;34m"
  75. PINK="${ANSI_ESC}1;35m"
  76. CYAN="${ANSI_ESC}1;36m"
  77. WHITE="${ANSI_ESC}1;37m"
  78. DARKGRAY="${ANSI_ESC}0;30m"
  79. DARKRED="${ANSI_ESC}0;31m"
  80. DARKGREEN="${ANSI_ESC}0;32m"
  81. DARKYELLOW="${ANSI_ESC}0;33m"
  82. DARKBLUE="${ANSI_ESC}0;34m"
  83. DARKPINK="${ANSI_ESC}0;35m"
  84. DARKCYAN="${ANSI_ESC}0;36m"
  85. DARKWHITE="${ANSI_ESC}0;37m"
  86. SUCCESS=$GREEN
  87. WARNING=$YELLOW
  88. FAILURE=$RED
  89. NOOP=$BLUE
  90. ON=$SUCCESS
  91. OFF=$FAILURE
  92. ERROR=$FAILURE
  93. else
  94. SET_COL_CHAR=
  95. SET_COL_STATUS=
  96. SET_COL_INFO=
  97. SET_COL_ELT=
  98. SET_BEGINCOL=
  99. NORMAL=
  100. RED=
  101. GREEN=
  102. YELLOW=
  103. BLUE=
  104. GRAY=
  105. WHITE=
  106. DARKGRAY=
  107. DARKRED=
  108. DARKGREEN=
  109. DARKYELLOW=
  110. DARKBLUE=
  111. DARKPINK=
  112. DARKCYAN=
  113. SUCCESS=
  114. WARNING=
  115. FAILURE=
  116. NOOP=
  117. ON=
  118. OFF=
  119. ERROR=
  120. fi
  121. ansi_color="$choice"
  122. export SET_COL_CHAR SET_COL_STATUS SET_COL_INFO SET_COL_ELT \
  123. SET_BEGINCOL UP DOWN LEFT RIGHT SAVE RESTORE NORMAL \
  124. GRAY RED GREEN YELLOW BLUE PINK CYAN WHITE DARKGRAY \
  125. DARKRED DARKGREEN DARKYELLOW DARKBLUE DARKPINK DARKCYAN \
  126. SUCCESS WARNING FAILURE NOOP ON OFF ERROR ansi_color
  127. }
  128. e() { printf "%s" "$*"; }
  129. warn() { e "${YELLOW}Warning:$NORMAL" "$*"$'\n' >&2 ; }
  130. info() { e "${BLUE}II$NORMAL" "$*"$'\n' >&2 ; }
  131. verb() { [ -z "$VERBOSE" ] || e "$*"$'\n' >&2; }
  132. debug() { [ -z "$DEBUG" ] || e "$*"$'\n' >&2; }
  133. err() { e "${RED}Error:$NORMAL $*"$'\n' >&2 ; }
  134. die() { err "$@" ; exit 1; }
  135. ## equivalent of 'xargs echo' with builtins
  136. nspc() {
  137. local content
  138. content=$(printf "%s " $(cat -))
  139. printf "%s" "${content::-1}"
  140. }
  141. get_path() { (
  142. IFS=:
  143. for d in $PATH; do
  144. filename="$d/$1"
  145. [ -f "$filename" -a -x "$filename" ] && {
  146. echo "$d/$1"
  147. return 0
  148. }
  149. done
  150. return 1
  151. ) }
  152. depends() {
  153. ## Avoid colliding with variables that are created with depends.
  154. local __i __path __new_name
  155. for __i in "$@"; do
  156. if ! __path=$(get_path "$__i"); then
  157. __new_name="$(echo "${__i//-/_}")"
  158. if [ "$__new_name" != "$__i" ]; then
  159. depends "$__new_name"
  160. else
  161. err "dependency check: couldn't find '$__i' required command."
  162. exit 1
  163. fi
  164. else
  165. if ! test -z "$__path" ; then
  166. export "$(echo "${__i//- /__}")"="$__path"
  167. fi
  168. fi
  169. done
  170. }
  171. get_os() {
  172. local uname_output
  173. uname_output="$(uname -s)"
  174. case "${uname_output}" in
  175. Linux*)
  176. if [[ "$(< /proc/version)" == *@(Microsoft|WSL)* ]]; then
  177. e wsl
  178. # elif [[ "$(< /proc/version)" == *"@(microsoft|WSL)"* ]]; then
  179. # e wsl2
  180. else
  181. e linux
  182. fi
  183. ;;
  184. Darwin*) e mac;;
  185. CYGWIN*) e cygwin;;
  186. MINGW*) e mingw;;
  187. *) e "UNKNOWN:${uname_output}";;
  188. esac
  189. }
  190. read-0() {
  191. local eof= IFS=''
  192. while [ "$1" ]; do
  193. read -r -d '' -- "$1" || eof=1
  194. shift
  195. done
  196. [ -z "$eof" ]
  197. }
  198. read-0a() {
  199. local eof= IFS=''
  200. while [ "$1" ]; do
  201. IFS='' read -r -d $'\n' -- "$1" || eof=1
  202. shift
  203. done
  204. [ -z "$eof" ]
  205. }
  206. p0() {
  207. printf "%s\0" "$@"
  208. }
  209. cla.normalize() {
  210. local letters arg i
  211. while [ "$#" != 0 ]; do
  212. arg=$1
  213. case "$arg" in
  214. --)
  215. p0 "$@"
  216. return 0
  217. ;;
  218. --*=*|-*=*)
  219. shift
  220. set -- "${arg%%=*}" "${arg#*=}" "$@"
  221. continue
  222. ;;
  223. --*|-?) :;;
  224. -*)
  225. letters=${arg:1}
  226. shift
  227. i=${#letters}
  228. while ((i--)); do
  229. set -- -${letters:$i:1} "$@"
  230. done
  231. continue
  232. ;;
  233. esac
  234. p0 "$arg"
  235. shift
  236. done
  237. }
  238. docker_has_image() {
  239. local image="$1"
  240. images=$(docker images -q "$image" 2>/dev/null) || {
  241. err "docker images call has failed unexpectedly."
  242. return 1
  243. }
  244. [ -n "$images" ]
  245. }
  246. docker_image_id() {
  247. local image="$1"
  248. image_id=$(docker inspect "$image" --format='{{.Id}}') || return 1
  249. echo "$image_id"
  250. }
  251. ##
  252. ## Compose-core common functions
  253. ##
  254. list_compose_vars() {
  255. while read-0a def; do
  256. def="${def##* }"
  257. def="${def%=*}"
  258. p0 "$def"
  259. done < <(declare -p | grep "^declare -x COMPOSE_[A-Z_]\+=\"")
  260. }
  261. get_running_compose_containers() {
  262. ## XXXvlab: docker bug: there will be a final newline anyway
  263. docker ps --filter label="compose.service" --format='{{.ID}}'
  264. }
  265. get_volumes_for_container() {
  266. local container="$1"
  267. docker inspect \
  268. --format '{{range $mount := .Mounts}}{{$mount.Source}}{{"\x00"}}{{$mount.Destination}}{{"\x00"}}{{end}}' \
  269. "$container"
  270. }
  271. is_volume_used() {
  272. local volume="$1"
  273. while read container_id; do
  274. while read-0 src dst; do
  275. [ "$src" == "$volume" ] && return 0
  276. done < <(get_volumes_for_container "$container_id")
  277. done < <(get_running_compose_containers)
  278. return 1
  279. }
  280. _MULTIOPTION_REGEX='^((-[a-zA-Z]|--[a-zA-Z0-9-]+)(, )?)+'
  281. _MULTIOPTION_REGEX_LINE_FILTER=$_MULTIOPTION_REGEX'(\s|=)'
  282. ##
  283. ## compose launcher functions
  284. ##
  285. clean_unused_sessions() {
  286. for f in "$SESSION_DIR/"*; do
  287. [ -e "$f" ] || continue
  288. is_volume_used "$f" && continue
  289. ## XXXvlab: the second rmdir should not be useful
  290. rm -f "$f" >/dev/null || rmdir "$f" >/dev/null || {
  291. debug "Unexpected session remnants $f"
  292. }
  293. done
  294. }
  295. check_no_links_subdirs() {
  296. local dir
  297. for dir in "$@"; do
  298. [ -d "$dir" ] || continue
  299. if [ -L "$dir" ]; then
  300. err "Unfortunately, this compose launcher do not support yet symlinks in charm-store."
  301. echo " Found symlink in charm-store: $dir" >&2
  302. return 1
  303. fi
  304. [ -e "$dir/metadata.yml" ] && continue
  305. check_no_links_subdirs "$dir"/* || return 1
  306. done
  307. }
  308. ## requires docker_run_opts to be set
  309. get_compose_file_opt() {
  310. local compose_docker_image="$1" hash override
  311. shift
  312. image_id=$(docker_image_id "$compose_docker_image")
  313. override=$(get_volume_opt "${docker_run_opts[@]}") || return 1
  314. if [ -n "$override" ]; then
  315. if ! [ -f "$override" ]; then
  316. err "Invalid override of 'compose-core' detected. File '$override' does not exist on host."
  317. return 1
  318. fi
  319. hash=$( { p0 "$image_id"; cat "$override"; } | md5_compat)
  320. else
  321. hash=$(p0 "$image_id" | md5_compat)
  322. fi
  323. _get_compose_file_opt "$hash" "$override" "$@" || return 1
  324. }
  325. _get_compose_file_opt() {
  326. local hash_bin="$1" override="$2" \
  327. cache_file="$COMPOSE_LAUNCHER_CACHE/$FUNCNAME.cache.$(p0 "$@" | md5_compat)"
  328. if [ -e "$cache_file" ]; then
  329. cat "$cache_file" &&
  330. touch "$cache_file" || return 1
  331. return 0
  332. fi
  333. shift 2
  334. DC_MATCH_MULTI=$(get_compose_multi_opts_list "$hash_bin" "$override") || return 1
  335. DC_MATCH_SINGLE=$(get_compose_single_opts_list "$hash_bin" "$override") || return 1
  336. while read-0 arg; do
  337. case "$arg" in
  338. "-f"|"--file")
  339. read-0 value
  340. e "$value"
  341. return 0
  342. ;;
  343. --*|-*)
  344. if str_pattern_matches "$arg" $DC_MATCH_MULTI; then
  345. read-0 value
  346. opts+=("$arg" "$value")
  347. shift
  348. elif str_pattern_matches "$arg" $DC_MATCH_SINGLE; then
  349. opts+=("$arg")
  350. else
  351. debug "Unknown option '$arg'. Didn't manage to pre-parse correctly options."
  352. return 1
  353. fi
  354. ;;
  355. *)
  356. return 1
  357. ;;
  358. esac
  359. done < <(cla.normalize "$@") | tee "$cache_file"
  360. }
  361. replace_compose_file_opt() {
  362. local compose_docker_image="$1" hash override
  363. shift
  364. image_id=$(docker_image_id "$compose_docker_image")
  365. override=$(get_volume_opt "${docker_run_opts[@]}") || return 1
  366. if [ -n "$override" ]; then
  367. if ! [ -f "$override" ]; then
  368. err "Invalid override of 'compose-core' detected. File '$override' does not exist on host."
  369. return 1
  370. fi
  371. hash=$( { p0 "$image_id"; cat "$override"; } | md5_compat)
  372. else
  373. hash=$(p0 "$image_id" | md5_compat)
  374. fi
  375. _replace_compose_file_opt "$hash" "$override" "$@" || return 1
  376. }
  377. _replace_compose_file_opt() {
  378. local hash_bin="$1" override="$2" \
  379. cache_file="$COMPOSE_LAUNCHER_CACHE/$FUNCNAME.cache.$(p0 "$@" | md5_compat)"
  380. if [ -e "$cache_file" ]; then
  381. cat "$cache_file" &&
  382. touch "$cache_file" || return 1
  383. return 0
  384. fi
  385. debug "Replacing '-f|--file' argument in command line."
  386. shift 2
  387. DC_MATCH_MULTI=$(get_compose_multi_opts_list "$hash_bin" "$override") || return 1
  388. DC_MATCH_SINGLE=$(get_compose_single_opts_list "$hash_bin" "$override") || return 1
  389. args=()
  390. while read-0 arg; do
  391. case "$arg" in
  392. "-f"|"--file")
  393. read-0 value
  394. args+=("$arg" "${value##*/}")
  395. ;;
  396. --*|-*)
  397. if str_pattern_matches "$arg" $DC_MATCH_MULTI; then
  398. read-0 value
  399. args+=("$arg" "$value")
  400. shift
  401. elif str_pattern_matches "$arg" $DC_MATCH_SINGLE; then
  402. args+=("$arg")
  403. else
  404. err "Unknown option '$arg'. Didn't manage to pre-parse correctly options."
  405. return 1
  406. fi
  407. ;;
  408. *)
  409. args+=("$arg")
  410. while read-0 arg; do
  411. args+=("$arg")
  412. done
  413. ;;
  414. esac
  415. done < <(cla.normalize "$@")
  416. p0 "${args[@]}" | tee "$cache_file"
  417. }
  418. get_compose_opts_list() {
  419. local hash_bin="$1" override="$2" \
  420. cache_file="$COMPOSE_LAUNCHER_CACHE/$FUNCNAME.cache.$1"
  421. if [ -e "$cache_file" ]; then
  422. cat "$cache_file" &&
  423. touch "$cache_file" || return 1
  424. return 0
  425. fi
  426. debug "Pre-Launching docker to retrieve command line argument definitions."
  427. opts_list=()
  428. if [ -n "$override" ]; then
  429. opts_list+=("-v" "$override:/usr/local/bin/compose-core:ro")
  430. fi
  431. compose_opts_help=$(docker run "${opts_list[@]}" "$COMPOSE_DOCKER_IMAGE" --help 2>/dev/null)
  432. echo "$compose_opts_help" |
  433. grep '^Options:' -A 20000 |
  434. tail -n +2 |
  435. { cat ; echo; } |
  436. grep -E -m 1 "^\S*\$" -B 10000 |
  437. head -n -1 |
  438. grep -E "^\s+-" |
  439. sed -r 's/\s+((((-[a-zA-Z]|--[a-zA-Z0-9-]+)( [A-Z=]+|=[^ ]+)?)(, )?)+)\s+.*$/\1/g' |
  440. tee "$cache_file" || return 1
  441. }
  442. multi_opts_filter() {
  443. grep -E "$_MULTIOPTION_REGEX_LINE_FILTER" |
  444. sed -r "s/^($_MULTIOPTION_REGEX)(\s|=).*$/\1/g" |
  445. tr ',' "\n" | nspc
  446. }
  447. single_opts_filter() {
  448. grep -E -v "$_MULTIOPTION_REGEX_LINE_FILTER" |
  449. tr ',' "\n" | nspc
  450. }
  451. get_compose_multi_opts_list() {
  452. local hash_bin="$1" override="$2" \
  453. cache_file="$COMPOSE_LAUNCHER_CACHE/$FUNCNAME.cache.$1" opts_list
  454. if [ -e "$cache_file" ]; then
  455. cat "$cache_file" &&
  456. touch "$cache_file" || return 1
  457. return 0
  458. fi
  459. opts_list=$(get_compose_opts_list "$hash_bin" "$override") || return 1
  460. echo "$opts_list" | multi_opts_filter | tee "$cache_file"
  461. }
  462. get_compose_single_opts_list() {
  463. local hash_bin="$1" override="$2" \
  464. cache_file="$COMPOSE_LAUNCHER_CACHE/$FUNCNAME.cache.$1" opts_list
  465. if [ -e "$cache_file" ]; then
  466. cat "$cache_file" &&
  467. touch "$cache_file" || return 1
  468. return 0
  469. fi
  470. opts_list=$(get_compose_opts_list "$hash_bin" "$override") || return 1
  471. echo "$opts_list" | single_opts_filter | tee "$cache_file"
  472. }
  473. get_volume_opt() {
  474. local cache_file="$COMPOSE_LAUNCHER_CACHE/$FUNCNAME.cache.$(p0 "$@" | md5_compat)"
  475. if [ -e "$cache_file" ]; then
  476. cat "$cache_file" &&
  477. touch "$cache_file" || return 1
  478. return 0
  479. fi
  480. while [ "$#" != 0 ]; do
  481. case "$1" in
  482. "-v")
  483. dst="${2#*:}"
  484. dst="${dst%:*}"
  485. if [ "$dst" == "/usr/local/bin/compose-core" ]; then
  486. override="${2%%:*}"
  487. debug "Override of compose-core found: $override"
  488. fi
  489. shift;;
  490. "-e"|"-w")
  491. shift;;
  492. *)
  493. :
  494. ;;
  495. esac
  496. shift
  497. done
  498. { [ -n "$override" ] && echo "$override"; } | tee "$cache_file"
  499. }
  500. get_tz() {
  501. if [ -n "$TZ" ]; then
  502. :
  503. elif [ -n "$COMPOSE_LOCAL_ROOT" ] && ## previous compose run
  504. [ -e "$COMPOSE_LOCAL_ROOT/etc/timezone" ]; then
  505. read -r TZ < "$COMPOSE_LOCAL_ROOT/etc/timezone"
  506. elif [ -e "/etc/timezone" ]; then ## debian host system timezone
  507. read -r TZ < /etc/timezone
  508. elif [ -e "/etc/localtime" ]; then ## redhat and macosx sys timezone
  509. local fullpath dirname
  510. fullpath="$(readlink -f /etc/localtime)"
  511. dirname="${fullpath%/*}"
  512. TZ=${TZ:-${fullpath##*/}/${dirname##*/}}
  513. else
  514. err "Timezone not found nor inferable !"
  515. echo " compose relies on '/etc/timezone' or '/etc/localtime' to be present " >&2
  516. echo " so as to ensure same timezone for all containers that need it." >&2
  517. echo >&2
  518. echo " You can set a default value for compose by create issuing:" >&2
  519. echo >&2
  520. if [ -n "$COMPOSE_LOCAL_ROOT" ] && [ "$UID" != 0 ]; then
  521. echo " mkdir -p $COMPOSE_LOCAL_ROOT/etc &&" >&2
  522. echo " echo \"Europe/Paris\" > $COMPOSE_LOCAL_ROOT/etc/timezone" >&2
  523. else
  524. echo " echo \"Europe/Paris\" > /etc/timezone" >&2
  525. fi
  526. echo >&2
  527. echo " Of course, you can change 'Europe/Paris' value by any other valid timezone." >&2
  528. echo " timezone." >&2
  529. echo >&2
  530. echo " Notice you can also use \$TZ environment variable, but the value" >&2
  531. echo " will be necessary each time you launch compose." >&2
  532. echo >&2
  533. return 1
  534. fi
  535. e "$TZ"
  536. }
  537. pretty_print() {
  538. while [ "$#" != 0 ]; do
  539. case "$1" in
  540. "-v"|"-e"|"-w")
  541. e "$1" "$2" "\\"$'\n'
  542. shift;;
  543. *)
  544. e "$1 ";;
  545. esac
  546. shift
  547. done
  548. }
  549. win_env() { "$CMD" /c "<nul set /p=%${1}%" 2>/dev/null; }
  550. wsl_path_env() { wslpath "$(win_env "${1}")"; }
  551. set_os() {
  552. OS="$(get_os)"
  553. case "$OS" in
  554. linux)
  555. COMPOSE_LOCAL_ROOT=${COMPOSE_LOCAL_ROOT:-"$HOME/.compose"}
  556. COMPOSE_VAR=${COMPOSE_VAR:-/var/lib/compose}
  557. COMPOSE_CACHE=${COMPOSE_CACHE:-/var/cache/compose}
  558. DATASTORE=${DATASTORE:-/srv/datastore/data}
  559. CONFIGSTORE=${CONFIGSTORE:-/srv/datastore/config}
  560. if [ "$UID" == 0 ]; then
  561. SESSION_DIR=${SESSION_DIR:-"$COMPOSE_VAR"/sessions}
  562. CHARM_STORE=${CHARM_STORE:-/srv/charm-store}
  563. TZ_PATH=${TZ_PATH:-"$COMPOSE_VAR"/timezones}
  564. COMPOSE_LAUNCHER_CACHE=${COMPOSE_LAUNCHER_CACHE:-"$COMPOSE_CACHE"}
  565. else
  566. SESSION_DIR=${SESSION_DIR:-"$COMPOSE_LOCAL_ROOT"/sessions}
  567. CHARM_STORE=${CHARM_STORE:-"$HOME"/.charm-store}
  568. TZ_PATH=${TZ_PATH:-"$COMPOSE_LOCAL_ROOT"/timezones}
  569. COMPOSE_LAUNCHER_CACHE=${COMPOSE_LAUNCHER_CACHE:-"$COMPOSE_LOCAL_ROOT"/cache}
  570. fi
  571. ;;
  572. mac)
  573. COMPOSE_LOCAL_ROOT=${COMPOSE_LOCAL_ROOT:-"$HOME/.compose"}
  574. COMPOSE_VAR=${COMPOSE_VAR:-"$COMPOSE_LOCAL_ROOT"/lib}
  575. COMPOSE_CACHE=${COMPOSE_CACHE:-"$COMPOSE_LOCAL_ROOT"/cache}
  576. SESSION_DIR=${SESSION_DIR:-"$COMPOSE_LOCAL_ROOT"/sessions}
  577. DATASTORE=${DATASTORE:-"$COMPOSE_LOCAL_ROOT"/data}
  578. CONFIGSTORE=${CONFIGSTORE:-"$COMPOSE_LOCAL_ROOT"/config}
  579. CHARM_STORE=${CHARM_STORE:-"$HOME"/.charm-store}
  580. TZ_PATH=${TZ_PATH:-"$COMPOSE_LOCAL_ROOT"/timezones}
  581. COMPOSE_LAUNCHER_CACHE=${COMPOSE_LAUNCHER_CACHE:-"$COMPOSE_LOCAL_ROOT"/cache}
  582. ;;
  583. wsl)
  584. type -p cmd.exe >/dev/null || {
  585. die "cmd.exe is not found in \$PATH."
  586. }
  587. CMD=$(type -p cmd.exe >/dev/null) || {
  588. for p in {/mnt,}/c/WINDOWS/SYSTEM32; do
  589. if [ -x "$p"/cmd.exe ]; then
  590. CMD="$p"/cmd.exe
  591. fi
  592. done
  593. if [ -z "$CMD" ]; then
  594. die "cmd.exe is not found in \$PATH." \
  595. "And could not find it in standard directories."
  596. fi
  597. }
  598. WIN_HOME="$(wsl_path_env UserProfile)"
  599. WIN_PROGRAM_FILES="$(wsl_path_env ProgramFiles)"
  600. COMPOSE_LAUNCHER_APP_DIR="$WIN_PROGRAM_FILES/Compose"
  601. COMPOSE_LOCAL_ROOT=${COMPOSE_LOCAL_ROOT:-"$WIN_HOME/AppData/Local/Compose/Launcher"}
  602. COMPOSE_VAR=${COMPOSE_VAR:-"$WIN_HOME/AppData/Local/Compose/lib"}
  603. COMPOSE_CACHE=${COMPOSE_CACHE:-"$WIN_HOME/AppData/Local/Compose/cache"}
  604. DATASTORE=${DATASTORE:-"$WIN_HOME/AppData/Local/Compose/data"}
  605. CONFIGSTORE=${CONFIGSTORE:-"$WIN_HOME/AppData/Local/Compose/config"}
  606. mkdir -p "$COMPOSE_VAR" "$COMPOSE_CACHE" "$DATASTORE" "$CONFIGSTORE" || return 1
  607. SESSION_DIR=${SESSION_DIR:-"$COMPOSE_LOCAL_ROOT"/sessions}
  608. CHARM_STORE=${CHARM_STORE:-"$COMPOSE_LAUNCHER_APP_DIR/charm-store"}
  609. TZ_PATH=${TZ_PATH:-"$COMPOSE_LOCAL_ROOT"/timezones}
  610. COMPOSE_LAUNCHER_CACHE=${COMPOSE_LAUNCHER_CACHE:-"$COMPOSE_LOCAL_ROOT"/cache}
  611. ;;
  612. *)
  613. echo "System '$os' not supported yet." >&2
  614. return 1
  615. ;;
  616. esac
  617. }
  618. mk_docker_run_options() {
  619. ## Order matters, files get to override vars
  620. compose_config_files=(
  621. ## DEFAULT LINUX VARS
  622. /etc/default/charm
  623. /etc/default/datastore
  624. /etc/default/compose
  625. ## COMPOSE SYSTEM-WIDE FILES
  626. /etc/compose.conf
  627. /etc/compose.local.conf
  628. /etc/compose/local.conf
  629. ## COMPOSE USER FILES
  630. ~/.compose/etc/local.conf
  631. ~/.compose.conf
  632. )
  633. set_os || return 1
  634. docker_run_opts=("-v" "/var/run/docker.sock:/var/run/docker.sock")
  635. ##
  636. ## Load config files
  637. ##
  638. if [ -z "$DISABLE_SYSTEM_CONFIG_FILE" ]; then
  639. ## XXXvlab: should provide YML config opportunities in possible parent dirs ?
  640. ## userdir ? and global /etc/compose.yml ?
  641. for cfgfile in "${compose_config_files[@]}"; do
  642. [ -e "$cfgfile" ] || continue
  643. docker_run_opts+=("-v" "$cfgfile:$cfgfile:ro")
  644. . "$cfgfile"
  645. done
  646. else
  647. docker_run_opts+=("-e" "DISABLE_SYSTEM_CONFIG_FILE=$DISABLE_SYSTEM_CONFIG_FILE")
  648. fi
  649. mkdir -p "$COMPOSE_LAUNCHER_CACHE" || return 1
  650. ## get TZ value and prepare TZ_PATH
  651. TZ=$(get_tz) || return 1
  652. mkdir -p "${TZ_PATH}"
  653. TZ_PATH="${TZ_PATH}/$(e "$TZ" | sha256sum | cut -c 1-8)" || return 1
  654. [ -e "$TZ_PATH" ] || e "$TZ" > "$TZ_PATH"
  655. ## CACHE/DATA DIRS
  656. docker_run_opts+=("-v" "$COMPOSE_VAR:/var/lib/compose")
  657. docker_run_opts+=("-v" "$COMPOSE_CACHE:/var/cache/compose")
  658. docker_run_opts+=("-v" "$TZ_PATH:/etc/timezone:ro")
  659. ##
  660. ## Checking vars
  661. ##
  662. ## CHARM_STORE
  663. [ -e "$CHARM_STORE" ] || mkdir -p "$CHARM_STORE" || return 1
  664. [ -L "$CHARM_STORE" ] && {
  665. CHARM_STORE=$(readlink -f "$CHARM_STORE") || return 1
  666. }
  667. docker_run_opts+=(
  668. "-v" "$CHARM_STORE:/srv/charm-store:ro"
  669. "-e" "CHARM_STORE=/srv/charm-store"
  670. "-e" "HOST_CHARM_STORE=$CHARM_STORE"
  671. )
  672. check_no_links_subdirs "$CHARM_STORE"/* || return 1
  673. ## DEFAULT_COMPOSE_FILE
  674. if [ "${DEFAULT_COMPOSE_FILE+x}" ]; then
  675. DEFAULT_COMPOSE_FILE=$(realpath "$DEFAULT_COMPOSE_FILE")
  676. dirname=$(dirname "$DEFAULT_COMPOSE_FILE")/
  677. if [ -e "${DEFAULT_COMPOSE_FILE}" ]; then
  678. docker_run_opts+=("-v" "$dirname:$dirname:ro")
  679. fi
  680. fi
  681. ## COMPOSE_YML_FILE
  682. if [ "${COMPOSE_YML_FILE+x}" ]; then
  683. if [ -e "${COMPOSE_YML_FILE}" ]; then
  684. docker_run_opts+=(
  685. "-v" "$COMPOSE_YML_FILE:/tmp/compose.yml:ro"
  686. "-e" "COMPOSE_YML_FILE=/tmp/compose.yml"
  687. "-e" "HOST_COMPOSE_YML_FILE=/tmp/compose.yml"
  688. )
  689. fi
  690. fi
  691. ## DATASTORE and CONFIGSTORE
  692. docker_run_opts+=(
  693. "-v" "$DATASTORE:/srv/datastore/data:rw"
  694. "-e" "DATASTORE=/srv/datastore/data"
  695. "-e" "HOST_DATASTORE=$DATASTORE"
  696. "-v" "$CONFIGSTORE:/srv/datastore/config:rw"
  697. "-e" "CONFIGSTORE=/srv/datastore/config"
  698. "-e" "HOST_CONFIGSTORE=$CONFIGSTORE"
  699. )
  700. #docker_run_opts+=("-v" "$HOME/.docker:/root/.docker")
  701. COMPOSE_DOCKER_IMAGE=${COMPOSE_DOCKER_IMAGE:-docker.0k.io/compose}
  702. docker_run_opts+=("-e" "COMPOSE_DOCKER_IMAGE=$COMPOSE_DOCKER_IMAGE")
  703. if ! docker_has_image "$COMPOSE_DOCKER_IMAGE"; then
  704. docker pull "$COMPOSE_DOCKER_IMAGE" || return 1
  705. fi
  706. ## SSH config
  707. # docker_run_opts+=(
  708. # "-v" "$HOME/.ssh:/root/.ssh:ro"
  709. # "-v" "/etc/ssh:/etc/ssh:ro"
  710. # )
  711. COMPOSE_LAUNCHER_BIN=$(readlink -f "${BASH_SOURCE[0]}")
  712. docker_run_opts+=("-v" "$COMPOSE_LAUNCHER_BIN:/usr/local/bin/compose")
  713. while read-0 var; do
  714. case "$var" in
  715. COMPOSE_YML_FILE|COMPOSE_LAUNCHER_BIN|COMPOSE_DOCKER_IMAGE|\
  716. COMPOSE_LAUNCHER_OPTS|COMPOSE_VAR|COMPOSE_CACHE)
  717. :
  718. ;;
  719. *)
  720. docker_run_opts+=("-e" "$var=${!var}")
  721. ;;
  722. esac
  723. done < <(list_compose_vars)
  724. compose_file=$(get_compose_file_opt "$COMPOSE_DOCKER_IMAGE" "$@") || return 1
  725. if [ -z "$compose_file" ]; then
  726. ## Find a compose.yml in parents
  727. debug "No config file specified on command line arguments"
  728. debug "Looking for 'compose.yml' in self and parents.."
  729. if parent=$(while true; do
  730. [ -e "./compose.yml" ] && {
  731. echo "$PWD"
  732. exit 0
  733. }
  734. [ "$PWD" == "/" ] && return 1
  735. cd ..
  736. done
  737. ); then
  738. compose_file="$(realpath "$parent"/"compose.yml")"
  739. debug " .. found '$compose_file'"
  740. else
  741. debug " .. not found."
  742. fi
  743. fi
  744. if [ -n "$compose_file" ]; then
  745. if ! [ -f "$compose_file" ]; then
  746. die "Specified compose file '$compose_file' not found."
  747. fi
  748. compose_file="$(realpath "$compose_file")"
  749. parent_dir="${compose_file%/*}"
  750. docker_path=/var/lib/compose/root/${parent_dir##*/}/${compose_file##*/}
  751. docker_run_opts+=(
  752. "-e" "COMPOSE_YML_FILE=${compose_file##*/}"
  753. "-v" "${compose_file}:${docker_path}:ro"
  754. "-w" "${docker_path%/*}"
  755. )
  756. else
  757. docker_path=/var/lib/compose/root
  758. docker_run_opts+=(
  759. "-w" "${docker_path}"
  760. )
  761. fi
  762. clean_unused_sessions
  763. filename=$(mktemp -p /tmp/ -t launch_opts-XXXXXXXXXXXXXXXX)
  764. p0 "${docker_run_opts[@]}" > "$filename"
  765. sha=$(sha256sum "$filename")
  766. sha=${sha:0:64}
  767. src="$SESSION_DIR/$UID-$sha"
  768. dest="/var/lib/compose/sessions/$UID-$sha"
  769. {
  770. p0 "-v" "$SESSION_DIR/$UID-$sha:$dest:ro"
  771. p0 "-e" "COMPOSE_LAUNCHER_OPTS=$dest"
  772. p0 "-e" "COMPOSE_LAUNCHER_BIN=$COMPOSE_LAUNCHER_BIN"
  773. } >> "$filename"
  774. mkdir -p "$SESSION_DIR" || return 1
  775. mv -f "$filename" "$SESSION_DIR/$UID-$sha" || return 1
  776. e "$SESSION_DIR/$UID-$sha"
  777. if [ -n "$DEBUG" ]; then
  778. echo "${WHITE}Environment:${NORMAL}"
  779. echo " COMPOSE_DOCKER_IMAGE: $COMPOSE_DOCKER_IMAGE"
  780. echo " CHARM_STORE: $CHARM_STORE"
  781. echo " DATASTORE: $DATASTORE"
  782. echo " CONFIGSTORE: $CONFIGSTORE"
  783. echo " COMPOSE_VAR: $COMPOSE_VAR"
  784. echo " COMPOSE_CACHE: $COMPOSE_CACHE"
  785. echo " COMPOSE_LAUNCHER_CACHE: $COMPOSE_LAUNCHER_CACHE"
  786. echo " SESSION_DIR: $SESSION_DIR"
  787. echo " TZ_PATH: $TZ_PATH"
  788. fi >&2
  789. }
  790. run() {
  791. local os docker_run_opts
  792. docker_run_opts=()
  793. if [ -z "$COMPOSE_LAUNCHER_OPTS" ]; then
  794. COMPOSE_LAUNCHER_OPTS="$(mk_docker_run_options "$@")" || return 1
  795. fi
  796. while read-0 opt; do
  797. docker_run_opts+=("$opt")
  798. ## catch COMPOSE_DOCKER_IMAGE
  799. if [[ "$env" == "true" && "$opt" == "COMPOSE_DOCKER_IMAGE="* ]]; then
  800. COMPOSE_DOCKER_IMAGE=${opt##COMPOSE_DOCKER_IMAGE=}
  801. elif [ "$opt" == "-e" ]; then
  802. env=true
  803. else
  804. env=
  805. fi
  806. done < <(cat "$COMPOSE_LAUNCHER_OPTS")
  807. set_os
  808. array_read-0 cmd_args < <(replace_compose_file_opt "$COMPOSE_DOCKER_IMAGE" "$@")
  809. set -- "${cmd_args[@]}"
  810. [ -t 0 ] && docker_run_opts+=("-i")
  811. [ -t 1 ] && docker_run_opts+=("-t")
  812. if [ -n "$DEBUG" ] || [ -n "$DRY_RUN" ]; then
  813. debug "${WHITE}Launching:${NORMAL}"
  814. echo "docker run --rm \\"
  815. pretty_print "${docker_run_opts[@]}" | sed -r 's/^/ /g;s/([^\])$/\1\\\n/g'
  816. if [ -z "$ENTER" ]; then
  817. echo " ${COMPOSE_DOCKER_IMAGE} \\"
  818. echo " " "$@"
  819. else
  820. echo " --entrypoint bash \\"
  821. echo " ${COMPOSE_DOCKER_IMAGE}"
  822. fi
  823. fi | { if [ -n "$DEBUG" ]; then sed -r 's/^/ /g'; else cat; fi } >&2
  824. if [ -z "$DRY_RUN" ]; then
  825. debug "${WHITE}Execution:${NORMAL}"
  826. if [ -z "$ENTER" ]; then
  827. exec docker run --rm "${docker_run_opts[@]}" "${COMPOSE_DOCKER_IMAGE}" "$@"
  828. else
  829. exec docker run --rm "${docker_run_opts[@]}" \
  830. --entrypoint bash \
  831. "${COMPOSE_DOCKER_IMAGE}"
  832. fi
  833. fi
  834. }
  835. [ -n "$SOURCED" ] && return 0
  836. ##
  837. ## Code
  838. ##
  839. depends docker cat readlink sed realpath tee sed grep tail
  840. ansi_color "${ansi_color:-tty}"
  841. run "$@"