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.

2392 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
  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") || {
  619. err "Failed to get top master charm for service $DARKYELLOW$service$NORMAL"
  620. return 1
  621. }
  622. ## The compose part
  623. links_yaml=$(get_docker_compose_links "$service") || return 1
  624. docker_compose_options=$(_get_docker_compose_opts "$service") || return 1
  625. ## the charm part
  626. #debug "Get charm name from service name $DARKYELLOW$service$NORMAL."
  627. charm=$(get_service_charm "$service") || return 1
  628. charm_part=$(get_docker_compose_mixin_from_metadata "$charm") || return 1
  629. ## Merge results
  630. if [ "$charm_part" ]; then
  631. charm_yaml="$(yaml_key_val_str "$master_charm" "$charm_part")" || return 1
  632. merge_yaml_str "$links_yaml" "$charm_yaml" "$docker_compose_options" || return 1
  633. else
  634. echo "$links_yaml"
  635. fi | tee "$cache_file"
  636. }
  637. export -f _get_docker_compose_service_mixin
  638. ##
  639. ## Get full `docker-compose.yml` format for all listed services (and
  640. ## their deps)
  641. ##
  642. ## @export
  643. ## @cache: !system !nofail +stdout
  644. get_docker_compose () {
  645. local cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)" entries services service start
  646. if [ -e "$cache_file" ]; then
  647. # debug "$FUNCNAME: cache hit ($*)"
  648. cat "$cache_file"
  649. return 0
  650. fi
  651. ##
  652. ## Adding sub services configurations
  653. ##
  654. declare -A entries
  655. start_compilation=$SECONDS
  656. debug "Compiling 'docker-compose.yml' base for $DARKYELLOW$@$NORMAL..."
  657. for target_service in "$@"; do
  658. start=$SECONDS
  659. services=$(get_ordered_service_dependencies "$target_service") || {
  660. err "Failed to get dependencies for $DARKYELLOW$target_service$NORMAL"
  661. return 1
  662. }
  663. debug " $DARKYELLOW$target_service$NORMAL deps:$DARKYELLOW" $services "$NORMAL$GRAY(in $((SECONDS - start))s)$NORMAL"
  664. for service in $services; do
  665. if [ "${entries[$service]}" ]; then
  666. ## Prevent double inclusion of same service if this
  667. ## service is deps of two or more of your
  668. ## requirements.
  669. continue
  670. fi
  671. ## mark the service as "loaded" as well as it's containers
  672. ## if this is a subordinate service
  673. start_service=$SECONDS
  674. entries[$service]=$(_get_docker_compose_service_mixin "$service") || {
  675. err "Failed to get service mixin for $DARKYELLOW$service$NORMAL"
  676. return 1
  677. }
  678. debug " Applied $DARKYELLOW$service$NORMAL charm metadata mixins $GRAY(in $((SECONDS - start_service))s)$NORMAL"
  679. done
  680. debug " ..finished all mixins for $DARKYELLOW$target_service$NORMAL $GRAY(in $((SECONDS - start))s)$NORMAL"
  681. done
  682. merge_yaml_str "${entries[@]}" > "$cache_file"
  683. export _current_docker_compose="$(cat "$cache_file")"
  684. echo "$_current_docker_compose"
  685. debug " ..compilation of base 'docker-compose.yml' done $GRAY(in $((SECONDS - start_compilation))s)$NORMAL" || true
  686. # debug " ** ${WHITE}docker-compose.yml${NORMAL}:"
  687. # debug "$_current_docker_compose"
  688. }
  689. export -f get_docker_compose
  690. _get_compose_service_def_cached () {
  691. local service="$1" docker_compose="$2" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  692. if [ -e "$cache_file" ]; then
  693. #debug "$FUNCNAME: STATIC cache hit"
  694. cat "$cache_file"
  695. touch "$cache_file"
  696. return 0
  697. fi
  698. service_def_base="charm: $service"
  699. value=$(echo "$docker_compose" | shyaml get-value "$service" 2>/dev/null)
  700. merge_yaml <(echo "$service_def_base") <(echo "$value") | tee "$cache_file"
  701. }
  702. export -f _get_compose_service_def_cached
  703. ## XXXvlab: a lot to be done to cache the results
  704. get_compose_service_def () {
  705. local service="$1" docker_compose cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  706. result
  707. if [ -e "$cache_file" ]; then
  708. #debug "$FUNCNAME: SESSION cache hit"
  709. cat "$cache_file"
  710. return 0
  711. fi
  712. [ -z "$service" ] && print_syntax_error "Missing service as first argument."
  713. docker_compose=$(cat "$COMPOSE_YML_FILE") || return 1
  714. result=$(_get_compose_service_def_cached "$service" "$docker_compose") || return 1
  715. echo "$result" | tee "$cache_file"
  716. }
  717. export -f get_compose_service_def
  718. _get_service_charm_cached () {
  719. local service="$1" service_def="$2" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  720. if [ -e "$cache_file" ]; then
  721. # debug "$FUNCNAME: cache hit $1"
  722. cat "$cache_file"
  723. touch "$cache_file"
  724. return 0
  725. fi
  726. charm=$(echo "$service_def" | shyaml get-value charm 2>/dev/null)
  727. if [ -z "$charm" ]; then
  728. err "Missing charm in service $DARKYELLOW$service$NORMAL definition."
  729. return 1
  730. fi
  731. echo "$charm" | tee "$cache_file"
  732. }
  733. export -f _get_service_charm_cached
  734. get_service_charm () {
  735. local service="$1"
  736. if [ -z "$service" ]; then
  737. print_syntax_error "$FUNCNAME: Please specify a service as first argument."
  738. return 1
  739. fi
  740. service_def=$(get_compose_service_def "$service") || return 1
  741. _get_service_charm_cached "$service" "$service_def"
  742. }
  743. export -f get_service_charm
  744. ## built above the docker-compose abstraction, so it relies on the
  745. ## full docker-compose.yml to be already built.
  746. get_service_def () {
  747. local service="$1" def
  748. if [ -z "$_current_docker_compose" ]; then
  749. print_syntax_error "$FUNCNAME is meant to be called after"\
  750. "\$_current_docker_compose has been calculated."
  751. fi
  752. def=$(echo "$_current_docker_compose" | shyaml get-value "$service" 2>/dev/null)
  753. if [ -z "$def" ]; then
  754. err "No definition for service $DARKYELLOW$service$NORMAL in compiled 'docker-compose.yml'."
  755. return 1
  756. fi
  757. echo "$def"
  758. }
  759. export -f get_service_def
  760. ## Return the base docker image name of a service
  761. service_base_docker_image() {
  762. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  763. master_charm charm service_image service_build service_dockerfile
  764. if [ -e "$cache_file" ]; then
  765. # debug "$FUNCNAME: cache hit ($*)"
  766. cat "$cache_file"
  767. return 0
  768. fi
  769. master_charm="$(get_top_master_charm_for_service "$service")" || {
  770. err "Could not compute base charm for service $DARKYELLOW$service$NORMAL."
  771. return 1
  772. }
  773. service_def="$(get_service_def "$master_charm")" || {
  774. err "Could not get docker-compose service definition for $DARKYELLOW$master_charm$NORMAL."
  775. return 1
  776. }
  777. service_image=$(echo "$service_def" | shyaml get-value image 2>/dev/null)
  778. if [ "$?" != 0 ]; then
  779. service_build=$(echo "$service_def" | shyaml get-value build 2>/dev/null)
  780. if [ "$?" != 0 ]; then
  781. err "Service $DARKYELLOW$service$NORMAL has no ${WHITE}image${NORMAL} nor ${WHITE}build${NORMAL} parameter."
  782. echo "$service_def" >&2
  783. return 1
  784. fi
  785. service_dockerfile="$(charm.get_dir "${service_build%%/*}")"/"${service_build#*/}"/Dockerfile
  786. if ! [ -e "$service_dockerfile" ]; then
  787. err "No Dockerfile found in '$service_dockerfile' location."
  788. return 1
  789. fi
  790. grep '^FROM' "$service_dockerfile" | xargs echo | cut -f 2 -d " "
  791. else
  792. echo "$service_image"
  793. fi | tee "$cache_file"
  794. }
  795. export -f service_base_docker_image
  796. get_charm_relation_def () {
  797. local charm="$1" relation_name="$2" cache_file="$state_tmpdir/$FUNCNAME.cache.$1.$2" \
  798. relation_def metadata
  799. if [ -e "$cache_file" ]; then
  800. # debug "$FUNCNAME: cache hit ($*)"
  801. cat "$cache_file"
  802. return 0
  803. fi
  804. metadata="$(charm.metadata "$charm")" || return 1
  805. relation_def="$(echo "$metadata" | shyaml get-value "provides.${relation_name}" 2>/dev/null)"
  806. echo "$relation_def" | tee "$cache_file"
  807. }
  808. export -f get_charm_relation_def
  809. get_charm_tech_dep_orientation_for_relation() {
  810. local charm="$1" relation_name="$2" cache_file="$state_tmpdir/$FUNCNAME.cache.$1.$2" \
  811. relation_def metadata value
  812. if [ -e "$cache_file" ]; then
  813. # debug "$FUNCNAME: cache hit ($*)"
  814. cat "$cache_file"
  815. return 0
  816. fi
  817. relation_def=$(get_charm_relation_def "$charm" "$relation_name" 2>/dev/null)
  818. value=$(echo "$relation_def" | shyaml get-value 'tech-dep' 2>/dev/null)
  819. value=${value:-True}
  820. echo "$value" | tee "$cache_file"
  821. }
  822. export -f get_charm_tech_dep_orientation_for_relation
  823. ##
  824. ## Use compose file to get deps, and relation definition in metadata.yml
  825. ## for tech-dep attribute.
  826. get_service_deps() {
  827. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1"
  828. if [ -e "$cache_file" ]; then
  829. # debug "$FUNCNAME: cache hit ($*)"
  830. cat "$cache_file"
  831. return 0
  832. fi
  833. (
  834. set -o pipefail
  835. get_compose_relations "$service" | \
  836. while read-0 relation_name target_service _relation_config tech_dep; do
  837. echo "$target_service"
  838. done | tee "$cache_file"
  839. ) || return 1
  840. }
  841. export -f get_service_deps
  842. _rec_get_depth() {
  843. local elt=$1 dep deps max cache_file="$state_tmpdir/$FUNCNAME.cache.$1"
  844. [ "${depths[$elt]}" ] && return 0
  845. if [ -e "$cache_file" ]; then
  846. # debug "$FUNCNAME: cache hit ($*)"
  847. depths[$elt]=$(cat "$cache_file")
  848. return 0
  849. fi
  850. visited[$elt]=1
  851. #debug "Setting visited[$elt]"
  852. #debug "Asking for $DARKYELLOW$elt$NORMAL dependencies"
  853. deps=$(get_service_deps "$elt") || {
  854. debug "Failed get_service_deps $elt"
  855. return 1
  856. }
  857. # debug "$elt deps are:" $deps
  858. max=0
  859. for dep in $deps; do
  860. [ "${visited[$dep]}" ] && {
  861. #debug "Already computing $dep"
  862. continue
  863. }
  864. _rec_get_depth "$dep" || return 1
  865. #debug "Requesting depth[$dep]"
  866. if (( ${depths[$dep]} > max )); then
  867. max="${depths[$dep]}"
  868. fi
  869. done
  870. # debug "Setting depth[$elt] to $((max + 1))"
  871. depths[$elt]=$((max + 1))
  872. echo "${depths[$elt]}" > $cache_file
  873. }
  874. export -f _rec_get_depth
  875. get_ordered_service_dependencies() {
  876. local services=("$@") cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  877. if [ -e "$cache_file" ]; then
  878. # debug "$FUNCNAME: cache hit ($*)"
  879. cat "$cache_file"
  880. return 0
  881. fi
  882. #debug "Figuring ordered deps of $DARKYELLOW$services$NORMAL"
  883. if [ -z "${services[*]}" ]; then
  884. print_syntax_error "$FUNCNAME: no arguments"
  885. return 1
  886. fi
  887. declare -A depths
  888. declare -A visited
  889. heads=("${services[@]}")
  890. while [ "${#heads[@]}" != 0 ]; do
  891. array_pop heads head
  892. _rec_get_depth "$head" || return 1
  893. done
  894. i=0
  895. while [ "${#depths[@]}" != 0 ]; do
  896. for key in "${!depths[@]}"; do
  897. value="${depths[$key]}"
  898. if [ "$value" == "$i" ]; then
  899. echo "$key"
  900. unset depths[$key]
  901. fi
  902. done
  903. i=$((i + 1))
  904. done | tee "$cache_file"
  905. }
  906. export -f get_ordered_service_dependencies
  907. run_service_hook () {
  908. local services="$1" action="$2" loaded
  909. declare -A loaded
  910. for service in $services; do
  911. for subservice in $(get_ordered_service_dependencies "$service"); do
  912. if [ "${loaded[$subservice]}" ]; then
  913. ## Prevent double inclusion of same service if this
  914. ## service is deps of two or more of your
  915. ## requirements.
  916. continue
  917. fi
  918. charm=$(get_service_charm "$subservice") || return 1
  919. charm.has_hook "$charm" "$action" >/dev/null || continue
  920. PROJECT_NAME=$(get_default_project_name) || return 1
  921. Wrap -d "$YELLOW$action$NORMAL hook of charm $DARKYELLOW$charm$NORMAL" <<EOF || return 1
  922. export SERVICE_NAME=$subservice
  923. export IMAGE_NAME=$(echo "${PROJECT_NAME}" | tr -d "_-")_\${SERVICE_NAME}
  924. export CONTAINER_NAME=\${IMAGE_NAME}_1
  925. export CHARM_NAME="$charm"
  926. export PROJECT_NAME=$PROJECT_NAME
  927. export DOCKER_BASE_IMAGE=$(service_base_docker_image "$charm")
  928. export SERVICE_DATASTORE="$DATASTORE/$charm"
  929. export SERVICE_CONFIGSTORE="$CONFIGSTORE/$charm"
  930. charm.run_hook "$charm" "$action"
  931. EOF
  932. loaded[$subservice]=1
  933. done
  934. done
  935. return 0
  936. }
  937. host_resource_get() {
  938. local location="$1" cfg="$2"
  939. type=$(echo "$cfg" | shyaml get-value type 2>/dev/null) || {
  940. err "Missing ${WHITE}type$NORMAL option in ${WHITE}get$NORMAL config for location '$location'"
  941. return 1
  942. }
  943. if fn.exists host_resource_get_$type; then
  944. host_resource_get_$type "$location" "$cfg"
  945. else
  946. err "Source ${WHITE}source$NORMAL type '$type' unknown for" \
  947. "${WHITE}host-resource$NORMAL '$location' defined in" \
  948. "$DARKYELLOW$subservice$NORMAL config."
  949. return 1
  950. fi
  951. }
  952. export -f host_resource_get
  953. host_resource_get_git() {
  954. local location="$1" cfg="$2" branch parent url
  955. branch=$(echo "$cfg" | shyaml get-value branch 2>/dev/null)
  956. branch=${branch:-master}
  957. url=$(echo "$cfg" | shyaml get-value url 2>/dev/null)
  958. parent="$(dirname "$location")"
  959. (
  960. mkdir -p "$parent" && cd "$parent" &&
  961. git clone -b "$branch" "$url" "$(basename "$location")"
  962. ) || return 1
  963. }
  964. export -f host_resource_get_git
  965. host_resource_get_git-sub() {
  966. local location="$1" cfg="$2" branch parent url
  967. branch=$(echo "$cfg" | shyaml get-value branch 2>/dev/null)
  968. branch=${branch:-master}
  969. url=$(echo "$cfg" | shyaml get-value url 2>/dev/null)
  970. parent="$(dirname "$location")"
  971. (
  972. mkdir -p "$parent" && cd "$parent" &&
  973. git sub clone -b "$branch" "$url" "$(basename "$location")"
  974. ) || return 1
  975. }
  976. export -f host_resource_get_git-sub
  977. setup_host_resources () {
  978. local services="$1" action="$2" loaded location cfg
  979. declare -A loaded
  980. for service in $services; do
  981. for subservice in $(get_ordered_service_dependencies "$service"); do
  982. if [ "${loaded[$subservice]}" ]; then
  983. ## Prevent double inclusion of same service if this
  984. ## service is deps of two or more of your
  985. ## requirements.
  986. continue
  987. fi
  988. service_def=$(get_compose_service_def "$subservice") || return 1
  989. while read-0 location cfg; do
  990. ## XXXvlab: will it be a git resources always ?
  991. if [ -d "$location" -a ! -d "$location/.git" ]; then
  992. err "Hum, location '$location' does not seem to be a git directory."
  993. return 1
  994. fi
  995. if [ -d "$location" ]; then
  996. info "host resource '$location' already set up."
  997. continue
  998. fi
  999. get=$(echo "$cfg" | shyaml get-value get 2>/dev/null)
  1000. if [ -z "$get" ]; then
  1001. err "No host directory '$location' found, and no ${WHITE}source$NORMAL" \
  1002. "specified for $DARKYELLOW$subservice$NORMAL."
  1003. return 1
  1004. fi
  1005. host_resource_get "$location" "$get" || return 1
  1006. done < <(echo "$service_def" | shyaml key-values-0 host-resources 2>/dev/null)
  1007. loaded[$subservice]=1
  1008. done
  1009. done
  1010. return 0
  1011. }
  1012. relation-get () {
  1013. local key="$1"
  1014. cat "$RELATION_DATA_FILE" | shyaml get-value "$key" 2>/dev/null
  1015. if [ "$?" != 0 ]; then
  1016. err "The key $WHITE$key$NORMAL was not found in relation's data."
  1017. return 1
  1018. fi
  1019. }
  1020. export -f relation-get
  1021. relation-base-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-base-compose-get
  1030. relation-target-compose-get () {
  1031. local key="$1"
  1032. echo "$RELATION_BASE_COMPOSE_DEF" | shyaml get-value "options.$key" 2>/dev/null
  1033. if [ "$?" != 0 ]; then
  1034. err "The key $WHITE$key$NORMAL was not found in base service compose definition.."
  1035. return 1
  1036. fi
  1037. }
  1038. export -f relation-target-compose-get
  1039. relation-set () {
  1040. local key="$1" value="$2"
  1041. if [ -z "$RELATION_DATA_FILE" ]; then
  1042. err "$FUNCNAME: relation does not seems to be correctly setup."
  1043. return 1
  1044. fi
  1045. if ! [ -r "$RELATION_DATA_FILE" ]; then
  1046. err "$FUNCNAME: can't read relation's data." >&2
  1047. return 1
  1048. fi
  1049. _config_merge "$RELATION_DATA_FILE" <(echo "$key: $value")
  1050. }
  1051. export -f relation-set
  1052. _config_merge() {
  1053. local config_filename="$1" merge_to_file="$2"
  1054. touch "$config_filename" &&
  1055. merge_yaml "$config_filename" "$merge_to_file" > "$config_filename.tmp" || return 1
  1056. mv "$config_filename.tmp" "$config_filename"
  1057. }
  1058. export -f _config_merge
  1059. ## XXXvlab; this can be used only in relation, I'd like to use it in init.
  1060. config-add() {
  1061. local metadata="$1"
  1062. _config_merge "$RELATION_CONFIG" <(echo "$metadata")
  1063. }
  1064. export -f config-add
  1065. ## XXXvlab; this can be used only in relation, I'd like to use it in init.
  1066. init-config-add() {
  1067. local metadata="$1"
  1068. _config_merge "$state_tmpdir/to-merge-in-docker-compose.yml" <(echo "$metadata")
  1069. }
  1070. export -f init-config-add
  1071. logstdout() {
  1072. local name="$1"
  1073. sed -r 's%^%'"${name}"'> %g'
  1074. }
  1075. export -f logstdout
  1076. logstderr() {
  1077. local name="$1"
  1078. sed -r 's%^(.*)$%'"${RED}${name}>${NORMAL} \1"'%g'
  1079. }
  1080. export -f logstderr
  1081. _run_service_relation () {
  1082. local relation_name="$1" service="$2" target_service="$3" relation_config="$4" relation_dir services
  1083. charm=$(get_service_charm "$service") || return 1
  1084. target_charm=$(get_service_charm "$target_service") || return 1
  1085. base_script_name=$(charm.has_relation_hook "$charm" "$relation_name" relation-joined) || true
  1086. target_script_name=$(charm.has_relation_hook "$target_charm" "$relation_name" relation-joined) || true
  1087. [ "$base_script_name" -o "$target_script_name" ] || return 0
  1088. relation_dir=$(get_relation_data_dir "$charm" "$target_charm" "$relation_name") || return 1
  1089. RELATION_DATA_FILE=$(get_relation_data_file "$charm" "$target_charm" "$relation_name" "$relation_config") || return 1
  1090. export BASE_CHARM_NAME=$charm
  1091. export TARGET_CHARM_NAME=$target_charm
  1092. export RELATION_DATA_FILE
  1093. target_errlvl=0
  1094. if [ -z "$target_script_name" ]; then
  1095. verb "No relation script $DARKBLUE$relation_name$NORMAL in target $DARKYELLOW$target_charm$NORMAL."
  1096. else
  1097. verb "Running ${DARKBLUE}$relation_name${NORMAL} relation-joined script" \
  1098. "for target charm $DARKYELLOW$target_charm$NORMAL"
  1099. RELATION_CONFIG="$relation_dir/config_provider"
  1100. DOCKER_BASE_IMAGE=$(service_base_docker_image "$target_service") || return 1
  1101. export DOCKER_BASE_IMAGE RELATION_CONFIG RELATION_DATA
  1102. {
  1103. (
  1104. SERVICE_NAME=$target_service
  1105. SERVICE_DATASTORE="$DATASTORE/$target_charm"
  1106. SERVICE_CONFIGSTORE="$CONFIGSTORE/$target_charm"
  1107. export SERVICE_NAME DOCKER_BASE_IMAGE SERVICE_DATASTORE SERVICE_CONFIGSTORE
  1108. charm.run_relation_hook "$target_charm" "$relation_name" relation-joined
  1109. echo "$?" > "$relation_dir/target_errlvl"
  1110. ) | logstdout "$DARKYELLOW$target_charm$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${GREEN}@${NORMAL}"
  1111. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$target_charm$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${RED}@${NORMAL}" 3>&1 1>&2 2>&3
  1112. target_errlvl="$(cat "$relation_dir/target_errlvl")" || {
  1113. err "Relation script '$script_name' in $DARKYELLOW$target_charm$NORMAL" \
  1114. "failed before outputing an errorlevel."
  1115. ((target_errlvl |= "1" ))
  1116. }
  1117. if [ -e "$RELATION_CONFIG" ]; then
  1118. debug "Merging some new config info in $DARKYELLOW$target_service$NORMAL"
  1119. _config_merge "$state_tmpdir/to-merge-in-docker-compose.yml" "$RELATION_CONFIG" &&
  1120. rm "$RELATION_CONFIG"
  1121. ((target_errlvl |= "$?"))
  1122. fi
  1123. fi
  1124. if [ "$target_errlvl" == 0 ]; then
  1125. errlvl=0
  1126. if [ "$base_script_name" ]; then
  1127. verb "Running ${DARKBLUE}$relation_name${NORMAL} relation-joined script" \
  1128. "for charm $DARKYELLOW$charm$NORMAL"
  1129. RELATION_CONFIG="$relation_dir/config_providee"
  1130. RELATION_DATA="$(cat "$RELATION_DATA_FILE")"
  1131. DOCKER_BASE_IMAGE=$(service_base_docker_image "$service") || return 1
  1132. export DOCKER_BASE_IMAGE RELATION_CONFIG RELATION_DATA
  1133. {
  1134. (
  1135. SERVICE_NAME=$service
  1136. SERVICE_DATASTORE="$DATASTORE/$charm"
  1137. SERVICE_CONFIGSTORE="$CONFIGSTORE/$charm"
  1138. export SERVICE_NAME DOCKER_BASE_IMAGE SERVICE_DATASTORE SERVICE_CONFIGSTORE
  1139. charm.run_relation_hook "$charm" "$relation_name" relation-joined
  1140. echo "$?" > "$relation_dir/errlvl"
  1141. ) | logstdout "$DARKYELLOW$charm$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${GREEN}@${NORMAL}"
  1142. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$charm$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${RED}@$NORMAL" 3>&1 1>&2 2>&3
  1143. errlvl="$(cat "$relation_dir/errlvl")" || {
  1144. err "Relation script '$script_name' in $DARKYELLOW$charm$NORMAL" \
  1145. "failed before outputing an errorlevel."
  1146. ((errlvl |= "1" ))
  1147. }
  1148. if [ -e "$RELATION_CONFIG" ]; then
  1149. _config_merge "$state_tmpdir/to-merge-in-docker-compose.yml" "$RELATION_CONFIG" &&
  1150. rm "$RELATION_CONFIG"
  1151. ((errlvl |= "$?" ))
  1152. fi
  1153. if [ "$errlvl" != 0 ]; then
  1154. err "Relation $DARKBLUE$relation_name$NORMAL on $DARKYELLOW$charm$NORMAL failed to run properly."
  1155. fi
  1156. else
  1157. verb "No relation script '$script_name' in charm $DARKYELLOW$charm$NORMAL. Ignoring."
  1158. fi
  1159. else
  1160. err "Relation $DARKBLUE$relation_name$NORMAL on $DARKYELLOW$target_charm$NORMAL failed to run properly."
  1161. fi
  1162. if [ "$target_errlvl" == 0 -a "$errlvl" == 0 ]; then
  1163. debug "Relation $DARKBLUE$relation_name$NORMAL is established" \
  1164. "between $DARKYELLOW$service$NORMAL and $DARKYELLOW$target_service$NORMAL."
  1165. return 0
  1166. else
  1167. return 1
  1168. fi
  1169. }
  1170. export -f _run_service_relation
  1171. _get_compose_relations_cached () {
  1172. local compose_service_def="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  1173. relation_name relation_def target_service
  1174. if [ -e "$cache_file" ]; then
  1175. #debug "$FUNCNAME: STATIC cache hit $1"
  1176. cat "$cache_file"
  1177. touch "$cache_file"
  1178. return 0
  1179. fi
  1180. (
  1181. set -o pipefail
  1182. if [ "$compose_service_def" ]; then
  1183. while read-0 relation_name relation_def; do
  1184. (
  1185. case "$(echo "$relation_def" | shyaml get-type 2>/dev/null)" in
  1186. "str")
  1187. target_service="$(echo "$relation_def" | shyaml get-value 2>/dev/null)"
  1188. tech_dep="$(get_charm_tech_dep_orientation_for_relation "$target_service" "$relation_name")"
  1189. echo -en "$relation_name\0$target_service\0\0$tech_dep\0"
  1190. ;;
  1191. "sequence")
  1192. while read-0 target_service; do
  1193. tech_dep="$(get_charm_tech_dep_orientation_for_relation "$target_service" "$relation_name")"
  1194. echo -en "$relation_name\0$target_service\0\0$tech_dep\0"
  1195. done < <(echo "$relation_def" | shyaml get-values-0 2>/dev/null)
  1196. ;;
  1197. "struct")
  1198. while read-0 target_service relation_config; do
  1199. tech_dep="$(get_charm_tech_dep_orientation_for_relation "$target_service" "$relation_name")"
  1200. echo -en "$relation_name\0$target_service\0$relation_config\0$tech_dep\0"
  1201. done < <(echo "$relation_def" | shyaml key-values-0 2>/dev/null)
  1202. ;;
  1203. esac
  1204. ) </dev/null >> "$cache_file"
  1205. done < <(echo "$compose_service_def" | shyaml key-values-0 relations 2>/dev/null)
  1206. fi
  1207. )
  1208. if [ "$?" != 0 ]; then
  1209. err "Error while looking for compose relations."
  1210. rm -f "$cache_file" ## no cache
  1211. return 1
  1212. fi
  1213. [ -e "$cache_file" ] && cat "$cache_file"
  1214. return 0
  1215. }
  1216. export -f _get_compose_relations_cached
  1217. get_compose_relations () {
  1218. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  1219. compose_def
  1220. if [ -e "$cache_file" ]; then
  1221. #debug "$FUNCNAME: SESSION cache hit $1"
  1222. cat "$cache_file"
  1223. return 0
  1224. fi
  1225. compose_def="$(get_compose_service_def "$service")" || return 1
  1226. _get_compose_relations_cached "$compose_def" > "$cache_file"
  1227. if [ "$?" != 0 ]; then
  1228. err "Error while looking for compose relations."
  1229. rm -f "$cache_file" ## no cache
  1230. return 1
  1231. fi
  1232. cat "$cache_file"
  1233. }
  1234. export -f get_compose_relations
  1235. run_service_relations () {
  1236. local services="$1" loaded
  1237. PROJECT_NAME=$(get_default_project_name) || return 1
  1238. export PROJECT_NAME
  1239. declare -A loaded
  1240. for service in $(get_ordered_service_dependencies $services); do
  1241. # debug "Upping dep's relations of ${DARKYELLOW}$service${NORMAL}:"
  1242. for subservice in $(get_service_deps "$service") "$service"; do
  1243. [ "${loaded[$subservice]}" ] && continue
  1244. export BASE_SERVICE_NAME=$service
  1245. MASTER_BASE_CHARM_NAME=$(get_top_master_charm_for_service "$subservice") || return 1
  1246. RELATION_BASE_COMPOSE_DEF=$(get_compose_service_def "$subservice") || return 1
  1247. export RELATION_BASE_COMPOSE_DEF MASTER_BASE_CHARM_NAME
  1248. # debug " Relations of ${DARKYELLOW}$subservice${NORMAL}:"
  1249. while read-0 relation_name target_service relation_config tech_dep; do
  1250. export relation_config
  1251. export TARGET_SERVICE_NAME=$target_service
  1252. MASTER_TARGET_CHARM_NAME=$(get_top_master_charm_for_service "$target_service") || return 1
  1253. RELATION_TARGET_COMPOSE_DEF=$(get_compose_service_def "$target_service") || return 1
  1254. export MASTER_TARGET_CHARM_NAME RELATION_TARGET_COMPOSE_DEF
  1255. Wrap -d "Building $DARKYELLOW$subservice$NORMAL --$DARKBLUE$relation_name$NORMAL--> $DARKYELLOW$target_service$NORMAL" <<EOF || return 1
  1256. _run_service_relation "$relation_name" "$subservice" "$target_service" "\$relation_config"
  1257. EOF
  1258. done < <(get_compose_relations "$subservice") || return 1
  1259. loaded[$subservice]=1
  1260. done
  1261. done
  1262. }
  1263. export -f run_service_relations
  1264. _run_service_action_direct() {
  1265. local service="$1" action="$2" charm _dummy
  1266. shift; shift
  1267. read-0 charm || true ## against 'set -e' that could be setup in parent scripts
  1268. if read-0 _dummy || [ "$_dummy" ]; then
  1269. print_syntax_error "$FUNCNAME: too many arguments in action descriptor"
  1270. return 1
  1271. fi
  1272. compose_file=$(get_compose_yml_location) || return 1
  1273. export action_errlvl_file="$state_tmpdir/action-$service-$charm-$action-errlvl"
  1274. export state_tmpdir
  1275. {
  1276. (
  1277. set +e ## Prevents unwanted leaks from parent shell
  1278. export COMPOSE_CONFIG=$(cat "$compose_file")
  1279. export METADATA_CONFIG=$(charm.metadata "$charm")
  1280. export SERVICE_NAME=$service
  1281. export ACTION_NAME=$action
  1282. export CONTAINER_NAME=$(get_top_master_charm_for_service "$service")
  1283. export DOCKER_BASE_IMAGE=$(service_base_docker_image "$CONTAINER_NAME")
  1284. export SERVICE_DATASTORE="$DATASTORE/$service"
  1285. export SERVICE_CONFIGSTORE="$CONFIGSTORE/$service"
  1286. exname="$exname $ACTION_NAME $SERVICE_NAME" \
  1287. stdbuf -oL -eL charm.run_direct_action "$charm" "$action" "$@"
  1288. echo "$?" > "$action_errlvl_file"
  1289. ) | logstdout "$DARKYELLOW$charm$NORMAL/${DARKCYAN}$action${NORMAL} ${GREEN}@${NORMAL}"
  1290. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$charm$NORMAL/${DARKCYAN}$action${NORMAL} ${RED}@$NORMAL" 3>&1 1>&2 2>&3
  1291. if ! [ -e "$action_errlvl_file" ]; then
  1292. err "Action $DARKYELLOW$service$NORMAL:$DARKCYAN$action$NORMAL has failed without having time" \
  1293. "to output an errlvl"
  1294. return 1
  1295. fi
  1296. return "$(cat "$action_errlvl_file")"
  1297. }
  1298. export -f _run_service_action_direct
  1299. _run_service_action_relation() {
  1300. local service="$1" action="$2" charm target_charm relation_name relation_config _dummy
  1301. shift; shift
  1302. read-0 charm target_charm relation_name relation_config || true
  1303. if read-0 _dummy || [ "$_dummy" ]; then
  1304. print_syntax_error "$FUNCNAME: too many arguments in action descriptor"
  1305. return 1
  1306. fi
  1307. export RELATION_DATA_FILE=$(get_relation_data_file "$charm" "$target_charm" "$relation_name" "$relation_config")
  1308. compose_file=$(get_compose_yml_location) || return 1
  1309. export action_errlvl_file="$state_tmpdir/action-$service-$charm-$action-errlvl"
  1310. export state_tmpdir
  1311. {
  1312. (
  1313. set +e ## Prevents unwanted leaks from parent shell
  1314. export METADATA_CONFIG=$(charm.metadata "$charm")
  1315. export SERVICE_NAME=$service
  1316. export RELATION_TARGET_CHARM="$target_charm"
  1317. export RELATION_BASE_CHARM="$charm"
  1318. export ACTION_NAME=$action
  1319. export CONTAINER_NAME=$(get_top_master_charm_for_service "$service")
  1320. export DOCKER_BASE_IMAGE=$(service_base_docker_image "$CONTAINER_NAME")
  1321. export SERVICE_DATASTORE="$DATASTORE/$service"
  1322. export SERVICE_CONFIGSTORE="$CONFIGSTORE/$service"
  1323. exname="$exname $ACTION_NAME $SERVICE_NAME" \
  1324. stdbuf -oL -eL charm.run_relation_action "$charm" "$relation_name" "$action" "$@"
  1325. echo "$?" > "$action_errlvl_file"
  1326. ) | logstdout "$DARKYELLOW$charm$NORMAL/${DARKCYAN}$action${NORMAL} ${GREEN}@${NORMAL}"
  1327. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$charm$NORMAL/${DARKCYAN}$action${NORMAL} ${RED}@$NORMAL" 3>&1 1>&2 2>&3
  1328. if ! [ -e "$action_errlvl_file" ]; then
  1329. err "Action $DARKYELLOW$service$NORMAL:$DARKCYAN$action$NORMAL has failed without having time" \
  1330. "to output an errlvl"
  1331. return 1
  1332. fi
  1333. return "$(cat "$action_errlvl_file")"
  1334. }
  1335. export -f _run_service_action_relation
  1336. get_relation_data_dir() {
  1337. local charm="$1" target_charm="$2" relation_name="$3" cache_file="$state_tmpdir/$FUNCNAME.cache.$1"
  1338. if [ -e "$cache_file" ]; then
  1339. # debug "$FUNCNAME: cache hit ($*)"
  1340. cat "$cache_file"
  1341. return 0
  1342. fi
  1343. project=$(get_default_project_name) || return 1
  1344. relation_dir="$VARDIR/relations/$project/$charm-$target_charm/$relation_name"
  1345. if ! [ -d "$relation_dir" ]; then
  1346. mkdir -p "$relation_dir" || return 1
  1347. chmod go-rwx "$relation_dir" || return 1 ## protecting this directory
  1348. fi
  1349. echo "$relation_dir" || tee "$cache_file"
  1350. }
  1351. export -f get_relation_data_dir
  1352. get_relation_data_file() {
  1353. local charm="$1" target_charm="$2" relation_name="$3" relation_config="$4"
  1354. relation_dir=$(get_relation_data_dir "$charm" "$target_charm" "$relation_name") || return 1
  1355. relation_data_file="$relation_dir/data"
  1356. new=
  1357. if [ -e "$relation_data_file" ]; then
  1358. ## Has reference changed ?
  1359. new_md5=$(echo "$relation_config" | md5_compat)
  1360. if [ "$new_md5" != "$(cat "$relation_data_file.md5_ref" 2>/dev/null)" ]; then
  1361. new=true
  1362. fi
  1363. else
  1364. new=true
  1365. fi
  1366. if [ "$new" ]; then
  1367. echo "$relation_config" > "$relation_data_file"
  1368. chmod go-rwx "$relation_data_file" ## protecting this file
  1369. echo "$relation_config" | md5_compat > "$relation_data_file.md5_ref"
  1370. fi
  1371. echo "$relation_data_file"
  1372. }
  1373. export -f get_relation_data_file
  1374. has_service_action () {
  1375. local service="$1" action="$2" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  1376. charm target_charm
  1377. if [ -e "$cache_file" ]; then
  1378. # debug "$FUNCNAME: cache hit ($*)"
  1379. cat "$cache_file"
  1380. return 0
  1381. fi
  1382. charm=$(get_service_charm "$service") || return 1
  1383. ## Action directly provided ?
  1384. if charm.has_direct_action "$charm" "$action"; then
  1385. echo -en "direct\0$charm" | tee "$cache_file"
  1386. return 0
  1387. fi
  1388. ## Action provided by relation ?
  1389. while read-0 relation_name target_service relation_config tech_dep; do
  1390. target_charm=$(get_service_charm "$target_service") || return 1
  1391. if charm.has_relation_action "$target_charm" "$relation_name" "$action"; then
  1392. echo -en "relation\0$charm\0$target_charm\0$relation_name\0$relation_config" | tee "$cache_file"
  1393. return 0
  1394. fi
  1395. done < <(get_compose_relations "$service")
  1396. return 1
  1397. # master=$(get_top_master_charm_for_service "$charm")
  1398. # [ "$master" == "$charm" ] && return 1
  1399. # has_service_action "$master" "$action"
  1400. }
  1401. export -f has_service_action
  1402. run_service_action () {
  1403. local service="$1" action="$2"
  1404. shift ; shift
  1405. {
  1406. if ! read-0 action_type; then
  1407. info "Service $DARKYELLOW$service$NORMAL does not have any action $DARKCYAN$action$NORMAL defined."
  1408. info " Add an executable script to 'actions/$action' to implement action."
  1409. return 1
  1410. fi
  1411. Section "running $DARKYELLOW$service$NORMAL/$DARKCYAN$action$NORMAL ($action_type)"; Feed
  1412. "_run_service_action_${action_type}" "$service" "$action" "$@"
  1413. } < <(has_service_action "$service" "$action")
  1414. }
  1415. export -f run_service_action
  1416. get_compose_relation_config() {
  1417. local service=$1 relation_config cache_file="$state_tmpdir/$FUNCNAME.cache.$1"
  1418. if [ -e "$cache_file" ]; then
  1419. # debug "$FUNCNAME: cache hit ($*)"
  1420. cat "$cache_file"
  1421. return 0
  1422. fi
  1423. compose_service_def=$(get_compose_service_def "$service") || return 1
  1424. echo "$compose_service_def" | shyaml get-value "relations" 2>/dev/null | tee "$cache_file"
  1425. }
  1426. export -f get_compose_relation_config
  1427. # ## Return key-values-0
  1428. # get_compose_relation_config_for_service() {
  1429. # local service=$1 relation_name=$2 relation_config
  1430. # compose_service_relations=$(get_compose_relation_config "$service") || return 1
  1431. # if ! relation_config=$(
  1432. # echo "$compose_service_relations" |
  1433. # shyaml get-value "${relation_name}" 2>/dev/null); then
  1434. # err "Couldn't find $DARKYELLOW${service}$NORMAL/${WHITE}${relation_name}$NORMAL" \
  1435. # "relation config in compose configuration."
  1436. # return 1
  1437. # fi
  1438. # if [ -z "$relation_config" ]; then
  1439. # err "Relation ${WHITE}mysql-database$NORMAL is empty in compose configuration."
  1440. # return 1
  1441. # fi
  1442. # if ! echo "$relation_config" | shyaml key-values-0 2>/dev/null; then
  1443. # err "No key/values in ${DARKBLUE}mysql-database$NORMAL of compose config."
  1444. # return 1
  1445. # fi
  1446. # }
  1447. # export -f get_compose_relation_config_for_service
  1448. _get_master_charm_for_service_cached () {
  1449. local service="$1" charm="$2" metadata="$3" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  1450. charm requires master_charm target_charm target_service service_def
  1451. if [ -e "$cache_file" ]; then
  1452. # debug "$FUNCNAME: STATIC cache hit ($1)"
  1453. cat "$cache_file"
  1454. touch "$cache_file"
  1455. return 0
  1456. fi
  1457. if [ "$(echo "$metadata" | shyaml get-value "subordinate" 2>/dev/null)" != "True" ]; then
  1458. ## just return charm name
  1459. echo "$charm" | tee "$cache_file"
  1460. return 0
  1461. fi
  1462. ## fetch the container relation
  1463. requires="$(echo "$metadata" | shyaml get-value "requires" 2>/dev/null)"
  1464. if [ -z "$requires" ]; then
  1465. die "Charm $DARKYELLOW$charm$NORMAL is a subordinate but does not have any 'requires' " \
  1466. "section."
  1467. fi
  1468. master_charm=
  1469. while read-0 relation_name relation; do
  1470. if [ "$(echo "$relation" | shyaml get-value "scope" 2>/dev/null)" == "container" ]; then
  1471. # debug "$DARKYELLOW$service$NORMAL's relation" \
  1472. # "$DARKBLUE${relation_name}$NORMAL is a container."
  1473. interface="$(echo "$relation" | shyaml get-value "interface" 2>/dev/null)"
  1474. if [ -z "$interface" ]; then
  1475. err "No ${WHITE}$interface${NORMAL} set for relation $relation_name."
  1476. return 1
  1477. fi
  1478. ## Action provided by relation ?
  1479. target_service=
  1480. while read-0 relation_name candidate_target_service _relation_config _tech_dep; do
  1481. [ "$interface" == "$relation_name" ] && {
  1482. target_service="$candidate_target_service"
  1483. break
  1484. }
  1485. done < <(get_compose_relations "$service")
  1486. if [ -z "$target_service" ]; then
  1487. err "Couldn't find ${WHITE}relations.$interface${NORMAL} in" \
  1488. "${DARKYELLOW}$service$NORMAL compose definition."
  1489. return 1
  1490. fi
  1491. master_charm=$(get_service_charm "$target_service") || return 1
  1492. break
  1493. fi
  1494. done < <(echo "$requires" | shyaml key-values-0 2>/dev/null)
  1495. if [ -z "$master_charm" ]; then
  1496. die "Charm $DARKYELLOW$charm$NORMAL is a subordinate but does not have any relation with" \
  1497. " ${WHITE}scope${NORMAL} set to 'container'."
  1498. fi
  1499. echo "$master_charm" | tee "$cache_file"
  1500. }
  1501. export -f _get_master_charm_for_service_cached
  1502. get_master_charm_for_service() {
  1503. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  1504. charm metadata result
  1505. if [ -e "$cache_file" ]; then
  1506. # debug "$FUNCNAME: SESSION cache hit ($*)"
  1507. cat "$cache_file"
  1508. return 0
  1509. fi
  1510. charm=$(get_service_charm "$service") || return 1
  1511. metadata=$(charm.metadata "$charm") || {
  1512. err "Failed to access metadata of charm $DARKYELLOW$charm$NORMAL"
  1513. return 1
  1514. }
  1515. result=$(_get_master_charm_for_service_cached "$service" "$charm" "$metadata") || return 1
  1516. echo "$result" | tee "$cache_file"
  1517. }
  1518. export -f get_master_charm_for_service
  1519. get_top_master_charm_for_service() {
  1520. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  1521. current_service
  1522. if [ -e "$cache_file" ]; then
  1523. # debug "$FUNCNAME: cache hit ($*)"
  1524. cat "$cache_file"
  1525. return 0
  1526. fi
  1527. current_service="$service"
  1528. while true; do
  1529. master_service=$(get_master_charm_for_service "$current_service") || return 1
  1530. [ "$master_service" == "$current_service" ] && break
  1531. current_service="$master_service"
  1532. done
  1533. echo "$current_service" | tee "$cache_file"
  1534. return 0
  1535. }
  1536. export -f get_top_master_charm_for_service
  1537. ##
  1538. ## The result is a mixin that is not always a complete valid
  1539. ## docker-compose entry (thinking of subordinates). The result
  1540. ## will be merge with master charms.
  1541. _get_docker_compose_mixin_from_metadata_cached() {
  1542. local charm="$1" metadata="$2" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  1543. metadata_file metadata volumes docker_compose subordinate image
  1544. if [ -e "$cache_file" ]; then
  1545. #debug "$FUNCNAME: STATIC cache hit $1"
  1546. cat "$cache_file"
  1547. touch "$cache_file"
  1548. return 0
  1549. fi
  1550. mixin=
  1551. if [ "$metadata" ]; then
  1552. ## resources to volumes
  1553. volumes=$(
  1554. for resource_type in data config; do
  1555. while read-0 resource; do
  1556. eval "echo \" - \$${resource_type^^}STORE/\$charm\$resource:\$resource:rw\""
  1557. done < <(echo "$metadata" | shyaml get-values-0 "${resource_type}-resources" 2>/dev/null)
  1558. done
  1559. while read-0 resource; do
  1560. if [[ "$resource" == *:* ]]; then
  1561. echo " - $resource:rw"
  1562. else
  1563. echo " - $resource:$resource:rw"
  1564. fi
  1565. done < <(echo "$metadata" | shyaml get-values-0 "host-resources" 2>/dev/null)
  1566. while read-0 resource; do
  1567. dest="$(charm.get_dir "$charm")/resources$resource"
  1568. if ! [ -e "$dest" ]; then
  1569. die "charm-resource: '$resource' does not exist (file: '$dest')."
  1570. fi
  1571. echo " - $dest:$resource:rw"
  1572. done < <(echo "$metadata" | shyaml get-values-0 "charm-resources" 2>/dev/null)
  1573. ) || return 1
  1574. if [ "$volumes" ]; then
  1575. mixin=$(echo -en "volumes:\n$volumes")
  1576. fi
  1577. docker_compose=$(echo "$metadata" | shyaml get-value "docker-compose" 2>/dev/null)
  1578. if [ "$docker_compose" ]; then
  1579. mixin=$(merge_yaml_str "$mixin" "$docker_compose")
  1580. fi
  1581. if [ "$(echo "$metadata" | shyaml get-value "subordinate" 2>/dev/null)" == "True" ]; then
  1582. subordinate=true
  1583. fi
  1584. fi
  1585. image=$(echo "$metadata" | shyaml get-value "docker-image" 2>/dev/null)
  1586. image_or_build_statement=
  1587. if [ "$image" ]; then
  1588. if [ "$subordinate" ]; then
  1589. err "Subordinate charm can not have a ${WHITE}docker-image${NORMAL} value."
  1590. return 1
  1591. fi
  1592. image_or_build_statement="image: $image"
  1593. elif [ -d "$(charm.get_dir "$charm")/build" ]; then
  1594. if [ "$subordinate" ]; then
  1595. err "Subordinate charm can not have a 'build' sub directory."
  1596. return 1
  1597. fi
  1598. image_or_build_statement="build: $charm/build"
  1599. fi
  1600. if [ "$image_or_build_statement" ]; then
  1601. mixin=$(merge_yaml_str "$mixin" "$image_or_build_statement")
  1602. fi
  1603. echo "$mixin" | tee "$cache_file"
  1604. }
  1605. export -f _get_docker_compose_mixin_from_metadata_cached
  1606. get_docker_compose_mixin_from_metadata() {
  1607. local charm="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1"
  1608. if [ -e "$cache_file" ]; then
  1609. #debug "$FUNCNAME: SESSION cache hit ($*)"
  1610. cat "$cache_file"
  1611. return 0
  1612. fi
  1613. metadata="$(charm.metadata "$charm")" || return 1
  1614. mixin=$(_get_docker_compose_mixin_from_metadata_cached "$charm" "$metadata") || return 1
  1615. echo "$mixin" | tee "$cache_file"
  1616. }
  1617. export -f get_docker_compose_mixin_from_metadata
  1618. _save() {
  1619. local name="$1"
  1620. cat - | tee -a "$docker_compose_dir/.data/$name"
  1621. }
  1622. export -f _save
  1623. get_default_project_name() {
  1624. if [ "$DEFAULT_PROJECT_NAME" ]; then
  1625. echo "$DEFAULT_PROJECT_NAME"
  1626. return 0
  1627. fi
  1628. compose_yml_location="$(get_compose_yml_location)" || return 1
  1629. if [ "$compose_yml_location" ]; then
  1630. if normalized_path=$(readlink -e "$compose_yml_location"); then
  1631. echo "$(basename "$(dirname "$normalized_path")")"
  1632. return 0
  1633. fi
  1634. fi
  1635. echo "project"
  1636. return 0
  1637. }
  1638. export -f get_default_project_name
  1639. launch_docker_compose() {
  1640. local charm
  1641. docker_compose_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
  1642. #debug "Creating temporary docker-compose directory in '$docker_compose_tmpdir'."
  1643. # trap_add EXIT "debug \"Removing temporary docker-compose directory in $docker_compose_tmpdir.\";\
  1644. # rm -rf \"$docker_compose_tmpdir\""
  1645. trap_add EXIT "rm -rf \"$docker_compose_tmpdir\""
  1646. project=$(get_default_project_name)
  1647. mkdir -p "$docker_compose_tmpdir/$project"
  1648. docker_compose_dir="$docker_compose_tmpdir/$project"
  1649. if [ -z "$SERVICE_PACK" ]; then
  1650. export SERVICE_PACK=$(get_default_target_services $SERVICE_PACK)
  1651. fi
  1652. get_docker_compose $SERVICE_PACK > "$docker_compose_dir/docker-compose.yml" || return 1
  1653. if [ -e "$state_tmpdir/to-merge-in-docker-compose.yml" ]; then
  1654. # debug "Merging some config data in docker-compose.yml:"
  1655. # debug "$(cat $state_tmpdir/to-merge-in-docker-compose.yml)"
  1656. _config_merge "$docker_compose_dir/docker-compose.yml" "$state_tmpdir/to-merge-in-docker-compose.yml" || return 1
  1657. fi
  1658. if [ -z "$(echo $(cat "$docker_compose_dir/docker-compose.yml"))" ]; then
  1659. die "Generated 'docker-compose.yml' is unexpectedly empty."
  1660. fi
  1661. ## XXXvlab: could be more specific and only link the needed charms
  1662. for charm in $(shyaml keys < "$docker_compose_dir/docker-compose.yml"); do
  1663. if charm.exists "$charm"; then
  1664. ln -sf "$(charm.get_dir "$charm")" "$docker_compose_dir/$charm" || exit 1
  1665. fi
  1666. done
  1667. mkdir "$docker_compose_dir/.data"
  1668. {
  1669. {
  1670. cd "$docker_compose_dir"
  1671. debug "${WHITE}docker-compose.yml$NORMAL for $DARKYELLOW$SERVICE_PACK$NORMAL:"
  1672. debug "$(cat "$docker_compose_dir/docker-compose.yml" | prefix " $GRAY|$NORMAL ")"
  1673. debug "${WHITE}Launching$NORMAL: docker-compose $@"
  1674. docker-compose "$@"
  1675. echo "$?" > "$docker_compose_dir/.data/errlvl"
  1676. } | _save stdout
  1677. } 3>&1 1>&2 2>&3 | _save stderr 3>&1 1>&2 2>&3
  1678. 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
  1679. err "Detected bug https://github.com/docker/docker/issues/4036 ... "
  1680. err "Please re-launch your command, or switch from 'devicemapper' driver to 'overlayfs' or 'aufs'."
  1681. fi
  1682. docker_compose_errlvl="$(cat "$docker_compose_dir/.data/errlvl" 2>/dev/null)"
  1683. if [ -z "$docker_compose_errlvl" ]; then
  1684. err "Something went wrong before you could gather docker-compose errorlevel."
  1685. return 1
  1686. fi
  1687. return "$docker_compose_errlvl"
  1688. }
  1689. export -f launch_docker_compose
  1690. get_compose_yml_location() {
  1691. parent=$(while ! [ -e "./compose.yml" ]; do
  1692. [ "$PWD" == "/" ] && exit 0
  1693. cd ..
  1694. done; echo "$PWD"
  1695. )
  1696. if [ "$parent" ]; then
  1697. echo "$parent/compose.yml"
  1698. return 0
  1699. fi
  1700. if [ "$DEFAULT_COMPOSE_FILE" ]; then
  1701. if ! [ -e "$DEFAULT_COMPOSE_FILE" ]; then
  1702. err "No 'compose.yml' was found in current or parent dirs," \
  1703. "and \$DEFAULT_COMPOSE_FILE points to an unexistent file."
  1704. die "Please provide a 'compose.yml' file."
  1705. fi
  1706. echo "$DEFAULT_COMPOSE_FILE"
  1707. return 0
  1708. fi
  1709. err "No 'compose.yml' was found in current or parent dirs, and no \$DEFAULT_COMPOSE_FILE was set."
  1710. die "Please provide a 'compose.yml' file."
  1711. return 1
  1712. }
  1713. export -f get_compose_yml_location
  1714. get_default_target_services() {
  1715. local services=("$@")
  1716. if [ -z "${services[*]}" ]; then
  1717. if [ "$DEFAULT_SERVICES" ]; then
  1718. debug "No service provided, using $WHITE\$DEFAULT_SERVICES$NORMAL variable." \
  1719. "Target services: $DARKYELLOW$DEFAULT_SERVICES$NORMAL"
  1720. services="$DEFAULT_SERVICES"
  1721. else
  1722. err "No service provided."
  1723. return 1
  1724. fi
  1725. fi
  1726. echo "${services[*]}"
  1727. }
  1728. export -f get_default_target_services
  1729. get_master_services() {
  1730. local loaded master_service
  1731. declare -A loaded
  1732. for service in "$@"; do
  1733. master_service=$(get_top_master_charm_for_service "$service") || return 1
  1734. if [ "${loaded[$master_service]}" ]; then
  1735. continue
  1736. fi
  1737. echo "$master_service"
  1738. loaded["$master_service"]=1
  1739. done | xargs echo
  1740. }
  1741. export -f get_master_services
  1742. _setup_state_dir() {
  1743. export state_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
  1744. #debug "Creating temporary state directory in '$state_tmpdir'."
  1745. # trap_add EXIT "debug \"Removing temporary state directory in $state_tmpdir.\";\
  1746. # rm -rf \"$state_tmpdir\""
  1747. trap_add EXIT "rm -rf \"$state_tmpdir\""
  1748. }
  1749. get_docker_compose_opts_list() {
  1750. local action="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$action"; cat "$(which docker-compose)" | md5_compat)"
  1751. if [ -e "$cache_file" ]; then
  1752. cat "$cache_file"
  1753. return 0
  1754. fi
  1755. docker_compose_help_msg=$(docker-compose "$action" --help 2>/dev/null) || return 1
  1756. echo "$docker_compose_help_msg" | grep '^Options:' -A 20000 |
  1757. tail -n +2 |
  1758. egrep "^\s+-" |
  1759. sed -r 's/\s+((((-[a-zA-Z]|--[a-zA-Z0-9-]+)( [A-Z=]+|=[^ ]+)?)(, )?)+)\s+.*$/\1/g' |
  1760. tee "$cache_file"
  1761. }
  1762. _MULTIOPTION_REGEX='^((-[a-zA-Z]|--[a-zA-Z0-9-]+)(, )?)+'
  1763. _MULTIOPTION_REGEX_LINE_FILTER=$_MULTIOPTION_REGEX'(\s|=)'
  1764. get_docker_compose_multi_opts_list() {
  1765. local action="$1" opts_list
  1766. opts_list=$(get_docker_compose_opts_list "$action") || return 1
  1767. echo "$opts_list" | egrep "$_MULTIOPTION_REGEX_LINE_FILTER" |
  1768. sed -r "s/^($_MULTIOPTION_REGEX)(\s|=).*$/\1/g" |
  1769. tr ',' "\n" | xargs echo
  1770. }
  1771. get_docker_compose_single_opts_list() {
  1772. local action="$1" opts_list
  1773. opts_list=$(get_docker_compose_opts_list "$action") || return 1
  1774. echo "$opts_list" | egrep -v "$_MULTIOPTION_REGEX_LINE_FILTER" |
  1775. tr ',' "\n" | xargs echo
  1776. }
  1777. clean_cache() {
  1778. local i=0
  1779. for f in $(ls -t "$CACHEDIR/"*.cache.* 2>/dev/null | tail -n +500); do
  1780. ((i++))
  1781. rm -f "$f"
  1782. done
  1783. if (( i > 0 )); then
  1784. debug "${WHITE}Cleaned cache:${NORMAL} Removed $((i)) elements (current cache size is $(du -sh "$CACHEDIR" | cut -f 1))"
  1785. fi
  1786. }
  1787. [ "$SOURCED" ] && return 0
  1788. if [ -z "$DISABLE_SYSTEM_CONFIG_FILE" ]; then
  1789. if [ -r /etc/default/charm ]; then
  1790. . /etc/default/charm
  1791. fi
  1792. if [ -r "/etc/default/$exname" ]; then
  1793. . "/etc/default/$exname"
  1794. fi
  1795. ## XXXvlab: should provide YML config opportunities in possible parent dirs ?
  1796. ## userdir ? and global /etc/compose.yml ?
  1797. for cfgfile in /etc/compose.conf /etc/compose.local.conf \
  1798. /etc/default/compose /etc/compose/local.conf; do
  1799. [ -e "$cfgfile" ] || continue
  1800. . "$cfgfile" || die "Loading config file '$cfgfile' failed."
  1801. done
  1802. fi
  1803. _setup_state_dir
  1804. mkdir -p "$CACHEDIR" || exit 1
  1805. DOCKER_HOST_NET=$(ip addr show docker0 | grep 'inet ' | xargs echo | cut -f 2 -d " ") || {
  1806. die "Couldn't determine docker host net"
  1807. }
  1808. DOCKER_HOST_IP=$(echo "$DOCKER_HOST_NET" | cut -f 1 -d "/") || {
  1809. die "Couldn't determine docker host ip"
  1810. }
  1811. export DOCKER_HOST_NET DOCKER_HOST_IP
  1812. ##
  1813. ## Argument parsing
  1814. ##
  1815. remainder_args=()
  1816. compose_opts=()
  1817. action_opts=()
  1818. no_hooks=
  1819. no_init=
  1820. action=
  1821. stage="main" ## switches from 'main', to 'action', 'remainder'
  1822. is_docker_compose_action=
  1823. DC_MATCH_MULTI=
  1824. DC_MATCH_SINGLE=
  1825. while [ "$#" != 0 ]; do
  1826. case "$stage" in
  1827. "main")
  1828. case "$1" in
  1829. --help|-h)
  1830. no_init=true ; no_hooks=true ; no_relations=true
  1831. print_help
  1832. exit 0
  1833. ;;
  1834. --verbose|-v)
  1835. export VERBOSE=true
  1836. ;;
  1837. --version|-V)
  1838. print_version
  1839. exit 0
  1840. ;;
  1841. -f)
  1842. [ -e "$2" ] || die "File $2 doesn't exists"
  1843. export DEFAULT_COMPOSE_FILE="$2"
  1844. shift
  1845. ;;
  1846. -p)
  1847. export DEFAULT_PROJECT_NAME="$2"
  1848. shift
  1849. ;;
  1850. --no-relations)
  1851. export no_relations=true
  1852. ;;
  1853. --no-hooks)
  1854. export no_hooks=true
  1855. ;;
  1856. --no-init)
  1857. export no_init=true
  1858. ;;
  1859. --debug)
  1860. export DEBUG=true
  1861. export VERBOSE=true
  1862. ;;
  1863. --*|-*)
  1864. compose_opts+=("$1")
  1865. ;;
  1866. *)
  1867. action="$1"
  1868. stage="action"
  1869. DC_MATCH_MULTI=$(get_docker_compose_multi_opts_list "$action") &&
  1870. DC_MATCH_SINGLE="$(get_docker_compose_single_opts_list "$action")"
  1871. if [ "$DC_MATCH_MULTI" ]; then
  1872. DC_MATCH_SINGLE="$DC_MATCH_SINGLE $(echo "$DC_MATCH_MULTI" | sed -r 's/( |$)/=\* /g')"
  1873. fi
  1874. is_docker_compose_action=true
  1875. ;;
  1876. esac
  1877. ;;
  1878. "action")
  1879. case "$1" in
  1880. --help|-h)
  1881. no_init=true ; no_hooks=true ; no_relations=true
  1882. action_opts+=("$1")
  1883. ;;
  1884. --*|-*)
  1885. if [ "$is_docker_compose_action" ]; then
  1886. if str_matches "$1" $DC_MATCH_MULTI; then
  1887. action_opts=("$1" "$2")
  1888. shift;
  1889. elif str_matches "$1" $DC_MATCH_SINGLE; then
  1890. action_opts=("$1")
  1891. else
  1892. err "Unknown option '$1'. Please check help."
  1893. docker-compose "$action" --help >&2
  1894. exit 1
  1895. fi
  1896. fi
  1897. ;;
  1898. *)
  1899. action_posargs+=("$1")
  1900. stage="remainder"
  1901. ;;
  1902. esac
  1903. ;;
  1904. "remainder")
  1905. remainder_args+=("$@")
  1906. break 3;;
  1907. esac
  1908. shift
  1909. done
  1910. [ "${compose_opts[*]}" ] && debug "Main docker-compose opts: ${compose_opts[*]}"
  1911. [ "${action_opts[*]}" ] && debug "Action $DARKCYAN$action$NORMAL with opts: ${action_opts[*]}"
  1912. [ "${remainder_args[*]}" ] && debug "Remainder args: ${remainder_args[*]}"
  1913. ##
  1914. ## Actual code
  1915. ##
  1916. export DOCKER_DATASTORE=${DOCKER_DATASTORE:-/srv/docker-datastore}
  1917. COMPOSE_YML_FILE=$(get_compose_yml_location) || exit 1
  1918. export COMPOSE_YML_FILE
  1919. debug "Found $WHITE$exname$NORMAL YAML file in '$COMPOSE_YML_FILE'."
  1920. output=$(shyaml get-value < "$COMPOSE_YML_FILE" 2>&1)
  1921. if [ "$?" != 0 ]; then
  1922. err "Invalid YAML in '$COMPOSE_YML_FILE':"
  1923. while IFS='' read -r line1 && IFS='' read -r line2; do
  1924. echo "$line1 $GRAY($line2)$NORMAL"
  1925. done < <(echo "$output" | grep ^yaml.scanner -A 100 |
  1926. sed -r 's/^ in "<stdin>", //g' | sed -r 's/^yaml.scanner.[a-zA-Z]+: //g') |
  1927. prefix " $GRAY|$NORMAL "
  1928. exit 1
  1929. fi
  1930. charm.sanity_checks || die "Sanity checks about charm-store failed. Please correct."
  1931. ##
  1932. ## Get services in command line.
  1933. ##
  1934. is_service_action=
  1935. case "$action" in
  1936. up|build|start|stop|config)
  1937. services="$(get_default_target_services "${action_posargs[@]}")" || exit 1
  1938. orig_services="${action_posargs[@]:1}"
  1939. ;;
  1940. run)
  1941. services="${action_posargs[0]}"
  1942. ;;
  1943. "")
  1944. services=
  1945. ;;
  1946. *)
  1947. if is_service_action=$(has_service_action "${action_posargs[0]}" "$action"); then
  1948. {
  1949. read-0 action_type
  1950. case "$action_type" in
  1951. "relation")
  1952. read-0 _ target_service relation_name
  1953. debug "Found action $DARKYELLOW${action_posargs[0]}$NORMAL/$DARKBLUE$relation_name$NORMAL/$DARKCYAN$action$NORMAL (in $DARKYELLOW$target_service$NORMAL)"
  1954. ;;
  1955. "direct")
  1956. debug "Found action $DARKYELLOW${action_posargs[0]}$NORMAL.$DARKCYAN$action$NORMAL"
  1957. ;;
  1958. esac
  1959. } < <(has_service_action "${action_posargs[0]}" "$action")
  1960. services="${action_posargs[0]}"
  1961. else
  1962. services="$(get_default_target_services "${action_posargs[@]}")"
  1963. fi
  1964. ;;
  1965. esac
  1966. get_docker_compose $services >/dev/null || { ## precalculate variable \$_current_docker_compose
  1967. err "Fails to compile base 'docker-compose.yml'"
  1968. exit 1
  1969. }
  1970. ##
  1971. ## Pre-action
  1972. ##
  1973. full_init=
  1974. case "$action" in
  1975. up|run)
  1976. full_init=true
  1977. ;;
  1978. "")
  1979. full_init=
  1980. ;;
  1981. *)
  1982. if [ "$is_service_action" ]; then
  1983. full_init=true
  1984. fi
  1985. ;;
  1986. esac
  1987. if [ "$full_init" ]; then
  1988. ## init in order
  1989. if [ -z "$no_init" ]; then
  1990. Section setup host resources
  1991. setup_host_resources "$services" || exit 1
  1992. Section initialisation
  1993. run_service_hook "$services" init || exit 1
  1994. fi
  1995. ## Get relations
  1996. if [ -z "$no_relations" ]; then
  1997. run_service_relations "$services" || exit 1
  1998. fi
  1999. ## XXXvlab: to be removed when all relation and service stuff is resolved
  2000. if [ -z "$no_hooks" ]; then
  2001. ordered_services=$(get_ordered_service_dependencies $services) || exit 1
  2002. for service in $ordered_services; do
  2003. charm=$(get_service_charm "$service") || exit 1
  2004. for script in "$CHARM_STORE/$charm/hooks.d/"*.sh; do
  2005. [ -e "$script" ] || continue
  2006. [ -x "$script" ] || { echo "compose: script $script is not executable." >&2; exit 1; }
  2007. (
  2008. debug "Launching '$script'."
  2009. cd "$(dirname "$script)")";
  2010. "$script" "$@"
  2011. ) || { echo "compose: hook $script failed. Stopping." >&2; exit 1; }
  2012. done
  2013. done
  2014. fi
  2015. fi
  2016. export SERVICE_PACK="$services"
  2017. ##
  2018. ## Docker-compose
  2019. ##
  2020. case "$action" in
  2021. up|start|stop|build)
  2022. master_services=$(get_master_services $SERVICE_PACK) || exit 1
  2023. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" $master_services
  2024. ;;
  2025. run)
  2026. master_service=$(get_master_services $SERVICE_PACK) || exit 1
  2027. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "$master_service" "${remainder_args[@]}"
  2028. ;;
  2029. # enter)
  2030. # master_service=$(get_master_services $SERVICE_PACK) || exit 1
  2031. # [ "${remainder_args[*]}" ] || remainder_args=("/bin/bash" "-c" "export TERM=xterm; exec bash")
  2032. # docker exec -ti "${action_opts[@]}" "$master_service" "${remainder_args[@]}"
  2033. # ;;
  2034. config)
  2035. ## removing the services
  2036. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "${remainder_args[@]}"
  2037. warn "Runtime configuration modification (from relations) are not included here."
  2038. ;;
  2039. *)
  2040. if [ "$is_service_action" ]; then
  2041. run_service_action "$SERVICE_PACK" "$action" "${remainder_args[@]}"
  2042. else
  2043. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "${remainder_args[@]}"
  2044. fi
  2045. ;;
  2046. esac