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.

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