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.

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