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.

3069 lines
103 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
  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. ## - Be clear about when the SERVICE name is used and the CHARM name is used.
  26. ## - in case of service contained in another container
  27. ## - in normal case
  28. ## - in docker-compose, can't use charm name: if we want 2 instances of the same charm
  29. ## we are stuck. What will be unique is the name of the service.
  30. ## - some relations are configured in compose.yml but should not trigger the loading
  31. ## of necessary component (for instance, apache --> log-rotate), if log-rotate is
  32. ## not there, this link should considered optional.
  33. ## - Could probably allow an unexistent charm to be populated with only "docker-image:"
  34. ## of the same name. Although this should trigger a visible warning.
  35. #:-
  36. [ -e /etc/shlib ] && . /etc/shlib || {
  37. echo "Unsatisfied dependency. Please install 'kal-shlib-core'."
  38. exit 1
  39. }
  40. #:-
  41. include common
  42. include pretty
  43. include parse
  44. include charm
  45. include array
  46. include cla
  47. depends shyaml docker
  48. version=0.1
  49. usage="$exname [COMPOSE_OPTS] [ACTION [ACTION_OPTS]]"
  50. help="\
  51. $WHITE$exname$NORMAL jobs is to run various shell scripts to build
  52. a running orchestrated and configured docker containers. These shell
  53. scripts will have the opportunity to build a 'docker-compose.yml'.
  54. Once init script and relations scripts are executed, $WHITE$exname$NORMAL
  55. delegate the launching to ${WHITE}docker-compose${NORMAL} by providing it
  56. the final 'docker-compose.yml'.
  57. $WHITE$exname$NORMAL also leverage charms to offer some additional custom
  58. actions per charm, which are simply other scripts that can be
  59. run without launching ${WHITE}docker-compose${NORMAL}.
  60. In compose message, color coding is enforced as such:
  61. - ${DARKCYAN}action$NORMAL,
  62. - ${DARKBLUE}relation$NORMAL,
  63. - ${DARKPINK}charm${NORMAL},
  64. - ${DARKYELLOW}service${NORMAL},
  65. - ${WHITE}option-name${NORMAL}/${WHITE}command-name${NORMAL}/${WHITE}Section-Title${NORMAL}
  66. $WHITE$exname$NORMAL reads '/etc/compose.conf' for global variables, and
  67. '/etc/compose.local.conf' for local host adjustements.
  68. "
  69. ## XXXvlab: this doesn't seem to work when 'compose' is called in
  70. ## a hook of a charm.
  71. #[[ "${BASH_SOURCE[0]}" == "" ]] && SOURCED=true
  72. $(return >/dev/null 2>&1) && SOURCED=true
  73. if [ "$UID" == 0 ]; then
  74. CACHEDIR=${CACHEDIR:-/var/cache/compose}
  75. VARDIR=${VARDIR:-/var/lib/compose}
  76. else
  77. [ "$XDG_CONFIG_HOME" ] && CACHEDIR=${CACHEDIR:-$XDG_CONFIG_HOME/compose}
  78. [ "$XDG_DATA_HOME" ] && VARDIR=${VARDIR:-$XDG_DATA_HOME/compose}
  79. CACHEDIR=${CACHEDIR:-$HOME/.cache/compose}
  80. VARDIR=${VARDIR:-$HOME/.local/share/compose}
  81. fi
  82. export VARDIR CACHEDIR
  83. md5_compat() { md5sum | cut -c -32; }
  84. quick_cat_file() { quick_cat_stdin < "$1"; }
  85. quick_cat_stdin() { local IFS=''; while read -r line; do echo "$line"; done ; }
  86. export -f quick_cat_file quick_cat_stdin md5_compat
  87. clean_cache() {
  88. local i=0
  89. for f in $(ls -t "$CACHEDIR/"*.cache.* 2>/dev/null | tail -n +500); do
  90. ((i++))
  91. rm -f "$f"
  92. done
  93. if (( i > 0 )); then
  94. debug "${WHITE}Cleaned cache:${NORMAL} Removed $((i)) elements (current cache size is $(du -sh "$CACHEDIR" | cut -f 1))"
  95. fi
  96. }
  97. usage="$exname SERVICE"'
  98. Deploy and manage a swarm of containers to provide services based on
  99. a ``compose.yml`` definition and charms from a ``charm-store``.
  100. '
  101. export DEFAULT_COMPOSE_FILE
  102. ##
  103. ## Merge YAML files
  104. ##
  105. export _merge_yaml_common_code="
  106. import sys
  107. import yaml
  108. try:
  109. # included in standard lib from Python 2.7
  110. from collections import OrderedDict
  111. except ImportError:
  112. # try importing the backported drop-in replacement
  113. # it's available on PyPI
  114. from ordereddict import OrderedDict
  115. ## Ensure that there are no collision with legacy OrderedDict
  116. ## that could be used for omap for instance.
  117. class MyOrderedDict(OrderedDict):
  118. pass
  119. yaml.add_representer(
  120. MyOrderedDict,
  121. lambda cls, data: cls.represent_dict(data.items()))
  122. yaml.add_constructor(
  123. yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
  124. lambda cls, node: MyOrderedDict(cls.construct_pairs(node)))
  125. def fc(filename):
  126. with open(filename) as f:
  127. return f.read()
  128. def merge(*args):
  129. # sys.stderr.write('%r\n' % (args, ))
  130. args = [arg for arg in args if arg is not None]
  131. if len(args) == 0:
  132. return None
  133. if len(args) == 1:
  134. return args[0]
  135. if all(isinstance(arg, (int, basestring, bool)) for arg in args):
  136. return args[-1]
  137. elif all(isinstance(arg, list) for arg in args):
  138. res = []
  139. for arg in args:
  140. for elt in arg:
  141. if elt in res:
  142. res.remove(elt)
  143. res.append(elt)
  144. return res
  145. elif all(isinstance(arg, dict) for arg in args):
  146. keys = set()
  147. for arg in args:
  148. keys |= set(arg.keys())
  149. dct = {}
  150. for key in keys:
  151. sub_args = []
  152. for arg in args:
  153. if key in arg:
  154. sub_args.append(arg)
  155. try:
  156. dct[key] = merge(*(a[key] for a in sub_args))
  157. except NotImplementedError as e:
  158. raise NotImplementedError(
  159. e.args[0],
  160. '%s.%s' % (key, e.args[1]) if e.args[1] else key,
  161. e.args[2])
  162. if dct[key] is None:
  163. del dct[key]
  164. return dct
  165. else:
  166. raise NotImplementedError(
  167. 'Unsupported types: %s'
  168. % (', '.join(list(set(arg.__class__.__name__ for arg in args)))), '', args)
  169. return None
  170. def merge_cli(*args):
  171. try:
  172. c = merge(*args)
  173. except NotImplementedError as e:
  174. sys.stderr.write('Merging Failed: %s.\n%s\n'
  175. ' Values are:\n %s\n'
  176. % (e.args[0],
  177. ' Conflicting key is %r.' % e.args[1] if e.args[1] else
  178. ' Conflict at base of structure.',
  179. '\\n '.join('v%d: %r' % (i, a)
  180. for i, a in enumerate(e.args[2]))))
  181. exit(1)
  182. if c is not None:
  183. print '%s' % yaml.dump(c, default_flow_style=False)
  184. "
  185. merge_yaml() {
  186. if ! [ -r "$state_tmpdir/merge_yaml.py" ]; then
  187. cat <<EOF > "$state_tmpdir/merge_yaml.py"
  188. $_merge_yaml_common_code
  189. merge_cli(*(yaml.load(fc(f)) for f in sys.argv[1:]))
  190. EOF
  191. fi
  192. python "$state_tmpdir/merge_yaml.py" "$@"
  193. }
  194. export -f merge_yaml
  195. merge_yaml_str() {
  196. local entries="$@"
  197. if ! [ -r "$state_tmpdir/merge_yaml_str.py" ]; then
  198. cat <<EOF > "$state_tmpdir/merge_yaml_str.py" || return 1
  199. $_merge_yaml_common_code
  200. merge_cli(*(yaml.load(f) for f in sys.argv[1:]))
  201. EOF
  202. fi
  203. python "$state_tmpdir/merge_yaml_str.py" "$@"
  204. }
  205. export -f merge_yaml_str
  206. yaml_key_val_str() {
  207. local entries="$@"
  208. if ! [ -r "$state_tmpdir/yaml_key_val_str.py" ]; then
  209. cat <<EOF > "$state_tmpdir/yaml_key_val_str.py"
  210. $_merge_yaml_common_code
  211. print '%s' % yaml.dump({
  212. yaml.load(sys.argv[1]):
  213. yaml.load(sys.argv[2])}, default_flow_style=False)
  214. EOF
  215. fi
  216. python "$state_tmpdir/yaml_key_val_str.py" "$@"
  217. }
  218. export -f yaml_key_val_str
  219. ##
  220. ## Docker
  221. ##
  222. docker_has_image() {
  223. local image="$1"
  224. images=$(docker images -q "$image" 2>/dev/null) || {
  225. err "docker images call has failed unexpectedly."
  226. return 1
  227. }
  228. [ "$images" ]
  229. }
  230. export -f docker_has_image
  231. cmd_on_base_image() {
  232. local service="$1" base_image
  233. shift
  234. base_image=$(service_base_docker_image "$service") || return 1
  235. docker run -i --entrypoint /bin/bash "$base_image" -c "$*"
  236. }
  237. export -f cmd_on_base_image
  238. cached_cmd_on_base_image() {
  239. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  240. shift
  241. if [ -e "$cache_file" ]; then
  242. # debug "$FUNCNAME: cache hit ($*)"
  243. quick_cat_stdin < "$cache_file"
  244. return 0
  245. fi
  246. result=$(cmd_on_base_image "$service" "$@") || return 1
  247. echo "$result" | tee "$cache_file"
  248. }
  249. export -f cached_cmd_on_base_image
  250. image_exposed_ports_0() {
  251. local image="$1"
  252. docker inspect --format='{{range $p, $conf := .Config.ExposedPorts}}{{$p}}{{"\x00"}}{{end}}' "$image"
  253. }
  254. export -f image_exposed_ports_0
  255. ##
  256. ## Generic
  257. ##
  258. fn.exists() {
  259. declare -F "$1" >/dev/null
  260. }
  261. str_pattern_matches() {
  262. local str="$1"
  263. shift
  264. for pattern in "$@"; do
  265. eval "[[ \"$str\" == $pattern ]]" && return 0
  266. done
  267. return 1
  268. }
  269. str_matches() {
  270. local str="$1"
  271. shift
  272. for pattern in "$@"; do
  273. [[ "$str" == "$pattern" ]] && return 0
  274. done
  275. return 1
  276. }
  277. gen_password() {
  278. local l=( {a..z} {A..Z} {0..9} ) nl="${#l[@]}" size=${1:-16}
  279. while ((size--)); do
  280. echo -n "${l[$((RANDOM * nl / 32768))]}"
  281. done
  282. echo
  283. }
  284. export -f gen_password
  285. file_put() {
  286. local TARGET="$1"
  287. mkdir -p "$(dirname "$TARGET")" &&
  288. cat - > "$TARGET"
  289. }
  290. export -f file_put
  291. file_put_0() {
  292. local TARGET="$1"
  293. mkdir -p "$(dirname "$TARGET")" &&
  294. cat > "$TARGET"
  295. }
  296. export -f file_put_0
  297. fetch_file() {
  298. local src="$1"
  299. case "$src" in
  300. *"://"*)
  301. err "Unsupported target scheme."
  302. return 1
  303. ;;
  304. *)
  305. ## Try direct
  306. if ! [ -r "$src" ]; then
  307. err "File '$src' not found/readable."
  308. return 1
  309. fi
  310. cat "$src" || return 1
  311. ;;
  312. esac
  313. }
  314. export -f fetch_file
  315. ## receives stdin content to decompress on stdout
  316. ## stdout content should be tar format.
  317. uncompress_file() {
  318. local filename="$1"
  319. ## Warning, the content of the file is already as stdin, the filename
  320. ## is there to hint for correct decompression.
  321. case "$filename" in
  322. *".gz")
  323. gunzip
  324. ;;
  325. *".bz2")
  326. bunzip2
  327. ;;
  328. *)
  329. cat
  330. ;;
  331. esac
  332. }
  333. export -f uncompress_file
  334. get_file() {
  335. local src="$1"
  336. fetch_file "$src" | uncompress_file "$src"
  337. }
  338. export -f get_file
  339. ##
  340. ## Common database lib
  341. ##
  342. _clean_docker() {
  343. local _DB_NAME="$1" container_id="$2"
  344. (
  345. set +e
  346. debug "Removing container $_DB_NAME"
  347. docker stop "$container_id"
  348. docker rm "$_DB_NAME"
  349. docker network rm "${_DB_NAME}"
  350. rm -vf "/tmp/${_DB_NAME}.state"
  351. )
  352. }
  353. export -f _clean_docker
  354. get_service_base_image_dir_uid_gid() {
  355. local service="$1" dir="$2" uid_gid
  356. uid_gid=$(cached_cmd_on_base_image "$service" "stat -c '%u %g' '$dir'") || {
  357. debug "Failed to query '$dir' uid in ${DARKYELLOW}$service${NORMAL} base image."
  358. return 1
  359. }
  360. info "uid and gid from ${DARKYELLOW}$service${NORMAL}:$dir is '$uid_gid'"
  361. echo "$uid_gid"
  362. }
  363. export -f get_service_base_image_dir_uid_gid
  364. are_files_locked_in_dir() {
  365. local dir="$1" device hdev ldev
  366. device=$(stat -c %d "$dir") || {
  367. err "Can't stat %d."
  368. return 1
  369. }
  370. device=$(printf "%04x" $device)
  371. hdev=${device:0:2}
  372. ldev=${device:2:2}
  373. inodes=$(find "$dir" -printf ':%i:\n')
  374. found=
  375. while read -r inode; do
  376. debug "try inode:$inode"
  377. if [[ "$inodes" == *":$inode:"* ]]; then
  378. found=1
  379. break
  380. fi
  381. done < <(cat /proc/locks | grep " $hdev:$ldev:" | sed -r "s/^.*$hdev:$ldev:([0-9]+).*$/\1/g")
  382. [ "$found" ]
  383. }
  384. export -f are_files_locked_in_dir
  385. export _PID="$$"
  386. ensure_db_docker_running () {
  387. local _STATE_FILE
  388. _DB_NAME="db_${DB_NAME}_${_PID}"
  389. _STATE_FILE=/tmp/${_DB_NAME}.state
  390. if [ -e "$_STATE_FILE" ]; then
  391. IFS=: read DOCKER_NETWORK DOCKER_IP <<<"$(cat "$_STATE_FILE")"
  392. debug "Re-using previous docker/connection '$DOCKER_IP'."
  393. _set_db_params "$DOCKER_IP" "$DOCKER_NETWORK"
  394. return 0
  395. fi
  396. if [ -e "/tmp/${_DB_NAME}.working" ]; then
  397. ## avoid recursive calls.
  398. if [ -z "$DOCKER_IP" ]; then
  399. err "Currently figuring up DOCKER_IP, please set it yourself before this call if needed."
  400. return 1
  401. else
  402. debug "ignoring recursive call of 'ensure_db_docker_running'."
  403. fi
  404. return 0
  405. fi
  406. touch "/tmp/${_DB_NAME}.working"
  407. docker rm "$_DB_NAME" 2>/dev/null || true
  408. host_db_working_dir="$DATASTORE/${SERVICE_NAME}$DB_DATADIR"
  409. if is_db_locked; then
  410. info "Some process is using '$host_db_working_dir'. Trying to find a docker that would do this..."
  411. found=
  412. for docker_id in $(docker ps -q); do
  413. has_volume_mounted=$(
  414. docker inspect \
  415. --format "{{range .Mounts}}{{if eq .Destination \"$DB_DATADIR\"}}{{.Source}}{{end}}{{end}}" \
  416. "$docker_id")
  417. if [ "$has_volume_mounted" == "$host_db_working_dir" ]; then
  418. found="$docker_id"
  419. break
  420. fi
  421. done
  422. if [ -z "$found" ]; then
  423. err "Please shutdown any other docker using this directory."
  424. return 1
  425. fi
  426. export container_id="$found"
  427. info "Found docker $docker_id is already running."
  428. else
  429. verb "Database is not locked."
  430. if ! docker_has_image "$DOCKER_BASE_IMAGE"; then
  431. docker pull "$DOCKER_BASE_IMAGE"
  432. fi
  433. docker_opts=
  434. if [ -f "$DB_PASSFILE" ]; then
  435. verb "Found and using '$DB_PASSFILE'."
  436. docker_opts="$db_docker_opts -v $SERVER_ROOT_PREFIX$DB_PASSFILE:$DB_PASSFILE"
  437. fi
  438. debug docker network create "$_DB_NAME"
  439. if ! network_id=$(docker network create "$_DB_NAME"); then
  440. err "'docker network create' failed !"
  441. _clean_docker "$_DB_NAME" "$container_id"
  442. rm "/tmp/${_DB_NAME}.working"
  443. return 1
  444. fi
  445. debug docker run -d \
  446. --name "$_DB_NAME" \
  447. $docker_opts \
  448. --network "$_DB_NAME" \
  449. -v "$host_db_working_dir:$DB_DATADIR" \
  450. "$DOCKER_BASE_IMAGE"
  451. if ! container_id=$(
  452. docker run -d \
  453. --name "$_DB_NAME" \
  454. $docker_opts \
  455. --network "$_DB_NAME" \
  456. -v "$host_db_working_dir:$DB_DATADIR" \
  457. "$DOCKER_BASE_IMAGE"
  458. ); then
  459. err "'docker run' failed !"
  460. _clean_docker "$_DB_NAME" "$container_id"
  461. rm "/tmp/${_DB_NAME}.working"
  462. return 1
  463. fi
  464. trap_add EXIT,ERR "_clean_docker \"$_DB_NAME\" \"$container_id\""
  465. fi
  466. if docker_ip=$(wait_for_docker_ip "$container_id"); then
  467. IFS=: read DOCKER_NETWORK DOCKER_IP <<<"$docker_ip"
  468. echo "$docker_ip" > "$_STATE_FILE"
  469. debug "written '$_STATE_FILE'"
  470. rm "/tmp/${_DB_NAME}.working"
  471. _set_db_params "$DOCKER_IP" "$DOCKER_NETWORK"
  472. return 0
  473. else
  474. errlvl="$?"
  475. err "Db not found (errlvl: $errlvl). Tail of docker logs follows:"
  476. docker logs --tail=5 "$container_id" 2>&1 | prefix " | " >&2
  477. rm "/tmp/${_DB_NAME}.working"
  478. return "$errlvl"
  479. fi
  480. }
  481. export -f ensure_db_docker_running
  482. ## Require to set $db_docker_opts if needed, and $DB_PASSFILE
  483. ##
  484. _dcmd() {
  485. local docker_opts command="$1"
  486. shift
  487. debug "Db> $command $@"
  488. if [ -f "$DB_PASSFILE" ]; then
  489. verb "Found and using '$DB_PASSFILE'."
  490. db_docker_opts="$db_docker_opts -v $SERVER_ROOT_PREFIX$DB_PASSFILE:$DB_PASSFILE"
  491. fi
  492. ## XXXX was here: actualy, we need only connection between this version and the client version
  493. debug docker run -i --rm \
  494. $db_docker_opts \
  495. --entrypoint "$command" "$DOCKER_BASE_IMAGE" $db_cmd_opts "$@"
  496. docker run -i --rm \
  497. $db_docker_opts \
  498. --entrypoint "$command" "$DOCKER_BASE_IMAGE" $db_cmd_opts "$@"
  499. }
  500. export -f _dcmd
  501. ## Executes code through db
  502. dcmd() {
  503. local fun
  504. [ "$DB_NAME" ] || print_syntax_error "$FUNCNAME: You must provide \$DB_NAME."
  505. [ "$DB_DATADIR" ] || print_syntax_error "$FUNCNAME: You must provide \$DB_DATADIR."
  506. # [ "$DB_PASSFILE" ] || print_syntax_error "$FUNCNAME: You must provide \$DB_PASSFILE."
  507. [ "$_PID" ] || print_syntax_error "$FUNCNAME: You must provide \$_PID."
  508. for fun in is_db_locked _set_db_params ddb; do
  509. [ "$(type -t "$fun")" == "function" ] ||
  510. print_syntax_error "$FUNCNAME: You must provide function '$fun'."
  511. done
  512. ensure_db_docker_running </dev/null || return 1
  513. _dcmd "$@"
  514. }
  515. export -f dcmd
  516. get_docker_ips() {
  517. local name="$1" ip format network_id
  518. if ! docker inspect --format='{{ .NetworkSettings.Networks }}' "$name" >/dev/null 2>&1; then
  519. echo "default:$(docker inspect --format='{{ .NetworkSettings.IPAdress }}' "$name" 2>/dev/null)"
  520. else
  521. format='{{range $name, $conf := .NetworkSettings.Networks}}{{$name}}{{"\x00"}}{{$conf.IPAddress}}{{"\x00"}}{{end}}'
  522. while read-0 network_id ip; do
  523. printf "%s:%s\n" "$network_id" "$ip"
  524. done < <(docker inspect --format="$format" "$name")
  525. fi
  526. }
  527. export -f get_docker_ips
  528. get_docker_ip() {
  529. local name="$1"
  530. get_docker_ips "$name"
  531. }
  532. export -f get_docker_ip
  533. wait_docker_ip() {
  534. local name="$1" timeout="${2:-15}" timeout_count=0 docker_ip=
  535. start=$SECONDS
  536. while [ -z "$docker_ip" ]; do
  537. sleep 0.5
  538. docker_ip=$(get_docker_ip "$name") && break
  539. elapsed=$((SECONDS - start))
  540. if ((elapsed > timeout)); then
  541. err "${RED}timeout error${NORMAL}(${timeout}s):" \
  542. "Could not find '$name' docker container's IP."
  543. return 1
  544. fi
  545. [ "$elapsed" == "$old_elapsed" ] ||
  546. verb "Waiting for docker $name... ($elapsed/$timeout)"
  547. old_elapsed="$elapsed"
  548. done
  549. verb "Found docker $name network and IP: $docker_ip"
  550. echo "$docker_ip"
  551. }
  552. export -f wait_docker_ip
  553. wait_for_tcp_port() {
  554. local host_port=$1 timeout=30 start=$SECONDS
  555. verb "Trying to connect to $host_port"
  556. while true; do
  557. timeout 1 bash -c "</dev/tcp/${host_port/://}" >/dev/null 2>&1 && break
  558. sleep 0.2
  559. if [ "$((SECONDS - start))" -gt "$timeout" ]; then
  560. err "${RED}timeout error${NORMAL}(${timeout}s):"\
  561. "Could not connect to $host_port."
  562. return 1
  563. fi
  564. done
  565. return 0
  566. }
  567. export -f wait_for_tcp_port
  568. ## Warning: requires a ``ddb`` matching current database to be checked
  569. wait_for_docker_ip() {
  570. local name=$1 DOCKER_IP= DOCKER_NETWORK= docker_ips= docker_ip=
  571. docker_ip=$(wait_docker_ip "$name" 5) || return 1
  572. IFS=: read DOCKER_NETWORK DOCKER_IP <<<"$docker_ip"
  573. if ! str_is_ipv4 "$DOCKER_IP"; then
  574. err "internal 'wait_docker_ip' did not return a valid IP. Returned IP is '$DOCKER_IP'."
  575. return 1
  576. fi
  577. _set_db_params "$DOCKER_IP" "$DOCKER_NETWORK"
  578. while read-0 port; do
  579. IFS="/" read port type <<<"$port"
  580. [ "$type" == "tcp" ] || continue
  581. wait_for_tcp_port "$DOCKER_IP:${port}" || return 17
  582. verb "Port $DOCKER_IP:${port} checked open."
  583. done < <(image_exposed_ports_0 "$container_id")
  584. ## Checking direct connection
  585. if ! echo "SELECT 1;" | ddb >/dev/null; then
  586. err "${RED}db connection error${NORMAL}:"\
  587. "Could not connect to db on $DOCKER_IP" \
  588. "container's IP. (IP up, TCP ports is(are) open)"
  589. return 18
  590. fi
  591. echo "${DOCKER_NETWORK}:${DOCKER_IP}"
  592. return 0
  593. }
  594. export -f wait_for_docker_ip
  595. docker_add_host_declaration() {
  596. local src_docker=$1 domain=$2 dst_docker=$3 dst_docker_ip= dst_docker_network
  597. dst_docker_ip=$(wait_docker_ip "$dst_docker") || exit 1
  598. IFS=: read dst_docker_ip dst_docker_network <<<"$dst_docker_ip"
  599. docker exec -i "$src_docker" bash <<EOF
  600. if cat /etc/hosts | grep -E "^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s+$domain\$" > /dev/null 2>&1; then
  601. sed -ri "s/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s+$domain\$/$dst_docker_ip $domain/g" /etc/hosts
  602. else
  603. echo "$dst_docker_ip $domain" >> /etc/hosts
  604. fi
  605. EOF
  606. }
  607. export -f docker_add_host_declaration
  608. get_running_containers_for_service() {
  609. local service="$1"
  610. docker ps --filter label="compose.service=$service" --format="{{.ID}}"
  611. }
  612. export -f get_running_containers_for_service
  613. ##
  614. ## Internal Process
  615. ##
  616. get_docker_compose_links() {
  617. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  618. deps master_service master_target_service _relation_name \
  619. target_service _relation_config tech_dep
  620. if [ -z "$service" ]; then
  621. print_syntax_error "$FUNCNAME: Please specify a service as first argument."
  622. return 1
  623. fi
  624. if [ -e "$cache_file" ]; then
  625. # debug "$FUNCNAME: cache hit ($*)"
  626. cat "$cache_file"
  627. return 0
  628. fi
  629. master_service=$(get_top_master_service_for_service "$service") || return 1
  630. ## XXXvlab: yuck, this make the assumption that next function is cached,
  631. ## and leverage the fact that the result is stored in a file. All this only
  632. ## to catch a failure of ``get_compose_relations``, that would go out silently.
  633. get_compose_relations "$service" >/dev/null || return 1 ## fetch cache and fail if necessary
  634. deps=()
  635. while read-0 _relation_name target_service _relation_config tech_dep; do
  636. master_target_service="$(get_top_master_service_for_service "$target_service")" || return 1
  637. [ "$master_service" == "$master_target_service" ] && continue
  638. if [ "$tech_dep" == "reversed" ]; then
  639. deps+=("$(echo -en "$master_target_service:\n links:\n - $master_service")")
  640. elif [ "$tech_dep" == "True" ]; then
  641. deps+=("$(echo -en "$master_service:\n links:\n - $master_target_service")")
  642. fi
  643. ## XXXvlab: an attempt to add depends_on, but this doesn't work well actually
  644. ## as there's a circular dependency issue. We don't really want the full feature
  645. ## of depends_on, but just to add it as targets when doing an 'up'
  646. # deps+=("$(echo -en "$master_service:\n depends_on:\n - $master_target_service")")
  647. done < <(get_compose_relations "$service")
  648. merge_yaml_str "${deps[@]}" | tee "$cache_file" || return 1
  649. if [ "${PIPESTATUS[0]}" != 0 ]; then
  650. rm "$cache_file"
  651. return 1
  652. fi
  653. }
  654. _get_docker_compose_opts() {
  655. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  656. compose_def master_service docker_compose_opts
  657. if [ -z "$service" ]; then
  658. print_syntax_error "$FUNCNAME: Please specify a service as first argument."
  659. return 1
  660. fi
  661. if [ -e "$cache_file" ]; then
  662. # debug "$FUNCNAME: cache hit ($*)"
  663. cat "$cache_file"
  664. return 0
  665. fi
  666. compose_def="$(get_compose_service_def "$service")" || return 1
  667. master_service="$(get_top_master_service_for_service "$service")"
  668. docker_compose_opts=$(echo "$compose_def" | shyaml get-value "docker-compose" 2>/dev/null)
  669. if [ "$docker_compose_opts" ]; then
  670. yaml_key_val_str "$master_service" "$docker_compose_opts"
  671. fi | tee "$cache_file"
  672. if [ "${PIPESTATUS[0]}" != 0 ]; then
  673. rm "$cache_file"
  674. return 1
  675. fi
  676. }
  677. ##
  678. ## By Reading the metadata.yml, we create a docker-compose.yml mixin.
  679. ## Some metadata.yml (of subordinates) will indeed modify other
  680. ## services than themselves.
  681. _get_docker_compose_service_mixin() {
  682. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  683. links_yaml base_mixin links_yaml docker_compose_options \
  684. charm charm_part
  685. if [ -z "$service" ]; then
  686. print_syntax_error "$FUNCNAME: Please specify a service as first argument."
  687. return 1
  688. fi
  689. if [ -e "$cache_file" ]; then
  690. # debug "$FUNCNAME: cache hit ($*)"
  691. cat "$cache_file"
  692. return 0
  693. fi
  694. master_service=$(get_top_master_service_for_service "$service") || {
  695. err "Failed to get top master service for service $DARKYELLOW$service$NORMAL"
  696. return 1
  697. }
  698. ## The compose part
  699. base_mixin="$master_service:
  700. labels:
  701. - compose.service=$service
  702. - compose.master-service=${master_service}
  703. - compose.project=$(get_default_project_name)"
  704. links_yaml=$(get_docker_compose_links "$service") || return 1
  705. docker_compose_options=$(_get_docker_compose_opts "$service") || return 1
  706. ## the charm part
  707. charm_part=$(get_docker_compose_mixin_from_metadata "$service") || return 1
  708. ## Merge results
  709. if [ "$charm_part" ]; then
  710. charm_yaml="$(yaml_key_val_str "$master_service" "$charm_part")" || return 1
  711. merge_yaml_str "$base_mixin" "$links_yaml" "$charm_yaml" "$docker_compose_options" || return 1
  712. else
  713. merge_yaml_str "$base_mixin" "$links_yaml" "$docker_compose_options" || return 1
  714. fi | tee "$cache_file"
  715. if [ "${PIPESTATUS[0]}" != 0 ]; then
  716. rm "$cache_file"
  717. return 1
  718. fi
  719. }
  720. export -f _get_docker_compose_service_mixin
  721. ##
  722. ## Get full `docker-compose.yml` format for all listed services (and
  723. ## their deps)
  724. ##
  725. ## @export
  726. ## @cache: !system !nofail +stdout
  727. get_docker_compose () {
  728. local cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  729. entries services service start docker_compose_services
  730. if [ -e "$cache_file" ]; then
  731. # debug "$FUNCNAME: cache hit ($*)"
  732. cat "$cache_file"
  733. return 0
  734. fi
  735. ##
  736. ## Adding sub services configurations
  737. ##
  738. declare -A entries
  739. start_compilation=$SECONDS
  740. debug "Compiling 'docker-compose.yml' base for ${DARKYELLOW}$*$NORMAL..."
  741. for target_service in "$@"; do
  742. start=$SECONDS
  743. services=$(get_ordered_service_dependencies "$target_service") || {
  744. err "Failed to get dependencies for $DARKYELLOW$target_service$NORMAL"
  745. return 1
  746. }
  747. debug " $DARKYELLOW$target_service$NORMAL deps:$DARKYELLOW" $services "$NORMAL$GRAY(in $((SECONDS - start))s)$NORMAL"
  748. for service in $services; do
  749. if [ "${entries[$service]}" ]; then
  750. ## Prevent double inclusion of same service if this
  751. ## service is deps of two or more of your
  752. ## requirements.
  753. continue
  754. fi
  755. ## mark the service as "loaded" as well as it's containers
  756. ## if this is a subordinate service
  757. start_service=$SECONDS
  758. entries[$service]=$(_get_docker_compose_service_mixin "$service") || {
  759. err "Failed to get service mixin for $DARKYELLOW$service$NORMAL"
  760. return 1
  761. }
  762. debug " Applied $DARKYELLOW$service$NORMAL charm metadata mixins $GRAY(in $((SECONDS - start_service))s)$NORMAL"
  763. done
  764. debug " ..finished all mixins for $DARKYELLOW$target_service$NORMAL $GRAY(in $((SECONDS - start))s)$NORMAL"
  765. done
  766. docker_compose_services=$(merge_yaml_str "${entries[@]}") || return 1
  767. base_v2="version: '2.0'
  768. networks:
  769. static:
  770. driver: bridge
  771. ipam:
  772. driver: default
  773. config:
  774. - subnet: 172.25.1.0/24
  775. "
  776. merge_yaml_str "$(yaml_key_val_str "services" "$docker_compose_services")" \
  777. "$base_v2" > "$cache_file" || return 1
  778. export _current_docker_compose="$(cat "$cache_file")"
  779. echo "$_current_docker_compose"
  780. debug " ..compilation of base 'docker-compose.yml' done $GRAY(in $((SECONDS - start_compilation))s)$NORMAL" || true
  781. # debug " ** ${WHITE}docker-compose.yml${NORMAL}:"
  782. # debug "$_current_docker_compose"
  783. }
  784. export -f get_docker_compose
  785. _get_compose_service_def_cached () {
  786. local service="$1" docker_compose="$2" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  787. if [ -e "$cache_file" ]; then
  788. #debug "$FUNCNAME: STATIC cache hit"
  789. cat "$cache_file" &&
  790. touch "$cache_file" || return 1
  791. return 0
  792. fi
  793. value=$(echo "$docker_compose" | shyaml get-value "$service" 2>/dev/null)
  794. if ! echo "$value" | shyaml get-value "charm" >/dev/null 2>&1; then
  795. if charm.exists "$service"; then
  796. value=$(merge_yaml <(echo "charm: $service") <(echo "$value")) || return 1
  797. else
  798. err "No ${WHITE}charm${NORMAL} value for service $DARKYELLOW$service$NORMAL" \
  799. "in compose, nor same name charm found."
  800. return 1
  801. fi
  802. fi
  803. echo "$value" | tee "$cache_file" || return 1
  804. # if [ "${PIPESTATUS[0]}" != 0 ]; then
  805. # rm "$cache_file"
  806. # return 1
  807. # fi
  808. return 0
  809. # if [ "${PIPESTATUS[0]}" != 0 -o \! -s "$cache_file" ]; then
  810. # rm "$cache_file"
  811. # err "PAS OK $service: $value"
  812. # return 1
  813. # fi
  814. }
  815. export -f _get_compose_service_def_cached
  816. ## XXXvlab: a lot to be done to cache the results
  817. get_compose_service_def () {
  818. local service="$1" docker_compose cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  819. result
  820. if [ -e "$cache_file" ]; then
  821. #debug "$FUNCNAME: SESSION cache hit"
  822. cat "$cache_file" || return 1
  823. return 0
  824. fi
  825. [ -z "$service" ] && print_syntax_error "Missing service as first argument."
  826. docker_compose=$(get_compose_yml_content) || return 1
  827. result=$(_get_compose_service_def_cached "$service" "$docker_compose") || return 1
  828. echo "$result" | tee "$cache_file" || return 1
  829. }
  830. export -f get_compose_service_def
  831. _get_service_charm_cached () {
  832. local service="$1" service_def="$2" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  833. if [ -e "$cache_file" ]; then
  834. # debug "$FUNCNAME: cache hit $1"
  835. cat "$cache_file" &&
  836. touch "$cache_file" || return 1
  837. return 0
  838. fi
  839. charm=$(echo "$service_def" | shyaml get-value charm 2>/dev/null)
  840. if [ -z "$charm" ]; then
  841. err "Missing ${WHITE}charm${NORMAL} value in service $DARKYELLOW$service$NORMAL definition."
  842. return 1
  843. fi
  844. echo "$charm" | tee "$cache_file" || return 1
  845. }
  846. export -f _get_service_charm_cached
  847. get_service_charm () {
  848. local service="$1"
  849. if [ -z "$service" ]; then
  850. echo ${FUNCNAME[@]} >&2
  851. print_syntax_error "$FUNCNAME: Please specify a service as first argument."
  852. return 1
  853. fi
  854. service_def=$(get_compose_service_def "$service") || return 1
  855. _get_service_charm_cached "$service" "$service_def"
  856. }
  857. export -f get_service_charm
  858. ## built above the docker-compose abstraction, so it relies on the
  859. ## full docker-compose.yml to be already built.
  860. get_service_def () {
  861. local service="$1" def
  862. if [ -z "$_current_docker_compose" ]; then
  863. print_syntax_error "$FUNCNAME is meant to be called after"\
  864. "\$_current_docker_compose has been calculated."
  865. fi
  866. def=$(echo "$_current_docker_compose" | shyaml get-value "services.$service" 2>/dev/null)
  867. if [ -z "$def" ]; then
  868. err "No definition for service $DARKYELLOW$service$NORMAL in compiled 'docker-compose.yml'."
  869. return 1
  870. fi
  871. echo "$def"
  872. }
  873. export -f get_service_def
  874. ## Return the base docker image name of a service
  875. service_base_docker_image() {
  876. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  877. master_service service_def service_image service_build service_dockerfile
  878. if [ -e "$cache_file" ]; then
  879. # debug "$FUNCNAME: cache hit ($*)"
  880. cat "$cache_file"
  881. return 0
  882. fi
  883. master_service="$(get_top_master_service_for_service "$service")" || {
  884. err "Could not compute master service for service $DARKYELLOW$service$NORMAL."
  885. return 1
  886. }
  887. service_def="$(get_service_def "$master_service")" || {
  888. err "Could not get docker-compose service definition for $DARKYELLOW$master_service$NORMAL."
  889. return 1
  890. }
  891. service_image=$(echo "$service_def" | shyaml get-value image 2>/dev/null)
  892. if [ "$?" != 0 ]; then
  893. service_build=$(echo "$service_def" | shyaml get-value build 2>/dev/null)
  894. if [ "$?" != 0 ]; then
  895. err "Service $DARKYELLOW$service$NORMAL has no ${WHITE}image${NORMAL} nor ${WHITE}build${NORMAL} parameter."
  896. echo "$service_def" >&2
  897. return 1
  898. fi
  899. service_dockerfile="${service_build}"/Dockerfile
  900. if ! [ -e "$service_dockerfile" ]; then
  901. err "No Dockerfile found in '$service_dockerfile' location."
  902. return 1
  903. fi
  904. grep '^FROM' "$service_dockerfile" | xargs printf "%s " | cut -f 2 -d " "
  905. else
  906. echo "$service_image"
  907. fi | tee "$cache_file"
  908. }
  909. export -f service_base_docker_image
  910. get_charm_relation_def () {
  911. local charm="$1" relation_name="$2" cache_file="$state_tmpdir/$FUNCNAME.cache.$1.$2" \
  912. relation_def metadata
  913. if [ -e "$cache_file" ]; then
  914. # debug "$FUNCNAME: cache hit ($*)"
  915. cat "$cache_file"
  916. return 0
  917. fi
  918. metadata="$(charm.metadata "$charm")" || return 1
  919. relation_def="$(echo "$metadata" | shyaml get-value "provides.${relation_name}" 2>/dev/null)"
  920. echo "$relation_def" | tee "$cache_file"
  921. }
  922. export -f get_charm_relation_def
  923. get_charm_tech_dep_orientation_for_relation() {
  924. local charm="$1" relation_name="$2" cache_file="$state_tmpdir/$FUNCNAME.cache.$1.$2" \
  925. relation_def value
  926. if [ -e "$cache_file" ]; then
  927. # debug "$FUNCNAME: cache hit ($*)"
  928. cat "$cache_file"
  929. return 0
  930. fi
  931. relation_def=$(get_charm_relation_def "$charm" "$relation_name" 2>/dev/null)
  932. value=$(echo "$relation_def" | shyaml get-value 'tech-dep' 2>/dev/null)
  933. value=${value:-True}
  934. echo "$value" | tee "$cache_file"
  935. }
  936. export -f get_charm_tech_dep_orientation_for_relation
  937. ##
  938. ## Use compose file to get deps, and relation definition in metadata.yml
  939. ## for tech-dep attribute.
  940. get_service_deps() {
  941. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1"
  942. if [ -e "$cache_file" ]; then
  943. # debug "$FUNCNAME: cache hit ($*)"
  944. cat "$cache_file"
  945. return 0
  946. fi
  947. (
  948. set -o pipefail
  949. get_compose_relations "$service" | \
  950. while read-0 relation_name target_service _relation_config tech_dep; do
  951. echo "$target_service"
  952. done | tee "$cache_file"
  953. ) || return 1
  954. }
  955. export -f get_service_deps
  956. _rec_get_depth() {
  957. local elt=$1 dep deps max cache_file="$state_tmpdir/$FUNCNAME.cache.$1"
  958. [ "${depths[$elt]}" ] && return 0
  959. if [ -e "$cache_file" ]; then
  960. # debug "$FUNCNAME: cache hit ($*)"
  961. depths[$elt]=$(cat "$cache_file")
  962. return 0
  963. fi
  964. visited[$elt]=1
  965. #debug "Setting visited[$elt]"
  966. #debug "Asking for $DARKYELLOW$elt$NORMAL dependencies"
  967. deps=$(get_service_deps "$elt") || {
  968. debug "Failed get_service_deps $elt"
  969. return 1
  970. }
  971. # debug "$elt deps are:" $deps
  972. max=0
  973. for dep in $deps; do
  974. [ "${visited[$dep]}" ] && {
  975. #debug "Already computing $dep"
  976. continue
  977. }
  978. _rec_get_depth "$dep" || return 1
  979. #debug "Requesting depth[$dep]"
  980. if (( ${depths[$dep]} > max )); then
  981. max="${depths[$dep]}"
  982. fi
  983. done
  984. # debug "Setting depth[$elt] to $((max + 1))"
  985. depths[$elt]=$((max + 1))
  986. echo "${depths[$elt]}" > $cache_file
  987. }
  988. export -f _rec_get_depth
  989. get_ordered_service_dependencies() {
  990. local services=("$@") cache_file="$state_tmpdir/$FUNCNAME.cache.$(echo "$*" | md5_compat)"
  991. if [ -e "$cache_file" ]; then
  992. # debug "$FUNCNAME: cache hit ($*)"
  993. cat "$cache_file"
  994. return 0
  995. fi
  996. #debug "Figuring ordered deps of $DARKYELLOW$services$NORMAL"
  997. if [ -z "${services[*]}" ]; then
  998. return 0
  999. # print_syntax_error "$FUNCNAME: no arguments"
  1000. # return 1
  1001. fi
  1002. declare -A depths
  1003. declare -A visited
  1004. heads=("${services[@]}")
  1005. while [ "${#heads[@]}" != 0 ]; do
  1006. array_pop heads head
  1007. _rec_get_depth "$head" || return 1
  1008. done
  1009. i=0
  1010. while [ "${#depths[@]}" != 0 ]; do
  1011. for key in "${!depths[@]}"; do
  1012. value="${depths[$key]}"
  1013. if [ "$value" == "$i" ]; then
  1014. echo "$key"
  1015. unset depths[$key]
  1016. fi
  1017. done
  1018. i=$((i + 1))
  1019. done | tee "$cache_file"
  1020. }
  1021. export -f get_ordered_service_dependencies
  1022. run_service_hook () {
  1023. local action="$1" service subservice subservices loaded
  1024. shift
  1025. declare -A loaded
  1026. for service in "$@"; do
  1027. subservices=$(get_ordered_service_dependencies "$service") || return 1
  1028. for subservice in $subservices; do
  1029. if [ "${loaded[$subservice]}" ]; then
  1030. ## Prevent double inclusion of same service if this
  1031. ## service is deps of two or more of your
  1032. ## requirements.
  1033. continue
  1034. fi
  1035. charm=$(get_service_charm "$subservice") || return 1
  1036. charm.has_hook "$charm" "$action" >/dev/null || continue
  1037. PROJECT_NAME=$(get_default_project_name) || return 1
  1038. export MASTER_BASE_SERVICE_NAME=$(get_top_master_service_for_service "$subservice") || return 1
  1039. export MASTER_BASE_CHARM_NAME=$(get_service_charm "$MASTER_BASE_SERVICE_NAME") || return 1
  1040. Wrap -d "running $YELLOW$action$NORMAL hook of $DARKYELLOW$subservice$NORMAL in charm $DARKPINK$charm$NORMAL" <<EOF || return 1
  1041. export SERVICE_NAME=$subservice
  1042. export IMAGE_NAME=$(echo "${PROJECT_NAME}" | tr -d "_-")_\${SERVICE_NAME}
  1043. export CONTAINER_NAME=\${IMAGE_NAME}_1
  1044. export CHARM_NAME="$charm"
  1045. export PROJECT_NAME=$PROJECT_NAME
  1046. export DOCKER_BASE_IMAGE=$(service_base_docker_image "$MASTER_BASE_SERVICE_NAME")
  1047. export SERVICE_DATASTORE="$DATASTORE/$subservice"
  1048. export SERVICE_CONFIGSTORE="$CONFIGSTORE/$subservice"
  1049. charm.run_hook "$charm" "$action"
  1050. EOF
  1051. loaded[$subservice]=1
  1052. done
  1053. done
  1054. return 0
  1055. }
  1056. host_resource_get() {
  1057. local location="$1" cfg="$2"
  1058. type=$(echo "$cfg" | shyaml get-value type 2>/dev/null) || {
  1059. err "Missing ${WHITE}type$NORMAL option in ${WHITE}get$NORMAL config for location '$location'"
  1060. return 1
  1061. }
  1062. if fn.exists host_resource_get_$type; then
  1063. host_resource_get_$type "$location" "$cfg"
  1064. else
  1065. err "Source ${WHITE}source$NORMAL type '$type' unknown for" \
  1066. "${WHITE}host-resource$NORMAL '$location' defined in" \
  1067. "$DARKYELLOW$subservice$NORMAL config."
  1068. return 1
  1069. fi
  1070. }
  1071. export -f host_resource_get
  1072. host_resource_get_git() {
  1073. local location="$1" cfg="$2" branch parent url
  1074. branch=$(echo "$cfg" | shyaml get-value branch 2>/dev/null)
  1075. branch=${branch:-master}
  1076. url=$(echo "$cfg" | shyaml get-value url 2>/dev/null)
  1077. parent="$(dirname "$location")"
  1078. (
  1079. mkdir -p "$parent" && cd "$parent" &&
  1080. git clone -b "$branch" "$url" "$(basename "$location")"
  1081. ) || return 1
  1082. }
  1083. export -f host_resource_get_git
  1084. host_resource_get_git-sub() {
  1085. local location="$1" cfg="$2" branch parent url
  1086. branch=$(echo "$cfg" | shyaml get-value branch 2>/dev/null)
  1087. branch=${branch:-master}
  1088. url=$(echo "$cfg" | shyaml get-value url 2>/dev/null)
  1089. parent="$(dirname "$location")"
  1090. (
  1091. mkdir -p "$parent" && cd "$parent" &&
  1092. git sub clone -b "$branch" "$url" "$(basename "$location")"
  1093. ) || return 1
  1094. }
  1095. export -f host_resource_get_git-sub
  1096. setup_host_resource () {
  1097. local subservice="$1" service_def location get cfg
  1098. service_def=$(get_compose_service_def "$subservice") || return 1
  1099. while read-0 location cfg; do
  1100. ## XXXvlab: will it be a git resources always ?
  1101. if [ -d "$location" -a ! -d "$location/.git" ]; then
  1102. err "Hum, location '$location' does not seem to be a git directory."
  1103. return 1
  1104. fi
  1105. if [ -d "$location" ]; then
  1106. info "host resource '$location' already set up."
  1107. continue
  1108. fi
  1109. get=$(echo "$cfg" | shyaml get-value get 2>/dev/null)
  1110. if [ -z "$get" ]; then
  1111. err "No host directory '$location' found, and no ${WHITE}source$NORMAL" \
  1112. "specified for $DARKYELLOW$subservice$NORMAL."
  1113. return 1
  1114. fi
  1115. host_resource_get "$location" "$get" || return 1
  1116. done < <(echo "$service_def" | shyaml key-values-0 host-resources 2>/dev/null)
  1117. }
  1118. export -f setup_host_resource
  1119. setup_host_resources () {
  1120. local service subservices subservice loaded
  1121. declare -A loaded
  1122. for service in "$@"; do
  1123. subservices=$(get_ordered_service_dependencies "$service") || return 1
  1124. for subservice in $subservices; do
  1125. if [ "${loaded[$subservice]}" ]; then
  1126. ## Prevent double inclusion of same service if this
  1127. ## service is deps of two or more of your
  1128. ## requirements.
  1129. continue
  1130. fi
  1131. setup_host_resource "$service"
  1132. loaded[$subservice]=1
  1133. done
  1134. done
  1135. return 0
  1136. }
  1137. export -f setup_host_resources
  1138. relation-get () {
  1139. local key="$1"
  1140. cat "$RELATION_DATA_FILE" | shyaml get-value "$key" 2>/dev/null
  1141. if [ "$?" != 0 ]; then
  1142. err "The key $WHITE$key$NORMAL was not found in relation's data."
  1143. return 1
  1144. fi
  1145. }
  1146. export -f relation-get
  1147. relation-base-compose-get () {
  1148. local key="$1"
  1149. echo "$RELATION_BASE_COMPOSE_DEF" | shyaml get-value "options.$key" 2>/dev/null
  1150. if [ "$?" != 0 ]; then
  1151. err "The key $WHITE$key$NORMAL was not found in base service compose definition.."
  1152. return 1
  1153. fi
  1154. }
  1155. export -f relation-base-compose-get
  1156. relation-target-compose-get () {
  1157. local key="$1"
  1158. echo "$RELATION_BASE_COMPOSE_DEF" | shyaml get-value "options.$key" 2>/dev/null
  1159. if [ "$?" != 0 ]; then
  1160. err "The key $WHITE$key$NORMAL was not found in base service compose definition.."
  1161. return 1
  1162. fi
  1163. }
  1164. export -f relation-target-compose-get
  1165. relation-set () {
  1166. local key="$1" value="$2"
  1167. if [ -z "$RELATION_DATA_FILE" ]; then
  1168. err "$FUNCNAME: relation does not seems to be correctly setup."
  1169. return 1
  1170. fi
  1171. if ! [ -r "$RELATION_DATA_FILE" ]; then
  1172. err "$FUNCNAME: can't read relation's data." >&2
  1173. return 1
  1174. fi
  1175. _config_merge "$RELATION_DATA_FILE" <(echo "$key: $value")
  1176. }
  1177. export -f relation-set
  1178. _config_merge() {
  1179. local config_filename="$1" merge_to_file="$2"
  1180. touch "$config_filename" &&
  1181. merge_yaml "$config_filename" "$merge_to_file" > "$config_filename.tmp" || return 1
  1182. mv "$config_filename.tmp" "$config_filename"
  1183. }
  1184. export -f _config_merge
  1185. ## XXXvlab; this can be used only in relation, I'd like to use it in init.
  1186. config-add() {
  1187. local metadata="$1"
  1188. _config_merge "$RELATION_CONFIG" <(echo "$metadata")
  1189. }
  1190. export -f config-add
  1191. ## XXXvlab; this can be used only in relation, I'd like to use it in init.
  1192. init-config-add() {
  1193. local metadata="$1"
  1194. _config_merge "$state_tmpdir/to-merge-in-docker-compose.yml" \
  1195. <(yaml_key_val_str "services" "$metadata")
  1196. }
  1197. export -f init-config-add
  1198. logstdout() {
  1199. local name="$1"
  1200. sed -r 's%^%'"${name}"'> %g'
  1201. }
  1202. export -f logstdout
  1203. logstderr() {
  1204. local name="$1"
  1205. sed -r 's%^(.*)$%'"${RED}${name}>${NORMAL} \1"'%g'
  1206. }
  1207. export -f logstderr
  1208. _run_service_relation () {
  1209. local relation_name="$1" service="$2" target_service="$3" relation_config="$4" relation_dir services
  1210. charm=$(get_service_charm "$service") || return 1
  1211. target_charm=$(get_service_charm "$target_service") || return 1
  1212. base_script_name=$(charm.has_relation_hook "$charm" "$relation_name" relation-joined) || true
  1213. target_script_name=$(charm.has_relation_hook "$target_charm" "$relation_name" relation-joined) || true
  1214. [ "$base_script_name" -o "$target_script_name" ] || return 0
  1215. relation_dir=$(get_relation_data_dir "$service" "$target_service" "$relation_name") || return 1
  1216. RELATION_DATA_FILE=$(get_relation_data_file "$service" "$target_service" "$relation_name" "$relation_config") || return 1
  1217. export BASE_SERVICE_NAME=$service
  1218. export BASE_CHARM_NAME=$charm
  1219. export BASE_CHARM_PATH=$(charm.get_dir "$charm")
  1220. export TARGET_SERVICE_NAME=$target_service
  1221. export TARGET_CHARM_NAME=$target_charm
  1222. export TARGET_CHARM_PATH=$(charm.get_dir "$target_charm")
  1223. export RELATION_DATA_FILE
  1224. target_errlvl=0
  1225. if [ -z "$target_script_name" ]; then
  1226. verb "No relation script $DARKBLUE$relation_name$NORMAL in target $DARKPINK$target_charm$NORMAL."
  1227. else
  1228. verb "Running ${DARKBLUE}$relation_name${NORMAL} relation-joined script" \
  1229. "for target $DARKYELLOW$target_service$NORMAL (charm $DARKPINK$target_charm$NORMAL)"
  1230. RELATION_CONFIG="$relation_dir/config_provider"
  1231. DOCKER_BASE_IMAGE=$(service_base_docker_image "$target_service") || return 1
  1232. export DOCKER_BASE_IMAGE RELATION_CONFIG RELATION_DATA
  1233. {
  1234. (
  1235. SERVICE_NAME=$target_service
  1236. SERVICE_DATASTORE="$DATASTORE/$target_service"
  1237. SERVICE_CONFIGSTORE="$CONFIGSTORE/$target_service"
  1238. export SERVICE_NAME DOCKER_BASE_IMAGE SERVICE_DATASTORE SERVICE_CONFIGSTORE
  1239. charm.run_relation_hook "$target_charm" "$relation_name" relation-joined
  1240. echo "$?" > "$relation_dir/target_errlvl"
  1241. ) | logstdout "$DARKYELLOW$target_service$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${GREEN}@${NORMAL}"
  1242. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$target_service$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${RED}@${NORMAL}" 3>&1 1>&2 2>&3
  1243. target_errlvl="$(cat "$relation_dir/target_errlvl")" || {
  1244. err "Relation script '$script_name' in $DARKPINK$target_charm$NORMAL" \
  1245. "failed before outputing an errorlevel."
  1246. ((target_errlvl |= "1" ))
  1247. }
  1248. if [ -e "$RELATION_CONFIG" ]; then
  1249. debug "Merging some new config info in $DARKYELLOW$target_service$NORMAL"
  1250. _config_merge "$state_tmpdir/to-merge-in-docker-compose.yml" "$RELATION_CONFIG" &&
  1251. rm "$RELATION_CONFIG"
  1252. ((target_errlvl |= "$?"))
  1253. fi
  1254. fi
  1255. if [ "$target_errlvl" == 0 ]; then
  1256. errlvl=0
  1257. if [ "$base_script_name" ]; then
  1258. verb "Running ${DARKBLUE}$relation_name${NORMAL} relation-joined script" \
  1259. "for $DARKYELLOW$service$NORMAL (charm $DARKPINK$charm$NORMAL)"
  1260. RELATION_CONFIG="$relation_dir/config_providee"
  1261. RELATION_DATA="$(cat "$RELATION_DATA_FILE")"
  1262. DOCKER_BASE_IMAGE=$(service_base_docker_image "$service") || return 1
  1263. export DOCKER_BASE_IMAGE RELATION_CONFIG RELATION_DATA
  1264. {
  1265. (
  1266. SERVICE_NAME=$service
  1267. SERVICE_DATASTORE="$DATASTORE/$service"
  1268. SERVICE_CONFIGSTORE="$CONFIGSTORE/$service"
  1269. export SERVICE_NAME DOCKER_BASE_IMAGE SERVICE_DATASTORE SERVICE_CONFIGSTORE
  1270. charm.run_relation_hook "$charm" "$relation_name" relation-joined
  1271. echo "$?" > "$relation_dir/errlvl"
  1272. ) | logstdout "$DARKYELLOW$service$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${GREEN}@${NORMAL}"
  1273. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$service$NORMAL/$DARKBLUE$relation_name$NORMAL (joined) ${RED}@$NORMAL" 3>&1 1>&2 2>&3
  1274. errlvl="$(cat "$relation_dir/errlvl")" || {
  1275. err "Relation script '$script_name' in $DARKPINK$charm$NORMAL" \
  1276. "failed before outputing an errorlevel."
  1277. ((errlvl |= "1" ))
  1278. }
  1279. if [ -e "$RELATION_CONFIG" ]; then
  1280. _config_merge "$state_tmpdir/to-merge-in-docker-compose.yml" "$RELATION_CONFIG" &&
  1281. rm "$RELATION_CONFIG"
  1282. ((errlvl |= "$?" ))
  1283. fi
  1284. if [ "$errlvl" != 0 ]; then
  1285. err "Relation $DARKBLUE$relation_name$NORMAL on $DARKYELLOW$service$NORMAL failed to run properly."
  1286. fi
  1287. else
  1288. verb "No relation script '$script_name' in charm $DARKPINK$charm$NORMAL. Ignoring."
  1289. fi
  1290. else
  1291. err "Relation $DARKBLUE$relation_name$NORMAL on $DARKYELLOW$target_service$NORMAL failed to run properly."
  1292. fi
  1293. if [ "$target_errlvl" == 0 -a "$errlvl" == 0 ]; then
  1294. debug "Relation $DARKBLUE$relation_name$NORMAL is established" \
  1295. "between $DARKYELLOW$service$NORMAL and $DARKYELLOW$target_service$NORMAL."
  1296. return 0
  1297. else
  1298. return 1
  1299. fi
  1300. }
  1301. export -f _run_service_relation
  1302. _get_compose_relations_cached () {
  1303. local compose_service_def="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  1304. relation_name relation_def target_service
  1305. if [ -e "$cache_file" ]; then
  1306. #debug "$FUNCNAME: STATIC cache hit $1"
  1307. cat "$cache_file" &&
  1308. touch "$cache_file" || return 1
  1309. return 0
  1310. fi
  1311. (
  1312. set -o pipefail
  1313. if [ "$compose_service_def" ]; then
  1314. while read-0 relation_name relation_def; do
  1315. ## XXXvlab: could we use braces here instead of parenthesis ?
  1316. (
  1317. case "$(echo "$relation_def" | shyaml get-type 2>/dev/null)" in
  1318. "str")
  1319. target_service="$(echo "$relation_def" | shyaml get-value 2>/dev/null)"
  1320. target_charm=$(get_service_charm "$target_service") || return 1
  1321. tech_dep="$(get_charm_tech_dep_orientation_for_relation "$target_charm" "$relation_name")"
  1322. echo -en "$relation_name\0$target_service\0\0$tech_dep\0"
  1323. ;;
  1324. "sequence")
  1325. while read-0 target_service; do
  1326. target_charm=$(get_service_charm "$target_service") || return 1
  1327. tech_dep="$(get_charm_tech_dep_orientation_for_relation "$target_charm" "$relation_name")"
  1328. echo -en "$relation_name\0$target_service\0\0$tech_dep\0"
  1329. done < <(echo "$relation_def" | shyaml get-values-0 2>/dev/null)
  1330. ;;
  1331. "struct")
  1332. while read-0 target_service relation_config; do
  1333. target_charm=$(get_service_charm "$target_service") || return 1
  1334. tech_dep="$(get_charm_tech_dep_orientation_for_relation "$target_charm" "$relation_name")"
  1335. echo -en "$relation_name\0$target_service\0$relation_config\0$tech_dep\0"
  1336. done < <(echo "$relation_def" | shyaml key-values-0 2>/dev/null)
  1337. ;;
  1338. esac
  1339. ) </dev/null >> "$cache_file" || return 1
  1340. done < <(echo "$compose_service_def" | shyaml key-values-0 relations 2>/dev/null)
  1341. fi
  1342. )
  1343. if [ "$?" != 0 ]; then
  1344. err "Error while looking for compose relations."
  1345. rm -f "$cache_file" ## no cache
  1346. return 1
  1347. fi
  1348. [ -e "$cache_file" ] && cat "$cache_file"
  1349. return 0
  1350. }
  1351. export -f _get_compose_relations_cached
  1352. get_compose_relations () {
  1353. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  1354. compose_def
  1355. if [ -e "$cache_file" ]; then
  1356. #debug "$FUNCNAME: SESSION cache hit $1"
  1357. cat "$cache_file"
  1358. return 0
  1359. fi
  1360. compose_def="$(get_compose_service_def "$service")" || return 1
  1361. _get_compose_relations_cached "$compose_def" > "$cache_file"
  1362. if [ "$?" != 0 ]; then
  1363. rm -f "$cache_file" ## no cache
  1364. return 1
  1365. fi
  1366. cat "$cache_file"
  1367. }
  1368. export -f get_compose_relations
  1369. get_compose_relation_def() {
  1370. local service="$1" relation="$2" relation_name target_service relation_config tech_dep
  1371. while read-0 relation_name target_service relation_config tech_dep; do
  1372. [ "$relation_name" == "$relation" ] || continue
  1373. printf "%s\0%s\0%s\0" "$target_service" "$relation_config" "$tech_dep"
  1374. done < <(get_compose_relations "$service") || return 1
  1375. }
  1376. export -f get_compose_relation_def
  1377. run_service_relations () {
  1378. local service services loaded subservices subservice
  1379. PROJECT_NAME=$(get_default_project_name) || return 1
  1380. export PROJECT_NAME
  1381. declare -A loaded
  1382. subservices=$(get_ordered_service_dependencies "$@") || return 1
  1383. for service in $subservices; do
  1384. # debug "Upping dep's relations of ${DARKYELLOW}$service${NORMAL}:"
  1385. for subservice in $(get_service_deps "$service") "$service"; do
  1386. [ "${loaded[$subservice]}" ] && continue
  1387. export BASE_SERVICE_NAME=$service
  1388. MASTER_BASE_SERVICE_NAME=$(get_top_master_service_for_service "$subservice") || return 1
  1389. MASTER_BASE_CHARM_NAME=$(get_service_charm "$MASTER_BASE_SERVICE_NAME") || return 1
  1390. RELATION_BASE_COMPOSE_DEF=$(get_compose_service_def "$subservice") || return 1
  1391. export RELATION_BASE_COMPOSE_DEF MASTER_BASE_{CHARM,SERVICE}_NAME
  1392. # debug " Relations of ${DARKYELLOW}$subservice${NORMAL}:"
  1393. while read-0 relation_name target_service relation_config tech_dep; do
  1394. export relation_config
  1395. export TARGET_SERVICE_NAME=$target_service
  1396. MASTER_TARGET_SERVICE_NAME=$(get_top_master_service_for_service "$target_service") || return 1
  1397. MASTER_TARGET_CHARM_NAME=$(get_service_charm "$MASTER_TARGET_SERVICE_NAME") || return 1
  1398. RELATION_TARGET_COMPOSE_DEF=$(get_compose_service_def "$target_service") || return 1
  1399. export RELATION_TARGET_COMPOSE_DEF MASTER_TARGET_{CHARM,SERVICE}_NAME
  1400. Wrap -d "Building $DARKYELLOW$subservice$NORMAL --$DARKBLUE$relation_name$NORMAL--> $DARKYELLOW$target_service$NORMAL" <<EOF || return 1
  1401. _run_service_relation "$relation_name" "$subservice" "$target_service" "\$relation_config"
  1402. EOF
  1403. done < <(get_compose_relations "$subservice") || return 1
  1404. loaded[$subservice]=1
  1405. done
  1406. done
  1407. }
  1408. export -f run_service_relations
  1409. _run_service_action_direct() {
  1410. local service="$1" action="$2" charm _dummy
  1411. shift; shift
  1412. read-0 charm || true ## against 'set -e' that could be setup in parent scripts
  1413. if read-0 _dummy || [ "$_dummy" ]; then
  1414. print_syntax_error "$FUNCNAME: too many arguments in action descriptor"
  1415. return 1
  1416. fi
  1417. export action_errlvl_file="$state_tmpdir/action-$service-$charm-$action-errlvl"
  1418. export state_tmpdir
  1419. {
  1420. (
  1421. set +e ## Prevents unwanted leaks from parent shell
  1422. export COMPOSE_CONFIG=$(get_compose_yml_content)
  1423. export METADATA_CONFIG=$(charm.metadata "$charm")
  1424. export SERVICE_NAME=$service
  1425. export ACTION_NAME=$action
  1426. export CONTAINER_NAME=$(get_top_master_service_for_service "$service")
  1427. export DOCKER_BASE_IMAGE=$(service_base_docker_image "$CONTAINER_NAME")
  1428. export SERVICE_DATASTORE="$DATASTORE/$service"
  1429. export SERVICE_CONFIGSTORE="$CONFIGSTORE/$service"
  1430. exname="$exname $ACTION_NAME $SERVICE_NAME" \
  1431. stdbuf -oL -eL bash -c 'charm.run_direct_action "$@"' -- "$charm" "$action" "$@"
  1432. echo "$?" > "$action_errlvl_file"
  1433. ) | logstdout "$DARKYELLOW$service$NORMAL/${DARKCYAN}$action${NORMAL} ${GREEN}@${NORMAL}"
  1434. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$service$NORMAL/${DARKCYAN}$action${NORMAL} ${RED}@$NORMAL" 3>&1 1>&2 2>&3
  1435. if ! [ -e "$action_errlvl_file" ]; then
  1436. err "Action $DARKYELLOW$service$NORMAL:$DARKCYAN$action$NORMAL has failed without having time" \
  1437. "to output an errlvl"
  1438. return 1
  1439. fi
  1440. return "$(cat "$action_errlvl_file")"
  1441. }
  1442. export -f _run_service_action_direct
  1443. _run_service_action_relation() {
  1444. local service="$1" action="$2" charm target_charm relation_name relation_config _dummy
  1445. shift; shift
  1446. read-0 charm target_service target_charm relation_name relation_config || true
  1447. if read-0 _dummy || [ "$_dummy" ]; then
  1448. print_syntax_error "$FUNCNAME: too many arguments in action descriptor"
  1449. return 1
  1450. fi
  1451. export RELATION_DATA_FILE=$(get_relation_data_file "$service" "$target_service" "$relation_name" "$relation_config")
  1452. export action_errlvl_file="$state_tmpdir/action-$service-$charm-$action-errlvl"
  1453. export state_tmpdir
  1454. {
  1455. (
  1456. set +e ## Prevents unwanted leaks from parent shell
  1457. export METADATA_CONFIG=$(charm.metadata "$charm")
  1458. export SERVICE_NAME=$service
  1459. export RELATION_TARGET_SERVICE="$target_service"
  1460. export RELATION_TARGET_CHARM="$target_charm"
  1461. export RELATION_BASE_SERVICE="$service"
  1462. export RELATION_BASE_CHARM="$charm"
  1463. export ACTION_NAME=$action
  1464. export CONTAINER_NAME=$(get_top_master_service_for_service "$service")
  1465. export DOCKER_BASE_IMAGE=$(service_base_docker_image "$CONTAINER_NAME")
  1466. export SERVICE_DATASTORE="$DATASTORE/$service"
  1467. export SERVICE_CONFIGSTORE="$CONFIGSTORE/$service"
  1468. exname="$exname $ACTION_NAME $SERVICE_NAME" \
  1469. stdbuf -oL -eL bash -c 'charm.run_relation_action "$@"' -- "$target_charm" "$relation_name" "$action" "$@"
  1470. echo "$?" > "$action_errlvl_file"
  1471. ) | logstdout "$DARKYELLOW$service$NORMAL/${DARKCYAN}$action${NORMAL} ${GREEN}@${NORMAL}"
  1472. } 3>&1 1>&2 2>&3 | logstderr "$DARKYELLOW$service$NORMAL/${DARKCYAN}$action${NORMAL} ${RED}@$NORMAL" 3>&1 1>&2 2>&3
  1473. if ! [ -e "$action_errlvl_file" ]; then
  1474. err "Action $DARKYELLOW$service$NORMAL:$DARKCYAN$action$NORMAL has failed without having time" \
  1475. "to output an errlvl"
  1476. return 1
  1477. fi
  1478. return "$(cat "$action_errlvl_file")"
  1479. }
  1480. export -f _run_service_action_relation
  1481. get_relation_data_dir() {
  1482. local service="$1" target_service="$2" relation_name="$3" cache_file="$state_tmpdir/$FUNCNAME.cache.$1"
  1483. if [ -e "$cache_file" ]; then
  1484. # debug "$FUNCNAME: cache hit ($*)"
  1485. cat "$cache_file"
  1486. return 0
  1487. fi
  1488. project=$(get_default_project_name) || return 1
  1489. relation_dir="$VARDIR/relations/$project/${service}-${target_service}/$relation_name"
  1490. if ! [ -d "$relation_dir" ]; then
  1491. mkdir -p "$relation_dir" || return 1
  1492. chmod go-rwx "$relation_dir" || return 1 ## protecting this directory
  1493. fi
  1494. echo "$relation_dir" || tee "$cache_file"
  1495. }
  1496. export -f get_relation_data_dir
  1497. get_relation_data_file() {
  1498. local service="$1" target_service="$2" relation_name="$3" relation_config="$4"
  1499. relation_dir=$(get_relation_data_dir "$service" "$target_service" "$relation_name") || return 1
  1500. relation_data_file="$relation_dir/data"
  1501. new=
  1502. if [ -e "$relation_data_file" ]; then
  1503. ## Has reference changed ?
  1504. new_md5=$(echo "$relation_config" | md5_compat)
  1505. if [ "$new_md5" != "$(cat "$relation_data_file.md5_ref" 2>/dev/null)" ]; then
  1506. new=true
  1507. fi
  1508. else
  1509. new=true
  1510. fi
  1511. if [ "$new" ]; then
  1512. echo "$relation_config" > "$relation_data_file"
  1513. chmod go-rwx "$relation_data_file" ## protecting this file
  1514. echo "$relation_config" | md5_compat > "$relation_data_file.md5_ref"
  1515. fi
  1516. echo "$relation_data_file"
  1517. }
  1518. export -f get_relation_data_file
  1519. has_service_action () {
  1520. local service="$1" action="$2" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  1521. charm target_charm relation_name target_service relation_config _tech_dep
  1522. if [ -e "$cache_file" ]; then
  1523. # debug "$FUNCNAME: cache hit ($*)"
  1524. cat "$cache_file"
  1525. return 0
  1526. fi
  1527. charm=$(get_service_charm "$service") || return 1
  1528. ## Action directly provided ?
  1529. if charm.has_direct_action "$charm" "$action" >/dev/null; then
  1530. echo -en "direct\0$charm" | tee "$cache_file"
  1531. return 0
  1532. fi
  1533. ## Action provided by relation ?
  1534. while read-0 relation_name target_service relation_config _tech_dep; do
  1535. target_charm=$(get_service_charm "$target_service") || return 1
  1536. if charm.has_relation_action "$target_charm" "$relation_name" "$action" >/dev/null; then
  1537. echo -en "relation\0$charm\0$target_service\0$target_charm\0$relation_name\0$relation_config" | tee "$cache_file"
  1538. return 0
  1539. fi
  1540. done < <(get_compose_relations "$service")
  1541. return 1
  1542. # master=$(get_top_master_service_for_service "$service")
  1543. # [ "$master" == "$charm" ] && return 1
  1544. # has_service_action "$master" "$action"
  1545. }
  1546. export -f has_service_action
  1547. run_service_action () {
  1548. local service="$1" action="$2"
  1549. shift ; shift
  1550. {
  1551. if ! read-0 action_type; then
  1552. info "Service $DARKYELLOW$service$NORMAL does not have any action $DARKCYAN$action$NORMAL defined."
  1553. info " Add an executable script to 'actions/$action' to implement action."
  1554. return 1
  1555. fi
  1556. Section "running $DARKYELLOW$service$NORMAL/$DARKCYAN$action$NORMAL ($action_type)"; Feed
  1557. "_run_service_action_${action_type}" "$service" "$action" "$@"
  1558. } < <(has_service_action "$service" "$action")
  1559. }
  1560. export -f run_service_action
  1561. get_compose_relation_config() {
  1562. local service=$1 relation_config cache_file="$state_tmpdir/$FUNCNAME.cache.$1"
  1563. if [ -e "$cache_file" ]; then
  1564. # debug "$FUNCNAME: cache hit ($*)"
  1565. cat "$cache_file"
  1566. return 0
  1567. fi
  1568. compose_service_def=$(get_compose_service_def "$service") || return 1
  1569. echo "$compose_service_def" | shyaml get-value "relations" 2>/dev/null | tee "$cache_file"
  1570. }
  1571. export -f get_compose_relation_config
  1572. # ## Return key-values-0
  1573. # get_compose_relation_config_for_service() {
  1574. # local service=$1 relation_name=$2 relation_config
  1575. # compose_service_relations=$(get_compose_relation_config "$service") || return 1
  1576. # if ! relation_config=$(
  1577. # echo "$compose_service_relations" |
  1578. # shyaml get-value "${relation_name}" 2>/dev/null); then
  1579. # err "Couldn't find $DARKYELLOW${service}$NORMAL/${WHITE}${relation_name}$NORMAL" \
  1580. # "relation config in compose configuration."
  1581. # return 1
  1582. # fi
  1583. # if [ -z "$relation_config" ]; then
  1584. # err "Relation ${WHITE}mysql-database$NORMAL is empty in compose configuration."
  1585. # return 1
  1586. # fi
  1587. # if ! echo "$relation_config" | shyaml key-values-0 2>/dev/null; then
  1588. # err "No key/values in ${DARKBLUE}mysql-database$NORMAL of compose config."
  1589. # return 1
  1590. # fi
  1591. # }
  1592. # export -f get_compose_relation_config_for_service
  1593. _get_master_service_for_service_cached () {
  1594. local service="$1" charm="$2" metadata="$3" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  1595. charm requires master_charm target_charm target_service service_def found
  1596. if [ -e "$cache_file" ]; then
  1597. # debug "$FUNCNAME: STATIC cache hit ($1)"
  1598. cat "$cache_file" &&
  1599. touch "$cache_file" || return 1
  1600. return 0
  1601. fi
  1602. if [ "$(echo "$metadata" | shyaml get-value "subordinate" 2>/dev/null)" != "True" ]; then
  1603. ## just return service name
  1604. echo "$service" | tee "$cache_file"
  1605. return 0
  1606. fi
  1607. ## fetch the container relation
  1608. requires="$(echo "$metadata" | shyaml get-value "requires" 2>/dev/null)"
  1609. if [ -z "$requires" ]; then
  1610. die "Charm $DARKPINK$charm$NORMAL is a subordinate but does not have any 'requires' " \
  1611. "section."
  1612. fi
  1613. found=
  1614. while read-0 relation_name relation; do
  1615. [ "$(echo "$relation" | shyaml get-value "scope" 2>/dev/null)" == "container" ] && {
  1616. found=1
  1617. break
  1618. }
  1619. done < <(echo "$requires" | shyaml key-values-0 2>/dev/null)
  1620. if [ -z "$found" ]; then
  1621. die "Charm $DARKPINK$charm$NORMAL is a subordinate but does not have any required relation declaration with" \
  1622. " ${WHITE}scope${NORMAL} set to 'container'."
  1623. fi
  1624. interface="$(echo "$relation" | shyaml get-value "interface" 2>/dev/null)"
  1625. if [ -z "$interface" ]; then
  1626. err "No ${WHITE}interface${NORMAL} set for relation $DARKBLUE$relation_name$NORMAL."
  1627. return 1
  1628. fi
  1629. ## Action provided by relation ?
  1630. found=
  1631. while read-0 relation_name target_service _relation_config _tech_dep; do
  1632. [ "$interface" == "$relation_name" ] && {
  1633. found=1
  1634. break
  1635. }
  1636. done < <(get_compose_relations "$service")
  1637. if [ -z "$found" ]; then
  1638. err "Couldn't find ${WHITE}relations.$interface${NORMAL} in" \
  1639. "${DARKYELLOW}$service$NORMAL compose definition."
  1640. return 1
  1641. fi
  1642. echo "$target_service" | tee "$cache_file"
  1643. }
  1644. export -f _get_master_service_for_service_cached
  1645. get_master_service_for_service() {
  1646. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  1647. charm metadata result
  1648. if [ -e "$cache_file" ]; then
  1649. # debug "$FUNCNAME: SESSION cache hit ($*)"
  1650. cat "$cache_file" || return 1
  1651. return 0
  1652. fi
  1653. charm=$(get_service_charm "$service") || return 1
  1654. metadata=$(charm.metadata "$charm" 2>/dev/null) || {
  1655. metadata=""
  1656. warn "No charm $DARKPINK$charm$NORMAL found."
  1657. }
  1658. result=$(_get_master_service_for_service_cached "$service" "$charm" "$metadata") || return 1
  1659. echo "$result" | tee "$cache_file" || return 1
  1660. }
  1661. export -f get_master_service_for_service
  1662. get_top_master_service_for_service() {
  1663. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1" \
  1664. current_service
  1665. if [ -e "$cache_file" ]; then
  1666. # debug "$FUNCNAME: cache hit ($*)"
  1667. cat "$cache_file"
  1668. return 0
  1669. fi
  1670. current_service="$service"
  1671. while true; do
  1672. master_service=$(get_master_service_for_service "$current_service") || return 1
  1673. [ "$master_service" == "$current_service" ] && break
  1674. current_service="$master_service"
  1675. done
  1676. echo "$current_service" | tee "$cache_file"
  1677. return 0
  1678. }
  1679. export -f get_top_master_service_for_service
  1680. ##
  1681. ## The result is a mixin that is not always a complete valid
  1682. ## docker-compose entry (thinking of subordinates). The result
  1683. ## will be merge with master charms.
  1684. _get_docker_compose_mixin_from_metadata_cached() {
  1685. local service="$1" charm="$2" metadata="$3" has_build_dir="$4" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  1686. metadata_file metadata volumes docker_compose subordinate image
  1687. if [ -e "$cache_file" ]; then
  1688. #debug "$FUNCNAME: STATIC cache hit $1"
  1689. cat "$cache_file" &&
  1690. touch "$cache_file" || return 1
  1691. return 0
  1692. fi
  1693. mixin=$(echo -en "labels:\n- compose.charm=$charm")
  1694. if [ "$metadata" ]; then
  1695. ## resources to volumes
  1696. volumes=$(
  1697. for resource_type in data config; do
  1698. while read-0 resource; do
  1699. eval "echo \" - \$${resource_type^^}STORE/\$service\$resource:\$resource:rw\""
  1700. done < <(echo "$metadata" | shyaml get-values-0 "${resource_type}-resources" 2>/dev/null)
  1701. done
  1702. while read-0 resource; do
  1703. if [[ "$resource" == /*:/*:* ]]; then
  1704. echo " - $resource"
  1705. elif [[ "$resource" == /*:/* ]]; then
  1706. echo " - $resource:rw"
  1707. elif [[ "$resource" == /*:* ]]; then
  1708. echo " - ${resource%%:*}:$resource"
  1709. elif [[ "$resource" =~ ^/[^:]+$ ]]; then
  1710. echo " - $resource:$resource:rw"
  1711. else
  1712. die "Invalid host-resource specified in 'metadata.yml'."
  1713. fi
  1714. done < <(echo "$metadata" | shyaml get-values-0 "host-resources" 2>/dev/null)
  1715. while read-0 resource; do
  1716. dest="$(charm.get_dir "$charm")/resources$resource"
  1717. if ! [ -e "$dest" ]; then
  1718. die "charm-resource: '$resource' does not exist (file: '$dest')."
  1719. fi
  1720. echo " - $dest:$resource:ro"
  1721. done < <(echo "$metadata" | shyaml get-values-0 "charm-resources" 2>/dev/null)
  1722. ) || return 1
  1723. if [ "$volumes" ]; then
  1724. mixin=$(merge_yaml_str "$mixin" "$(echo -en "volumes:\n$volumes")") || {
  1725. err "Failed to merge mixin with ${WHITE}docker-compose${NORMAL} option" \
  1726. "from charm ${DARKPINK}$charm$NORMAL."
  1727. return 1
  1728. }
  1729. fi
  1730. docker_compose=$(echo "$metadata" | shyaml get-value -y "docker-compose" 2>/dev/null)
  1731. if [ "$docker_compose" ]; then
  1732. mixin=$(merge_yaml_str "$mixin" "$docker_compose") || {
  1733. err "Failed to merge mixin with ${WHITE}docker-compose${NORMAL} option" \
  1734. "from charm ${DARKPINK}$charm$NORMAL."
  1735. return 1
  1736. }
  1737. fi
  1738. if [ "$(echo "$metadata" | shyaml get-value "subordinate" 2>/dev/null)" == "True" ]; then
  1739. subordinate=true
  1740. fi
  1741. fi
  1742. image=$(echo "$metadata" | shyaml get-value "docker-image" 2>/dev/null)
  1743. image_or_build_statement=
  1744. if [ "$image" ]; then
  1745. if [ "$subordinate" ]; then
  1746. err "Subordinate charm can not have a ${WHITE}docker-image${NORMAL} value."
  1747. return 1
  1748. fi
  1749. image_or_build_statement="image: $image"
  1750. elif [ "$has_build_dir" ]; then
  1751. if [ "$subordinate" ]; then
  1752. err "Subordinate charm can not have a 'build' sub directory."
  1753. return 1
  1754. fi
  1755. image_or_build_statement="build: $(charm.get_dir "$charm")/build"
  1756. fi
  1757. if [ "$image_or_build_statement" ]; then
  1758. mixin=$(merge_yaml_str "$mixin" "$image_or_build_statement")
  1759. fi
  1760. echo "$mixin" | tee "$cache_file"
  1761. }
  1762. export -f _get_docker_compose_mixin_from_metadata_cached
  1763. get_docker_compose_mixin_from_metadata() {
  1764. local service="$1" cache_file="$state_tmpdir/$FUNCNAME.cache.$1"
  1765. if [ -e "$cache_file" ]; then
  1766. #debug "$FUNCNAME: SESSION cache hit ($*)"
  1767. cat "$cache_file"
  1768. return 0
  1769. fi
  1770. charm=$(get_service_charm "$service") || return 1
  1771. metadata="$(charm.metadata "$charm" 2>/dev/null)" || return 1
  1772. has_build_dir=
  1773. [ -d "$(charm.get_dir "$charm")/build" ] && has_build_dir=true
  1774. mixin=$(_get_docker_compose_mixin_from_metadata_cached "$service" "$charm" "$metadata" "$has_build_dir") || return 1
  1775. echo "$mixin" | tee "$cache_file"
  1776. }
  1777. export -f get_docker_compose_mixin_from_metadata
  1778. _save() {
  1779. local name="$1"
  1780. cat - | tee -a "$docker_compose_dir/.data/$name"
  1781. }
  1782. export -f _save
  1783. get_default_project_name() {
  1784. if [ "$DEFAULT_PROJECT_NAME" ]; then
  1785. echo "$DEFAULT_PROJECT_NAME"
  1786. return 0
  1787. fi
  1788. compose_yml_location="$(get_compose_yml_location)" || return 1
  1789. if [ "$compose_yml_location" ]; then
  1790. if normalized_path=$(readlink -e "$compose_yml_location"); then
  1791. echo "$(basename "$(dirname "$normalized_path")")"
  1792. return 0
  1793. fi
  1794. fi
  1795. echo "orphan"
  1796. return 0
  1797. }
  1798. export -f get_default_project_name
  1799. launch_docker_compose() {
  1800. local charm docker_compose_tmpdir docker_compose_dir
  1801. docker_compose_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
  1802. #debug "Creating temporary docker-compose directory in '$docker_compose_tmpdir'."
  1803. # trap_add EXIT "debug \"Removing temporary docker-compose directory in $docker_compose_tmpdir.\";\
  1804. # rm -rf \"$docker_compose_tmpdir\""
  1805. trap_add EXIT "rm -rf \"$docker_compose_tmpdir\""
  1806. project=$(get_default_project_name)
  1807. mkdir -p "$docker_compose_tmpdir/$project"
  1808. docker_compose_dir="$docker_compose_tmpdir/$project"
  1809. # if [ -z "$SERVICE_PACK" ]; then
  1810. # export SERVICE_PACK=$(get_default_target_services $SERVICE_PACK)
  1811. # fi
  1812. get_docker_compose $SERVICE_PACK > "$docker_compose_dir/docker-compose.yml" || return 1
  1813. if [ -e "$state_tmpdir/to-merge-in-docker-compose.yml" ]; then
  1814. # debug "Merging some config data in docker-compose.yml:"
  1815. # debug "$(cat $state_tmpdir/to-merge-in-docker-compose.yml)"
  1816. _config_merge "$docker_compose_dir/docker-compose.yml" "$state_tmpdir/to-merge-in-docker-compose.yml" || return 1
  1817. fi
  1818. if [ -z "$(echo $(cat "$docker_compose_dir/docker-compose.yml"))" ]; then
  1819. die "Generated 'docker-compose.yml' is unexpectedly empty."
  1820. fi
  1821. ## XXXvlab: could be more specific and only link the needed charms
  1822. ## XXXvlab: why do we need these links ? If this is for the build command, then it is not useful anymore.
  1823. # for charm in $(shyaml keys services < "$docker_compose_dir/docker-compose.yml"); do
  1824. # if charm.exists "$charm"; then
  1825. # ln -sf "$(charm.get_dir "$charm")" "$docker_compose_dir/$charm" || exit 1
  1826. # fi
  1827. # done
  1828. mkdir "$docker_compose_dir/.data"
  1829. {
  1830. {
  1831. cd "$docker_compose_dir"
  1832. debug "${WHITE}docker-compose.yml$NORMAL for $DARKYELLOW$SERVICE_PACK$NORMAL:"
  1833. debug "$(cat "$docker_compose_dir/docker-compose.yml" | prefix " $GRAY|$NORMAL ")"
  1834. debug "${WHITE}Launching$NORMAL: docker-compose $@"
  1835. if [ "$DRY_COMPOSE_RUN" ]; then
  1836. echo docker-compose "$@"
  1837. else
  1838. docker-compose "$@"
  1839. fi
  1840. echo "$?" > "$docker_compose_dir/.data/errlvl"
  1841. } | _save stdout
  1842. } 3>&1 1>&2 2>&3 | _save stderr 3>&1 1>&2 2>&3
  1843. 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
  1844. err "Detected bug https://github.com/docker/docker/issues/4036 ... "
  1845. err "Please re-launch your command, or switch from 'devicemapper' driver to 'overlayfs' or 'aufs'."
  1846. fi
  1847. docker_compose_errlvl="$(cat "$docker_compose_dir/.data/errlvl" 2>/dev/null)"
  1848. if [ -z "$docker_compose_errlvl" ]; then
  1849. err "Something went wrong before you could gather docker-compose errorlevel."
  1850. return 1
  1851. fi
  1852. return "$docker_compose_errlvl"
  1853. }
  1854. export -f launch_docker_compose
  1855. get_compose_yml_location() {
  1856. if ! [ -z ${COMPOSE_YML_FILE+x} ]; then ## if set, even if empty
  1857. echo "$COMPOSE_YML_FILE"
  1858. return 0
  1859. fi
  1860. parent=$(while ! [ -e "./compose.yml" ]; do
  1861. [ "$PWD" == "/" ] && exit 0
  1862. cd ..
  1863. done; echo "$PWD"
  1864. )
  1865. if [ "$parent" ]; then
  1866. echo "$parent/compose.yml"
  1867. return 0
  1868. fi
  1869. ## XXXvlab: do we need this additional environment variable,
  1870. ## COMPOSE_YML_FILE is not sufficient ?
  1871. if [ "$DEFAULT_COMPOSE_FILE" ]; then
  1872. if ! [ -e "$DEFAULT_COMPOSE_FILE" ]; then
  1873. warn "No 'compose.yml' was found in current or parent dirs," \
  1874. "and \$DEFAULT_COMPOSE_FILE points to an unexistent file." \
  1875. "(${DEFAULT_COMPOSE_FILE})"
  1876. return 0
  1877. fi
  1878. echo "$DEFAULT_COMPOSE_FILE"
  1879. return 0
  1880. fi
  1881. warn "No 'compose.yml' was found in current or parent dirs, and no \$DEFAULT_COMPOSE_FILE was set."
  1882. return 0
  1883. }
  1884. export -f get_compose_yml_location
  1885. get_compose_yml_content() {
  1886. local cache_file="$state_tmpdir/$FUNCNAME.cache"
  1887. if [ -e "$cache_file" ]; then
  1888. cat "$cache_file" &&
  1889. touch "$cache_file" || return 1
  1890. return 0
  1891. fi
  1892. if [ -z "$COMPOSE_YML_FILE" ]; then
  1893. COMPOSE_YML_FILE=$(get_compose_yml_location) || exit 1
  1894. fi
  1895. if [ -e "$COMPOSE_YML_FILE" ]; then
  1896. debug "Found $WHITE$exname$NORMAL YAML file in '$COMPOSE_YML_FILE'."
  1897. COMPOSE_YML_CONTENT=$(cat "$COMPOSE_YML_FILE")
  1898. else
  1899. debug "No compose file found. Using an empty one."
  1900. COMPOSE_YML_CONTENT=""
  1901. fi
  1902. COMPOSE_YML_CONTENT=$(merge_yaml_str "$COMPOSE_YML_CONTENT" "${compose_contents[@]}") || return 1
  1903. output=$(echo "$COMPOSE_YML_CONTENT"| shyaml get-value 2>&1)
  1904. if [ "$?" != 0 ]; then
  1905. outputed_something=
  1906. while IFS='' read -r line1 && IFS='' read -r line2; do
  1907. [ "$outputed_something" ] || err "Invalid YAML in '$COMPOSE_YML_FILE':"
  1908. outputed_something=true
  1909. echo "$line1 $GRAY($line2)$NORMAL"
  1910. done < <(echo "$output" | grep ^yaml.scanner -A 100 |
  1911. sed -r 's/^ in "<stdin>", //g' | sed -r 's/^yaml.scanner.[a-zA-Z]+: //g') |
  1912. prefix " $GRAY|$NORMAL "
  1913. [ "$outputed_something" ] || {
  1914. err "Unexpected error while running 'shyaml get-value' on '$COMPOSE_YML_FILE':"
  1915. echo "$output" | prefix " $GRAY|$NORMAL "
  1916. }
  1917. exit 1
  1918. fi
  1919. echo "$COMPOSE_YML_CONTENT" | tee "$cache_file" || return 1
  1920. }
  1921. export -f get_compose_yml_content
  1922. get_default_target_services() {
  1923. local services=("$@")
  1924. if [ -z "${services[*]}" ]; then
  1925. if [ "$DEFAULT_SERVICES" ]; then
  1926. debug "No service provided, using $WHITE\$DEFAULT_SERVICES$NORMAL variable." \
  1927. "Target services: $DARKYELLOW$DEFAULT_SERVICES$NORMAL"
  1928. services="$DEFAULT_SERVICES"
  1929. else
  1930. err "No service provided."
  1931. return 1
  1932. fi
  1933. fi
  1934. echo "${services[*]}"
  1935. }
  1936. export -f get_default_target_services
  1937. get_master_services() {
  1938. local loaded master_service service
  1939. declare -A loaded
  1940. for service in "$@"; do
  1941. master_service=$(get_top_master_service_for_service "$service") || return 1
  1942. if [ "${loaded[$master_service]}" ]; then
  1943. continue
  1944. fi
  1945. echo "$master_service"
  1946. loaded["$master_service"]=1
  1947. done | xargs printf "%s "
  1948. return "${PIPESTATUS[0]}"
  1949. }
  1950. export -f get_master_services
  1951. _setup_state_dir() {
  1952. export state_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
  1953. #debug "Creating temporary state directory in '$state_tmpdir'."
  1954. # trap_add EXIT "debug \"Removing temporary state directory in $state_tmpdir.\";\
  1955. # rm -rf \"$state_tmpdir\""
  1956. trap_add EXIT "rm -rf \"$state_tmpdir\""
  1957. }
  1958. get_docker_compose_help_msg() {
  1959. local action="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$1"; cat "$(which docker-compose)" | md5_compat)" \
  1960. docker_compose_help_msg
  1961. if [ -e "$cache_file" ]; then
  1962. cat "$cache_file" &&
  1963. touch "$cache_file" || return 1
  1964. return 0
  1965. fi
  1966. docker_compose_help_msg=$(docker-compose $action --help 2>/dev/null) || return 1
  1967. echo "$docker_compose_help_msg" |
  1968. tee "$cache_file" || return 1
  1969. }
  1970. get_docker_compose_usage() {
  1971. local action="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$1"; cat "$(which docker-compose)" | md5_compat)" \
  1972. docker_compose_help_msg
  1973. if [ -e "$cache_file" ]; then
  1974. cat "$cache_file" &&
  1975. touch "$cache_file" || return 1
  1976. return 0
  1977. fi
  1978. docker_compose_help_msg=$(get_docker_compose_help_msg $action) || return 1
  1979. echo "$docker_compose_help_msg" |
  1980. grep -m 1 "^Usage:" -A 10000 |
  1981. egrep -m 1 "^\$" -B 10000 |
  1982. xargs printf "%s " |
  1983. sed -r 's/^Usage: //g' |
  1984. tee "$cache_file" || return 1
  1985. }
  1986. get_docker_compose_opts_help() {
  1987. local action="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$1"; cat "$(which docker-compose)" | md5_compat)" \
  1988. docker_compose_help_msg
  1989. if [ -e "$cache_file" ]; then
  1990. cat "$cache_file" &&
  1991. touch "$cache_file" || return 1
  1992. return 0
  1993. fi
  1994. docker_compose_opts_help=$(get_docker_compose_help_msg $action) || return 1
  1995. echo "$docker_compose_opts_help" |
  1996. grep '^Options:' -A 20000 |
  1997. tail -n +2 |
  1998. { cat ; echo; } |
  1999. egrep -m 1 "^\S*\$" -B 10000 |
  2000. head -n -1 |
  2001. tee "$cache_file" || return 1
  2002. }
  2003. get_docker_compose_commands_help() {
  2004. local action="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$1"; cat "$(which docker-compose)" | md5_compat)" \
  2005. docker_compose_help_msg
  2006. if [ -e "$cache_file" ]; then
  2007. cat "$cache_file" &&
  2008. touch "$cache_file" || return 1
  2009. return 0
  2010. fi
  2011. docker_compose_opts_help=$(get_docker_compose_help_msg $action) || return 1
  2012. echo "$docker_compose_opts_help" |
  2013. grep '^Commands:' -A 20000 |
  2014. tail -n +2 |
  2015. { cat ; echo; } |
  2016. egrep -m 1 "^\S*\$" -B 10000 |
  2017. head -n -1 |
  2018. tee "$cache_file" || return 1
  2019. }
  2020. get_docker_compose_opts_list() {
  2021. local action="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$1"; cat "$(which docker-compose)" | md5_compat)" \
  2022. docker_compose_help_msg
  2023. if [ -e "$cache_file" ]; then
  2024. cat "$cache_file" &&
  2025. touch "$cache_file" || return 1
  2026. return 0
  2027. fi
  2028. docker_compose_opts_help=$(get_docker_compose_opts_help $action) || return 1
  2029. echo "$docker_compose_opts_help" |
  2030. egrep "^\s+-" |
  2031. sed -r 's/\s+((((-[a-zA-Z]|--[a-zA-Z0-9-]+)( [A-Z=]+|=[^ ]+)?)(, )?)+)\s+.*$/\1/g' |
  2032. tee "$cache_file" || return 1
  2033. }
  2034. options_parser() {
  2035. sed -r 's/^(\s+(((-[a-zA-Z]|--[a-zA-Z0-9-]+)([ =]([a-zA-Z_=\"\[]|\])+)?(, | )?)+)\s+)[^ ].*$/\x0\2\x0\0/g'
  2036. printf "\0"
  2037. }
  2038. remove_options_in_option_help_msg() {
  2039. {
  2040. read-0 null
  2041. if [ "$null" ]; then
  2042. err "options parsing error, should start with an option line."
  2043. return 1
  2044. fi
  2045. while read-0 opt full_txt;do
  2046. multi_opts="$(printf "%s " $opt | multi_opts_filter)"
  2047. single_opts="$(printf "%s " $opt | single_opts_filter)"
  2048. for to_remove in "$@"; do
  2049. str_matches "$to_remove" $multi_opts $single_opts && {
  2050. continue 2
  2051. }
  2052. done
  2053. echo -n "$full_txt"
  2054. done
  2055. } < <(options_parser)
  2056. }
  2057. _MULTIOPTION_REGEX='^((-[a-zA-Z]|--[a-zA-Z0-9-]+)(, )?)+'
  2058. _MULTIOPTION_REGEX_LINE_FILTER=$_MULTIOPTION_REGEX'(\s|=)'
  2059. multi_opts_filter() {
  2060. egrep "$_MULTIOPTION_REGEX_LINE_FILTER" |
  2061. sed -r "s/^($_MULTIOPTION_REGEX)(\s|=).*$/\1/g" |
  2062. tr ',' "\n" | xargs printf "%s "
  2063. }
  2064. single_opts_filter() {
  2065. egrep -v "$_MULTIOPTION_REGEX_LINE_FILTER" |
  2066. tr ',' "\n" | xargs printf "%s "
  2067. }
  2068. get_docker_compose_multi_opts_list() {
  2069. local action="$1" opts_list
  2070. opts_list=$(get_docker_compose_opts_list "$action") || return 1
  2071. echo "$opts_list" | multi_opts_filter
  2072. }
  2073. get_docker_compose_single_opts_list() {
  2074. local action="$1" opts_list
  2075. opts_list=$(get_docker_compose_opts_list "$action") || return 1
  2076. echo "$opts_list" | single_opts_filter
  2077. }
  2078. display_help() {
  2079. print_help
  2080. echo "${WHITE}Options${NORMAL}:"
  2081. echo " -h, --help Print this message and quit"
  2082. echo " (ignoring any other options)"
  2083. echo " -V, --version Print current version and quit"
  2084. echo " (ignoring any other options)"
  2085. echo " --dirs Display data dirs and quit"
  2086. echo " (ignoring any other options)"
  2087. echo " -v, --verbose Be more verbose"
  2088. echo " -d, --debug Print full debugging information (sets also verbose)"
  2089. echo " --dry-compose-run If docker-compose will be run, only print out what"
  2090. echo " command line will be used."
  2091. echo " --rebuild-relations-to-service, -R SERVICE"
  2092. echo " Will rebuild all relations to given service"
  2093. echo " --add-compose-content, -Y YAML"
  2094. echo " Will merge some direct YAML with the current compose"
  2095. get_docker_compose_opts_help | remove_options_in_option_help_msg --version --help --verbose |
  2096. filter_docker_compose_help_message
  2097. echo
  2098. echo "${WHITE}Commands${NORMAL} (thanks to docker-compose):"
  2099. get_docker_compose_commands_help | sed -r "s/ ([a-z]+)(\s+)/ ${DARKCYAN}\1${NORMAL}\2/g"
  2100. }
  2101. _graph_service() {
  2102. local service="$1" base="$1"
  2103. charm=$(get_service_charm "$service") || return 1
  2104. metadata=$(charm.metadata "$charm") || return 1
  2105. subordinate=$(echo "$metadata" | shyaml get-value "subordinate" 2>/dev/null)
  2106. if [ "$subordinate" == "True" ]; then
  2107. requires="$(echo "$metadata" | shyaml get-value "requires" 2>/dev/null)"
  2108. master_charm=
  2109. while read-0 relation_name relation; do
  2110. [ "$(echo "$relation" | shyaml get-value "scope" 2>/dev/null)" == "container" ] || continue
  2111. interface="$(echo "$relation" | shyaml get-value "interface" 2>/dev/null)"
  2112. if [ -z "$interface" ]; then
  2113. err "No ${WHITE}$interface${NORMAL} set for relation $relation_name."
  2114. return 1
  2115. fi
  2116. ## Action provided by relation ?
  2117. target_service=
  2118. while read-0 relation_name candidate_target_service _relation_config _tech_dep; do
  2119. [ "$interface" == "$relation_name" ] && {
  2120. target_service="$candidate_target_service"
  2121. break
  2122. }
  2123. done < <(get_compose_relations "$service")
  2124. if [ -z "$target_service" ]; then
  2125. err "Couldn't find ${WHITE}relations.$interface${NORMAL} in" \
  2126. "${DARKYELLOW}$service$NORMAL compose definition."
  2127. return 1
  2128. fi
  2129. master_service="$target_service"
  2130. master_charm=$(get_service_charm "$target_service") || return 1
  2131. break
  2132. done < <(echo "$requires" | shyaml key-values-0 2>/dev/null)
  2133. fi
  2134. _graph_node_service "$service" "$base" "$charm"
  2135. _graph_edge_service "$service" "$subordinate" "$master_service"
  2136. }
  2137. _graph_node_service() {
  2138. local service="$1" base="$2" charm="$3"
  2139. cat <<EOF
  2140. "$(_graph_node_service_label ${service})" [
  2141. style = "filled, $([ "$subordinate" == "True" ] && echo "dashed" || echo "bold")"
  2142. penwidth = $([ "$subordinate" == "True" ] && echo "3" || echo "5")
  2143. color = $([ "$base" ] && echo "blue" || echo "black")
  2144. fillcolor = "white"
  2145. fontname = "Courier New"
  2146. shape = "Mrecord"
  2147. label =<$(_graph_node_service_content "$service")>
  2148. ];
  2149. EOF
  2150. }
  2151. _graph_edge_service() {
  2152. local service="$1" subordinate="$2" master_service="$3"
  2153. while read-0 relation_name target_service relation_config tech_dep; do
  2154. cat <<EOF
  2155. "$(_graph_node_service_label ${service})" -> "$(_graph_node_service_label ${target_service})" [
  2156. penwidth = $([ "$master_service" == "$target_service" ] && echo 3 || echo 2)
  2157. fontsize = 16
  2158. fontcolor = "black"
  2159. style = $([ "$master_service" == "$target_service" ] && echo dashed || echo "\"\"")
  2160. weight = $([ "$master_service" == "$target_service" ] && echo 2.0 || echo 1.0)
  2161. dir = $([ "$master_service" == "$target_service" ] && echo none || echo both)
  2162. arrowtail = odot
  2163. # arrowhead = dotlicurve
  2164. taillabel = "$relation_name" ];
  2165. EOF
  2166. done < <(get_compose_relations "$service") || return 1
  2167. }
  2168. _graph_node_service_label() {
  2169. local service="$1"
  2170. echo "service_$service"
  2171. }
  2172. _graph_node_service_content() {
  2173. local service="$1"
  2174. charm=$(get_service_charm "$service") || return 1
  2175. cat <<EOF
  2176. <table border="0" cellborder="0" cellpadding="3" bgcolor="white">
  2177. <tr>
  2178. <td bgcolor="black" align="center" colspan="2">
  2179. <font color="white">$service</font>
  2180. </td>
  2181. </tr>
  2182. $(if [ "$charm" != "$service" ]; then
  2183. cat <<EOF2
  2184. <tr>
  2185. <td align="left" port="r0">charm: $charm</td>
  2186. </tr>
  2187. EOF2
  2188. fi)
  2189. </table>
  2190. EOF
  2191. }
  2192. cla_contains () {
  2193. local e
  2194. for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  2195. return 1
  2196. }
  2197. filter_docker_compose_help_message() {
  2198. cat - |
  2199. sed -r "s/docker-compose run/${DARKWHITE}compose${NORMAL} ${DARKCYAN}$action${NORMAL}/g;
  2200. s/docker-compose.yml/compose.yml/g;
  2201. s/SERVICES?/${DARKYELLOW}\0${NORMAL}/g;
  2202. s/^(\s+)\\$/\1${WHITE}\$${NORMAL}/g;
  2203. s/^(\s+)run/\1${DARKCYAN}$action${NORMAL}/g;
  2204. s/docker-compose/${DARKWHITE}compose${NORMAL}/g"
  2205. }
  2206. graph() {
  2207. local services=("$@")
  2208. declare -A entries
  2209. cat <<EOF
  2210. digraph g {
  2211. graph [
  2212. fontsize=30
  2213. labelloc="t"
  2214. label=""
  2215. splines=true
  2216. overlap=false
  2217. #rankdir = "LR"
  2218. ];
  2219. ratio = auto;
  2220. EOF
  2221. for target_service in "$@"; do
  2222. services=$(get_ordered_service_dependencies "$target_service") || return 1
  2223. for service in $services; do
  2224. [ "${entries[$service]}" ] && continue || entries[$service]=1
  2225. if cla_contains "$service" "${services[@]}"; then
  2226. base=true
  2227. else
  2228. base=
  2229. fi
  2230. _graph_service "$service" "$base"
  2231. done
  2232. done
  2233. echo "}"
  2234. }
  2235. cached_wget() {
  2236. local cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
  2237. url="$1"
  2238. if [ -e "$cache_file" ]; then
  2239. cat "$cache_file"
  2240. touch "$cache_file"
  2241. return 0
  2242. fi
  2243. wget -O- "${url}" |
  2244. tee "$cache_file"
  2245. if [ "${PIPESTATUS[0]}" != 0 ]; then
  2246. rm "$cache_file"
  2247. die "Unable to fetch '$url'."
  2248. return 1
  2249. fi
  2250. }
  2251. export -f cached_wget
  2252. [ "$SOURCED" ] && return 0
  2253. trap_add "EXIT" clean_cache
  2254. # if [[ "$UID" != 0 ]]; then
  2255. # die "'$exname' requires root permissions (for now). Please run as root."
  2256. # fi
  2257. if [ -z "$DISABLE_SYSTEM_CONFIG_FILE" ]; then
  2258. if [ -r /etc/default/charm ]; then
  2259. . /etc/default/charm
  2260. fi
  2261. if [ -r "/etc/default/$exname" ]; then
  2262. . "/etc/default/$exname"
  2263. fi
  2264. ## XXXvlab: should provide YML config opportunities in possible parent dirs ?
  2265. ## userdir ? and global /etc/compose.yml ?
  2266. for cfgfile in /etc/compose.conf /etc/compose.local.conf \
  2267. /etc/default/compose /etc/compose/local.conf; do
  2268. [ -e "$cfgfile" ] || continue
  2269. . "$cfgfile" || die "Loading config file '$cfgfile' failed."
  2270. done
  2271. fi
  2272. _setup_state_dir
  2273. mkdir -p "$CACHEDIR" || exit 1
  2274. DOCKER_HOST_NET=$(ip addr show docker0 | grep 'inet ' | xargs printf "%s " | cut -f 2 -d " ") || {
  2275. die "Couldn't determine docker host net"
  2276. }
  2277. DOCKER_HOST_IP=$(echo "$DOCKER_HOST_NET" | cut -f 1 -d "/") || {
  2278. die "Couldn't determine docker host ip"
  2279. }
  2280. export DOCKER_HOST_NET DOCKER_HOST_IP
  2281. ##
  2282. ## Argument parsing
  2283. ##
  2284. services=()
  2285. remainder_args=()
  2286. compose_opts=()
  2287. compose_contents=()
  2288. action_opts=()
  2289. services_args=()
  2290. pos_arg_ct=0
  2291. no_hooks=
  2292. no_init=
  2293. action=
  2294. stage="main" ## switches from 'main', to 'action', 'remainder'
  2295. is_docker_compose_action=
  2296. rebuild_relations_to_service=()
  2297. DC_MATCH_MULTI=$(get_docker_compose_multi_opts_list) &&
  2298. DC_MATCH_SINGLE=$(get_docker_compose_single_opts_list) || return 1
  2299. while read-0 arg; do
  2300. case "$stage" in
  2301. "main")
  2302. case "$arg" in
  2303. --help|-h)
  2304. no_init=true ; no_hooks=true ; no_relations=true
  2305. display_help
  2306. exit 0
  2307. ;;
  2308. --verbose|-v)
  2309. export VERBOSE=true
  2310. compose_opts+=("--verbose")
  2311. ;;
  2312. --version|-V)
  2313. print_version
  2314. docker-compose --version
  2315. docker --version
  2316. exit 0
  2317. ;;
  2318. -f|--file)
  2319. read-0 value
  2320. [ -e "$value" ] || die "File $value doesn't exists"
  2321. export DEFAULT_COMPOSE_FILE="$value"
  2322. compose_opts+=("--file $value")
  2323. shift
  2324. ;;
  2325. -p|--project-name)
  2326. read-0 value
  2327. export DEFAULT_PROJECT_NAME="$value"
  2328. compose_opts+=("--project-name $value")
  2329. shift
  2330. ;;
  2331. --no-relations)
  2332. export no_relations=true
  2333. ;;
  2334. --no-hooks)
  2335. export no_hooks=true
  2336. ;;
  2337. --no-init)
  2338. export no_init=true
  2339. ;;
  2340. --rebuild-relations-to-service|-R)
  2341. read-0 value
  2342. rebuild_relations_to_service+=("$value")
  2343. shift
  2344. ;;
  2345. --debug)
  2346. export DEBUG=true
  2347. export VERBOSE=true
  2348. #compose_opts+=("--verbose" "--log-level" "DEBUG")
  2349. ;;
  2350. --add-compose-content|-Y)
  2351. read-0 value
  2352. compose_contents+=("$value")
  2353. shift
  2354. ;;
  2355. --dirs)
  2356. echo "CACHEDIR: $CACHEDIR"
  2357. echo "VARDIR: $VARDIR"
  2358. exit 0
  2359. ;;
  2360. --dry-compose-run)
  2361. export DRY_COMPOSE_RUN=true
  2362. ;;
  2363. --*|-*)
  2364. if str_pattern_matches "$arg" $DC_MATCH_MULTI; then
  2365. read-0 value
  2366. compose_opts+=("$arg" "$value")
  2367. shift;
  2368. elif str_pattern_matches "$arg" $DC_MATCH_SINGLE; then
  2369. compose_opts+=("$arg")
  2370. else
  2371. err "Unknown option '$arg'. Please check help:"
  2372. display_help
  2373. exit 1
  2374. fi
  2375. ;;
  2376. *)
  2377. action="$arg"
  2378. stage="action"
  2379. if DC_USAGE=$(get_docker_compose_usage "$action"); then
  2380. is_docker_compose_action=true
  2381. DC_MATCH_MULTI=$(get_docker_compose_multi_opts_list "$action") &&
  2382. DC_MATCH_SINGLE="$(get_docker_compose_single_opts_list "$action")"
  2383. if [ "$DC_MATCH_MULTI" ]; then
  2384. DC_MATCH_SINGLE="$DC_MATCH_SINGLE $(echo "$DC_MATCH_MULTI" | sed -r 's/( |$)/=\* /g')"
  2385. fi
  2386. pos_args=($(echo "$DC_USAGE" | sed -r 's/\[-[^]]+\] ?//g;s/\[options\] ?//g'))
  2387. pos_args=("${pos_args[@]:1}")
  2388. # echo "USAGE: $DC_USAGE"
  2389. # echo "pos_args: ${pos_args[@]}"
  2390. # echo "MULTI: $DC_MATCH_MULTI"
  2391. # echo "SINGLE: $DC_MATCH_SINGLE"
  2392. # exit 1
  2393. else
  2394. stage="remainder"
  2395. fi
  2396. ;;
  2397. esac
  2398. ;;
  2399. "action") ## Only for docker-compose actions
  2400. case "$arg" in
  2401. --help|-h)
  2402. no_init=true ; no_hooks=true ; no_relations=true
  2403. action_opts+=("$arg")
  2404. ;;
  2405. --*|-*)
  2406. if [ "$is_docker_compose_action" ]; then
  2407. if str_pattern_matches "$arg" $DC_MATCH_MULTI; then
  2408. read-0 value
  2409. action_opts+=("$arg" "$value")
  2410. shift
  2411. elif str_pattern_matches "$arg" $DC_MATCH_SINGLE; then
  2412. action_opts+=("$arg")
  2413. else
  2414. err "Unknown option '$arg'. Please check '${DARKCYAN}$action${NORMAL}' help:"
  2415. docker-compose "$action" --help |
  2416. filter_docker_compose_help_message >&2
  2417. exit 1
  2418. fi
  2419. fi
  2420. ;;
  2421. *)
  2422. # echo "LOOP $1 : pos_arg: $pos_arg_ct // ${pos_args[$pos_arg_ct]}"
  2423. if [[ "${pos_args[$pos_arg_ct]}" == "[SERVICE...]" ]]; then
  2424. services_args+=("$arg")
  2425. elif [[ "${pos_args[$pos_arg_ct]}" == "SERVICE" ]]; then
  2426. services_args=("$arg") || exit 1
  2427. stage="remainder"
  2428. else
  2429. action_posargs+=("$arg")
  2430. ((pos_arg_ct++))
  2431. fi
  2432. ;;
  2433. esac
  2434. ;;
  2435. "remainder")
  2436. remainder_args+=("$arg")
  2437. while read-0 arg; do
  2438. remainder_args+=("$arg")
  2439. done
  2440. break 3
  2441. ;;
  2442. esac
  2443. shift
  2444. done < <(cla.normalize "$@")
  2445. export compose_contents
  2446. [ "${services_args[*]}" ] && debug " ${DARKWHITE}Services:$NORMAL ${DARKYELLOW}${services_args[*]}$NORMAL"
  2447. [ "${compose_opts[*]}" ] && debug " ${DARKWHITE}Main docker-compose opts:$NORMAL ${compose_opts[*]}"
  2448. [ "${action_posargs[*]}" ] && debug " ${DARKWHITE}Main docker-compose pos args:$NORMAL ${action_posargs[*]}"
  2449. [ "${action_opts[*]}" ] && debug " ${DARKWHITE}Action $DARKCYAN$action$NORMAL with opts:$NORMAL ${action_opts[*]}"
  2450. [ "${remainder_args[*]}" ] && debug " ${DARKWHITE}Remainder args:$NORMAL ${remainder_args[*]}"
  2451. aexport remainder_args
  2452. ##
  2453. ## Actual code
  2454. ##
  2455. export DOCKER_DATASTORE=${DOCKER_DATASTORE:-/srv/docker-datastore}
  2456. COMPOSE_YML_FILE=$(get_compose_yml_location) || exit 1
  2457. COMPOSE_YML_CONTENT=$(get_compose_yml_content) || exit 1
  2458. export COMPOSE_YML_FILE COMPOSE_YML_CONTENT
  2459. charm.sanity_checks || die "Sanity checks about charm-store failed. Please correct."
  2460. ##
  2461. ## Get services in command line.
  2462. ##
  2463. if [ -z "$is_docker_compose_action" -a "$action" ]; then
  2464. action_service=${remainder_args[0]}
  2465. remainder_args=("${remainder_args[@]:1}")
  2466. if is_service_action=$(has_service_action "$action_service" "$action"); then
  2467. {
  2468. read-0 action_type
  2469. case "$action_type" in
  2470. "relation")
  2471. read-0 _ target_service _target_charm relation_name
  2472. debug "Found action $DARKYELLOW${action_service}$NORMAL/$DARKBLUE$relation_name$NORMAL/$DARKCYAN$action$NORMAL (in $DARKYELLOW$target_service$NORMAL)"
  2473. ;;
  2474. "direct")
  2475. debug "Found action $DARKYELLOW${action_service}$NORMAL.$DARKCYAN$action$NORMAL"
  2476. ;;
  2477. esac
  2478. } < <(has_service_action "$action_service" "$action")
  2479. services_args=("$action_service")
  2480. else
  2481. die "Unknown action '${DARKCYAN}$action$NORMAL': It doesn't match any docker-compose commands nor inner charm actions."
  2482. fi
  2483. else
  2484. case "$action" in
  2485. ps)
  2486. if [ "${#services_args[@]}" == 0 ]; then
  2487. services_args=($(printf "%s" "$COMPOSE_YML_CONTENT" | shyaml keys 2>/dev/null)) || true
  2488. fi
  2489. ;;
  2490. up)
  2491. if [ "${#services_args[@]}" == 0 ]; then
  2492. while read-0 service; do
  2493. charm=$(get_service_charm "$service")
  2494. metadata=$(charm.metadata "$charm")
  2495. type="$(printf "%s" "$metadata" | shyaml get-value type 2>/dev/null)" || true
  2496. if [ "$type" != "run-once" ]; then
  2497. services_args+=("$service")
  2498. fi
  2499. done < <(printf "%s" "$COMPOSE_YML_CONTENT" | shyaml keys-0 2>/dev/null)
  2500. fi
  2501. ;;
  2502. config)
  2503. services_args=("${action_posargs[@]}")
  2504. ;;
  2505. esac
  2506. if [ "$is_docker_compose_action" -a "${#services_args[@]}" -gt 0 ]; then
  2507. services=($(get_master_services "${services_args[@]}")) || exit 1
  2508. action_posargs+=("${services[@]}")
  2509. fi
  2510. fi
  2511. get_docker_compose "${services_args[@]}" >/dev/null || { ## precalculate variable \$_current_docker_compose
  2512. err "Fails to compile base 'docker-compose.yml'"
  2513. exit 1
  2514. }
  2515. ##
  2516. ## Pre-action
  2517. ##
  2518. full_init=
  2519. case "$action" in
  2520. up|run)
  2521. full_init=true
  2522. post_hook=true
  2523. ;;
  2524. ""|down|restart|logs|config|ps)
  2525. full_init=
  2526. ;;
  2527. *)
  2528. if [ "$is_service_action" ]; then
  2529. full_init=true
  2530. fi
  2531. ;;
  2532. esac
  2533. if [ "$full_init" ]; then
  2534. ## init in order
  2535. if [ -z "$no_init" ]; then
  2536. Section setup host resources
  2537. setup_host_resources "${services_args[@]}" || exit 1
  2538. Section initialisation
  2539. run_service_hook init "${services_args[@]}" || exit 1
  2540. fi
  2541. ## Get relations
  2542. if [ -z "$no_relations" ]; then
  2543. if [ "${#rebuild_relations_to_service[@]}" != 0 ]; then
  2544. rebuild_relations_to_service=(
  2545. $(get_master_services "${rebuild_relations_to_service[@]}"))
  2546. project=$(get_default_project_name) || return 1
  2547. for service in "${rebuild_relations_to_service[@]}"; do
  2548. for dir in "$VARDIR/relations/$project/"*"-${service}/"*; do
  2549. [ -d "$dir" ] && {
  2550. debug rm -rf "$dir"
  2551. rm -rf "$dir"
  2552. }
  2553. done
  2554. done
  2555. fi
  2556. run_service_relations "${services_args[@]}" || exit 1
  2557. fi
  2558. run_service_hook pre_deploy "${services_args[@]}" || exit 1
  2559. fi
  2560. if [ "$action" == "run" ]; then
  2561. charm=$(get_service_charm "${services_args[0]}") || exit 1
  2562. metadata=$(charm.metadata "$charm") || exit 1
  2563. type="$(printf "%s" "$metadata" | shyaml get-value type 2>/dev/null)" || true
  2564. if [ "$type" == "run-once" ]; then
  2565. run_service_hook dc-pre-run "${services_args[@]}" || exit 1
  2566. fi
  2567. fi
  2568. export SERVICE_PACK="${services_args[*]}"
  2569. ##
  2570. ## Docker-compose
  2571. ##
  2572. case "$action" in
  2573. up|start|stop|build|run)
  2574. if [[ "$action" == "up" ]] && ! array_member action_opts -d; then ## force daemon mode for up
  2575. action_opts+=("-d")
  2576. fi
  2577. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "${action_posargs[@]}" "${remainder_args[@]}"
  2578. ;;
  2579. logs)
  2580. if ! array_member action_opts --tail; then ## force daemon mode for up
  2581. action_opts+=("--tail" "10")
  2582. fi
  2583. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "${action_posargs[@]}" "${remainder_args[@]}"
  2584. ;;
  2585. "")
  2586. launch_docker_compose "${compose_opts[@]}"
  2587. ;;
  2588. graph)
  2589. graph $SERVICE_PACK
  2590. ;;
  2591. config)
  2592. ## removing the services
  2593. services=($(get_master_services "${action_posargs[@]}")) || exit 1
  2594. ## forcing docker-compose config to output the config file to stdout and not stderr
  2595. out=$(launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "${remainder_args[@]}" 2>&1) || {
  2596. echo "$out"
  2597. exit 1
  2598. }
  2599. echo "$out"
  2600. warn "Runtime configuration modification (from relations) are not included here."
  2601. ;;
  2602. down)
  2603. if ! array_member action_opts --remove-orphans; then ## force daemon mode for up
  2604. debug "Adding a default argument of '--remove-orphans'"
  2605. action_opts+=("--remove-orphans")
  2606. fi
  2607. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "${remainder_args[@]}"
  2608. ;;
  2609. *)
  2610. if [ "$is_service_action" ]; then
  2611. run_service_action "$SERVICE_PACK" "$action" "${remainder_args[@]}"
  2612. else
  2613. launch_docker_compose "${compose_opts[@]}" "$action" "${action_opts[@]}" "${action_posargs[@]}" "${remainder_args[@]}"
  2614. fi
  2615. ;;
  2616. esac || exit 1
  2617. if [ "$post_hook" -a "${#services_args[@]}" != 0 ]; then
  2618. run_service_hook post_deploy "${services_args[@]}" || exit 1
  2619. fi