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.

211 lines
5.6 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
  1. #!/bin/bash
  2. ## Bash wrap script to launch the ``compose`` docker with right options.
  3. ##
  4. ##
  5. ## Launcher
  6. ## - should need minimum requirement to run
  7. ## - no shell libs
  8. ##
  9. read-0() {
  10. local eof= IFS=''
  11. while [ "$1" ]; do
  12. read -r -d '' -- "$1" || eof=1
  13. shift
  14. done
  15. [ -z "$eof" ]
  16. }
  17. get_running_compose_containers() {
  18. ## XXXvlab: docker bug: there will be a final newline anyway
  19. docker ps --filter label="compose.service" --format='{{.ID}}'
  20. }
  21. get_volumes_for_container() {
  22. local container="$1"
  23. docker inspect \
  24. --format '{{range $mount := .Mounts}}{{$mount.Source}}{{"\x00"}}{{$mount.Destination}}{{"\x00"}}{{end}}' \
  25. "$container"
  26. }
  27. is_volume_used() {
  28. local volume="$1"
  29. while read container_id; do
  30. while read-0 src dst; do
  31. [ "$src" == "$volume" ] && return 0
  32. done < <(get_volumes_for_container "$container_id")
  33. done < <(get_running_compose_containers)
  34. return 1
  35. }
  36. clean_unused_sessions() {
  37. for f in /var/lib/compose/sessions/*; do
  38. [ -e "$f" ] || continue
  39. is_volume_used "$f" && continue
  40. rm -f "$f"
  41. done
  42. }
  43. relink_subdirs() {
  44. local dir
  45. for dir in "$@"; do
  46. [ -L "$dir" ] || continue
  47. target=$(realpath "$dir")
  48. [ -d "$target" ] || continue
  49. docker_run_opts+=("-v" "$target:$dir")
  50. [ -e "$dir/metadata.yml" ] && continue
  51. relink_subdirs "$dir"/*
  52. done
  53. }
  54. mk_docker_run_options() {
  55. docker_run_opts=("-v" "/var/run/docker.sock:/var/run/docker.sock")
  56. ## CACHE/DATA DIRS
  57. docker_run_opts+=("-v" "/var/lib/compose:/var/lib/compose")
  58. docker_run_opts+=("-v" "/var/cache/compose:/var/cache/compose")
  59. docker_run_opts+=("-v" "/etc/timezone:/etc/timezone:ro")
  60. ## current dir
  61. if parent=$(while true; do
  62. [ -e "./compose.yml" ] && {
  63. echo "$PWD"
  64. exit 0
  65. }
  66. [ "$PWD" == "/" ] && exit 1
  67. cd ..
  68. done
  69. ); then
  70. docker_path=/var/lib/compose/root/$(basename "$parent")
  71. docker_run_opts+=("-v" "$parent:$docker_path:ro" \
  72. "-w" "$docker_path")
  73. fi
  74. ##
  75. ## Load config files
  76. ##
  77. if [ -z "$DISABLE_SYSTEM_CONFIG_FILE" ]; then
  78. if [ -r /etc/default/charm ]; then
  79. docker_run_opts+=("-v" "/etc/default/charm:/etc/default/charm:ro")
  80. . /etc/default/charm
  81. fi
  82. ## XXXvlab: should provide YML config opportunities in possible parent dirs ?
  83. ## userdir ? and global /etc/compose.yml ?
  84. for cfgfile in /etc/compose.conf /etc/compose.local.conf \
  85. /etc/default/compose /etc/compose/local.conf; do
  86. [ -e "$cfgfile" ] || continue
  87. docker_run_opts+=("-v" "$cfgfile:$cfgfile:ro")
  88. . "$cfgfile"
  89. done
  90. for cfgfile in /etc/default/datastore; do
  91. [ -e "$cfgfile" ] || continue
  92. docker_run_opts+=("-v" "$cfgfile:$cfgfile:ro")
  93. done
  94. else
  95. docker_run_opts+=("-e" "DISABLE_SYSTEM_CONFIG_FILE=$DISABLE_SYSTEM_CONFIG_FILE")
  96. fi
  97. ##
  98. ## Checking vars
  99. ##
  100. ## CHARM_STORE
  101. CHARM_STORE=${CHARM_STORE:-/srv/charm-store}
  102. [ -L "$CHARM_STORE" ] && {
  103. CHARM_STORE=$(readlink "$CHARM_STORE") || exit 1
  104. }
  105. docker_run_opts+=("-v" "$CHARM_STORE:/srv/charm-store:ro")
  106. relink_subdirs /srv/charm-store/*
  107. ## DEFAULT_COMPOSE_FILE
  108. if [ "${DEFAULT_COMPOSE_FILE+x}" ]; then
  109. DEFAULT_COMPOSE_FILE=$(realpath "$DEFAULT_COMPOSE_FILE")
  110. dirname=$(dirname "$DEFAULT_COMPOSE_FILE")/
  111. if [ -e "${DEFAULT_COMPOSE_FILE}" ]; then
  112. docker_run_opts+=("-v" "$dirname:$dirname:ro")
  113. fi
  114. fi
  115. ## COMPOSE_YML_FILE
  116. if [ "${COMPOSE_YML_FILE+x}" ]; then
  117. if [ -e "${COMPOSE_YML_FILE}" ]; then
  118. docker_run_opts+=("-v" "$COMPOSE_YML_FILE:/tmp/compose.yml:ro")
  119. docker_run_opts+=("-e" "COMPOSE_YML_FILE=/tmp/compose.yml")
  120. fi
  121. fi
  122. ## DATASTORE
  123. if [ "${DATASTORE+x}" ]; then
  124. docker_run_opts+=("-v" "$DATASTORE:/srv/datastore/data:rw")
  125. docker_run_opts+=("-e" "DATASTORE=/srv/datastore/data")
  126. fi
  127. ## CONFIGSTORE
  128. if [ "${CONFIGSTORE+x}" ]; then
  129. docker_run_opts+=("-v" "$CONFIGSTORE:/srv/datastore/config:rw")
  130. docker_run_opts+=("-e" "CONFIGSTORE=/srv/datastore/config")
  131. fi
  132. docker_run_opts+=("-v" "$HOME/.docker:/root/.docker")
  133. COMPOSE_LAUNCHER_BIN=$(readlink -f "${BASH_SOURCE[0]}")
  134. filename=$(mktemp -p /tmp/ -t launch_opts-XXXXXXXXXXXXXXXX)
  135. {
  136. printf "%s\0" "${docker_run_opts[@]}"
  137. } > "$filename"
  138. sha=$(sha256sum "$filename")
  139. sha=${sha:0:64}
  140. dest="/var/lib/compose/sessions/$sha"
  141. {
  142. printf "%s\0" "-v" "$dest:$dest"
  143. printf "%s\0" "-e" "COMPOSE_LAUNCHER_OPTS=$dest"
  144. printf "%s\0" "-e" "COMPOSE_LAUNCHER_BIN=$COMPOSE_LAUNCHER_BIN"
  145. } >> "$filename"
  146. mkdir -p /var/lib/compose/sessions
  147. mv "$filename" "$dest"
  148. echo "$dest"
  149. }
  150. run() {
  151. docker_run_opts=()
  152. if [ -z "$COMPOSE_LAUNCHER_OPTS" ]; then
  153. clean_unused_sessions
  154. COMPOSE_LAUNCHER_OPTS="$(mk_docker_run_options)"
  155. fi
  156. while read-0 opt; do
  157. docker_run_opts+=("$opt")
  158. done < <(cat "$COMPOSE_LAUNCHER_OPTS")
  159. COMPOSE_DOCKER_IMAGE=${COMPOSE_DOCKER_IMAGE:-docker.0k.io/compose}
  160. if [ -t 0 ]; then
  161. docker_run_opts+=("-i")
  162. fi
  163. if [ -t 1 ]; then
  164. docker_run_opts+=("-t")
  165. fi
  166. exec docker run --rm "${docker_run_opts[@]}" "${COMPOSE_DOCKER_IMAGE}" "$@"
  167. }
  168. run "$@"