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.

1798 lines
58 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. #!/bin/bash
  2. #:-
  3. . /etc/shlib
  4. #:-
  5. include pretty
  6. include parse
  7. depends shyaml docker
  8. [[ "${BASH_SOURCE[0]}" != "${0}" ]] && SOURCED=true
  9. export VARDIR=/var/lib/compose
  10. usage="$exname CHARM"'
  11. Deploy and manage a swarm of containers to provide services based on
  12. a ``compose.yml`` definition and charms from a ``charm-store``.
  13. '
  14. export DEFAULT_COMPOSE_FILE
  15. ##
  16. ## Merge YAML files
  17. ##
  18. _merge_yaml_common_code="
  19. import sys
  20. import yaml
  21. try:
  22. # included in standard lib from Python 2.7
  23. from collections import OrderedDict
  24. except ImportError:
  25. # try importing the backported drop-in replacement
  26. # it's available on PyPI
  27. from ordereddict import OrderedDict
  28. ## Ensure that there are no collision with legacy OrderedDict
  29. ## that could be used for omap for instance.
  30. class MyOrderedDict(OrderedDict):
  31. pass
  32. yaml.add_representer(
  33. MyOrderedDict,
  34. lambda cls, data: cls.represent_dict(data.items()))
  35. yaml.add_constructor(
  36. yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
  37. lambda cls, node: MyOrderedDict(cls.construct_pairs(node)))
  38. def fc(filename):
  39. with open(filename) as f:
  40. return f.read()
  41. def merge(*args):
  42. # sys.stderr.write('%r\n' % (args, ))
  43. args = [arg for arg in args if arg is not None]
  44. if len(args) == 0:
  45. return None
  46. if len(args) == 1:
  47. return args[0]
  48. if all(isinstance(arg, (int, basestring, bool)) for arg in args):
  49. return args[-1]
  50. elif all(isinstance(arg, list) for arg in args):
  51. res = []
  52. for arg in args:
  53. res.extend(arg)
  54. return res
  55. elif all(isinstance(arg, dict) for arg in args):
  56. keys = set()
  57. for arg in args:
  58. keys |= set(arg.keys())
  59. dct = {}
  60. for key in keys:
  61. sub_args = []
  62. for arg in args:
  63. if key in arg:
  64. sub_args.append(arg)
  65. try:
  66. dct[key] = merge(*(a[key] for a in sub_args))
  67. except NotImplementedError as e:
  68. raise NotImplementedError(
  69. e.args[0],
  70. '%s.%s' % (key, e.args[1]) if e.args[1] else key,
  71. e.args[2])
  72. if dct[key] is None:
  73. del dct[key]
  74. return dct
  75. else:
  76. raise NotImplementedError(
  77. 'Unsupported types: %s'
  78. % (', '.join(list(set(arg.__class__.__name__ for arg in args)))), '', args)
  79. return None
  80. def merge_cli(*args):
  81. try:
  82. c = merge(*args)
  83. except NotImplementedError as e:
  84. sys.stderr.write('%s. Conflicting key is %r. Values are:\n%s\n' % (e.args[0], e.args[1], e.args[2]))
  85. exit(1)
  86. if c is not None:
  87. print '%s' % yaml.dump(c, default_flow_style=False)
  88. "
  89. merge_yaml() {
  90. if ! [ -r "$state_tmpdir/merge_yaml.py" ]; then
  91. cat <<EOF > "$state_tmpdir/merge_yaml.py"
  92. $_merge_yaml_common_code
  93. merge_cli(*(yaml.load(fc(f)) for f in sys.argv[1:]))
  94. EOF
  95. fi
  96. python "$state_tmpdir/merge_yaml.py" "$@"
  97. }
  98. export -f merge_yaml
  99. merge_yaml_str() {
  100. local entries="$@"
  101. if ! [ -r "$state_tmpdir/merge_yaml_str.py" ]; then
  102. cat <<EOF > "$state_tmpdir/merge_yaml_str.py"
  103. $_merge_yaml_common_code
  104. merge_cli(*(yaml.load(f) for f in sys.argv[1:]))
  105. EOF
  106. fi
  107. python "$state_tmpdir/merge_yaml_str.py" "$@"
  108. }
  109. export -f merge_yaml_str
  110. yaml_key_val_str() {
  111. local entries="$@"
  112. if ! [ -r "$state_tmpdir/yaml_key_val_str.py" ]; then
  113. cat <<EOF > "$state_tmpdir/yaml_key_val_str.py"
  114. $_merge_yaml_common_code
  115. print '%s' % yaml.dump({
  116. yaml.load(sys.argv[1]):
  117. yaml.load(sys.argv[2])}, default_flow_style=False)
  118. EOF
  119. fi
  120. python "$state_tmpdir/yaml_key_val_str.py" "$@"
  121. }
  122. export -f yaml_key_val_str
  123. ##
  124. ## Functions
  125. ##
  126. docker_has_image() {
  127. local image="$1"
  128. images=$(docker images -q "$image" 2>/dev/null) || {
  129. err "docker images call has failled unexpectedly."
  130. return 1
  131. }
  132. [ "$images" ]
  133. }
  134. export -f docker_has_image
  135. gen_password() {
  136. python -c 'import random; \
  137. xx = "azertyuiopqsdfghjklmwxcvbn1234567890AZERTYUIOPQSDFGHJKLMWXCVBN+_-"; \
  138. print "".join([xx[random.randint(0, len(xx)-1)] for x in range(0, 14)])'
  139. }
  140. export -f gen_password
  141. file_put() {
  142. local TARGET="$1"
  143. mkdir -p "$(dirname "$TARGET")" &&
  144. cat - > "$TARGET"
  145. }
  146. export -f file_put
  147. _get_docker_compose_links() {
  148. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  149. links charm charm_part master_charm
  150. if [ -z "$service" ]; then
  151. print_syntax_error "$FUNCNAME: Please specify a service as first argument."
  152. return 1
  153. fi
  154. if [ -e "$cache_file" ]; then
  155. # debug "$FUNCNAME: cache hit ($*)"
  156. cat "$cache_file"
  157. return 0
  158. fi
  159. master_charm=$(_get_top_master_charm_for_service "$service") || return 1
  160. deps=()
  161. while read-0 relation_name target_service relation_config tech_dep; do
  162. master_target_charm="$(_get_top_master_charm_for_service "$target_service")"
  163. [ "$master_charm" == "$master_target_charm" ] && continue
  164. if [ "$tech_dep" == "reversed" ]; then
  165. deps+=("$(echo -en "$master_target_charm:\n links:\n - $master_charm")")
  166. elif [ "$tech_dep" == "True" ]; then
  167. deps+=("$(echo -en "$master_charm:\n links:\n - $master_target_charm")")
  168. fi
  169. done < <(get_compose_relations "$service") || return 1
  170. merge_yaml_str "${deps[@]}" | tee "$cache_file"
  171. }
  172. ##
  173. ## By Reading the metadata.yml, we create a docker-compose.yml mixin.
  174. ## Some metadata.yml (of subordinates) will indeed modify other
  175. ## services than themselves.
  176. _get_docker_compose_service_mixin() {
  177. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)" links charm charm_part
  178. if [ -z "$service" ]; then
  179. print_syntax_error "$FUNCNAME: Please specify a service as first argument."
  180. return 1
  181. fi
  182. if [ -e "$cache_file" ]; then
  183. # debug "$FUNCNAME: cache hit ($*)"
  184. cat "$cache_file"
  185. return 0
  186. fi
  187. master_charm=$(_get_top_master_charm_for_service "$service") || return 1
  188. ## The compose part
  189. links_yaml=$(_get_docker_compose_links "$service") || return 1
  190. ## the charm part
  191. #debug "Get charm name from service name $DARKYELLOW$service$NORMAL."
  192. charm=$(get_service_charm "$service") || return 1
  193. charm_part=$(get_docker_compose_mixin_from_metadata "$charm") || return 1
  194. ## Merge results
  195. if [ "$charm_part" ]; then
  196. charm_yaml="$(yaml_key_val_str "$master_charm" "$charm_part")" || return 1
  197. merge_yaml_str "$links_yaml" "$charm_yaml"
  198. else
  199. echo "$links_yaml"
  200. fi > "$cache_file"
  201. cat "$cache_file"
  202. }
  203. export -f _get_docker_compose_service_mixin
  204. ##
  205. ## Get full `docker-compose.yml` format for all listed services (and
  206. ## their deps)
  207. ##
  208. ## @export
  209. ## @cache: !system !nofail +stdout
  210. get_docker_compose () {
  211. local cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)" entries services service
  212. if [ -e "$cache_file" ]; then
  213. # debug "$FUNCNAME: cache hit ($*)"
  214. cat "$cache_file"
  215. return 0
  216. fi
  217. ##
  218. ## Adding sub services configurations
  219. ##
  220. declare -A entries
  221. debug "Compiling 'docker-compose.conf' for $DARKYELLOW$@$NORMAL..."
  222. for target_service in "$@"; do
  223. services=$(get_ordered_service_dependencies "$target_service") || return 1
  224. debug "$DARKYELLOW$target_service$NORMAL deps:$DARKYELLOW" $services "$NORMAL"
  225. for service in $services; do
  226. if [ "${entries[$service]}" ]; then
  227. ## Prevent double inclusion of same service if this
  228. ## service is deps of two or more of your
  229. ## requirements.
  230. continue
  231. fi
  232. ## mark the service as "loaded" as well as it's containers
  233. ## if this is a subordinate service
  234. entries[$service]=$(_get_docker_compose_service_mixin "$service") || return 1
  235. done
  236. done
  237. merge_yaml_str "${entries[@]}" > "$cache_file"
  238. export _current_docker_compose="$(cat "$cache_file")"
  239. echo "$_current_docker_compose"
  240. debug " ... Compilation of base 'docker-compose.conf' done." || true
  241. # debug " ** ${WHITE}docker-compose.conf${NORMAL}:"
  242. # debug "$_current_docker_compose"
  243. }
  244. export -f get_docker_compose
  245. ## XXXvlab: a lot to be done to cache the results
  246. get_compose_service_def () {
  247. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  248. if [ -e "$cache_file" ]; then
  249. # debug "$FUNCNAME: cache hit ($*)"
  250. cat "$cache_file"
  251. return 0
  252. fi
  253. [ -z "$service" ] && print_syntax_error "Missing service as first argument."
  254. service_def_base=
  255. if [ -d "$CHARM_STORE/$service" ]; then
  256. service_def_base="charm: $service"
  257. fi
  258. value=
  259. if [ -r "$COMPOSE_YML_FILE" ]; then
  260. value=$(shyaml get-value "$service" 2>/dev/null < "$COMPOSE_YML_FILE")
  261. fi
  262. if [ -z "$service_def_base" -a -z "$value" ]; then
  263. err "Invalid service $DARKYELLOW$service$NORMAL: no definition in" \
  264. "compose file nor charm with same name."
  265. return 1
  266. fi
  267. merge_yaml <(echo "$service_def_base") <(echo "$value") > "$cache_file"
  268. cat "$cache_file"
  269. }
  270. export -f get_compose_service_def
  271. get_service_charm () {
  272. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  273. if [ -e "$cache_file" ]; then
  274. # debug "$FUNCNAME: cache hit ($*)"
  275. cat "$cache_file"
  276. return 0
  277. fi
  278. if [ -z "$service" ]; then
  279. print_syntax_error "$FUNCNAME: Please specify a service as first argument."
  280. return 1
  281. fi
  282. service_def=$(get_compose_service_def "$service") || return 1
  283. charm=$(echo "$service_def" | shyaml get-value charm 2>/dev/null)
  284. if [ -z "$charm" ]; then
  285. err "Missing charm in service $DARKYELLOW$service$NORMAL definition."
  286. return 1
  287. fi
  288. echo "$charm" | tee "$cache_file"
  289. }
  290. export -f get_service_charm
  291. ## built above the docker-compose abstraction, so it relies on the
  292. ## full docker-compose.yml to be already built.
  293. get_service_def () {
  294. local service="$1" def
  295. if [ -z "$_current_docker_compose" ]; then
  296. print_syntax_error "$FUNCNAME is meant to be called after"\
  297. "\$_current_docker_compose has been calculated."
  298. fi
  299. def=$(echo "$_current_docker_compose" | shyaml get-value "$service" 2>/dev/null)
  300. if [ -z "$def" ]; then
  301. err "No definition for service $DARKYELLOW$service$NORMAL in compiled 'docker-compose.conf'."
  302. return 1
  303. fi
  304. echo "$def"
  305. }
  306. export -f get_service_def
  307. ## Return the base docker image name of a service
  308. service_base_docker_image() {
  309. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  310. master_charm charm service_image service_build service_dockerfile
  311. if [ -e "$cache_file" ]; then
  312. # debug "$FUNCNAME: cache hit ($*)"
  313. cat "$cache_file"
  314. return 0
  315. fi
  316. master_charm="$(_get_top_master_charm_for_service "$service")" || {
  317. err "Could not compute base charm for service $DARKYELLOW$service$NORMAL."
  318. return 1
  319. }
  320. service_def="$(get_service_def "$master_charm")" || {
  321. err "Could not get docker-compose service definition for $DARKYELLOW$master_charm$NORMAL."
  322. return 1
  323. }
  324. service_image=$(echo "$service_def" | shyaml get-value image 2>/dev/null)
  325. if [ "$?" != 0 ]; then
  326. service_build=$(echo "$service_def" | shyaml get-value build 2>/dev/null)
  327. if [ "$?" != 0 ]; then
  328. err "Service $DARKYELLOW$service$NORMAL has no ${WHITE}image${NORMAL} nor ${WHITE}build${NORMAL} parameter."
  329. echo "$service_def" >&2
  330. return 1
  331. fi
  332. service_dockerfile="$CHARM_STORE/${service_build}/Dockerfile"
  333. if ! [ -e "$service_dockerfile" ]; then
  334. err "No Dockerfile found in '$service_dockerfile' location."
  335. return 1
  336. fi
  337. grep '^FROM' "$service_dockerfile" | xargs echo | cut -f 2 -d " "
  338. else
  339. echo "$service_image"
  340. fi | tee "$cache_file"
  341. }
  342. export -f service_base_docker_image
  343. ## XXXvlab: provided in shlib/common
  344. # read-0() {
  345. # local eof
  346. # eof=
  347. # while [ "$1" ]; do
  348. # IFS='' read -r -d '' "$1" || eof=true
  349. # shift
  350. # done
  351. # test "$eof" != true
  352. # }
  353. # export -f read-0
  354. fetch_file() {
  355. local src="$1"
  356. case "$src" in
  357. *"://"*)
  358. err "Unsupported target scheme."
  359. return 1
  360. ;;
  361. *)
  362. ## Try direct
  363. if ! [ -r "$src" ]; then
  364. err "File '$src' not found/readable."
  365. return 1
  366. fi
  367. cat "$src" || return 1
  368. ;;
  369. esac
  370. }
  371. export -f fetch_file
  372. ## receives stdin content to decompress on stdout
  373. ## stdout content should be tar format.
  374. uncompress_file() {
  375. local filename="$1"
  376. ## Warning, the content of the file is already as stdin, the filename
  377. ## is there to hint for correct decompression.
  378. case "$filename" in
  379. *".gz")
  380. gunzip
  381. ;;
  382. *".bz2")
  383. bunzip2
  384. ;;
  385. *)
  386. cat
  387. ;;
  388. esac
  389. }
  390. export -f uncompress_file
  391. get_file() {
  392. local src="$1"
  393. fetch_file "$src" | uncompress_file "$src"
  394. }
  395. export -f get_file
  396. cmd_on_base_image() {
  397. local charm="$1"
  398. shift
  399. base_image=$(service_base_docker_image "$charm") || return 1
  400. docker run -i --entrypoint /bin/bash "$base_image" -c "$*"
  401. }
  402. export -f cmd_on_base_image
  403. cached_cmd_on_base_image() {
  404. local charm="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  405. shift
  406. if [ -e "$cache_file" ]; then
  407. # debug "$FUNCNAME: cache hit ($*)"
  408. cat "$cache_file"
  409. return 0
  410. fi
  411. base_image=$(service_base_docker_image "$charm") || return 1
  412. result=$(cmd_on_base_image "$charm" "$@") || return 1
  413. echo "$result" | tee "$cache_file"
  414. }
  415. export -f cached_cmd_on_base_image
  416. array_values_to_stdin() {
  417. local e
  418. if [ "$#" -ne "1" ]; then
  419. print_syntax_warning "$FUNCNAME: need one argument."
  420. return 1
  421. fi
  422. var="$1"
  423. eval "for e in \"\${$var[@]}\"; do echo -en \"\$e\\0\"; done"
  424. }
  425. array_keys_to_stdin() {
  426. local e
  427. if [ "$#" -ne "1" ]; then
  428. print_syntax_warning "$FUNCNAME: need one argument."
  429. return 1
  430. fi
  431. var="$1"
  432. eval "for e in \"\${!$var[@]}\"; do echo -en \"\$e\\0\"; done"
  433. }
  434. array_kv_to_stdin() {
  435. local e
  436. if [ "$#" -ne "1" ]; then
  437. print_syntax_warning "$FUNCNAME: need one argument."
  438. return 1
  439. fi
  440. var="$1"
  441. eval "for e in \"\${!$var[@]}\"; do echo -n \"\$e\"; echo -en '\0'; echo -n \"\${$var[\$e]}\"; echo -en '\0'; done"
  442. }
  443. array_pop() {
  444. local narr="$1" nres="$2"
  445. for key in $(eval "echo \${!$narr[@]}"); do
  446. eval "$nres=\${$narr[\"\$key\"]}"
  447. eval "unset $narr[\"\$key\"]"
  448. return 0
  449. done
  450. }
  451. export -f array_pop
  452. array_member() {
  453. local src elt
  454. src="$1"
  455. elt="$2"
  456. while read-0 key; do
  457. if [ "$(eval "echo -n \"\${$src[\$key]}\"")" == "$elt" ]; then
  458. return 0
  459. fi
  460. done < <(array_keys_to_stdin "$src")
  461. return 1
  462. }
  463. export -f array_member
  464. get_charm_relation_def () {
  465. local charm="$1" relation_name="$2" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  466. relation_def metadata
  467. if [ -e "$cache_file" ]; then
  468. # debug "$FUNCNAME: cache hit ($*)"
  469. cat "$cache_file"
  470. return 0
  471. fi
  472. metadata="$(get_charm_metadata "$charm")" || return 1
  473. relation_def="$(echo "$metadata" | shyaml get-value "provides.${relation_name}" 2>/dev/null)"
  474. echo "$relation_def" | tee "$cache_file"
  475. }
  476. export -f get_charm_relation_def
  477. get_charm_tech_dep_orientation_for_relation() {
  478. local charm="$1" relation_name="$2" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  479. relation_def metadata value
  480. if [ -e "$cache_file" ]; then
  481. # debug "$FUNCNAME: cache hit ($*)"
  482. cat "$cache_file"
  483. return 0
  484. fi
  485. relation_def=$(get_charm_relation_def "$charm" "$relation_name" 2>/dev/null)
  486. value=$(echo "$relation_def" | shyaml get-value 'tech-dep' 2>/dev/null)
  487. value=${value:-True}
  488. echo "$value" | tee "$cache_file"
  489. }
  490. export -f get_charm_tech_dep_orientation_for_relation
  491. ##
  492. ## Use compose file to get deps, and relation definition in metadata.yml
  493. ## for tech-dep attribute.
  494. get_service_deps() {
  495. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  496. if [ -e "$cache_file" ]; then
  497. # debug "$FUNCNAME: cache hit ($*)"
  498. cat "$cache_file"
  499. return 0
  500. fi
  501. (
  502. set -o pipefail
  503. get_compose_relations "$service" | \
  504. while read-0 relation_name target_service _relation_config tech_dep; do
  505. echo "$target_service"
  506. done | tee "$cache_file"
  507. ) || return 1
  508. }
  509. export -f get_service_deps
  510. _rec_get_depth() {
  511. local elt=$1
  512. #debug "Asking for $DARKYELLOW$elt$NORMAL dependencies"
  513. if [ "${depths[$elt]}" ]; then
  514. return 0
  515. fi
  516. deps=$(get_service_deps "$elt") || return 1
  517. if [ -z "$deps" ]; then
  518. depths[$elt]=0
  519. fi
  520. max=0
  521. for dep in $deps; do
  522. _rec_get_depth "$dep" || return 1
  523. if (( "${depths[$dep]}" > "$max" )); then
  524. max="${depths[$dep]}"
  525. fi
  526. done
  527. depths[$elt]=$((max + 1))
  528. }
  529. export -f _rec_get_depth
  530. get_ordered_service_dependencies() {
  531. local services=("$@") cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  532. if [ -e "$cache_file" ]; then
  533. # debug "$FUNCNAME: cache hit ($*)"
  534. cat "$cache_file"
  535. return 0
  536. fi
  537. #debug "Figuring ordered deps of $DARKYELLOW$services$NORMAL"
  538. if [ -z "${services[*]}" ]; then
  539. print_syntax_error "$FUNCNAME: no arguments"
  540. return 1
  541. fi
  542. declare -A depths
  543. visited=()
  544. heads=("${services[@]}")
  545. while [ "${#heads[@]}" != 0 ]; do
  546. array_pop heads head
  547. visited+=("$head")
  548. _rec_get_depth "$head" || return 1
  549. done
  550. i=0
  551. while [ "${#depths[@]}" != 0 ]; do
  552. for key in "${!depths[@]}"; do
  553. value="${depths[$key]}"
  554. if [ "$value" == "$i" ]; then
  555. echo "$key"
  556. unset depths[$key]
  557. fi
  558. done
  559. i=$((i + 1))
  560. done > "$cache_file"
  561. cat "$cache_file"
  562. }
  563. export -f get_ordered_service_dependencies
  564. run_service_hook () {
  565. local services="$1" action="$2" loaded
  566. declare -A loaded
  567. for service in $services; do
  568. for subservice in $(get_ordered_service_dependencies "$service"); do
  569. if [ "${loaded[$subservice]}" ]; then
  570. ## Prevent double inclusion of same service if this
  571. ## service is deps of two or more of your
  572. ## requirements.
  573. continue
  574. fi
  575. charm=$(get_service_charm "$subservice") || return 1
  576. TARGET_SCRIPT="$CHARM_STORE/$charm/hooks/$action"
  577. [ -e "$TARGET_SCRIPT" ] || continue
  578. PROJECT_NAME=$(get_default_project_name) || return 1
  579. Wrap -d "$YELLOW$action$NORMAL hook of charm $DARKYELLOW$charm$NORMAL" <<EOF || return 1
  580. cd "$CHARM_STORE/$charm"
  581. SERVICE_NAME=$subservice \
  582. PROJECT_NAME=$PROJECT_NAME \
  583. DOCKER_BASE_IMAGE=$(service_base_docker_image "$charm") \
  584. SERVICE_DATASTORE="$DATASTORE/$charm" \
  585. SERVICE_CONFIGSTORE="$CONFIGSTORE/$charm" \
  586. "$TARGET_SCRIPT"
  587. EOF
  588. loaded[$subservice]=1
  589. done
  590. done
  591. return 0
  592. }
  593. relation-get () {
  594. local key="$1"
  595. cat "$RELATION_DATA_FILE" | shyaml get-value "$key" 2>/dev/null
  596. if [ "$?" != 0 ]; then
  597. err "The key $WHITE$key$NORMAL was not found in relation's data."
  598. return 1
  599. fi
  600. }
  601. export -f relation-get
  602. relation-base-compose-get () {
  603. local key="$1"
  604. echo "$RELATION_BASE_COMPOSE_DEF" | shyaml get-value "options.$key" 2>/dev/null
  605. if [ "$?" != 0 ]; then
  606. err "The key $WHITE$key$NORMAL was not found in base service compose definition.."
  607. return 1
  608. fi
  609. }
  610. export -f relation-base-compose-get
  611. relation-target-compose-get () {
  612. local key="$1"
  613. echo "$RELATION_BASE_COMPOSE_DEF" | shyaml get-value "options.$key" 2>/dev/null
  614. if [ "$?" != 0 ]; then
  615. err "The key $WHITE$key$NORMAL was not found in base service compose definition.."
  616. return 1
  617. fi
  618. }
  619. export -f relation-base-compose-get
  620. relation-set () {
  621. local key="$1" value="$2"
  622. if [ -z "$RELATION_DATA_FILE" ]; then
  623. err "$FUNCNAME: relation does not seems to be correctly setup."
  624. return 1
  625. fi
  626. if ! [ -r "$RELATION_DATA_FILE" ]; then
  627. err "$FUNCNAME: can't read relation's data." >&2
  628. return 1
  629. fi
  630. _config_merge "$RELATION_DATA_FILE" <(echo "$key: $value")
  631. }
  632. export -f relation-set
  633. _config_merge() {
  634. local config_filename="$1" merge_to_file="$2"
  635. touch "$config_filename" &&
  636. merge_yaml "$config_filename" "$merge_to_file" > "$config_filename.tmp" || return 1
  637. mv "$config_filename.tmp" "$config_filename"
  638. }
  639. export -f _config_merge
  640. ## XXXvlab; this can be used only in relation, I'd like to use it in init.
  641. config-add() {
  642. local metadata="$1"
  643. _config_merge "$RELATION_CONFIG" <(echo "$metadata")
  644. }
  645. export -f config-add
  646. ## XXXvlab; this can be used only in relation, I'd like to use it in init.
  647. init-config-add() {
  648. local metadata="$1"
  649. _config_merge "$state_tmpdir/to-merge-in-docker-compose.yml" <(echo "$metadata")
  650. }
  651. export -f init-config-add
  652. logstdout() {
  653. local name="$1"
  654. sed -r 's%^%'"${name}"'> %g'
  655. }
  656. export -f logstdout
  657. logstderr() {
  658. local name="$1"
  659. sed -r 's%^(.*)$%'"${RED}${name}>${NORMAL} \1"'%g'
  660. }
  661. export -f logstderr
  662. _run_service_relation () {
  663. local relation_name="$1" service="$2" target_service="$3" relation_config="$4" relation_dir services
  664. charm=$(get_service_charm "$service") || return 1
  665. target_charm=$(get_service_charm "$target_service") || return 1
  666. base_script_name=$(echo "$relation_name" | tr "-" "_" )-relation-joined
  667. script_name="hooks/${base_script_name}"
  668. [ ! -e "$CHARM_STORE/$target_charm/$script_name" -a ! -e "$CHARM_STORE/$charm/$script_name" ] &&
  669. return 0
  670. relation_dir=$(get_relation_data_dir "$charm" "$target_charm" "$relation_name") || return 1
  671. RELATION_DATA_FILE=$(get_relation_data_file "$charm" "$target_charm" "$relation_name" "$relation_config") || return 1
  672. RELATION_BASE_COMPOSE_DEF=$(get_compose_service_def "$service") || return 1
  673. RELATION_TARGET_COMPOSE_DEF=$(get_compose_service_def "$target_service") || return 1
  674. export BASE_SERVICE_NAME=$service
  675. export TARGET_SERVICE_NAME=$target_service
  676. export BASE_CHARM_NAME=$charm
  677. export TARGET_CHARM_NAME=$target_charm
  678. PROJECT_NAME=$(get_default_project_name) || return 1
  679. MASTER_BASE_CHARM_NAME=$(_get_top_master_charm_for_service "$service") || return 1
  680. MASTER_TARGET_CHARM_NAME=$(_get_top_master_charm_for_service "$target_service") || return 1
  681. export RELATION_DATA_FILE RELATION_BASE_COMPOSE_DEF RELATION_TARGET_COMPOSE_DEF
  682. export MASTER_BASE_CHARM_NAME MASTER_TARGET_CHARM_NAME PROJECT_NAME
  683. target_errlvl=0
  684. if ! [ -e "$CHARM_STORE/$target_charm/$script_name" ]; then
  685. verb "No relation script '$script_name' in $DARKYELLOW$target_charm$NORMAL."
  686. else
  687. verb "Running ${DARKBLUE}$relation_name${NORMAL} relation-joined script" \
  688. "for target charm $DARKYELLOW$target_charm$NORMAL"
  689. RELATION_CONFIG="$relation_dir/config_provider"
  690. DOCKER_BASE_IMAGE=$(service_base_docker_image "$target_service") || return 1
  691. export DOCKER_BASE_IMAGE RELATION_CONFIG RELATION_DATA
  692. {
  693. (
  694. cd "$CHARM_STORE/$target_charm"
  695. SERVICE_NAME=$target_service
  696. SERVICE_DATASTORE="$DATASTORE/$target_charm"
  697. SERVICE_CONFIGSTORE="$CONFIGSTORE/$target_charm"
  698. export SERVICE_NAME DOCKER_BASE_IMAGE SERVICE_DATASTORE SERVICE_CONFIGSTORE
  699. "$script_name"
  700. echo "$?" > "$relation_dir/target_errlvl"
  701. ) | logstdout "$DARKYELLOW$target_charm$NORMAL/$base_script_name ${GREEN}@${NORMAL}"
  702. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$target_charm$NORMAL/$base_script_name ${RED}@${NORMAL}" 3>&1 1>&2 2>&3
  703. target_errlvl="$(cat "$relation_dir/target_errlvl")" || {
  704. err "Relation script '$script_name' in $DARKYELLOW$target_charm$NORMAL" \
  705. "failed before outputing an errorlevel."
  706. ((target_errlvl |= "1" ))
  707. }
  708. if [ -e "$RELATION_CONFIG" ]; then
  709. debug "Merging some new config info in $DARKYELLOW$target_service$NORMAL"
  710. _config_merge "$state_tmpdir/to-merge-in-docker-compose.yml" "$RELATION_CONFIG" &&
  711. rm "$RELATION_CONFIG"
  712. ((target_errlvl |= "$?"))
  713. fi
  714. fi
  715. if [ "$target_errlvl" == 0 ]; then
  716. errlvl=0
  717. if [ -e "$CHARM_STORE/$charm/$script_name" ]; then
  718. verb "Running ${DARKBLUE}$relation_name${NORMAL} relation-joined script" \
  719. "for charm $DARKYELLOW$charm$NORMAL"
  720. RELATION_CONFIG="$relation_dir/config_providee"
  721. RELATION_DATA="$(cat "$RELATION_DATA_FILE")"
  722. DOCKER_BASE_IMAGE=$(service_base_docker_image "$service") || return 1
  723. export DOCKER_BASE_IMAGE RELATION_CONFIG RELATION_DATA
  724. {
  725. (
  726. cd "$CHARM_STORE/$charm"
  727. SERVICE_NAME=$service
  728. SERVICE_DATASTORE="$DATASTORE/$charm"
  729. SERVICE_CONFIGSTORE="$CONFIGSTORE/$charm"
  730. export SERVICE_NAME DOCKER_BASE_IMAGE SERVICE_DATASTORE SERVICE_CONFIGSTORE
  731. "$script_name"
  732. echo "$?" > "$relation_dir/errlvl"
  733. ) | logstdout "$DARKYELLOW$charm$NORMAL/$base_script_name ${GREEN}@${NORMAL}"
  734. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$charm$NORMAL/$base_script_name ${RED}@$NORMAL" 3>&1 1>&2 2>&3
  735. errlvl="$(cat "$relation_dir/errlvl")" || {
  736. err "Relation script '$script_name' in $DARKYELLOW$charm$NORMAL" \
  737. "failed before outputing an errorlevel."
  738. ((errlvl |= "1" ))
  739. }
  740. if [ -e "$RELATION_CONFIG" ]; then
  741. _config_merge "$state_tmpdir/to-merge-in-docker-compose.yml" "$RELATION_CONFIG" &&
  742. rm "$RELATION_CONFIG"
  743. ((errlvl |= "$?" ))
  744. fi
  745. if [ "$errlvl" != 0 ]; then
  746. err "Relation $DARKBLUE$relation_name$NORMAL on $DARKYELLOW$charm$NORMAL failed to run properly."
  747. fi
  748. else
  749. verb "No relation script '$script_name' in charm $DARKYELLOW$charm$NORMAL. Ignoring."
  750. fi
  751. else
  752. err "Relation $DARKBLUE$relation_name$NORMAL on $DARKYELLOW$target_charm$NORMAL failed to run properly."
  753. fi
  754. if [ "$target_errlvl" == 0 -a "$errlvl" == 0 ]; then
  755. debug "Relation $DARKBLUE$relation_name$NORMAL is established" \
  756. "between $DARKYELLOW$service$NORMAL and $DARKYELLOW$target_service$NORMAL."
  757. return 0
  758. else
  759. return 1
  760. fi
  761. }
  762. export -f _run_service_relation
  763. get_compose_relations () {
  764. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)" relation_name relation_def
  765. if [ -e "$cache_file" ]; then
  766. # debug "$FUNCNAME: cache hit ($*)"
  767. cat "$cache_file"
  768. return 0
  769. fi
  770. compose_def="$(get_compose_service_def "$service")" || return 1
  771. (
  772. set -o pipefail
  773. if [ "$compose_def" ]; then
  774. while read-0 relation_name relation_def; do
  775. (
  776. case "$(echo "$relation_def" | shyaml get-type 2>/dev/null)" in
  777. "str")
  778. target_service="$(echo "$relation_def" | shyaml get-value 2>/dev/null)"
  779. tech_dep="$(get_charm_tech_dep_orientation_for_relation "$target_service" "$relation_name")"
  780. echo -en "$relation_name\0$target_service\0\0$tech_dep\0"
  781. ;;
  782. "sequence")
  783. while read-0 target_service; do
  784. tech_dep="$(get_charm_tech_dep_orientation_for_relation "$target_service" "$relation_name")"
  785. echo -en "$relation_name\0$target_service\0\0$tech_dep\0"
  786. done < <(echo "$relation_def" | shyaml get-values-0 2>/dev/null)
  787. ;;
  788. "struct")
  789. while read-0 target_service relation_config; do
  790. tech_dep="$(get_charm_tech_dep_orientation_for_relation "$target_service" "$relation_name")"
  791. echo -en "$relation_name\0$target_service\0$relation_config\0$tech_dep\0"
  792. done < <(echo "$relation_def" | shyaml key-values-0 2>/dev/null)
  793. ;;
  794. esac
  795. ) </dev/null >> "$cache_file"
  796. done < <(echo "$compose_def" | shyaml key-values-0 relations 2>/dev/null)
  797. fi
  798. )
  799. if [ "$?" != 0 ]; then
  800. err "Error while looking for compose relations."
  801. rm -f "$cache_file" ## no cache
  802. return 1
  803. fi
  804. [ -e "$cache_file" ] && cat "$cache_file"
  805. return 0
  806. }
  807. export -f get_compose_relations
  808. run_service_relations () {
  809. local services="$1" loaded
  810. declare -A loaded
  811. for service in $(get_ordered_service_dependencies $services); do
  812. # debug "Upping dep's relations of ${DARKYELLOW}$service${NORMAL}:"
  813. for subservice in $(get_service_deps "$service") "$service"; do
  814. [ "${loaded[$subservice]}" ] && continue
  815. # debug " Relations of ${DARKYELLOW}$subservice${NORMAL}:"
  816. while read-0 relation_name target_service relation_config tech_dep; do
  817. export relation_config
  818. Wrap -d "Building $DARKYELLOW$subservice$NORMAL --$DARKBLUE$relation_name$NORMAL--> $DARKYELLOW$target_service$NORMAL" <<EOF || return 1
  819. _run_service_relation "$relation_name" "$subservice" "$target_service" "\$relation_config"
  820. EOF
  821. done < <(get_compose_relations "$subservice") || return 1
  822. loaded[$subservice]=1
  823. done
  824. done
  825. }
  826. export -f run_service_relations
  827. _run_service_action_direct() {
  828. local service="$1" action="$2" charm script _dummy
  829. shift; shift
  830. read-0 charm script || true ## against 'set -e' that could be setup in parent scripts
  831. if read-0 _dummy || [ "$_dummy" ]; then
  832. print_syntax_error "$FUNCNAME: too many arguments in action descriptor"
  833. return 1
  834. fi
  835. compose_file=$(get_compose_yml_location) || return 1
  836. export action_errlvl_file="$state_tmpdir/action-$service-$charm-$action-errlvl"
  837. export state_tmpdir
  838. {
  839. (
  840. set +e ## Prevents unwanted leaks from parent shell
  841. cd "$CHARM_STORE/$charm"
  842. export COMPOSE_CONFIG=$(cat "$compose_file")
  843. export METADATA_CONFIG=$(cat "$CHARM_STORE/$charm/metadata.yml")
  844. export SERVICE_NAME=$service
  845. export ACTION_NAME=$action
  846. export CONTAINER_NAME=$(_get_top_master_charm_for_service "$service")
  847. export DOCKER_BASE_IMAGE=$(service_base_docker_image "$CONTAINER_NAME")
  848. export SERVICE_DATASTORE="$DATASTORE/$service"
  849. export SERVICE_CONFIGSTORE="$CONFIGSTORE/$service"
  850. exname="$exname $ACTION_NAME $SERVICE_NAME" \
  851. stdbuf -oL -eL "$script" "$@"
  852. echo "$?" > "$action_errlvl_file"
  853. ) | logstdout "$DARKYELLOW$charm$NORMAL/${DARKCYAN}$action${NORMAL} ${GREEN}@${NORMAL}"
  854. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$charm$NORMAL/${DARKCYAN}$action${NORMAL} ${RED}@$NORMAL" 3>&1 1>&2 2>&3
  855. if ! [ -e "$action_errlvl_file" ]; then
  856. err "Action $DARKYELLOW$service$NORMAL:$DARKCYAN$action$NORMAL has failed without having time" \
  857. "to output an errlvl"
  858. return 1
  859. fi
  860. return "$(cat "$action_errlvl_file")"
  861. }
  862. export -f _run_service_action_direct
  863. _run_service_action_relation() {
  864. local service="$1" action="$2" charm target_charm relation_name target_script relation_config _dummy
  865. shift; shift
  866. read-0 charm target_charm relation_name target_script relation_config || true
  867. if read-0 _dummy || [ "$_dummy" ]; then
  868. print_syntax_error "$FUNCNAME: too many arguments in action descriptor"
  869. return 1
  870. fi
  871. export RELATION_DATA_FILE=$(get_relation_data_file "$charm" "$target_charm" "$relation_name" "$relation_config")
  872. compose_file=$(get_compose_yml_location) || return 1
  873. export action_errlvl_file="$state_tmpdir/action-$service-$charm-$action-errlvl"
  874. export state_tmpdir
  875. {
  876. (
  877. set +e ## Prevents unwanted leaks from parent shell
  878. cd "$CHARM_STORE/$charm"
  879. export METADATA_CONFIG=$(cat "$CHARM_STORE/$charm/metadata.yml")
  880. export SERVICE_NAME=$service
  881. export RELATION_TARGET_CHARM="$target_charm"
  882. export RELATION_CHARM="$charm"
  883. export ACTION_NAME=$action
  884. export CONTAINER_NAME=$(_get_top_master_charm_for_service "$service")
  885. export DOCKER_BASE_IMAGE=$(service_base_docker_image "$CONTAINER_NAME")
  886. export SERVICE_DATASTORE="$DATASTORE/$service"
  887. export SERVICE_CONFIGSTORE="$CONFIGSTORE/$service"
  888. exname="$exname $ACTION_NAME $SERVICE_NAME" \
  889. stdbuf -oL -eL "$target_script" "$@"
  890. echo "$?" > "$action_errlvl_file"
  891. ) | logstdout "$DARKYELLOW$charm$NORMAL/${DARKCYAN}$action${NORMAL} ${GREEN}@${NORMAL}"
  892. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$charm$NORMAL/${DARKCYAN}$action${NORMAL} ${RED}@$NORMAL" 3>&1 1>&2 2>&3
  893. if ! [ -e "$action_errlvl_file" ]; then
  894. err "Action $DARKYELLOW$service$NORMAL:$DARKCYAN$action$NORMAL has failed without having time" \
  895. "to output an errlvl"
  896. return 1
  897. fi
  898. return "$(cat "$action_errlvl_file")"
  899. }
  900. export -f _run_service_action_relation
  901. get_relation_data_dir() {
  902. local charm="$1" target_charm="$2" relation_name="$3" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  903. if [ -e "$cache_file" ]; then
  904. # debug "$FUNCNAME: cache hit ($*)"
  905. cat "$cache_file"
  906. return 0
  907. fi
  908. project=$(get_default_project_name) || return 1
  909. relation_dir="$VARDIR/relations/$project/$charm-$target_charm/$relation_name"
  910. if ! [ -d "$relation_dir" ]; then
  911. mkdir -p "$relation_dir" || return 1
  912. chmod go-rwx "$relation_dir" || return 1 ## protecting this directory
  913. fi
  914. echo "$relation_dir" || tee "$cache_file"
  915. }
  916. export -f get_relation_data_dir
  917. get_relation_data_file() {
  918. local charm="$1" target_charm="$2" relation_name="$3" relation_config="$4"
  919. relation_dir=$(get_relation_data_dir "$charm" "$target_charm" "$relation_name") || return 1
  920. relation_data_file="$relation_dir/data"
  921. new=
  922. if [ -e "$relation_data_file" ]; then
  923. ## Has reference changed ?
  924. new_md5=$(echo "$relation_config" | md5_compat)
  925. if [ "$new_md5" != "$(cat "$relation_data_file.md5_ref" 2>/dev/null)" ]; then
  926. new=true
  927. fi
  928. else
  929. new=true
  930. fi
  931. if [ "$new" ]; then
  932. echo "$relation_config" > "$relation_data_file"
  933. chmod go-rwx "$relation_data_file" ## protecting this file
  934. echo "$relation_config" | md5_compat > "$relation_data_file.md5_ref"
  935. fi
  936. echo "$relation_data_file"
  937. }
  938. export -f get_relation_data_file
  939. has_service_action () {
  940. local service="$1" action="$2" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  941. target_script charm target_charm
  942. if [ -e "$cache_file" ]; then
  943. # debug "$FUNCNAME: cache hit ($*)"
  944. cat "$cache_file"
  945. return 0
  946. fi
  947. charm=$(get_service_charm "$service") || return 1
  948. ## Action directly provided ?
  949. target_script="$CHARM_STORE/$charm/actions/$action"
  950. if [ -x "$target_script" ]; then
  951. echo -en "direct\0$charm\0$target_script" | tee "$cache_file"
  952. return 0
  953. fi
  954. ## Action provided by relation ?
  955. while read-0 relation_name target_service relation_config tech_dep; do
  956. target_charm=$(get_service_charm "$target_service") || return 1
  957. target_script="$CHARM_STORE/$target_charm/actions/relations/$relation_name/$action"
  958. if [ -x "$target_script" ]; then
  959. echo -en "relation\0$charm\0$target_charm\0$relation_name\0$target_script\0$relation_config" | tee "$cache_file"
  960. return 0
  961. fi
  962. done < <(get_compose_relations "$service")
  963. master=$(_get_top_master_charm_for_service "$charm")
  964. [ "$master" == "$charm" ] && return 1
  965. has_service_action "$master" "$action"
  966. }
  967. export -f has_service_action
  968. run_service_action () {
  969. local service="$1" action="$2"
  970. shift ; shift
  971. {
  972. if ! read-0 action_type; then
  973. info "Service $DARKYELLOW$service$NORMAL does not have any action $DARKCYAN$action$NORMAL defined."
  974. info " Add an executable script to '$CHARM_STORE/$charm/actions/$action' to implement action."
  975. return 1
  976. fi
  977. Section "running $DARKYELLOW$service$NORMAL/$DARKCYAN$action$NORMAL ($action_type)"; Feed
  978. "_run_service_action_${action_type}" "$service" "$action" "$@"
  979. } < <(has_service_action "$service" "$action")
  980. }
  981. export -f run_service_action
  982. get_compose_relation_config() {
  983. local service=$1 relation_config cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  984. if [ -e "$cache_file" ]; then
  985. # debug "$FUNCNAME: cache hit ($*)"
  986. cat "$cache_file"
  987. return 0
  988. fi
  989. compose_service_def=$(get_compose_service_def "$service") || return 1
  990. echo "$compose_service_def" | shyaml get-value "relations" 2>/dev/null | tee "$cache_file"
  991. }
  992. export -f get_compose_relation_config
  993. # ## Return key-values-0
  994. # get_compose_relation_config_for_service() {
  995. # local service=$1 relation_name=$2 relation_config
  996. # compose_service_relations=$(get_compose_relation_config "$service") || return 1
  997. # if ! relation_config=$(
  998. # echo "$compose_service_relations" |
  999. # shyaml get-value "${relation_name}" 2>/dev/null); then
  1000. # err "Couldn't find $DARKYELLOW${service}$NORMAL/${WHITE}${relation_name}$NORMAL" \
  1001. # "relation config in compose configuration."
  1002. # return 1
  1003. # fi
  1004. # if [ -z "$relation_config" ]; then
  1005. # err "Relation ${WHITE}mysql-database$NORMAL is empty in compose configuration."
  1006. # return 1
  1007. # fi
  1008. # if ! echo "$relation_config" | shyaml key-values-0 2>/dev/null; then
  1009. # err "No key/values in ${DARKBLUE}mysql-database$NORMAL of compose config."
  1010. # return 1
  1011. # fi
  1012. # }
  1013. # export -f get_compose_relation_config_for_service
  1014. _get_master_charm_for_service() {
  1015. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  1016. charm metadata requires master_charm target_charm target_service service_def
  1017. if [ -e "$cache_file" ]; then
  1018. # debug "$FUNCNAME: cache hit ($*)"
  1019. cat "$cache_file"
  1020. return 0
  1021. fi
  1022. charm=$(get_service_charm "$service") || return 1
  1023. metadata=$(get_charm_metadata "$charm") || return 1
  1024. if [ "$(echo "$metadata" | shyaml get-value "subordinate" 2>/dev/null)" != "True" ]; then
  1025. ## just return charm name
  1026. echo "$charm" > "$cache_file"
  1027. echo "$charm"
  1028. return 0
  1029. fi
  1030. ## fetch the container relation
  1031. requires="$(echo "$metadata" | shyaml get-value "requires" 2>/dev/null)"
  1032. if [ -z "$requires" ]; then
  1033. die "Charm $DARKYELLOW$charm$NORMAL is a subordinate but does not have any 'requires' " \
  1034. "section."
  1035. fi
  1036. master_charm=
  1037. while read-0 relation_name relation; do
  1038. if [ "$(echo "$relation" | shyaml get-value "scope" 2>/dev/null)" == "container" ]; then
  1039. # debug "$DARKYELLOW$service$NORMAL's relation" \
  1040. # "$DARKBLUE${relation_name}$NORMAL is a container."
  1041. interface="$(echo "$relation" | shyaml get-value "interface" 2>/dev/null)"
  1042. if [ -z "$interface" ]; then
  1043. err "No ${WHITE}$interface${NORMAL} set for relation $relation_name."
  1044. return 1
  1045. fi
  1046. ## Action provided by relation ?
  1047. target_service=
  1048. while read-0 relation_name candidate_target_service _relation_config _tech_dep; do
  1049. [ "$interface" == "$relation_name" ] && {
  1050. target_service="$candidate_target_service"
  1051. break
  1052. }
  1053. done < <(get_compose_relations "$service")
  1054. if [ -z "$target_service" ]; then
  1055. err "Couldn't find ${WHITE}relations.$interface${NORMAL} in" \
  1056. "${DARKYELLOW}$service$NORMAL compose definition."
  1057. return 1
  1058. fi
  1059. master_charm=$(get_service_charm "$target_service") || return 1
  1060. break
  1061. fi
  1062. done < <(echo "$requires" | shyaml key-values-0 2>/dev/null)
  1063. if [ -z "$master_charm" ]; then
  1064. die "Charm $DARKYELLOW$charm$NORMAL is a subordinate but does not have any relation with" \
  1065. " ${WHITE}scope${NORMAL} set to 'container'."
  1066. fi
  1067. echo "$master_charm" > "$cache_file"
  1068. echo "$master_charm"
  1069. return 0
  1070. }
  1071. export -f _get_master_charm_for_service
  1072. _get_top_master_charm_for_service() {
  1073. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  1074. current_service
  1075. if [ -e "$cache_file" ]; then
  1076. # debug "$FUNCNAME: cache hit ($*)"
  1077. cat "$cache_file"
  1078. return 0
  1079. fi
  1080. current_service="$service"
  1081. while true; do
  1082. master_service=$(_get_master_charm_for_service "$current_service") || return 1
  1083. [ "$master_service" == "$current_service" ] && break
  1084. current_service="$master_service"
  1085. done
  1086. echo "$current_service" | tee "$cache_file"
  1087. return 0
  1088. }
  1089. export -f _get_top_master_charm_for_service
  1090. get_charm_metadata() {
  1091. local charm="$1" metadata_file
  1092. metadata_file="$CHARM_STORE/$charm/metadata.yml"
  1093. if ! [ -e "$metadata_file" ]; then
  1094. return 0 ## No metadata file is as if metadata was empty
  1095. fi
  1096. cat "$metadata_file"
  1097. }
  1098. export -f get_charm_metadata
  1099. ##
  1100. ## The result is a mixin that is not always a complete valid
  1101. ## docker-compose entry (thinking of subordinates). The result
  1102. ## will be merge with master charms.
  1103. get_docker_compose_mixin_from_metadata() {
  1104. local charm="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  1105. metadata_file metadata volumes docker_compose subordinate image
  1106. if [ -e "$cache_file" ]; then
  1107. debug "$FUNCNAME: cache hit ($*)"
  1108. cat "$cache_file"
  1109. return 0
  1110. fi
  1111. mixin=
  1112. metadata="$(get_charm_metadata "$charm")"
  1113. if [ "$metadata" ]; then
  1114. ## resources to volumes
  1115. volumes=$(
  1116. for resource_type in data config; do
  1117. while read-0 resource; do
  1118. eval "echo \" - \$${resource_type^^}STORE/\$charm\$resource:\$resource:rw\""
  1119. done < <(echo "$metadata" | shyaml get-values-0 "${resource_type}-resources" 2>/dev/null)
  1120. done
  1121. while read-0 resource; do
  1122. if [[ "$resource" == *:* ]]; then
  1123. echo " - $resource:rw"
  1124. else
  1125. echo " - $resource:$resource:rw"
  1126. fi
  1127. done < <(echo "$metadata" | shyaml get-values-0 "host-resources" 2>/dev/null)
  1128. while read-0 resource; do
  1129. if ! [ -e "$CHARM_STORE/$charm/resources$resource" ]; then
  1130. err "No '$resource' resource found in ${BLUE}resources/${NORMAL}" \
  1131. "directory of charm $DARKYELLOW$charm$NORMAL."
  1132. exit 1
  1133. fi
  1134. echo " - $CHARM_STORE/$charm/resources$resource:$resource:rw"
  1135. done < <(echo "$metadata" | shyaml get-values-0 "charm-resources" 2>/dev/null)
  1136. ) || return 1
  1137. if [ "$volumes" ]; then
  1138. mixin=$(echo -en "volumes:\n$volumes")
  1139. fi
  1140. docker_compose=$(echo "$metadata" | shyaml get-value "docker-compose" 2>/dev/null)
  1141. if [ "$docker_compose" ]; then
  1142. mixin=$(merge_yaml_str "$mixin" "$docker_compose")
  1143. fi
  1144. if [ "$(echo "$metadata" | shyaml get-value "subordinate" 2>/dev/null)" == "True" ]; then
  1145. subordinate=true
  1146. fi
  1147. fi
  1148. image=$(echo "$metadata" | shyaml get-value "docker-image" 2>/dev/null)
  1149. image_or_build_statement=
  1150. if [ "$image" ]; then
  1151. if [ "$subordinate" ]; then
  1152. err "Subordinate charm can not have a ${WHITE}docker-image${NORMAL} value."
  1153. return 1
  1154. fi
  1155. image_or_build_statement="image: $image"
  1156. elif [ -d "$CHARM_STORE/$charm/build" ]; then
  1157. if [ "$subordinate" ]; then
  1158. err "Subordinate charm can not have a 'build' sub directory."
  1159. return 1
  1160. fi
  1161. image_or_build_statement="build: $charm/build"
  1162. fi
  1163. if [ "$image_or_build_statement" ]; then
  1164. mixin=$(merge_yaml_str "$mixin" "$image_or_build_statement")
  1165. fi
  1166. echo "$mixin" > "$cache_file"
  1167. echo "$mixin"
  1168. }
  1169. export -f get_docker_compose_mixin_from_metadata
  1170. _save() {
  1171. local name="$1"
  1172. cat - | tee -a "$docker_compose_dir/.data/$name"
  1173. }
  1174. export -f _save
  1175. get_default_project_name() {
  1176. if [ "$DEFAULT_PROJECT_NAME" ]; then
  1177. echo "$DEFAULT_PROJECT_NAME"
  1178. return 0
  1179. fi
  1180. compose_yml_location="$(get_compose_yml_location)"
  1181. if [ "$compose_yml_location" ]; then
  1182. if normalized_path=$(readlink -e "$compose_yml_location"); then
  1183. echo "$(basename "$(dirname "$normalized_path")")"
  1184. return 0
  1185. fi
  1186. fi
  1187. echo "project"
  1188. return 0
  1189. }
  1190. export -f get_default_project_name
  1191. launch_docker_compose() {
  1192. docker_compose_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
  1193. #debug "Creating temporary docker-compose directory in '$docker_compose_tmpdir'."
  1194. # trap_add EXIT "debug \"Removing temporary docker-compose directory in $docker_compose_tmpdir.\";\
  1195. # rm -rf \"$docker_compose_tmpdir\""
  1196. trap_add EXIT "rm -rf \"$docker_compose_tmpdir\""
  1197. project=$(get_default_project_name)
  1198. mkdir -p "$docker_compose_tmpdir/$project"
  1199. docker_compose_dir="$docker_compose_tmpdir/$project"
  1200. if [ -z "$SERVICE_PACK" ]; then
  1201. export SERVICE_PACK=$(get_default_target_services $SERVICE_PACK)
  1202. fi
  1203. get_docker_compose $SERVICE_PACK > "$docker_compose_dir/docker-compose.yml" || return 1
  1204. if [ -e "$state_tmpdir/to-merge-in-docker-compose.yml" ]; then
  1205. # debug "Merging some config data in docker-compose.yml:"
  1206. # debug "$(cat $state_tmpdir/to-merge-in-docker-compose.yml)"
  1207. _config_merge "$docker_compose_dir/docker-compose.yml" "$state_tmpdir/to-merge-in-docker-compose.yml" || return 1
  1208. fi
  1209. ## XXXvlab: could be more specific and only link the needed charms
  1210. ln -sf "$CHARM_STORE/"* "$docker_compose_dir/"
  1211. mkdir "$docker_compose_dir/.data"
  1212. {
  1213. {
  1214. cd "$docker_compose_dir"
  1215. debug "${WHITE}docker-compose.yml$NORMAL for $DARKYELLOW$SERVICE_PACK$NORMAL"
  1216. debug "$(cat "$docker_compose_dir/docker-compose.yml")"
  1217. debug "${WHITE}Launching$NORMAL: docker-compose $@"
  1218. docker-compose "$@"
  1219. echo "$?" > "$docker_compose_dir/.data/errlvl"
  1220. } | _save stdout
  1221. } 3>&1 1>&2 2>&3 | _save stderr 3>&1 1>&2 2>&3
  1222. 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
  1223. err "Detected bug https://github.com/docker/docker/issues/4036 ... "
  1224. err "Please re-launch your command, or switch from 'devicemapper' driver to 'overlayfs' or 'aufs'."
  1225. fi
  1226. docker_compose_errlvl="$(cat "$docker_compose_dir/.data/errlvl")"
  1227. if [ -z "docker_compose_dir" ]; then
  1228. err "Something went wrong before you could gather docker-compose errorlevel."
  1229. return 1
  1230. fi
  1231. return "$docker_compose_errlvl"
  1232. }
  1233. export -f launch_docker_compose
  1234. get_compose_yml_location() {
  1235. parent=$(while ! [ -e "./compose.yml" ]; do
  1236. [ "$PWD" == "/" ] && exit 0
  1237. cd ..
  1238. done; echo "$PWD"
  1239. )
  1240. if [ "$parent" ]; then
  1241. echo "$parent/compose.yml"
  1242. return 0
  1243. fi
  1244. if [ "$DEFAULT_COMPOSE_FILE" ]; then
  1245. if ! [ -e "$DEFAULT_COMPOSE_FILE" ]; then
  1246. err "No 'compose.yml' was found in current or parent dirs," \
  1247. "and \$DEFAULT_COMPOSE_FILE points to an unexistent file."
  1248. die "Please provide a 'compose.yml' file."
  1249. fi
  1250. echo "$DEFAULT_COMPOSE_FILE"
  1251. return 0
  1252. fi
  1253. err "No 'compose.yml' was found in current or parent dirs, and no \$DEFAULT_COMPOSE_FILE was set."
  1254. die "Please provide a 'compose.yml' file."
  1255. return 1
  1256. }
  1257. export -f get_compose_yml_location
  1258. get_default_target_services() {
  1259. local services=("$@")
  1260. if [ -z "${services[*]}" ]; then
  1261. if [ "$DEFAULT_SERVICES" ]; then
  1262. info "No service provided, using $WHITE\$DEFAULT_SERVICES$NORMAL variable." \
  1263. "Target services: $DARKYELLOW$DEFAULT_SERVICES$NORMAL"
  1264. services="$DEFAULT_SERVICES"
  1265. else
  1266. err "No service provided."
  1267. return 1
  1268. fi
  1269. fi
  1270. echo "${services[*]}"
  1271. }
  1272. export -f get_default_target_services
  1273. get_master_services() {
  1274. local loaded master_service
  1275. declare -A loaded
  1276. for service in "$@"; do
  1277. master_service=$(_get_top_master_charm_for_service "$service") || return 1
  1278. if [ "${loaded[$master_service]}" ]; then
  1279. continue
  1280. fi
  1281. echo "$master_service"
  1282. loaded["$master_service"]=1
  1283. done | xargs echo
  1284. }
  1285. export -f get_master_services
  1286. _setup_state_dir() {
  1287. export state_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
  1288. #debug "Creating temporary state directory in '$state_tmpdir'."
  1289. # trap_add EXIT "debug \"Removing temporary state directory in $state_tmpdir.\";\
  1290. # rm -rf \"$state_tmpdir\""
  1291. trap_add EXIT "rm -rf \"$state_tmpdir\""
  1292. }
  1293. get_docker_compose_opts_list() {
  1294. local cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  1295. if [ -e "$cache_file" ]; then
  1296. debug "$FUNCNAME: cache hit ($*)"
  1297. cat "$cache_file"
  1298. return 0
  1299. fi
  1300. docker-compose "$@" --help | grep '^Options:' -A 20000 |
  1301. tail -n +2 |
  1302. egrep "^\s+-" |
  1303. sed -r 's/\s+((((-[a-zA-Z]|--[a-zA-Z0-9-]+)( [A-Z=]+|=[^ ]+)?)(, )?)+)\s+.*$/\1/g' |
  1304. tee "$cache_file"
  1305. }
  1306. _MULTIOPTION_REGEX='^((-[a-zA-Z]|--[a-zA-Z0-9-]+)(, )?)+'
  1307. _MULTIOPTION_REGEX_LINE_FILTER=$_MULTIOPTION_REGEX'(\s|=)'
  1308. get_docker_compose_multi_opts_list() {
  1309. opts_list=$(get_docker_compose_opts_list "$@")
  1310. echo "$opts_list" | egrep "$_MULTIOPTION_REGEX_LINE_FILTER" |
  1311. sed -r "s/^($_MULTIOPTION_REGEX)(\s|=).*$/\1/g" |
  1312. tr ',' "\n" | xargs echo
  1313. }
  1314. get_docker_compose_single_opts_list() {
  1315. opts_list=$(get_docker_compose_opts_list "$@")
  1316. echo "$opts_list" | egrep -v "$_MULTIOPTION_REGEX_LINE_FILTER" |
  1317. tr ',' "\n" | xargs echo
  1318. }
  1319. [ "$SOURCED" ] && return 0
  1320. if [ -z "$DISABLE_SYSTEM_CONFIG_FILE" ]; then
  1321. if [ -r /etc/default/charm ]; then
  1322. . /etc/default/charm
  1323. fi
  1324. if [ -r "/etc/default/$exname" ]; then
  1325. . "/etc/default/$exname"
  1326. fi
  1327. ## XXXvlab: should provide YML config opportunities in possible parent dirs ?
  1328. ## userdir ? and global /etc/compose.yml ?
  1329. . /etc/compose.conf
  1330. . /etc/compose.local.conf
  1331. fi
  1332. _setup_state_dir
  1333. ##
  1334. ## Argument parsing
  1335. ##
  1336. remainder_args=()
  1337. compose_opts=()
  1338. action_opts=()
  1339. no_hooks=
  1340. no_init=
  1341. action=
  1342. stage="main" ## switches from 'main', to 'action', 'remainder'
  1343. # DC_MATCH_MULTI=
  1344. # DC_MATCH_SINGLE=
  1345. while [ "$#" != 0 ]; do
  1346. case "$stage" in
  1347. "main")
  1348. case "$1" in
  1349. --help|-h)
  1350. no_init=true ; no_hooks=true ; no_relations=true
  1351. compose_opts+=("$1")
  1352. ;;
  1353. --verbose|-v)
  1354. export VERBOSE=true
  1355. compose_opts+=("$1")
  1356. ;;
  1357. -f)
  1358. [ -e "$2" ] || die "File $2 doesn't exists"
  1359. export DEFAULT_COMPOSE_FILE="$2"
  1360. shift
  1361. ;;
  1362. -p)
  1363. export DEFAULT_PROJECT_NAME="$2"
  1364. shift
  1365. ;;
  1366. --no-relations)
  1367. export no_relations=true
  1368. ;;
  1369. --no-hooks)
  1370. export no_hooks=true
  1371. ;;
  1372. --no-init)
  1373. export no_init=true
  1374. ;;
  1375. --debug)
  1376. export DEBUG=true
  1377. export VERBOSE=true
  1378. ;;
  1379. --*|-*)
  1380. compose_opts+=("$1")
  1381. ;;
  1382. *)
  1383. action="$1"
  1384. stage="action"
  1385. # DC_MATCH_MULTI=$(get_docker_compose_multi_opts_list "$action") || return 1
  1386. # DC_MATCH_SINGLE="$(get_docker_compose_single_opts_list "$action") $(echo "$DC_MATCH_MULTI" | sed -r 's/( |$)/=\* /g')"
  1387. ;;
  1388. esac
  1389. ;;
  1390. "action")
  1391. case "$1" in
  1392. --help|-h)
  1393. no_init=true ; no_hooks=true ; no_relations=true
  1394. action_opts+=("$1")
  1395. ;;
  1396. --verbose|-v)
  1397. export VERBOSE=true
  1398. action_opts+=("$1")
  1399. ;;
  1400. --*|-*)
  1401. action_opts+=("$1")
  1402. ;;
  1403. *)
  1404. action_posargs+=("$1")
  1405. stage="remainder"
  1406. ;;
  1407. esac
  1408. ;;
  1409. "remainder")
  1410. remainder_args+=("$@")
  1411. break 3;;
  1412. esac
  1413. shift
  1414. done
  1415. [ "${compose_opts[*]}" ] && debug "Main docker-compose opts: ${compose_opts[*]}"
  1416. [ "${action_opts[*]}" ] && debug "Action '$action' opts: ${action_opts[*]}"
  1417. [ "${remainder_args[*]}" ] && debug "Remainder args: ${remainder_args[*]}"
  1418. ##
  1419. ## Actual code
  1420. ##
  1421. export CHARM_STORE=${CHARM_STORE:-/srv/charm-store}
  1422. export DOCKER_DATASTORE=${DOCKER_DATASTORE:-/srv/docker-datastore}
  1423. export COMPOSE_YML_FILE=$(get_compose_yml_location) || exit 1
  1424. debug "Found 'compose.yml' file in '$COMPOSE_YML_FILE'."
  1425. if ! [ -d "$CHARM_STORE" ]; then
  1426. err "Charm store path $YELLOW$CHARM_STORE$NORMAL does not exists. "
  1427. err "Please check your $YELLOW\$CHARM_STORE$NORMAL variable value."
  1428. exit 1
  1429. fi
  1430. if [ -z "$(cd "$CHARM_STORE"; ls)" ]; then
  1431. err "no available charms in charm store $YELLOW$CHARM_STORE$NORMAL. Either:"
  1432. err " - check $YELLOW\$CHARM_STORE$NORMAL variable value"
  1433. err " - download charms in $CHARM_STORE"
  1434. print_error "Charm store is empty. Cannot continue."
  1435. fi
  1436. ##
  1437. ## Get services in command line.
  1438. ##
  1439. is_service_action=
  1440. case "$action" in
  1441. up|build|start|stop|config)
  1442. services="$(get_default_target_services "${action_posargs[@]}")" || exit 1
  1443. orig_services="${action_posargs[@]:1}"
  1444. ;;
  1445. run)
  1446. services="${action_posargs[0]}"
  1447. ;;
  1448. "")
  1449. services=
  1450. ;;
  1451. *)
  1452. if is_service_action=$(has_service_action "${action_posargs[0]}" "$action"); then
  1453. debug "Found action $DARKCYAN$action$NORMAL in service $DARKYELLOW${action_posargs[0]}$NORMAL"
  1454. services="${action_posargs[0]}"
  1455. else
  1456. services="$(get_default_target_services "${action_posargs[@]}")"
  1457. fi
  1458. ;;
  1459. esac
  1460. get_docker_compose $services >/dev/null || { ## precalculate variable \$_current_docker_compose
  1461. err "Fails to compile base 'docker-conmpose.conf'"
  1462. exit 1
  1463. }
  1464. ##
  1465. ## Pre-action
  1466. ##
  1467. full_init=
  1468. case "$action" in
  1469. up|run)
  1470. full_init=true
  1471. ;;
  1472. "")
  1473. full_init=
  1474. ;;
  1475. *)
  1476. if [ "$is_service_action" ]; then
  1477. full_init=true
  1478. fi
  1479. ;;
  1480. esac
  1481. if [ "$full_init" ]; then
  1482. ## init in order
  1483. Section initialisation
  1484. if [ -z "$no_init" ]; then
  1485. run_service_hook "$services" init || exit 1
  1486. fi
  1487. ## Get relations
  1488. if [ -z "$no_relations" ]; then
  1489. run_service_relations "$services" || exit 1
  1490. fi
  1491. ## XXXvlab: to be removed when all relation and service stuff is resolved
  1492. if [ -z "$no_hooks" ]; then
  1493. ordered_services=$(get_ordered_service_dependencies $services) || exit 1
  1494. for service in $ordered_services; do
  1495. charm=$(get_service_charm "$service") || exit 1
  1496. for script in "$CHARM_STORE/$charm/hooks.d/"*.sh; do
  1497. [ -e "$script" ] || continue
  1498. [ -x "$script" ] || { echo "compose: script $script is not executable." >&2; exit 1; }
  1499. (
  1500. debug "Launching '$script'."
  1501. cd "$(dirname "$script)")";
  1502. "$script" "$@"
  1503. ) || { echo "compose: hook $script failed. Stopping." >&2; exit 1; }
  1504. done
  1505. done
  1506. fi
  1507. fi
  1508. export SERVICE_PACK="$services"
  1509. ##
  1510. ## Docker-compose
  1511. ##
  1512. case "$action" in
  1513. up|start|stop|build)
  1514. master_services=$(get_master_services $SERVICE_PACK) || exit 1
  1515. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" $master_services
  1516. ;;
  1517. run)
  1518. master_service=$(get_master_services $SERVICE_PACK) || exit 1
  1519. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "$master_service" "${remainder_args[@]}"
  1520. ;;
  1521. # enter)
  1522. # master_service=$(get_master_services $SERVICE_PACK) || exit 1
  1523. # [ "${remainder_args[*]}" ] || remainder_args=("/bin/bash" "-c" "export TERM=xterm; exec bash")
  1524. # docker exec -ti "${action_opts[@]}" "$master_service" "${remainder_args[@]}"
  1525. # ;;
  1526. config)
  1527. ## removing the services
  1528. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "${remainder_args[@]}"
  1529. warn "Runtime configuration modification (from relations) are not included here."
  1530. ;;
  1531. *)
  1532. if [ "$is_service_action" ]; then
  1533. run_service_action "$SERVICE_PACK" "$action" "${remainder_args[@]}"
  1534. else
  1535. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "${remainder_args[@]}"
  1536. fi
  1537. ;;
  1538. esac