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.

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