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.

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