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.

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