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.

2383 lines
78 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. #!/bin/bash
  2. ##
  3. ## TODO:
  4. ## - subordinate container should really be able to modify base image of their master
  5. ## - this could be done through docker-update
  6. ## - I'm not happy with the current build using 'build/' directory, this should be
  7. ## changed to:
  8. ## - always have a base image (specified in metadata), and always have hooks/install
  9. ## executed and merge in image (like docker-build-charm).
  10. ## - container base image is ALWAYS the image of the master container... this brings
  11. ## questions about a double way to express inheritage (through relations as it is
  12. ## implemented now, or through this base-image ?)
  13. ## - the name of the scripts for relation (aka relation_name-relation-joined) is bad as
  14. ## reading the name in a hooks/ dir, there are no way to know if we are the target or
  15. ## the base of the relation.
  16. ## - we could leverage a 'relations/' dir on the root of the charm, with both:
  17. ## 'relations/provide/relation_name' and 'relations/receive/relation_name'
  18. ## - a very bad point with the actual naming is that we can't have a providing AND
  19. ## receiving a relation with same name.
  20. ## - The cache system should keep md5 of docker-compose and other things between runs
  21. ## - The cache system should use underlying function that have only arguments inputs.
  22. ## This will allow to cache completely without issues function in time.
  23. ## - would probably need instrospection in charm custom action to know if these need
  24. ## init or relations to be set up.
  25. #:-
  26. [ -e /etc/shlib ] && . /etc/shlib || {
  27. echo "Unsatisfied dependency. Please install 'kal-shlib-core'."
  28. exit 1
  29. }
  30. #:-
  31. include pretty
  32. include parse
  33. include charm
  34. depends shyaml docker
  35. version=0.1
  36. usage="$exname [COMPOSE_OPTS] [ACTION [ACTION_OPTS]]"
  37. help="\
  38. $WHITE$exname$NORMAL jobs is to run various shell scripts to build
  39. a running orchestrated and configured docker containers. These shell
  40. scripts will have the opportunity to build a 'docker-compose.yml'.
  41. Once init script and relations scripts are executed, $WHITE$exname$NORMAL
  42. delegate the launching to ${WHITE}docker-compose${NORMAL} by providing it
  43. the final 'docker-compose.yml'.
  44. $WHITE$exname$NORMAL also leverage charms to offer some additional custom
  45. actions per charm, which are simply other scripts that can be
  46. run without launching ${WHITE}docker-compose${NORMAL}.
  47. In compose message, color coding is enforced as such:
  48. - ${DARKCYAN}action$NORMAL,
  49. - ${DARKBLUE}relation$NORMAL,
  50. - ${DARKYELLOW}charm${NORMAL}/${DARKYELLOW}service${NORMAL},
  51. - ${WHITE}option-name${NORMAL}/${WHITE}command-name${NORMAL}/${WHITE}Section-Title${NORMAL}
  52. $WHITE$exname$NORMAL reads '/etc/compose.conf' for global variables, and
  53. '/etc/compose.local.conf' for local host adjustements.
  54. ${WHITE}$exname Options${NORMAL}:
  55. -h, --help Print this message and quit
  56. (ignoring any other options)
  57. -V, --version Print current version and quit
  58. (ignoring any other options)
  59. -v, --verbose Be more verbose
  60. -d, --debug Print full debugging information (sets also verbose)
  61. "
  62. [[ "${BASH_SOURCE[0]}" != "${0}" ]] && SOURCED=true
  63. export CACHEDIR=/var/cache/compose
  64. export VARDIR=/var/lib/compose
  65. md5_compat() { md5sum | cut -c -32; }
  66. quick_cat_file() { quick_cat_stdin < "$1"; }
  67. quick_cat_stdin() { local IFS=''; while read -r line; do echo "$line"; done ; }
  68. export -f quick_cat_file quick_cat_stdin md5_compat
  69. clean_cache() {
  70. local i=0
  71. for f in $(ls -t "$CACHEDIR/"*.cache.* 2>/dev/null | tail -n +500); do
  72. ((i++))
  73. rm -f "$f"
  74. done
  75. if (( i > 0 )); then
  76. debug "${WHITE}Cleaned cache:${NORMAL} Removed $((i)) elements (current cache size is $(du -sh "$CACHEDIR" | cut -f 1))"
  77. fi
  78. }
  79. trap_add "EXIT" clean_cache
  80. usage="$exname CHARM"'
  81. Deploy and manage a swarm of containers to provide services based on
  82. a ``compose.yml`` definition and charms from a ``charm-store``.
  83. '
  84. export DEFAULT_COMPOSE_FILE
  85. ##
  86. ## Merge YAML files
  87. ##
  88. export _merge_yaml_common_code="
  89. import sys
  90. import yaml
  91. try:
  92. # included in standard lib from Python 2.7
  93. from collections import OrderedDict
  94. except ImportError:
  95. # try importing the backported drop-in replacement
  96. # it's available on PyPI
  97. from ordereddict import OrderedDict
  98. ## Ensure that there are no collision with legacy OrderedDict
  99. ## that could be used for omap for instance.
  100. class MyOrderedDict(OrderedDict):
  101. pass
  102. yaml.add_representer(
  103. MyOrderedDict,
  104. lambda cls, data: cls.represent_dict(data.items()))
  105. yaml.add_constructor(
  106. yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
  107. lambda cls, node: MyOrderedDict(cls.construct_pairs(node)))
  108. def fc(filename):
  109. with open(filename) as f:
  110. return f.read()
  111. def merge(*args):
  112. # sys.stderr.write('%r\n' % (args, ))
  113. args = [arg for arg in args if arg is not None]
  114. if len(args) == 0:
  115. return None
  116. if len(args) == 1:
  117. return args[0]
  118. if all(isinstance(arg, (int, basestring, bool)) for arg in args):
  119. return args[-1]
  120. elif all(isinstance(arg, list) for arg in args):
  121. res = []
  122. for arg in args:
  123. for elt in arg:
  124. if elt in res:
  125. res.remove(elt)
  126. res.append(elt)
  127. return res
  128. elif all(isinstance(arg, dict) for arg in args):
  129. keys = set()
  130. for arg in args:
  131. keys |= set(arg.keys())
  132. dct = {}
  133. for key in keys:
  134. sub_args = []
  135. for arg in args:
  136. if key in arg:
  137. sub_args.append(arg)
  138. try:
  139. dct[key] = merge(*(a[key] for a in sub_args))
  140. except NotImplementedError as e:
  141. raise NotImplementedError(
  142. e.args[0],
  143. '%s.%s' % (key, e.args[1]) if e.args[1] else key,
  144. e.args[2])
  145. if dct[key] is None:
  146. del dct[key]
  147. return dct
  148. else:
  149. raise NotImplementedError(
  150. 'Unsupported types: %s'
  151. % (', '.join(list(set(arg.__class__.__name__ for arg in args)))), '', args)
  152. return None
  153. def merge_cli(*args):
  154. try:
  155. c = merge(*args)
  156. except NotImplementedError as e:
  157. sys.stderr.write('%s. Conflicting key is %r. Values are:\n%s\n' % (e.args[0], e.args[1], e.args[2]))
  158. exit(1)
  159. if c is not None:
  160. print '%s' % yaml.dump(c, default_flow_style=False)
  161. "
  162. merge_yaml() {
  163. if ! [ -r "$state_tmpdir/merge_yaml.py" ]; then
  164. cat <<EOF > "$state_tmpdir/merge_yaml.py"
  165. $_merge_yaml_common_code
  166. merge_cli(*(yaml.load(fc(f)) for f in sys.argv[1:]))
  167. EOF
  168. fi
  169. python "$state_tmpdir/merge_yaml.py" "$@"
  170. }
  171. export -f merge_yaml
  172. merge_yaml_str() {
  173. local entries="$@"
  174. if ! [ -r "$state_tmpdir/merge_yaml_str.py" ]; then
  175. cat <<EOF > "$state_tmpdir/merge_yaml_str.py"
  176. $_merge_yaml_common_code
  177. merge_cli(*(yaml.load(f) for f in sys.argv[1:]))
  178. EOF
  179. fi
  180. python "$state_tmpdir/merge_yaml_str.py" "$@"
  181. }
  182. export -f merge_yaml_str
  183. yaml_key_val_str() {
  184. local entries="$@"
  185. if ! [ -r "$state_tmpdir/yaml_key_val_str.py" ]; then
  186. cat <<EOF > "$state_tmpdir/yaml_key_val_str.py"
  187. $_merge_yaml_common_code
  188. print '%s' % yaml.dump({
  189. yaml.load(sys.argv[1]):
  190. yaml.load(sys.argv[2])}, default_flow_style=False)
  191. EOF
  192. fi
  193. python "$state_tmpdir/yaml_key_val_str.py" "$@"
  194. }
  195. export -f yaml_key_val_str
  196. ##
  197. ## Docker
  198. ##
  199. docker_has_image() {
  200. local image="$1"
  201. images=$(docker images -q "$image" 2>/dev/null) || {
  202. err "docker images call has failed unexpectedly."
  203. return 1
  204. }
  205. [ "$images" ]
  206. }
  207. export -f docker_has_image
  208. cmd_on_base_image() {
  209. local charm="$1"
  210. shift
  211. base_image=$(service_base_docker_image "$charm") || return 1
  212. docker run -i --entrypoint /bin/bash "$base_image" -c "$*"
  213. }
  214. export -f cmd_on_base_image
  215. cached_cmd_on_base_image() {
  216. local charm="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  217. shift
  218. if [ -e "$cache_file" ]; then
  219. # debug "$FUNCNAME: cache hit ($*)"
  220. quick_cat_stdin < "$cache_file"
  221. return 0
  222. fi
  223. base_image=$(service_base_docker_image "$charm") || return 1
  224. result=$(cmd_on_base_image "$charm" "$@") || return 1
  225. echo "$result" | tee "$cache_file"
  226. }
  227. export -f cached_cmd_on_base_image
  228. image_exposed_ports_0() {
  229. local image="$1"
  230. docker inspect --format='{{range $p, $conf := .Config.ExposedPorts}}{{$p}}{{"\x00"}}{{end}}' "$image"
  231. }
  232. export -f image_exposed_ports_0
  233. ##
  234. ## Generic
  235. ##
  236. fn.exists() {
  237. declare -F "$1" >/dev/null
  238. }
  239. str_matches() {
  240. local str="$1"
  241. shift
  242. for pattern in "$@"; do
  243. eval "[[ \"$str\" == $pattern ]]" && return 0
  244. done
  245. return 1
  246. }
  247. gen_password() {
  248. local l=( {a..z} {A..Z} {0..9} ) nl="${#l[@]}" size=${1:-16}
  249. while ((size--)); do
  250. echo -n "${l[$((RANDOM * nl / 32768))]}"
  251. done
  252. echo
  253. }
  254. export -f gen_password
  255. file_put() {
  256. local TARGET="$1"
  257. mkdir -p "$(dirname "$TARGET")" &&
  258. cat - > "$TARGET"
  259. }
  260. export -f file_put
  261. file_put_0() {
  262. local TARGET="$1"
  263. mkdir -p "$(dirname "$TARGET")" &&
  264. cat > "$TARGET"
  265. }
  266. export -f file_put_0
  267. fetch_file() {
  268. local src="$1"
  269. case "$src" in
  270. *"://"*)
  271. err "Unsupported target scheme."
  272. return 1
  273. ;;
  274. *)
  275. ## Try direct
  276. if ! [ -r "$src" ]; then
  277. err "File '$src' not found/readable."
  278. return 1
  279. fi
  280. cat "$src" || return 1
  281. ;;
  282. esac
  283. }
  284. export -f fetch_file
  285. ## receives stdin content to decompress on stdout
  286. ## stdout content should be tar format.
  287. uncompress_file() {
  288. local filename="$1"
  289. ## Warning, the content of the file is already as stdin, the filename
  290. ## is there to hint for correct decompression.
  291. case "$filename" in
  292. *".gz")
  293. gunzip
  294. ;;
  295. *".bz2")
  296. bunzip2
  297. ;;
  298. *)
  299. cat
  300. ;;
  301. esac
  302. }
  303. export -f uncompress_file
  304. get_file() {
  305. local src="$1"
  306. fetch_file "$src" | uncompress_file "$src"
  307. }
  308. export -f get_file
  309. ##
  310. ## Common database lib
  311. ##
  312. _clean_docker() {
  313. local _DB_NAME="$1" container_id="$2"
  314. (
  315. set +e
  316. debug "Removing container $_DB_NAME"
  317. docker stop "$container_id"
  318. docker rm "$_DB_NAME"
  319. rm -vf "/tmp/${_DB_NAME}.state"
  320. )
  321. }
  322. export -f _clean_docker
  323. get_service_base_image_dir_uid_gid() {
  324. local service="$1" dir="$2" uid_gid
  325. uid_gid=$(cached_cmd_on_base_image "$service" "stat -c '%u %g' '$dir'") || {
  326. debug "Failed to query '$dir' uid in ${DARKYELLOW}$service${NORMAL} base image."
  327. return 1
  328. }
  329. info "uid and gid from ${DARKYELLOW}$service${NORMAL}:$dir is '$uid_gid'"
  330. echo "$uid_gid"
  331. }
  332. export -f get_service_base_image_dir_uid_gid
  333. are_files_locked_in_dir() {
  334. local dir="$1" device hdev ldev
  335. device=$(stat -c %d "$dir") || {
  336. err "Can't stat %d."
  337. return 1
  338. }
  339. device=$(printf "%04x" $device)
  340. hdev=${device:0:2}
  341. ldev=${device:2:2}
  342. inodes=$(find "$dir" -printf ':%i:\n')
  343. found=
  344. while read -r inode; do
  345. debug "try inode:$inode"
  346. if [[ "$inodes" == *":$inode:"* ]]; then
  347. found=1
  348. break
  349. fi
  350. done < <(cat /proc/locks | grep " $hdev:$ldev:" | sed -r "s/^.*$hdev:$ldev:([0-9]+).*$/\1/g")
  351. [ "$found" ]
  352. }
  353. export -f are_files_locked_in_dir
  354. export _PID="$$"
  355. ensure_db_docker_running () {
  356. _DB_NAME="db_${DB_NAME}_${_PID}"
  357. if [ -e "/tmp/${_DB_NAME}.state" ]; then
  358. export DOCKER_IP="$(cat "/tmp/${_DB_NAME}.state")"
  359. debug "Re-using previous docker/connection '$DOCKER_IP'."
  360. _set_db_params "$DOCKER_IP"
  361. return 0
  362. fi
  363. if [ -e "/tmp/${_DB_NAME}.working" ]; then
  364. ## avoid recursive calls.
  365. if [ -z "$DOCKER_IP" ]; then
  366. err "Currently figuring up DOCKER_IP, please set it yourself before this call if needed."
  367. return 1
  368. else
  369. debug "Ignoring recursive call."
  370. fi
  371. return 0
  372. fi
  373. touch "/tmp/${_DB_NAME}.working"
  374. docker rm "$_DB_NAME" 2>/dev/null || true
  375. host_db_working_dir="$DATASTORE/${SERVICE_NAME}$DB_DATADIR"
  376. if is_db_locked; then
  377. info "Some process is using '$host_db_working_dir'. Trying to find a docker that would do this..."
  378. found=
  379. for docker_id in $(docker ps -q); do
  380. has_volume_mounted=$(
  381. docker inspect \
  382. --format "{{range .Mounts}}{{if eq .Destination \"$DB_DATADIR\"}}{{.Source}}{{end}}{{end}}" \
  383. "$docker_id")
  384. if [ "$has_volume_mounted" == "$host_db_working_dir" ]; then
  385. found="$docker_id"
  386. break
  387. fi
  388. done
  389. if [ -z "$found" ]; then
  390. err "Please shutdown any other docker using this directory."
  391. return 1
  392. fi
  393. export container_id="$found"
  394. info "Found docker $docker_id is already running."
  395. else
  396. verb "Database is not locked."
  397. if ! docker_has_image "$DOCKER_BASE_IMAGE"; then
  398. docker pull "$DOCKER_BASE_IMAGE"
  399. fi
  400. docker_opts=
  401. if [ -f "$DB_PASSFILE" ]; then
  402. verb "Found and using '$DB_PASSFILE'."
  403. docker_opts="$db_docker_opts -v $SERVER_ROOT_PREFIX$DB_PASSFILE:$DB_PASSFILE"
  404. fi
  405. debug docker run -d \
  406. --name "$_DB_NAME" \
  407. $docker_opts \
  408. -v "$host_db_working_dir:$DB_DATADIR" \
  409. "$DOCKER_BASE_IMAGE"
  410. if ! container_id=$(
  411. docker run -d \
  412. --name "$_DB_NAME" \
  413. $docker_opts \
  414. -v "$host_db_working_dir:$DB_DATADIR" \
  415. "$DOCKER_BASE_IMAGE"
  416. ); then
  417. err "'docker run' failed !"
  418. _clean_docker "$_DB_NAME" "$container_id"
  419. rm "/tmp/${_DB_NAME}.working"
  420. return 1
  421. fi
  422. trap_add EXIT,ERR "_clean_docker '$_DB_NAME' '$container_id'"
  423. fi
  424. if DOCKER_IP=$(wait_for_docker_ip "$container_id"); then
  425. echo "$DOCKER_IP" > "/tmp/${_DB_NAME}.state"
  426. debug "written /tmp/${_DB_NAME}.state"
  427. rm "/tmp/${_DB_NAME}.working"
  428. _set_db_params "$DOCKER_IP"
  429. return 0
  430. else
  431. err "Db not found. Docker logs follows:"
  432. docker logs --tail=5 "$container_id" 2>&1 | prefix " | " >&2
  433. rm "/tmp/${_DB_NAME}.working"
  434. return 1
  435. fi
  436. }
  437. export -f ensure_db_docker_running
  438. ## Require to set $db_docker_opts if needed, and $DB_PASSFILE
  439. ##
  440. _dcmd() {
  441. local docker_opts command="$1"
  442. shift
  443. debug "Db> $command $@"
  444. if [ -f "$DB_PASSFILE" ]; then
  445. verb "Found and using '$DB_PASSFILE'."
  446. db_docker_opts="$db_docker_opts -v $SERVER_ROOT_PREFIX$DB_PASSFILE:$DB_PASSFILE"
  447. fi
  448. debug docker run -i --rm \
  449. $db_docker_opts \
  450. --entrypoint "$command" "$DOCKER_BASE_IMAGE" $db_cmd_opts "$@"
  451. docker run -i --rm \
  452. $db_docker_opts \
  453. --entrypoint "$command" "$DOCKER_BASE_IMAGE" $db_cmd_opts "$@"
  454. }
  455. export -f _dcmd
  456. ## Executes code through db
  457. dcmd () {
  458. [ "$DB_NAME" ] || print_syntax_error "$FUNCNAME: You must provide \$DB_NAME."
  459. [ "$DB_DATADIR" ] || print_syntax_error "$FUNCNAME: You must provide \$DB_DATADIR."
  460. [ "$DB_PASSFILE" ] || print_syntax_error "$FUNCNAME: You must provide \$DB_PASSFILE."
  461. [ "$_PID" ] || print_syntax_error "$FUNCNAME: You must provide \$_PID."
  462. [ "$(type -t is_db_locked)" == "function" ] || print_syntax_error "$FUNCNAME: You must provide function 'is_db_locked'."
  463. [ "$(type -t _set_db_params)" == "function" ] || print_syntax_error "$FUNCNAME: You must provide function '_set_db_params'."
  464. [ "$(type -t ddb)" == "function" ] || print_syntax_error "$FUNCNAME: You must provide function 'ddb'."
  465. ensure_db_docker_running </dev/null || return 1
  466. _dcmd "$@"
  467. }
  468. export -f dcmd
  469. ## Warning: requires a ``ddb`` matching current database to be checked
  470. wait_for_docker_ip() {
  471. local name=$1 timeout=5 timeout_count=0 DOCKER_IP= DB_OK=
  472. while [ -z "$DOCKER_IP" ]; do
  473. sleep 1
  474. DOCKER_IP=$(docker inspect --format='{{.NetworkSettings.IPAddress}}' "$name" 2>/dev/null)
  475. verb "[1/2] Waiting for docker $name... ($[timeout_count + 1]/$timeout)"
  476. ((timeout_count++)) || true
  477. if [ "$timeout_count" == "$timeout" ]; then
  478. err "${RED}timeout error${NORMAL}(${timeout}s):"\
  479. "Could not find '$name' docker" \
  480. "container's IP."
  481. return 1
  482. fi
  483. done
  484. verb "[1/2] Found docker $name IP: $DOCKER_IP"
  485. timeout_count=0
  486. DB_OK=
  487. _set_db_params "$DOCKER_IP"
  488. while [ -z "$DB_OK" ]; do
  489. sleep 1
  490. echo "SELECT 1;" | ddb >/dev/null && DB_OK=1
  491. verb "[2/2] Waiting for db service from docker $name... ($[timeout_count + 1]/$timeout)"
  492. ((timeout_count++)) || true
  493. if [ "$timeout_count" == "$timeout" ]; then
  494. err "${RED}timeout error${NORMAL}(${timeout}s):"\
  495. "Could not connect to db on $DOCKER_IP." \
  496. "container's IP."
  497. return 1
  498. fi
  499. done
  500. verb "[2/2] Db is ready !"
  501. echo "$DOCKER_IP"
  502. return 0
  503. }
  504. export -f wait_for_docker_ip
  505. ##
  506. ## Arrays
  507. ##
  508. array_values_to_stdin() {
  509. local e
  510. if [ "$#" -ne "1" ]; then
  511. print_syntax_warning "$FUNCNAME: need one argument."
  512. return 1
  513. fi
  514. var="$1"
  515. eval "for e in \"\${$var[@]}\"; do echo -en \"\$e\\0\"; done"
  516. }
  517. array_keys_to_stdin() {
  518. local e
  519. if [ "$#" -ne "1" ]; then
  520. print_syntax_warning "$FUNCNAME: need one argument."
  521. return 1
  522. fi
  523. var="$1"
  524. eval "for e in \"\${!$var[@]}\"; do echo -en \"\$e\\0\"; done"
  525. }
  526. array_kv_to_stdin() {
  527. local e
  528. if [ "$#" -ne "1" ]; then
  529. print_syntax_warning "$FUNCNAME: need one argument."
  530. return 1
  531. fi
  532. var="$1"
  533. eval "for e in \"\${!$var[@]}\"; do echo -n \"\$e\"; echo -en '\0'; echo -n \"\${$var[\$e]}\"; echo -en '\0'; done"
  534. }
  535. array_pop() {
  536. local narr="$1" nres="$2"
  537. for key in $(eval "echo \${!$narr[@]}"); do
  538. eval "$nres=\${$narr[\"\$key\"]}"
  539. eval "unset $narr[\"\$key\"]"
  540. return 0
  541. done
  542. }
  543. export -f array_pop
  544. array_member() {
  545. local src elt
  546. src="$1"
  547. elt="$2"
  548. while read-0 key; do
  549. if [ "$(eval "echo -n \"\${$src[\$key]}\"")" == "$elt" ]; then
  550. return 0
  551. fi
  552. done < <(array_keys_to_stdin "$src")
  553. return 1
  554. }
  555. export -f array_member
  556. ##
  557. ## Internal Process
  558. ##
  559. get_docker_compose_links() {
  560. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  561. links charm charm_part master_charm
  562. if [ -z "$service" ]; then
  563. print_syntax_error "$FUNCNAME: Please specify a service as first argument."
  564. return 1
  565. fi
  566. if [ -e "$cache_file" ]; then
  567. # debug "$FUNCNAME: cache hit ($*)"
  568. cat "$cache_file"
  569. return 0
  570. fi
  571. master_charm=$(get_top_master_charm_for_service "$service") || return 1
  572. deps=()
  573. while read-0 relation_name target_service relation_config tech_dep; do
  574. master_target_charm="$(get_top_master_charm_for_service "$target_service")"
  575. [ "$master_charm" == "$master_target_charm" ] && continue
  576. if [ "$tech_dep" == "reversed" ]; then
  577. deps+=("$(echo -en "$master_target_charm:\n links:\n - $master_charm")")
  578. elif [ "$tech_dep" == "True" ]; then
  579. deps+=("$(echo -en "$master_charm:\n links:\n - $master_target_charm")")
  580. fi
  581. done < <(get_compose_relations "$service") || return 1
  582. merge_yaml_str "${deps[@]}" | tee "$cache_file"
  583. }
  584. _get_docker_compose_opts() {
  585. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  586. links charm charm_part master_charm
  587. if [ -z "$service" ]; then
  588. print_syntax_error "$FUNCNAME: Please specify a service as first argument."
  589. return 1
  590. fi
  591. if [ -e "$cache_file" ]; then
  592. # debug "$FUNCNAME: cache hit ($*)"
  593. cat "$cache_file"
  594. return 0
  595. fi
  596. compose_def="$(get_compose_service_def "$service")" || return 1
  597. master_charm="$(get_top_master_charm_for_service "$service")"
  598. docker_compose_opts=$(echo "$compose_def" | shyaml get-value "docker-compose" 2>/dev/null)
  599. if [ "$docker_compose_opts" ]; then
  600. yaml_key_val_str "$master_charm" "$docker_compose_opts"
  601. fi | tee "$cache_file"
  602. }
  603. ##
  604. ## By Reading the metadata.yml, we create a docker-compose.yml mixin.
  605. ## Some metadata.yml (of subordinates) will indeed modify other
  606. ## services than themselves.
  607. _get_docker_compose_service_mixin() {
  608. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" links charm charm_part
  609. if [ -z "$service" ]; then
  610. print_syntax_error "$FUNCNAME: Please specify a service as first argument."
  611. return 1
  612. fi
  613. if [ -e "$cache_file" ]; then
  614. # debug "$FUNCNAME: cache hit ($*)"
  615. cat "$cache_file"
  616. return 0
  617. fi
  618. master_charm=$(get_top_master_charm_for_service "$service") || return 1
  619. ## The compose part
  620. links_yaml=$(get_docker_compose_links "$service") || return 1
  621. docker_compose_options=$(_get_docker_compose_opts "$service") || return 1
  622. ## the charm part
  623. #debug "Get charm name from service name $DARKYELLOW$service$NORMAL."
  624. charm=$(get_service_charm "$service") || return 1
  625. charm_part=$(get_docker_compose_mixin_from_metadata "$charm") || return 1
  626. ## Merge results
  627. if [ "$charm_part" ]; then
  628. charm_yaml="$(yaml_key_val_str "$master_charm" "$charm_part")" || return 1
  629. merge_yaml_str "$links_yaml" "$charm_yaml" "$docker_compose_options" || return 1
  630. else
  631. echo "$links_yaml"
  632. fi | tee "$cache_file"
  633. }
  634. export -f _get_docker_compose_service_mixin
  635. ##
  636. ## Get full `docker-compose.yml` format for all listed services (and
  637. ## their deps)
  638. ##
  639. ## @export
  640. ## @cache: !system !nofail +stdout
  641. get_docker_compose () {
  642. local cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)" entries services service start
  643. if [ -e "$cache_file" ]; then
  644. # debug "$FUNCNAME: cache hit ($*)"
  645. cat "$cache_file"
  646. return 0
  647. fi
  648. ##
  649. ## Adding sub services configurations
  650. ##
  651. declare -A entries
  652. start_compilation=$SECONDS
  653. debug "Compiling 'docker-compose.yml' base for $DARKYELLOW$@$NORMAL..."
  654. for target_service in "$@"; do
  655. start=$SECONDS
  656. services=$(get_ordered_service_dependencies "$target_service") || return 1
  657. debug " $DARKYELLOW$target_service$NORMAL deps:$DARKYELLOW" $services "$NORMAL$GRAY(in $((SECONDS - start))s)$NORMAL"
  658. for service in $services; do
  659. if [ "${entries[$service]}" ]; then
  660. ## Prevent double inclusion of same service if this
  661. ## service is deps of two or more of your
  662. ## requirements.
  663. continue
  664. fi
  665. ## mark the service as "loaded" as well as it's containers
  666. ## if this is a subordinate service
  667. start_service=$SECONDS
  668. entries[$service]=$(_get_docker_compose_service_mixin "$service") || return 1
  669. debug " Applied $DARKYELLOW$service$NORMAL charm metadata mixins $GRAY(in $((SECONDS - start_service))s)$NORMAL"
  670. done
  671. debug " ..finished all mixins for $DARKYELLOW$target_service$NORMAL $GRAY(in $((SECONDS - start))s)$NORMAL"
  672. done
  673. merge_yaml_str "${entries[@]}" > "$cache_file"
  674. export _current_docker_compose="$(cat "$cache_file")"
  675. echo "$_current_docker_compose"
  676. debug " ..compilation of base 'docker-compose.yml' done $GRAY(in $((SECONDS - start_compilation))s)$NORMAL" || true
  677. # debug " ** ${WHITE}docker-compose.yml${NORMAL}:"
  678. # debug "$_current_docker_compose"
  679. }
  680. export -f get_docker_compose
  681. _get_compose_service_def_cached () {
  682. local service="$1" docker_compose="$2" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  683. if [ -e "$cache_file" ]; then
  684. #debug "$FUNCNAME: STATIC cache hit"
  685. cat "$cache_file"
  686. touch "$cache_file"
  687. return 0
  688. fi
  689. service_def_base="charm: $service"
  690. value=$(echo "$docker_compose" | shyaml get-value "$service" 2>/dev/null)
  691. merge_yaml <(echo "$service_def_base") <(echo "$value") | tee "$cache_file"
  692. }
  693. export -f _get_compose_service_def_cached
  694. ## XXXvlab: a lot to be done to cache the results
  695. get_compose_service_def () {
  696. local service="$1" docker_compose cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  697. result
  698. if [ -e "$cache_file" ]; then
  699. #debug "$FUNCNAME: SESSION cache hit"
  700. cat "$cache_file"
  701. return 0
  702. fi
  703. [ -z "$service" ] && print_syntax_error "Missing service as first argument."
  704. docker_compose=$(cat "$COMPOSE_YML_FILE") || return 1
  705. result=$(_get_compose_service_def_cached "$service" "$docker_compose") || return 1
  706. echo "$result" | tee "$cache_file"
  707. }
  708. export -f get_compose_service_def
  709. _get_service_charm_cached () {
  710. local service="$1" service_def="$2" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  711. if [ -e "$cache_file" ]; then
  712. # debug "$FUNCNAME: cache hit $1"
  713. cat "$cache_file"
  714. touch "$cache_file"
  715. return 0
  716. fi
  717. charm=$(echo "$service_def" | shyaml get-value charm 2>/dev/null)
  718. if [ -z "$charm" ]; then
  719. err "Missing charm in service $DARKYELLOW$service$NORMAL definition."
  720. return 1
  721. fi
  722. echo "$charm" | tee "$cache_file"
  723. }
  724. export -f _get_service_charm_cached
  725. get_service_charm () {
  726. local service="$1"
  727. if [ -z "$service" ]; then
  728. print_syntax_error "$FUNCNAME: Please specify a service as first argument."
  729. return 1
  730. fi
  731. service_def=$(get_compose_service_def "$service") || return 1
  732. _get_service_charm_cached "$service" "$service_def"
  733. }
  734. export -f get_service_charm
  735. ## built above the docker-compose abstraction, so it relies on the
  736. ## full docker-compose.yml to be already built.
  737. get_service_def () {
  738. local service="$1" def
  739. if [ -z "$_current_docker_compose" ]; then
  740. print_syntax_error "$FUNCNAME is meant to be called after"\
  741. "\$_current_docker_compose has been calculated."
  742. fi
  743. def=$(echo "$_current_docker_compose" | shyaml get-value "$service" 2>/dev/null)
  744. if [ -z "$def" ]; then
  745. err "No definition for service $DARKYELLOW$service$NORMAL in compiled 'docker-compose.yml'."
  746. return 1
  747. fi
  748. echo "$def"
  749. }
  750. export -f get_service_def
  751. ## Return the base docker image name of a service
  752. service_base_docker_image() {
  753. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  754. master_charm charm service_image service_build service_dockerfile
  755. if [ -e "$cache_file" ]; then
  756. # debug "$FUNCNAME: cache hit ($*)"
  757. cat "$cache_file"
  758. return 0
  759. fi
  760. master_charm="$(get_top_master_charm_for_service "$service")" || {
  761. err "Could not compute base charm for service $DARKYELLOW$service$NORMAL."
  762. return 1
  763. }
  764. service_def="$(get_service_def "$master_charm")" || {
  765. err "Could not get docker-compose service definition for $DARKYELLOW$master_charm$NORMAL."
  766. return 1
  767. }
  768. service_image=$(echo "$service_def" | shyaml get-value image 2>/dev/null)
  769. if [ "$?" != 0 ]; then
  770. service_build=$(echo "$service_def" | shyaml get-value build 2>/dev/null)
  771. if [ "$?" != 0 ]; then
  772. err "Service $DARKYELLOW$service$NORMAL has no ${WHITE}image${NORMAL} nor ${WHITE}build${NORMAL} parameter."
  773. echo "$service_def" >&2
  774. return 1
  775. fi
  776. service_dockerfile="$(charm.get_dir "${service_build%%/*}")"/"${service_build#*/}"/Dockerfile
  777. if ! [ -e "$service_dockerfile" ]; then
  778. err "No Dockerfile found in '$service_dockerfile' location."
  779. return 1
  780. fi
  781. grep '^FROM' "$service_dockerfile" | xargs echo | cut -f 2 -d " "
  782. else
  783. echo "$service_image"
  784. fi | tee "$cache_file"
  785. }
  786. export -f service_base_docker_image
  787. get_charm_relation_def () {
  788. local charm="$1" relation_name="$2" cache_file="$state_tmpdir/$FUNCNAME.cache.$1.$2" \
  789. relation_def metadata
  790. if [ -e "$cache_file" ]; then
  791. # debug "$FUNCNAME: cache hit ($*)"
  792. cat "$cache_file"
  793. return 0
  794. fi
  795. metadata="$(charm.metadata "$charm")" || return 1
  796. relation_def="$(echo "$metadata" | shyaml get-value "provides.${relation_name}" 2>/dev/null)"
  797. echo "$relation_def" | tee "$cache_file"
  798. }
  799. export -f get_charm_relation_def
  800. get_charm_tech_dep_orientation_for_relation() {
  801. local charm="$1" relation_name="$2" cache_file="$state_tmpdir/$FUNCNAME.cache.$1.$2" \
  802. relation_def metadata value
  803. if [ -e "$cache_file" ]; then
  804. # debug "$FUNCNAME: cache hit ($*)"
  805. cat "$cache_file"
  806. return 0
  807. fi
  808. relation_def=$(get_charm_relation_def "$charm" "$relation_name" 2>/dev/null)
  809. value=$(echo "$relation_def" | shyaml get-value 'tech-dep' 2>/dev/null)
  810. value=${value:-True}
  811. echo "$value" | tee "$cache_file"
  812. }
  813. export -f get_charm_tech_dep_orientation_for_relation
  814. ##
  815. ## Use compose file to get deps, and relation definition in metadata.yml
  816. ## for tech-dep attribute.
  817. get_service_deps() {
  818. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1"
  819. if [ -e "$cache_file" ]; then
  820. # debug "$FUNCNAME: cache hit ($*)"
  821. cat "$cache_file"
  822. return 0
  823. fi
  824. (
  825. set -o pipefail
  826. get_compose_relations "$service" | \
  827. while read-0 relation_name target_service _relation_config tech_dep; do
  828. echo "$target_service"
  829. done | tee "$cache_file"
  830. ) || return 1
  831. }
  832. export -f get_service_deps
  833. _rec_get_depth() {
  834. local elt=$1 dep deps max cache_file="$state_tmpdir/$FUNCNAME.cache.$1"
  835. [ "${depths[$elt]}" ] && return 0
  836. if [ -e "$cache_file" ]; then
  837. # debug "$FUNCNAME: cache hit ($*)"
  838. depths[$elt]=$(cat "$cache_file")
  839. return 0
  840. fi
  841. visited[$elt]=1
  842. #debug "Setting visited[$elt]"
  843. #debug "Asking for $DARKYELLOW$elt$NORMAL dependencies"
  844. deps=$(get_service_deps "$elt") || {
  845. debug "Failed get_service_deps $elt"
  846. return 1
  847. }
  848. # debug "$elt deps are:" $deps
  849. max=0
  850. for dep in $deps; do
  851. [ "${visited[$dep]}" ] && {
  852. #debug "Already computing $dep"
  853. continue
  854. }
  855. _rec_get_depth "$dep" || return 1
  856. #debug "Requesting depth[$dep]"
  857. if (( ${depths[$dep]} > max )); then
  858. max="${depths[$dep]}"
  859. fi
  860. done
  861. # debug "Setting depth[$elt] to $((max + 1))"
  862. depths[$elt]=$((max + 1))
  863. echo "${depths[$elt]}" > $cache_file
  864. }
  865. export -f _rec_get_depth
  866. get_ordered_service_dependencies() {
  867. local services=("$@") cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  868. if [ -e "$cache_file" ]; then
  869. # debug "$FUNCNAME: cache hit ($*)"
  870. cat "$cache_file"
  871. return 0
  872. fi
  873. #debug "Figuring ordered deps of $DARKYELLOW$services$NORMAL"
  874. if [ -z "${services[*]}" ]; then
  875. print_syntax_error "$FUNCNAME: no arguments"
  876. return 1
  877. fi
  878. declare -A depths
  879. declare -A visited
  880. heads=("${services[@]}")
  881. while [ "${#heads[@]}" != 0 ]; do
  882. array_pop heads head
  883. _rec_get_depth "$head" || return 1
  884. done
  885. i=0
  886. while [ "${#depths[@]}" != 0 ]; do
  887. for key in "${!depths[@]}"; do
  888. value="${depths[$key]}"
  889. if [ "$value" == "$i" ]; then
  890. echo "$key"
  891. unset depths[$key]
  892. fi
  893. done
  894. i=$((i + 1))
  895. done | tee "$cache_file"
  896. }
  897. export -f get_ordered_service_dependencies
  898. run_service_hook () {
  899. local services="$1" action="$2" loaded
  900. declare -A loaded
  901. for service in $services; do
  902. for subservice in $(get_ordered_service_dependencies "$service"); do
  903. if [ "${loaded[$subservice]}" ]; then
  904. ## Prevent double inclusion of same service if this
  905. ## service is deps of two or more of your
  906. ## requirements.
  907. continue
  908. fi
  909. charm=$(get_service_charm "$subservice") || return 1
  910. charm.has_hook "$charm" "$action" >/dev/null || continue
  911. PROJECT_NAME=$(get_default_project_name) || return 1
  912. Wrap -d "$YELLOW$action$NORMAL hook of charm $DARKYELLOW$charm$NORMAL" <<EOF || return 1
  913. export SERVICE_NAME=$subservice
  914. export IMAGE_NAME=$(echo "${PROJECT_NAME}" | tr -d "_-")_\${SERVICE_NAME}
  915. export CONTAINER_NAME=\${IMAGE_NAME}_1
  916. export CHARM_NAME="$charm"
  917. export PROJECT_NAME=$PROJECT_NAME
  918. export DOCKER_BASE_IMAGE=$(service_base_docker_image "$charm")
  919. export SERVICE_DATASTORE="$DATASTORE/$charm"
  920. export SERVICE_CONFIGSTORE="$CONFIGSTORE/$charm"
  921. charm.run_hook "$charm" "$action"
  922. EOF
  923. loaded[$subservice]=1
  924. done
  925. done
  926. return 0
  927. }
  928. host_resource_get() {
  929. local location="$1" cfg="$2"
  930. type=$(echo "$cfg" | shyaml get-value type 2>/dev/null) || {
  931. err "Missing ${WHITE}type$NORMAL option in ${WHITE}get$NORMAL config for location '$location'"
  932. return 1
  933. }
  934. if fn.exists host_resource_get_$type; then
  935. host_resource_get_$type "$location" "$cfg"
  936. else
  937. err "Source ${WHITE}source$NORMAL type '$type' unknown for" \
  938. "${WHITE}host-resource$NORMAL '$location' defined in" \
  939. "$DARKYELLOW$subservice$NORMAL config."
  940. return 1
  941. fi
  942. }
  943. export -f host_resource_get
  944. host_resource_get_git() {
  945. local location="$1" cfg="$2" branch parent url
  946. branch=$(echo "$cfg" | shyaml get-value branch 2>/dev/null)
  947. branch=${branch:-master}
  948. url=$(echo "$cfg" | shyaml get-value url 2>/dev/null)
  949. parent="$(dirname "$location")"
  950. (
  951. mkdir -p "$parent" && cd "$parent" &&
  952. git clone -b "$branch" "$url" "$(basename "$location")"
  953. ) || return 1
  954. }
  955. export -f host_resource_get_git
  956. host_resource_get_git-sub() {
  957. local location="$1" cfg="$2" branch parent url
  958. branch=$(echo "$cfg" | shyaml get-value branch 2>/dev/null)
  959. branch=${branch:-master}
  960. url=$(echo "$cfg" | shyaml get-value url 2>/dev/null)
  961. parent="$(dirname "$location")"
  962. (
  963. mkdir -p "$parent" && cd "$parent" &&
  964. git sub clone -b "$branch" "$url" "$(basename "$location")"
  965. ) || return 1
  966. }
  967. export -f host_resource_get_git-sub
  968. setup_host_resources () {
  969. local services="$1" action="$2" loaded location cfg
  970. declare -A loaded
  971. for service in $services; do
  972. for subservice in $(get_ordered_service_dependencies "$service"); do
  973. if [ "${loaded[$subservice]}" ]; then
  974. ## Prevent double inclusion of same service if this
  975. ## service is deps of two or more of your
  976. ## requirements.
  977. continue
  978. fi
  979. service_def=$(get_compose_service_def "$subservice") || return 1
  980. while read-0 location cfg; do
  981. ## XXXvlab: will it be a git resources always ?
  982. if [ -d "$location" -a ! -d "$location/.git" ]; then
  983. err "Hum, location '$location' does not seem to be a git directory."
  984. return 1
  985. fi
  986. if [ -d "$location" ]; then
  987. info "host resource '$location' already set up."
  988. continue
  989. fi
  990. get=$(echo "$cfg" | shyaml get-value get 2>/dev/null)
  991. if [ -z "$get" ]; then
  992. err "No host directory '$location' found, and no ${WHITE}source$NORMAL" \
  993. "specified for $DARKYELLOW$subservice$NORMAL."
  994. return 1
  995. fi
  996. host_resource_get "$location" "$get" || return 1
  997. done < <(echo "$service_def" | shyaml key-values-0 host-resources 2>/dev/null)
  998. loaded[$subservice]=1
  999. done
  1000. done
  1001. return 0
  1002. }
  1003. relation-get () {
  1004. local key="$1"
  1005. cat "$RELATION_DATA_FILE" | shyaml get-value "$key" 2>/dev/null
  1006. if [ "$?" != 0 ]; then
  1007. err "The key $WHITE$key$NORMAL was not found in relation's data."
  1008. return 1
  1009. fi
  1010. }
  1011. export -f relation-get
  1012. relation-base-compose-get () {
  1013. local key="$1"
  1014. echo "$RELATION_BASE_COMPOSE_DEF" | shyaml get-value "options.$key" 2>/dev/null
  1015. if [ "$?" != 0 ]; then
  1016. err "The key $WHITE$key$NORMAL was not found in base service compose definition.."
  1017. return 1
  1018. fi
  1019. }
  1020. export -f relation-base-compose-get
  1021. relation-target-compose-get () {
  1022. local key="$1"
  1023. echo "$RELATION_BASE_COMPOSE_DEF" | shyaml get-value "options.$key" 2>/dev/null
  1024. if [ "$?" != 0 ]; then
  1025. err "The key $WHITE$key$NORMAL was not found in base service compose definition.."
  1026. return 1
  1027. fi
  1028. }
  1029. export -f relation-target-compose-get
  1030. relation-set () {
  1031. local key="$1" value="$2"
  1032. if [ -z "$RELATION_DATA_FILE" ]; then
  1033. err "$FUNCNAME: relation does not seems to be correctly setup."
  1034. return 1
  1035. fi
  1036. if ! [ -r "$RELATION_DATA_FILE" ]; then
  1037. err "$FUNCNAME: can't read relation's data." >&2
  1038. return 1
  1039. fi
  1040. _config_merge "$RELATION_DATA_FILE" <(echo "$key: $value")
  1041. }
  1042. export -f relation-set
  1043. _config_merge() {
  1044. local config_filename="$1" merge_to_file="$2"
  1045. touch "$config_filename" &&
  1046. merge_yaml "$config_filename" "$merge_to_file" > "$config_filename.tmp" || return 1
  1047. mv "$config_filename.tmp" "$config_filename"
  1048. }
  1049. export -f _config_merge
  1050. ## XXXvlab; this can be used only in relation, I'd like to use it in init.
  1051. config-add() {
  1052. local metadata="$1"
  1053. _config_merge "$RELATION_CONFIG" <(echo "$metadata")
  1054. }
  1055. export -f config-add
  1056. ## XXXvlab; this can be used only in relation, I'd like to use it in init.
  1057. init-config-add() {
  1058. local metadata="$1"
  1059. _config_merge "$state_tmpdir/to-merge-in-docker-compose.yml" <(echo "$metadata")
  1060. }
  1061. export -f init-config-add
  1062. logstdout() {
  1063. local name="$1"
  1064. sed -r 's%^%'"${name}"'> %g'
  1065. }
  1066. export -f logstdout
  1067. logstderr() {
  1068. local name="$1"
  1069. sed -r 's%^(.*)$%'"${RED}${name}>${NORMAL} \1"'%g'
  1070. }
  1071. export -f logstderr
  1072. _run_service_relation () {
  1073. local relation_name="$1" service="$2" target_service="$3" relation_config="$4" relation_dir services
  1074. charm=$(get_service_charm "$service") || return 1
  1075. target_charm=$(get_service_charm "$target_service") || return 1
  1076. base_script_name=$(charm.has_relation_hook "$charm" "$relation_name" relation-joined) || true
  1077. target_script_name=$(charm.has_relation_hook "$target_charm" "$relation_name" relation-joined) || true
  1078. [ "$base_script_name" -o "$target_script_name" ] || return 0
  1079. relation_dir=$(get_relation_data_dir "$charm" "$target_charm" "$relation_name") || return 1
  1080. RELATION_DATA_FILE=$(get_relation_data_file "$charm" "$target_charm" "$relation_name" "$relation_config") || return 1
  1081. export BASE_CHARM_NAME=$charm
  1082. export TARGET_CHARM_NAME=$target_charm
  1083. export RELATION_DATA_FILE
  1084. target_errlvl=0
  1085. if [ -z "$target_script_name" ]; then
  1086. verb "No relation script $DARKBLUE$relation_name$NORMAL in target $DARKYELLOW$target_charm$NORMAL."
  1087. else
  1088. verb "Running ${DARKBLUE}$relation_name${NORMAL} relation-joined script" \
  1089. "for target charm $DARKYELLOW$target_charm$NORMAL"
  1090. RELATION_CONFIG="$relation_dir/config_provider"
  1091. DOCKER_BASE_IMAGE=$(service_base_docker_image "$target_service") || return 1
  1092. export DOCKER_BASE_IMAGE RELATION_CONFIG RELATION_DATA
  1093. {
  1094. (
  1095. SERVICE_NAME=$target_service
  1096. SERVICE_DATASTORE="$DATASTORE/$target_charm"
  1097. SERVICE_CONFIGSTORE="$CONFIGSTORE/$target_charm"
  1098. export SERVICE_NAME DOCKER_BASE_IMAGE SERVICE_DATASTORE SERVICE_CONFIGSTORE
  1099. charm.run_relation_hook "$target_charm" "$relation_name" relation-joined
  1100. echo "$?" > "$relation_dir/target_errlvl"
  1101. ) | logstdout "$DARKYELLOW$target_charm$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${GREEN}@${NORMAL}"
  1102. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$target_charm$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${RED}@${NORMAL}" 3>&1 1>&2 2>&3
  1103. target_errlvl="$(cat "$relation_dir/target_errlvl")" || {
  1104. err "Relation script '$script_name' in $DARKYELLOW$target_charm$NORMAL" \
  1105. "failed before outputing an errorlevel."
  1106. ((target_errlvl |= "1" ))
  1107. }
  1108. if [ -e "$RELATION_CONFIG" ]; then
  1109. debug "Merging some new config info in $DARKYELLOW$target_service$NORMAL"
  1110. _config_merge "$state_tmpdir/to-merge-in-docker-compose.yml" "$RELATION_CONFIG" &&
  1111. rm "$RELATION_CONFIG"
  1112. ((target_errlvl |= "$?"))
  1113. fi
  1114. fi
  1115. if [ "$target_errlvl" == 0 ]; then
  1116. errlvl=0
  1117. if [ "$base_script_name" ]; then
  1118. verb "Running ${DARKBLUE}$relation_name${NORMAL} relation-joined script" \
  1119. "for charm $DARKYELLOW$charm$NORMAL"
  1120. RELATION_CONFIG="$relation_dir/config_providee"
  1121. RELATION_DATA="$(cat "$RELATION_DATA_FILE")"
  1122. DOCKER_BASE_IMAGE=$(service_base_docker_image "$service") || return 1
  1123. export DOCKER_BASE_IMAGE RELATION_CONFIG RELATION_DATA
  1124. {
  1125. (
  1126. SERVICE_NAME=$service
  1127. SERVICE_DATASTORE="$DATASTORE/$charm"
  1128. SERVICE_CONFIGSTORE="$CONFIGSTORE/$charm"
  1129. export SERVICE_NAME DOCKER_BASE_IMAGE SERVICE_DATASTORE SERVICE_CONFIGSTORE
  1130. charm.run_relation_hook "$charm" "$relation_name" relation-joined
  1131. echo "$?" > "$relation_dir/errlvl"
  1132. ) | logstdout "$DARKYELLOW$charm$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${GREEN}@${NORMAL}"
  1133. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$charm$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${RED}@$NORMAL" 3>&1 1>&2 2>&3
  1134. errlvl="$(cat "$relation_dir/errlvl")" || {
  1135. err "Relation script '$script_name' in $DARKYELLOW$charm$NORMAL" \
  1136. "failed before outputing an errorlevel."
  1137. ((errlvl |= "1" ))
  1138. }
  1139. if [ -e "$RELATION_CONFIG" ]; then
  1140. _config_merge "$state_tmpdir/to-merge-in-docker-compose.yml" "$RELATION_CONFIG" &&
  1141. rm "$RELATION_CONFIG"
  1142. ((errlvl |= "$?" ))
  1143. fi
  1144. if [ "$errlvl" != 0 ]; then
  1145. err "Relation $DARKBLUE$relation_name$NORMAL on $DARKYELLOW$charm$NORMAL failed to run properly."
  1146. fi
  1147. else
  1148. verb "No relation script '$script_name' in charm $DARKYELLOW$charm$NORMAL. Ignoring."
  1149. fi
  1150. else
  1151. err "Relation $DARKBLUE$relation_name$NORMAL on $DARKYELLOW$target_charm$NORMAL failed to run properly."
  1152. fi
  1153. if [ "$target_errlvl" == 0 -a "$errlvl" == 0 ]; then
  1154. debug "Relation $DARKBLUE$relation_name$NORMAL is established" \
  1155. "between $DARKYELLOW$service$NORMAL and $DARKYELLOW$target_service$NORMAL."
  1156. return 0
  1157. else
  1158. return 1
  1159. fi
  1160. }
  1161. export -f _run_service_relation
  1162. _get_compose_relations_cached () {
  1163. local compose_service_def="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  1164. relation_name relation_def target_service
  1165. if [ -e "$cache_file" ]; then
  1166. #debug "$FUNCNAME: STATIC cache hit $1"
  1167. cat "$cache_file"
  1168. touch "$cache_file"
  1169. return 0
  1170. fi
  1171. (
  1172. set -o pipefail
  1173. if [ "$compose_service_def" ]; then
  1174. while read-0 relation_name relation_def; do
  1175. (
  1176. case "$(echo "$relation_def" | shyaml get-type 2>/dev/null)" in
  1177. "str")
  1178. target_service="$(echo "$relation_def" | shyaml get-value 2>/dev/null)"
  1179. tech_dep="$(get_charm_tech_dep_orientation_for_relation "$target_service" "$relation_name")"
  1180. echo -en "$relation_name\0$target_service\0\0$tech_dep\0"
  1181. ;;
  1182. "sequence")
  1183. while read-0 target_service; do
  1184. tech_dep="$(get_charm_tech_dep_orientation_for_relation "$target_service" "$relation_name")"
  1185. echo -en "$relation_name\0$target_service\0\0$tech_dep\0"
  1186. done < <(echo "$relation_def" | shyaml get-values-0 2>/dev/null)
  1187. ;;
  1188. "struct")
  1189. while read-0 target_service relation_config; do
  1190. tech_dep="$(get_charm_tech_dep_orientation_for_relation "$target_service" "$relation_name")"
  1191. echo -en "$relation_name\0$target_service\0$relation_config\0$tech_dep\0"
  1192. done < <(echo "$relation_def" | shyaml key-values-0 2>/dev/null)
  1193. ;;
  1194. esac
  1195. ) </dev/null >> "$cache_file"
  1196. done < <(echo "$compose_service_def" | shyaml key-values-0 relations 2>/dev/null)
  1197. fi
  1198. )
  1199. if [ "$?" != 0 ]; then
  1200. err "Error while looking for compose relations."
  1201. rm -f "$cache_file" ## no cache
  1202. return 1
  1203. fi
  1204. [ -e "$cache_file" ] && cat "$cache_file"
  1205. return 0
  1206. }
  1207. export -f _get_compose_relations_cached
  1208. get_compose_relations () {
  1209. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  1210. compose_def
  1211. if [ -e "$cache_file" ]; then
  1212. #debug "$FUNCNAME: SESSION cache hit $1"
  1213. cat "$cache_file"
  1214. return 0
  1215. fi
  1216. compose_def="$(get_compose_service_def "$service")" || return 1
  1217. _get_compose_relations_cached "$compose_def" > "$cache_file"
  1218. if [ "$?" != 0 ]; then
  1219. err "Error while looking for compose relations."
  1220. rm -f "$cache_file" ## no cache
  1221. return 1
  1222. fi
  1223. cat "$cache_file"
  1224. }
  1225. export -f get_compose_relations
  1226. run_service_relations () {
  1227. local services="$1" loaded
  1228. PROJECT_NAME=$(get_default_project_name) || return 1
  1229. export PROJECT_NAME
  1230. declare -A loaded
  1231. for service in $(get_ordered_service_dependencies $services); do
  1232. # debug "Upping dep's relations of ${DARKYELLOW}$service${NORMAL}:"
  1233. for subservice in $(get_service_deps "$service") "$service"; do
  1234. [ "${loaded[$subservice]}" ] && continue
  1235. export BASE_SERVICE_NAME=$service
  1236. MASTER_BASE_CHARM_NAME=$(get_top_master_charm_for_service "$subservice") || return 1
  1237. RELATION_BASE_COMPOSE_DEF=$(get_compose_service_def "$subservice") || return 1
  1238. export RELATION_BASE_COMPOSE_DEF MASTER_BASE_CHARM_NAME
  1239. # debug " Relations of ${DARKYELLOW}$subservice${NORMAL}:"
  1240. while read-0 relation_name target_service relation_config tech_dep; do
  1241. export relation_config
  1242. export TARGET_SERVICE_NAME=$target_service
  1243. MASTER_TARGET_CHARM_NAME=$(get_top_master_charm_for_service "$target_service") || return 1
  1244. RELATION_TARGET_COMPOSE_DEF=$(get_compose_service_def "$target_service") || return 1
  1245. export MASTER_TARGET_CHARM_NAME RELATION_TARGET_COMPOSE_DEF
  1246. Wrap -d "Building $DARKYELLOW$subservice$NORMAL --$DARKBLUE$relation_name$NORMAL--> $DARKYELLOW$target_service$NORMAL" <<EOF || return 1
  1247. _run_service_relation "$relation_name" "$subservice" "$target_service" "\$relation_config"
  1248. EOF
  1249. done < <(get_compose_relations "$subservice") || return 1
  1250. loaded[$subservice]=1
  1251. done
  1252. done
  1253. }
  1254. export -f run_service_relations
  1255. _run_service_action_direct() {
  1256. local service="$1" action="$2" charm _dummy
  1257. shift; shift
  1258. read-0 charm || true ## against 'set -e' that could be setup in parent scripts
  1259. if read-0 _dummy || [ "$_dummy" ]; then
  1260. print_syntax_error "$FUNCNAME: too many arguments in action descriptor"
  1261. return 1
  1262. fi
  1263. compose_file=$(get_compose_yml_location) || return 1
  1264. export action_errlvl_file="$state_tmpdir/action-$service-$charm-$action-errlvl"
  1265. export state_tmpdir
  1266. {
  1267. (
  1268. set +e ## Prevents unwanted leaks from parent shell
  1269. export COMPOSE_CONFIG=$(cat "$compose_file")
  1270. export METADATA_CONFIG=$(charm.metadata "$charm")
  1271. export SERVICE_NAME=$service
  1272. export ACTION_NAME=$action
  1273. export CONTAINER_NAME=$(get_top_master_charm_for_service "$service")
  1274. export DOCKER_BASE_IMAGE=$(service_base_docker_image "$CONTAINER_NAME")
  1275. export SERVICE_DATASTORE="$DATASTORE/$service"
  1276. export SERVICE_CONFIGSTORE="$CONFIGSTORE/$service"
  1277. exname="$exname $ACTION_NAME $SERVICE_NAME" \
  1278. stdbuf -oL -eL charm.run_direct_action "$charm" "$action" "$@"
  1279. echo "$?" > "$action_errlvl_file"
  1280. ) | logstdout "$DARKYELLOW$charm$NORMAL/${DARKCYAN}$action${NORMAL} ${GREEN}@${NORMAL}"
  1281. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$charm$NORMAL/${DARKCYAN}$action${NORMAL} ${RED}@$NORMAL" 3>&1 1>&2 2>&3
  1282. if ! [ -e "$action_errlvl_file" ]; then
  1283. err "Action $DARKYELLOW$service$NORMAL:$DARKCYAN$action$NORMAL has failed without having time" \
  1284. "to output an errlvl"
  1285. return 1
  1286. fi
  1287. return "$(cat "$action_errlvl_file")"
  1288. }
  1289. export -f _run_service_action_direct
  1290. _run_service_action_relation() {
  1291. local service="$1" action="$2" charm target_charm relation_name relation_config _dummy
  1292. shift; shift
  1293. read-0 charm target_charm relation_name relation_config || true
  1294. if read-0 _dummy || [ "$_dummy" ]; then
  1295. print_syntax_error "$FUNCNAME: too many arguments in action descriptor"
  1296. return 1
  1297. fi
  1298. export RELATION_DATA_FILE=$(get_relation_data_file "$charm" "$target_charm" "$relation_name" "$relation_config")
  1299. compose_file=$(get_compose_yml_location) || return 1
  1300. export action_errlvl_file="$state_tmpdir/action-$service-$charm-$action-errlvl"
  1301. export state_tmpdir
  1302. {
  1303. (
  1304. set +e ## Prevents unwanted leaks from parent shell
  1305. export METADATA_CONFIG=$(charm.metadata "$charm")
  1306. export SERVICE_NAME=$service
  1307. export RELATION_TARGET_CHARM="$target_charm"
  1308. export RELATION_BASE_CHARM="$charm"
  1309. export ACTION_NAME=$action
  1310. export CONTAINER_NAME=$(get_top_master_charm_for_service "$service")
  1311. export DOCKER_BASE_IMAGE=$(service_base_docker_image "$CONTAINER_NAME")
  1312. export SERVICE_DATASTORE="$DATASTORE/$service"
  1313. export SERVICE_CONFIGSTORE="$CONFIGSTORE/$service"
  1314. exname="$exname $ACTION_NAME $SERVICE_NAME" \
  1315. stdbuf -oL -eL charm.run_relation_action "$charm" "$relation_name" "$action" "$@"
  1316. echo "$?" > "$action_errlvl_file"
  1317. ) | logstdout "$DARKYELLOW$charm$NORMAL/${DARKCYAN}$action${NORMAL} ${GREEN}@${NORMAL}"
  1318. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$charm$NORMAL/${DARKCYAN}$action${NORMAL} ${RED}@$NORMAL" 3>&1 1>&2 2>&3
  1319. if ! [ -e "$action_errlvl_file" ]; then
  1320. err "Action $DARKYELLOW$service$NORMAL:$DARKCYAN$action$NORMAL has failed without having time" \
  1321. "to output an errlvl"
  1322. return 1
  1323. fi
  1324. return "$(cat "$action_errlvl_file")"
  1325. }
  1326. export -f _run_service_action_relation
  1327. get_relation_data_dir() {
  1328. local charm="$1" target_charm="$2" relation_name="$3" cache_file="$state_tmpdir/$FUNCNAME.cache.$1"
  1329. if [ -e "$cache_file" ]; then
  1330. # debug "$FUNCNAME: cache hit ($*)"
  1331. cat "$cache_file"
  1332. return 0
  1333. fi
  1334. project=$(get_default_project_name) || return 1
  1335. relation_dir="$VARDIR/relations/$project/$charm-$target_charm/$relation_name"
  1336. if ! [ -d "$relation_dir" ]; then
  1337. mkdir -p "$relation_dir" || return 1
  1338. chmod go-rwx "$relation_dir" || return 1 ## protecting this directory
  1339. fi
  1340. echo "$relation_dir" || tee "$cache_file"
  1341. }
  1342. export -f get_relation_data_dir
  1343. get_relation_data_file() {
  1344. local charm="$1" target_charm="$2" relation_name="$3" relation_config="$4"
  1345. relation_dir=$(get_relation_data_dir "$charm" "$target_charm" "$relation_name") || return 1
  1346. relation_data_file="$relation_dir/data"
  1347. new=
  1348. if [ -e "$relation_data_file" ]; then
  1349. ## Has reference changed ?
  1350. new_md5=$(echo "$relation_config" | md5_compat)
  1351. if [ "$new_md5" != "$(cat "$relation_data_file.md5_ref" 2>/dev/null)" ]; then
  1352. new=true
  1353. fi
  1354. else
  1355. new=true
  1356. fi
  1357. if [ "$new" ]; then
  1358. echo "$relation_config" > "$relation_data_file"
  1359. chmod go-rwx "$relation_data_file" ## protecting this file
  1360. echo "$relation_config" | md5_compat > "$relation_data_file.md5_ref"
  1361. fi
  1362. echo "$relation_data_file"
  1363. }
  1364. export -f get_relation_data_file
  1365. has_service_action () {
  1366. local service="$1" action="$2" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  1367. charm target_charm
  1368. if [ -e "$cache_file" ]; then
  1369. # debug "$FUNCNAME: cache hit ($*)"
  1370. cat "$cache_file"
  1371. return 0
  1372. fi
  1373. charm=$(get_service_charm "$service") || return 1
  1374. ## Action directly provided ?
  1375. if charm.has_direct_action "$charm" "$action"; then
  1376. echo -en "direct\0$charm" | tee "$cache_file"
  1377. return 0
  1378. fi
  1379. ## Action provided by relation ?
  1380. while read-0 relation_name target_service relation_config tech_dep; do
  1381. target_charm=$(get_service_charm "$target_service") || return 1
  1382. if charm.has_relation_action "$target_charm" "$relation_name" "$action"; then
  1383. echo -en "relation\0$charm\0$target_charm\0$relation_name\0$relation_config" | tee "$cache_file"
  1384. return 0
  1385. fi
  1386. done < <(get_compose_relations "$service")
  1387. return 1
  1388. # master=$(get_top_master_charm_for_service "$charm")
  1389. # [ "$master" == "$charm" ] && return 1
  1390. # has_service_action "$master" "$action"
  1391. }
  1392. export -f has_service_action
  1393. run_service_action () {
  1394. local service="$1" action="$2"
  1395. shift ; shift
  1396. {
  1397. if ! read-0 action_type; then
  1398. info "Service $DARKYELLOW$service$NORMAL does not have any action $DARKCYAN$action$NORMAL defined."
  1399. info " Add an executable script to 'actions/$action' to implement action."
  1400. return 1
  1401. fi
  1402. Section "running $DARKYELLOW$service$NORMAL/$DARKCYAN$action$NORMAL ($action_type)"; Feed
  1403. "_run_service_action_${action_type}" "$service" "$action" "$@"
  1404. } < <(has_service_action "$service" "$action")
  1405. }
  1406. export -f run_service_action
  1407. get_compose_relation_config() {
  1408. local service=$1 relation_config cache_file="$state_tmpdir/$FUNCNAME.cache.$1"
  1409. if [ -e "$cache_file" ]; then
  1410. # debug "$FUNCNAME: cache hit ($*)"
  1411. cat "$cache_file"
  1412. return 0
  1413. fi
  1414. compose_service_def=$(get_compose_service_def "$service") || return 1
  1415. echo "$compose_service_def" | shyaml get-value "relations" 2>/dev/null | tee "$cache_file"
  1416. }
  1417. export -f get_compose_relation_config
  1418. # ## Return key-values-0
  1419. # get_compose_relation_config_for_service() {
  1420. # local service=$1 relation_name=$2 relation_config
  1421. # compose_service_relations=$(get_compose_relation_config "$service") || return 1
  1422. # if ! relation_config=$(
  1423. # echo "$compose_service_relations" |
  1424. # shyaml get-value "${relation_name}" 2>/dev/null); then
  1425. # err "Couldn't find $DARKYELLOW${service}$NORMAL/${WHITE}${relation_name}$NORMAL" \
  1426. # "relation config in compose configuration."
  1427. # return 1
  1428. # fi
  1429. # if [ -z "$relation_config" ]; then
  1430. # err "Relation ${WHITE}mysql-database$NORMAL is empty in compose configuration."
  1431. # return 1
  1432. # fi
  1433. # if ! echo "$relation_config" | shyaml key-values-0 2>/dev/null; then
  1434. # err "No key/values in ${DARKBLUE}mysql-database$NORMAL of compose config."
  1435. # return 1
  1436. # fi
  1437. # }
  1438. # export -f get_compose_relation_config_for_service
  1439. _get_master_charm_for_service_cached () {
  1440. local service="$1" charm="$2" metadata="$3" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  1441. charm requires master_charm target_charm target_service service_def
  1442. if [ -e "$cache_file" ]; then
  1443. # debug "$FUNCNAME: STATIC cache hit ($1)"
  1444. cat "$cache_file"
  1445. touch "$cache_file"
  1446. return 0
  1447. fi
  1448. if [ "$(echo "$metadata" | shyaml get-value "subordinate" 2>/dev/null)" != "True" ]; then
  1449. ## just return charm name
  1450. echo "$charm" | tee "$cache_file"
  1451. return 0
  1452. fi
  1453. ## fetch the container relation
  1454. requires="$(echo "$metadata" | shyaml get-value "requires" 2>/dev/null)"
  1455. if [ -z "$requires" ]; then
  1456. die "Charm $DARKYELLOW$charm$NORMAL is a subordinate but does not have any 'requires' " \
  1457. "section."
  1458. fi
  1459. master_charm=
  1460. while read-0 relation_name relation; do
  1461. if [ "$(echo "$relation" | shyaml get-value "scope" 2>/dev/null)" == "container" ]; then
  1462. # debug "$DARKYELLOW$service$NORMAL's relation" \
  1463. # "$DARKBLUE${relation_name}$NORMAL is a container."
  1464. interface="$(echo "$relation" | shyaml get-value "interface" 2>/dev/null)"
  1465. if [ -z "$interface" ]; then
  1466. err "No ${WHITE}$interface${NORMAL} set for relation $relation_name."
  1467. return 1
  1468. fi
  1469. ## Action provided by relation ?
  1470. target_service=
  1471. while read-0 relation_name candidate_target_service _relation_config _tech_dep; do
  1472. [ "$interface" == "$relation_name" ] && {
  1473. target_service="$candidate_target_service"
  1474. break
  1475. }
  1476. done < <(get_compose_relations "$service")
  1477. if [ -z "$target_service" ]; then
  1478. err "Couldn't find ${WHITE}relations.$interface${NORMAL} in" \
  1479. "${DARKYELLOW}$service$NORMAL compose definition."
  1480. return 1
  1481. fi
  1482. master_charm=$(get_service_charm "$target_service") || return 1
  1483. break
  1484. fi
  1485. done < <(echo "$requires" | shyaml key-values-0 2>/dev/null)
  1486. if [ -z "$master_charm" ]; then
  1487. die "Charm $DARKYELLOW$charm$NORMAL is a subordinate but does not have any relation with" \
  1488. " ${WHITE}scope${NORMAL} set to 'container'."
  1489. fi
  1490. echo "$master_charm" | tee "$cache_file"
  1491. }
  1492. export -f _get_master_charm_for_service_cached
  1493. get_master_charm_for_service() {
  1494. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  1495. charm metadata result
  1496. if [ -e "$cache_file" ]; then
  1497. # debug "$FUNCNAME: SESSION cache hit ($*)"
  1498. cat "$cache_file"
  1499. return 0
  1500. fi
  1501. charm=$(get_service_charm "$service") || return 1
  1502. metadata=$(charm.metadata "$charm") || {
  1503. err "Failed to access metadata of charm $DARKYELLOW$charm$NORMAL"
  1504. return 1
  1505. }
  1506. result=$(_get_master_charm_for_service_cached "$service" "$charm" "$metadata") || return 1
  1507. echo "$result" | tee "$cache_file"
  1508. }
  1509. export -f get_master_charm_for_service
  1510. get_top_master_charm_for_service() {
  1511. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  1512. current_service
  1513. if [ -e "$cache_file" ]; then
  1514. # debug "$FUNCNAME: cache hit ($*)"
  1515. cat "$cache_file"
  1516. return 0
  1517. fi
  1518. current_service="$service"
  1519. while true; do
  1520. master_service=$(get_master_charm_for_service "$current_service") || return 1
  1521. [ "$master_service" == "$current_service" ] && break
  1522. current_service="$master_service"
  1523. done
  1524. echo "$current_service" | tee "$cache_file"
  1525. return 0
  1526. }
  1527. export -f get_top_master_charm_for_service
  1528. ##
  1529. ## The result is a mixin that is not always a complete valid
  1530. ## docker-compose entry (thinking of subordinates). The result
  1531. ## will be merge with master charms.
  1532. _get_docker_compose_mixin_from_metadata_cached() {
  1533. local charm="$1" metadata="$2" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  1534. metadata_file metadata volumes docker_compose subordinate image
  1535. if [ -e "$cache_file" ]; then
  1536. #debug "$FUNCNAME: STATIC cache hit $1"
  1537. cat "$cache_file"
  1538. touch "$cache_file"
  1539. return 0
  1540. fi
  1541. mixin=
  1542. if [ "$metadata" ]; then
  1543. ## resources to volumes
  1544. volumes=$(
  1545. for resource_type in data config; do
  1546. while read-0 resource; do
  1547. eval "echo \" - \$${resource_type^^}STORE/\$charm\$resource:\$resource:rw\""
  1548. done < <(echo "$metadata" | shyaml get-values-0 "${resource_type}-resources" 2>/dev/null)
  1549. done
  1550. while read-0 resource; do
  1551. if [[ "$resource" == *:* ]]; then
  1552. echo " - $resource:rw"
  1553. else
  1554. echo " - $resource:$resource:rw"
  1555. fi
  1556. done < <(echo "$metadata" | shyaml get-values-0 "host-resources" 2>/dev/null)
  1557. while read-0 resource; do
  1558. dest="$(charm.get_dir "$charm")/resources$resource"
  1559. if ! [ -e "$dest" ]; then
  1560. die "charm-resource: '$resource' does not exist (file: '$dest')."
  1561. fi
  1562. echo " - $dest:$resource:rw"
  1563. done < <(echo "$metadata" | shyaml get-values-0 "charm-resources" 2>/dev/null)
  1564. ) || return 1
  1565. if [ "$volumes" ]; then
  1566. mixin=$(echo -en "volumes:\n$volumes")
  1567. fi
  1568. docker_compose=$(echo "$metadata" | shyaml get-value "docker-compose" 2>/dev/null)
  1569. if [ "$docker_compose" ]; then
  1570. mixin=$(merge_yaml_str "$mixin" "$docker_compose")
  1571. fi
  1572. if [ "$(echo "$metadata" | shyaml get-value "subordinate" 2>/dev/null)" == "True" ]; then
  1573. subordinate=true
  1574. fi
  1575. fi
  1576. image=$(echo "$metadata" | shyaml get-value "docker-image" 2>/dev/null)
  1577. image_or_build_statement=
  1578. if [ "$image" ]; then
  1579. if [ "$subordinate" ]; then
  1580. err "Subordinate charm can not have a ${WHITE}docker-image${NORMAL} value."
  1581. return 1
  1582. fi
  1583. image_or_build_statement="image: $image"
  1584. elif [ -d "$(charm.get_dir "$charm")/build" ]; then
  1585. if [ "$subordinate" ]; then
  1586. err "Subordinate charm can not have a 'build' sub directory."
  1587. return 1
  1588. fi
  1589. image_or_build_statement="build: $charm/build"
  1590. fi
  1591. if [ "$image_or_build_statement" ]; then
  1592. mixin=$(merge_yaml_str "$mixin" "$image_or_build_statement")
  1593. fi
  1594. echo "$mixin" | tee "$cache_file"
  1595. }
  1596. export -f _get_docker_compose_mixin_from_metadata_cached
  1597. get_docker_compose_mixin_from_metadata() {
  1598. local charm="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1"
  1599. if [ -e "$cache_file" ]; then
  1600. #debug "$FUNCNAME: SESSION cache hit ($*)"
  1601. cat "$cache_file"
  1602. return 0
  1603. fi
  1604. metadata="$(charm.metadata "$charm")" || return 1
  1605. mixin=$(_get_docker_compose_mixin_from_metadata_cached "$charm" "$metadata") || return 1
  1606. echo "$mixin" | tee "$cache_file"
  1607. }
  1608. export -f get_docker_compose_mixin_from_metadata
  1609. _save() {
  1610. local name="$1"
  1611. cat - | tee -a "$docker_compose_dir/.data/$name"
  1612. }
  1613. export -f _save
  1614. get_default_project_name() {
  1615. if [ "$DEFAULT_PROJECT_NAME" ]; then
  1616. echo "$DEFAULT_PROJECT_NAME"
  1617. return 0
  1618. fi
  1619. compose_yml_location="$(get_compose_yml_location)" || return 1
  1620. if [ "$compose_yml_location" ]; then
  1621. if normalized_path=$(readlink -e "$compose_yml_location"); then
  1622. echo "$(basename "$(dirname "$normalized_path")")"
  1623. return 0
  1624. fi
  1625. fi
  1626. echo "project"
  1627. return 0
  1628. }
  1629. export -f get_default_project_name
  1630. launch_docker_compose() {
  1631. local charm
  1632. docker_compose_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
  1633. #debug "Creating temporary docker-compose directory in '$docker_compose_tmpdir'."
  1634. # trap_add EXIT "debug \"Removing temporary docker-compose directory in $docker_compose_tmpdir.\";\
  1635. # rm -rf \"$docker_compose_tmpdir\""
  1636. trap_add EXIT "rm -rf \"$docker_compose_tmpdir\""
  1637. project=$(get_default_project_name)
  1638. mkdir -p "$docker_compose_tmpdir/$project"
  1639. docker_compose_dir="$docker_compose_tmpdir/$project"
  1640. if [ -z "$SERVICE_PACK" ]; then
  1641. export SERVICE_PACK=$(get_default_target_services $SERVICE_PACK)
  1642. fi
  1643. get_docker_compose $SERVICE_PACK > "$docker_compose_dir/docker-compose.yml" || return 1
  1644. if [ -e "$state_tmpdir/to-merge-in-docker-compose.yml" ]; then
  1645. # debug "Merging some config data in docker-compose.yml:"
  1646. # debug "$(cat $state_tmpdir/to-merge-in-docker-compose.yml)"
  1647. _config_merge "$docker_compose_dir/docker-compose.yml" "$state_tmpdir/to-merge-in-docker-compose.yml" || return 1
  1648. fi
  1649. if [ -z "$(echo $(cat "$docker_compose_dir/docker-compose.yml"))" ]; then
  1650. die "Generated 'docker-compose.yml' is unexpectedly empty."
  1651. fi
  1652. ## XXXvlab: could be more specific and only link the needed charms
  1653. for charm in $(shyaml keys < "$docker_compose_dir/docker-compose.yml"); do
  1654. if charm.exists "$charm"; then
  1655. ln -sf "$(charm.get_dir "$charm")" "$docker_compose_dir/$charm" || exit 1
  1656. fi
  1657. done
  1658. mkdir "$docker_compose_dir/.data"
  1659. {
  1660. {
  1661. cd "$docker_compose_dir"
  1662. debug "${WHITE}docker-compose.yml$NORMAL for $DARKYELLOW$SERVICE_PACK$NORMAL:"
  1663. debug "$(cat "$docker_compose_dir/docker-compose.yml" | prefix " $GRAY|$NORMAL ")"
  1664. debug "${WHITE}Launching$NORMAL: docker-compose $@"
  1665. docker-compose "$@"
  1666. echo "$?" > "$docker_compose_dir/.data/errlvl"
  1667. } | _save stdout
  1668. } 3>&1 1>&2 2>&3 | _save stderr 3>&1 1>&2 2>&3
  1669. 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
  1670. err "Detected bug https://github.com/docker/docker/issues/4036 ... "
  1671. err "Please re-launch your command, or switch from 'devicemapper' driver to 'overlayfs' or 'aufs'."
  1672. fi
  1673. docker_compose_errlvl="$(cat "$docker_compose_dir/.data/errlvl" 2>/dev/null)"
  1674. if [ -z "$docker_compose_errlvl" ]; then
  1675. err "Something went wrong before you could gather docker-compose errorlevel."
  1676. return 1
  1677. fi
  1678. return "$docker_compose_errlvl"
  1679. }
  1680. export -f launch_docker_compose
  1681. get_compose_yml_location() {
  1682. parent=$(while ! [ -e "./compose.yml" ]; do
  1683. [ "$PWD" == "/" ] && exit 0
  1684. cd ..
  1685. done; echo "$PWD"
  1686. )
  1687. if [ "$parent" ]; then
  1688. echo "$parent/compose.yml"
  1689. return 0
  1690. fi
  1691. if [ "$DEFAULT_COMPOSE_FILE" ]; then
  1692. if ! [ -e "$DEFAULT_COMPOSE_FILE" ]; then
  1693. err "No 'compose.yml' was found in current or parent dirs," \
  1694. "and \$DEFAULT_COMPOSE_FILE points to an unexistent file."
  1695. die "Please provide a 'compose.yml' file."
  1696. fi
  1697. echo "$DEFAULT_COMPOSE_FILE"
  1698. return 0
  1699. fi
  1700. err "No 'compose.yml' was found in current or parent dirs, and no \$DEFAULT_COMPOSE_FILE was set."
  1701. die "Please provide a 'compose.yml' file."
  1702. return 1
  1703. }
  1704. export -f get_compose_yml_location
  1705. get_default_target_services() {
  1706. local services=("$@")
  1707. if [ -z "${services[*]}" ]; then
  1708. if [ "$DEFAULT_SERVICES" ]; then
  1709. debug "No service provided, using $WHITE\$DEFAULT_SERVICES$NORMAL variable." \
  1710. "Target services: $DARKYELLOW$DEFAULT_SERVICES$NORMAL"
  1711. services="$DEFAULT_SERVICES"
  1712. else
  1713. err "No service provided."
  1714. return 1
  1715. fi
  1716. fi
  1717. echo "${services[*]}"
  1718. }
  1719. export -f get_default_target_services
  1720. get_master_services() {
  1721. local loaded master_service
  1722. declare -A loaded
  1723. for service in "$@"; do
  1724. master_service=$(get_top_master_charm_for_service "$service") || return 1
  1725. if [ "${loaded[$master_service]}" ]; then
  1726. continue
  1727. fi
  1728. echo "$master_service"
  1729. loaded["$master_service"]=1
  1730. done | xargs echo
  1731. }
  1732. export -f get_master_services
  1733. _setup_state_dir() {
  1734. export state_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
  1735. #debug "Creating temporary state directory in '$state_tmpdir'."
  1736. # trap_add EXIT "debug \"Removing temporary state directory in $state_tmpdir.\";\
  1737. # rm -rf \"$state_tmpdir\""
  1738. trap_add EXIT "rm -rf \"$state_tmpdir\""
  1739. }
  1740. get_docker_compose_opts_list() {
  1741. local action="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$action"; cat "$(which docker-compose)" | md5_compat)"
  1742. if [ -e "$cache_file" ]; then
  1743. cat "$cache_file"
  1744. return 0
  1745. fi
  1746. docker_compose_help_msg=$(docker-compose "$action" --help 2>/dev/null) || return 1
  1747. echo "$docker_compose_help_msg" | grep '^Options:' -A 20000 |
  1748. tail -n +2 |
  1749. egrep "^\s+-" |
  1750. sed -r 's/\s+((((-[a-zA-Z]|--[a-zA-Z0-9-]+)( [A-Z=]+|=[^ ]+)?)(, )?)+)\s+.*$/\1/g' |
  1751. tee "$cache_file"
  1752. }
  1753. _MULTIOPTION_REGEX='^((-[a-zA-Z]|--[a-zA-Z0-9-]+)(, )?)+'
  1754. _MULTIOPTION_REGEX_LINE_FILTER=$_MULTIOPTION_REGEX'(\s|=)'
  1755. get_docker_compose_multi_opts_list() {
  1756. local action="$1" opts_list
  1757. opts_list=$(get_docker_compose_opts_list "$action") || return 1
  1758. echo "$opts_list" | egrep "$_MULTIOPTION_REGEX_LINE_FILTER" |
  1759. sed -r "s/^($_MULTIOPTION_REGEX)(\s|=).*$/\1/g" |
  1760. tr ',' "\n" | xargs echo
  1761. }
  1762. get_docker_compose_single_opts_list() {
  1763. local action="$1" opts_list
  1764. opts_list=$(get_docker_compose_opts_list "$action") || return 1
  1765. echo "$opts_list" | egrep -v "$_MULTIOPTION_REGEX_LINE_FILTER" |
  1766. tr ',' "\n" | xargs echo
  1767. }
  1768. clean_cache() {
  1769. local i=0
  1770. for f in $(ls -t "$CACHEDIR/"*.cache.* 2>/dev/null | tail -n +500); do
  1771. ((i++))
  1772. rm -f "$f"
  1773. done
  1774. if (( i > 0 )); then
  1775. debug "${WHITE}Cleaned cache:${NORMAL} Removed $((i)) elements (current cache size is $(du -sh "$CACHEDIR" | cut -f 1))"
  1776. fi
  1777. }
  1778. [ "$SOURCED" ] && return 0
  1779. if [ -z "$DISABLE_SYSTEM_CONFIG_FILE" ]; then
  1780. if [ -r /etc/default/charm ]; then
  1781. . /etc/default/charm
  1782. fi
  1783. if [ -r "/etc/default/$exname" ]; then
  1784. . "/etc/default/$exname"
  1785. fi
  1786. ## XXXvlab: should provide YML config opportunities in possible parent dirs ?
  1787. ## userdir ? and global /etc/compose.yml ?
  1788. for cfgfile in /etc/compose.conf /etc/compose.local.conf \
  1789. /etc/default/compose /etc/compose/local.conf; do
  1790. [ -e "$cfgfile" ] || continue
  1791. . "$cfgfile" || die "Loading config file '$cfgfile' failed."
  1792. done
  1793. fi
  1794. _setup_state_dir
  1795. mkdir -p "$CACHEDIR" || exit 1
  1796. DOCKER_HOST_NET=$(ip addr show docker0 | grep 'inet ' | xargs echo | cut -f 2 -d " ") || {
  1797. die "Couldn't determine docker host net"
  1798. }
  1799. DOCKER_HOST_IP=$(echo "$DOCKER_HOST_NET" | cut -f 1 -d "/") || {
  1800. die "Couldn't determine docker host ip"
  1801. }
  1802. export DOCKER_HOST_NET DOCKER_HOST_IP
  1803. ##
  1804. ## Argument parsing
  1805. ##
  1806. remainder_args=()
  1807. compose_opts=()
  1808. action_opts=()
  1809. no_hooks=
  1810. no_init=
  1811. action=
  1812. stage="main" ## switches from 'main', to 'action', 'remainder'
  1813. is_docker_compose_action=
  1814. DC_MATCH_MULTI=
  1815. DC_MATCH_SINGLE=
  1816. while [ "$#" != 0 ]; do
  1817. case "$stage" in
  1818. "main")
  1819. case "$1" in
  1820. --help|-h)
  1821. no_init=true ; no_hooks=true ; no_relations=true
  1822. print_help
  1823. exit 0
  1824. ;;
  1825. --verbose|-v)
  1826. export VERBOSE=true
  1827. ;;
  1828. --version|-V)
  1829. print_version
  1830. exit 0
  1831. ;;
  1832. -f)
  1833. [ -e "$2" ] || die "File $2 doesn't exists"
  1834. export DEFAULT_COMPOSE_FILE="$2"
  1835. shift
  1836. ;;
  1837. -p)
  1838. export DEFAULT_PROJECT_NAME="$2"
  1839. shift
  1840. ;;
  1841. --no-relations)
  1842. export no_relations=true
  1843. ;;
  1844. --no-hooks)
  1845. export no_hooks=true
  1846. ;;
  1847. --no-init)
  1848. export no_init=true
  1849. ;;
  1850. --debug)
  1851. export DEBUG=true
  1852. export VERBOSE=true
  1853. ;;
  1854. --*|-*)
  1855. compose_opts+=("$1")
  1856. ;;
  1857. *)
  1858. action="$1"
  1859. stage="action"
  1860. DC_MATCH_MULTI=$(get_docker_compose_multi_opts_list "$action") &&
  1861. DC_MATCH_SINGLE="$(get_docker_compose_single_opts_list "$action")"
  1862. if [ "$DC_MATCH_MULTI" ]; then
  1863. DC_MATCH_SINGLE="$DC_MATCH_SINGLE $(echo "$DC_MATCH_MULTI" | sed -r 's/( |$)/=\* /g')"
  1864. fi
  1865. is_docker_compose_action=true
  1866. ;;
  1867. esac
  1868. ;;
  1869. "action")
  1870. case "$1" in
  1871. --help|-h)
  1872. no_init=true ; no_hooks=true ; no_relations=true
  1873. action_opts+=("$1")
  1874. ;;
  1875. --*|-*)
  1876. if [ "$is_docker_compose_action" ]; then
  1877. if str_matches "$1" $DC_MATCH_MULTI; then
  1878. action_opts=("$1" "$2")
  1879. shift;
  1880. elif str_matches "$1" $DC_MATCH_SINGLE; then
  1881. action_opts=("$1")
  1882. else
  1883. err "Unknown option '$1'. Please check help."
  1884. docker-compose "$action" --help >&2
  1885. exit 1
  1886. fi
  1887. fi
  1888. ;;
  1889. *)
  1890. action_posargs+=("$1")
  1891. stage="remainder"
  1892. ;;
  1893. esac
  1894. ;;
  1895. "remainder")
  1896. remainder_args+=("$@")
  1897. break 3;;
  1898. esac
  1899. shift
  1900. done
  1901. [ "${compose_opts[*]}" ] && debug "Main docker-compose opts: ${compose_opts[*]}"
  1902. [ "${action_opts[*]}" ] && debug "Action $DARKCYAN$action$NORMAL with opts: ${action_opts[*]}"
  1903. [ "${remainder_args[*]}" ] && debug "Remainder args: ${remainder_args[*]}"
  1904. ##
  1905. ## Actual code
  1906. ##
  1907. export DOCKER_DATASTORE=${DOCKER_DATASTORE:-/srv/docker-datastore}
  1908. COMPOSE_YML_FILE=$(get_compose_yml_location) || exit 1
  1909. export COMPOSE_YML_FILE
  1910. debug "Found $WHITE$exname$NORMAL YAML file in '$COMPOSE_YML_FILE'."
  1911. output=$(shyaml get-value < "$COMPOSE_YML_FILE" 2>&1)
  1912. if [ "$?" != 0 ]; then
  1913. err "Invalid YAML in '$COMPOSE_YML_FILE':"
  1914. while IFS='' read -r line1 && IFS='' read -r line2; do
  1915. echo "$line1 $GRAY($line2)$NORMAL"
  1916. done < <(echo "$output" | grep ^yaml.scanner -A 100 |
  1917. sed -r 's/^ in "<stdin>", //g' | sed -r 's/^yaml.scanner.[a-zA-Z]+: //g') |
  1918. prefix " $GRAY|$NORMAL "
  1919. exit 1
  1920. fi
  1921. charm.sanity_checks || die "Sanity checks about charm-store failed. Please correct."
  1922. ##
  1923. ## Get services in command line.
  1924. ##
  1925. is_service_action=
  1926. case "$action" in
  1927. up|build|start|stop|config)
  1928. services="$(get_default_target_services "${action_posargs[@]}")" || exit 1
  1929. orig_services="${action_posargs[@]:1}"
  1930. ;;
  1931. run)
  1932. services="${action_posargs[0]}"
  1933. ;;
  1934. "")
  1935. services=
  1936. ;;
  1937. *)
  1938. if is_service_action=$(has_service_action "${action_posargs[0]}" "$action"); then
  1939. {
  1940. read-0 action_type
  1941. case "$action_type" in
  1942. "relation")
  1943. read-0 _ target_service relation_name
  1944. debug "Found action $DARKYELLOW${action_posargs[0]}$NORMAL/$DARKBLUE$relation_name$NORMAL/$DARKCYAN$action$NORMAL (in $DARKYELLOW$target_service$NORMAL)"
  1945. ;;
  1946. "direct")
  1947. debug "Found action $DARKYELLOW${action_posargs[0]}$NORMAL.$DARKCYAN$action$NORMAL"
  1948. ;;
  1949. esac
  1950. } < <(has_service_action "${action_posargs[0]}" "$action")
  1951. services="${action_posargs[0]}"
  1952. else
  1953. services="$(get_default_target_services "${action_posargs[@]}")"
  1954. fi
  1955. ;;
  1956. esac
  1957. get_docker_compose $services >/dev/null || { ## precalculate variable \$_current_docker_compose
  1958. err "Fails to compile base 'docker-compose.yml'"
  1959. exit 1
  1960. }
  1961. ##
  1962. ## Pre-action
  1963. ##
  1964. full_init=
  1965. case "$action" in
  1966. up|run)
  1967. full_init=true
  1968. ;;
  1969. "")
  1970. full_init=
  1971. ;;
  1972. *)
  1973. if [ "$is_service_action" ]; then
  1974. full_init=true
  1975. fi
  1976. ;;
  1977. esac
  1978. if [ "$full_init" ]; then
  1979. ## init in order
  1980. if [ -z "$no_init" ]; then
  1981. Section setup host resources
  1982. setup_host_resources "$services" || exit 1
  1983. Section initialisation
  1984. run_service_hook "$services" init || exit 1
  1985. fi
  1986. ## Get relations
  1987. if [ -z "$no_relations" ]; then
  1988. run_service_relations "$services" || exit 1
  1989. fi
  1990. ## XXXvlab: to be removed when all relation and service stuff is resolved
  1991. if [ -z "$no_hooks" ]; then
  1992. ordered_services=$(get_ordered_service_dependencies $services) || exit 1
  1993. for service in $ordered_services; do
  1994. charm=$(get_service_charm "$service") || exit 1
  1995. for script in "$CHARM_STORE/$charm/hooks.d/"*.sh; do
  1996. [ -e "$script" ] || continue
  1997. [ -x "$script" ] || { echo "compose: script $script is not executable." >&2; exit 1; }
  1998. (
  1999. debug "Launching '$script'."
  2000. cd "$(dirname "$script)")";
  2001. "$script" "$@"
  2002. ) || { echo "compose: hook $script failed. Stopping." >&2; exit 1; }
  2003. done
  2004. done
  2005. fi
  2006. fi
  2007. export SERVICE_PACK="$services"
  2008. ##
  2009. ## Docker-compose
  2010. ##
  2011. case "$action" in
  2012. up|start|stop|build)
  2013. master_services=$(get_master_services $SERVICE_PACK) || exit 1
  2014. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" $master_services
  2015. ;;
  2016. run)
  2017. master_service=$(get_master_services $SERVICE_PACK) || exit 1
  2018. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "$master_service" "${remainder_args[@]}"
  2019. ;;
  2020. # enter)
  2021. # master_service=$(get_master_services $SERVICE_PACK) || exit 1
  2022. # [ "${remainder_args[*]}" ] || remainder_args=("/bin/bash" "-c" "export TERM=xterm; exec bash")
  2023. # docker exec -ti "${action_opts[@]}" "$master_service" "${remainder_args[@]}"
  2024. # ;;
  2025. config)
  2026. ## removing the services
  2027. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "${remainder_args[@]}"
  2028. warn "Runtime configuration modification (from relations) are not included here."
  2029. ;;
  2030. *)
  2031. if [ "$is_service_action" ]; then
  2032. run_service_action "$SERVICE_PACK" "$action" "${remainder_args[@]}"
  2033. else
  2034. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "${remainder_args[@]}"
  2035. fi
  2036. ;;
  2037. esac