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.

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