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.

1102 lines
31 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. ;;
  604. mac)
  605. ## Order matters, files get to override vars
  606. compose_config_files=(
  607. ## COMPOSE SYSTEM-WIDE FILES
  608. /etc/compose.conf
  609. /etc/compose.local.conf
  610. /etc/compose/local.conf
  611. ## COMPOSE USER FILES
  612. ~/.compose/etc/local.conf
  613. ~/.compose.conf
  614. )
  615. COMPOSE_LOCAL_ROOT=${COMPOSE_LOCAL_ROOT:-"$HOME/.compose"}
  616. COMPOSE_VAR=${COMPOSE_VAR:-"$COMPOSE_LOCAL_ROOT"/lib}
  617. COMPOSE_CACHE=${COMPOSE_CACHE:-"$COMPOSE_LOCAL_ROOT"/cache}
  618. SESSION_DIR=${SESSION_DIR:-"$COMPOSE_LOCAL_ROOT"/sessions}
  619. DATASTORE=${DATASTORE:-"$COMPOSE_LOCAL_ROOT"/data}
  620. CONFIGSTORE=${CONFIGSTORE:-"$COMPOSE_LOCAL_ROOT"/config}
  621. CHARM_STORE=${CHARM_STORE:-"$HOME"/.charm-store}
  622. TZ_PATH=${TZ_PATH:-"$COMPOSE_LOCAL_ROOT"/timezones}
  623. COMPOSE_LAUNCHER_CACHE=${COMPOSE_LAUNCHER_CACHE:-"$COMPOSE_LOCAL_ROOT"/cache}
  624. ;;
  625. wsl)
  626. type -p cmd.exe >/dev/null || {
  627. die "cmd.exe is not found in \$PATH."
  628. }
  629. CMD=$(type -p cmd.exe >/dev/null) || {
  630. for p in {/mnt,}/c/WINDOWS/SYSTEM32; do
  631. if [ -x "$p"/cmd.exe ]; then
  632. CMD="$p"/cmd.exe
  633. fi
  634. done
  635. if [ -z "$CMD" ]; then
  636. die "cmd.exe is not found in \$PATH." \
  637. "And could not find it in standard directories."
  638. fi
  639. }
  640. WIN_HOME="$(wsl_path_env UserProfile)"
  641. WIN_PROGRAM_FILES="$(wsl_path_env ProgramFiles)"
  642. ## Order matters, files get to override vars
  643. compose_config_files=(
  644. ## COMPOSE SYSTEM-WIDE FILES
  645. /etc/compose.conf
  646. /etc/compose.local.conf
  647. /etc/compose/local.conf
  648. ## COMPOSE USER FILES
  649. ~/.compose/etc/local.conf
  650. ~/.compose.conf
  651. ## COMPOSE USER FILES
  652. {~,"$WIN_HOME"}/.compose/etc/local.conf
  653. {~,"$WIN_HOME"}/.compose.conf
  654. )
  655. APPDATA_BASE="$WIN_HOME/AppData/Local/Compose"
  656. COMPOSE_LAUNCHER_APP_DIR="$WIN_PROGRAM_FILES/Compose"
  657. COMPOSE_LOCAL_ROOT=${COMPOSE_LOCAL_ROOT:-"$APPDATA_BASE/Launcher"}
  658. COMPOSE_VAR=${COMPOSE_VAR:-"$APPDATA_BASE/lib"}
  659. COMPOSE_CACHE=${COMPOSE_CACHE:-"$APPDATA_BASE/cache"}
  660. DATASTORE=${DATASTORE:-"$APPDATA_BASE/data"}
  661. CONFIGSTORE=${CONFIGSTORE:-"$APPDATA_BASE/config"}
  662. mkdir -p "$COMPOSE_VAR" "$COMPOSE_CACHE" "$DATASTORE" "$CONFIGSTORE" || return 1
  663. SESSION_DIR=${SESSION_DIR:-"$COMPOSE_LOCAL_ROOT"/sessions}
  664. CHARM_STORE=${CHARM_STORE:-"$COMPOSE_LAUNCHER_APP_DIR/charm-store"}
  665. TZ_PATH=${TZ_PATH:-"$COMPOSE_LOCAL_ROOT"/timezones}
  666. COMPOSE_LAUNCHER_CACHE=${COMPOSE_LAUNCHER_CACHE:-"$COMPOSE_LOCAL_ROOT"/cache}
  667. ;;
  668. *)
  669. echo "System '$os' not supported yet." >&2
  670. return 1
  671. ;;
  672. esac
  673. }
  674. mk_docker_run_options() {
  675. set_os || return 1
  676. docker_run_opts=("-v" "/var/run/docker.sock:/var/run/docker.sock")
  677. ##
  678. ## Load config files
  679. ##
  680. if [ -z "$DISABLE_SYSTEM_CONFIG_FILE" ]; then
  681. ## XXXvlab: should provide YML config opportunities in possible parent dirs ?
  682. ## userdir ? and global /etc/compose.yml ?
  683. for cfgfile in "${compose_config_files[@]}"; do
  684. [ -e "$cfgfile" ] || continue
  685. docker_run_opts+=("-v" "$cfgfile:$cfgfile:ro")
  686. debug "Loading config file '$cfgfile'."
  687. . "$cfgfile"
  688. done
  689. else
  690. docker_run_opts+=("-e" "DISABLE_SYSTEM_CONFIG_FILE=$DISABLE_SYSTEM_CONFIG_FILE")
  691. fi
  692. mkdir -p "$COMPOSE_LAUNCHER_CACHE" || return 1
  693. ## get TZ value and prepare TZ_PATH
  694. TZ=$(get_tz) || return 1
  695. mkdir -p "${TZ_PATH}"
  696. TZ_PATH="${TZ_PATH}/$(e "$TZ" | hash_get | cut -c 1-8)" || return 1
  697. [ -e "$TZ_PATH" ] || e "$TZ" > "$TZ_PATH"
  698. ## CACHE/DATA DIRS
  699. docker_run_opts+=("-v" "$COMPOSE_VAR:/var/lib/compose")
  700. docker_run_opts+=("-v" "$COMPOSE_CACHE:/var/cache/compose")
  701. docker_run_opts+=("-v" "$TZ_PATH:/etc/timezone:ro")
  702. ##
  703. ## Checking vars
  704. ##
  705. ## CHARM_STORE
  706. [ -e "$CHARM_STORE" ] || mkdir -p "$CHARM_STORE" || return 1
  707. [ -L "$CHARM_STORE" ] && {
  708. CHARM_STORE=$(readlink -f "$CHARM_STORE") || return 1
  709. }
  710. docker_run_opts+=(
  711. "-v" "$CHARM_STORE:/srv/charm-store:ro"
  712. "-e" "CHARM_STORE=/srv/charm-store"
  713. "-e" "HOST_CHARM_STORE=$CHARM_STORE"
  714. )
  715. check_no_links_subdirs "$CHARM_STORE"/* || return 1
  716. ## DATASTORE and CONFIGSTORE
  717. docker_run_opts+=(
  718. "-v" "$DATASTORE:/srv/datastore/data:rw"
  719. "-e" "DATASTORE=/srv/datastore/data"
  720. "-e" "HOST_DATASTORE=$DATASTORE"
  721. "-v" "$CONFIGSTORE:/srv/datastore/config:rw"
  722. "-e" "CONFIGSTORE=/srv/datastore/config"
  723. "-e" "HOST_CONFIGSTORE=$CONFIGSTORE"
  724. )
  725. if [ "$OS" == "linux" ]; then
  726. [ -d "$HOME/.docker" ] && \
  727. docker_run_opts+=("-v" "$HOME/.docker:/root/.docker:ro")
  728. [ -d "$HOME/.ssh" ] && \
  729. docker_run_opts+=("-v" "$HOME/.ssh:/root/.ssh:ro")
  730. [ -d "/etc/ssh" ] && \
  731. docker_run_opts+=("-v" "/etc/ssh:/etc/ssh:ro")
  732. fi
  733. COMPOSE_DOCKER_IMAGE=${COMPOSE_DOCKER_IMAGE:-docker.0k.io/compose}
  734. docker_run_opts+=("-e" "COMPOSE_DOCKER_IMAGE=$COMPOSE_DOCKER_IMAGE")
  735. if ! docker_has_image "$COMPOSE_DOCKER_IMAGE"; then
  736. docker pull "$COMPOSE_DOCKER_IMAGE" || return 1
  737. fi
  738. COMPOSE_LAUNCHER_BIN=$(readlink -f "${BASH_SOURCE[0]}")
  739. docker_run_opts+=("-v" "$COMPOSE_LAUNCHER_BIN:/usr/local/bin/compose")
  740. COMPOSE_LAUNCHER_BIN_OVERRIDE=$(get_override "${docker_run_opts[@]}") || return 1
  741. COMPOSE_LAUNCHER_HASH=$(
  742. get_hash_image "$COMPOSE_DOCKER_IMAGE" "$COMPOSE_LAUNCHER_BIN_OVERRIDE") || return 1
  743. while read-0 var; do
  744. case "$var" in
  745. COMPOSE_YML_FILE|COMPOSE_LAUNCHER_BIN|COMPOSE_DOCKER_IMAGE|\
  746. COMPOSE_LAUNCHER_OPTS|COMPOSE_VAR|COMPOSE_CACHE)
  747. :
  748. ;;
  749. *)
  750. docker_run_opts+=("-e" "$var=${!var}")
  751. ;;
  752. esac
  753. done < <(list_compose_vars)
  754. ARG_COMPOSE_FILE=$(
  755. get_compose_file_opt "$COMPOSE_LAUNCHER_HASH" "$COMPOSE_LAUNCHER_BIN_OVERRIDE" \
  756. "$@") || return 1
  757. compose_file="${ARG_COMPOSE_FILE:-$COMPOSE_YML_FILE}"
  758. if [ -z "$compose_file" ]; then
  759. ## Find a compose.yml in parents
  760. debug "No config file specified on command line arguments"
  761. debug "Looking for 'compose.yml' in self and parents.."
  762. if parent=$(while true; do
  763. [ -e "./compose.yml" ] && {
  764. echo "$PWD"
  765. exit 0
  766. }
  767. [ "$PWD" == "/" ] && return 1
  768. cd ..
  769. done
  770. ); then
  771. compose_file="$(realpath "$parent"/"compose.yml")"
  772. debug " .. found '$compose_file'"
  773. else
  774. debug " .. not found."
  775. fi
  776. fi
  777. if [ -z "$compose_file" ] && [ "${DEFAULT_COMPOSE_FILE+x}" ]; then
  778. debug "Using \$DEFAULT_COMPOSE_FILE value '$DEFAULT_COMPOSE_FILE' as compose file."
  779. compose_file="$DEFAULT_COMPOSE_FILE"
  780. fi
  781. if [ -n "$compose_file" ]; then
  782. if ! [ -f "$compose_file" ]; then
  783. die "Specified compose file '$compose_file' not found."
  784. fi
  785. compose_file="$(realpath "$compose_file")"
  786. if [ "$OS" == "wsl" ]; then
  787. ## Docker host is not same linux than WSL, so
  788. ## access to root files are not the same.
  789. ##YYYvlab, check on cp where is the base
  790. dst="$COMPOSE_LAUNCHER_CACHE/compose.$(hash_get < "$compose_file").yml"
  791. cp "$compose_file" "$dst"
  792. ## docker host start with /c/... whereas WSL could start with /mnt/c/...
  793. local="$dst"
  794. else
  795. local="$compose_file"
  796. fi
  797. parent_dir="${compose_file%/*}"
  798. docker_path=/var/lib/compose/root/${parent_dir##*/}/${compose_file##*/}
  799. docker_run_opts+=(
  800. "-e" "COMPOSE_YML_FILE=${compose_file##*/}"
  801. "-e" "HOST_COMPOSE_YML_FILE=${local}"
  802. "-v" "${local}:${docker_path}:ro"
  803. "-w" "${docker_path%/*}"
  804. )
  805. else
  806. docker_path=/var/lib/compose/root
  807. docker_run_opts+=(
  808. "-w" "${docker_path}"
  809. )
  810. fi
  811. clean_unused_sessions
  812. filename=$(mktemp -p /tmp/ -t launch_opts-XXXXXXXXXXXXXXXX)
  813. p0 "${docker_run_opts[@]}" > "$filename"
  814. hash=$(hash_get < "$filename") || return 1
  815. src="$SESSION_DIR/$UID-$hash"
  816. dest="/var/lib/compose/sessions/$UID-$hash"
  817. additional_docker_run_opts=(
  818. "-v" "$SESSION_DIR/$UID-$hash:$dest:ro"
  819. "-e" "HOST_COMPOSE_LAUNCHER_OPTS=$SESSION_DIR/$UID-$hash"
  820. "-e" "COMPOSE_LAUNCHER_OPTS=$dest"
  821. "-e" "COMPOSE_LAUNCHER_BIN=$COMPOSE_LAUNCHER_BIN"
  822. "--label" "compose=1"
  823. )
  824. p0 "${additional_docker_run_opts[@]}" >> "$filename"
  825. docker_run_opts+=("${additional_docker_run_opts[@]}")
  826. ## keep also some env vars:
  827. for var in ARG_COMPOSE_FILE COMPOSE_DOCKER_IMAGE COMPOSE_LAUNCHER_{BIN_OVERRIDE,HASH}; do
  828. p0 "!env:$var=${!var}"
  829. done >> "$filename"
  830. mkdir -p "$SESSION_DIR" || return 1
  831. mv -f "$filename" "$src" || return 1
  832. }
  833. load_env() {
  834. docker_run_opts=()
  835. if [ -z "$COMPOSE_LAUNCHER_OPTS" ]; then
  836. mk_docker_run_options "$@" || return 1
  837. else
  838. set_os || return 1
  839. while read-0 opt; do
  840. if [[ "$opt" == "!env:"* ]]; then
  841. opt="${opt##!env:}"
  842. var="${opt%%=*}"
  843. value="${opt#*=}"
  844. debug "Loading var: $var=$value"
  845. export "$var"="$value"
  846. else
  847. docker_run_opts+=("$opt")
  848. fi
  849. done < <(cat "$COMPOSE_LAUNCHER_OPTS")
  850. fi
  851. }
  852. show_env() {
  853. echo "${WHITE}Environment:${NORMAL}"
  854. echo " COMPOSE_DOCKER_IMAGE: $COMPOSE_DOCKER_IMAGE"
  855. echo " CHARM_STORE: $CHARM_STORE"
  856. echo " DATASTORE: $DATASTORE"
  857. echo " CONFIGSTORE: $CONFIGSTORE"
  858. echo " COMPOSE_VAR: $COMPOSE_VAR"
  859. echo " COMPOSE_CACHE: $COMPOSE_CACHE"
  860. echo " COMPOSE_LAUNCHER_CACHE: $COMPOSE_LAUNCHER_CACHE"
  861. echo " SESSION_DIR: $SESSION_DIR"
  862. echo " TZ_PATH: $TZ_PATH"
  863. }
  864. run() {
  865. local os docker_run_opts
  866. load_env "$@" || return 1
  867. [ -n "$DEBUG" ] && show_env >&2
  868. if [ -n "$ARG_COMPOSE_FILE" ]; then
  869. while read-0-err E cmd_arg; do
  870. cmd_args+=("$cmd_arg")
  871. done < <(p-err replace_compose_file_opt "$COMPOSE_LAUNCHER_HASH" \
  872. "$COMPOSE_LAUNCHER_BIN_OVERRIDE" \
  873. "$@")
  874. if [ "$E" != "0" ]; then
  875. err "Unexpecte failure while trying to replace compose file option."
  876. return 1
  877. fi
  878. set -- "${cmd_args[@]}"
  879. fi
  880. ## XXXvlab: can't see a place where we wouldn't want to link stdin
  881. ## to internal process be it a terminal or not.
  882. docker_run_opts+=("-i")
  883. ## If stdin is a not a tty, then adding ``-t`` will fail
  884. [ -t 0 -a -t 1 ] && docker_run_opts+=("-t")
  885. debug "${WHITE}Launching:${NORMAL}"
  886. if [ -n "$DEBUG" ] || [ -n "$DRY_RUN" ]; then
  887. echo "docker run --rm \\"
  888. pretty_print "${docker_run_opts[@]}" | sed_compat 's/^/ /g;s/([^\])$/\1\\\n/g'
  889. if [ -z "$ENTER" ]; then
  890. printf "%s\n" " ${COMPOSE_DOCKER_IMAGE} \\"
  891. printf " "
  892. printf "%s " "$@"
  893. printf "\n"
  894. else
  895. echo " --entrypoint bash \\"
  896. echo " ${COMPOSE_DOCKER_IMAGE}"
  897. fi
  898. fi | { if [ -n "$DEBUG" ]; then sed_compat 's/^/ /g'; else cat; fi } >&2
  899. if [ -z "$DRY_RUN" ]; then
  900. debug "${WHITE}Execution:${NORMAL}"
  901. if [ -z "$ENTER" ]; then
  902. exec docker run --rm "${docker_run_opts[@]}" "${COMPOSE_DOCKER_IMAGE}" "$@"
  903. else
  904. exec docker run --rm "${docker_run_opts[@]}" \
  905. --entrypoint bash \
  906. "${COMPOSE_DOCKER_IMAGE}"
  907. fi
  908. fi
  909. }
  910. [ -n "$SOURCED" ] && return 0
  911. ##
  912. ## Code
  913. ##
  914. depends docker cat readlink sed realpath tee sed grep tail
  915. ansi_color "${ansi_color:-tty}"
  916. if [ "$SHOW_ENV" ]; then
  917. load_env "$@" || return 1
  918. show_env >&2
  919. exit 0
  920. fi
  921. if [ "$SHOW_CONFIG_LOCATIONS" ]; then
  922. set_os || return 1
  923. echo "compose will read these files if existing in the given order:"
  924. for loc in "${compose_config_files[@]}"; do
  925. echo " - $loc"
  926. done
  927. exit 0
  928. fi
  929. run "$@"