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.

2399 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_resource () {
  978. local service="$1" service_def location get cfg
  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. }
  999. export -f setup_host_resource
  1000. setup_host_resources () {
  1001. local services="$1" loaded
  1002. declare -A loaded
  1003. for service in $services; do
  1004. for subservice in $(get_ordered_service_dependencies "$service"); do
  1005. if [ "${loaded[$subservice]}" ]; then
  1006. ## Prevent double inclusion of same service if this
  1007. ## service is deps of two or more of your
  1008. ## requirements.
  1009. continue
  1010. fi
  1011. setup_host_resource "$service"
  1012. loaded[$subservice]=1
  1013. done
  1014. done
  1015. return 0
  1016. }
  1017. export -f setup_host_resources
  1018. relation-get () {
  1019. local key="$1"
  1020. cat "$RELATION_DATA_FILE" | shyaml get-value "$key" 2>/dev/null
  1021. if [ "$?" != 0 ]; then
  1022. err "The key $WHITE$key$NORMAL was not found in relation's data."
  1023. return 1
  1024. fi
  1025. }
  1026. export -f relation-get
  1027. relation-base-compose-get () {
  1028. local key="$1"
  1029. echo "$RELATION_BASE_COMPOSE_DEF" | shyaml get-value "options.$key" 2>/dev/null
  1030. if [ "$?" != 0 ]; then
  1031. err "The key $WHITE$key$NORMAL was not found in base service compose definition.."
  1032. return 1
  1033. fi
  1034. }
  1035. export -f relation-base-compose-get
  1036. relation-target-compose-get () {
  1037. local key="$1"
  1038. echo "$RELATION_BASE_COMPOSE_DEF" | shyaml get-value "options.$key" 2>/dev/null
  1039. if [ "$?" != 0 ]; then
  1040. err "The key $WHITE$key$NORMAL was not found in base service compose definition.."
  1041. return 1
  1042. fi
  1043. }
  1044. export -f relation-target-compose-get
  1045. relation-set () {
  1046. local key="$1" value="$2"
  1047. if [ -z "$RELATION_DATA_FILE" ]; then
  1048. err "$FUNCNAME: relation does not seems to be correctly setup."
  1049. return 1
  1050. fi
  1051. if ! [ -r "$RELATION_DATA_FILE" ]; then
  1052. err "$FUNCNAME: can't read relation's data." >&2
  1053. return 1
  1054. fi
  1055. _config_merge "$RELATION_DATA_FILE" <(echo "$key: $value")
  1056. }
  1057. export -f relation-set
  1058. _config_merge() {
  1059. local config_filename="$1" merge_to_file="$2"
  1060. touch "$config_filename" &&
  1061. merge_yaml "$config_filename" "$merge_to_file" > "$config_filename.tmp" || return 1
  1062. mv "$config_filename.tmp" "$config_filename"
  1063. }
  1064. export -f _config_merge
  1065. ## XXXvlab; this can be used only in relation, I'd like to use it in init.
  1066. config-add() {
  1067. local metadata="$1"
  1068. _config_merge "$RELATION_CONFIG" <(echo "$metadata")
  1069. }
  1070. export -f config-add
  1071. ## XXXvlab; this can be used only in relation, I'd like to use it in init.
  1072. init-config-add() {
  1073. local metadata="$1"
  1074. _config_merge "$state_tmpdir/to-merge-in-docker-compose.yml" <(echo "$metadata")
  1075. }
  1076. export -f init-config-add
  1077. logstdout() {
  1078. local name="$1"
  1079. sed -r 's%^%'"${name}"'> %g'
  1080. }
  1081. export -f logstdout
  1082. logstderr() {
  1083. local name="$1"
  1084. sed -r 's%^(.*)$%'"${RED}${name}>${NORMAL} \1"'%g'
  1085. }
  1086. export -f logstderr
  1087. _run_service_relation () {
  1088. local relation_name="$1" service="$2" target_service="$3" relation_config="$4" relation_dir services
  1089. charm=$(get_service_charm "$service") || return 1
  1090. target_charm=$(get_service_charm "$target_service") || return 1
  1091. base_script_name=$(charm.has_relation_hook "$charm" "$relation_name" relation-joined) || true
  1092. target_script_name=$(charm.has_relation_hook "$target_charm" "$relation_name" relation-joined) || true
  1093. [ "$base_script_name" -o "$target_script_name" ] || return 0
  1094. relation_dir=$(get_relation_data_dir "$charm" "$target_charm" "$relation_name") || return 1
  1095. RELATION_DATA_FILE=$(get_relation_data_file "$charm" "$target_charm" "$relation_name" "$relation_config") || return 1
  1096. export BASE_CHARM_NAME=$charm
  1097. export TARGET_CHARM_NAME=$target_charm
  1098. export RELATION_DATA_FILE
  1099. target_errlvl=0
  1100. if [ -z "$target_script_name" ]; then
  1101. verb "No relation script $DARKBLUE$relation_name$NORMAL in target $DARKYELLOW$target_charm$NORMAL."
  1102. else
  1103. verb "Running ${DARKBLUE}$relation_name${NORMAL} relation-joined script" \
  1104. "for target charm $DARKYELLOW$target_charm$NORMAL"
  1105. RELATION_CONFIG="$relation_dir/config_provider"
  1106. DOCKER_BASE_IMAGE=$(service_base_docker_image "$target_service") || return 1
  1107. export DOCKER_BASE_IMAGE RELATION_CONFIG RELATION_DATA
  1108. {
  1109. (
  1110. SERVICE_NAME=$target_service
  1111. SERVICE_DATASTORE="$DATASTORE/$target_charm"
  1112. SERVICE_CONFIGSTORE="$CONFIGSTORE/$target_charm"
  1113. export SERVICE_NAME DOCKER_BASE_IMAGE SERVICE_DATASTORE SERVICE_CONFIGSTORE
  1114. charm.run_relation_hook "$target_charm" "$relation_name" relation-joined
  1115. echo "$?" > "$relation_dir/target_errlvl"
  1116. ) | logstdout "$DARKYELLOW$target_charm$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${GREEN}@${NORMAL}"
  1117. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$target_charm$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${RED}@${NORMAL}" 3>&1 1>&2 2>&3
  1118. target_errlvl="$(cat "$relation_dir/target_errlvl")" || {
  1119. err "Relation script '$script_name' in $DARKYELLOW$target_charm$NORMAL" \
  1120. "failed before outputing an errorlevel."
  1121. ((target_errlvl |= "1" ))
  1122. }
  1123. if [ -e "$RELATION_CONFIG" ]; then
  1124. debug "Merging some new config info in $DARKYELLOW$target_service$NORMAL"
  1125. _config_merge "$state_tmpdir/to-merge-in-docker-compose.yml" "$RELATION_CONFIG" &&
  1126. rm "$RELATION_CONFIG"
  1127. ((target_errlvl |= "$?"))
  1128. fi
  1129. fi
  1130. if [ "$target_errlvl" == 0 ]; then
  1131. errlvl=0
  1132. if [ "$base_script_name" ]; then
  1133. verb "Running ${DARKBLUE}$relation_name${NORMAL} relation-joined script" \
  1134. "for charm $DARKYELLOW$charm$NORMAL"
  1135. RELATION_CONFIG="$relation_dir/config_providee"
  1136. RELATION_DATA="$(cat "$RELATION_DATA_FILE")"
  1137. DOCKER_BASE_IMAGE=$(service_base_docker_image "$service") || return 1
  1138. export DOCKER_BASE_IMAGE RELATION_CONFIG RELATION_DATA
  1139. {
  1140. (
  1141. SERVICE_NAME=$service
  1142. SERVICE_DATASTORE="$DATASTORE/$charm"
  1143. SERVICE_CONFIGSTORE="$CONFIGSTORE/$charm"
  1144. export SERVICE_NAME DOCKER_BASE_IMAGE SERVICE_DATASTORE SERVICE_CONFIGSTORE
  1145. charm.run_relation_hook "$charm" "$relation_name" relation-joined
  1146. echo "$?" > "$relation_dir/errlvl"
  1147. ) | logstdout "$DARKYELLOW$charm$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${GREEN}@${NORMAL}"
  1148. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$charm$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${RED}@$NORMAL" 3>&1 1>&2 2>&3
  1149. errlvl="$(cat "$relation_dir/errlvl")" || {
  1150. err "Relation script '$script_name' in $DARKYELLOW$charm$NORMAL" \
  1151. "failed before outputing an errorlevel."
  1152. ((errlvl |= "1" ))
  1153. }
  1154. if [ -e "$RELATION_CONFIG" ]; then
  1155. _config_merge "$state_tmpdir/to-merge-in-docker-compose.yml" "$RELATION_CONFIG" &&
  1156. rm "$RELATION_CONFIG"
  1157. ((errlvl |= "$?" ))
  1158. fi
  1159. if [ "$errlvl" != 0 ]; then
  1160. err "Relation $DARKBLUE$relation_name$NORMAL on $DARKYELLOW$charm$NORMAL failed to run properly."
  1161. fi
  1162. else
  1163. verb "No relation script '$script_name' in charm $DARKYELLOW$charm$NORMAL. Ignoring."
  1164. fi
  1165. else
  1166. err "Relation $DARKBLUE$relation_name$NORMAL on $DARKYELLOW$target_charm$NORMAL failed to run properly."
  1167. fi
  1168. if [ "$target_errlvl" == 0 -a "$errlvl" == 0 ]; then
  1169. debug "Relation $DARKBLUE$relation_name$NORMAL is established" \
  1170. "between $DARKYELLOW$service$NORMAL and $DARKYELLOW$target_service$NORMAL."
  1171. return 0
  1172. else
  1173. return 1
  1174. fi
  1175. }
  1176. export -f _run_service_relation
  1177. _get_compose_relations_cached () {
  1178. local compose_service_def="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  1179. relation_name relation_def target_service
  1180. if [ -e "$cache_file" ]; then
  1181. #debug "$FUNCNAME: STATIC cache hit $1"
  1182. cat "$cache_file"
  1183. touch "$cache_file"
  1184. return 0
  1185. fi
  1186. (
  1187. set -o pipefail
  1188. if [ "$compose_service_def" ]; then
  1189. while read-0 relation_name relation_def; do
  1190. (
  1191. case "$(echo "$relation_def" | shyaml get-type 2>/dev/null)" in
  1192. "str")
  1193. target_service="$(echo "$relation_def" | shyaml get-value 2>/dev/null)"
  1194. tech_dep="$(get_charm_tech_dep_orientation_for_relation "$target_service" "$relation_name")"
  1195. echo -en "$relation_name\0$target_service\0\0$tech_dep\0"
  1196. ;;
  1197. "sequence")
  1198. while read-0 target_service; do
  1199. tech_dep="$(get_charm_tech_dep_orientation_for_relation "$target_service" "$relation_name")"
  1200. echo -en "$relation_name\0$target_service\0\0$tech_dep\0"
  1201. done < <(echo "$relation_def" | shyaml get-values-0 2>/dev/null)
  1202. ;;
  1203. "struct")
  1204. while read-0 target_service relation_config; do
  1205. tech_dep="$(get_charm_tech_dep_orientation_for_relation "$target_service" "$relation_name")"
  1206. echo -en "$relation_name\0$target_service\0$relation_config\0$tech_dep\0"
  1207. done < <(echo "$relation_def" | shyaml key-values-0 2>/dev/null)
  1208. ;;
  1209. esac
  1210. ) </dev/null >> "$cache_file"
  1211. done < <(echo "$compose_service_def" | shyaml key-values-0 relations 2>/dev/null)
  1212. fi
  1213. )
  1214. if [ "$?" != 0 ]; then
  1215. err "Error while looking for compose relations."
  1216. rm -f "$cache_file" ## no cache
  1217. return 1
  1218. fi
  1219. [ -e "$cache_file" ] && cat "$cache_file"
  1220. return 0
  1221. }
  1222. export -f _get_compose_relations_cached
  1223. get_compose_relations () {
  1224. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  1225. compose_def
  1226. if [ -e "$cache_file" ]; then
  1227. #debug "$FUNCNAME: SESSION cache hit $1"
  1228. cat "$cache_file"
  1229. return 0
  1230. fi
  1231. compose_def="$(get_compose_service_def "$service")" || return 1
  1232. _get_compose_relations_cached "$compose_def" > "$cache_file"
  1233. if [ "$?" != 0 ]; then
  1234. err "Error while looking for compose relations."
  1235. rm -f "$cache_file" ## no cache
  1236. return 1
  1237. fi
  1238. cat "$cache_file"
  1239. }
  1240. export -f get_compose_relations
  1241. run_service_relations () {
  1242. local services="$1" loaded
  1243. PROJECT_NAME=$(get_default_project_name) || return 1
  1244. export PROJECT_NAME
  1245. declare -A loaded
  1246. for service in $(get_ordered_service_dependencies $services); do
  1247. # debug "Upping dep's relations of ${DARKYELLOW}$service${NORMAL}:"
  1248. for subservice in $(get_service_deps "$service") "$service"; do
  1249. [ "${loaded[$subservice]}" ] && continue
  1250. export BASE_SERVICE_NAME=$service
  1251. MASTER_BASE_CHARM_NAME=$(get_top_master_charm_for_service "$subservice") || return 1
  1252. RELATION_BASE_COMPOSE_DEF=$(get_compose_service_def "$subservice") || return 1
  1253. export RELATION_BASE_COMPOSE_DEF MASTER_BASE_CHARM_NAME
  1254. # debug " Relations of ${DARKYELLOW}$subservice${NORMAL}:"
  1255. while read-0 relation_name target_service relation_config tech_dep; do
  1256. export relation_config
  1257. export TARGET_SERVICE_NAME=$target_service
  1258. MASTER_TARGET_CHARM_NAME=$(get_top_master_charm_for_service "$target_service") || return 1
  1259. RELATION_TARGET_COMPOSE_DEF=$(get_compose_service_def "$target_service") || return 1
  1260. export MASTER_TARGET_CHARM_NAME RELATION_TARGET_COMPOSE_DEF
  1261. Wrap -d "Building $DARKYELLOW$subservice$NORMAL --$DARKBLUE$relation_name$NORMAL--> $DARKYELLOW$target_service$NORMAL" <<EOF || return 1
  1262. _run_service_relation "$relation_name" "$subservice" "$target_service" "\$relation_config"
  1263. EOF
  1264. done < <(get_compose_relations "$subservice") || return 1
  1265. loaded[$subservice]=1
  1266. done
  1267. done
  1268. }
  1269. export -f run_service_relations
  1270. _run_service_action_direct() {
  1271. local service="$1" action="$2" charm _dummy
  1272. shift; shift
  1273. read-0 charm || true ## against 'set -e' that could be setup in parent scripts
  1274. if read-0 _dummy || [ "$_dummy" ]; then
  1275. print_syntax_error "$FUNCNAME: too many arguments in action descriptor"
  1276. return 1
  1277. fi
  1278. compose_file=$(get_compose_yml_location) || return 1
  1279. export action_errlvl_file="$state_tmpdir/action-$service-$charm-$action-errlvl"
  1280. export state_tmpdir
  1281. {
  1282. (
  1283. set +e ## Prevents unwanted leaks from parent shell
  1284. export COMPOSE_CONFIG=$(cat "$compose_file")
  1285. export METADATA_CONFIG=$(charm.metadata "$charm")
  1286. export SERVICE_NAME=$service
  1287. export ACTION_NAME=$action
  1288. export CONTAINER_NAME=$(get_top_master_charm_for_service "$service")
  1289. export DOCKER_BASE_IMAGE=$(service_base_docker_image "$CONTAINER_NAME")
  1290. export SERVICE_DATASTORE="$DATASTORE/$service"
  1291. export SERVICE_CONFIGSTORE="$CONFIGSTORE/$service"
  1292. exname="$exname $ACTION_NAME $SERVICE_NAME" \
  1293. stdbuf -oL -eL charm.run_direct_action "$charm" "$action" "$@"
  1294. echo "$?" > "$action_errlvl_file"
  1295. ) | logstdout "$DARKYELLOW$charm$NORMAL/${DARKCYAN}$action${NORMAL} ${GREEN}@${NORMAL}"
  1296. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$charm$NORMAL/${DARKCYAN}$action${NORMAL} ${RED}@$NORMAL" 3>&1 1>&2 2>&3
  1297. if ! [ -e "$action_errlvl_file" ]; then
  1298. err "Action $DARKYELLOW$service$NORMAL:$DARKCYAN$action$NORMAL has failed without having time" \
  1299. "to output an errlvl"
  1300. return 1
  1301. fi
  1302. return "$(cat "$action_errlvl_file")"
  1303. }
  1304. export -f _run_service_action_direct
  1305. _run_service_action_relation() {
  1306. local service="$1" action="$2" charm target_charm relation_name relation_config _dummy
  1307. shift; shift
  1308. read-0 charm target_charm relation_name relation_config || true
  1309. if read-0 _dummy || [ "$_dummy" ]; then
  1310. print_syntax_error "$FUNCNAME: too many arguments in action descriptor"
  1311. return 1
  1312. fi
  1313. export RELATION_DATA_FILE=$(get_relation_data_file "$charm" "$target_charm" "$relation_name" "$relation_config")
  1314. compose_file=$(get_compose_yml_location) || return 1
  1315. export action_errlvl_file="$state_tmpdir/action-$service-$charm-$action-errlvl"
  1316. export state_tmpdir
  1317. {
  1318. (
  1319. set +e ## Prevents unwanted leaks from parent shell
  1320. export METADATA_CONFIG=$(charm.metadata "$charm")
  1321. export SERVICE_NAME=$service
  1322. export RELATION_TARGET_CHARM="$target_charm"
  1323. export RELATION_BASE_CHARM="$charm"
  1324. export ACTION_NAME=$action
  1325. export CONTAINER_NAME=$(get_top_master_charm_for_service "$service")
  1326. export DOCKER_BASE_IMAGE=$(service_base_docker_image "$CONTAINER_NAME")
  1327. export SERVICE_DATASTORE="$DATASTORE/$service"
  1328. export SERVICE_CONFIGSTORE="$CONFIGSTORE/$service"
  1329. exname="$exname $ACTION_NAME $SERVICE_NAME" \
  1330. stdbuf -oL -eL charm.run_relation_action "$charm" "$relation_name" "$action" "$@"
  1331. echo "$?" > "$action_errlvl_file"
  1332. ) | logstdout "$DARKYELLOW$charm$NORMAL/${DARKCYAN}$action${NORMAL} ${GREEN}@${NORMAL}"
  1333. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$charm$NORMAL/${DARKCYAN}$action${NORMAL} ${RED}@$NORMAL" 3>&1 1>&2 2>&3
  1334. if ! [ -e "$action_errlvl_file" ]; then
  1335. err "Action $DARKYELLOW$service$NORMAL:$DARKCYAN$action$NORMAL has failed without having time" \
  1336. "to output an errlvl"
  1337. return 1
  1338. fi
  1339. return "$(cat "$action_errlvl_file")"
  1340. }
  1341. export -f _run_service_action_relation
  1342. get_relation_data_dir() {
  1343. local charm="$1" target_charm="$2" relation_name="$3" cache_file="$state_tmpdir/$FUNCNAME.cache.$1"
  1344. if [ -e "$cache_file" ]; then
  1345. # debug "$FUNCNAME: cache hit ($*)"
  1346. cat "$cache_file"
  1347. return 0
  1348. fi
  1349. project=$(get_default_project_name) || return 1
  1350. relation_dir="$VARDIR/relations/$project/$charm-$target_charm/$relation_name"
  1351. if ! [ -d "$relation_dir" ]; then
  1352. mkdir -p "$relation_dir" || return 1
  1353. chmod go-rwx "$relation_dir" || return 1 ## protecting this directory
  1354. fi
  1355. echo "$relation_dir" || tee "$cache_file"
  1356. }
  1357. export -f get_relation_data_dir
  1358. get_relation_data_file() {
  1359. local charm="$1" target_charm="$2" relation_name="$3" relation_config="$4"
  1360. relation_dir=$(get_relation_data_dir "$charm" "$target_charm" "$relation_name") || return 1
  1361. relation_data_file="$relation_dir/data"
  1362. new=
  1363. if [ -e "$relation_data_file" ]; then
  1364. ## Has reference changed ?
  1365. new_md5=$(echo "$relation_config" | md5_compat)
  1366. if [ "$new_md5" != "$(cat "$relation_data_file.md5_ref" 2>/dev/null)" ]; then
  1367. new=true
  1368. fi
  1369. else
  1370. new=true
  1371. fi
  1372. if [ "$new" ]; then
  1373. echo "$relation_config" > "$relation_data_file"
  1374. chmod go-rwx "$relation_data_file" ## protecting this file
  1375. echo "$relation_config" | md5_compat > "$relation_data_file.md5_ref"
  1376. fi
  1377. echo "$relation_data_file"
  1378. }
  1379. export -f get_relation_data_file
  1380. has_service_action () {
  1381. local service="$1" action="$2" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  1382. charm target_charm
  1383. if [ -e "$cache_file" ]; then
  1384. # debug "$FUNCNAME: cache hit ($*)"
  1385. cat "$cache_file"
  1386. return 0
  1387. fi
  1388. charm=$(get_service_charm "$service") || return 1
  1389. ## Action directly provided ?
  1390. if charm.has_direct_action "$charm" "$action"; then
  1391. echo -en "direct\0$charm" | tee "$cache_file"
  1392. return 0
  1393. fi
  1394. ## Action provided by relation ?
  1395. while read-0 relation_name target_service relation_config tech_dep; do
  1396. target_charm=$(get_service_charm "$target_service") || return 1
  1397. if charm.has_relation_action "$target_charm" "$relation_name" "$action"; then
  1398. echo -en "relation\0$charm\0$target_charm\0$relation_name\0$relation_config" | tee "$cache_file"
  1399. return 0
  1400. fi
  1401. done < <(get_compose_relations "$service")
  1402. return 1
  1403. # master=$(get_top_master_charm_for_service "$charm")
  1404. # [ "$master" == "$charm" ] && return 1
  1405. # has_service_action "$master" "$action"
  1406. }
  1407. export -f has_service_action
  1408. run_service_action () {
  1409. local service="$1" action="$2"
  1410. shift ; shift
  1411. {
  1412. if ! read-0 action_type; then
  1413. info "Service $DARKYELLOW$service$NORMAL does not have any action $DARKCYAN$action$NORMAL defined."
  1414. info " Add an executable script to 'actions/$action' to implement action."
  1415. return 1
  1416. fi
  1417. Section "running $DARKYELLOW$service$NORMAL/$DARKCYAN$action$NORMAL ($action_type)"; Feed
  1418. "_run_service_action_${action_type}" "$service" "$action" "$@"
  1419. } < <(has_service_action "$service" "$action")
  1420. }
  1421. export -f run_service_action
  1422. get_compose_relation_config() {
  1423. local service=$1 relation_config cache_file="$state_tmpdir/$FUNCNAME.cache.$1"
  1424. if [ -e "$cache_file" ]; then
  1425. # debug "$FUNCNAME: cache hit ($*)"
  1426. cat "$cache_file"
  1427. return 0
  1428. fi
  1429. compose_service_def=$(get_compose_service_def "$service") || return 1
  1430. echo "$compose_service_def" | shyaml get-value "relations" 2>/dev/null | tee "$cache_file"
  1431. }
  1432. export -f get_compose_relation_config
  1433. # ## Return key-values-0
  1434. # get_compose_relation_config_for_service() {
  1435. # local service=$1 relation_name=$2 relation_config
  1436. # compose_service_relations=$(get_compose_relation_config "$service") || return 1
  1437. # if ! relation_config=$(
  1438. # echo "$compose_service_relations" |
  1439. # shyaml get-value "${relation_name}" 2>/dev/null); then
  1440. # err "Couldn't find $DARKYELLOW${service}$NORMAL/${WHITE}${relation_name}$NORMAL" \
  1441. # "relation config in compose configuration."
  1442. # return 1
  1443. # fi
  1444. # if [ -z "$relation_config" ]; then
  1445. # err "Relation ${WHITE}mysql-database$NORMAL is empty in compose configuration."
  1446. # return 1
  1447. # fi
  1448. # if ! echo "$relation_config" | shyaml key-values-0 2>/dev/null; then
  1449. # err "No key/values in ${DARKBLUE}mysql-database$NORMAL of compose config."
  1450. # return 1
  1451. # fi
  1452. # }
  1453. # export -f get_compose_relation_config_for_service
  1454. _get_master_charm_for_service_cached () {
  1455. local service="$1" charm="$2" metadata="$3" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  1456. charm requires master_charm target_charm target_service service_def
  1457. if [ -e "$cache_file" ]; then
  1458. # debug "$FUNCNAME: STATIC cache hit ($1)"
  1459. cat "$cache_file"
  1460. touch "$cache_file"
  1461. return 0
  1462. fi
  1463. if [ "$(echo "$metadata" | shyaml get-value "subordinate" 2>/dev/null)" != "True" ]; then
  1464. ## just return charm name
  1465. echo "$charm" | tee "$cache_file"
  1466. return 0
  1467. fi
  1468. ## fetch the container relation
  1469. requires="$(echo "$metadata" | shyaml get-value "requires" 2>/dev/null)"
  1470. if [ -z "$requires" ]; then
  1471. die "Charm $DARKYELLOW$charm$NORMAL is a subordinate but does not have any 'requires' " \
  1472. "section."
  1473. fi
  1474. master_charm=
  1475. while read-0 relation_name relation; do
  1476. if [ "$(echo "$relation" | shyaml get-value "scope" 2>/dev/null)" == "container" ]; then
  1477. # debug "$DARKYELLOW$service$NORMAL's relation" \
  1478. # "$DARKBLUE${relation_name}$NORMAL is a container."
  1479. interface="$(echo "$relation" | shyaml get-value "interface" 2>/dev/null)"
  1480. if [ -z "$interface" ]; then
  1481. err "No ${WHITE}$interface${NORMAL} set for relation $relation_name."
  1482. return 1
  1483. fi
  1484. ## Action provided by relation ?
  1485. target_service=
  1486. while read-0 relation_name candidate_target_service _relation_config _tech_dep; do
  1487. [ "$interface" == "$relation_name" ] && {
  1488. target_service="$candidate_target_service"
  1489. break
  1490. }
  1491. done < <(get_compose_relations "$service")
  1492. if [ -z "$target_service" ]; then
  1493. err "Couldn't find ${WHITE}relations.$interface${NORMAL} in" \
  1494. "${DARKYELLOW}$service$NORMAL compose definition."
  1495. return 1
  1496. fi
  1497. master_charm=$(get_service_charm "$target_service") || return 1
  1498. break
  1499. fi
  1500. done < <(echo "$requires" | shyaml key-values-0 2>/dev/null)
  1501. if [ -z "$master_charm" ]; then
  1502. die "Charm $DARKYELLOW$charm$NORMAL is a subordinate but does not have any relation with" \
  1503. " ${WHITE}scope${NORMAL} set to 'container'."
  1504. fi
  1505. echo "$master_charm" | tee "$cache_file"
  1506. }
  1507. export -f _get_master_charm_for_service_cached
  1508. get_master_charm_for_service() {
  1509. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  1510. charm metadata result
  1511. if [ -e "$cache_file" ]; then
  1512. # debug "$FUNCNAME: SESSION cache hit ($*)"
  1513. cat "$cache_file"
  1514. return 0
  1515. fi
  1516. charm=$(get_service_charm "$service") || return 1
  1517. metadata=$(charm.metadata "$charm") || {
  1518. err "Failed to access metadata of charm $DARKYELLOW$charm$NORMAL"
  1519. return 1
  1520. }
  1521. result=$(_get_master_charm_for_service_cached "$service" "$charm" "$metadata") || return 1
  1522. echo "$result" | tee "$cache_file"
  1523. }
  1524. export -f get_master_charm_for_service
  1525. get_top_master_charm_for_service() {
  1526. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  1527. current_service
  1528. if [ -e "$cache_file" ]; then
  1529. # debug "$FUNCNAME: cache hit ($*)"
  1530. cat "$cache_file"
  1531. return 0
  1532. fi
  1533. current_service="$service"
  1534. while true; do
  1535. master_service=$(get_master_charm_for_service "$current_service") || return 1
  1536. [ "$master_service" == "$current_service" ] && break
  1537. current_service="$master_service"
  1538. done
  1539. echo "$current_service" | tee "$cache_file"
  1540. return 0
  1541. }
  1542. export -f get_top_master_charm_for_service
  1543. ##
  1544. ## The result is a mixin that is not always a complete valid
  1545. ## docker-compose entry (thinking of subordinates). The result
  1546. ## will be merge with master charms.
  1547. _get_docker_compose_mixin_from_metadata_cached() {
  1548. local charm="$1" metadata="$2" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  1549. metadata_file metadata volumes docker_compose subordinate image
  1550. if [ -e "$cache_file" ]; then
  1551. #debug "$FUNCNAME: STATIC cache hit $1"
  1552. cat "$cache_file"
  1553. touch "$cache_file"
  1554. return 0
  1555. fi
  1556. mixin=
  1557. if [ "$metadata" ]; then
  1558. ## resources to volumes
  1559. volumes=$(
  1560. for resource_type in data config; do
  1561. while read-0 resource; do
  1562. eval "echo \" - \$${resource_type^^}STORE/\$charm\$resource:\$resource:rw\""
  1563. done < <(echo "$metadata" | shyaml get-values-0 "${resource_type}-resources" 2>/dev/null)
  1564. done
  1565. while read-0 resource; do
  1566. if [[ "$resource" == *:* ]]; then
  1567. echo " - $resource:rw"
  1568. else
  1569. echo " - $resource:$resource:rw"
  1570. fi
  1571. done < <(echo "$metadata" | shyaml get-values-0 "host-resources" 2>/dev/null)
  1572. while read-0 resource; do
  1573. dest="$(charm.get_dir "$charm")/resources$resource"
  1574. if ! [ -e "$dest" ]; then
  1575. die "charm-resource: '$resource' does not exist (file: '$dest')."
  1576. fi
  1577. echo " - $dest:$resource:rw"
  1578. done < <(echo "$metadata" | shyaml get-values-0 "charm-resources" 2>/dev/null)
  1579. ) || return 1
  1580. if [ "$volumes" ]; then
  1581. mixin=$(echo -en "volumes:\n$volumes")
  1582. fi
  1583. docker_compose=$(echo "$metadata" | shyaml get-value "docker-compose" 2>/dev/null)
  1584. if [ "$docker_compose" ]; then
  1585. mixin=$(merge_yaml_str "$mixin" "$docker_compose")
  1586. fi
  1587. if [ "$(echo "$metadata" | shyaml get-value "subordinate" 2>/dev/null)" == "True" ]; then
  1588. subordinate=true
  1589. fi
  1590. fi
  1591. image=$(echo "$metadata" | shyaml get-value "docker-image" 2>/dev/null)
  1592. image_or_build_statement=
  1593. if [ "$image" ]; then
  1594. if [ "$subordinate" ]; then
  1595. err "Subordinate charm can not have a ${WHITE}docker-image${NORMAL} value."
  1596. return 1
  1597. fi
  1598. image_or_build_statement="image: $image"
  1599. elif [ -d "$(charm.get_dir "$charm")/build" ]; then
  1600. if [ "$subordinate" ]; then
  1601. err "Subordinate charm can not have a 'build' sub directory."
  1602. return 1
  1603. fi
  1604. image_or_build_statement="build: $charm/build"
  1605. fi
  1606. if [ "$image_or_build_statement" ]; then
  1607. mixin=$(merge_yaml_str "$mixin" "$image_or_build_statement")
  1608. fi
  1609. echo "$mixin" | tee "$cache_file"
  1610. }
  1611. export -f _get_docker_compose_mixin_from_metadata_cached
  1612. get_docker_compose_mixin_from_metadata() {
  1613. local charm="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1"
  1614. if [ -e "$cache_file" ]; then
  1615. #debug "$FUNCNAME: SESSION cache hit ($*)"
  1616. cat "$cache_file"
  1617. return 0
  1618. fi
  1619. metadata="$(charm.metadata "$charm")" || return 1
  1620. mixin=$(_get_docker_compose_mixin_from_metadata_cached "$charm" "$metadata") || return 1
  1621. echo "$mixin" | tee "$cache_file"
  1622. }
  1623. export -f get_docker_compose_mixin_from_metadata
  1624. _save() {
  1625. local name="$1"
  1626. cat - | tee -a "$docker_compose_dir/.data/$name"
  1627. }
  1628. export -f _save
  1629. get_default_project_name() {
  1630. if [ "$DEFAULT_PROJECT_NAME" ]; then
  1631. echo "$DEFAULT_PROJECT_NAME"
  1632. return 0
  1633. fi
  1634. compose_yml_location="$(get_compose_yml_location)" || return 1
  1635. if [ "$compose_yml_location" ]; then
  1636. if normalized_path=$(readlink -e "$compose_yml_location"); then
  1637. echo "$(basename "$(dirname "$normalized_path")")"
  1638. return 0
  1639. fi
  1640. fi
  1641. echo "project"
  1642. return 0
  1643. }
  1644. export -f get_default_project_name
  1645. launch_docker_compose() {
  1646. local charm
  1647. docker_compose_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
  1648. #debug "Creating temporary docker-compose directory in '$docker_compose_tmpdir'."
  1649. # trap_add EXIT "debug \"Removing temporary docker-compose directory in $docker_compose_tmpdir.\";\
  1650. # rm -rf \"$docker_compose_tmpdir\""
  1651. trap_add EXIT "rm -rf \"$docker_compose_tmpdir\""
  1652. project=$(get_default_project_name)
  1653. mkdir -p "$docker_compose_tmpdir/$project"
  1654. docker_compose_dir="$docker_compose_tmpdir/$project"
  1655. if [ -z "$SERVICE_PACK" ]; then
  1656. export SERVICE_PACK=$(get_default_target_services $SERVICE_PACK)
  1657. fi
  1658. get_docker_compose $SERVICE_PACK > "$docker_compose_dir/docker-compose.yml" || return 1
  1659. if [ -e "$state_tmpdir/to-merge-in-docker-compose.yml" ]; then
  1660. # debug "Merging some config data in docker-compose.yml:"
  1661. # debug "$(cat $state_tmpdir/to-merge-in-docker-compose.yml)"
  1662. _config_merge "$docker_compose_dir/docker-compose.yml" "$state_tmpdir/to-merge-in-docker-compose.yml" || return 1
  1663. fi
  1664. if [ -z "$(echo $(cat "$docker_compose_dir/docker-compose.yml"))" ]; then
  1665. die "Generated 'docker-compose.yml' is unexpectedly empty."
  1666. fi
  1667. ## XXXvlab: could be more specific and only link the needed charms
  1668. for charm in $(shyaml keys < "$docker_compose_dir/docker-compose.yml"); do
  1669. if charm.exists "$charm"; then
  1670. ln -sf "$(charm.get_dir "$charm")" "$docker_compose_dir/$charm" || exit 1
  1671. fi
  1672. done
  1673. mkdir "$docker_compose_dir/.data"
  1674. {
  1675. {
  1676. cd "$docker_compose_dir"
  1677. debug "${WHITE}docker-compose.yml$NORMAL for $DARKYELLOW$SERVICE_PACK$NORMAL:"
  1678. debug "$(cat "$docker_compose_dir/docker-compose.yml" | prefix " $GRAY|$NORMAL ")"
  1679. debug "${WHITE}Launching$NORMAL: docker-compose $@"
  1680. docker-compose "$@"
  1681. echo "$?" > "$docker_compose_dir/.data/errlvl"
  1682. } | _save stdout
  1683. } 3>&1 1>&2 2>&3 | _save stderr 3>&1 1>&2 2>&3
  1684. 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
  1685. err "Detected bug https://github.com/docker/docker/issues/4036 ... "
  1686. err "Please re-launch your command, or switch from 'devicemapper' driver to 'overlayfs' or 'aufs'."
  1687. fi
  1688. docker_compose_errlvl="$(cat "$docker_compose_dir/.data/errlvl" 2>/dev/null)"
  1689. if [ -z "$docker_compose_errlvl" ]; then
  1690. err "Something went wrong before you could gather docker-compose errorlevel."
  1691. return 1
  1692. fi
  1693. return "$docker_compose_errlvl"
  1694. }
  1695. export -f launch_docker_compose
  1696. get_compose_yml_location() {
  1697. parent=$(while ! [ -e "./compose.yml" ]; do
  1698. [ "$PWD" == "/" ] && exit 0
  1699. cd ..
  1700. done; echo "$PWD"
  1701. )
  1702. if [ "$parent" ]; then
  1703. echo "$parent/compose.yml"
  1704. return 0
  1705. fi
  1706. if [ "$DEFAULT_COMPOSE_FILE" ]; then
  1707. if ! [ -e "$DEFAULT_COMPOSE_FILE" ]; then
  1708. err "No 'compose.yml' was found in current or parent dirs," \
  1709. "and \$DEFAULT_COMPOSE_FILE points to an unexistent file."
  1710. die "Please provide a 'compose.yml' file."
  1711. fi
  1712. echo "$DEFAULT_COMPOSE_FILE"
  1713. return 0
  1714. fi
  1715. err "No 'compose.yml' was found in current or parent dirs, and no \$DEFAULT_COMPOSE_FILE was set."
  1716. die "Please provide a 'compose.yml' file."
  1717. return 1
  1718. }
  1719. export -f get_compose_yml_location
  1720. get_default_target_services() {
  1721. local services=("$@")
  1722. if [ -z "${services[*]}" ]; then
  1723. if [ "$DEFAULT_SERVICES" ]; then
  1724. debug "No service provided, using $WHITE\$DEFAULT_SERVICES$NORMAL variable." \
  1725. "Target services: $DARKYELLOW$DEFAULT_SERVICES$NORMAL"
  1726. services="$DEFAULT_SERVICES"
  1727. else
  1728. err "No service provided."
  1729. return 1
  1730. fi
  1731. fi
  1732. echo "${services[*]}"
  1733. }
  1734. export -f get_default_target_services
  1735. get_master_services() {
  1736. local loaded master_service
  1737. declare -A loaded
  1738. for service in "$@"; do
  1739. master_service=$(get_top_master_charm_for_service "$service") || return 1
  1740. if [ "${loaded[$master_service]}" ]; then
  1741. continue
  1742. fi
  1743. echo "$master_service"
  1744. loaded["$master_service"]=1
  1745. done | xargs echo
  1746. }
  1747. export -f get_master_services
  1748. _setup_state_dir() {
  1749. export state_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
  1750. #debug "Creating temporary state directory in '$state_tmpdir'."
  1751. # trap_add EXIT "debug \"Removing temporary state directory in $state_tmpdir.\";\
  1752. # rm -rf \"$state_tmpdir\""
  1753. trap_add EXIT "rm -rf \"$state_tmpdir\""
  1754. }
  1755. get_docker_compose_opts_list() {
  1756. local action="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$action"; cat "$(which docker-compose)" | md5_compat)"
  1757. if [ -e "$cache_file" ]; then
  1758. cat "$cache_file"
  1759. return 0
  1760. fi
  1761. docker_compose_help_msg=$(docker-compose "$action" --help 2>/dev/null) || return 1
  1762. echo "$docker_compose_help_msg" | grep '^Options:' -A 20000 |
  1763. tail -n +2 |
  1764. egrep "^\s+-" |
  1765. sed -r 's/\s+((((-[a-zA-Z]|--[a-zA-Z0-9-]+)( [A-Z=]+|=[^ ]+)?)(, )?)+)\s+.*$/\1/g' |
  1766. tee "$cache_file"
  1767. }
  1768. _MULTIOPTION_REGEX='^((-[a-zA-Z]|--[a-zA-Z0-9-]+)(, )?)+'
  1769. _MULTIOPTION_REGEX_LINE_FILTER=$_MULTIOPTION_REGEX'(\s|=)'
  1770. get_docker_compose_multi_opts_list() {
  1771. local action="$1" opts_list
  1772. opts_list=$(get_docker_compose_opts_list "$action") || return 1
  1773. echo "$opts_list" | egrep "$_MULTIOPTION_REGEX_LINE_FILTER" |
  1774. sed -r "s/^($_MULTIOPTION_REGEX)(\s|=).*$/\1/g" |
  1775. tr ',' "\n" | xargs echo
  1776. }
  1777. get_docker_compose_single_opts_list() {
  1778. local action="$1" opts_list
  1779. opts_list=$(get_docker_compose_opts_list "$action") || return 1
  1780. echo "$opts_list" | egrep -v "$_MULTIOPTION_REGEX_LINE_FILTER" |
  1781. tr ',' "\n" | xargs echo
  1782. }
  1783. clean_cache() {
  1784. local i=0
  1785. for f in $(ls -t "$CACHEDIR/"*.cache.* 2>/dev/null | tail -n +500); do
  1786. ((i++))
  1787. rm -f "$f"
  1788. done
  1789. if (( i > 0 )); then
  1790. debug "${WHITE}Cleaned cache:${NORMAL} Removed $((i)) elements (current cache size is $(du -sh "$CACHEDIR" | cut -f 1))"
  1791. fi
  1792. }
  1793. [ "$SOURCED" ] && return 0
  1794. if [ -z "$DISABLE_SYSTEM_CONFIG_FILE" ]; then
  1795. if [ -r /etc/default/charm ]; then
  1796. . /etc/default/charm
  1797. fi
  1798. if [ -r "/etc/default/$exname" ]; then
  1799. . "/etc/default/$exname"
  1800. fi
  1801. ## XXXvlab: should provide YML config opportunities in possible parent dirs ?
  1802. ## userdir ? and global /etc/compose.yml ?
  1803. for cfgfile in /etc/compose.conf /etc/compose.local.conf \
  1804. /etc/default/compose /etc/compose/local.conf; do
  1805. [ -e "$cfgfile" ] || continue
  1806. . "$cfgfile" || die "Loading config file '$cfgfile' failed."
  1807. done
  1808. fi
  1809. _setup_state_dir
  1810. mkdir -p "$CACHEDIR" || exit 1
  1811. DOCKER_HOST_NET=$(ip addr show docker0 | grep 'inet ' | xargs echo | cut -f 2 -d " ") || {
  1812. die "Couldn't determine docker host net"
  1813. }
  1814. DOCKER_HOST_IP=$(echo "$DOCKER_HOST_NET" | cut -f 1 -d "/") || {
  1815. die "Couldn't determine docker host ip"
  1816. }
  1817. export DOCKER_HOST_NET DOCKER_HOST_IP
  1818. ##
  1819. ## Argument parsing
  1820. ##
  1821. remainder_args=()
  1822. compose_opts=()
  1823. action_opts=()
  1824. no_hooks=
  1825. no_init=
  1826. action=
  1827. stage="main" ## switches from 'main', to 'action', 'remainder'
  1828. is_docker_compose_action=
  1829. DC_MATCH_MULTI=
  1830. DC_MATCH_SINGLE=
  1831. while [ "$#" != 0 ]; do
  1832. case "$stage" in
  1833. "main")
  1834. case "$1" in
  1835. --help|-h)
  1836. no_init=true ; no_hooks=true ; no_relations=true
  1837. print_help
  1838. exit 0
  1839. ;;
  1840. --verbose|-v)
  1841. export VERBOSE=true
  1842. ;;
  1843. --version|-V)
  1844. print_version
  1845. exit 0
  1846. ;;
  1847. -f)
  1848. [ -e "$2" ] || die "File $2 doesn't exists"
  1849. export DEFAULT_COMPOSE_FILE="$2"
  1850. shift
  1851. ;;
  1852. -p)
  1853. export DEFAULT_PROJECT_NAME="$2"
  1854. shift
  1855. ;;
  1856. --no-relations)
  1857. export no_relations=true
  1858. ;;
  1859. --no-hooks)
  1860. export no_hooks=true
  1861. ;;
  1862. --no-init)
  1863. export no_init=true
  1864. ;;
  1865. --debug)
  1866. export DEBUG=true
  1867. export VERBOSE=true
  1868. ;;
  1869. --*|-*)
  1870. compose_opts+=("$1")
  1871. ;;
  1872. *)
  1873. action="$1"
  1874. stage="action"
  1875. DC_MATCH_MULTI=$(get_docker_compose_multi_opts_list "$action") &&
  1876. DC_MATCH_SINGLE="$(get_docker_compose_single_opts_list "$action")"
  1877. if [ "$DC_MATCH_MULTI" ]; then
  1878. DC_MATCH_SINGLE="$DC_MATCH_SINGLE $(echo "$DC_MATCH_MULTI" | sed -r 's/( |$)/=\* /g')"
  1879. fi
  1880. is_docker_compose_action=true
  1881. ;;
  1882. esac
  1883. ;;
  1884. "action")
  1885. case "$1" in
  1886. --help|-h)
  1887. no_init=true ; no_hooks=true ; no_relations=true
  1888. action_opts+=("$1")
  1889. ;;
  1890. --*|-*)
  1891. if [ "$is_docker_compose_action" ]; then
  1892. if str_matches "$1" $DC_MATCH_MULTI; then
  1893. action_opts=("$1" "$2")
  1894. shift;
  1895. elif str_matches "$1" $DC_MATCH_SINGLE; then
  1896. action_opts=("$1")
  1897. else
  1898. err "Unknown option '$1'. Please check help."
  1899. docker-compose "$action" --help >&2
  1900. exit 1
  1901. fi
  1902. fi
  1903. ;;
  1904. *)
  1905. action_posargs+=("$1")
  1906. stage="remainder"
  1907. ;;
  1908. esac
  1909. ;;
  1910. "remainder")
  1911. remainder_args+=("$@")
  1912. break 3;;
  1913. esac
  1914. shift
  1915. done
  1916. [ "${compose_opts[*]}" ] && debug "Main docker-compose opts: ${compose_opts[*]}"
  1917. [ "${action_opts[*]}" ] && debug "Action $DARKCYAN$action$NORMAL with opts: ${action_opts[*]}"
  1918. [ "${remainder_args[*]}" ] && debug "Remainder args: ${remainder_args[*]}"
  1919. ##
  1920. ## Actual code
  1921. ##
  1922. export DOCKER_DATASTORE=${DOCKER_DATASTORE:-/srv/docker-datastore}
  1923. COMPOSE_YML_FILE=$(get_compose_yml_location) || exit 1
  1924. export COMPOSE_YML_FILE
  1925. debug "Found $WHITE$exname$NORMAL YAML file in '$COMPOSE_YML_FILE'."
  1926. output=$(shyaml get-value < "$COMPOSE_YML_FILE" 2>&1)
  1927. if [ "$?" != 0 ]; then
  1928. err "Invalid YAML in '$COMPOSE_YML_FILE':"
  1929. while IFS='' read -r line1 && IFS='' read -r line2; do
  1930. echo "$line1 $GRAY($line2)$NORMAL"
  1931. done < <(echo "$output" | grep ^yaml.scanner -A 100 |
  1932. sed -r 's/^ in "<stdin>", //g' | sed -r 's/^yaml.scanner.[a-zA-Z]+: //g') |
  1933. prefix " $GRAY|$NORMAL "
  1934. exit 1
  1935. fi
  1936. charm.sanity_checks || die "Sanity checks about charm-store failed. Please correct."
  1937. ##
  1938. ## Get services in command line.
  1939. ##
  1940. is_service_action=
  1941. case "$action" in
  1942. up|build|start|stop|config)
  1943. services="$(get_default_target_services "${action_posargs[@]}")" || exit 1
  1944. orig_services="${action_posargs[@]:1}"
  1945. ;;
  1946. run)
  1947. services="${action_posargs[0]}"
  1948. ;;
  1949. "")
  1950. services=
  1951. ;;
  1952. *)
  1953. if is_service_action=$(has_service_action "${action_posargs[0]}" "$action"); then
  1954. {
  1955. read-0 action_type
  1956. case "$action_type" in
  1957. "relation")
  1958. read-0 _ target_service relation_name
  1959. debug "Found action $DARKYELLOW${action_posargs[0]}$NORMAL/$DARKBLUE$relation_name$NORMAL/$DARKCYAN$action$NORMAL (in $DARKYELLOW$target_service$NORMAL)"
  1960. ;;
  1961. "direct")
  1962. debug "Found action $DARKYELLOW${action_posargs[0]}$NORMAL.$DARKCYAN$action$NORMAL"
  1963. ;;
  1964. esac
  1965. } < <(has_service_action "${action_posargs[0]}" "$action")
  1966. services="${action_posargs[0]}"
  1967. else
  1968. services="$(get_default_target_services "${action_posargs[@]}")"
  1969. fi
  1970. ;;
  1971. esac
  1972. get_docker_compose $services >/dev/null || { ## precalculate variable \$_current_docker_compose
  1973. err "Fails to compile base 'docker-compose.yml'"
  1974. exit 1
  1975. }
  1976. ##
  1977. ## Pre-action
  1978. ##
  1979. full_init=
  1980. case "$action" in
  1981. up|run)
  1982. full_init=true
  1983. ;;
  1984. "")
  1985. full_init=
  1986. ;;
  1987. *)
  1988. if [ "$is_service_action" ]; then
  1989. full_init=true
  1990. fi
  1991. ;;
  1992. esac
  1993. if [ "$full_init" ]; then
  1994. ## init in order
  1995. if [ -z "$no_init" ]; then
  1996. Section setup host resources
  1997. setup_host_resources "$services" || exit 1
  1998. Section initialisation
  1999. run_service_hook "$services" init || exit 1
  2000. fi
  2001. ## Get relations
  2002. if [ -z "$no_relations" ]; then
  2003. run_service_relations "$services" || exit 1
  2004. fi
  2005. ## XXXvlab: to be removed when all relation and service stuff is resolved
  2006. if [ -z "$no_hooks" ]; then
  2007. ordered_services=$(get_ordered_service_dependencies $services) || exit 1
  2008. for service in $ordered_services; do
  2009. charm=$(get_service_charm "$service") || exit 1
  2010. for script in "$CHARM_STORE/$charm/hooks.d/"*.sh; do
  2011. [ -e "$script" ] || continue
  2012. [ -x "$script" ] || { echo "compose: script $script is not executable." >&2; exit 1; }
  2013. (
  2014. debug "Launching '$script'."
  2015. cd "$(dirname "$script)")";
  2016. "$script" "$@"
  2017. ) || { echo "compose: hook $script failed. Stopping." >&2; exit 1; }
  2018. done
  2019. done
  2020. fi
  2021. fi
  2022. export SERVICE_PACK="$services"
  2023. ##
  2024. ## Docker-compose
  2025. ##
  2026. case "$action" in
  2027. up|start|stop|build)
  2028. master_services=$(get_master_services $SERVICE_PACK) || exit 1
  2029. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" $master_services
  2030. ;;
  2031. run)
  2032. master_service=$(get_master_services $SERVICE_PACK) || exit 1
  2033. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "$master_service" "${remainder_args[@]}"
  2034. ;;
  2035. # enter)
  2036. # master_service=$(get_master_services $SERVICE_PACK) || exit 1
  2037. # [ "${remainder_args[*]}" ] || remainder_args=("/bin/bash" "-c" "export TERM=xterm; exec bash")
  2038. # docker exec -ti "${action_opts[@]}" "$master_service" "${remainder_args[@]}"
  2039. # ;;
  2040. config)
  2041. ## removing the services
  2042. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "${remainder_args[@]}"
  2043. warn "Runtime configuration modification (from relations) are not included here."
  2044. ;;
  2045. *)
  2046. if [ "$is_service_action" ]; then
  2047. run_service_action "$SERVICE_PACK" "$action" "${remainder_args[@]}"
  2048. else
  2049. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "${remainder_args[@]}"
  2050. fi
  2051. ;;
  2052. esac