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.

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