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.

1045 lines
30 KiB

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