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.

1825 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. CHARM_NAME="$charm" \
  603. PROJECT_NAME=$PROJECT_NAME \
  604. DOCKER_BASE_IMAGE=$(service_base_docker_image "$charm") \
  605. SERVICE_DATASTORE="$DATASTORE/$charm" \
  606. SERVICE_CONFIGSTORE="$CONFIGSTORE/$charm" \
  607. "$TARGET_SCRIPT"
  608. EOF
  609. loaded[$subservice]=1
  610. done
  611. done
  612. return 0
  613. }
  614. relation-get () {
  615. local key="$1"
  616. cat "$RELATION_DATA_FILE" | shyaml get-value "$key" 2>/dev/null
  617. if [ "$?" != 0 ]; then
  618. err "The key $WHITE$key$NORMAL was not found in relation's data."
  619. return 1
  620. fi
  621. }
  622. export -f relation-get
  623. relation-base-compose-get () {
  624. local key="$1"
  625. echo "$RELATION_BASE_COMPOSE_DEF" | shyaml get-value "options.$key" 2>/dev/null
  626. if [ "$?" != 0 ]; then
  627. err "The key $WHITE$key$NORMAL was not found in base service compose definition.."
  628. return 1
  629. fi
  630. }
  631. export -f relation-base-compose-get
  632. relation-target-compose-get () {
  633. local key="$1"
  634. echo "$RELATION_BASE_COMPOSE_DEF" | shyaml get-value "options.$key" 2>/dev/null
  635. if [ "$?" != 0 ]; then
  636. err "The key $WHITE$key$NORMAL was not found in base service compose definition.."
  637. return 1
  638. fi
  639. }
  640. export -f relation-base-compose-get
  641. relation-set () {
  642. local key="$1" value="$2"
  643. if [ -z "$RELATION_DATA_FILE" ]; then
  644. err "$FUNCNAME: relation does not seems to be correctly setup."
  645. return 1
  646. fi
  647. if ! [ -r "$RELATION_DATA_FILE" ]; then
  648. err "$FUNCNAME: can't read relation's data." >&2
  649. return 1
  650. fi
  651. _config_merge "$RELATION_DATA_FILE" <(echo "$key: $value")
  652. }
  653. export -f relation-set
  654. _config_merge() {
  655. local config_filename="$1" merge_to_file="$2"
  656. touch "$config_filename" &&
  657. merge_yaml "$config_filename" "$merge_to_file" > "$config_filename.tmp" || return 1
  658. mv "$config_filename.tmp" "$config_filename"
  659. }
  660. export -f _config_merge
  661. ## XXXvlab; this can be used only in relation, I'd like to use it in init.
  662. config-add() {
  663. local metadata="$1"
  664. _config_merge "$RELATION_CONFIG" <(echo "$metadata")
  665. }
  666. export -f config-add
  667. ## XXXvlab; this can be used only in relation, I'd like to use it in init.
  668. init-config-add() {
  669. local metadata="$1"
  670. _config_merge "$state_tmpdir/to-merge-in-docker-compose.yml" <(echo "$metadata")
  671. }
  672. export -f init-config-add
  673. logstdout() {
  674. local name="$1"
  675. sed -r 's%^%'"${name}"'> %g'
  676. }
  677. export -f logstdout
  678. logstderr() {
  679. local name="$1"
  680. sed -r 's%^(.*)$%'"${RED}${name}>${NORMAL} \1"'%g'
  681. }
  682. export -f logstderr
  683. _run_service_relation () {
  684. local relation_name="$1" service="$2" target_service="$3" relation_config="$4" relation_dir services
  685. charm=$(get_service_charm "$service") || return 1
  686. target_charm=$(get_service_charm "$target_service") || return 1
  687. base_script_name=$(echo "$relation_name" | tr "-" "_" )-relation-joined
  688. script_name="hooks/${base_script_name}"
  689. [ ! -e "$CHARM_STORE/$target_charm/$script_name" -a ! -e "$CHARM_STORE/$charm/$script_name" ] &&
  690. return 0
  691. relation_dir=$(get_relation_data_dir "$charm" "$target_charm" "$relation_name") || return 1
  692. RELATION_DATA_FILE=$(get_relation_data_file "$charm" "$target_charm" "$relation_name" "$relation_config") || return 1
  693. RELATION_BASE_COMPOSE_DEF=$(get_compose_service_def "$service") || return 1
  694. RELATION_TARGET_COMPOSE_DEF=$(get_compose_service_def "$target_service") || return 1
  695. export BASE_SERVICE_NAME=$service
  696. export TARGET_SERVICE_NAME=$target_service
  697. export BASE_CHARM_NAME=$charm
  698. export TARGET_CHARM_NAME=$target_charm
  699. PROJECT_NAME=$(get_default_project_name) || return 1
  700. MASTER_BASE_CHARM_NAME=$(_get_top_master_charm_for_service "$service") || return 1
  701. MASTER_TARGET_CHARM_NAME=$(_get_top_master_charm_for_service "$target_service") || return 1
  702. export RELATION_DATA_FILE RELATION_BASE_COMPOSE_DEF RELATION_TARGET_COMPOSE_DEF
  703. export MASTER_BASE_CHARM_NAME MASTER_TARGET_CHARM_NAME PROJECT_NAME
  704. target_errlvl=0
  705. if ! [ -e "$CHARM_STORE/$target_charm/$script_name" ]; then
  706. verb "No relation script '$script_name' in $DARKYELLOW$target_charm$NORMAL."
  707. else
  708. verb "Running ${DARKBLUE}$relation_name${NORMAL} relation-joined script" \
  709. "for target charm $DARKYELLOW$target_charm$NORMAL"
  710. RELATION_CONFIG="$relation_dir/config_provider"
  711. DOCKER_BASE_IMAGE=$(service_base_docker_image "$target_service") || return 1
  712. export DOCKER_BASE_IMAGE RELATION_CONFIG RELATION_DATA
  713. {
  714. (
  715. cd "$CHARM_STORE/$target_charm"
  716. SERVICE_NAME=$target_service
  717. SERVICE_DATASTORE="$DATASTORE/$target_charm"
  718. SERVICE_CONFIGSTORE="$CONFIGSTORE/$target_charm"
  719. export SERVICE_NAME DOCKER_BASE_IMAGE SERVICE_DATASTORE SERVICE_CONFIGSTORE
  720. "$script_name"
  721. echo "$?" > "$relation_dir/target_errlvl"
  722. ) | logstdout "$DARKYELLOW$target_charm$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${GREEN}@${NORMAL}"
  723. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$target_charm$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${RED}@${NORMAL}" 3>&1 1>&2 2>&3
  724. target_errlvl="$(cat "$relation_dir/target_errlvl")" || {
  725. err "Relation script '$script_name' in $DARKYELLOW$target_charm$NORMAL" \
  726. "failed before outputing an errorlevel."
  727. ((target_errlvl |= "1" ))
  728. }
  729. if [ -e "$RELATION_CONFIG" ]; then
  730. debug "Merging some new config info in $DARKYELLOW$target_service$NORMAL"
  731. _config_merge "$state_tmpdir/to-merge-in-docker-compose.yml" "$RELATION_CONFIG" &&
  732. rm "$RELATION_CONFIG"
  733. ((target_errlvl |= "$?"))
  734. fi
  735. fi
  736. if [ "$target_errlvl" == 0 ]; then
  737. errlvl=0
  738. if [ -e "$CHARM_STORE/$charm/$script_name" ]; then
  739. verb "Running ${DARKBLUE}$relation_name${NORMAL} relation-joined script" \
  740. "for charm $DARKYELLOW$charm$NORMAL"
  741. RELATION_CONFIG="$relation_dir/config_providee"
  742. RELATION_DATA="$(cat "$RELATION_DATA_FILE")"
  743. DOCKER_BASE_IMAGE=$(service_base_docker_image "$service") || return 1
  744. export DOCKER_BASE_IMAGE RELATION_CONFIG RELATION_DATA
  745. {
  746. (
  747. cd "$CHARM_STORE/$charm"
  748. SERVICE_NAME=$service
  749. SERVICE_DATASTORE="$DATASTORE/$charm"
  750. SERVICE_CONFIGSTORE="$CONFIGSTORE/$charm"
  751. export SERVICE_NAME DOCKER_BASE_IMAGE SERVICE_DATASTORE SERVICE_CONFIGSTORE
  752. "$script_name"
  753. echo "$?" > "$relation_dir/errlvl"
  754. ) | logstdout "$DARKYELLOW$charm$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${GREEN}@${NORMAL}"
  755. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$charm$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${RED}@$NORMAL" 3>&1 1>&2 2>&3
  756. errlvl="$(cat "$relation_dir/errlvl")" || {
  757. err "Relation script '$script_name' in $DARKYELLOW$charm$NORMAL" \
  758. "failed before outputing an errorlevel."
  759. ((errlvl |= "1" ))
  760. }
  761. if [ -e "$RELATION_CONFIG" ]; then
  762. _config_merge "$state_tmpdir/to-merge-in-docker-compose.yml" "$RELATION_CONFIG" &&
  763. rm "$RELATION_CONFIG"
  764. ((errlvl |= "$?" ))
  765. fi
  766. if [ "$errlvl" != 0 ]; then
  767. err "Relation $DARKBLUE$relation_name$NORMAL on $DARKYELLOW$charm$NORMAL failed to run properly."
  768. fi
  769. else
  770. verb "No relation script '$script_name' in charm $DARKYELLOW$charm$NORMAL. Ignoring."
  771. fi
  772. else
  773. err "Relation $DARKBLUE$relation_name$NORMAL on $DARKYELLOW$target_charm$NORMAL failed to run properly."
  774. fi
  775. if [ "$target_errlvl" == 0 -a "$errlvl" == 0 ]; then
  776. debug "Relation $DARKBLUE$relation_name$NORMAL is established" \
  777. "between $DARKYELLOW$service$NORMAL and $DARKYELLOW$target_service$NORMAL."
  778. return 0
  779. else
  780. return 1
  781. fi
  782. }
  783. export -f _run_service_relation
  784. get_compose_relations () {
  785. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)" relation_name relation_def
  786. if [ -e "$cache_file" ]; then
  787. # debug "$FUNCNAME: cache hit ($*)"
  788. cat "$cache_file"
  789. return 0
  790. fi
  791. compose_def="$(get_compose_service_def "$service")" || return 1
  792. (
  793. set -o pipefail
  794. if [ "$compose_def" ]; then
  795. while read-0 relation_name relation_def; do
  796. (
  797. case "$(echo "$relation_def" | shyaml get-type 2>/dev/null)" in
  798. "str")
  799. target_service="$(echo "$relation_def" | shyaml get-value 2>/dev/null)"
  800. tech_dep="$(get_charm_tech_dep_orientation_for_relation "$target_service" "$relation_name")"
  801. echo -en "$relation_name\0$target_service\0\0$tech_dep\0"
  802. ;;
  803. "sequence")
  804. while read-0 target_service; do
  805. tech_dep="$(get_charm_tech_dep_orientation_for_relation "$target_service" "$relation_name")"
  806. echo -en "$relation_name\0$target_service\0\0$tech_dep\0"
  807. done < <(echo "$relation_def" | shyaml get-values-0 2>/dev/null)
  808. ;;
  809. "struct")
  810. while read-0 target_service relation_config; do
  811. tech_dep="$(get_charm_tech_dep_orientation_for_relation "$target_service" "$relation_name")"
  812. echo -en "$relation_name\0$target_service\0$relation_config\0$tech_dep\0"
  813. done < <(echo "$relation_def" | shyaml key-values-0 2>/dev/null)
  814. ;;
  815. esac
  816. ) </dev/null >> "$cache_file"
  817. done < <(echo "$compose_def" | shyaml key-values-0 relations 2>/dev/null)
  818. fi
  819. )
  820. if [ "$?" != 0 ]; then
  821. err "Error while looking for compose relations."
  822. rm -f "$cache_file" ## no cache
  823. return 1
  824. fi
  825. [ -e "$cache_file" ] && cat "$cache_file"
  826. return 0
  827. }
  828. export -f get_compose_relations
  829. run_service_relations () {
  830. local services="$1" loaded
  831. declare -A loaded
  832. for service in $(get_ordered_service_dependencies $services); do
  833. # debug "Upping dep's relations of ${DARKYELLOW}$service${NORMAL}:"
  834. for subservice in $(get_service_deps "$service") "$service"; do
  835. [ "${loaded[$subservice]}" ] && continue
  836. # debug " Relations of ${DARKYELLOW}$subservice${NORMAL}:"
  837. while read-0 relation_name target_service relation_config tech_dep; do
  838. export relation_config
  839. Wrap -d "Building $DARKYELLOW$subservice$NORMAL --$DARKBLUE$relation_name$NORMAL--> $DARKYELLOW$target_service$NORMAL" <<EOF || return 1
  840. _run_service_relation "$relation_name" "$subservice" "$target_service" "\$relation_config"
  841. EOF
  842. done < <(get_compose_relations "$subservice") || return 1
  843. loaded[$subservice]=1
  844. done
  845. done
  846. }
  847. export -f run_service_relations
  848. _run_service_action_direct() {
  849. local service="$1" action="$2" charm script _dummy
  850. shift; shift
  851. read-0 charm script || true ## against 'set -e' that could be setup in parent scripts
  852. if read-0 _dummy || [ "$_dummy" ]; then
  853. print_syntax_error "$FUNCNAME: too many arguments in action descriptor"
  854. return 1
  855. fi
  856. compose_file=$(get_compose_yml_location) || return 1
  857. export action_errlvl_file="$state_tmpdir/action-$service-$charm-$action-errlvl"
  858. export state_tmpdir
  859. {
  860. (
  861. set +e ## Prevents unwanted leaks from parent shell
  862. cd "$CHARM_STORE/$charm"
  863. export COMPOSE_CONFIG=$(cat "$compose_file")
  864. export METADATA_CONFIG=$(cat "$CHARM_STORE/$charm/metadata.yml")
  865. export SERVICE_NAME=$service
  866. export ACTION_NAME=$action
  867. export CONTAINER_NAME=$(_get_top_master_charm_for_service "$service")
  868. export DOCKER_BASE_IMAGE=$(service_base_docker_image "$CONTAINER_NAME")
  869. export SERVICE_DATASTORE="$DATASTORE/$service"
  870. export SERVICE_CONFIGSTORE="$CONFIGSTORE/$service"
  871. exname="$exname $ACTION_NAME $SERVICE_NAME" \
  872. stdbuf -oL -eL "$script" "$@"
  873. echo "$?" > "$action_errlvl_file"
  874. ) | logstdout "$DARKYELLOW$charm$NORMAL/${DARKCYAN}$action${NORMAL} ${GREEN}@${NORMAL}"
  875. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$charm$NORMAL/${DARKCYAN}$action${NORMAL} ${RED}@$NORMAL" 3>&1 1>&2 2>&3
  876. if ! [ -e "$action_errlvl_file" ]; then
  877. err "Action $DARKYELLOW$service$NORMAL:$DARKCYAN$action$NORMAL has failed without having time" \
  878. "to output an errlvl"
  879. return 1
  880. fi
  881. return "$(cat "$action_errlvl_file")"
  882. }
  883. export -f _run_service_action_direct
  884. _run_service_action_relation() {
  885. local service="$1" action="$2" charm target_charm relation_name target_script relation_config _dummy
  886. shift; shift
  887. read-0 charm target_charm relation_name target_script relation_config || true
  888. if read-0 _dummy || [ "$_dummy" ]; then
  889. print_syntax_error "$FUNCNAME: too many arguments in action descriptor"
  890. return 1
  891. fi
  892. export RELATION_DATA_FILE=$(get_relation_data_file "$charm" "$target_charm" "$relation_name" "$relation_config")
  893. compose_file=$(get_compose_yml_location) || return 1
  894. export action_errlvl_file="$state_tmpdir/action-$service-$charm-$action-errlvl"
  895. export state_tmpdir
  896. {
  897. (
  898. set +e ## Prevents unwanted leaks from parent shell
  899. cd "$CHARM_STORE/$charm"
  900. export METADATA_CONFIG=$(cat "$CHARM_STORE/$charm/metadata.yml")
  901. export SERVICE_NAME=$service
  902. export RELATION_TARGET_CHARM="$target_charm"
  903. export RELATION_CHARM="$charm"
  904. export ACTION_NAME=$action
  905. export CONTAINER_NAME=$(_get_top_master_charm_for_service "$service")
  906. export DOCKER_BASE_IMAGE=$(service_base_docker_image "$CONTAINER_NAME")
  907. export SERVICE_DATASTORE="$DATASTORE/$service"
  908. export SERVICE_CONFIGSTORE="$CONFIGSTORE/$service"
  909. exname="$exname $ACTION_NAME $SERVICE_NAME" \
  910. stdbuf -oL -eL "$target_script" "$@"
  911. echo "$?" > "$action_errlvl_file"
  912. ) | logstdout "$DARKYELLOW$charm$NORMAL/${DARKCYAN}$action${NORMAL} ${GREEN}@${NORMAL}"
  913. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$charm$NORMAL/${DARKCYAN}$action${NORMAL} ${RED}@$NORMAL" 3>&1 1>&2 2>&3
  914. if ! [ -e "$action_errlvl_file" ]; then
  915. err "Action $DARKYELLOW$service$NORMAL:$DARKCYAN$action$NORMAL has failed without having time" \
  916. "to output an errlvl"
  917. return 1
  918. fi
  919. return "$(cat "$action_errlvl_file")"
  920. }
  921. export -f _run_service_action_relation
  922. get_relation_data_dir() {
  923. local charm="$1" target_charm="$2" relation_name="$3" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  924. if [ -e "$cache_file" ]; then
  925. # debug "$FUNCNAME: cache hit ($*)"
  926. cat "$cache_file"
  927. return 0
  928. fi
  929. project=$(get_default_project_name) || return 1
  930. relation_dir="$VARDIR/relations/$project/$charm-$target_charm/$relation_name"
  931. if ! [ -d "$relation_dir" ]; then
  932. mkdir -p "$relation_dir" || return 1
  933. chmod go-rwx "$relation_dir" || return 1 ## protecting this directory
  934. fi
  935. echo "$relation_dir" || tee "$cache_file"
  936. }
  937. export -f get_relation_data_dir
  938. get_relation_data_file() {
  939. local charm="$1" target_charm="$2" relation_name="$3" relation_config="$4"
  940. relation_dir=$(get_relation_data_dir "$charm" "$target_charm" "$relation_name") || return 1
  941. relation_data_file="$relation_dir/data"
  942. new=
  943. if [ -e "$relation_data_file" ]; then
  944. ## Has reference changed ?
  945. new_md5=$(echo "$relation_config" | md5_compat)
  946. if [ "$new_md5" != "$(cat "$relation_data_file.md5_ref" 2>/dev/null)" ]; then
  947. new=true
  948. fi
  949. else
  950. new=true
  951. fi
  952. if [ "$new" ]; then
  953. echo "$relation_config" > "$relation_data_file"
  954. chmod go-rwx "$relation_data_file" ## protecting this file
  955. echo "$relation_config" | md5_compat > "$relation_data_file.md5_ref"
  956. fi
  957. echo "$relation_data_file"
  958. }
  959. export -f get_relation_data_file
  960. has_service_action () {
  961. local service="$1" action="$2" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  962. target_script charm target_charm
  963. if [ -e "$cache_file" ]; then
  964. # debug "$FUNCNAME: cache hit ($*)"
  965. cat "$cache_file"
  966. return 0
  967. fi
  968. charm=$(get_service_charm "$service") || return 1
  969. ## Action directly provided ?
  970. target_script="$CHARM_STORE/$charm/actions/$action"
  971. if [ -x "$target_script" ]; then
  972. echo -en "direct\0$charm\0$target_script" | tee "$cache_file"
  973. return 0
  974. fi
  975. ## Action provided by relation ?
  976. while read-0 relation_name target_service relation_config tech_dep; do
  977. target_charm=$(get_service_charm "$target_service") || return 1
  978. target_script="$CHARM_STORE/$target_charm/actions/relations/$relation_name/$action"
  979. if [ -x "$target_script" ]; then
  980. echo -en "relation\0$charm\0$target_charm\0$relation_name\0$target_script\0$relation_config" | tee "$cache_file"
  981. return 0
  982. fi
  983. done < <(get_compose_relations "$service")
  984. master=$(_get_top_master_charm_for_service "$charm")
  985. [ "$master" == "$charm" ] && return 1
  986. has_service_action "$master" "$action"
  987. }
  988. export -f has_service_action
  989. run_service_action () {
  990. local service="$1" action="$2"
  991. shift ; shift
  992. {
  993. if ! read-0 action_type; then
  994. info "Service $DARKYELLOW$service$NORMAL does not have any action $DARKCYAN$action$NORMAL defined."
  995. info " Add an executable script to '$CHARM_STORE/$charm/actions/$action' to implement action."
  996. return 1
  997. fi
  998. Section "running $DARKYELLOW$service$NORMAL/$DARKCYAN$action$NORMAL ($action_type)"; Feed
  999. "_run_service_action_${action_type}" "$service" "$action" "$@"
  1000. } < <(has_service_action "$service" "$action")
  1001. }
  1002. export -f run_service_action
  1003. get_compose_relation_config() {
  1004. local service=$1 relation_config cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  1005. if [ -e "$cache_file" ]; then
  1006. # debug "$FUNCNAME: cache hit ($*)"
  1007. cat "$cache_file"
  1008. return 0
  1009. fi
  1010. compose_service_def=$(get_compose_service_def "$service") || return 1
  1011. echo "$compose_service_def" | shyaml get-value "relations" 2>/dev/null | tee "$cache_file"
  1012. }
  1013. export -f get_compose_relation_config
  1014. # ## Return key-values-0
  1015. # get_compose_relation_config_for_service() {
  1016. # local service=$1 relation_name=$2 relation_config
  1017. # compose_service_relations=$(get_compose_relation_config "$service") || return 1
  1018. # if ! relation_config=$(
  1019. # echo "$compose_service_relations" |
  1020. # shyaml get-value "${relation_name}" 2>/dev/null); then
  1021. # err "Couldn't find $DARKYELLOW${service}$NORMAL/${WHITE}${relation_name}$NORMAL" \
  1022. # "relation config in compose configuration."
  1023. # return 1
  1024. # fi
  1025. # if [ -z "$relation_config" ]; then
  1026. # err "Relation ${WHITE}mysql-database$NORMAL is empty in compose configuration."
  1027. # return 1
  1028. # fi
  1029. # if ! echo "$relation_config" | shyaml key-values-0 2>/dev/null; then
  1030. # err "No key/values in ${DARKBLUE}mysql-database$NORMAL of compose config."
  1031. # return 1
  1032. # fi
  1033. # }
  1034. # export -f get_compose_relation_config_for_service
  1035. _get_master_charm_for_service() {
  1036. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  1037. charm metadata requires master_charm target_charm target_service service_def
  1038. if [ -e "$cache_file" ]; then
  1039. # debug "$FUNCNAME: cache hit ($*)"
  1040. cat "$cache_file"
  1041. return 0
  1042. fi
  1043. charm=$(get_service_charm "$service") || return 1
  1044. metadata=$(get_charm_metadata "$charm") || return 1
  1045. if [ "$(echo "$metadata" | shyaml get-value "subordinate" 2>/dev/null)" != "True" ]; then
  1046. ## just return charm name
  1047. echo "$charm" > "$cache_file"
  1048. echo "$charm"
  1049. return 0
  1050. fi
  1051. ## fetch the container relation
  1052. requires="$(echo "$metadata" | shyaml get-value "requires" 2>/dev/null)"
  1053. if [ -z "$requires" ]; then
  1054. die "Charm $DARKYELLOW$charm$NORMAL is a subordinate but does not have any 'requires' " \
  1055. "section."
  1056. fi
  1057. master_charm=
  1058. while read-0 relation_name relation; do
  1059. if [ "$(echo "$relation" | shyaml get-value "scope" 2>/dev/null)" == "container" ]; then
  1060. # debug "$DARKYELLOW$service$NORMAL's relation" \
  1061. # "$DARKBLUE${relation_name}$NORMAL is a container."
  1062. interface="$(echo "$relation" | shyaml get-value "interface" 2>/dev/null)"
  1063. if [ -z "$interface" ]; then
  1064. err "No ${WHITE}$interface${NORMAL} set for relation $relation_name."
  1065. return 1
  1066. fi
  1067. ## Action provided by relation ?
  1068. target_service=
  1069. while read-0 relation_name candidate_target_service _relation_config _tech_dep; do
  1070. [ "$interface" == "$relation_name" ] && {
  1071. target_service="$candidate_target_service"
  1072. break
  1073. }
  1074. done < <(get_compose_relations "$service")
  1075. if [ -z "$target_service" ]; then
  1076. err "Couldn't find ${WHITE}relations.$interface${NORMAL} in" \
  1077. "${DARKYELLOW}$service$NORMAL compose definition."
  1078. return 1
  1079. fi
  1080. master_charm=$(get_service_charm "$target_service") || return 1
  1081. break
  1082. fi
  1083. done < <(echo "$requires" | shyaml key-values-0 2>/dev/null)
  1084. if [ -z "$master_charm" ]; then
  1085. die "Charm $DARKYELLOW$charm$NORMAL is a subordinate but does not have any relation with" \
  1086. " ${WHITE}scope${NORMAL} set to 'container'."
  1087. fi
  1088. echo "$master_charm" > "$cache_file"
  1089. echo "$master_charm"
  1090. return 0
  1091. }
  1092. export -f _get_master_charm_for_service
  1093. _get_top_master_charm_for_service() {
  1094. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  1095. current_service
  1096. if [ -e "$cache_file" ]; then
  1097. # debug "$FUNCNAME: cache hit ($*)"
  1098. cat "$cache_file"
  1099. return 0
  1100. fi
  1101. current_service="$service"
  1102. while true; do
  1103. master_service=$(_get_master_charm_for_service "$current_service") || return 1
  1104. [ "$master_service" == "$current_service" ] && break
  1105. current_service="$master_service"
  1106. done
  1107. echo "$current_service" | tee "$cache_file"
  1108. return 0
  1109. }
  1110. export -f _get_top_master_charm_for_service
  1111. get_charm_metadata() {
  1112. local charm="$1" metadata_file
  1113. metadata_file="$CHARM_STORE/$charm/metadata.yml"
  1114. if ! [ -e "$metadata_file" ]; then
  1115. return 0 ## No metadata file is as if metadata was empty
  1116. fi
  1117. cat "$metadata_file"
  1118. }
  1119. export -f get_charm_metadata
  1120. ##
  1121. ## The result is a mixin that is not always a complete valid
  1122. ## docker-compose entry (thinking of subordinates). The result
  1123. ## will be merge with master charms.
  1124. get_docker_compose_mixin_from_metadata() {
  1125. local charm="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  1126. metadata_file metadata volumes docker_compose subordinate image
  1127. if [ -e "$cache_file" ]; then
  1128. debug "$FUNCNAME: cache hit ($*)"
  1129. cat "$cache_file"
  1130. return 0
  1131. fi
  1132. mixin=
  1133. metadata="$(get_charm_metadata "$charm")"
  1134. if [ "$metadata" ]; then
  1135. ## resources to volumes
  1136. volumes=$(
  1137. for resource_type in data config; do
  1138. while read-0 resource; do
  1139. eval "echo \" - \$${resource_type^^}STORE/\$charm\$resource:\$resource:rw\""
  1140. done < <(echo "$metadata" | shyaml get-values-0 "${resource_type}-resources" 2>/dev/null)
  1141. done
  1142. while read-0 resource; do
  1143. if [[ "$resource" == *:* ]]; then
  1144. echo " - $resource:rw"
  1145. else
  1146. echo " - $resource:$resource:rw"
  1147. fi
  1148. done < <(echo "$metadata" | shyaml get-values-0 "host-resources" 2>/dev/null)
  1149. while read-0 resource; do
  1150. if ! [ -e "$CHARM_STORE/$charm/resources$resource" ]; then
  1151. err "No '$resource' resource found in ${BLUE}resources/${NORMAL}" \
  1152. "directory of charm $DARKYELLOW$charm$NORMAL."
  1153. exit 1
  1154. fi
  1155. echo " - $CHARM_STORE/$charm/resources$resource:$resource:rw"
  1156. done < <(echo "$metadata" | shyaml get-values-0 "charm-resources" 2>/dev/null)
  1157. ) || return 1
  1158. if [ "$volumes" ]; then
  1159. mixin=$(echo -en "volumes:\n$volumes")
  1160. fi
  1161. docker_compose=$(echo "$metadata" | shyaml get-value "docker-compose" 2>/dev/null)
  1162. if [ "$docker_compose" ]; then
  1163. mixin=$(merge_yaml_str "$mixin" "$docker_compose")
  1164. fi
  1165. if [ "$(echo "$metadata" | shyaml get-value "subordinate" 2>/dev/null)" == "True" ]; then
  1166. subordinate=true
  1167. fi
  1168. fi
  1169. image=$(echo "$metadata" | shyaml get-value "docker-image" 2>/dev/null)
  1170. image_or_build_statement=
  1171. if [ "$image" ]; then
  1172. if [ "$subordinate" ]; then
  1173. err "Subordinate charm can not have a ${WHITE}docker-image${NORMAL} value."
  1174. return 1
  1175. fi
  1176. image_or_build_statement="image: $image"
  1177. elif [ -d "$CHARM_STORE/$charm/build" ]; then
  1178. if [ "$subordinate" ]; then
  1179. err "Subordinate charm can not have a 'build' sub directory."
  1180. return 1
  1181. fi
  1182. image_or_build_statement="build: $charm/build"
  1183. fi
  1184. if [ "$image_or_build_statement" ]; then
  1185. mixin=$(merge_yaml_str "$mixin" "$image_or_build_statement")
  1186. fi
  1187. echo "$mixin" > "$cache_file"
  1188. echo "$mixin"
  1189. }
  1190. export -f get_docker_compose_mixin_from_metadata
  1191. _save() {
  1192. local name="$1"
  1193. cat - | tee -a "$docker_compose_dir/.data/$name"
  1194. }
  1195. export -f _save
  1196. get_default_project_name() {
  1197. if [ "$DEFAULT_PROJECT_NAME" ]; then
  1198. echo "$DEFAULT_PROJECT_NAME"
  1199. return 0
  1200. fi
  1201. compose_yml_location="$(get_compose_yml_location)"
  1202. if [ "$compose_yml_location" ]; then
  1203. if normalized_path=$(readlink -e "$compose_yml_location"); then
  1204. echo "$(basename "$(dirname "$normalized_path")")"
  1205. return 0
  1206. fi
  1207. fi
  1208. echo "project"
  1209. return 0
  1210. }
  1211. export -f get_default_project_name
  1212. launch_docker_compose() {
  1213. docker_compose_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
  1214. #debug "Creating temporary docker-compose directory in '$docker_compose_tmpdir'."
  1215. # trap_add EXIT "debug \"Removing temporary docker-compose directory in $docker_compose_tmpdir.\";\
  1216. # rm -rf \"$docker_compose_tmpdir\""
  1217. trap_add EXIT "rm -rf \"$docker_compose_tmpdir\""
  1218. project=$(get_default_project_name)
  1219. mkdir -p "$docker_compose_tmpdir/$project"
  1220. docker_compose_dir="$docker_compose_tmpdir/$project"
  1221. if [ -z "$SERVICE_PACK" ]; then
  1222. export SERVICE_PACK=$(get_default_target_services $SERVICE_PACK)
  1223. fi
  1224. get_docker_compose $SERVICE_PACK > "$docker_compose_dir/docker-compose.yml" || return 1
  1225. if [ -e "$state_tmpdir/to-merge-in-docker-compose.yml" ]; then
  1226. # debug "Merging some config data in docker-compose.yml:"
  1227. # debug "$(cat $state_tmpdir/to-merge-in-docker-compose.yml)"
  1228. _config_merge "$docker_compose_dir/docker-compose.yml" "$state_tmpdir/to-merge-in-docker-compose.yml" || return 1
  1229. fi
  1230. ## XXXvlab: could be more specific and only link the needed charms
  1231. ln -sf "$CHARM_STORE/"* "$docker_compose_dir/"
  1232. mkdir "$docker_compose_dir/.data"
  1233. {
  1234. {
  1235. cd "$docker_compose_dir"
  1236. debug "${WHITE}docker-compose.yml$NORMAL for $DARKYELLOW$SERVICE_PACK$NORMAL"
  1237. debug "$(cat "$docker_compose_dir/docker-compose.yml")"
  1238. debug "${WHITE}Launching$NORMAL: docker-compose $@"
  1239. docker-compose "$@"
  1240. echo "$?" > "$docker_compose_dir/.data/errlvl"
  1241. } | _save stdout
  1242. } 3>&1 1>&2 2>&3 | _save stderr 3>&1 1>&2 2>&3
  1243. 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
  1244. err "Detected bug https://github.com/docker/docker/issues/4036 ... "
  1245. err "Please re-launch your command, or switch from 'devicemapper' driver to 'overlayfs' or 'aufs'."
  1246. fi
  1247. docker_compose_errlvl="$(cat "$docker_compose_dir/.data/errlvl")"
  1248. if [ -z "docker_compose_dir" ]; then
  1249. err "Something went wrong before you could gather docker-compose errorlevel."
  1250. return 1
  1251. fi
  1252. return "$docker_compose_errlvl"
  1253. }
  1254. export -f launch_docker_compose
  1255. get_compose_yml_location() {
  1256. parent=$(while ! [ -e "./compose.yml" ]; do
  1257. [ "$PWD" == "/" ] && exit 0
  1258. cd ..
  1259. done; echo "$PWD"
  1260. )
  1261. if [ "$parent" ]; then
  1262. echo "$parent/compose.yml"
  1263. return 0
  1264. fi
  1265. if [ "$DEFAULT_COMPOSE_FILE" ]; then
  1266. if ! [ -e "$DEFAULT_COMPOSE_FILE" ]; then
  1267. err "No 'compose.yml' was found in current or parent dirs," \
  1268. "and \$DEFAULT_COMPOSE_FILE points to an unexistent file."
  1269. die "Please provide a 'compose.yml' file."
  1270. fi
  1271. echo "$DEFAULT_COMPOSE_FILE"
  1272. return 0
  1273. fi
  1274. err "No 'compose.yml' was found in current or parent dirs, and no \$DEFAULT_COMPOSE_FILE was set."
  1275. die "Please provide a 'compose.yml' file."
  1276. return 1
  1277. }
  1278. export -f get_compose_yml_location
  1279. get_default_target_services() {
  1280. local services=("$@")
  1281. if [ -z "${services[*]}" ]; then
  1282. if [ "$DEFAULT_SERVICES" ]; then
  1283. info "No service provided, using $WHITE\$DEFAULT_SERVICES$NORMAL variable." \
  1284. "Target services: $DARKYELLOW$DEFAULT_SERVICES$NORMAL"
  1285. services="$DEFAULT_SERVICES"
  1286. else
  1287. err "No service provided."
  1288. return 1
  1289. fi
  1290. fi
  1291. echo "${services[*]}"
  1292. }
  1293. export -f get_default_target_services
  1294. get_master_services() {
  1295. local loaded master_service
  1296. declare -A loaded
  1297. for service in "$@"; do
  1298. master_service=$(_get_top_master_charm_for_service "$service") || return 1
  1299. if [ "${loaded[$master_service]}" ]; then
  1300. continue
  1301. fi
  1302. echo "$master_service"
  1303. loaded["$master_service"]=1
  1304. done | xargs echo
  1305. }
  1306. export -f get_master_services
  1307. _setup_state_dir() {
  1308. export state_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
  1309. #debug "Creating temporary state directory in '$state_tmpdir'."
  1310. # trap_add EXIT "debug \"Removing temporary state directory in $state_tmpdir.\";\
  1311. # rm -rf \"$state_tmpdir\""
  1312. trap_add EXIT "rm -rf \"$state_tmpdir\""
  1313. }
  1314. get_docker_compose_opts_list() {
  1315. local cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  1316. if [ -e "$cache_file" ]; then
  1317. debug "$FUNCNAME: cache hit ($*)"
  1318. cat "$cache_file"
  1319. return 0
  1320. fi
  1321. docker-compose "$@" --help | grep '^Options:' -A 20000 |
  1322. tail -n +2 |
  1323. egrep "^\s+-" |
  1324. sed -r 's/\s+((((-[a-zA-Z]|--[a-zA-Z0-9-]+)( [A-Z=]+|=[^ ]+)?)(, )?)+)\s+.*$/\1/g' |
  1325. tee "$cache_file"
  1326. }
  1327. _MULTIOPTION_REGEX='^((-[a-zA-Z]|--[a-zA-Z0-9-]+)(, )?)+'
  1328. _MULTIOPTION_REGEX_LINE_FILTER=$_MULTIOPTION_REGEX'(\s|=)'
  1329. get_docker_compose_multi_opts_list() {
  1330. opts_list=$(get_docker_compose_opts_list "$@")
  1331. echo "$opts_list" | egrep "$_MULTIOPTION_REGEX_LINE_FILTER" |
  1332. sed -r "s/^($_MULTIOPTION_REGEX)(\s|=).*$/\1/g" |
  1333. tr ',' "\n" | xargs echo
  1334. }
  1335. get_docker_compose_single_opts_list() {
  1336. opts_list=$(get_docker_compose_opts_list "$@")
  1337. echo "$opts_list" | egrep -v "$_MULTIOPTION_REGEX_LINE_FILTER" |
  1338. tr ',' "\n" | xargs echo
  1339. }
  1340. [ "$SOURCED" ] && return 0
  1341. if [ -z "$DISABLE_SYSTEM_CONFIG_FILE" ]; then
  1342. if [ -r /etc/default/charm ]; then
  1343. . /etc/default/charm
  1344. fi
  1345. if [ -r "/etc/default/$exname" ]; then
  1346. . "/etc/default/$exname"
  1347. fi
  1348. ## XXXvlab: should provide YML config opportunities in possible parent dirs ?
  1349. ## userdir ? and global /etc/compose.yml ?
  1350. . /etc/compose.conf
  1351. . /etc/compose.local.conf
  1352. fi
  1353. _setup_state_dir
  1354. ##
  1355. ## Argument parsing
  1356. ##
  1357. remainder_args=()
  1358. compose_opts=()
  1359. action_opts=()
  1360. no_hooks=
  1361. no_init=
  1362. action=
  1363. stage="main" ## switches from 'main', to 'action', 'remainder'
  1364. # DC_MATCH_MULTI=
  1365. # DC_MATCH_SINGLE=
  1366. while [ "$#" != 0 ]; do
  1367. case "$stage" in
  1368. "main")
  1369. case "$1" in
  1370. --help|-h)
  1371. no_init=true ; no_hooks=true ; no_relations=true
  1372. compose_opts+=("$1")
  1373. ;;
  1374. --verbose|-v)
  1375. export VERBOSE=true
  1376. compose_opts+=("$1")
  1377. ;;
  1378. -f)
  1379. [ -e "$2" ] || die "File $2 doesn't exists"
  1380. export DEFAULT_COMPOSE_FILE="$2"
  1381. shift
  1382. ;;
  1383. -p)
  1384. export DEFAULT_PROJECT_NAME="$2"
  1385. shift
  1386. ;;
  1387. --no-relations)
  1388. export no_relations=true
  1389. ;;
  1390. --no-hooks)
  1391. export no_hooks=true
  1392. ;;
  1393. --no-init)
  1394. export no_init=true
  1395. ;;
  1396. --debug)
  1397. export DEBUG=true
  1398. export VERBOSE=true
  1399. ;;
  1400. --*|-*)
  1401. compose_opts+=("$1")
  1402. ;;
  1403. *)
  1404. action="$1"
  1405. stage="action"
  1406. # DC_MATCH_MULTI=$(get_docker_compose_multi_opts_list "$action") || return 1
  1407. # DC_MATCH_SINGLE="$(get_docker_compose_single_opts_list "$action") $(echo "$DC_MATCH_MULTI" | sed -r 's/( |$)/=\* /g')"
  1408. ;;
  1409. esac
  1410. ;;
  1411. "action")
  1412. case "$1" in
  1413. --help|-h)
  1414. no_init=true ; no_hooks=true ; no_relations=true
  1415. action_opts+=("$1")
  1416. ;;
  1417. --verbose|-v)
  1418. export VERBOSE=true
  1419. action_opts+=("$1")
  1420. ;;
  1421. --*|-*)
  1422. action_opts+=("$1")
  1423. ;;
  1424. *)
  1425. action_posargs+=("$1")
  1426. stage="remainder"
  1427. ;;
  1428. esac
  1429. ;;
  1430. "remainder")
  1431. remainder_args+=("$@")
  1432. break 3;;
  1433. esac
  1434. shift
  1435. done
  1436. [ "${compose_opts[*]}" ] && debug "Main docker-compose opts: ${compose_opts[*]}"
  1437. [ "${action_opts[*]}" ] && debug "Action '$action' opts: ${action_opts[*]}"
  1438. [ "${remainder_args[*]}" ] && debug "Remainder args: ${remainder_args[*]}"
  1439. ##
  1440. ## Actual code
  1441. ##
  1442. export CHARM_STORE=${CHARM_STORE:-/srv/charm-store}
  1443. export DOCKER_DATASTORE=${DOCKER_DATASTORE:-/srv/docker-datastore}
  1444. export COMPOSE_YML_FILE=$(get_compose_yml_location) || exit 1
  1445. debug "Found 'compose.yml' file in '$COMPOSE_YML_FILE'."
  1446. if ! [ -d "$CHARM_STORE" ]; then
  1447. err "Charm store path $YELLOW$CHARM_STORE$NORMAL does not exists. "
  1448. err "Please check your $YELLOW\$CHARM_STORE$NORMAL variable value."
  1449. exit 1
  1450. fi
  1451. if [ -z "$(cd "$CHARM_STORE"; ls)" ]; then
  1452. err "no available charms in charm store $YELLOW$CHARM_STORE$NORMAL. Either:"
  1453. err " - check $YELLOW\$CHARM_STORE$NORMAL variable value"
  1454. err " - download charms in $CHARM_STORE"
  1455. print_error "Charm store is empty. Cannot continue."
  1456. fi
  1457. ##
  1458. ## Get services in command line.
  1459. ##
  1460. is_service_action=
  1461. case "$action" in
  1462. up|build|start|stop|config)
  1463. services="$(get_default_target_services "${action_posargs[@]}")" || exit 1
  1464. orig_services="${action_posargs[@]:1}"
  1465. ;;
  1466. run)
  1467. services="${action_posargs[0]}"
  1468. ;;
  1469. "")
  1470. services=
  1471. ;;
  1472. *)
  1473. if is_service_action=$(has_service_action "${action_posargs[0]}" "$action"); then
  1474. debug "Found action $DARKCYAN$action$NORMAL in service $DARKYELLOW${action_posargs[0]}$NORMAL"
  1475. services="${action_posargs[0]}"
  1476. else
  1477. services="$(get_default_target_services "${action_posargs[@]}")"
  1478. fi
  1479. ;;
  1480. esac
  1481. get_docker_compose $services >/dev/null || { ## precalculate variable \$_current_docker_compose
  1482. err "Fails to compile base 'docker-conmpose.conf'"
  1483. exit 1
  1484. }
  1485. ##
  1486. ## Pre-action
  1487. ##
  1488. full_init=
  1489. case "$action" in
  1490. up|run)
  1491. full_init=true
  1492. ;;
  1493. "")
  1494. full_init=
  1495. ;;
  1496. *)
  1497. if [ "$is_service_action" ]; then
  1498. full_init=true
  1499. fi
  1500. ;;
  1501. esac
  1502. if [ "$full_init" ]; then
  1503. ## init in order
  1504. Section initialisation
  1505. if [ -z "$no_init" ]; then
  1506. run_service_hook "$services" init || exit 1
  1507. fi
  1508. ## Get relations
  1509. if [ -z "$no_relations" ]; then
  1510. run_service_relations "$services" || exit 1
  1511. fi
  1512. ## XXXvlab: to be removed when all relation and service stuff is resolved
  1513. if [ -z "$no_hooks" ]; then
  1514. ordered_services=$(get_ordered_service_dependencies $services) || exit 1
  1515. for service in $ordered_services; do
  1516. charm=$(get_service_charm "$service") || exit 1
  1517. for script in "$CHARM_STORE/$charm/hooks.d/"*.sh; do
  1518. [ -e "$script" ] || continue
  1519. [ -x "$script" ] || { echo "compose: script $script is not executable." >&2; exit 1; }
  1520. (
  1521. debug "Launching '$script'."
  1522. cd "$(dirname "$script)")";
  1523. "$script" "$@"
  1524. ) || { echo "compose: hook $script failed. Stopping." >&2; exit 1; }
  1525. done
  1526. done
  1527. fi
  1528. fi
  1529. export SERVICE_PACK="$services"
  1530. ##
  1531. ## Docker-compose
  1532. ##
  1533. case "$action" in
  1534. up|start|stop|build)
  1535. master_services=$(get_master_services $SERVICE_PACK) || exit 1
  1536. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" $master_services
  1537. ;;
  1538. run)
  1539. master_service=$(get_master_services $SERVICE_PACK) || exit 1
  1540. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "$master_service" "${remainder_args[@]}"
  1541. ;;
  1542. # enter)
  1543. # master_service=$(get_master_services $SERVICE_PACK) || exit 1
  1544. # [ "${remainder_args[*]}" ] || remainder_args=("/bin/bash" "-c" "export TERM=xterm; exec bash")
  1545. # docker exec -ti "${action_opts[@]}" "$master_service" "${remainder_args[@]}"
  1546. # ;;
  1547. config)
  1548. ## removing the services
  1549. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "${remainder_args[@]}"
  1550. warn "Runtime configuration modification (from relations) are not included here."
  1551. ;;
  1552. *)
  1553. if [ "$is_service_action" ]; then
  1554. run_service_action "$SERVICE_PACK" "$action" "${remainder_args[@]}"
  1555. else
  1556. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "${remainder_args[@]}"
  1557. fi
  1558. ;;
  1559. esac