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.

4351 lines
143 KiB

  1. #!/bin/bash
  2. ##
  3. ## TODO:
  4. ## - subordinate container should really be able to modify base image of their master
  5. ## - this could be done through docker-update
  6. ## - I'm not happy with the current build using 'build/' directory, this should be
  7. ## changed to:
  8. ## - always have a base image (specified in metadata), and always have hooks/install
  9. ## executed and merge in image (like docker-build-charm).
  10. ## - container base image is ALWAYS the image of the master container... this brings
  11. ## questions about a double way to express inheritage (through relations as it is
  12. ## implemented now, or through this base-image ?)
  13. ## - the name of the scripts for relation (aka relation_name-relation-joined) is bad as
  14. ## reading the name in a hooks/ dir, there are no way to know if we are the target or
  15. ## the base of the relation.
  16. ## - we could leverage a 'relations/' dir on the root of the charm, with both:
  17. ## 'relations/provide/relation_name' and 'relations/receive/relation_name'
  18. ## - a very bad point with the actual naming is that we can't have a providing AND
  19. ## receiving a relation with same name.
  20. ## - The cache system should keep md5 of docker-compose and other things between runs
  21. ## - The cache system should use underlying function that have only arguments inputs.
  22. ## This will allow to cache completely without issues function in time.
  23. ## - would probably need instrospection in charm custom action to know if these need
  24. ## init or relations to be set up.
  25. ## - Be clear about when the SERVICE name is used and the CHARM name is used.
  26. ## - in case of service contained in another container
  27. ## - in normal case
  28. ## - in docker-compose, can't use charm name: if we want 2 instances of the same charm
  29. ## we are stuck. What will be unique is the name of the service.
  30. ## - some relations are configured in compose.yml but should not trigger the loading
  31. ## of necessary component (for instance, apache --> log-rotate), if log-rotate is
  32. ## not there, this link should considered optional.
  33. ## - Could probably allow an unexistent charm to be populated with only "docker-image:"
  34. ## of the same name. Although this should trigger a visible warning.
  35. #:-
  36. [ -e /etc/shlib ] && . /etc/shlib || {
  37. echo "Unsatisfied dependency. Please install 'kal-shlib-core'."
  38. exit 1
  39. }
  40. #:-
  41. include common
  42. include pretty
  43. include parse
  44. include charm
  45. include array
  46. include cla
  47. include docker
  48. depends shyaml docker
  49. exname="compose"
  50. version=0.1
  51. usage="$exname [COMPOSE_OPTS] [ACTION [ACTION_OPTS]]"
  52. help="\
  53. $WHITE$exname$NORMAL jobs is to run various shell scripts to build
  54. a running orchestrated and configured docker containers. These shell
  55. scripts will have the opportunity to build a 'docker-compose.yml'.
  56. Once init script and relations scripts are executed, $WHITE$exname$NORMAL
  57. delegate the launching to ${WHITE}docker-compose${NORMAL} by providing it
  58. the final 'docker-compose.yml'.
  59. $WHITE$exname$NORMAL also leverage charms to offer some additional custom
  60. actions per charm, which are simply other scripts that can be
  61. run without launching ${WHITE}docker-compose${NORMAL}.
  62. In compose message, color coding is enforced as such:
  63. - ${DARKCYAN}action$NORMAL,
  64. - ${DARKBLUE}relation$NORMAL,
  65. - ${DARKPINK}charm${NORMAL},
  66. - ${DARKYELLOW}service${NORMAL},
  67. - ${WHITE}option-name${NORMAL}/${WHITE}command-name${NORMAL}/${WHITE}Section-Title${NORMAL}
  68. $WHITE$exname$NORMAL reads '/etc/compose.conf' for global variables, and
  69. '/etc/compose.local.conf' for local host adjustements.
  70. "
  71. ## XXXvlab: this doesn't seem to work when 'compose' is called in
  72. ## a hook of a charm.
  73. #[[ "${BASH_SOURCE[0]}" == "" ]] && SOURCED=true
  74. $(return >/dev/null 2>&1) && SOURCED=true
  75. if [ "$UID" == 0 ]; then
  76. CACHEDIR=${CACHEDIR:-/var/cache/compose}
  77. VARDIR=${VARDIR:-/var/lib/compose}
  78. else
  79. [ "$XDG_CONFIG_HOME" ] && CACHEDIR=${CACHEDIR:-$XDG_CONFIG_HOME/compose}
  80. [ "$XDG_DATA_HOME" ] && VARDIR=${VARDIR:-$XDG_DATA_HOME/compose}
  81. CACHEDIR=${CACHEDIR:-$HOME/.cache/compose}
  82. VARDIR=${VARDIR:-$HOME/.local/share/compose}
  83. fi
  84. export VARDIR CACHEDIR
  85. md5_compat() { md5sum | cut -c -32; }
  86. quick_cat_file() { quick_cat_stdin < "$1"; }
  87. quick_cat_stdin() { local IFS=''; while read -r line; do echo "$line"; done ; }
  88. export -f quick_cat_file quick_cat_stdin md5_compat
  89. clean_cache() {
  90. local i=0
  91. for f in $(ls -t "$CACHEDIR/"*.cache.* 2>/dev/null | tail -n +500); do
  92. ((i++))
  93. rm -f "$f"
  94. done
  95. if (( i > 0 )); then
  96. debug "${WHITE}Cleaned cache:${NORMAL} Removed $((i)) elements (current cache size is $(du -sh "$CACHEDIR" | cut -f 1))"
  97. fi
  98. }
  99. export DEFAULT_COMPOSE_FILE
  100. ##
  101. ## Merge YAML files
  102. ##
  103. export _merge_yaml_common_code="
  104. import sys
  105. import yaml
  106. try:
  107. from yaml import CSafeLoader as SafeLoader, CSafeDumper as SafeDumper
  108. except ImportError: ## pragma: no cover
  109. sys.stderr.write('YAML code in pure python\n')
  110. exit(1)
  111. from yaml import SafeLoader, SafeDumper
  112. class MySafeLoader(SafeLoader): pass
  113. class MySafeDumper(SafeDumper): pass
  114. try:
  115. # included in standard lib from Python 2.7
  116. from collections import OrderedDict
  117. except ImportError:
  118. # try importing the backported drop-in replacement
  119. # it's available on PyPI
  120. from ordereddict import OrderedDict
  121. ## Ensure that there are no collision with legacy OrderedDict
  122. ## that could be used for omap for instance.
  123. class MyOrderedDict(OrderedDict):
  124. pass
  125. MySafeDumper.add_representer(
  126. MyOrderedDict,
  127. lambda cls, data: cls.represent_dict(data.items()))
  128. def construct_omap(cls, node):
  129. cls.flatten_mapping(node)
  130. return MyOrderedDict(cls.construct_pairs(node))
  131. MySafeLoader.add_constructor(
  132. yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
  133. construct_omap)
  134. ##
  135. ## Support local and global objects
  136. ##
  137. class EncapsulatedNode(object): pass
  138. def mk_encapsulated_node(s, node):
  139. method = 'construct_%s' % (node.id, )
  140. data = getattr(s, method)(node)
  141. class _E(data.__class__, EncapsulatedNode):
  142. pass
  143. _E.__name__ = str(node.tag)
  144. _E._node = node
  145. return _E(data)
  146. def represent_encapsulated_node(s, o):
  147. value = s.represent_data(o.__class__.__bases__[0](o))
  148. value.tag = o.__class__.__name__
  149. return value
  150. MySafeDumper.add_multi_representer(EncapsulatedNode,
  151. represent_encapsulated_node)
  152. MySafeLoader.add_constructor(None, mk_encapsulated_node)
  153. def fc(filename):
  154. with open(filename) as f:
  155. return f.read()
  156. def merge(*args):
  157. # sys.stderr.write('%r\n' % (args, ))
  158. args = [arg for arg in args if arg is not None]
  159. if len(args) == 0:
  160. return None
  161. if len(args) == 1:
  162. return args[0]
  163. if all(isinstance(arg, (int, basestring, bool, float)) for arg in args):
  164. return args[-1]
  165. elif all(isinstance(arg, list) for arg in args):
  166. res = []
  167. for arg in args:
  168. for elt in arg:
  169. if elt in res:
  170. res.remove(elt)
  171. res.append(elt)
  172. return res
  173. elif all(isinstance(arg, dict) for arg in args):
  174. keys = set()
  175. for arg in args:
  176. keys |= set(arg.keys())
  177. dct = {}
  178. for key in keys:
  179. sub_args = []
  180. for arg in args:
  181. if key in arg:
  182. sub_args.append(arg)
  183. try:
  184. dct[key] = merge(*(a[key] for a in sub_args))
  185. except NotImplementedError as e:
  186. raise NotImplementedError(
  187. e.args[0],
  188. '%s.%s' % (key, e.args[1]) if e.args[1] else key,
  189. e.args[2])
  190. if dct[key] is None:
  191. del dct[key]
  192. return dct
  193. else:
  194. raise NotImplementedError(
  195. 'Unsupported types: %s'
  196. % (', '.join(list(set(arg.__class__.__name__ for arg in args)))), '', args)
  197. return None
  198. def merge_cli(*args):
  199. try:
  200. c = merge(*args)
  201. except NotImplementedError as e:
  202. sys.stderr.write('Merging Failed: %s.\n%s\n'
  203. ' Values are:\n %s\n'
  204. % (e.args[0],
  205. ' Conflicting key is %r.' % e.args[1] if e.args[1] else
  206. ' Conflict at base of structure.',
  207. '\\n '.join('v%d: %r' % (i, a)
  208. for i, a in enumerate(e.args[2]))))
  209. exit(1)
  210. if c is not None:
  211. print '%s' % yaml.dump(c, default_flow_style=False, Dumper=MySafeDumper)
  212. "
  213. merge_yaml() {
  214. if ! [ -r "$state_tmpdir/merge_yaml.py" ]; then
  215. cat <<EOF > "$state_tmpdir/merge_yaml.py"
  216. $_merge_yaml_common_code
  217. merge_cli(*(yaml.load(fc(f), Loader=MySafeLoader) for f in sys.argv[1:]))
  218. EOF
  219. fi
  220. python "$state_tmpdir/merge_yaml.py" "$@"
  221. }
  222. export -f merge_yaml
  223. merge_yaml_str() {
  224. local entries="$@"
  225. if ! [ -r "$state_tmpdir/merge_yaml_str.py" ]; then
  226. cat <<EOF > "$state_tmpdir/merge_yaml_str.py" || return 1
  227. $_merge_yaml_common_code
  228. merge_cli(*(yaml.load(f, Loader=MySafeLoader) for f in sys.argv[1:]))
  229. EOF
  230. fi
  231. if ! python "$state_tmpdir/merge_yaml_str.py" "$@"; then
  232. err "Failed to merge yaml strings:"
  233. local s
  234. for s in "$@"; do
  235. printf " - \n"
  236. printf "%s\n" "$s" | prefix " ${GRAY}|$NORMAL "
  237. done >&2
  238. return 1
  239. fi
  240. }
  241. export -f merge_yaml_str
  242. yaml_get_values() {
  243. local sep=${1:-$'\n'} value input type first elt
  244. input=$(cat -)
  245. if [ -z "$input" ] || [ "$input" == "None" ]; then
  246. return 0
  247. fi
  248. type=$(e "$input" | shyaml get-type)
  249. value=
  250. case "$type" in
  251. "sequence")
  252. first=1
  253. while read-0 elt; do
  254. elt="$(e "$elt" | yaml_get_interpret)" || return 1
  255. [ "$elt" ] || continue
  256. if [ "$first" ]; then
  257. first=
  258. else
  259. value+="$sep"
  260. fi
  261. first=
  262. value+="$elt"
  263. done < <(e "$input" | shyaml -y get-values-0)
  264. ;;
  265. "struct")
  266. while read-0 val; do
  267. value+=$'\n'"$(e "$val" | yaml_get_interpret)" || return 1
  268. done < <(e "$input" | shyaml -y values-0)
  269. ;;
  270. "NoneType")
  271. value=""
  272. ;;
  273. "str"|*)
  274. value+="$(e "$input" | yaml_get_interpret)"
  275. ;;
  276. esac
  277. e "$value"
  278. }
  279. export -f yaml_get_values
  280. yaml_key_val_str() {
  281. local entries="$@"
  282. if ! [ -r "$state_tmpdir/yaml_key_val_str.py" ]; then
  283. cat <<EOF > "$state_tmpdir/yaml_key_val_str.py"
  284. $_merge_yaml_common_code
  285. print '%s' % yaml.dump(
  286. {
  287. yaml.load(sys.argv[1], Loader=MySafeLoader):
  288. yaml.load(sys.argv[2], Loader=MySafeLoader)
  289. },
  290. default_flow_style=False,
  291. Dumper=MySafeDumper,
  292. )
  293. EOF
  294. fi
  295. python "$state_tmpdir/yaml_key_val_str.py" "$@"
  296. }
  297. export -f yaml_key_val_str
  298. ##
  299. ## Docker
  300. ##
  301. docker_has_image() {
  302. local image="$1"
  303. images=$(docker images -q "$image" 2>/dev/null) || {
  304. err "docker images call has failed unexpectedly."
  305. return 1
  306. }
  307. [ "$images" ]
  308. }
  309. export -f docker_has_image
  310. docker_image_id() {
  311. local image="$1"
  312. image_id=$(docker inspect "$image" --format='{{.Id}}') || return 1
  313. echo "$image_id" # | tee "$cache_file"
  314. }
  315. export -f docker_image_id
  316. cached_cmd_on_image() {
  317. local image="$1" cache_file
  318. image_id=$(docker_image_id "$image") || return 1
  319. cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  320. if [ -e "$cache_file" ]; then
  321. # debug "$FUNCNAME: cache hit ($*)"
  322. quick_cat_stdin < "$cache_file"
  323. return 0
  324. fi
  325. shift
  326. out=$(docker run -i --rm --entrypoint /bin/sh "$image_id" -c "$*") || return 1
  327. echo "$out" | tee "$cache_file"
  328. }
  329. export -f cached_cmd_on_image
  330. cmd_on_base_image() {
  331. local service="$1" base_image
  332. shift
  333. base_image=$(service_base_docker_image "$service") || return 1
  334. docker run -i --rm --entrypoint /bin/bash "$base_image" -c "$*"
  335. }
  336. export -f cmd_on_base_image
  337. cached_cmd_on_base_image() {
  338. local service="$1" base_image cache_file="$state_tmpdir/$FUNCNAME.cache.$(printf "%s\0" "$@" | md5_compat)"
  339. shift
  340. if [ -e "$cache_file" ]; then
  341. # debug "$FUNCNAME: cache hit ($*)"
  342. quick_cat_stdin < "$cache_file"
  343. return 0
  344. fi
  345. base_image=$(service_base_docker_image "$service") || return 1
  346. if ! docker_has_image "$base_image"; then
  347. docker pull "$base_image" 1>&2
  348. fi
  349. result=$(cached_cmd_on_image "$base_image" "$@") || return 1
  350. echo "$result" | tee "$cache_file"
  351. }
  352. export -f cached_cmd_on_base_image
  353. docker_update() {
  354. ## YYY: warning, we a storing important information in cache, cache can
  355. ## be removed.
  356. ## We want here to cache the last script on given service whatever that script was
  357. local service="$1" script="$2" cache_file="$CACHEDIR/$FUNCNAME.cache.$1" \
  358. previous_base_image stored_image_id
  359. shift
  360. shift
  361. ## this will build it if necessary
  362. base_image=$(service_base_docker_image "$service") || return 1
  363. ## XXXvlab: there are probably ways to avoid rebuilding that each time
  364. image_id="$(docker_image_id "$base_image")" || return 1
  365. if [ -e "$cache_file" ]; then
  366. info "Cache file exists"
  367. read-0 previous_base_image stored_image_id < <(cat "$cache_file")
  368. info "previous: $previous_base_image"
  369. info "stored: $stored_image_id"
  370. else
  371. info "No cache file $cache_file"
  372. previous_base_image=""
  373. fi
  374. if [ "$previous_base_image" -a "$stored_image_id" == "$image_id" ]; then
  375. info "Resetting $base_image to $previous_base_image"
  376. docker tag "$previous_base_image" "$base_image" || return 1
  377. image_id="$(docker_image_id "$base_image")" || return 1
  378. else
  379. previous_base_image="$image_id"
  380. fi
  381. info "Updating base image: $base_image (hash: $image_id)"
  382. echo "$script" | dupd --debug -u "$base_image" -- "$@" || {
  383. err "Failed updating base image"
  384. return 1
  385. }
  386. new_image_id="$(docker_image_id "$base_image")"
  387. [ "$new_image_id" == "$previous_base_image" ] && {
  388. err "Image was not updated correctly (same id)."
  389. return 1
  390. }
  391. printf "%s\0" "$previous_base_image" "$new_image_id" > "$cache_file"
  392. info "Wrote cache file $cache_file"
  393. }
  394. export -f docker_update
  395. image_exposed_ports_0() {
  396. local image="$1"
  397. docker inspect --format='{{range $p, $conf := .Config.ExposedPorts}}{{$p}}{{"\x00"}}{{end}}' "$image"
  398. }
  399. export -f image_exposed_ports_0
  400. ## feature not yet included in docker: https://github.com/moby/moby/issues/16079
  401. docker_image_export_dir() {
  402. local image="$1" src="$2" dst="$3" container_id
  403. (
  404. container_id=$(docker create "$image") || exit 1
  405. trap_add EXIT,ERR "docker rm \"$container_id\" >/dev/null"
  406. docker cp "$container_id":"$src" "$dst"
  407. )
  408. }
  409. export -f docker_image_export_dir
  410. service_base_image_export_dir() {
  411. local service="$1" src="$2" dst="$3" base_image
  412. shift
  413. base_image=$(service_base_docker_image "$service") || return 1
  414. if ! docker_has_image "$base_image"; then
  415. docker pull "$base_image"
  416. fi
  417. docker_image_export_dir "$base_image" "$src" "$dst"
  418. }
  419. export -f service_base_image_export_dir
  420. service_base_image_id() {
  421. local service="$1" src="$2" dst="$3" base_image
  422. shift
  423. base_image=$(service_base_docker_image "$service") || return 1
  424. if ! docker_has_image "$base_image"; then
  425. docker pull "$base_image"
  426. fi
  427. docker inspect "$base_image" --format="{{ .Id }}"
  428. }
  429. export -f service_base_image_id
  430. ##
  431. ## Generic
  432. ##
  433. fn.exists() {
  434. declare -F "$1" >/dev/null
  435. }
  436. str_pattern_matches() {
  437. local str="$1"
  438. shift
  439. for pattern in "$@"; do
  440. eval "[[ \"$str\" == $pattern ]]" && return 0
  441. done
  442. return 1
  443. }
  444. str_matches() {
  445. local str="$1"
  446. shift
  447. for pattern in "$@"; do
  448. [[ "$str" == "$pattern" ]] && return 0
  449. done
  450. return 1
  451. }
  452. gen_password() {
  453. local l=( {a..z} {A..Z} {0..9} ) nl="${#l[@]}" size=${1:-16}
  454. while ((size--)); do
  455. echo -n "${l[$((RANDOM * nl / 32768))]}"
  456. done
  457. echo
  458. }
  459. export -f gen_password
  460. file_put() {
  461. local TARGET="$1"
  462. mkdir -p "$(dirname "$TARGET")" &&
  463. cat - > "$TARGET"
  464. }
  465. export -f file_put
  466. file_put_0() {
  467. local TARGET="$1"
  468. mkdir -p "$(dirname "$TARGET")" &&
  469. cat > "$TARGET"
  470. }
  471. export -f file_put_0
  472. fetch_file() {
  473. local src="$1"
  474. case "$src" in
  475. *"://"*)
  476. err "Unsupported target scheme."
  477. return 1
  478. ;;
  479. *)
  480. ## Try direct
  481. if ! [ -r "$src" ]; then
  482. err "File '$src' not found/readable."
  483. return 1
  484. fi
  485. cat "$src" || return 1
  486. ;;
  487. esac
  488. }
  489. export -f fetch_file
  490. ## receives stdin content to decompress on stdout
  491. ## stdout content should be tar format.
  492. uncompress_file() {
  493. local filename="$1"
  494. ## Warning, the content of the file is already as stdin, the filename
  495. ## is there to hint for correct decompression.
  496. case "$filename" in
  497. *".gz")
  498. gunzip
  499. ;;
  500. *".bz2")
  501. bunzip2
  502. ;;
  503. *)
  504. cat
  505. ;;
  506. esac
  507. }
  508. export -f uncompress_file
  509. get_file() {
  510. local src="$1"
  511. fetch_file "$src" | uncompress_file "$src"
  512. }
  513. export -f get_file
  514. ##
  515. ## Common database lib
  516. ##
  517. _clean_docker() {
  518. local _DB_NAME="$1" container_id="$2"
  519. (
  520. set +e
  521. debug "Removing container $_DB_NAME"
  522. docker stop "$container_id"
  523. docker rm "$_DB_NAME"
  524. docker network rm "${_DB_NAME}"
  525. rm -vf "$state_tmpdir/${_DB_NAME}.state"
  526. )
  527. }
  528. export -f _clean_docker
  529. get_service_base_image_dir_uid_gid() {
  530. local service="$1" dir="$2" uid_gid
  531. uid_gid=$(cached_cmd_on_base_image "$service" "stat -c '%u %g' '$dir'") || {
  532. debug "Failed to query '$dir' uid in ${DARKYELLOW}$service${NORMAL} base image."
  533. return 1
  534. }
  535. info "uid and gid from ${DARKYELLOW}$service${NORMAL}:$dir is '$uid_gid'"
  536. echo "$uid_gid"
  537. }
  538. export -f get_service_base_image_dir_uid_gid
  539. get_service_type() {
  540. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1"
  541. if [ -z "$service" ]; then
  542. print_syntax_error "$FUNCNAME: Please specify a service as first argument."
  543. return 1
  544. fi
  545. if [ -e "$cache_file" ]; then
  546. # debug "$FUNCNAME: cache hit ($*)"
  547. cat "$cache_file"
  548. return 0
  549. fi
  550. charm=$(get_service_charm "$service") || return 1
  551. metadata=$(charm.metadata "$charm") || return 1
  552. printf "%s" "$metadata" | shyaml get-value type service 2>/dev/null |
  553. tee "$cache_file"
  554. }
  555. are_files_locked_in_dir() {
  556. local dir="$1" device hdev ldev
  557. device=$(stat -c %d "$dir") || {
  558. err "Can't stat '$dir'."
  559. return 1
  560. }
  561. device=$(printf "%04x" $device)
  562. hdev=${device:0:2}
  563. ldev=${device:2:2}
  564. inodes=$(find "$dir" -printf ':%i:\n')
  565. found=
  566. while read -r inode; do
  567. debug "try inode:$inode"
  568. if [[ "$inodes" == *":$inode:"* ]]; then
  569. found=1
  570. break
  571. fi
  572. done < <(cat /proc/locks | grep " $hdev:$ldev:" | sed -r "s/^.*$hdev:$ldev:([0-9]+).*$/\1/g")
  573. [ "$found" ]
  574. }
  575. export -f are_files_locked_in_dir
  576. set_db_params() {
  577. local docker_ip="$1" docker_network="$2"
  578. if [ -z "$DB_PARAMS_LOADED" ]; then
  579. DB_PARAMS_LOADED=1
  580. _set_db_params "$docker_ip" "$docker_network"
  581. fi
  582. }
  583. export -f set_db_params
  584. export _PID="$$"
  585. ensure_db_docker_running () {
  586. local _STATE_FILE errlvl
  587. _DB_NAME="db_${DB_NAME}_${_PID}"
  588. _STATE_FILE="$state_tmpdir/${_DB_NAME}.state"
  589. if [ -e "$_STATE_FILE" ]; then
  590. IFS=: read DOCKER_NETWORK DOCKER_IP <<<"$(cat "$_STATE_FILE")"
  591. debug "Re-using previous docker/connection '$DOCKER_IP'."
  592. set_db_params "$DOCKER_IP" "$DOCKER_NETWORK"
  593. return 0
  594. fi
  595. if [ -e "$state_tmpdir/${_DB_NAME}.working" ]; then
  596. ## avoid recursive calls.
  597. if [ -z "$DOCKER_IP" ]; then
  598. err "Currently figuring up DOCKER_IP, please set it yourself before this call if needed."
  599. return 1
  600. else
  601. debug "ignoring recursive call of 'ensure_db_docker_running'."
  602. fi
  603. return 0
  604. fi
  605. touch "$state_tmpdir/${_DB_NAME}.working"
  606. docker rm "$_DB_NAME" 2>/dev/null || true
  607. host_db_working_dir="$HOST_DATASTORE/${SERVICE_NAME}$DB_DATADIR"
  608. if is_db_locked; then
  609. info "Some process is using '$host_db_working_dir'. Trying to find a docker that would do this..."
  610. found=
  611. for docker_id in $(docker ps -q); do
  612. has_volume_mounted=$(
  613. docker inspect \
  614. --format "{{range .Mounts}}{{if eq .Destination \"$DB_DATADIR\"}}{{.Source}}{{end}}{{end}}" \
  615. "$docker_id")
  616. if [ "$has_volume_mounted" == "$host_db_working_dir" ]; then
  617. found="$docker_id"
  618. break
  619. fi
  620. done
  621. if [ -z "$found" ]; then
  622. err "Please shutdown any other docker using this directory."
  623. return 1
  624. fi
  625. export container_id="$found"
  626. info "Found docker $docker_id is already running."
  627. else
  628. verb "Database is not locked."
  629. if ! docker_has_image "$DOCKER_BASE_IMAGE"; then
  630. docker pull "$DOCKER_BASE_IMAGE"
  631. fi
  632. _set_server_db_params || return 1
  633. debug docker network create "$_DB_NAME"
  634. if ! network_id=$(docker network create "$_DB_NAME"); then
  635. err "'docker network create $_DB_NAME' failed !"
  636. _clean_docker "$_DB_NAME" "$container_id"
  637. rm "$state_tmpdir/${_DB_NAME}.working"
  638. return 1
  639. fi
  640. debug docker run -d \
  641. --name "$_DB_NAME" \
  642. "${server_docker_opts[@]}" \
  643. --network "$_DB_NAME" \
  644. -v "$host_db_working_dir:$DB_DATADIR" \
  645. "$DOCKER_BASE_IMAGE"
  646. if ! container_id=$(
  647. docker run -d \
  648. --name "$_DB_NAME" \
  649. "${server_docker_opts[@]}" \
  650. --network "$_DB_NAME" \
  651. -v "$host_db_working_dir:$DB_DATADIR" \
  652. "$DOCKER_BASE_IMAGE"
  653. ); then
  654. err "'docker run' failed !"
  655. _clean_docker "$_DB_NAME" "$container_id"
  656. rm "$state_tmpdir/${_DB_NAME}.working"
  657. return 1
  658. fi
  659. trap_add EXIT,ERR "_clean_docker \"$_DB_NAME\" \"$container_id\""
  660. fi
  661. if docker_ip=$(wait_for_docker_ip "$container_id"); then
  662. IFS=: read DOCKER_NETWORK DOCKER_IP <<<"$docker_ip"
  663. echo "$docker_ip" > "$_STATE_FILE"
  664. debug "written '$_STATE_FILE'"
  665. rm "$state_tmpdir/${_DB_NAME}.working"
  666. set_db_params "$DOCKER_IP" "$DOCKER_NETWORK"
  667. return 0
  668. else
  669. errlvl="$?"
  670. err "Db not found (errlvl: $errlvl). Tail of docker logs follows:"
  671. docker logs --tail=5 "$container_id" 2>&1 | prefix " | " >&2
  672. rm "$state_tmpdir/${_DB_NAME}.working"
  673. return "$errlvl"
  674. fi
  675. }
  676. export -f ensure_db_docker_running
  677. ## Require to set $db_docker_opts if needed, and $DB_PASSFILE
  678. ##
  679. _dcmd() {
  680. local docker_opts command="$1"
  681. shift
  682. debug "Db> $command $@"
  683. if [ "$HOST_DB_PASSFILE" -a -f "$LOCAL_DB_PASSFILE" -a "$CLIENT_DB_PASSFILE" ]; then
  684. verb "Found and using '$HOST_DB_PASSFILE' as '$CLIENT_DB_PASSFILE'."
  685. docker_opts=("${db_docker_opts[@]}" "-v" "$HOST_DB_PASSFILE:$CLIENT_DB_PASSFILE")
  686. else
  687. docker_opts=("${db_docker_opts[@]}")
  688. fi
  689. ## XXXX was here: actualy, we need only connection between this version and the client version
  690. debug docker run -i --rm \
  691. "${docker_opts[@]}" \
  692. --entrypoint "$command" "$DOCKER_BASE_IMAGE" "${db_cmd_opts[@]}" "$@"
  693. docker run -i --rm \
  694. "${docker_opts[@]}" \
  695. --entrypoint "$command" "$DOCKER_BASE_IMAGE" "${db_cmd_opts[@]}" "$@"
  696. }
  697. export -f _dcmd
  698. ## Executes code through db
  699. dcmd() {
  700. local fun
  701. [ "$DB_NAME" ] || print_syntax_error "$FUNCNAME: You must provide \$DB_NAME."
  702. [ "$DB_DATADIR" ] || print_syntax_error "$FUNCNAME: You must provide \$DB_DATADIR."
  703. # [ "$DB_PASSFILE" ] || print_syntax_error "$FUNCNAME: You must provide \$DB_PASSFILE."
  704. [ "$_PID" ] || print_syntax_error "$FUNCNAME: You must provide \$_PID."
  705. for fun in is_db_locked _set_db_params ddb; do
  706. [ "$(type -t "$fun")" == "function" ] ||
  707. print_syntax_error "$FUNCNAME: You must provide function '$fun'."
  708. done
  709. ensure_db_docker_running </dev/null || return 1
  710. _dcmd "$@"
  711. }
  712. export -f dcmd
  713. get_docker_ips() {
  714. local name="$1" ip format network_id
  715. if ! docker inspect --format='{{ .NetworkSettings.Networks }}' "$name" >/dev/null 2>&1; then
  716. echo "default:$(docker inspect --format='{{ .NetworkSettings.IPAdress }}' "$name" 2>/dev/null)"
  717. else
  718. format='{{range $name, $conf := .NetworkSettings.Networks}}{{$name}}{{"\x00"}}{{$conf.IPAddress}}{{"\x00"}}{{end}}'
  719. while read-0 network_id ip; do
  720. printf "%s:%s\n" "$network_id" "$ip"
  721. done < <(docker inspect --format="$format" "$name")
  722. fi
  723. }
  724. export -f get_docker_ips
  725. get_docker_ip() {
  726. local name="$1"
  727. get_docker_ips "$name"
  728. }
  729. export -f get_docker_ip
  730. wait_docker_ip() {
  731. local name="$1" timeout="${2:-15}" timeout_count=0 docker_ip=
  732. start=$SECONDS
  733. while [ -z "$docker_ip" ]; do
  734. sleep 0.5
  735. docker_ip=$(get_docker_ip "$name") && break
  736. elapsed=$((SECONDS - start))
  737. if ((elapsed > timeout)); then
  738. err "${RED}timeout error${NORMAL}(${timeout}s):" \
  739. "Could not find '$name' docker container's IP."
  740. return 1
  741. fi
  742. [ "$elapsed" == "$old_elapsed" ] ||
  743. verb "Waiting for docker $name... ($elapsed/$timeout)"
  744. old_elapsed="$elapsed"
  745. done
  746. verb "Found docker $name network and IP: $docker_ip"
  747. echo "$docker_ip"
  748. }
  749. export -f wait_docker_ip
  750. wait_for_tcp_port() {
  751. local network=$1 host_port=$2 timeout=${3:-60}
  752. verb "Trying to connect to $host_port"
  753. bash_image=${DEFAULT_BASH_IMAGE:-docker.0k.io/bash}
  754. #echo docker run --rm -i --network "$network" "$bash_image" >&2
  755. docker run --rm -i --network "$network" "$bash_image" <<EOF
  756. start=\$SECONDS
  757. while true; do
  758. timeout 1 bash -c "</dev/tcp/${host_port/://}" >/dev/null 2>&1 && break
  759. sleep 0.2
  760. if [ "\$((SECONDS - start))" -gt "$timeout" ]; then
  761. exit 1
  762. fi
  763. done
  764. exit 0
  765. EOF
  766. if [ "$?" != 0 ]; then
  767. err "${RED}timeout error${NORMAL}(${timeout}s):"\
  768. "Could not connect to $host_port."
  769. return 1
  770. fi
  771. return 0
  772. }
  773. export -f wait_for_tcp_port
  774. ## Warning: requires a ``ddb`` matching current database to be checked
  775. wait_for_docker_ip() {
  776. local name=$1 DOCKER_IP= DOCKER_NETWORK= docker_ips= docker_ip= elapsed timeout=10
  777. docker_ip=$(wait_docker_ip "$name" 5) || return 1
  778. IFS=: read DOCKER_NETWORK DOCKER_IP <<<"$docker_ip"
  779. if ! str_is_ipv4 "$DOCKER_IP"; then
  780. err "internal 'wait_docker_ip' did not return a valid IP. Returned IP is '$DOCKER_IP'."
  781. return 1
  782. fi
  783. set_db_params "$DOCKER_IP" "$DOCKER_NETWORK"
  784. while read-0 port; do
  785. IFS="/" read port type <<<"$port"
  786. [ "$type" == "tcp" ] || continue
  787. wait_for_tcp_port "$DOCKER_NETWORK" "$DOCKER_IP:${port}" || return 17
  788. info "Host/Port $DOCKER_IP:${port} checked ${GREEN}open${NORMAL}."
  789. ## XXXvlab: what to do with more than one port ?
  790. break
  791. done < <(image_exposed_ports_0 "$container_id")
  792. ## Checking direct connection
  793. timeout=120
  794. start=$SECONDS
  795. while true; do
  796. if err=$(echo "$check_command" | ddb 2>&1 >/dev/null); then
  797. break
  798. fi
  799. if ! [[ "$err" == *"the database system is starting up" ]]; then
  800. err "${RED}db connection error${NORMAL}:" \
  801. "Could not connect to db on $DOCKER_IP container's IP."
  802. echo " Note: IP up, TCP ports is(are) open" >&2
  803. if [ "$err" ]; then
  804. echo " Error:" >&2
  805. printf "%s\n" "$err" | prefix " ${RED}!${NORMAL} " >&2
  806. fi
  807. return 18
  808. fi
  809. debug "Got 'database system is starting up' error."
  810. elapsed=$((SECONDS - start))
  811. if ((elapsed > timeout)); then
  812. err "${RED}db connection error${NORMAL}:"\
  813. "Could not connect to db on $DOCKER_IP" \
  814. "container's IP. (IP up, TCP ports is(are) open, sql answer after ${timeout}s)"
  815. return 1
  816. fi
  817. sleep 0.2
  818. done
  819. echo "${DOCKER_NETWORK}:${DOCKER_IP}"
  820. return 0
  821. }
  822. export -f wait_for_docker_ip
  823. docker_add_host_declaration() {
  824. local src_docker=$1 domain=$2 dst_docker=$3 dst_docker_ip= dst_docker_network
  825. dst_docker_ip=$(wait_docker_ip "$dst_docker") || exit 1
  826. IFS=: read dst_docker_ip dst_docker_network <<<"$dst_docker_ip"
  827. docker exec -i "$src_docker" bash <<EOF
  828. if cat /etc/hosts | grep -E "^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s+$domain\$" > /dev/null 2>&1; then
  829. sed -ri "s/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s+$domain\$/$dst_docker_ip $domain/g" /etc/hosts
  830. else
  831. echo "$dst_docker_ip $domain" >> /etc/hosts
  832. fi
  833. EOF
  834. }
  835. export -f docker_add_host_declaration
  836. get_running_containers_for_service() {
  837. local service="$1"
  838. docker ps --filter label="compose.master-service=$service" --format="{{.ID}}"
  839. }
  840. export -f get_running_containers_for_service
  841. get_container_network_ips() {
  842. local container="$1"
  843. docker inspect "$container" \
  844. --format='{{range $key, $val :=.NetworkSettings.Networks}}{{$key}}{{"\x00"}}{{$val.IPAddress}}{{"\x00"}}{{end}}'
  845. }
  846. export -f get_container_network_ips
  847. get_container_network_ip() {
  848. local container="$1"
  849. while read-0 network ip; do
  850. printf "%s\0" "$network" "$ip"
  851. break
  852. done < <(get_container_network_ips "$container")
  853. }
  854. export -f get_container_network_ip
  855. ##
  856. ## Internal Process
  857. ##
  858. get_docker_compose_links() {
  859. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  860. deps master_service master_target_service _relation_name \
  861. target_service _relation_config tech_dep
  862. if [ -z "$service" ]; then
  863. print_syntax_error "$FUNCNAME: Please specify a service as first argument."
  864. return 1
  865. fi
  866. if [ -e "$cache_file" ]; then
  867. # debug "$FUNCNAME: cache hit ($*)"
  868. cat "$cache_file"
  869. return 0
  870. fi
  871. master_service=$(get_top_master_service_for_service "$service") || return 1
  872. deps=()
  873. while read-0 _relation_name target_service _relation_config tech_dep; do
  874. master_target_service="$(get_top_master_service_for_service "$target_service")" || return 1
  875. [ "$master_service" == "$master_target_service" ] && continue
  876. type="$(get_service_type "$target_service")" || return 1
  877. [ "$type" == "run-once" ] && continue
  878. if [ "$tech_dep" == "reversed" ]; then
  879. deps+=("$(echo -en "$master_target_service:\n links:\n - $master_service")")
  880. elif [ "$tech_dep" == "True" ]; then
  881. deps+=("$(echo -en "$master_service:\n links:\n - $master_target_service")")
  882. fi
  883. ## XXXvlab: an attempt to add depends_on, but this doesn't work well actually
  884. ## as there's a circular dependency issue. We don't really want the full feature
  885. ## of depends_on, but just to add it as targets when doing an 'up'
  886. # deps+=("$(echo -en "$master_service:\n depends_on:\n - $master_target_service")")
  887. done < <(get_service_relations "$service")
  888. merge_yaml_str "${deps[@]}" | tee "$cache_file" || return 1
  889. if [ "${PIPESTATUS[0]}" != 0 ]; then
  890. rm "$cache_file"
  891. err "Failed to merge YAML from all ${WHITE}links${NORMAL} dependencies."
  892. return 1
  893. fi
  894. }
  895. _get_docker_compose_opts() {
  896. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  897. compose_def master_service docker_compose_opts
  898. if [ -z "$service" ]; then
  899. print_syntax_error "$FUNCNAME: Please specify a service as first argument."
  900. return 1
  901. fi
  902. if [ -e "$cache_file" ]; then
  903. # debug "$FUNCNAME: cache hit ($*)"
  904. cat "$cache_file"
  905. return 0
  906. fi
  907. compose_def="$(get_compose_service_def "$service")" || return 1
  908. master_service="$(get_top_master_service_for_service "$service")"
  909. if docker_compose_opts=$(echo "$compose_def" | shyaml get-value -y "docker-compose" 2>/dev/null); then
  910. yaml_key_val_str "$master_service" "$docker_compose_opts"
  911. fi | tee "$cache_file"
  912. if [ "${PIPESTATUS[0]}" != 0 ]; then
  913. rm "$cache_file"
  914. return 1
  915. fi
  916. }
  917. ##
  918. ## By Reading the metadata.yml, we create a docker-compose.yml mixin.
  919. ## Some metadata.yml (of subordinates) will indeed modify other
  920. ## services than themselves.
  921. _get_docker_compose_service_mixin() {
  922. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  923. links_yaml base_mixin links_yaml docker_compose_options \
  924. charm charm_part
  925. if [ -z "$service" ]; then
  926. print_syntax_error "$FUNCNAME: Please specify a service as first argument."
  927. return 1
  928. fi
  929. if [ -e "$cache_file" ]; then
  930. # debug "$FUNCNAME: cache hit ($*)"
  931. cat "$cache_file"
  932. return 0
  933. fi
  934. master_service=$(get_top_master_service_for_service "$service") || {
  935. err "Failed to get top master service for service $DARKYELLOW$service$NORMAL"
  936. return 1
  937. }
  938. ## The compose part
  939. base_mixin="$master_service:
  940. labels:
  941. - compose.service=$service
  942. - compose.master-service=${master_service}
  943. - compose.project=$(get_default_project_name)"
  944. links_yaml=$(get_docker_compose_links "$service") || return 1
  945. docker_compose_options=$(_get_docker_compose_opts "$service") || return 1
  946. ## the charm part
  947. charm_part=$(get_docker_compose_mixin_from_metadata "$service") || return 1
  948. ## Merge results
  949. if [ "$charm_part" ]; then
  950. charm_yaml="$(yaml_key_val_str "$master_service" "$charm_part")" || return 1
  951. merge_yaml_str "$base_mixin" "$links_yaml" "$charm_yaml" "$docker_compose_options" || return 1
  952. else
  953. merge_yaml_str "$base_mixin" "$links_yaml" "$docker_compose_options" || return 1
  954. fi | tee "$cache_file"
  955. if [ "${PIPESTATUS[0]}" != 0 ]; then
  956. err "Failed to constitute the base YAML for service '${DARKYELLOW}$service${NORMAL}'"
  957. rm "$cache_file"
  958. return 1
  959. fi
  960. }
  961. export -f _get_docker_compose_service_mixin
  962. ##
  963. ## Get full `docker-compose.yml` format for all listed services (and
  964. ## their deps)
  965. ##
  966. ## @export
  967. ## @cache: !system !nofail +stdout
  968. get_docker_compose () {
  969. local cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  970. entries services service start docker_compose_services
  971. if [ -e "$cache_file" ]; then
  972. # debug "$FUNCNAME: cache hit ($*)"
  973. cat "$cache_file"
  974. return 0
  975. fi
  976. ##
  977. ## Adding sub services configurations
  978. ##
  979. declare -A entries
  980. start_compilation=$SECONDS
  981. debug "Compiling 'docker-compose.yml' base for ${DARKYELLOW}$*$NORMAL..."
  982. for target_service in "$@"; do
  983. start=$SECONDS
  984. services=($(get_ordered_service_dependencies "$target_service")) || {
  985. err "Failed to get dependencies for $DARKYELLOW$target_service$NORMAL"
  986. return 1
  987. }
  988. if [ "$DEBUG" ]; then
  989. debug " $DARKYELLOW$target_service$NORMAL deps:$DARKYELLOW" \
  990. "${services[@]::$((${#services[@]} - 1))}" \
  991. "$NORMAL$GRAY(in $((SECONDS - start))s)$NORMAL"
  992. fi
  993. for service in "${services[@]}"; do
  994. if [ "${entries[$service]}" ]; then
  995. ## Prevent double inclusion of same service if this
  996. ## service is deps of two or more of your
  997. ## requirements.
  998. continue
  999. fi
  1000. ## mark the service as "loaded" as well as it's containers
  1001. ## if this is a subordinate service
  1002. start_service=$SECONDS
  1003. entries[$service]=$(_get_docker_compose_service_mixin "$service") || {
  1004. err "Failed to get service mixin for $DARKYELLOW$service$NORMAL"
  1005. return 1
  1006. }
  1007. debug " Applied $DARKYELLOW$service$NORMAL charm metadata mixins $GRAY(in $((SECONDS - start_service))s)$NORMAL"
  1008. done
  1009. debug " ..finished all mixins for $DARKYELLOW$target_service$NORMAL $GRAY(in $((SECONDS - start))s)$NORMAL"
  1010. done
  1011. docker_compose_services=$(merge_yaml_str "${entries[@]}") || {
  1012. err "Failed to merge YAML services entries together."
  1013. return 1
  1014. }
  1015. base_v2="version: '2.0'"
  1016. merge_yaml_str "$(yaml_key_val_str "services" "$docker_compose_services")" \
  1017. "$base_v2" > "$cache_file" || return 1
  1018. export _current_docker_compose="$(cat "$cache_file")"
  1019. echo "$_current_docker_compose"
  1020. debug " ..compilation of base 'docker-compose.yml' done $GRAY(in $((SECONDS - start_compilation))s)$NORMAL" || true
  1021. # debug " ** ${WHITE}docker-compose.yml${NORMAL}:"
  1022. # debug "$_current_docker_compose"
  1023. }
  1024. export -f get_docker_compose
  1025. _get_compose_service_def_cached () {
  1026. local service="$1" docker_compose="$2" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  1027. if [ -e "$cache_file" ]; then
  1028. #debug "$FUNCNAME: STATIC cache hit"
  1029. cat "$cache_file" &&
  1030. touch "$cache_file" || return 1
  1031. return 0
  1032. fi
  1033. value=$(echo "$docker_compose" | shyaml get-value "${service//./\\.}" 2>/dev/null)
  1034. [ "$value" == None ] && value=""
  1035. if ! echo "$value" | shyaml get-value "charm" >/dev/null 2>&1; then
  1036. if charm.exists "$service"; then
  1037. value=$(merge_yaml <(echo "charm: $service") <(echo "$value")) || {
  1038. err "Can't merge YAML infered 'charm: $service' with base ${DARKYELLOW}$service${NORMAL} YAML definition."
  1039. return 1
  1040. }
  1041. else
  1042. err "No ${WHITE}charm${NORMAL} value for service $DARKYELLOW$service$NORMAL" \
  1043. "in compose, nor same name charm found."
  1044. return 1
  1045. fi
  1046. fi
  1047. echo "$value" | tee "$cache_file" || return 1
  1048. # if [ "${PIPESTATUS[0]}" != 0 ]; then
  1049. # rm "$cache_file"
  1050. # return 1
  1051. # fi
  1052. return 0
  1053. # if [ "${PIPESTATUS[0]}" != 0 -o \! -s "$cache_file" ]; then
  1054. # rm "$cache_file"
  1055. # err "PAS OK $service: $value"
  1056. # return 1
  1057. # fi
  1058. }
  1059. export -f _get_compose_service_def_cached
  1060. ## XXXvlab: a lot to be done to cache the results
  1061. get_compose_service_def () {
  1062. local service="$1" docker_compose cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  1063. result
  1064. if [ -e "$cache_file" ]; then
  1065. #debug "$FUNCNAME: SESSION cache hit"
  1066. cat "$cache_file" || return 1
  1067. return 0
  1068. fi
  1069. [ -z "$service" ] && print_syntax_error "Missing service as first argument."
  1070. docker_compose=$(get_compose_yml_content) || return 1
  1071. result=$(_get_compose_service_def_cached "$service" "$docker_compose") || return 1
  1072. charm=$(echo "$result" | shyaml get-value charm 2>/dev/null) || return 1
  1073. metadata=$(charm.metadata "$charm") || return 1
  1074. if default_options=$(printf "%s" "$metadata" | shyaml -y -q get-value default-options); then
  1075. default_options=$(yaml_key_val_str "options" "$default_options") || return 1
  1076. result=$(merge_yaml_str "$default_options" "$result") || return 1
  1077. fi
  1078. echo "$result" | tee "$cache_file" || return 1
  1079. }
  1080. export -f get_compose_service_def
  1081. _get_service_charm_cached () {
  1082. local service="$1" service_def="$2" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  1083. if [ -e "$cache_file" ]; then
  1084. # debug "$FUNCNAME: cache hit $1"
  1085. cat "$cache_file" &&
  1086. touch "$cache_file" || return 1
  1087. return 0
  1088. fi
  1089. charm=$(echo "$service_def" | shyaml get-value charm 2>/dev/null)
  1090. if [ -z "$charm" ]; then
  1091. err "Missing ${WHITE}charm${NORMAL} value in service $DARKYELLOW$service$NORMAL definition."
  1092. return 1
  1093. fi
  1094. echo "$charm" | tee "$cache_file" || return 1
  1095. }
  1096. export -f _get_service_charm_cached
  1097. get_service_charm () {
  1098. local service="$1"
  1099. if [ -z "$service" ]; then
  1100. echo ${FUNCNAME[@]} >&2
  1101. print_syntax_error "$FUNCNAME: Please specify a service as first argument."
  1102. return 1
  1103. fi
  1104. service_def=$(get_compose_service_def "$service") || return 1
  1105. _get_service_charm_cached "$service" "$service_def"
  1106. }
  1107. export -f get_service_charm
  1108. ## built above the docker-compose abstraction, so it relies on the
  1109. ## full docker-compose.yml to be already built.
  1110. get_service_def () {
  1111. local service="$1" def
  1112. if [ -z "$_current_docker_compose" ]; then
  1113. print_syntax_error "$FUNCNAME is meant to be called after"\
  1114. "\$_current_docker_compose has been calculated."
  1115. fi
  1116. def=$(echo "$_current_docker_compose" | shyaml get-value "services.${service//./\\.}" 2>/dev/null)
  1117. if [ -z "$def" ]; then
  1118. err "No definition for service $DARKYELLOW$service$NORMAL in compiled 'docker-compose.yml'."
  1119. return 1
  1120. fi
  1121. echo "$def"
  1122. }
  1123. export -f get_service_def
  1124. ## Return the base docker image name of a service
  1125. service_base_docker_image() {
  1126. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  1127. master_service service_def service_image service_build service_dockerfile
  1128. if [ -e "$cache_file" ]; then
  1129. # debug "$FUNCNAME: cache hit ($*)"
  1130. cat "$cache_file"
  1131. return 0
  1132. fi
  1133. master_service="$(get_top_master_service_for_service "$service")" || {
  1134. err "Could not compute master service for service $DARKYELLOW$service$NORMAL."
  1135. return 1
  1136. }
  1137. service_def="$(get_service_def "$master_service")" || {
  1138. err "Could not get docker-compose service definition for $DARKYELLOW$master_service$NORMAL."
  1139. return 1
  1140. }
  1141. service_image=$(echo "$service_def" | shyaml get-value image 2>/dev/null)
  1142. if [ "$?" != 0 ]; then
  1143. ## According to https://stackoverflow.com/questions/32230577 , if there's a build,
  1144. ## then the builded image will get name ${project}_${service}
  1145. project=$(get_default_project_name) || return 1
  1146. image_name="${project}_${service}"
  1147. if ! docker_has_image "$image_name"; then
  1148. service_build=$(echo "$service_def" | shyaml get-value build 2>/dev/null)
  1149. if [ "$?" != 0 ]; then
  1150. err "Service $DARKYELLOW$service$NORMAL has no ${WHITE}image${NORMAL} nor ${WHITE}build${NORMAL} parameter."
  1151. echo "$service_def" >&2
  1152. return 1
  1153. fi
  1154. docker build "$service_build" -t "${project}_${service}" >&2 || {
  1155. err "Failed to build image for ${DARKYELLOW}$service${NORMAL}."
  1156. return 1
  1157. }
  1158. fi
  1159. printf "%s" "${project}_${service}"
  1160. else
  1161. printf "%s" "${service_image}"
  1162. fi | tee "$cache_file"
  1163. if [ "${PIPESTATUS[0]}" != 0 ]; then
  1164. rm "$cache_file"
  1165. return 1
  1166. fi
  1167. }
  1168. export -f service_base_docker_image
  1169. get_charm_relation_def () {
  1170. local charm="$1" relation_name="$2" cache_file="$state_tmpdir/$FUNCNAME.cache.$1.$2" \
  1171. relation_def metadata
  1172. if [ -e "$cache_file" ]; then
  1173. # debug "$FUNCNAME: cache hit ($*)"
  1174. cat "$cache_file"
  1175. return 0
  1176. fi
  1177. metadata="$(charm.metadata "$charm")" || return 1
  1178. relation_def="$(echo "$metadata" | shyaml get-value "provides.${relation_name}" 2>/dev/null)"
  1179. echo "$relation_def" | tee "$cache_file"
  1180. }
  1181. export -f get_charm_relation_def
  1182. get_charm_tech_dep_orientation_for_relation() {
  1183. local charm="$1" relation_name="$2" cache_file="$state_tmpdir/$FUNCNAME.cache.$1.$2" \
  1184. relation_def value
  1185. if [ -e "$cache_file" ]; then
  1186. # debug "$FUNCNAME: cache hit ($*)"
  1187. cat "$cache_file"
  1188. return 0
  1189. fi
  1190. relation_def=$(get_charm_relation_def "$charm" "$relation_name" 2>/dev/null)
  1191. value=$(echo "$relation_def" | shyaml get-value 'tech-dep' 2>/dev/null)
  1192. value=${value:-True}
  1193. printf "%s" "$value" | tee "$cache_file"
  1194. }
  1195. export -f get_charm_tech_dep_orientation_for_relation
  1196. get_service_relation_tech_dep() {
  1197. local service="$1" relation_name="$2" cache_file="$state_tmpdir/$FUNCNAME.cache.$1.$2" \
  1198. charm tech_dep
  1199. if [ -e "$cache_file" ]; then
  1200. # debug "$FUNCNAME: cache hit ($*)"
  1201. cat "$cache_file"
  1202. return 0
  1203. fi
  1204. charm=$(get_service_charm "$service") || return 1
  1205. tech_dep="$(get_charm_tech_dep_orientation_for_relation "$charm" "$relation_name")" || return 1
  1206. printf "%s" "$tech_dep" | tee "$cache_file"
  1207. }
  1208. export -f get_service_relation_tech_dep
  1209. ##
  1210. ## Use compose file to get deps, and relation definition in metadata.yml
  1211. ## for tech-dep attribute.
  1212. get_service_deps() {
  1213. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$(H "$@" "$ALL_RELATIONS")"
  1214. if [ -e "$cache_file" ]; then
  1215. # debug "$FUNCNAME: cache hit ($*)"
  1216. cat "$cache_file"
  1217. return 0
  1218. fi
  1219. (
  1220. set -o pipefail
  1221. get_service_relations "$service" | \
  1222. while read-0 relation_name target_service _relation_config tech_dep; do
  1223. echo "$target_service"
  1224. done | tee "$cache_file"
  1225. ) || return 1
  1226. }
  1227. export -f get_service_deps
  1228. ## XXXvlab: cache was disabled because improper. Indeed, this needs to cache
  1229. ## 'depths' full state. Second, it should be
  1230. _rec_get_depth() {
  1231. local elt=$1 dep deps max
  1232. [ "${depths[$elt]}" ] && return 0
  1233. local cache_file="$state_tmpdir/$FUNCNAME.cache.$(H "$@" "$(declare -pA depths)" "$ALL_RELATIONS")"
  1234. if [ -e "$cache_file.depths" ]; then
  1235. #debug "$FUNCNAME: cache hit ($*) - $cache_file.depths"
  1236. while read-0 k v; do
  1237. depths["$k"]="$v"
  1238. done < "$cache_file.depths"
  1239. while read-0 k v; do
  1240. visited["$k"]="$v"
  1241. done < "$cache_file.visited"
  1242. return 0
  1243. fi
  1244. visited[$elt]=1
  1245. #debug "Setting visited[$elt]"
  1246. #debug "Asking for $DARKYELLOW$elt$NORMAL dependencies"
  1247. deps=$(get_service_deps "$elt") || {
  1248. debug "Failed get_service_deps $elt"
  1249. return 1
  1250. }
  1251. # debug "$elt deps are:" $deps
  1252. max=0
  1253. for dep in $deps; do
  1254. [ "${visited[$dep]}" ] && {
  1255. #debug "Already computing $dep"
  1256. continue
  1257. }
  1258. _rec_get_depth "$dep" || return 1
  1259. #debug "Requesting depth[$dep]"
  1260. if (( ${depths[$dep]} > max )); then
  1261. max="${depths[$dep]}"
  1262. fi
  1263. done
  1264. # debug "Setting depth[$elt] to $((max + 1))"
  1265. depths[$elt]=$((max + 1))
  1266. array_kv_to_stdin depths > "$cache_file.depths"
  1267. array_kv_to_stdin visited > "$cache_file.visited"
  1268. # debug "DEPTHS: $(declare -pA depths)"
  1269. # debug "$FUNCNAME: caching hit ($*) - $cache_file"
  1270. }
  1271. export -f _rec_get_depth
  1272. get_ordered_service_dependencies() {
  1273. local services=("$@") cache_file="$state_tmpdir/$FUNCNAME.cache.$(H "$@" "$ALL_RELATIONS")" \
  1274. i value key heads depths visited
  1275. if [ -e "$cache_file" ]; then
  1276. # debug "$FUNCNAME: cache hit ($*)"
  1277. cat "$cache_file"
  1278. return 0
  1279. fi
  1280. #debug "Figuring ordered deps of $DARKYELLOW${services[@]}$NORMAL"
  1281. if [ -z "${services[*]}" ]; then
  1282. return 0
  1283. # print_syntax_error "$FUNCNAME: no arguments"
  1284. # return 1
  1285. fi
  1286. declare -A depths
  1287. declare -A visited
  1288. heads=("${services[@]}")
  1289. while [ "${#heads[@]}" != 0 ]; do
  1290. array_pop heads head
  1291. _rec_get_depth "$head" || return 1
  1292. done
  1293. i=0
  1294. while [ "${#depths[@]}" != 0 ]; do
  1295. for key in "${!depths[@]}"; do
  1296. value="${depths[$key]}"
  1297. if [ "$value" == "$i" ]; then
  1298. echo "$key"
  1299. unset depths[$key]
  1300. fi
  1301. done
  1302. ((i++))
  1303. done | tee "$cache_file"
  1304. }
  1305. export -f get_ordered_service_dependencies
  1306. run_service_hook () {
  1307. local action="$1" service subservice subservices loaded
  1308. shift
  1309. declare -A loaded
  1310. for service in "$@"; do
  1311. subservices=$(get_ordered_service_dependencies "$service") || return 1
  1312. for subservice in $subservices; do
  1313. if [ "${loaded[$subservice]}" ]; then
  1314. ## Prevent double inclusion of same service if this
  1315. ## service is deps of two or more of your
  1316. ## requirements.
  1317. continue
  1318. fi
  1319. charm=$(get_service_charm "$subservice") || return 1
  1320. charm.has_hook "$charm" "$action" >/dev/null || continue
  1321. PROJECT_NAME=$(get_default_project_name) || return 1
  1322. MASTER_BASE_SERVICE_NAME=$(get_top_master_service_for_service "$subservice") || return 1
  1323. MASTER_BASE_CHARM_NAME=$(get_service_charm "$MASTER_BASE_SERVICE_NAME") || return 1
  1324. DOCKER_BASE_IMAGE=$(service_base_docker_image "$MASTER_BASE_SERVICE_NAME") || return 1
  1325. Wrap "${wrap_opts[@]}" -d "running $YELLOW$action$NORMAL hook of $DARKYELLOW$subservice$NORMAL in charm $DARKPINK$charm$NORMAL" <<EOF || return 1
  1326. export DOCKER_BASE_IMAGE="$DOCKER_BASE_IMAGE"
  1327. export SERVICE_NAME=$subservice
  1328. export IMAGE_NAME=$(echo "${PROJECT_NAME}" | tr -d "_-")_\${SERVICE_NAME}
  1329. export CONTAINER_NAME=\${IMAGE_NAME}_1
  1330. export CHARM_NAME="$charm"
  1331. export PROJECT_NAME="$PROJECT_NAME"
  1332. export SERVICE_DATASTORE="$DATASTORE/$subservice"
  1333. export SERVICE_CONFIGSTORE="$CONFIGSTORE/$subservice"
  1334. export MASTER_BASE_SERVICE_NAME="$MASTER_BASE_SERVICE_NAME"
  1335. export MASTER_BASE_CHARM_NAME="$MASTER_BASE_CHARM_NAME"
  1336. charm.run_hook "local" "$charm" "$action"
  1337. EOF
  1338. loaded[$subservice]=1
  1339. done
  1340. done
  1341. return 0
  1342. }
  1343. host_resource_get() {
  1344. local location="$1" cfg="$2"
  1345. type=$(echo "$cfg" | shyaml get-value type 2>/dev/null) || {
  1346. err "Missing ${WHITE}type$NORMAL option in ${WHITE}get$NORMAL config for location '$location'"
  1347. return 1
  1348. }
  1349. if fn.exists host_resource_get_$type; then
  1350. host_resource_get_$type "$location" "$cfg"
  1351. else
  1352. err "Source ${WHITE}source$NORMAL type '$type' unknown for" \
  1353. "${WHITE}host-resource$NORMAL '$location' defined in" \
  1354. "$DARKYELLOW$subservice$NORMAL config."
  1355. return 1
  1356. fi
  1357. }
  1358. export -f host_resource_get
  1359. host_resource_get_git() {
  1360. local location="$1" cfg="$2" branch parent url
  1361. branch=$(echo "$cfg" | shyaml get-value branch 2>/dev/null)
  1362. branch=${branch:-master}
  1363. url=$(echo "$cfg" | shyaml get-value url 2>/dev/null)
  1364. parent="$(dirname "$location")"
  1365. (
  1366. mkdir -p "$parent" && cd "$parent" &&
  1367. git clone -b "$branch" "$url" "$(basename "$location")"
  1368. ) || return 1
  1369. }
  1370. export -f host_resource_get_git
  1371. host_resource_get_git-sub() {
  1372. local location="$1" cfg="$2" branch parent url
  1373. branch=$(echo "$cfg" | shyaml get-value branch 2>/dev/null)
  1374. branch=${branch:-master}
  1375. url=$(echo "$cfg" | shyaml get-value url 2>/dev/null)
  1376. parent="$(dirname "$location")"
  1377. (
  1378. mkdir -p "$parent" && cd "$parent" &&
  1379. git sub clone -b "$branch" "$url" "$(basename "$location")"
  1380. ) || return 1
  1381. }
  1382. export -f host_resource_get_git-sub
  1383. setup_host_resource () {
  1384. local subservice="$1" service_def location get cfg
  1385. service_def=$(get_compose_service_def "$subservice") || return 1
  1386. while read-0 location cfg; do
  1387. ## XXXvlab: will it be a git resources always ?
  1388. if [ -d "$location" -a ! -d "$location/.git" ]; then
  1389. err "Hum, location '$location' does not seem to be a git directory."
  1390. return 1
  1391. fi
  1392. if [ -d "$location" ]; then
  1393. info "host resource '$location' already set up."
  1394. continue
  1395. fi
  1396. get=$(echo "$cfg" | shyaml get-value get 2>/dev/null)
  1397. if [ -z "$get" ]; then
  1398. err "No host directory '$location' found, and no ${WHITE}source$NORMAL" \
  1399. "specified for $DARKYELLOW$subservice$NORMAL."
  1400. return 1
  1401. fi
  1402. host_resource_get "$location" "$get" || return 1
  1403. done < <(echo "$service_def" | shyaml key-values-0 host-resources 2>/dev/null)
  1404. }
  1405. export -f setup_host_resource
  1406. setup_host_resources () {
  1407. local service subservices subservice loaded
  1408. declare -A loaded
  1409. for service in "$@"; do
  1410. subservices=$(get_ordered_service_dependencies "$service") || return 1
  1411. for subservice in $subservices; do
  1412. if [ "${loaded[$subservice]}" ]; then
  1413. ## Prevent double inclusion of same service if this
  1414. ## service is deps of two or more of your
  1415. ## requirements.
  1416. continue
  1417. fi
  1418. setup_host_resource "$subservice" || return 1
  1419. loaded[$subservice]=1
  1420. done
  1421. done
  1422. return 0
  1423. }
  1424. export -f setup_host_resources
  1425. ## Works on stdin
  1426. cfg-get-value () {
  1427. local key="$1" out
  1428. if [ -z "$key" ]; then
  1429. yaml_get_interpret || return 1
  1430. return 0
  1431. fi
  1432. if ! out=$(shyaml -y get-value "$key" 2>/dev/null); then
  1433. err "The key $WHITE$key$NORMAL was not found in relation's data."
  1434. return 1
  1435. fi
  1436. echo "$out" | yaml_get_interpret
  1437. }
  1438. export -f cfg-get-value
  1439. relation-get () {
  1440. cfg-get-value "$1" < "$RELATION_DATA_FILE"
  1441. }
  1442. export -f relation-get
  1443. expand_vars() {
  1444. local unlikely_prefix="UNLIKELY_PREFIX"
  1445. content=$(cat -)
  1446. ## find first identifier not in content
  1447. remaining_lines=$(echo "$content" | grep "^$unlikely_prefix")
  1448. while [ "$(echo "$remaining_lines" | grep "^$unlikely_prefix$")" ]; do
  1449. size_prefix="${#unlikely_prefix}"
  1450. first_matching=$(echo "$remaining_lines" |
  1451. grep -v "^$unlikely_prefix$" |
  1452. uniq -w "$((size_prefix + 1))" -c |
  1453. sort -rn |
  1454. head -n 1)
  1455. first_matching=${first_matching#"${x%%[![:space:]]*}"}
  1456. first_matching="${first_matching#* }"
  1457. next_char=${first_matching:$size_prefix:1}
  1458. if [ "$next_char" != "0" ]; then
  1459. unlikely_prefix+="0"
  1460. else
  1461. unlikely_prefix+="1"
  1462. fi
  1463. remaining_lines=$(echo "$remaining_lines" | grep "^$unlikely_prefix")
  1464. done
  1465. eval "cat <<$unlikely_prefix
  1466. $content
  1467. $unlikely_prefix"
  1468. }
  1469. export -f expand_vars
  1470. yaml_get_interpret() {
  1471. local content tag
  1472. content=$(cat -)
  1473. tag=$(echo "$content" | shyaml -y get-value) || return 1
  1474. tag="${tag%% *}"
  1475. content=$(echo "$content" | shyaml get-value) || return 1
  1476. if ! [ "${tag:0:1}" == "!" ]; then
  1477. echo "$content" || return 1
  1478. return 0
  1479. fi
  1480. case "$tag" in
  1481. "!bash-stdout")
  1482. echo "$content" | bash || {
  1483. err "shell code didn't end with errorlevel 0"
  1484. return 1
  1485. }
  1486. ;;
  1487. "!var-expand")
  1488. echo "$content" | expand_vars || {
  1489. err "shell expansion failed"
  1490. return 1
  1491. }
  1492. ;;
  1493. "!file-content")
  1494. source=$(echo "$content" | expand_vars) || {
  1495. err "shell expansion failed"
  1496. return 1
  1497. }
  1498. cat "$source" || return 1
  1499. ;;
  1500. *)
  1501. err "Invalid object tag ${WHITE}$tag${NORMAL}"
  1502. return 1
  1503. ;;
  1504. esac
  1505. }
  1506. export -f yaml_get_interpret
  1507. options-get () {
  1508. local key="$1" out
  1509. service_def=$(get_compose_service_def "$SERVICE_NAME") || return 1
  1510. if ! out=$(echo "$service_def" | shyaml -y get-value "options.$key" 2>/dev/null); then
  1511. err "The key $WHITE$key$NORMAL was not found in base service compose definition.."
  1512. return 1
  1513. fi
  1514. echo "$out" | yaml_get_interpret
  1515. }
  1516. export -f options-get
  1517. relation-base-compose-get () {
  1518. local key="$1" out
  1519. if ! out=$(echo "$RELATION_BASE_COMPOSE_DEF" | shyaml -y get-value "options.$key" 2>/dev/null); then
  1520. err "The key $WHITE$key$NORMAL was not found in base service compose definition.."
  1521. return 1
  1522. fi
  1523. echo "$out" | yaml_get_interpret
  1524. }
  1525. export -f relation-base-compose-get
  1526. relation-target-compose-get () {
  1527. local key="$1" out
  1528. if ! out=$(echo "$RELATION_TARGET_COMPOSE_DEF" | shyaml -y get-value "options.$key" 2>/dev/null); then
  1529. err "The key $WHITE$key$NORMAL was not found in base service compose definition.."
  1530. return 1
  1531. fi
  1532. echo "$out" | yaml_get_interpret
  1533. }
  1534. export -f relation-target-compose-get
  1535. relation-set () {
  1536. local key="$1" value="$2"
  1537. if [ -z "$RELATION_DATA_FILE" ]; then
  1538. err "$FUNCNAME: relation does not seems to be correctly setup."
  1539. return 1
  1540. fi
  1541. if ! [ -r "$RELATION_DATA_FILE" ]; then
  1542. err "$FUNCNAME: can't read relation's data." >&2
  1543. return 1
  1544. fi
  1545. _config_merge "$RELATION_DATA_FILE" <(yaml_key_val_str "$key" "$value")
  1546. }
  1547. export -f relation-set
  1548. _config_merge() {
  1549. local config_filename="$1" mixin="$2"
  1550. touch "$config_filename" &&
  1551. merge_yaml "$config_filename" "$mixin" > "$config_filename.tmp" || return 1
  1552. mv "$config_filename.tmp" "$config_filename"
  1553. }
  1554. export -f _config_merge
  1555. ## XXXvlab; this can be used only in relation, I'd like to use it in init.
  1556. config-add() {
  1557. local metadata="$1"
  1558. _config_merge "$RELATION_CONFIG" <(echo "$metadata")
  1559. }
  1560. export -f config-add
  1561. ## XXXvlab; this can be used only in relation, I'd like to use it in init.
  1562. init-config-add() {
  1563. local metadata="$1"
  1564. _config_merge "$state_tmpdir/to-merge-in-docker-compose.yml" \
  1565. <(yaml_key_val_str "services" "$metadata")
  1566. }
  1567. export -f init-config-add
  1568. docker_get_uid() {
  1569. local service="$1" user="$2" uid
  1570. uid=$(cached_cmd_on_base_image "$service" "id -u \"$user\"") || {
  1571. debug "Failed to query for '$user' uid in ${DARKYELLOW}$service${NORMAL} base image."
  1572. return 1
  1573. }
  1574. info "uid from ${DARKYELLOW}$service${NORMAL} for user '$user' is '$uid'"
  1575. echo "$uid"
  1576. }
  1577. export -f docker_get_uid
  1578. docker_get_uid_gid() {
  1579. local service="$1" user="$2" group="$3" uid
  1580. uid_gid=$(cached_cmd_on_base_image "$service" "id -u \"$user\"; id -g \"$group\"") || {
  1581. debug "Failed to query for '$user' uid in ${DARKYELLOW}$service${NORMAL} base image."
  1582. return 1
  1583. }
  1584. info "uid from ${DARKYELLOW}$service${NORMAL} for user '$user' is '$uid_gid'"
  1585. echo "$uid_gid"
  1586. }
  1587. export -f docker_get_uid_gid
  1588. logstdout() {
  1589. local name="$1"
  1590. sed -r 's%^%'"${name}"'> %g'
  1591. }
  1592. export -f logstdout
  1593. logstderr() {
  1594. local name="$1"
  1595. sed -r 's%^(.*)$%'"${RED}${name}>${NORMAL} \1"'%g'
  1596. }
  1597. export -f logstderr
  1598. _run_service_relation () {
  1599. local relation_name="$1" service="$2" target_service="$3" relation_config="$4" relation_dir services
  1600. local errlvl
  1601. charm=$(get_service_charm "$service") || return 1
  1602. target_charm=$(get_service_charm "$target_service") || return 1
  1603. base_script_name=$(charm.has_relation_hook "$charm" "$relation_name" relation-joined) || true
  1604. target_script_name=$(charm.has_relation_hook "$target_charm" "$relation_name" relation-joined) || true
  1605. [ "$base_script_name" -o "$target_script_name" ] || return 0
  1606. relation_dir=$(get_relation_data_dir "$service" "$target_service" "$relation_name") || return 1
  1607. RELATION_DATA_FILE=$(get_relation_data_file "$service" "$target_service" "$relation_name" "$relation_config") || return 1
  1608. export BASE_SERVICE_NAME=$service
  1609. export BASE_CHARM_NAME=$charm
  1610. export BASE_CHARM_PATH=$(charm.get_dir "$charm")
  1611. export TARGET_SERVICE_NAME=$target_service
  1612. export TARGET_CHARM_NAME=$target_charm
  1613. export TARGET_CHARM_PATH=$(charm.get_dir "$target_charm")
  1614. export RELATION_DATA_FILE
  1615. target_errlvl=0
  1616. if [ -z "$target_script_name" ]; then
  1617. verb "No relation script $DARKBLUE$relation_name$NORMAL in target $DARKPINK$target_charm$NORMAL."
  1618. else
  1619. verb "Running ${DARKBLUE}$relation_name${NORMAL} relation-joined script" \
  1620. "for target $DARKYELLOW$target_service$NORMAL (charm $DARKPINK$target_charm$NORMAL)"
  1621. RELATION_CONFIG="$relation_dir/config_provider"
  1622. DOCKER_BASE_IMAGE=$(service_base_docker_image "$target_service") || return 1
  1623. export DOCKER_BASE_IMAGE RELATION_CONFIG RELATION_DATA
  1624. {
  1625. (
  1626. SERVICE_NAME=$target_service
  1627. SERVICE_DATASTORE="$DATASTORE/$target_service"
  1628. SERVICE_CONFIGSTORE="$CONFIGSTORE/$target_service"
  1629. export SERVICE_NAME DOCKER_BASE_IMAGE SERVICE_DATASTORE SERVICE_CONFIGSTORE
  1630. charm.run_relation_hook local "$target_charm" "$relation_name" relation-joined
  1631. echo "$?" > "$relation_dir/target_errlvl"
  1632. ) | logstdout "$DARKYELLOW$target_service$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${GREEN}@${NORMAL}"
  1633. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$target_service$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${RED}@${NORMAL}" 3>&1 1>&2 2>&3
  1634. target_errlvl="$(cat "$relation_dir/target_errlvl")" || {
  1635. err "Relation script '$script_name' in $DARKPINK$target_charm$NORMAL" \
  1636. "failed before outputing an errorlevel."
  1637. ((target_errlvl |= "1" ))
  1638. }
  1639. if [ -e "$RELATION_CONFIG" ]; then
  1640. debug "Merging some new config info in $DARKYELLOW$target_service$NORMAL"
  1641. _config_merge "$state_tmpdir/to-merge-in-docker-compose.yml" "$RELATION_CONFIG" &&
  1642. rm "$RELATION_CONFIG"
  1643. ((target_errlvl |= "$?"))
  1644. fi
  1645. fi
  1646. if [ "$target_errlvl" == 0 ]; then
  1647. errlvl=0
  1648. if [ "$base_script_name" ]; then
  1649. verb "Running ${DARKBLUE}$relation_name${NORMAL} relation-joined script" \
  1650. "for $DARKYELLOW$service$NORMAL (charm $DARKPINK$charm$NORMAL)"
  1651. RELATION_CONFIG="$relation_dir/config_providee"
  1652. RELATION_DATA="$(cat "$RELATION_DATA_FILE")"
  1653. DOCKER_BASE_IMAGE=$(service_base_docker_image "$service") || return 1
  1654. export DOCKER_BASE_IMAGE RELATION_CONFIG RELATION_DATA
  1655. {
  1656. (
  1657. SERVICE_NAME=$service
  1658. SERVICE_DATASTORE="$DATASTORE/$service"
  1659. SERVICE_CONFIGSTORE="$CONFIGSTORE/$service"
  1660. export SERVICE_NAME DOCKER_BASE_IMAGE SERVICE_DATASTORE SERVICE_CONFIGSTORE
  1661. charm.run_relation_hook local "$charm" "$relation_name" relation-joined
  1662. echo "$?" > "$relation_dir/errlvl"
  1663. ) | logstdout "$DARKYELLOW$service$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${GREEN}@${NORMAL}"
  1664. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$service$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${RED}@$NORMAL" 3>&1 1>&2 2>&3
  1665. errlvl="$(cat "$relation_dir/errlvl")" || {
  1666. err "Relation script '$script_name' in $DARKPINK$charm$NORMAL" \
  1667. "failed before outputing an errorlevel."
  1668. ((errlvl |= "1" ))
  1669. }
  1670. if [ -e "$RELATION_CONFIG" ]; then
  1671. _config_merge "$state_tmpdir/to-merge-in-docker-compose.yml" "$RELATION_CONFIG" &&
  1672. rm "$RELATION_CONFIG"
  1673. ((errlvl |= "$?" ))
  1674. fi
  1675. if [ "$errlvl" != 0 ]; then
  1676. err "Relation $DARKBLUE$relation_name$NORMAL on $DARKYELLOW$service$NORMAL failed to run properly."
  1677. fi
  1678. else
  1679. verb "No relation script '$script_name' in charm $DARKPINK$charm$NORMAL. Ignoring."
  1680. fi
  1681. else
  1682. err "Relation $DARKBLUE$relation_name$NORMAL on $DARKYELLOW$target_service$NORMAL failed to run properly."
  1683. fi
  1684. if [ "$target_errlvl" == 0 -a "$errlvl" == 0 ]; then
  1685. debug "Relation $DARKBLUE$relation_name$NORMAL is established" \
  1686. "between $DARKYELLOW$service$NORMAL and $DARKYELLOW$target_service$NORMAL."
  1687. return 0
  1688. else
  1689. return 1
  1690. fi
  1691. }
  1692. export -f _run_service_relation
  1693. _get_compose_relations_cached () {
  1694. local compose_service_def="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  1695. relation_name relation_def target_service
  1696. if [ -e "$cache_file" ]; then
  1697. #debug "$FUNCNAME: STATIC cache hit $1"
  1698. cat "$cache_file" &&
  1699. touch "$cache_file" || return 1
  1700. return 0
  1701. fi
  1702. (
  1703. set -o pipefail
  1704. if [ "$compose_service_def" ]; then
  1705. while read-0 relation_name relation_def; do
  1706. ## XXXvlab: could we use braces here instead of parenthesis ?
  1707. (
  1708. case "$(echo "$relation_def" | shyaml get-type 2>/dev/null)" in
  1709. "str")
  1710. target_service="$(echo "$relation_def" | shyaml get-value 2>/dev/null)" || return 1
  1711. tech_dep="$(get_service_relation_tech_dep "$target_service" "$relation_name")" || return 1
  1712. printf "%s\0" "$relation_name" "$target_service" "" "$tech_dep"
  1713. ;;
  1714. "sequence")
  1715. while read-0 target_service; do
  1716. tech_dep="$(get_service_relation_tech_dep "$target_service" "$relation_name")" || return 1
  1717. printf "%s\0" "$relation_name" "$target_service" "" "$tech_dep"
  1718. done < <(echo "$relation_def" | shyaml get-values-0 2>/dev/null)
  1719. ;;
  1720. "struct")
  1721. while read-0 target_service relation_config; do
  1722. tech_dep="$(get_service_relation_tech_dep "$target_service" "$relation_name")" || return 1
  1723. printf "%s\0" "$relation_name" "$target_service" "$relation_config" "$tech_dep"
  1724. done < <(echo "$relation_def" | shyaml key-values-0 2>/dev/null)
  1725. ;;
  1726. esac
  1727. ) </dev/null >> "$cache_file" || return 1
  1728. done < <(echo "$compose_service_def" | shyaml key-values-0 relations 2>/dev/null)
  1729. fi
  1730. )
  1731. if [ "$?" != 0 ]; then
  1732. err "Error while looking for compose relations."
  1733. rm -f "$cache_file" ## no cache
  1734. return 1
  1735. fi
  1736. [ -e "$cache_file" ] && cat "$cache_file"
  1737. return 0
  1738. }
  1739. export -f _get_compose_relations_cached
  1740. get_compose_relations () {
  1741. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  1742. compose_def
  1743. if [ -e "$cache_file" ]; then
  1744. #debug "$FUNCNAME: SESSION cache hit $1"
  1745. cat "$cache_file"
  1746. return 0
  1747. fi
  1748. compose_def="$(get_compose_service_def "$service")" || return 1
  1749. _get_compose_relations_cached "$compose_def" > "$cache_file"
  1750. if [ "$?" != 0 ]; then
  1751. rm -f "$cache_file" ## no cache
  1752. return 1
  1753. fi
  1754. cat "$cache_file"
  1755. }
  1756. export -f get_compose_relations
  1757. get_service_relations () {
  1758. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$(H "$@" "$ALL_RELATIONS")" \
  1759. s rn ts rc td
  1760. if [ -e "$cache_file" ]; then
  1761. #debug "$FUNCNAME: SESSION cache hit $1"
  1762. cat "$cache_file"
  1763. return 0
  1764. fi
  1765. if [ -z "$ALL_RELATIONS" ]; then
  1766. err "Can't access global \$ALL_RELATIONS"
  1767. return 1
  1768. fi
  1769. while read-0 s rn ts rc td; do
  1770. [[ "$s" == "$service" ]] || continue
  1771. printf "%s\0" "$rn" "$ts" "$rc" "$td"
  1772. done < <(cat "$ALL_RELATIONS") > "$cache_file"
  1773. if [ "$?" != 0 ]; then
  1774. rm -f "$cache_file" ## no cache
  1775. return 1
  1776. fi
  1777. cat "$cache_file"
  1778. }
  1779. export -f get_service_relations
  1780. get_service_relation() {
  1781. local service="$1" relation="$2" cache_file="$state_tmpdir/$FUNCNAME.cache.$1.$2" \
  1782. rn ts rc td
  1783. if [ -e "$cache_file" ]; then
  1784. #debug "$FUNCNAME: SESSION cache hit $1"
  1785. cat "$cache_file"
  1786. return 0
  1787. fi
  1788. while read-0 rn ts rc td; do
  1789. [ "$relation" == "$rn" ] && {
  1790. printf "%s\0" "$ts" "$rc" "$td"
  1791. break
  1792. }
  1793. done < <(get_service_relations "$service") > "$cache_file"
  1794. if [ "$?" != 0 ]; then
  1795. rm -f "$cache_file" ## no cache
  1796. return 1
  1797. fi
  1798. cat "$cache_file"
  1799. }
  1800. export -f get_service_relation
  1801. _get_charm_metadata_uses() {
  1802. local metadata="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$(printf "%s\0" "$@" | md5_compat)"
  1803. if [ -e "$cache_file" ]; then
  1804. #debug "$FUNCNAME: SESSION cache hit $1"
  1805. cat "$cache_file" || return 1
  1806. return 0
  1807. fi
  1808. printf "%s" "$metadata" | { shyaml key-values-0 uses 2>/dev/null || true; } | tee "$cache_file"
  1809. }
  1810. export -f _get_charm_metadata_uses
  1811. _get_service_metadata() {
  1812. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  1813. charm
  1814. if [ -e "$cache_file" ]; then
  1815. #debug "$FUNCNAME: SESSION cache hit $1"
  1816. cat "$cache_file"
  1817. return 0
  1818. fi
  1819. charm="$(get_service_charm "$service")" || return 1
  1820. charm.metadata "$charm" > "$cache_file"
  1821. if [ "$?" != 0 ]; then
  1822. rm -f "$cache_file" ## no cache
  1823. return 1
  1824. fi
  1825. cat "$cache_file"
  1826. }
  1827. export -f _get_service_metadata
  1828. _get_service_uses() {
  1829. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  1830. metadata
  1831. if [ -e "$cache_file" ]; then
  1832. #debug "$FUNCNAME: SESSION cache hit $1"
  1833. cat "$cache_file"
  1834. return 0
  1835. fi
  1836. metadata="$(_get_service_metadata "$service")" || return 1
  1837. _get_charm_metadata_uses "$metadata" > "$cache_file"
  1838. if [ "$?" != 0 ]; then
  1839. rm -f "$cache_file" ## no cache
  1840. return 1
  1841. fi
  1842. cat "$cache_file"
  1843. }
  1844. export -f _get_service_uses
  1845. _get_services_uses() {
  1846. local cache_file="$state_tmpdir/$FUNCNAME.cache.$(printf "%s\0" "$@" | md5_compat)" \
  1847. service rn rd
  1848. if [ -e "$cache_file" ]; then
  1849. #debug "$FUNCNAME: SESSION cache hit $1"
  1850. cat "$cache_file"
  1851. return 0
  1852. fi
  1853. for service in "$@"; do
  1854. _get_service_uses "$service" | while read-0 rn rd; do
  1855. printf "%s\0" "$service" "$rn" "$rd"
  1856. done
  1857. [ "${PIPESTATUS[0]}" == 0 ] || {
  1858. return 1
  1859. }
  1860. done > "${cache_file}.wip"
  1861. mv "${cache_file}"{.wip,} &&
  1862. cat "$cache_file" || return 1
  1863. }
  1864. export -f _get_services_uses
  1865. _get_provides_provides() {
  1866. local provides="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$(printf "%s\0" "$@" | md5_compat)" \
  1867. service rn rd
  1868. if [ -e "$cache_file" ]; then
  1869. # debug "$FUNCNAME: CACHEDIR cache hit $1"
  1870. cat "$cache_file"
  1871. return 0
  1872. fi
  1873. type=$(printf "%s" "$provides" | shyaml get-type)
  1874. case "$type" in
  1875. sequence)
  1876. while read-0 prov; do
  1877. printf "%s\0" "$prov" ""
  1878. done < <(echo "$provides" | shyaml get-values-0)
  1879. ;;
  1880. struct)
  1881. printf "%s" "$provides" | shyaml key-values-0
  1882. ;;
  1883. str)
  1884. printf "%s\0" "$(echo "$provides" | shyaml get-value)" ""
  1885. ;;
  1886. *)
  1887. err "Unexpected type '$type' for provider identifier in charm '$charm'."
  1888. return 1
  1889. esac | tee "$cache_file"
  1890. return "${PIPESTATUS[0]}"
  1891. }
  1892. _get_metadata_provides() {
  1893. local metadata="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$(printf "%s\0" "$@" | md5_compat)" \
  1894. service rn rd
  1895. if [ -e "$cache_file" ]; then
  1896. #debug "$FUNCNAME: CACHEDIR cache hit"
  1897. cat "$cache_file"
  1898. return 0
  1899. fi
  1900. provides=$(printf "%s" "$metadata" | shyaml get-value -y -q provides "")
  1901. [ "$provides" -a "$provides" != "''" ] || { touch "$cache_file"; return 0; }
  1902. _get_provides_provides "$provides" | tee "$cache_file"
  1903. return "${PIPESTATUS[0]}"
  1904. }
  1905. _get_services_provides() {
  1906. local cache_file="$state_tmpdir/$FUNCNAME.cache.$(printf "%s\0" "$@" | md5_compat)" \
  1907. service rn rd
  1908. if [ -e "$cache_file" ]; then
  1909. #debug "$FUNCNAME: SESSION cache hit $1"
  1910. cat "$cache_file"
  1911. return 0
  1912. fi
  1913. ## YYY: replace the inner loop by a cached function
  1914. for service in "$@"; do
  1915. metadata="$(_get_service_metadata "$service")" || return 1
  1916. while read-0 rn rd; do
  1917. printf "%s\0" "$service" "$rn" "$rd"
  1918. done < <(_get_metadata_provides "$metadata")
  1919. done > "$cache_file"
  1920. if [ "$?" != 0 ]; then
  1921. rm -f "$cache_file" ## no cache
  1922. return 1
  1923. fi
  1924. cat "$cache_file"
  1925. }
  1926. export -f _get_services_provides
  1927. _get_charm_provides() {
  1928. local cache_file="$CACHEDIR/$FUNCNAME.cache.$(charm.store_metadata_hash)" errlvl
  1929. if [ -e "$cache_file" ]; then
  1930. #debug "$FUNCNAME: SESSION cache hit"
  1931. cat "$cache_file"
  1932. return 0
  1933. fi
  1934. start="$SECONDS"
  1935. debug "Getting charm provider list..."
  1936. while read-0 charm _ realpath metadata; do
  1937. metadata="$(charm.metadata "$charm")" || continue
  1938. # echo "reading $charm" >&2
  1939. while read-0 rn rd; do
  1940. printf "%s\0" "$charm" "$rn" "$rd"
  1941. done < <(_get_metadata_provides "$metadata")
  1942. done < <(charm.ls) | tee "$cache_file"
  1943. errlvl="${PIPESTATUS[0]}"
  1944. debug " ..charm provider list done $GRAY(in $((SECONDS - start))s)$NORMAL"
  1945. return "$errlvl"
  1946. }
  1947. _get_charm_providing() {
  1948. local cache_file="$state_tmpdir/$FUNCNAME.cache.$(printf "%s\0" "$@" | md5_compat)" \
  1949. relation="$1"
  1950. if [ -e "$cache_file" ]; then
  1951. #debug "$FUNCNAME: SESSION cache hit $1"
  1952. cat "$cache_file"
  1953. return 0
  1954. fi
  1955. while read-0 charm relation_name relation_def; do
  1956. [ "$relation_name" == "$relation" ] || continue
  1957. printf "%s\0" "$charm" "$relation_def"
  1958. done < <(_get_charm_provides) > "$cache_file"
  1959. if [ "$?" != 0 ]; then
  1960. rm -f "$cache_file" ## no cache
  1961. return 1
  1962. fi
  1963. cat "$cache_file"
  1964. }
  1965. _get_services_providing() {
  1966. local cache_file="$state_tmpdir/$FUNCNAME.cache.$(printf "%s\0" "$@" | md5_compat)" \
  1967. relation="$1"
  1968. shift ## services is "$@"
  1969. if [ -e "$cache_file" ]; then
  1970. #debug "$FUNCNAME: SESSION cache hit $1"
  1971. cat "$cache_file"
  1972. return 0
  1973. fi
  1974. while read-0 service relation_name relation_def; do
  1975. [ "$relation_name" == "$relation" ] || continue
  1976. printf "%s\0" "$service" "$relation_def"
  1977. done < <(_get_services_provides "$@") > "$cache_file"
  1978. if [ "$?" != 0 ]; then
  1979. rm -f "$cache_file" ## no cache
  1980. return 1
  1981. fi
  1982. cat "$cache_file"
  1983. }
  1984. export -f _get_services_provides
  1985. _out_new_relation_from_defs() {
  1986. local service="$1" rn="$2" ts="$3" prov_def="$4" rel_def="$5" rc td rc_prov
  1987. rc_prov=$(printf "%s" "$prov_def" | shyaml -y get-value "default-options" 2>/dev/null)
  1988. ## YYYvlab: should be seen even in no debug mode no ?
  1989. rc=$(printf "%s" "$rel_def" | shyaml -y get-value "default-options" 2>/dev/null)
  1990. td=$(echo "$prov_def" | shyaml get-value 'tech-dep' 2>/dev/null)
  1991. td=${td:-True}
  1992. rc=$(merge_yaml_str "$rc_prov" "$rc") || return 1
  1993. printf "%s\0" "$service" "$relation_name" "$ts" "$rc" "$td"
  1994. }
  1995. get_all_relations () {
  1996. local cache_file="$state_tmpdir/$FUNCNAME.cache.$(H "$@" "$(declare -p without_relations)")" \
  1997. services
  1998. if [ -e "${cache_file}" ]; then
  1999. #debug "$FUNCNAME: SESSION cache hit $1"
  2000. cat "${cache_file}"
  2001. return 0
  2002. fi
  2003. declare -A services
  2004. services_uses=()
  2005. ## XXXvlab: bwerk, leveraging cache to be able to get the errorlevel here.
  2006. _get_services_uses "$@" || return 1
  2007. array_read-0 services_uses < <(_get_services_uses "$@")
  2008. services_provides=()
  2009. ## XXXvlab: bwerk, leveraging cache to be able to get the errorlevel here.
  2010. _get_services_provides "$@" || return 1
  2011. array_read-0 services_provides < <(_get_services_provides "$@")
  2012. for service in "$@"; do
  2013. services[$service]=1
  2014. done
  2015. all_services=("$@")
  2016. while [ "${#all_services[@]}" != 0 ]; do
  2017. array_pop all_services service
  2018. while read-0 relation_name ts relation_config tech_dep; do
  2019. [ "${without_relations[$service:$relation_name]}" ] && {
  2020. debug "Ignoring compose $DARKYELLOW$service$NORMAL --$DARKBLUE$relation_name$NORMAL--> ${DARKYELLOW}$ts$NORMAL"
  2021. continue
  2022. }
  2023. printf "%s\0" "$service" "$relation_name" "$ts" "$relation_config" "$tech_dep"
  2024. ## adding target services ?
  2025. [ "${services[$ts]}" ] && continue
  2026. array_read-0 services_uses < <(_get_services_uses "$ts")
  2027. all_services+=("$ts")
  2028. services[$ts]=1
  2029. done < <(get_compose_relations "$service")
  2030. done > "${cache_file}.wip"
  2031. while true; do
  2032. changed=
  2033. new_services_uses=()
  2034. summon=()
  2035. required=()
  2036. recommended=()
  2037. optional=()
  2038. while [ "${#services_uses[@]}" != 0 ]; do
  2039. service="${services_uses[0]}"
  2040. relation_name="${services_uses[1]}"
  2041. relation_def="${services_uses[2]}"
  2042. services_uses=("${services_uses[@]:3}")
  2043. [ "${without_relations[$service:$relation_name]}" ] && {
  2044. debug "Skipping $DARKYELLOW$service$NORMAL --$DARKBLUE$relation_name$NORMAL--> $DARKYELLOW*$NORMAL"
  2045. continue
  2046. }
  2047. default_options=$(printf "%s" "$relation_def" | shyaml -y get-value "default-options" 2>/dev/null)
  2048. ## is this "use" declaration satisfied ?
  2049. found=
  2050. while read-0 s rn ts rc td; do
  2051. if [ -z "$found" -a "$service" == "$s" -a "$relation_name" == "$rn" ]; then
  2052. if [ "$default_options" ]; then
  2053. rc=$(merge_yaml_str "$default_options" "$rc") || return 1
  2054. fi
  2055. found="$ts"
  2056. fi
  2057. printf "%s\0" "$s" "$rn" "$ts" "$rc" "$td"
  2058. done < "${cache_file}.wip" > "${cache_file}.wip.new"
  2059. mv "${cache_file}.wip.new" "${cache_file}.wip"
  2060. if [ "$found" ]; then ## this "use" declaration was satisfied
  2061. debug "${DARKYELLOW}$service${NORMAL} use declaration for relation" \
  2062. "${DARKBLUE}$relation_name${NORMAL} is satisfied with ${DARKYELLOW}$found${NORMAL}"
  2063. continue
  2064. fi
  2065. auto=$(echo "$relation_def" | shyaml get-value auto pair 2>/dev/null)
  2066. case "$auto" in
  2067. "pair"|"summon")
  2068. service_list=()
  2069. array_read-0 service_list < <(array_keys_to_stdin services)
  2070. providers=()
  2071. providers_def=()
  2072. array_read-0 providers providers_def < <(_get_services_providing "$relation_name" "${service_list[@]}")
  2073. if [ "${#providers[@]}" == 1 ]; then
  2074. ts="${providers[0]}"
  2075. debug "Auto-pairs ${DARKYELLOW}$service${NORMAL}" \
  2076. "--${DARKBLUE}$relation_name${NORMAL}--> ${DARKYELLOW}$ts${NORMAL}"
  2077. _out_new_relation_from_defs "$service" "$relation_name" "$ts" \
  2078. "${providers_def[0]}" "$relation_def" \
  2079. >> "${cache_file}.wip"
  2080. ## Adding service
  2081. [ "${services[$ts]}" ] && continue
  2082. array_read-0 new_services_uses < <(_get_services_uses "$ts")
  2083. services[$ts]=1
  2084. changed=1
  2085. continue
  2086. elif [ "${#providers[@]}" -gt 1 ]; then
  2087. msg=""
  2088. warn "No auto-pairing ${DARKYELLOW}$service${NORMAL}" \
  2089. "--${DARKBLUE}$relation_name${NORMAL}--> ($DARKYELLOW""${providers[@]}""$NORMAL)"\
  2090. "(> 1 provider)."
  2091. continue
  2092. else
  2093. if [ "$auto" == "summon" ]; then
  2094. summon+=("$service" "$relation_name" "$relation_def")
  2095. fi
  2096. fi
  2097. ;;
  2098. ""|null|disable|disabled)
  2099. :
  2100. ;;
  2101. *)
  2102. err "Invalid ${WHITE}auto${NORMAL} value '$auto'."
  2103. return 1
  2104. ;;
  2105. esac
  2106. constraint=$(echo "$relation_def" | shyaml get-value constraint auto-pair 2>/dev/null)
  2107. case "$constraint" in
  2108. "required")
  2109. required+=("$service" "$relation_name" "$relation_def")
  2110. ;;
  2111. "recommended")
  2112. recommended+=("$service" "$relation_name" "$relation_def")
  2113. ;;
  2114. "optional")
  2115. optional+=("$service" "$relation_name" "$relation_def")
  2116. ;;
  2117. *)
  2118. err "Invalid ${WHITE}constraint${NORMAL} value '$constraint'."
  2119. return 1
  2120. ;;
  2121. esac
  2122. new_services_uses+=("$service" "$relation_name" "$relation_def") ## re-queue it
  2123. done
  2124. services_uses=("${new_services_uses[@]}")
  2125. if [ "$changed" ]; then
  2126. continue
  2127. fi
  2128. ## situation is stable
  2129. if [ "${#summon[@]}" != 0 ]; then
  2130. while [ "${#summon[@]}" != 0 ]; do
  2131. service="${summon[0]}"
  2132. relation_name="${summon[1]}"
  2133. relation_def="${summon[2]}"
  2134. summon=("${summon[@]:3}")
  2135. providers=()
  2136. providers_def=()
  2137. array_read-0 providers providers_def < <(_get_charm_providing "$relation_name" "${service_list[@]}")
  2138. if [ "${#providers[@]}" == 0 ]; then
  2139. die "Summoning a ${DARKBLUE}$relation_name${NORMAL} provider failed: none were found in charm store."
  2140. fi
  2141. if [ "${#providers[@]}" -gt 1 ]; then
  2142. warn "Auto-summon ${DARKYELLOW}$service${NORMAL}" \
  2143. "--${DARKBLUE}$relation_name${NORMAL}--> ($DARKYELLOW""${providers[@]}""$NORMAL)"\
  2144. "(> 1 provider). Choosing first."
  2145. fi
  2146. ts="${providers[0]}"
  2147. ## YYYvlab: should be seen even in no debug mode no ?
  2148. debug "Auto-summon ${DARKYELLOW}$service${NORMAL}" \
  2149. "--${DARKBLUE}$relation_name${NORMAL}--> ${DARKYELLOW}$ts${NORMAL}"
  2150. _out_new_relation_from_defs "$service" "$relation_name" "$ts" \
  2151. "${providers_def[0]}" "$relation_def" \
  2152. >> "${cache_file}.wip"
  2153. ## Adding service
  2154. [ "${services[$ts]}" ] && continue
  2155. array_read-0 services_uses < <(_get_services_uses "$ts")
  2156. services[$ts]=1
  2157. changed=1
  2158. done
  2159. continue
  2160. fi
  2161. [ "$NO_CONSTRAINT_CHECK" ] && break
  2162. if [ "${#required[@]}" != 0 ]; then
  2163. echo "$(_display_solves required)" | sed -r "s/^/${RED}||${NORMAL} /g" >&2
  2164. err "Required relations not satisfied"
  2165. return 1
  2166. fi
  2167. if [ "${#recommended[@]}" != 0 ]; then
  2168. ## make recommendation
  2169. echo "$(_display_solves recommended)" | sed -r "s/^/${YELLOW}||${NORMAL} /g" >&2
  2170. fi
  2171. if [ "${#optional[@]}" != 0 ]; then
  2172. ## inform about options
  2173. echo "$(_display_solves optional)" | sed -r "s/^/${BLUE}||${NORMAL} /g" >&2
  2174. fi
  2175. # if [ "${#required[@]}" != 0 ]; then
  2176. # err "Required relations not satisfied"
  2177. # return 1
  2178. # fi
  2179. if [ "${#recommended[@]}" != 0 ]; then
  2180. warn "Recommended relations not satisfied"
  2181. fi
  2182. break
  2183. done
  2184. if [ "$?" != 0 ]; then
  2185. rm -f "${cache_file}"{,.wip,.wip.new} ## no cache
  2186. return 1
  2187. fi
  2188. export ALL_RELATIONS="$cache_file"
  2189. mv "${cache_file}"{.wip,}
  2190. cat "$cache_file"
  2191. }
  2192. export -f get_all_relations
  2193. _display_solves() {
  2194. local array_name="$1" by_relation msg
  2195. ## inform about options
  2196. msg=""
  2197. declare -A by_relation
  2198. while read-0 service relation_name relation_def; do
  2199. solves=$(printf "%s" "$relation_def" | shyaml -y get-value solves 2>/dev/null);
  2200. auto=$(printf "%s" "$relation_def" | shyaml get-value auto 2>/dev/null);
  2201. if [ -z "$solves" ]; then
  2202. continue
  2203. fi
  2204. by_relation[$relation_name]+=$(printf "\n %s" "${DARKYELLOW}$service$NORMAL for:")
  2205. if [ "$auto" == "pair" ]; then
  2206. requirement="add provider in cluster to auto-pair"
  2207. else
  2208. requirement="add explicit relation"
  2209. fi
  2210. while read-0 name def; do
  2211. by_relation[$relation_name]+=$(printf "\n - ${DARKCYAN}%-15s${NORMAL} %s (%s)" "$name" "$def" "$requirement")
  2212. done < <(printf "%s" "$solves" | shyaml key-values-0)
  2213. done < <(array_values_to_stdin "$array_name")
  2214. while read-0 relation_name message; do
  2215. msg+="$(printf "\n${DARKBLUE}%s$NORMAL provider is $array_name by%s" \
  2216. "$relation_name" "$message" )"
  2217. done < <(array_kv_to_stdin by_relation)
  2218. if [ "$msg" ]; then
  2219. printf "%s\n" "${msg:1}"
  2220. fi
  2221. }
  2222. get_compose_relation_def() {
  2223. local service="$1" relation="$2" relation_name target_service relation_config tech_dep
  2224. while read-0 relation_name target_service relation_config tech_dep; do
  2225. [ "$relation_name" == "$relation" ] || continue
  2226. printf "%s\0%s\0%s\0" "$target_service" "$relation_config" "$tech_dep"
  2227. done < <(get_compose_relations "$service") || return 1
  2228. }
  2229. export -f get_compose_relation_def
  2230. run_service_relations () {
  2231. local service services loaded subservices subservice
  2232. PROJECT_NAME=$(get_default_project_name) || return 1
  2233. export PROJECT_NAME
  2234. declare -A loaded
  2235. subservices=$(get_ordered_service_dependencies "$@") || return 1
  2236. for service in $subservices; do
  2237. # debug "Upping dep's relations of ${DARKYELLOW}$service${NORMAL}:"
  2238. for subservice in $(get_service_deps "$service") "$service"; do
  2239. [ "${loaded[$subservice]}" ] && continue
  2240. export BASE_SERVICE_NAME=$service
  2241. MASTER_BASE_SERVICE_NAME=$(get_top_master_service_for_service "$subservice") || return 1
  2242. MASTER_BASE_CHARM_NAME=$(get_service_charm "$MASTER_BASE_SERVICE_NAME") || return 1
  2243. RELATION_BASE_COMPOSE_DEF=$(get_compose_service_def "$subservice") || return 1
  2244. export RELATION_BASE_COMPOSE_DEF MASTER_BASE_{CHARM,SERVICE}_NAME
  2245. # debug " Relations of ${DARKYELLOW}$subservice${NORMAL}:"
  2246. while read-0 relation_name target_service relation_config tech_dep; do
  2247. export relation_config
  2248. export TARGET_SERVICE_NAME=$target_service
  2249. MASTER_TARGET_SERVICE_NAME=$(get_top_master_service_for_service "$target_service") || return 1
  2250. MASTER_TARGET_CHARM_NAME=$(get_service_charm "$MASTER_TARGET_SERVICE_NAME") || return 1
  2251. RELATION_TARGET_COMPOSE_DEF=$(get_compose_service_def "$target_service") || return 1
  2252. export RELATION_TARGET_COMPOSE_DEF MASTER_TARGET_{CHARM,SERVICE}_NAME
  2253. Wrap "${wrap_opts[@]}" -d "Building $DARKYELLOW$subservice$NORMAL --$DARKBLUE$relation_name$NORMAL--> $DARKYELLOW$target_service$NORMAL" <<EOF || return 1
  2254. _run_service_relation "$relation_name" "$subservice" "$target_service" "\$relation_config"
  2255. EOF
  2256. done < <(get_service_relations "$subservice") || return 1
  2257. loaded[$subservice]=1
  2258. done
  2259. done
  2260. }
  2261. export -f run_service_relations
  2262. _run_service_action_direct() {
  2263. local service="$1" action="$2" charm _dummy
  2264. shift; shift
  2265. read-0 charm || true ## against 'set -e' that could be setup in parent scripts
  2266. if read-0 _dummy || [ "$_dummy" ]; then
  2267. print_syntax_error "$FUNCNAME: too many arguments in action descriptor"
  2268. return 1
  2269. fi
  2270. export state_tmpdir
  2271. (
  2272. set +e ## Prevents unwanted leaks from parent shell
  2273. export COMPOSE_CONFIG=$(get_compose_yml_content)
  2274. export METADATA_CONFIG=$(charm.metadata "$charm")
  2275. export SERVICE_NAME=$service
  2276. export ACTION_NAME=$action
  2277. export CONTAINER_NAME=$(get_top_master_service_for_service "$service")
  2278. export DOCKER_BASE_IMAGE=$(service_base_docker_image "$CONTAINER_NAME")
  2279. export SERVICE_DATASTORE="$DATASTORE/$service"
  2280. export SERVICE_CONFIGSTORE="$CONFIGSTORE/$service"
  2281. exname="$exname $ACTION_NAME $SERVICE_NAME" \
  2282. stdbuf -oL -eL bash -c 'charm.run_direct_action "$@"' -- "$charm" "$action" "$@"
  2283. ) 0<&6 ## inject general stdin
  2284. }
  2285. export -f _run_service_action_direct
  2286. _run_service_action_relation() {
  2287. local service="$1" action="$2" charm target_charm relation_name relation_config _dummy
  2288. shift; shift
  2289. read-0 charm target_service target_charm relation_name relation_config || true
  2290. if read-0 _dummy || [ "$_dummy" ]; then
  2291. print_syntax_error "$FUNCNAME: too many arguments in action descriptor"
  2292. return 1
  2293. fi
  2294. export RELATION_DATA_FILE=$(get_relation_data_file "$service" "$target_service" "$relation_name" "$relation_config")
  2295. export action_errlvl_file="$state_tmpdir/action-$service-$charm-$action-errlvl"
  2296. export state_tmpdir
  2297. {
  2298. (
  2299. set +e ## Prevents unwanted leaks from parent shell
  2300. export METADATA_CONFIG=$(charm.metadata "$charm")
  2301. export SERVICE_NAME=$service
  2302. export RELATION_TARGET_SERVICE="$target_service"
  2303. export RELATION_TARGET_CHARM="$target_charm"
  2304. export RELATION_BASE_SERVICE="$service"
  2305. export RELATION_BASE_CHARM="$charm"
  2306. export ACTION_NAME=$action
  2307. export CONTAINER_NAME=$(get_top_master_service_for_service "$service")
  2308. export DOCKER_BASE_IMAGE=$(service_base_docker_image "$CONTAINER_NAME")
  2309. export SERVICE_DATASTORE="$DATASTORE/$service"
  2310. export SERVICE_CONFIGSTORE="$CONFIGSTORE/$service"
  2311. exname="$exname $ACTION_NAME $SERVICE_NAME" \
  2312. stdbuf -oL -eL bash -c 'charm.run_relation_action "$@"' -- "$target_charm" "$relation_name" "$action" "$@"
  2313. echo "$?" > "$action_errlvl_file"
  2314. ) | logstdout "$DARKYELLOW$service$NORMAL/${DARKCYAN}$action${NORMAL} ${GREEN}@${NORMAL}"
  2315. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$service$NORMAL/${DARKCYAN}$action${NORMAL} ${RED}@$NORMAL" 3>&1 1>&2 2>&3
  2316. if ! [ -e "$action_errlvl_file" ]; then
  2317. err "Action $DARKYELLOW$service$NORMAL:$DARKCYAN$action$NORMAL has failed without having time" \
  2318. "to output an errlvl"
  2319. return 1
  2320. fi
  2321. return "$(cat "$action_errlvl_file")"
  2322. }
  2323. export -f _run_service_action_relation
  2324. get_relation_data_dir() {
  2325. local service="$1" target_service="$2" relation_name="$3" \
  2326. cache_file="$state_tmpdir/$FUNCNAME.cache.$(printf "%s\0" "$@" | md5_compat)"
  2327. if [ -e "$cache_file" ]; then
  2328. # debug "$FUNCNAME: cache hit ($*)"
  2329. cat "$cache_file"
  2330. return 0
  2331. fi
  2332. project=$(get_default_project_name) || return 1
  2333. relation_dir="$VARDIR/relations/$project/${service}-${target_service}/$relation_name"
  2334. if ! [ -d "$relation_dir" ]; then
  2335. mkdir -p "$relation_dir" || return 1
  2336. chmod go-rwx "$relation_dir" || return 1 ## protecting this directory
  2337. fi
  2338. echo "$relation_dir" | tee "$cache_file"
  2339. }
  2340. export -f get_relation_data_dir
  2341. get_relation_data_file() {
  2342. local service="$1" target_service="$2" relation_name="$3" relation_config="$4"
  2343. relation_dir=$(get_relation_data_dir "$service" "$target_service" "$relation_name") || return 1
  2344. relation_data_file="$relation_dir/data"
  2345. new=
  2346. if [ -e "$relation_data_file" ]; then
  2347. ## Has reference changed ?
  2348. new_md5=$(echo "$relation_config" | md5_compat)
  2349. if [ "$new_md5" != "$(cat "$relation_data_file.md5_ref" 2>/dev/null)" ]; then
  2350. new=true
  2351. fi
  2352. else
  2353. new=true
  2354. fi
  2355. if [ "$new" ]; then
  2356. echo "$relation_config" > "$relation_data_file"
  2357. chmod go-rwx "$relation_data_file" ## protecting this file
  2358. echo "$relation_config" | md5_compat > "$relation_data_file.md5_ref"
  2359. fi
  2360. echo "$relation_data_file"
  2361. }
  2362. export -f get_relation_data_file
  2363. has_service_action () {
  2364. local service="$1" action="$2" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  2365. charm target_charm relation_name target_service relation_config _tech_dep
  2366. if [ -e "$cache_file" ]; then
  2367. # debug "$FUNCNAME: cache hit ($*)"
  2368. cat "$cache_file"
  2369. return 0
  2370. fi
  2371. charm=$(get_service_charm "$service") || return 1
  2372. ## Action directly provided ?
  2373. if charm.has_direct_action "$charm" "$action" >/dev/null; then
  2374. echo -en "direct\0$charm" | tee "$cache_file"
  2375. return 0
  2376. fi
  2377. ## Action provided by relation ?
  2378. while read-0 relation_name target_service relation_config _tech_dep; do
  2379. target_charm=$(get_service_charm "$target_service") || return 1
  2380. if charm.has_relation_action "$target_charm" "$relation_name" "$action" >/dev/null; then
  2381. echo -en "relation\0$charm\0$target_service\0$target_charm\0$relation_name\0$relation_config" | tee "$cache_file"
  2382. return 0
  2383. fi
  2384. done < <(get_service_relations "$service")
  2385. return 1
  2386. # master=$(get_top_master_service_for_service "$service")
  2387. # [ "$master" == "$charm" ] && return 1
  2388. # has_service_action "$master" "$action"
  2389. }
  2390. export -f has_service_action
  2391. run_service_action () {
  2392. local service="$1" action="$2" errlvl
  2393. shift ; shift
  2394. exec 6<&0 ## saving stdin
  2395. {
  2396. if ! read-0 action_type; then
  2397. info "Service $DARKYELLOW$service$NORMAL does not have any action $DARKCYAN$action$NORMAL defined."
  2398. info " Add an executable script to 'actions/$action' to implement action."
  2399. return 1
  2400. fi
  2401. "_run_service_action_${action_type}" "$service" "$action" "$@"
  2402. errlvl="$?"
  2403. } < <(has_service_action "$service" "$action")
  2404. exec 0<&6 6<&- ## restoring stdin
  2405. return "$errlvl"
  2406. }
  2407. export -f run_service_action
  2408. get_compose_relation_config() {
  2409. local service=$1 relation_config cache_file="$state_tmpdir/$FUNCNAME.cache.$1"
  2410. if [ -e "$cache_file" ]; then
  2411. # debug "$FUNCNAME: cache hit ($*)"
  2412. cat "$cache_file"
  2413. return 0
  2414. fi
  2415. compose_service_def=$(get_compose_service_def "$service") || return 1
  2416. echo "$compose_service_def" | shyaml get-value "relations" 2>/dev/null | tee "$cache_file"
  2417. }
  2418. export -f get_compose_relation_config
  2419. # ## Return key-values-0
  2420. # get_compose_relation_config_for_service() {
  2421. # local service=$1 relation_name=$2 relation_config
  2422. # compose_service_relations=$(get_compose_relation_config "$service") || return 1
  2423. # if ! relation_config=$(
  2424. # echo "$compose_service_relations" |
  2425. # shyaml get-value "${relation_name}" 2>/dev/null); then
  2426. # err "Couldn't find $DARKYELLOW${service}$NORMAL/${WHITE}${relation_name}$NORMAL" \
  2427. # "relation config in compose configuration."
  2428. # return 1
  2429. # fi
  2430. # if [ -z "$relation_config" ]; then
  2431. # err "Relation ${WHITE}mysql-database$NORMAL is empty in compose configuration."
  2432. # return 1
  2433. # fi
  2434. # if ! echo "$relation_config" | shyaml key-values-0 2>/dev/null; then
  2435. # err "No key/values in ${DARKBLUE}mysql-database$NORMAL of compose config."
  2436. # return 1
  2437. # fi
  2438. # }
  2439. # export -f get_compose_relation_config_for_service
  2440. _get_container_relation() {
  2441. local metadata=$1 found relation_name relation_def
  2442. found=
  2443. while read-0 relation_name relation_def; do
  2444. [ "$(echo "$relation_def" | shyaml get-value "scope" 2>/dev/null)" == "container" ] && {
  2445. found="$relation_name"
  2446. break
  2447. }
  2448. done < <(_get_charm_metadata_uses "$metadata")
  2449. if [ -z "$found" ]; then
  2450. die "Charm $DARKPINK$charm$NORMAL is a subordinate but does not have any required relation declaration with" \
  2451. "${WHITE}scope${NORMAL} set to 'container'."
  2452. fi
  2453. printf "%s" "$found"
  2454. }
  2455. _get_master_service_for_service_cached () {
  2456. local service="$1" charm="$2" metadata="$3" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  2457. charm requires master_charm target_charm target_service service_def found
  2458. if [ -e "$cache_file" ]; then
  2459. # debug "$FUNCNAME: STATIC cache hit ($1)"
  2460. cat "$cache_file" &&
  2461. touch "$cache_file" || return 1
  2462. return 0
  2463. fi
  2464. if [ "$(echo "$metadata" | shyaml get-value "subordinate" 2>/dev/null)" != "True" ]; then
  2465. ## just return service name
  2466. echo "$service" | tee "$cache_file"
  2467. return 0
  2468. fi
  2469. ## Action provided by relation ?
  2470. container_relation=$(_get_container_relation "$metadata") || exit 1
  2471. read-0 target_service _ _ < <(get_service_relation "$service" "$container_relation")
  2472. if [ -z "$target_service" ]; then
  2473. err "Couldn't find ${WHITE}relations.${container_relation}${NORMAL} in" \
  2474. "${DARKYELLOW}$service$NORMAL compose definition."
  2475. err ${FUNCNAME[@]}
  2476. return 1
  2477. fi
  2478. echo "$target_service" | tee "$cache_file"
  2479. }
  2480. export -f _get_master_service_for_service_cached
  2481. get_master_service_for_service() {
  2482. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  2483. charm metadata result
  2484. if [ -e "$cache_file" ]; then
  2485. # debug "$FUNCNAME: SESSION cache hit ($*)"
  2486. cat "$cache_file" || return 1
  2487. return 0
  2488. fi
  2489. charm=$(get_service_charm "$service") || return 1
  2490. metadata=$(charm.metadata "$charm" 2>/dev/null) || {
  2491. metadata=""
  2492. warn "No charm $DARKPINK$charm$NORMAL found."
  2493. }
  2494. result=$(_get_master_service_for_service_cached "$service" "$charm" "$metadata") || return 1
  2495. echo "$result" | tee "$cache_file" || return 1
  2496. }
  2497. export -f get_master_service_for_service
  2498. get_top_master_service_for_service() {
  2499. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  2500. current_service
  2501. if [ -e "$cache_file" ]; then
  2502. # debug "$FUNCNAME: cache hit ($*)"
  2503. cat "$cache_file"
  2504. return 0
  2505. fi
  2506. current_service="$service"
  2507. while true; do
  2508. master_service=$(get_master_service_for_service "$current_service") || return 1
  2509. [ "$master_service" == "$current_service" ] && break
  2510. current_service="$master_service"
  2511. done
  2512. echo "$current_service" | tee "$cache_file"
  2513. return 0
  2514. }
  2515. export -f get_top_master_service_for_service
  2516. ##
  2517. ## The result is a mixin that is not always a complete valid
  2518. ## docker-compose entry (thinking of subordinates). The result
  2519. ## will be merge with master charms.
  2520. _get_docker_compose_mixin_from_metadata_cached() {
  2521. local service="$1" charm="$2" metadata="$3" \
  2522. has_build_dir="$4" \
  2523. cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  2524. metadata_file metadata volumes docker_compose subordinate image mixin mixins
  2525. if [ -e "$cache_file" ]; then
  2526. #debug "$FUNCNAME: STATIC cache hit $1"
  2527. cat "$cache_file" &&
  2528. touch "$cache_file" || return 1
  2529. return 0
  2530. fi
  2531. mixins=("$(echo -en "labels:\n- compose.charm=$charm")")
  2532. if [ "$metadata" ]; then
  2533. ## resources to volumes
  2534. volumes=$(
  2535. for resource_type in data config; do
  2536. while read-0 resource; do
  2537. eval "echo \" - \$HOST_${resource_type^^}STORE/\$service\$resource:\$resource:rw\""
  2538. done < <(echo "$metadata" | shyaml get-values-0 "${resource_type}-resources" 2>/dev/null)
  2539. done
  2540. while read-0 resource; do
  2541. if [[ "$resource" == /*:/*:* ]]; then
  2542. echo " - $resource"
  2543. elif [[ "$resource" == /*:/* ]]; then
  2544. echo " - $resource:rw"
  2545. elif [[ "$resource" == /*:* ]]; then
  2546. echo " - ${resource%%:*}:$resource"
  2547. elif [[ "$resource" =~ ^/[^:]+$ ]]; then
  2548. echo " - $resource:$resource:rw"
  2549. else
  2550. die "Invalid host-resource specified in 'metadata.yml'."
  2551. fi
  2552. done < <(printf "%s" "$metadata" | shyaml get-values-0 "host-resources" 2>/dev/null)
  2553. while read-0 resource; do
  2554. dest="$(charm.get_dir "$charm")/resources$resource"
  2555. if ! [ -e "$dest" ]; then
  2556. die "charm-resource: '$resource' does not exist (file: '$dest')."
  2557. fi
  2558. echo " - $dest:$resource:ro"
  2559. done < <(echo "$metadata" | shyaml get-values-0 "charm-resources" 2>/dev/null)
  2560. ) || return 1
  2561. if [ "$volumes" ]; then
  2562. mixins+=("volumes:"$'\n'"$volumes")
  2563. fi
  2564. type="$(printf "%s" "$metadata" | shyaml get-value type 2>/dev/null)" || true
  2565. if [ "$type" != "run-once" ]; then
  2566. mixins+=("restart: unless-stopped")
  2567. fi
  2568. docker_compose=$(printf "%s" "$metadata" | shyaml get-value -y "docker-compose" 2>/dev/null) || true
  2569. if [ "$docker_compose" ]; then
  2570. mixins+=("$docker_compose")
  2571. fi
  2572. if [ "$(echo "$metadata" | shyaml get-value "subordinate" 2>/dev/null)" == "True" ]; then
  2573. subordinate=true
  2574. fi
  2575. fi
  2576. image=$(echo "$metadata" | shyaml get-value "docker-image" 2>/dev/null)
  2577. [ "$image" == "None" ] && image=""
  2578. if [ "$image" ]; then
  2579. if [ "$subordinate" ]; then
  2580. err "Subordinate charm can not have a ${WHITE}docker-image${NORMAL} value."
  2581. return 1
  2582. fi
  2583. mixins+=("image: $image")
  2584. elif [ "$has_build_dir" ]; then
  2585. if [ "$subordinate" ]; then
  2586. err "Subordinate charm can not have a 'build' sub directory."
  2587. return 1
  2588. fi
  2589. mixins+=("build: $(charm.get_dir "$charm")/build")
  2590. fi
  2591. mixin=$(merge_yaml_str "${mixins[@]}") || {
  2592. err "Failed to merge mixins from ${DARKPINK}${charm}${NORMAL} metadata."
  2593. return 1
  2594. }
  2595. echo "$mixin" | tee "$cache_file"
  2596. }
  2597. export -f _get_docker_compose_mixin_from_metadata_cached
  2598. get_docker_compose_mixin_from_metadata() {
  2599. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1"
  2600. if [ -e "$cache_file" ]; then
  2601. #debug "$FUNCNAME: SESSION cache hit ($*)"
  2602. cat "$cache_file"
  2603. return 0
  2604. fi
  2605. charm=$(get_service_charm "$service") || return 1
  2606. metadata="$(charm.metadata "$charm" 2>/dev/null)" || return 1
  2607. has_build_dir=
  2608. [ -d "$(charm.get_dir "$charm")/build" ] && has_build_dir=true
  2609. mixin=$(_get_docker_compose_mixin_from_metadata_cached "$service" "$charm" "$metadata" "$has_build_dir") || return 1
  2610. echo "$mixin" | tee "$cache_file"
  2611. }
  2612. export -f get_docker_compose_mixin_from_metadata
  2613. _save() {
  2614. local name="$1"
  2615. cat - | tee -a "$docker_compose_dir/.data/$name"
  2616. }
  2617. export -f _save
  2618. get_default_project_name() {
  2619. local normalized_path compose_yml_location name
  2620. if [ -n "$DEFAULT_PROJECT_NAME" ]; then
  2621. echo "$DEFAULT_PROJECT_NAME"
  2622. return 0
  2623. fi
  2624. compose_yml_location="$(get_compose_yml_location)" || return 1
  2625. if [ -n "$compose_yml_location" ]; then
  2626. if normalized_path=$(readlink -f "$compose_yml_location"); then
  2627. name="${normalized_path%/*}" ## dirname
  2628. name="${name##*/}" ## basename
  2629. name="${name%%-deploy}" ## remove any '-deploy'
  2630. name="${name,,}" ## lowercase
  2631. e "$name"
  2632. return 0
  2633. fi
  2634. fi
  2635. echo "orphan"
  2636. return 0
  2637. }
  2638. export -f get_default_project_name
  2639. get_running_compose_containers() {
  2640. ## XXXvlab: docker bug: there will be a final newline anyway
  2641. docker ps --filter label="compose.service" --format='{{.ID}}'
  2642. }
  2643. export -f get_running_compose_containers
  2644. get_healthy_container_ip_for_service () {
  2645. local service="$1" port="$2" timeout=${3:-60}
  2646. local containers container container_network container_ip
  2647. containers="$(get_running_containers_for_service "$service")"
  2648. if [ -z "$containers" ]; then
  2649. err "No containers running for service $DARKYELLOW$service$NORMAL."
  2650. exit 1
  2651. fi
  2652. ## XXXvlab: taking first container is probably not a good idea
  2653. container="$(echo "$containers" | head -n 1)"
  2654. ## XXXvlab: taking first ip is probably not a good idea
  2655. read-0 container_network container_ip < <(get_container_network_ip "$container")
  2656. if [ -z "$container_ip" ]; then
  2657. err "Can't get container's IP. You should check health of" \
  2658. "${DARKYELLOW}$service${NORMAL}'s container."
  2659. exit 1
  2660. fi
  2661. wait_for_tcp_port "$container_network" "$container_ip:8069" 4 || {
  2662. err "TCP port of ${DARKYELLOW}$service${NORMAL}'s container doesn't seem open"
  2663. echo " Please check that container is healthy. Here are last logs:" >&2
  2664. docker logs "$container" --tail=10 | prefix " ${GRAY}|${NORMAL} " >&2
  2665. exit 1
  2666. }
  2667. info "Host/Port ${container_ip}:${port} checked ${GREEN}open${NORMAL}."
  2668. echo "$container_network:$container_ip"
  2669. }
  2670. export -f get_healthy_container_ip_for_service
  2671. switch_to_relation_service() {
  2672. local relation="$1"
  2673. ## XXXvlab: can't get real config here
  2674. if ! read-0 ts _ _ < <(get_service_relation "$SERVICE_NAME" "$relation"); then
  2675. err "Couldn't find relation ${DARKCYAN}$relation${NORMAL}."
  2676. exit 1
  2677. fi
  2678. export SERVICE_NAME="$ts"
  2679. export SERVICE_DATASTORE="$DATASTORE/$SERVICE_NAME"
  2680. DOCKER_BASE_IMAGE=$(service_base_docker_image "$SERVICE_NAME")
  2681. export DOCKER_BASE_IMAGE
  2682. target_charm=$(get_service_charm "$ts") || exit 1
  2683. target_charm_path=$(charm.get_dir "$target_charm") || exit 1
  2684. cd "$target_charm_path"
  2685. }
  2686. export -f switch_to_relation_service
  2687. get_volumes_for_container() {
  2688. local container="$1"
  2689. docker inspect \
  2690. --format '{{range $mount := .Mounts}}{{$mount.Source}}{{"\x00"}}{{$mount.Destination}}{{"\x00"}}{{end}}' \
  2691. "$container"
  2692. }
  2693. export -f get_volumes_for_container
  2694. is_volume_used() {
  2695. local volume="$1" container_id src dst
  2696. while read -r container_id; do
  2697. while read-0 src dst; do
  2698. [[ "$src/" == "$volume"/* ]] && return 0
  2699. done < <(get_volumes_for_container "$container_id")
  2700. done < <(get_running_compose_containers)
  2701. return 1
  2702. }
  2703. export -f is_volume_used
  2704. clean_unused_docker_compose() {
  2705. for f in /var/lib/compose/docker-compose/*; do
  2706. [ -e "$f" ] || continue
  2707. is_volume_used "$f" && continue
  2708. debug "Cleaning unused docker-compose ${f##*/}"
  2709. rm -rf "$f" || return 1
  2710. done
  2711. return 0
  2712. }
  2713. export -f clean_unused_docker_compose
  2714. stdin_get_hash() {
  2715. local sha
  2716. sha=$(sha256sum) || return 1
  2717. sha=${sha:0:64}
  2718. echo "$sha"
  2719. }
  2720. export -f stdin_get_hash
  2721. file_get_hash() {
  2722. stdin_get_hash < "$1" || return 1
  2723. }
  2724. export -f file_get_hash
  2725. docker_compose_store() {
  2726. local file="$1" sha
  2727. sha=$(file_get_hash "$file") || return 1
  2728. project=$(get_default_project_name) || return 1
  2729. dst="/var/lib/compose/docker-compose/$sha/$project"
  2730. mkdir -p "$dst" || return 1
  2731. cat <<EOF > "$dst/.env" || return 1
  2732. DOCKER_COMPOSE_PATH=$dst
  2733. COMPOSE_HTTP_TIMEOUT=7200
  2734. EOF
  2735. cp "$file" "$dst/docker-compose.yml" || return 1
  2736. mkdir -p "$dst/bin" || return 1
  2737. cat <<EOF > "$dst/bin/dc" || return 1
  2738. #!/bin/bash
  2739. $(declare -f read-0)
  2740. docker_run_opts=()
  2741. while read-0 opt; do
  2742. if [[ "\$opt" == "!env:"* ]]; then
  2743. opt="\${opt##!env:}"
  2744. var="\${opt%%=*}"
  2745. value="\${opt#*=}"
  2746. export "\$var"="\$value"
  2747. else
  2748. docker_run_opts+=("\$opt")
  2749. fi
  2750. done < <(cat "$COMPOSE_LAUNCHER_OPTS")
  2751. docker_run_opts+=(
  2752. "-w" "$dst"
  2753. "--entrypoint" "/usr/local/bin/docker-compose"
  2754. )
  2755. [ -t 1 ] && {
  2756. docker_run_opts+=("-ti")
  2757. }
  2758. exec docker run --rm "\${docker_run_opts[@]}" "\${COMPOSE_DOCKER_IMAGE:-docker.0k.io/compose}" "\$@"
  2759. EOF
  2760. chmod +x "$dst/bin/dc" || return 1
  2761. printf "%s" "$sha"
  2762. }
  2763. export -f docker_compose_store
  2764. launch_docker_compose() {
  2765. local charm docker_compose_tmpdir docker_compose_dir
  2766. docker_compose_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
  2767. #debug "Creating temporary docker-compose directory in '$docker_compose_tmpdir'."
  2768. trap_add EXIT "rm -rf \"$docker_compose_tmpdir\""
  2769. ## docker-compose will name network from the parent dir name
  2770. project=$(get_default_project_name)
  2771. mkdir -p "$docker_compose_tmpdir/$project"
  2772. docker_compose_dir="$docker_compose_tmpdir/$project"
  2773. get_docker_compose $SERVICE_PACK > "$docker_compose_dir/docker-compose.yml" || return 1
  2774. if [ -e "$state_tmpdir/to-merge-in-docker-compose.yml" ]; then
  2775. # debug "Merging some config data in docker-compose.yml:"
  2776. # debug "$(cat $state_tmpdir/to-merge-in-docker-compose.yml)"
  2777. _config_merge "$docker_compose_dir/docker-compose.yml" "$state_tmpdir/to-merge-in-docker-compose.yml" || return 1
  2778. fi
  2779. if [ -z "$(echo $(cat "$docker_compose_dir/docker-compose.yml"))" ]; then
  2780. die "Generated 'docker-compose.yml' is unexpectedly empty."
  2781. fi
  2782. ## XXXvlab: could be more specific and only link the needed charms
  2783. ## XXXvlab: why do we need these links ? If this is for the build command, then it is not useful anymore.
  2784. # for charm in $(shyaml keys services < "$docker_compose_dir/docker-compose.yml"); do
  2785. # if charm.exists "$charm"; then
  2786. # ln -sf "$(charm.get_dir "$charm")" "$docker_compose_dir/$charm" || exit 1
  2787. # fi
  2788. # done
  2789. mkdir "$docker_compose_dir/.data"
  2790. if [ -z "$COMPOSE_DISABLE_DOCKER_COMPOSE_STORE" ]; then
  2791. sha=$(docker_compose_store "$docker_compose_dir/docker-compose.yml") || return 1
  2792. fi
  2793. {
  2794. {
  2795. {
  2796. if [ -z "$COMPOSE_DISABLE_DOCKER_COMPOSE_STORE" ]; then
  2797. cd "/var/lib/compose/docker-compose/$sha/$project" || return 1
  2798. else
  2799. cd "$docker_compose_dir" || return 1
  2800. fi
  2801. if [ -f ".env" ]; then
  2802. debug "${WHITE}.env$NORMAL for $DARKYELLOW$SERVICE_PACK$NORMAL:"
  2803. debug "$(cat ".env" | prefix " $GRAY|$NORMAL ")"
  2804. fi
  2805. debug "${WHITE}docker-compose.yml$NORMAL for $DARKYELLOW$SERVICE_PACK$NORMAL:"
  2806. debug "$(cat "docker-compose.yml" | prefix " $GRAY|$NORMAL ")"
  2807. debug "${WHITE}Launching$NORMAL: docker-compose $@"
  2808. if [ "$DRY_COMPOSE_RUN" ]; then
  2809. echo docker-compose "$@"
  2810. else
  2811. docker-compose "$@"
  2812. fi
  2813. echo "$?" > "$docker_compose_dir/.data/errlvl"
  2814. } | _save stdout
  2815. } 3>&1 1>&2 2>&3 | _save stderr
  2816. } 3>&1 1>&2 2>&3
  2817. if tail -n 1 "$docker_compose_dir/.data/stderr" | grep -E "Service .+ failed to build: Error getting container [0-9a-f]+ from driver devicemapper: (open|Error mounting) /dev/mapper/docker-.*: no such file or directory$" >/dev/null 2>&1; then
  2818. err "Detected bug https://github.com/docker/docker/issues/4036 ... "
  2819. err "Please re-launch your command, or switch from 'devicemapper' driver to 'overlayfs' or 'aufs'."
  2820. fi
  2821. docker_compose_errlvl="$(cat "$docker_compose_dir/.data/errlvl" 2>/dev/null)"
  2822. if [ -z "$docker_compose_errlvl" ]; then
  2823. err "Something went wrong before you could gather docker-compose errorlevel."
  2824. return 1
  2825. fi
  2826. return "$docker_compose_errlvl"
  2827. }
  2828. export -f launch_docker_compose
  2829. get_compose_yml_location() {
  2830. if ! [ -z ${COMPOSE_YML_FILE+x} ]; then ## if set, even if empty
  2831. echo "$COMPOSE_YML_FILE"
  2832. return 0
  2833. fi
  2834. parent=$(while ! [ -f "./compose.yml" ]; do
  2835. [ "$PWD" == "/" ] && exit 0
  2836. cd ..
  2837. done; echo "$PWD"
  2838. )
  2839. if [ "$parent" ]; then
  2840. echo "$parent/compose.yml"
  2841. return 0
  2842. fi
  2843. ## XXXvlab: do we need this additional environment variable,
  2844. ## COMPOSE_YML_FILE is not sufficient ?
  2845. if [ "$DEFAULT_COMPOSE_FILE" ]; then
  2846. if ! [ -e "$DEFAULT_COMPOSE_FILE" ]; then
  2847. warn "No 'compose.yml' was found in current or parent dirs," \
  2848. "and \$DEFAULT_COMPOSE_FILE points to an unexistent file." \
  2849. "(${DEFAULT_COMPOSE_FILE})"
  2850. return 0
  2851. fi
  2852. echo "$DEFAULT_COMPOSE_FILE"
  2853. return 0
  2854. fi
  2855. warn "No 'compose.yml' was found in current or parent dirs, and no \$DEFAULT_COMPOSE_FILE was set."
  2856. return 0
  2857. }
  2858. export -f get_compose_yml_location
  2859. get_compose_yml_content() {
  2860. local cache_file="$state_tmpdir/$FUNCNAME.cache"
  2861. if [ -e "$cache_file" ]; then
  2862. cat "$cache_file" &&
  2863. touch "$cache_file" || return 1
  2864. return 0
  2865. fi
  2866. if [ -z "$COMPOSE_YML_FILE" ]; then
  2867. COMPOSE_YML_FILE=$(get_compose_yml_location) || exit 1
  2868. fi
  2869. if [ -e "$COMPOSE_YML_FILE" ]; then
  2870. debug "Found $WHITE$exname$NORMAL YAML file in '$COMPOSE_YML_FILE'."
  2871. COMPOSE_YML_CONTENT=$(cat "$COMPOSE_YML_FILE") || {
  2872. err "Could not read '$COMPOSE_YML_FILE'."
  2873. return 1
  2874. }
  2875. else
  2876. debug "No compose file found. Using an empty one."
  2877. COMPOSE_YML_CONTENT=""
  2878. fi
  2879. COMPOSE_YML_CONTENT=$(merge_yaml_str "$COMPOSE_YML_CONTENT" "${compose_contents[@]}") || return 1
  2880. output=$(echo "$COMPOSE_YML_CONTENT"| shyaml get-value 2>&1)
  2881. if [ "$?" != 0 ]; then
  2882. outputed_something=
  2883. while IFS='' read -r line1 && IFS='' read -r line2; do
  2884. [ "$outputed_something" ] || err "Invalid YAML in '$COMPOSE_YML_FILE':"
  2885. outputed_something=true
  2886. echo "$line1 $GRAY($line2)$NORMAL"
  2887. done < <(echo "$output" | grep ^yaml.scanner -A 100 |
  2888. sed -r 's/^ in "<stdin>", //g' | sed -r 's/^yaml.scanner.[a-zA-Z]+: //g') |
  2889. prefix " $GRAY|$NORMAL "
  2890. [ "$outputed_something" ] || {
  2891. err "Unexpected error while running 'shyaml get-value' on '$COMPOSE_YML_FILE':"
  2892. echo "$output" | prefix " $GRAY|$NORMAL "
  2893. }
  2894. return 1
  2895. fi
  2896. echo "$COMPOSE_YML_CONTENT" | tee "$cache_file" || return 1
  2897. }
  2898. export -f get_compose_yml_content
  2899. get_default_target_services() {
  2900. local services=("$@")
  2901. if [ -z "${services[*]}" ]; then
  2902. if [ "$DEFAULT_SERVICES" ]; then
  2903. debug "No service provided, using $WHITE\$DEFAULT_SERVICES$NORMAL variable." \
  2904. "Target services: $DARKYELLOW$DEFAULT_SERVICES$NORMAL"
  2905. services="$DEFAULT_SERVICES"
  2906. else
  2907. err "No service provided."
  2908. return 1
  2909. fi
  2910. fi
  2911. echo "${services[*]}"
  2912. }
  2913. export -f get_default_target_services
  2914. get_master_services() {
  2915. local loaded master_service service
  2916. declare -A loaded
  2917. for service in "$@"; do
  2918. master_service=$(get_top_master_service_for_service "$service") || return 1
  2919. if [ "${loaded[$master_service]}" ]; then
  2920. continue
  2921. fi
  2922. echo "$master_service"
  2923. loaded["$master_service"]=1
  2924. done | nspc
  2925. return "${PIPESTATUS[0]}"
  2926. }
  2927. export -f get_master_services
  2928. get_current_docker_container_id() {
  2929. local line
  2930. line=$(cat "/proc/self/cpuset") || return 1
  2931. [[ "$line" == *docker* ]] || return 1
  2932. echo "${line##*/}"
  2933. }
  2934. export -f get_current_docker_container_id
  2935. ## if we are in a docker compose, we might want to know what is the
  2936. ## real host path of some local paths.
  2937. get_host_path() {
  2938. local path="$1"
  2939. path=$(realpath "$path") || return 1
  2940. container_id=$(get_current_docker_container_id) || {
  2941. print "%s" "$path"
  2942. return 0
  2943. }
  2944. biggest_dst=
  2945. current_src=
  2946. while read-0 src dst; do
  2947. [[ "$path" == "$dst"* ]] || continue
  2948. if [[ "${#biggest_dst}" < "${#dst}" ]]; then
  2949. biggest_dst="$dst"
  2950. current_src="$src"
  2951. fi
  2952. done < <(get_volumes_for_container "$container_id")
  2953. if [ "$current_src" ]; then
  2954. printf "%s" "$current_src"
  2955. else
  2956. return 1
  2957. fi
  2958. }
  2959. export -f get_host_path
  2960. _setup_state_dir() {
  2961. export state_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
  2962. #debug "Creating temporary state directory in '$state_tmpdir'."
  2963. # trap_add EXIT "debug \"Removing temporary state directory in $state_tmpdir.\";\
  2964. # rm -rf \"$state_tmpdir\""
  2965. trap_add EXIT "rm -rf \"$state_tmpdir\""
  2966. }
  2967. get_docker_compose_help_msg() {
  2968. local action="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$({ p0 "$1"; cat "$(which docker-compose)"; } | md5_compat)" \
  2969. docker_compose_help_msg
  2970. if [ -e "$cache_file" ]; then
  2971. cat "$cache_file" &&
  2972. touch "$cache_file" || return 1
  2973. return 0
  2974. fi
  2975. docker_compose_help_msg=$(docker-compose $action --help 2>/dev/null) || return 1
  2976. echo "$docker_compose_help_msg" |
  2977. tee "$cache_file" || return 1
  2978. }
  2979. get_docker_compose_usage() {
  2980. local action="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$({ p0 "$1"; cat "$(which docker-compose)"; } | md5_compat)" \
  2981. docker_compose_help_msg
  2982. if [ -e "$cache_file" ]; then
  2983. cat "$cache_file" &&
  2984. touch "$cache_file" || return 1
  2985. return 0
  2986. fi
  2987. docker_compose_help_msg=$(get_docker_compose_help_msg $action) || return 1
  2988. echo "$docker_compose_help_msg" |
  2989. grep -m 1 "^Usage:" -A 10000 |
  2990. egrep -m 1 "^\$" -B 10000 |
  2991. nspc |
  2992. sed -r 's/^Usage: //g' |
  2993. tee "$cache_file" || return 1
  2994. }
  2995. get_docker_compose_opts_help() {
  2996. local action="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$({ p0 "$1"; cat "$(which docker-compose)"; } | md5_compat)" \
  2997. docker_compose_help_msg
  2998. if [ -e "$cache_file" ]; then
  2999. cat "$cache_file" &&
  3000. touch "$cache_file" || return 1
  3001. return 0
  3002. fi
  3003. docker_compose_opts_help=$(get_docker_compose_help_msg $action) || return 1
  3004. echo "$docker_compose_opts_help" |
  3005. grep '^Options:' -A 20000 |
  3006. tail -n +2 |
  3007. { cat ; echo; } |
  3008. egrep -m 1 "^\S*\$" -B 10000 |
  3009. head -n -1 |
  3010. tee "$cache_file" || return 1
  3011. }
  3012. get_docker_compose_commands_help() {
  3013. local action="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$({ p0 "$1"; cat "$(which docker-compose)"; } | md5_compat)" \
  3014. docker_compose_help_msg
  3015. if [ -e "$cache_file" ]; then
  3016. cat "$cache_file" &&
  3017. touch "$cache_file" || return 1
  3018. return 0
  3019. fi
  3020. docker_compose_opts_help=$(get_docker_compose_help_msg $action) || return 1
  3021. echo "$docker_compose_opts_help" |
  3022. grep '^Commands:' -A 20000 |
  3023. tail -n +2 |
  3024. { cat ; echo; } |
  3025. egrep -m 1 "^\S*\$" -B 10000 |
  3026. head -n -1 |
  3027. tee "$cache_file" || return 1
  3028. }
  3029. get_docker_compose_opts_list() {
  3030. local action="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$({ p0 "$1"; cat "$(which docker-compose)"; } | md5_compat)" \
  3031. docker_compose_help_msg
  3032. if [ -e "$cache_file" ]; then
  3033. cat "$cache_file" &&
  3034. touch "$cache_file" || return 1
  3035. return 0
  3036. fi
  3037. docker_compose_opts_help=$(get_docker_compose_opts_help $action) || return 1
  3038. echo "$docker_compose_opts_help" |
  3039. egrep "^\s+-" |
  3040. sed -r 's/\s+((((-[a-zA-Z]|--[a-zA-Z0-9-]+)( [A-Z=]+|=[^ ]+)?)(, )?)+)\s+.*$/\1/g' |
  3041. tee "$cache_file" || return 1
  3042. }
  3043. options_parser() {
  3044. sed -r 's/^(\s+(((-[a-zA-Z]|--[a-zA-Z0-9-]+)([ =]([a-zA-Z_=\"\[]|\])+)?(, | )?)+)\s+)[^ ].*$/\x0\2\x0\0/g'
  3045. printf "\0"
  3046. }
  3047. remove_options_in_option_help_msg() {
  3048. {
  3049. read-0 null
  3050. if [ "$null" ]; then
  3051. err "options parsing error, should start with an option line."
  3052. return 1
  3053. fi
  3054. while read-0 opt full_txt;do
  3055. multi_opts="$(printf "%s " $opt | multi_opts_filter)"
  3056. single_opts="$(printf "%s " $opt | single_opts_filter)"
  3057. for to_remove in "$@"; do
  3058. str_matches "$to_remove" $multi_opts $single_opts && {
  3059. continue 2
  3060. }
  3061. done
  3062. echo -n "$full_txt"
  3063. done
  3064. } < <(options_parser)
  3065. }
  3066. _MULTIOPTION_REGEX='^((-[a-zA-Z]|--[a-zA-Z0-9-]+)(, )?)+'
  3067. _MULTIOPTION_REGEX_LINE_FILTER=$_MULTIOPTION_REGEX'(\s|=)'
  3068. multi_opts_filter() {
  3069. egrep "$_MULTIOPTION_REGEX_LINE_FILTER" |
  3070. sed -r "s/^($_MULTIOPTION_REGEX)(\s|=).*$/\1/g" |
  3071. tr ',' "\n" | nspc
  3072. }
  3073. single_opts_filter() {
  3074. egrep -v "$_MULTIOPTION_REGEX_LINE_FILTER" |
  3075. tr ',' "\n" | nspc
  3076. }
  3077. get_docker_compose_multi_opts_list() {
  3078. local action="$1" opts_list
  3079. opts_list=$(get_docker_compose_opts_list "$action") || return 1
  3080. echo "$opts_list" | multi_opts_filter
  3081. }
  3082. get_docker_compose_single_opts_list() {
  3083. local action="$1" opts_list
  3084. opts_list=$(get_docker_compose_opts_list "$action") || return 1
  3085. echo "$opts_list" | single_opts_filter
  3086. }
  3087. display_commands_help() {
  3088. local charm_actions
  3089. echo
  3090. echo "${WHITE}Commands${NORMAL} (thanks to docker-compose):"
  3091. get_docker_compose_commands_help | sed -r "s/ ([a-z]+)(\s+)/ ${DARKCYAN}\1${NORMAL}\2/g"
  3092. charm_actions_help=$(get_docker_charm_action_help) || return 1
  3093. if [ "$charm_actions_help" ]; then
  3094. echo
  3095. echo "${WHITE}Charm actions${NORMAL}:"
  3096. printf "%s\n" "$charm_actions_help" | \
  3097. sed -r "s/^ ([a-z0-9-]+)(\s+)([a-z0-9-]+)(\s+)/ ${DARKCYAN}\1${NORMAL}\2${DARKYELLOW}\3${NORMAL}\4/g"
  3098. fi
  3099. }
  3100. get_docker_charm_action_help() {
  3101. local services service charm relation_name target_service relation_config \
  3102. target_charm
  3103. services=($(get_compose_yml_content | shyaml keys 2>/dev/null))
  3104. for service in "${services[@]}"; do
  3105. out=$(
  3106. charm=$(get_service_charm "$service") || return 1
  3107. for action in $(charm.ls_direct_actions "$charm"); do
  3108. printf " %-28s %s\n" "$action $service" "Direct action from ${DARKPINK}$charm${NORMAL}"
  3109. done
  3110. while read-0 relation_name target_service _relation_config _tech_dep; do
  3111. target_charm=$(get_service_charm "$target_service") || return 1
  3112. for action in $(charm.ls_relation_actions "$target_charm" "$relation_name"); do
  3113. printf " %-28s %s\n" "$action $service" "Indirect action from ${DARKPINK}$target_charm${NORMAL}"
  3114. done
  3115. done < <(get_compose_relations "$service")
  3116. )
  3117. if [ "$out" ]; then
  3118. echo " for ${DARKYELLOW}$service${NORMAL}:"
  3119. printf "%s\n" "$out"
  3120. fi
  3121. done
  3122. }
  3123. display_help() {
  3124. print_help
  3125. echo "${WHITE}Usage${NORMAL}:"
  3126. echo " $usage"
  3127. echo "${WHITE}Options${NORMAL}:"
  3128. echo " -h, --help Print this message and quit"
  3129. echo " (ignoring any other options)"
  3130. echo " -V, --version Print current version and quit"
  3131. echo " (ignoring any other options)"
  3132. echo " --dirs Display data dirs and quit"
  3133. echo " (ignoring any other options)"
  3134. echo " -v, --verbose Be more verbose"
  3135. echo " -q, --quiet Be quiet"
  3136. echo " -d, --debug Print full debugging information (sets also verbose)"
  3137. echo " --dry-compose-run If docker-compose will be run, only print out what"
  3138. echo " command line will be used."
  3139. echo " --no-relations Do not run any relation script"
  3140. echo " --no-hooks Do not run any hook script"
  3141. echo " --no-init Do not run any init script"
  3142. echo " --no-post-deploy Do not run any post-deploy script"
  3143. echo " --no-pre-deploy Do not run any pre-deploy script"
  3144. echo " --without-relation RELATION "
  3145. echo " Do not run given relation"
  3146. echo " -R, --rebuild-relations-to-service SERVICE"
  3147. echo " Will rebuild all relations to given service"
  3148. echo " --add-compose-content, -Y YAML"
  3149. echo " Will merge some direct YAML with the current compose"
  3150. get_docker_compose_opts_help | remove_options_in_option_help_msg --version --help --verbose |
  3151. filter_docker_compose_help_message
  3152. display_commands_help
  3153. }
  3154. _graph_service() {
  3155. local service="$1" base="$1"
  3156. charm=$(get_service_charm "$service") || return 1
  3157. metadata=$(charm.metadata "$charm") || return 1
  3158. subordinate=$(echo "$metadata" | shyaml get-value "subordinate" 2>/dev/null)
  3159. if [ "$subordinate" == "True" ]; then
  3160. requires="$(echo "$metadata" | shyaml get-value "requires" 2>/dev/null)"
  3161. master_charm=
  3162. while read-0 relation_name relation; do
  3163. [ "$(echo "$relation" | shyaml get-value "scope" 2>/dev/null)" == "container" ] || continue
  3164. interface="$(echo "$relation" | shyaml get-value "interface" 2>/dev/null)"
  3165. if [ -z "$interface" ]; then
  3166. err "No ${WHITE}$interface${NORMAL} set for relation $relation_name."
  3167. return 1
  3168. fi
  3169. ## Action provided by relation ?
  3170. target_service=
  3171. while read-0 relation_name candidate_target_service _relation_config _tech_dep; do
  3172. [ "$interface" == "$relation_name" ] && {
  3173. target_service="$candidate_target_service"
  3174. break
  3175. }
  3176. done < <(get_service_relations "$service")
  3177. if [ -z "$target_service" ]; then
  3178. err "Couldn't find ${WHITE}relations.$interface${NORMAL} in" \
  3179. "${DARKYELLOW}$service$NORMAL compose definition."
  3180. return 1
  3181. fi
  3182. master_service="$target_service"
  3183. master_charm=$(get_service_charm "$target_service") || return 1
  3184. break
  3185. done < <(echo "$requires" | shyaml key-values-0 2>/dev/null)
  3186. fi
  3187. _graph_node_service "$service" "$base" "$charm"
  3188. _graph_edge_service "$service" "$subordinate" "$master_service"
  3189. }
  3190. _graph_node_service() {
  3191. local service="$1" base="$2" charm="$3"
  3192. cat <<EOF
  3193. "$(_graph_node_service_label ${service})" [
  3194. style = "filled, $([ "$subordinate" == "True" ] && echo "dashed" || echo "bold")"
  3195. penwidth = $([ "$subordinate" == "True" ] && echo "3" || echo "5")
  3196. color = $([ "$base" ] && echo "blue" || echo "black")
  3197. fillcolor = "white"
  3198. fontname = "Courier New"
  3199. shape = "Mrecord"
  3200. label =<$(_graph_node_service_content "$service")>
  3201. ];
  3202. EOF
  3203. }
  3204. _graph_edge_service() {
  3205. local service="$1" subordinate="$2" master_service="$3"
  3206. while read-0 relation_name target_service relation_config tech_dep; do
  3207. cat <<EOF
  3208. "$(_graph_node_service_label ${service})" -> "$(_graph_node_service_label ${target_service})" [
  3209. penwidth = $([ "$master_service" == "$target_service" ] && echo 3 || echo 2)
  3210. fontsize = 16
  3211. fontcolor = "black"
  3212. style = $([ "$master_service" == "$target_service" ] && echo dashed || echo "\"\"")
  3213. weight = $([ "$master_service" == "$target_service" ] && echo 2.0 || echo 1.0)
  3214. dir = $([ "$master_service" == "$target_service" ] && echo none || echo both)
  3215. arrowtail = odot
  3216. # arrowhead = dotlicurve
  3217. taillabel = "$relation_name" ];
  3218. EOF
  3219. done < <(get_service_relations "$service") || return 1
  3220. }
  3221. _graph_node_service_label() {
  3222. local service="$1"
  3223. echo "service_$service"
  3224. }
  3225. _graph_node_service_content() {
  3226. local service="$1"
  3227. charm=$(get_service_charm "$service") || return 1
  3228. cat <<EOF
  3229. <table border="0" cellborder="0" cellpadding="3" bgcolor="white">
  3230. <tr>
  3231. <td bgcolor="black" align="center" colspan="2">
  3232. <font color="white">$service</font>
  3233. </td>
  3234. </tr>
  3235. $(if [ "$charm" != "$service" ]; then
  3236. cat <<EOF2
  3237. <tr>
  3238. <td align="left" port="r0">charm: $charm</td>
  3239. </tr>
  3240. EOF2
  3241. fi)
  3242. </table>
  3243. EOF
  3244. }
  3245. cla_contains () {
  3246. local e
  3247. for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  3248. return 1
  3249. }
  3250. filter_docker_compose_help_message() {
  3251. cat - |
  3252. sed -r "s/docker-compose run/${DARKWHITE}compose${NORMAL} ${DARKCYAN}$action${NORMAL}/g;
  3253. s/docker-compose.yml/compose.yml/g;
  3254. s/SERVICES?/${DARKYELLOW}\0${NORMAL}/g;
  3255. s/^(\s+)\\$/\1${WHITE}\$${NORMAL}/g;
  3256. s/^(\s+)run/\1${DARKCYAN}$action${NORMAL}/g;
  3257. s/docker-compose/${DARKWHITE}compose${NORMAL}/g"
  3258. }
  3259. graph() {
  3260. local services=("$@")
  3261. declare -A entries
  3262. cat <<EOF
  3263. digraph g {
  3264. graph [
  3265. fontsize=30
  3266. labelloc="t"
  3267. label=""
  3268. splines=true
  3269. overlap=false
  3270. #rankdir = "LR"
  3271. ];
  3272. ratio = auto;
  3273. EOF
  3274. for target_service in "$@"; do
  3275. services=$(get_ordered_service_dependencies "$target_service") || return 1
  3276. for service in $services; do
  3277. [ "${entries[$service]}" ] && continue || entries[$service]=1
  3278. if cla_contains "$service" "${services[@]}"; then
  3279. base=true
  3280. else
  3281. base=
  3282. fi
  3283. _graph_service "$service" "$base"
  3284. done
  3285. done
  3286. echo "}"
  3287. }
  3288. cached_wget() {
  3289. local cache_file="$CACHEDIR/$FUNCNAME.cache.$(p0 "$@" | md5_compat)" \
  3290. url="$1"
  3291. if [ -e "$cache_file" ]; then
  3292. cat "$cache_file"
  3293. touch "$cache_file"
  3294. return 0
  3295. fi
  3296. wget -O- "${url}" |
  3297. tee "$cache_file"
  3298. if [ "${PIPESTATUS[0]}" != 0 ]; then
  3299. rm "$cache_file"
  3300. die "Unable to fetch '$url'."
  3301. return 1
  3302. fi
  3303. }
  3304. export -f cached_wget
  3305. [ "$SOURCED" ] && return 0
  3306. trap_add "EXIT" clean_cache
  3307. if [ -z "$DISABLE_SYSTEM_CONFIG_FILE" ]; then
  3308. if [ -r /etc/default/charm ]; then
  3309. . /etc/default/charm
  3310. fi
  3311. if [ -r "/etc/default/$exname" ]; then
  3312. . "/etc/default/$exname"
  3313. fi
  3314. ## XXXvlab: should provide YML config opportunities in possible parent dirs ?
  3315. ## userdir ? and global /etc/compose.yml ?
  3316. for cfgfile in /etc/compose.conf /etc/compose.local.conf \
  3317. /etc/default/compose /etc/compose/local.conf; do
  3318. [ -e "$cfgfile" ] || continue
  3319. . "$cfgfile" || die "Loading config file '$cfgfile' failed."
  3320. done
  3321. fi
  3322. _setup_state_dir
  3323. mkdir -p "$CACHEDIR" || exit 1
  3324. log () { cat; }
  3325. export -f log
  3326. ##
  3327. ## Argument parsing
  3328. ##
  3329. wrap_opts=()
  3330. services=()
  3331. remainder_args=()
  3332. compose_opts=()
  3333. compose_contents=()
  3334. action_opts=()
  3335. services_args=()
  3336. pos_arg_ct=0
  3337. no_hooks=
  3338. no_init=
  3339. action=
  3340. stage="main" ## switches from 'main', to 'action', 'remainder'
  3341. is_docker_compose_action=
  3342. rebuild_relations_to_service=()
  3343. declare -A without_relations
  3344. DC_MATCH_MULTI=$(get_docker_compose_multi_opts_list) &&
  3345. DC_MATCH_SINGLE=$(get_docker_compose_single_opts_list) || exit 1
  3346. while read-0 arg; do
  3347. case "$stage" in
  3348. "main")
  3349. case "$arg" in
  3350. --help|-h)
  3351. no_init=true ; no_hooks=true ; no_relations=true
  3352. display_help
  3353. exit 0
  3354. ;;
  3355. --verbose|-v)
  3356. export VERBOSE=true
  3357. compose_opts+=("--verbose")
  3358. ;;
  3359. --quiet|-q)
  3360. export QUIET=true
  3361. export wrap_opts+=("-q")
  3362. log () { cat >&2; }
  3363. export -f log
  3364. ;;
  3365. --version|-V)
  3366. print_version
  3367. docker-compose --version
  3368. docker --version
  3369. exit 0
  3370. ;;
  3371. -f|--file)
  3372. read-0 value
  3373. [ -e "$value" ] || die "File $value doesn't exists"
  3374. export COMPOSE_YML_FILE="$value"
  3375. shift
  3376. ;;
  3377. -p|--project-name)
  3378. read-0 value
  3379. export DEFAULT_PROJECT_NAME="$value"
  3380. compose_opts+=("--project-name $value")
  3381. shift
  3382. ;;
  3383. --no-relations)
  3384. export no_relations=true
  3385. ;;
  3386. --without-relation)
  3387. read-0 value
  3388. without_relations["$value"]=1
  3389. shift
  3390. ;;
  3391. --no-hooks)
  3392. export no_hooks=true
  3393. ;;
  3394. --no-init)
  3395. export no_init=true
  3396. ;;
  3397. --no-post-deploy)
  3398. export no_post_deploy=true
  3399. ;;
  3400. --no-pre-deploy)
  3401. export no_pre_deploy=true
  3402. ;;
  3403. --rebuild-relations-to-service|-R)
  3404. read-0 value
  3405. rebuild_relations_to_service+=("$value")
  3406. shift
  3407. ;;
  3408. --debug|-d)
  3409. export DEBUG=true
  3410. export VERBOSE=true
  3411. #compose_opts+=("--verbose" "--log-level" "DEBUG")
  3412. ;;
  3413. --add-compose-content|-Y)
  3414. read-0 value
  3415. compose_contents+=("$value")
  3416. shift
  3417. ;;
  3418. --dirs)
  3419. echo "CACHEDIR: $CACHEDIR"
  3420. echo "VARDIR: $VARDIR"
  3421. exit 0
  3422. ;;
  3423. --dry-compose-run)
  3424. export DRY_COMPOSE_RUN=true
  3425. ;;
  3426. --*|-*)
  3427. if str_pattern_matches "$arg" $DC_MATCH_MULTI; then
  3428. read-0 value
  3429. compose_opts+=("$arg" "$value")
  3430. shift;
  3431. elif str_pattern_matches "$arg" $DC_MATCH_SINGLE; then
  3432. compose_opts+=("$arg")
  3433. else
  3434. err "Unknown option '$arg'. Please check help:"
  3435. display_help >&2
  3436. exit 1
  3437. fi
  3438. ;;
  3439. *)
  3440. action="$arg"
  3441. stage="action"
  3442. if DC_USAGE=$(get_docker_compose_usage "$action"); then
  3443. is_docker_compose_action=true
  3444. DC_MATCH_MULTI=$(get_docker_compose_multi_opts_list "$action") &&
  3445. DC_MATCH_SINGLE="$(get_docker_compose_single_opts_list "$action")"
  3446. if [ "$DC_MATCH_MULTI" ]; then
  3447. DC_MATCH_SINGLE="$DC_MATCH_SINGLE $(echo "$DC_MATCH_MULTI" | sed -r 's/( |$)/=\* /g')"
  3448. fi
  3449. pos_args=($(echo "$DC_USAGE" | sed -r 's/\[-[^]]+\] ?//g;s/\[options\] ?//g'))
  3450. pos_args=("${pos_args[@]:1}")
  3451. # echo "USAGE: $DC_USAGE"
  3452. # echo "pos_args: ${pos_args[@]}"
  3453. # echo "MULTI: $DC_MATCH_MULTI"
  3454. # echo "SINGLE: $DC_MATCH_SINGLE"
  3455. # exit 1
  3456. else
  3457. stage="remainder"
  3458. fi
  3459. ;;
  3460. esac
  3461. ;;
  3462. "action") ## Only for docker-compose actions
  3463. case "$arg" in
  3464. --help|-h)
  3465. no_init=true ; no_hooks=true ; no_relations=true
  3466. action_opts+=("$arg")
  3467. ;;
  3468. --*|-*)
  3469. if [ "$is_docker_compose_action" ]; then
  3470. if str_pattern_matches "$arg" $DC_MATCH_MULTI; then
  3471. read-0 value
  3472. action_opts+=("$arg" "$value")
  3473. shift
  3474. elif str_pattern_matches "$arg" $DC_MATCH_SINGLE; then
  3475. action_opts+=("$arg")
  3476. else
  3477. err "Unknown option '$arg'. Please check '${DARKCYAN}$action${NORMAL}' help:"
  3478. docker-compose "$action" --help |
  3479. filter_docker_compose_help_message >&2
  3480. exit 1
  3481. fi
  3482. fi
  3483. ;;
  3484. *)
  3485. # echo "LOOP $1 : pos_arg: $pos_arg_ct // ${pos_args[$pos_arg_ct]}"
  3486. if [[ "${pos_args[$pos_arg_ct]}" == "[SERVICE...]" ]]; then
  3487. services_args+=("$arg")
  3488. elif [[ "${pos_args[$pos_arg_ct]}" == "SERVICE" ]]; then
  3489. services_args=("$arg") || exit 1
  3490. stage="remainder"
  3491. else
  3492. action_posargs+=("$arg")
  3493. ((pos_arg_ct++))
  3494. fi
  3495. ;;
  3496. esac
  3497. ;;
  3498. "remainder")
  3499. remainder_args+=("$arg")
  3500. while read-0 arg; do
  3501. remainder_args+=("$arg")
  3502. done
  3503. break 3
  3504. ;;
  3505. esac
  3506. shift
  3507. done < <(cla.normalize "$@")
  3508. export compose_contents
  3509. [ "${services_args[*]}" ] && debug " ${DARKWHITE}Services:$NORMAL ${DARKYELLOW}${services_args[*]}$NORMAL"
  3510. [ "${compose_opts[*]}" ] && debug " ${DARKWHITE}Main docker-compose opts:$NORMAL ${compose_opts[*]}"
  3511. [ "${action_posargs[*]}" ] && debug " ${DARKWHITE}Main docker-compose pos args:$NORMAL ${action_posargs[*]}"
  3512. [ "${action_opts[*]}" ] && debug " ${DARKWHITE}Action $DARKCYAN$action$NORMAL with opts:$NORMAL ${action_opts[*]}"
  3513. [ "${remainder_args[*]}" ] && debug " ${DARKWHITE}Remainder args:$NORMAL ${remainder_args[*]}"
  3514. aexport remainder_args
  3515. ##
  3516. ## Actual code
  3517. ##
  3518. COMPOSE_YML_FILE=$(get_compose_yml_location) || exit 1
  3519. COMPOSE_YML_CONTENT=$(get_compose_yml_content) || exit 1
  3520. export COMPOSE_YML_FILE COMPOSE_YML_CONTENT
  3521. charm.sanity_checks || die "Sanity checks about charm-store failed. Please correct."
  3522. ##
  3523. ## Get services in command line.
  3524. ##
  3525. if [ -z "$is_docker_compose_action" -a "$action" ]; then
  3526. action_service=${remainder_args[0]}
  3527. if [ -z "$action_service" ]; then
  3528. err "No such command or action: ${DARKCYAN}$action${NORMAL}"
  3529. display_commands_help
  3530. exit 1
  3531. fi
  3532. remainder_args=("${remainder_args[@]:1}")
  3533. if has_service_action "$action_service" "$action" >/dev/null; then
  3534. is_service_action=true
  3535. {
  3536. read-0 action_type
  3537. case "$action_type" in
  3538. "relation")
  3539. read-0 _ target_service _target_charm relation_name
  3540. debug "Found action $DARKYELLOW${action_service}$NORMAL/$DARKBLUE$relation_name$NORMAL/$DARKCYAN$action$NORMAL (in $DARKYELLOW$target_service$NORMAL)"
  3541. ;;
  3542. "direct")
  3543. debug "Found action $DARKYELLOW${action_service}$NORMAL.$DARKCYAN$action$NORMAL"
  3544. ;;
  3545. esac
  3546. } < <(has_service_action "$action_service" "$action")
  3547. services_args=("$action_service")
  3548. ## Divert logging to stdout to stderr
  3549. log () { cat >&2; }
  3550. export -f log
  3551. else
  3552. die "Unknown action '${DARKCYAN}$action$NORMAL': It doesn't match any docker-compose commands nor inner charm actions."
  3553. fi
  3554. else
  3555. case "$action" in
  3556. ps|up)
  3557. if [ "${#services_args[@]}" == 0 ]; then
  3558. array_read-0 services_args < <(printf "%s" "$COMPOSE_YML_CONTENT" | shyaml keys-0 2>/dev/null)
  3559. fi
  3560. ;;
  3561. config)
  3562. services_args=("${action_posargs[@]}")
  3563. ;;
  3564. esac
  3565. fi
  3566. NO_CONSTRAINT_CHECK=True
  3567. case "$action" in
  3568. up)
  3569. NO_CONSTRAINT_CHECK=
  3570. ;;
  3571. esac
  3572. get_all_relations "${services_args[@]}" >/dev/null || exit 1
  3573. if [ "$is_docker_compose_action" -a "${#services_args[@]}" -gt 0 ]; then
  3574. services=($(get_master_services "${services_args[@]}")) || exit 1
  3575. if [ "$action" == "up" ]; then
  3576. declare -A seen
  3577. for service in $(get_ordered_service_dependencies "${services_args[@]}"); do
  3578. mservice=$(get_master_service_for_service "$service")
  3579. [ "${seen[$mservice]}" ] && continue
  3580. type="$(get_service_type "$mservice")" || exit 1
  3581. ## remove run-once
  3582. [ "$type" == "run-once" ] && continue
  3583. seen[$mservice]=1
  3584. action_posargs+=("$mservice")
  3585. done
  3586. else
  3587. action_posargs+=("${services[@]}")
  3588. fi
  3589. ## Get rid of subordinates
  3590. action_posargs=($(get_master_services "${action_posargs[@]}")) || exit 1
  3591. fi
  3592. get_docker_compose "${services_args[@]}" >/dev/null || { ## precalculate variable \$_current_docker_compose
  3593. err "Fails to compile base 'docker-compose.yml'"
  3594. exit 1
  3595. }
  3596. ##
  3597. ## Pre-action
  3598. ##
  3599. full_init=
  3600. case "$action" in
  3601. up|run)
  3602. full_init=true
  3603. post_hook=true
  3604. ;;
  3605. ""|down|restart|logs|config|ps)
  3606. full_init=
  3607. ;;
  3608. *)
  3609. if [ "$is_service_action" ]; then
  3610. full_init=true
  3611. fi
  3612. ;;
  3613. esac
  3614. if [ -n "$full_init" ]; then
  3615. ## init in order
  3616. if [[ -z "$no_init" && -z "$no_hooks" ]]; then
  3617. Section setup host resources
  3618. setup_host_resources "${services_args[@]}" || exit 1
  3619. Section initialisation
  3620. run_service_hook init "${services_args[@]}" || exit 1
  3621. fi
  3622. ## Get relations
  3623. if [[ -z "$no_relations" && -z "$no_hooks" ]]; then
  3624. if [ "${#rebuild_relations_to_service[@]}" != 0 ]; then
  3625. rebuild_relations_to_service=$(get_master_services "${rebuild_relations_to_service[@]}") || return 1
  3626. rebuild_relations_to_service=($rebuild_relations_to_service)
  3627. project=$(get_default_project_name) || return 1
  3628. for service in "${rebuild_relations_to_service[@]}"; do
  3629. for dir in "$VARDIR/relations/$project/"*"-${service}/"*; do
  3630. [ -d "$dir" ] && {
  3631. debug rm -rf "$dir"
  3632. rm -rf "$dir"
  3633. }
  3634. done
  3635. done
  3636. fi
  3637. run_service_relations "${services_args[@]}" || exit 1
  3638. fi
  3639. if [[ -z "$no_pre_deploy" && -z "$no_hooks" ]]; then
  3640. run_service_hook pre_deploy "${services_args[@]}" || exit 1
  3641. fi
  3642. fi | log
  3643. if [ "$action" == "run" -a "${#services_args}" != 0 ]; then
  3644. charm=$(get_service_charm "${services_args[0]}") || exit 1
  3645. metadata=$(charm.metadata "$charm") || exit 1
  3646. SERVICE_TYPE="$(printf "%s" "$metadata" | shyaml get-value type 2>/dev/null)" || true
  3647. if [ "$SERVICE_TYPE" == "run-once" ]; then
  3648. run_service_hook dc-pre-run "${services_args[@]}" || exit 1
  3649. fi
  3650. fi
  3651. export SERVICE_PACK="${services_args[*]}"
  3652. ##
  3653. ## Docker-compose
  3654. ##
  3655. errlvl="0"
  3656. case "$action" in
  3657. up|start|stop|build|run)
  3658. ## force daemon mode for up
  3659. if [[ "$action" == "up" ]]; then
  3660. if ! array_member action_opts -d; then
  3661. action_opts+=("-d")
  3662. fi
  3663. if ! array_member action_opts --remove-orphans; then
  3664. action_opts+=("--remove-orphans")
  3665. fi
  3666. fi
  3667. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "${action_posargs[@]}" "${remainder_args[@]}"
  3668. ;;
  3669. logs)
  3670. if ! array_member action_opts --tail; then ## force daemon mode for up
  3671. action_opts+=("--tail" "10")
  3672. fi
  3673. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "${action_posargs[@]}" "${remainder_args[@]}"
  3674. ;;
  3675. "")
  3676. launch_docker_compose "${compose_opts[@]}"
  3677. ;;
  3678. graph)
  3679. graph $SERVICE_PACK
  3680. ;;
  3681. config)
  3682. ## removing the services
  3683. services=($(get_master_services "${action_posargs[@]}")) || exit 1
  3684. ## forcing docker-compose config to output the config file to stdout and not stderr
  3685. out=$(launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "${remainder_args[@]}" 2>&1) || {
  3686. echo "$out"
  3687. exit 1
  3688. }
  3689. echo "$out"
  3690. warn "Runtime configuration modification (from relations) are not included here."
  3691. ;;
  3692. down)
  3693. if ! array_member action_opts --remove-orphans; then ## force daemon mode for up
  3694. debug "Adding a default argument of '--remove-orphans'"
  3695. action_opts+=("--remove-orphans")
  3696. fi
  3697. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "${remainder_args[@]}"
  3698. ;;
  3699. *)
  3700. if [ "$is_service_action" ]; then
  3701. run_service_action "$SERVICE_PACK" "$action" "${remainder_args[@]}"
  3702. errlvl="$?"
  3703. else
  3704. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "${action_posargs[@]}" "${remainder_args[@]}"
  3705. fi
  3706. ;;
  3707. esac || exit 1
  3708. if [ "$post_hook" -a "${#services_args[@]}" != 0 -a -z "$no_hooks" -a -z "$no_post_deploy" ]; then
  3709. run_service_hook post_deploy "${services_args[@]}" || exit 1
  3710. fi
  3711. if [ "$action" == "run" -a "${#services_args}" != 0 ]; then
  3712. if [ "$SERVICE_TYPE" == "run-once" ]; then
  3713. run_service_hook dc-post-run "${services_args[@]}" || exit 1
  3714. fi
  3715. fi
  3716. clean_unused_docker_compose || exit 1
  3717. exit "$errlvl"