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.

1839 lines
60 KiB

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