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.

1129 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" container_id src dst
  299. while read container_id; do
  300. while read-0 src dst; do
  301. [ "$src" == "$volume" ] && {
  302. return 0
  303. }
  304. done < <(get_volumes_for_container "$container_id")
  305. done < <(get_running_compose_containers)
  306. return 1
  307. }
  308. _MULTIOPTION_REGEX='^((-[a-zA-Z]|--[a-zA-Z0-9-]+)(, )?)+'
  309. _MULTIOPTION_REGEX_LINE_FILTER=$_MULTIOPTION_REGEX'(\s|=)'
  310. ##
  311. ## compose launcher functions
  312. ##
  313. clean_unused_sessions() {
  314. for f in "$SESSION_DIR/"*; do
  315. [ -e "$f" ] || continue
  316. is_volume_used "$f" && continue
  317. ## XXXvlab: the second rmdir should not be useful
  318. rm -f "$f" >/dev/null || rmdir "$f" >/dev/null || {
  319. debug "Unexpected session remnants $f"
  320. }
  321. done
  322. }
  323. check_no_links_subdirs() {
  324. local dir
  325. for dir in "$@"; do
  326. [ -d "$dir" ] || continue
  327. if [ -L "$dir" ]; then
  328. err "Unfortunately, this compose launcher do not support yet symlinks in charm-store."
  329. echo " Found symlink in charm-store: $dir" >&2
  330. return 1
  331. fi
  332. [ -e "$dir/metadata.yml" ] && continue
  333. check_no_links_subdirs "$dir"/* || return 1
  334. done
  335. }
  336. get_override() {
  337. local override
  338. override=$(get_volume_opt "$@") || return 1
  339. if [ -n "$override" ]; then
  340. if ! [ -f "$override" ]; then
  341. err "Invalid override of 'compose-core' detected." \
  342. "File '$override' does not exist on host."
  343. return 1
  344. fi
  345. echo "$override"
  346. fi
  347. }
  348. get_hash_image() {
  349. local compose_docker_image="$1" override="$2"
  350. {
  351. docker_image_id "$compose_docker_image" || {
  352. err "Failed to get docker image id of image '$compose_docker_image'."
  353. return 1
  354. }
  355. p0 ""
  356. [ -n "$override" ] && cat "$override"
  357. true
  358. } | hash_get
  359. return "${PIPESTATUS[0]}"
  360. }
  361. get_compose_file_opt() {
  362. local hash_bin="$1" override="$2" \
  363. cache_file="$COMPOSE_LAUNCHER_CACHE/$FUNCNAME.cache.$(p0 "$@" | hash_get)"
  364. if [ -e "$cache_file" ]; then
  365. cat "$cache_file" &&
  366. touch "$cache_file" || return 1
  367. return 0
  368. fi
  369. shift 2
  370. DC_MATCH_MULTI=$(get_compose_multi_opts_list "$hash_bin" "$override") || return 1
  371. DC_MATCH_SINGLE=$(get_compose_single_opts_list "$hash_bin" "$override") || return 1
  372. while read-0 arg; do
  373. case "$arg" in
  374. "-f"|"--file")
  375. read-0 value
  376. e "$value"
  377. return 0
  378. ;;
  379. --*|-*)
  380. if str_pattern_matches "$arg" $DC_MATCH_MULTI; then
  381. read-0 value
  382. opts+=("$arg" "$value")
  383. shift
  384. elif str_pattern_matches "$arg" $DC_MATCH_SINGLE; then
  385. opts+=("$arg")
  386. else
  387. debug "Unknown option '$arg'. Didn't manage to pre-parse correctly options."
  388. return 1
  389. fi
  390. ;;
  391. *)
  392. return 1
  393. ;;
  394. esac
  395. done < <(cla.normalize "$@") | tee "$cache_file"
  396. }
  397. replace_compose_file_opt() {
  398. local hash_bin="$1" override="$2" args arg \
  399. cache_file="$COMPOSE_LAUNCHER_CACHE/$FUNCNAME.cache.$(p0 "$@" | hash_get)"
  400. if [ -e "$cache_file" ]; then
  401. cat "$cache_file" &&
  402. touch "$cache_file" || return 1
  403. return 0
  404. fi
  405. debug "Replacing '-f|--file' argument in command line."
  406. shift 2
  407. DC_MATCH_MULTI=$(get_compose_multi_opts_list "$hash_bin" "$override") || return 1
  408. DC_MATCH_SINGLE=$(get_compose_single_opts_list "$hash_bin" "$override") || return 1
  409. args=()
  410. while read-0 arg; do
  411. case "$arg" in
  412. "-f"|"--file")
  413. read-0 value
  414. args+=("$arg" "${value##*/}")
  415. ;;
  416. --*|-*)
  417. if str_pattern_matches "$arg" $DC_MATCH_MULTI; then
  418. read-0 value
  419. args+=("$arg" "$value")
  420. shift
  421. elif str_pattern_matches "$arg" $DC_MATCH_SINGLE; then
  422. args+=("$arg")
  423. else
  424. err "Unknown option '$arg'. Didn't manage to pre-parse correctly options."
  425. return 1
  426. fi
  427. ;;
  428. *)
  429. args+=("$arg")
  430. while read-0 arg; do
  431. args+=("$arg")
  432. done
  433. ;;
  434. esac
  435. done < <(cla.normalize "$@")
  436. p0 "${args[@]}" | tee "$cache_file"
  437. }
  438. get_compose_opts_list() {
  439. local hash_bin="$1" override="$2" \
  440. cache_file="$COMPOSE_LAUNCHER_CACHE/$FUNCNAME.cache.$1"
  441. if [ -e "$cache_file" ]; then
  442. cat "$cache_file" &&
  443. touch "$cache_file" || return 1
  444. return 0
  445. fi
  446. debug "Pre-Launching docker to retrieve command line argument definitions."
  447. opts_list=()
  448. if [ -n "$override" ]; then
  449. opts_list+=("-v" "$override:/usr/local/bin/compose-core:ro")
  450. fi
  451. compose_opts_help=$(docker run "${opts_list[@]}" "$COMPOSE_DOCKER_IMAGE" --help 2>/dev/null)
  452. echo "$compose_opts_help" |
  453. grep '^Options:' -A 20000 |
  454. tail -n +2 |
  455. { cat ; echo; } |
  456. grep -E -m 1 "^\S*\$" -B 10000 |
  457. head -n -1 |
  458. grep -E "^\s+-" |
  459. sed_compat 's/\s+((((-[a-zA-Z]|--[a-zA-Z0-9-]+)( [A-Z=]+|=[^ ]+)?)(, )?)+)\s+.*$/\1/g' |
  460. tee "$cache_file" || return 1
  461. }
  462. multi_opts_filter() {
  463. grep -E "$_MULTIOPTION_REGEX_LINE_FILTER" |
  464. sed_compat "s/^($_MULTIOPTION_REGEX)(\s|=).*$/\1/g" |
  465. tr ',' "\n" | nspc
  466. }
  467. single_opts_filter() {
  468. grep -E -v "$_MULTIOPTION_REGEX_LINE_FILTER" |
  469. tr ',' "\n" | nspc
  470. }
  471. get_compose_multi_opts_list() {
  472. local hash_bin="$1" override="$2" \
  473. cache_file="$COMPOSE_LAUNCHER_CACHE/$FUNCNAME.cache.$1" opts_list
  474. if [ -e "$cache_file" ]; then
  475. cat "$cache_file" &&
  476. touch "$cache_file" || return 1
  477. return 0
  478. fi
  479. opts_list=$(get_compose_opts_list "$hash_bin" "$override") || return 1
  480. echo "$opts_list" | multi_opts_filter | tee "$cache_file"
  481. }
  482. get_compose_single_opts_list() {
  483. local hash_bin="$1" override="$2" \
  484. cache_file="$COMPOSE_LAUNCHER_CACHE/$FUNCNAME.cache.$1" opts_list
  485. if [ -e "$cache_file" ]; then
  486. cat "$cache_file" &&
  487. touch "$cache_file" || return 1
  488. return 0
  489. fi
  490. opts_list=$(get_compose_opts_list "$hash_bin" "$override") || return 1
  491. echo "$opts_list" | single_opts_filter | tee "$cache_file"
  492. }
  493. get_volume_opt() {
  494. local cache_file="$COMPOSE_LAUNCHER_CACHE/$FUNCNAME.cache.$(p0 "$@" | hash_get)"
  495. if [ -e "$cache_file" ]; then
  496. cat "$cache_file" &&
  497. touch "$cache_file" || return 1
  498. return 0
  499. fi
  500. while [ "$#" != 0 ]; do
  501. case "$1" in
  502. "-v")
  503. dst="${2#*:}"
  504. dst="${dst%:*}"
  505. if [ "$dst" == "/usr/local/bin/compose-core" ]; then
  506. override="${2%%:*}"
  507. debug "Override of compose-core found: $override"
  508. fi
  509. shift;;
  510. "-e"|"-w")
  511. shift;;
  512. *)
  513. :
  514. ;;
  515. esac
  516. shift
  517. done
  518. { [ -n "$override" ] && echo "$override"; } | tee "$cache_file"
  519. }
  520. get_tz() {
  521. if [ -n "$TZ" ]; then
  522. :
  523. elif [ -n "$COMPOSE_LOCAL_ROOT" ] && ## previous compose run
  524. [ -e "$COMPOSE_LOCAL_ROOT/etc/timezone" ]; then
  525. read -r TZ < "$COMPOSE_LOCAL_ROOT/etc/timezone"
  526. elif [ -e "/etc/timezone" ]; then ## debian host system timezone
  527. read -r TZ < /etc/timezone
  528. elif [ -e "/etc/localtime" ]; then ## redhat and macosx sys timezone
  529. local fullpath dirname
  530. fullpath="$(readlink -f /etc/localtime)"
  531. dirname="${fullpath%/*}"
  532. TZ=${TZ:-${fullpath##*/}/${dirname##*/}}
  533. else
  534. err "Timezone not found nor inferable !"
  535. echo " compose relies on '/etc/timezone' or '/etc/localtime' to be present " >&2
  536. echo " so as to ensure same timezone for all containers that need it." >&2
  537. echo >&2
  538. echo " You can set a default value for compose by create issuing:" >&2
  539. echo >&2
  540. if [ -n "$COMPOSE_LOCAL_ROOT" ] && [ "$UID" != 0 ]; then
  541. echo " mkdir -p $COMPOSE_LOCAL_ROOT/etc &&" >&2
  542. echo " echo \"Europe/Paris\" > $COMPOSE_LOCAL_ROOT/etc/timezone" >&2
  543. else
  544. echo " echo \"Europe/Paris\" > /etc/timezone" >&2
  545. fi
  546. echo >&2
  547. echo " Of course, you can change 'Europe/Paris' value by any other valid timezone." >&2
  548. echo " timezone." >&2
  549. echo >&2
  550. echo " Notice you can also use \$TZ environment variable, but the value" >&2
  551. echo " will be necessary each time you launch compose." >&2
  552. echo >&2
  553. return 1
  554. fi
  555. e "$TZ"
  556. }
  557. pretty_print() {
  558. while [ "$#" != 0 ]; do
  559. case "$1" in
  560. "-v"|"-e"|"-w")
  561. e "$1" "$2" "\\"$'\n'
  562. shift;;
  563. *)
  564. e "$1 ";;
  565. esac
  566. shift
  567. done
  568. }
  569. win_env() { "$CMD" /c "<nul set /p=%${1}%" 2>/dev/null; }
  570. wsl_path_env() { wslpath "$(win_env "${1}")"; }
  571. set_os() {
  572. OS="$(get_os)"
  573. case "$OS" in
  574. linux)
  575. ## Order matters, files get to override vars
  576. compose_config_files=(
  577. ## DEFAULT LINUX VARS
  578. /etc/default/charm
  579. /etc/default/datastore
  580. /etc/default/compose
  581. ## COMPOSE SYSTEM-WIDE FILES
  582. /etc/compose.conf
  583. /etc/compose.local.conf
  584. /etc/compose/local.conf
  585. ## COMPOSE USER FILES
  586. ~/.compose/etc/local.conf
  587. ~/.compose.conf
  588. )
  589. COMPOSE_LOCAL_ROOT=${COMPOSE_LOCAL_ROOT:-"$HOME/.compose"}
  590. COMPOSE_VAR=${COMPOSE_VAR:-/var/lib/compose}
  591. COMPOSE_CACHE=${COMPOSE_CACHE:-/var/cache/compose}
  592. DATASTORE=${DATASTORE:-/srv/datastore/data}
  593. CONFIGSTORE=${CONFIGSTORE:-/srv/datastore/config}
  594. if [ "$UID" == 0 ]; then
  595. SESSION_DIR=${SESSION_DIR:-"$COMPOSE_VAR"/sessions}
  596. CHARM_STORE=${CHARM_STORE:-/srv/charm-store}
  597. TZ_PATH=${TZ_PATH:-"$COMPOSE_VAR"/timezones}
  598. COMPOSE_LAUNCHER_CACHE=${COMPOSE_LAUNCHER_CACHE:-"$COMPOSE_CACHE"}
  599. else
  600. SESSION_DIR=${SESSION_DIR:-"$COMPOSE_LOCAL_ROOT"/sessions}
  601. CHARM_STORE=${CHARM_STORE:-"$HOME"/.charm-store}
  602. TZ_PATH=${TZ_PATH:-"$COMPOSE_LOCAL_ROOT"/timezones}
  603. COMPOSE_LAUNCHER_CACHE=${COMPOSE_LAUNCHER_CACHE:-"$COMPOSE_LOCAL_ROOT"/cache}
  604. fi
  605. mkdir -p "$COMPOSE_CACHE" || return 1
  606. ;;
  607. mac)
  608. ## Order matters, files get to override vars
  609. compose_config_files=(
  610. ## COMPOSE SYSTEM-WIDE FILES
  611. /etc/compose.conf
  612. /etc/compose.local.conf
  613. /etc/compose/local.conf
  614. ## COMPOSE USER FILES
  615. ~/.compose/etc/local.conf
  616. ~/.compose.conf
  617. )
  618. COMPOSE_LOCAL_ROOT=${COMPOSE_LOCAL_ROOT:-"$HOME/.compose"}
  619. COMPOSE_VAR=${COMPOSE_VAR:-"$COMPOSE_LOCAL_ROOT"/lib}
  620. COMPOSE_CACHE=${COMPOSE_CACHE:-"$COMPOSE_LOCAL_ROOT"/cache}
  621. SESSION_DIR=${SESSION_DIR:-"$COMPOSE_LOCAL_ROOT"/sessions}
  622. DATASTORE=${DATASTORE:-"$COMPOSE_LOCAL_ROOT"/data}
  623. CONFIGSTORE=${CONFIGSTORE:-"$COMPOSE_LOCAL_ROOT"/config}
  624. CHARM_STORE=${CHARM_STORE:-"$HOME"/.charm-store}
  625. TZ_PATH=${TZ_PATH:-"$COMPOSE_LOCAL_ROOT"/timezones}
  626. COMPOSE_LAUNCHER_CACHE=${COMPOSE_LAUNCHER_CACHE:-"$COMPOSE_LOCAL_ROOT"/cache}
  627. ;;
  628. wsl)
  629. type -p cmd.exe >/dev/null || {
  630. die "cmd.exe is not found in \$PATH."
  631. }
  632. CMD=$(type -p cmd.exe >/dev/null) || {
  633. for p in {/mnt,}/c/WINDOWS/SYSTEM32; do
  634. if [ -x "$p"/cmd.exe ]; then
  635. CMD="$p"/cmd.exe
  636. fi
  637. done
  638. if [ -z "$CMD" ]; then
  639. die "cmd.exe is not found in \$PATH." \
  640. "And could not find it in standard directories."
  641. fi
  642. }
  643. WIN_HOME="$(wsl_path_env UserProfile)"
  644. WIN_PROGRAM_FILES="$(wsl_path_env ProgramFiles)"
  645. ## Order matters, files get to override vars
  646. compose_config_files=(
  647. ## COMPOSE SYSTEM-WIDE FILES
  648. /etc/compose.conf
  649. /etc/compose.local.conf
  650. /etc/compose/local.conf
  651. ## COMPOSE USER FILES
  652. ~/.compose/etc/local.conf
  653. ~/.compose.conf
  654. ## COMPOSE USER FILES
  655. {~,"$WIN_HOME"}/.compose/etc/local.conf
  656. {~,"$WIN_HOME"}/.compose.conf
  657. )
  658. APPDATA_BASE="$WIN_HOME/AppData/Local/Compose"
  659. COMPOSE_LAUNCHER_APP_DIR="$WIN_PROGRAM_FILES/Compose"
  660. COMPOSE_LOCAL_ROOT=${COMPOSE_LOCAL_ROOT:-"$APPDATA_BASE/Launcher"}
  661. COMPOSE_VAR=${COMPOSE_VAR:-"$APPDATA_BASE/lib"}
  662. COMPOSE_CACHE=${COMPOSE_CACHE:-"$APPDATA_BASE/cache"}
  663. DATASTORE=${DATASTORE:-"$APPDATA_BASE/data"}
  664. CONFIGSTORE=${CONFIGSTORE:-"$APPDATA_BASE/config"}
  665. mkdir -p "$COMPOSE_VAR" "$COMPOSE_CACHE" "$DATASTORE" "$CONFIGSTORE" || return 1
  666. SESSION_DIR=${SESSION_DIR:-"$COMPOSE_LOCAL_ROOT"/sessions}
  667. CHARM_STORE=${CHARM_STORE:-"$COMPOSE_LAUNCHER_APP_DIR/charm-store"}
  668. TZ_PATH=${TZ_PATH:-"$COMPOSE_LOCAL_ROOT"/timezones}
  669. COMPOSE_LAUNCHER_CACHE=${COMPOSE_LAUNCHER_CACHE:-"$COMPOSE_LOCAL_ROOT"/cache}
  670. ;;
  671. *)
  672. echo "System '$os' not supported yet." >&2
  673. return 1
  674. ;;
  675. esac
  676. }
  677. mk_docker_run_options() {
  678. set_os || return 1
  679. docker_run_opts=("-v" "/var/run/docker.sock:/var/run/docker.sock")
  680. ##
  681. ## Load config files
  682. ##
  683. if [ -z "$DISABLE_SYSTEM_CONFIG_FILE" ]; then
  684. ## XXXvlab: should provide YML config opportunities in possible parent dirs ?
  685. ## userdir ? and global /etc/compose.yml ?
  686. for cfgfile in "${compose_config_files[@]}"; do
  687. [ -e "$cfgfile" ] || continue
  688. docker_run_opts+=("-v" "$cfgfile:$cfgfile:ro")
  689. debug "Loading config file '$cfgfile'."
  690. . "$cfgfile"
  691. done
  692. else
  693. docker_run_opts+=("-e" "DISABLE_SYSTEM_CONFIG_FILE=$DISABLE_SYSTEM_CONFIG_FILE")
  694. fi
  695. mkdir -p "$COMPOSE_LAUNCHER_CACHE" || return 1
  696. ## get TZ value and prepare TZ_PATH
  697. TZ=$(get_tz) || return 1
  698. mkdir -p "${TZ_PATH}"
  699. TZ_PATH="${TZ_PATH}/$(e "$TZ" | hash_get | cut -c 1-8)" || return 1
  700. [ -e "$TZ_PATH" ] || e "$TZ" > "$TZ_PATH"
  701. ## CACHE/DATA DIRS
  702. docker_run_opts+=("-v" "$COMPOSE_VAR:/var/lib/compose")
  703. docker_run_opts+=("-v" "$COMPOSE_CACHE:/var/cache/compose")
  704. docker_run_opts+=("-v" "$TZ_PATH:/etc/timezone:ro")
  705. ##
  706. ## Checking vars
  707. ##
  708. ## CHARM_STORE
  709. [ -e "$CHARM_STORE" ] || mkdir -p "$CHARM_STORE" || return 1
  710. [ -L "$CHARM_STORE" ] && {
  711. CHARM_STORE=$(readlink -f "$CHARM_STORE") || return 1
  712. }
  713. docker_run_opts+=(
  714. "-v" "$CHARM_STORE:/srv/charm-store:ro"
  715. "-e" "CHARM_STORE=/srv/charm-store"
  716. "-e" "HOST_CHARM_STORE=$CHARM_STORE"
  717. )
  718. check_no_links_subdirs "$CHARM_STORE"/* || return 1
  719. ## DATASTORE and CONFIGSTORE
  720. docker_run_opts+=(
  721. "-v" "$DATASTORE:/srv/datastore/data:rw"
  722. "-e" "DATASTORE=/srv/datastore/data"
  723. "-e" "HOST_DATASTORE=$DATASTORE"
  724. "-v" "$CONFIGSTORE:/srv/datastore/config:rw"
  725. "-e" "CONFIGSTORE=/srv/datastore/config"
  726. "-e" "HOST_CONFIGSTORE=$CONFIGSTORE"
  727. )
  728. if [ "$OS" == "linux" ]; then
  729. [ -d "$HOME/.docker" ] && \
  730. docker_run_opts+=("-v" "$HOME/.docker:/root/.docker:ro")
  731. [ -d "$HOME/.ssh" ] && \
  732. docker_run_opts+=("-v" "$HOME/.ssh:/root/.ssh:ro")
  733. [ -d "/etc/ssh" ] && \
  734. docker_run_opts+=("-v" "/etc/ssh:/etc/ssh:ro")
  735. fi
  736. COMPOSE_DOCKER_IMAGE=${COMPOSE_DOCKER_IMAGE:-docker.0k.io/compose}
  737. docker_run_opts+=("-e" "COMPOSE_DOCKER_IMAGE=$COMPOSE_DOCKER_IMAGE")
  738. if ! docker_has_image "$COMPOSE_DOCKER_IMAGE"; then
  739. docker pull "$COMPOSE_DOCKER_IMAGE" || return 1
  740. fi
  741. COMPOSE_LAUNCHER_BIN=$(readlink -f "${BASH_SOURCE[0]}")
  742. docker_run_opts+=("-v" "$COMPOSE_LAUNCHER_BIN:/usr/local/bin/compose")
  743. COMPOSE_LAUNCHER_BIN_OVERRIDE=$(get_override "${docker_run_opts[@]}") || return 1
  744. COMPOSE_LAUNCHER_HASH=$(
  745. get_hash_image "$COMPOSE_DOCKER_IMAGE" "$COMPOSE_LAUNCHER_BIN_OVERRIDE") || return 1
  746. while read-0 var; do
  747. case "$var" in
  748. COMPOSE_YML_FILE|COMPOSE_LAUNCHER_BIN|COMPOSE_DOCKER_IMAGE|\
  749. COMPOSE_LAUNCHER_OPTS|COMPOSE_VAR|COMPOSE_CACHE)
  750. :
  751. ;;
  752. *)
  753. docker_run_opts+=("-e" "$var=${!var}")
  754. ;;
  755. esac
  756. done < <(list_compose_vars)
  757. ARG_COMPOSE_FILE=$(
  758. get_compose_file_opt "$COMPOSE_LAUNCHER_HASH" "$COMPOSE_LAUNCHER_BIN_OVERRIDE" \
  759. "$@") || return 1
  760. compose_file="${ARG_COMPOSE_FILE:-$COMPOSE_YML_FILE}"
  761. if [ -z "$compose_file" ]; then
  762. ## Find a compose.yml in parents
  763. debug "No config file specified on command line arguments"
  764. debug "Looking for 'compose.yml' in self and parents.."
  765. if parent=$(while true; do
  766. [ -e "./compose.yml" ] && {
  767. echo "$PWD"
  768. exit 0
  769. }
  770. [ "$PWD" == "/" ] && return 1
  771. cd ..
  772. done
  773. ); then
  774. compose_file="$(realpath "$parent"/"compose.yml")"
  775. debug " .. found '$compose_file'"
  776. else
  777. debug " .. not found."
  778. fi
  779. fi
  780. if [ -z "$compose_file" ] && [ "${DEFAULT_COMPOSE_FILE+x}" ]; then
  781. debug "Using \$DEFAULT_COMPOSE_FILE value '$DEFAULT_COMPOSE_FILE' as compose file."
  782. compose_file="$DEFAULT_COMPOSE_FILE"
  783. fi
  784. if [ -n "$compose_file" ]; then
  785. if ! [ -f "$compose_file" ]; then
  786. die "Specified compose file '$compose_file' not found."
  787. fi
  788. compose_file="$(realpath "$compose_file")"
  789. if [ "$OS" == "wsl" ]; then
  790. ## Docker host is not same linux than WSL, so
  791. ## access to root files are not the same.
  792. ##YYYvlab, check on cp where is the base
  793. dst="$COMPOSE_LAUNCHER_CACHE/compose.$(hash_get < "$compose_file").yml"
  794. cp "$compose_file" "$dst"
  795. ## docker host start with /c/... whereas WSL could start with /mnt/c/...
  796. local="$dst"
  797. else
  798. local="$compose_file"
  799. fi
  800. parent_dir="${compose_file%/*}"
  801. docker_path=/var/lib/compose/root/${parent_dir##*/}/${compose_file##*/}
  802. docker_run_opts+=(
  803. "-e" "COMPOSE_YML_FILE=${compose_file##*/}"
  804. "-e" "HOST_COMPOSE_YML_FILE=${local}"
  805. "-v" "${local}:${docker_path}:ro"
  806. "-w" "${docker_path%/*}"
  807. )
  808. else
  809. docker_path=/var/lib/compose/root
  810. docker_run_opts+=(
  811. "-w" "${docker_path}"
  812. )
  813. fi
  814. clean_unused_sessions
  815. filename=$(mktemp -p /tmp/ -t launch_opts-XXXXXXXXXXXXXXXX)
  816. p0 "${docker_run_opts[@]}" > "$filename"
  817. hash=$(hash_get < "$filename") || return 1
  818. src="$SESSION_DIR/$UID-$hash"
  819. dest="/var/lib/compose/sessions/$UID-$hash"
  820. additional_docker_run_opts=(
  821. "-v" "$SESSION_DIR/$UID-$hash:$dest:ro"
  822. "-e" "HOST_COMPOSE_LAUNCHER_OPTS=$SESSION_DIR/$UID-$hash"
  823. "-e" "COMPOSE_LAUNCHER_OPTS=$dest"
  824. "-e" "COMPOSE_LAUNCHER_BIN=$COMPOSE_LAUNCHER_BIN"
  825. "--label" "compose=1"
  826. )
  827. p0 "${additional_docker_run_opts[@]}" >> "$filename"
  828. docker_run_opts+=("${additional_docker_run_opts[@]}")
  829. ## keep also some env vars:
  830. for var in ARG_COMPOSE_FILE COMPOSE_DOCKER_IMAGE COMPOSE_LAUNCHER_{BIN_OVERRIDE,HASH}; do
  831. p0 "!env:$var=${!var}"
  832. done >> "$filename"
  833. mkdir -p "$SESSION_DIR" || return 1
  834. mv -f "$filename" "$src" || return 1
  835. }
  836. load_env() {
  837. docker_run_opts=()
  838. if [ -z "$COMPOSE_LAUNCHER_OPTS" ]; then
  839. mk_docker_run_options "$@" || return 1
  840. else
  841. set_os || return 1
  842. while read-0 opt; do
  843. if [[ "$opt" == "!env:"* ]]; then
  844. opt="${opt##!env:}"
  845. var="${opt%%=*}"
  846. value="${opt#*=}"
  847. debug "Loading var: $var=$value"
  848. export "$var"="$value"
  849. else
  850. docker_run_opts+=("$opt")
  851. fi
  852. done < "$COMPOSE_LAUNCHER_OPTS"
  853. fi
  854. if [ -n "$ALL_RELATIONS" ]; then
  855. if ! [ -e "$ALL_RELATIONS" ]; then
  856. err "Variable \$ALL_RELATION provided but it doesn't point on an existing file."
  857. return 1
  858. fi
  859. if ! [ -r "$ALL_RELATIONS" ]; then
  860. err "Variable \$ALL_RELATION provided but it doesn't point on a readable file."
  861. return 1
  862. fi
  863. NEW_ALL_RELATIONS="$COMPOSE_CACHE/${ALL_RELATIONS##*/}"
  864. if [ "$NEW_ALL_RELATIONS" != "$ALL_RELATIONS" ]; then
  865. cp "$ALL_RELATIONS" "$NEW_ALL_RELATIONS" || return 1
  866. fi
  867. docker_run_opts+=("-e" "ALL_RELATIONS=$NEW_ALL_RELATIONS")
  868. fi
  869. if [ -n "$PROJECT_NAME" ]; then
  870. docker_run_opts+=("-e" "PROJECT_NAME=$PROJECT_NAME")
  871. fi
  872. }
  873. show_env() {
  874. echo "${WHITE}Environment:${NORMAL}"
  875. echo " COMPOSE_DOCKER_IMAGE: $COMPOSE_DOCKER_IMAGE"
  876. echo " CHARM_STORE: $CHARM_STORE"
  877. echo " DATASTORE: $DATASTORE"
  878. echo " CONFIGSTORE: $CONFIGSTORE"
  879. echo " COMPOSE_VAR: $COMPOSE_VAR"
  880. echo " COMPOSE_CACHE: $COMPOSE_CACHE"
  881. echo " COMPOSE_LAUNCHER_CACHE: $COMPOSE_LAUNCHER_CACHE"
  882. echo " SESSION_DIR: $SESSION_DIR"
  883. echo " TZ_PATH: $TZ_PATH"
  884. [ -n "$ALL_RELATIONS" ] &&
  885. echo " ALL_RELATIONS: $ALL_RELATIONS"
  886. [ -n "$PROJECT_NAME" ] &&
  887. echo " PROJECT_NAME: $PROJECT_NAME"
  888. }
  889. run() {
  890. local os docker_run_opts
  891. load_env "$@" || return 1
  892. [ -n "$DEBUG" ] && show_env >&2
  893. if [ -n "$ARG_COMPOSE_FILE" ]; then
  894. while read-0-err E cmd_arg; do
  895. cmd_args+=("$cmd_arg")
  896. done < <(p-err replace_compose_file_opt "$COMPOSE_LAUNCHER_HASH" \
  897. "$COMPOSE_LAUNCHER_BIN_OVERRIDE" \
  898. "$@")
  899. if [ "$E" != "0" ]; then
  900. err "Unexpected failure while trying to replace compose file option."
  901. return 1
  902. fi
  903. set -- "${cmd_args[@]}"
  904. fi
  905. ## XXXvlab: can't see a place where we wouldn't want to link stdin
  906. ## to internal process be it a terminal or not.
  907. docker_run_opts+=("-i")
  908. ## If stdin is a not a tty, then adding ``-t`` will fail
  909. [ -t 0 -a -t 1 ] && docker_run_opts+=("-t")
  910. debug "${WHITE}Launching:${NORMAL}"
  911. if [ -n "$DEBUG" ] || [ -n "$DRY_RUN" ]; then
  912. echo "docker run --rm \\"
  913. pretty_print "${docker_run_opts[@]}" | sed_compat 's/^/ /g;s/([^\])$/\1\\\n/g'
  914. if [ -z "$ENTER" ]; then
  915. printf "%s\n" " ${COMPOSE_DOCKER_IMAGE} \\"
  916. printf " "
  917. printf "%s " "$@"
  918. printf "\n"
  919. else
  920. echo " --entrypoint bash \\"
  921. echo " ${COMPOSE_DOCKER_IMAGE}"
  922. fi
  923. fi | { if [ -n "$DEBUG" ]; then sed_compat 's/^/ /g'; else cat; fi } >&2
  924. if [ -z "$DRY_RUN" ]; then
  925. debug "${WHITE}Execution:${NORMAL}"
  926. if [ -z "$ENTER" ]; then
  927. exec docker run --rm "${docker_run_opts[@]}" "${COMPOSE_DOCKER_IMAGE}" "$@"
  928. else
  929. exec docker run --rm "${docker_run_opts[@]}" \
  930. --entrypoint bash \
  931. "${COMPOSE_DOCKER_IMAGE}"
  932. fi
  933. fi
  934. }
  935. [ -n "$SOURCED" ] && return 0
  936. ##
  937. ## Code
  938. ##
  939. depends docker cat readlink sed realpath tee sed grep tail
  940. ansi_color "${ansi_color:-tty}"
  941. if [ "$SHOW_ENV" ]; then
  942. load_env "$@" || return 1
  943. show_env >&2
  944. exit 0
  945. fi
  946. if [ "$SHOW_CONFIG_LOCATIONS" ]; then
  947. set_os || return 1
  948. echo "compose will read these files if existing in the given order:"
  949. for loc in "${compose_config_files[@]}"; do
  950. echo " - $loc"
  951. done
  952. exit 0
  953. fi
  954. run "$@"