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.

274 lines
7.1 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. get_os() {
  10. local uname_output machine
  11. uname_output="$(uname -s)"
  12. case "${uname_output}" in
  13. Linux*) machine=linux;;
  14. Darwin*) machine=mac;;
  15. CYGWIN*) machine=cygwin;;
  16. MINGW*) machine=mingw;;
  17. *) machine="UNKNOWN:${uname_output}"
  18. esac
  19. echo "${machine}"
  20. }
  21. read-0() {
  22. local eof= IFS=''
  23. while [ "$1" ]; do
  24. read -r -d '' -- "$1" || eof=1
  25. shift
  26. done
  27. [ -z "$eof" ]
  28. }
  29. get_running_compose_containers() {
  30. ## XXXvlab: docker bug: there will be a final newline anyway
  31. docker ps --filter label="compose.service" --format='{{.ID}}'
  32. }
  33. get_volumes_for_container() {
  34. local container="$1"
  35. docker inspect \
  36. --format '{{range $mount := .Mounts}}{{$mount.Source}}{{"\x00"}}{{$mount.Destination}}{{"\x00"}}{{end}}' \
  37. "$container"
  38. }
  39. is_volume_used() {
  40. local volume="$1"
  41. while read container_id; do
  42. while read-0 src dst; do
  43. [ "$src" == "$volume" ] && return 0
  44. done < <(get_volumes_for_container "$container_id")
  45. done < <(get_running_compose_containers)
  46. return 1
  47. }
  48. clean_unused_sessions() {
  49. for f in "$COMPOSE_VAR/sessions/"*; do
  50. [ -e "$f" ] || continue
  51. is_volume_used "$f" && continue
  52. rm -f "$f"
  53. done
  54. }
  55. relink_subdirs() {
  56. local dir
  57. for dir in "$@"; do
  58. [ -L "$dir" ] || continue
  59. target=$(realpath "$dir")
  60. [ -d "$target" ] || continue
  61. docker_run_opts+=("-v" "$target:$dir")
  62. [ -e "$dir/metadata.yml" ] && continue
  63. relink_subdirs "$dir"/*
  64. done
  65. }
  66. mk_docker_run_options() {
  67. docker_run_opts=("-v" "/var/run/docker.sock:/var/run/docker.sock")
  68. ## CACHE/DATA DIRS
  69. docker_run_opts+=("-v" "$COMPOSE_VAR:/var/lib/compose")
  70. docker_run_opts+=("-v" "$COMPOSE_CACHE:/var/cache/compose")
  71. docker_run_opts+=("-v" "$TZ_PATH:/etc/timezone:ro")
  72. ## current dir
  73. if parent=$(while true; do
  74. [ -e "./compose.yml" ] && {
  75. echo "$PWD"
  76. exit 0
  77. }
  78. [ "$PWD" == "/" ] && exit 1
  79. cd ..
  80. done
  81. ); then
  82. docker_path=$COMPOSE_VAR/root/$(basename "$parent")
  83. docker_run_opts+=("-v" "$parent:$docker_path:ro" \
  84. "-w" "$docker_path")
  85. fi
  86. ##
  87. ## Load config files
  88. ##
  89. if [ -z "$DISABLE_SYSTEM_CONFIG_FILE" ]; then
  90. ## XXXvlab: should provide YML config opportunities in possible parent dirs ?
  91. ## userdir ? and global /etc/compose.yml ?
  92. for cfgfile in "${compose_config_files[@]}"; do
  93. [ -e "$cfgfile" ] || continue
  94. docker_run_opts+=("-v" "$cfgfile:$cfgfile:ro")
  95. . "$cfgfile"
  96. done
  97. else
  98. docker_run_opts+=("-e" "DISABLE_SYSTEM_CONFIG_FILE=$DISABLE_SYSTEM_CONFIG_FILE")
  99. fi
  100. ##
  101. ## Checking vars
  102. ##
  103. ## CHARM_STORE
  104. CHARM_STORE=${CHARM_STORE:-/srv/charm-store}
  105. [ -L "$CHARM_STORE" ] && {
  106. CHARM_STORE=$(readlink "$CHARM_STORE") || exit 1
  107. }
  108. docker_run_opts+=(
  109. "-v" "$CHARM_STORE:/srv/charm-store:ro"
  110. "-e" "CHARM_STORE=/srv/charm-store"
  111. "-e" "HOST_CHARM_STORE=$CHARM_STORE"
  112. )
  113. relink_subdirs /srv/charm-store/*
  114. ## DEFAULT_COMPOSE_FILE
  115. if [ "${DEFAULT_COMPOSE_FILE+x}" ]; then
  116. DEFAULT_COMPOSE_FILE=$(realpath "$DEFAULT_COMPOSE_FILE")
  117. dirname=$(dirname "$DEFAULT_COMPOSE_FILE")/
  118. if [ -e "${DEFAULT_COMPOSE_FILE}" ]; then
  119. docker_run_opts+=("-v" "$dirname:$dirname:ro")
  120. fi
  121. fi
  122. ## COMPOSE_YML_FILE
  123. if [ "${COMPOSE_YML_FILE+x}" ]; then
  124. if [ -e "${COMPOSE_YML_FILE}" ]; then
  125. docker_run_opts+=(
  126. "-v" "$COMPOSE_YML_FILE:/tmp/compose.yml:ro"
  127. "-e" "COMPOSE_YML_FILE=/tmp/compose.yml"
  128. "-e" "HOST_COMPOSE_YML_FILE=/tmp/compose.yml"
  129. )
  130. fi
  131. fi
  132. ## DATASTORE
  133. if [ "${DATASTORE+x}" ]; then
  134. docker_run_opts+=(
  135. "-v" "$DATASTORE:/srv/datastore/data:rw"
  136. "-e" "DATASTORE=/srv/datastore/data"
  137. "-e" "HOST_DATASTORE=$DATASTORE"
  138. )
  139. fi
  140. ## CONFIGSTORE
  141. if [ "${CONFIGSTORE+x}" ]; then
  142. docker_run_opts+=(
  143. "-v" "$CONFIGSTORE:/srv/datastore/config:rw"
  144. "-e" "CONFIGSTORE=/srv/datastore/config"
  145. "-e" "HOST_CONFIGSTORE=$CONFIGSTORE"
  146. )
  147. fi
  148. docker_run_opts+=("-v" "$HOME/.docker:/root/.docker")
  149. COMPOSE_DOCKER_IMAGE=${COMPOSE_DOCKER_IMAGE:-docker.0k.io/compose}
  150. docker_run_opts+=("-e" "COMPOSE_DOCKER_IMAGE=$COMPOSE_DOCKER_IMAGE")
  151. COMPOSE_LAUNCHER_BIN=$(readlink -f "${BASH_SOURCE[0]}")
  152. docker_run_opts+=("-v" "$COMPOSE_LAUNCHER_BIN:/usr/local/bin/compose")
  153. filename=$(mktemp -p /tmp/ -t launch_opts-XXXXXXXXXXXXXXXX)
  154. {
  155. printf "%s\0" "${docker_run_opts[@]}"
  156. } > "$filename"
  157. sha=$(sha256sum "$filename")
  158. sha=${sha:0:64}
  159. dest="$COMPOSE_VAR/sessions/$sha"
  160. {
  161. printf "%s\0" "-v" "$dest:$dest"
  162. printf "%s\0" "-e" "COMPOSE_LAUNCHER_OPTS=$dest"
  163. printf "%s\0" "-e" "COMPOSE_LAUNCHER_BIN=$COMPOSE_LAUNCHER_BIN"
  164. } >> "$filename"
  165. mkdir -p "$COMPOSE_VAR"/sessions
  166. mv "$filename" "$dest"
  167. echo "$dest"
  168. }
  169. run() {
  170. local os docker_run_opts
  171. ## Order matters, files get to override vars
  172. compose_config_files=(
  173. /etc/default/charm
  174. /etc/default/datastore
  175. /etc/compose.conf
  176. /etc/compose.local.conf
  177. /etc/default/compose
  178. ~/.compose/etc/local.conf
  179. /etc/compose/local.conf
  180. )
  181. os=$(get_os)
  182. case "$os" in
  183. linux)
  184. COMPOSE_VAR=/var/lib/compose
  185. COMPOSE_CACHE=/var/cache/compose
  186. ;;
  187. mac)
  188. if ! [ -e "$HOME/.compose/etc/timezone" ]; then
  189. TZ=${TZ:-"Europe/Paris"}
  190. echo "$TZ" > "$HOME"/.compose/etc/timezone
  191. fi
  192. TZ_PATH="$HOME"/.compose/etc/timezone
  193. COMPOSE_VAR="$HOME"/.compose/lib
  194. COMPOSE_CACHE="$HOME"/.compose/cache
  195. ;;
  196. *)
  197. echo "System '$os' not supported yet." >&2
  198. exit 1
  199. ;;
  200. esac
  201. docker_run_opts=()
  202. if [ -z "$COMPOSE_LAUNCHER_OPTS" ]; then
  203. clean_unused_sessions
  204. COMPOSE_LAUNCHER_OPTS="$(mk_docker_run_options)"
  205. fi
  206. while read-0 opt; do
  207. docker_run_opts+=("$opt")
  208. ## catch COMPOSE_DOCKER_IMAGE
  209. if [[ "$env" == "true" && "$opt" == "COMPOSE_DOCKER_IMAGE="* ]]; then
  210. COMPOSE_DOCKER_IMAGE=${opt##COMPOSE_DOCKER_IMAGE=}
  211. elif [ "$opt" == "-e" ]; then
  212. env=true
  213. else
  214. env=
  215. fi
  216. done < <(cat "$COMPOSE_LAUNCHER_OPTS")
  217. if [ -t 0 ]; then
  218. docker_run_opts+=("-i")
  219. fi
  220. if [ -t 1 ]; then
  221. docker_run_opts+=("-t")
  222. fi
  223. exec docker run --rm "${docker_run_opts[@]}" "${COMPOSE_DOCKER_IMAGE}" "$@"
  224. }
  225. run "$@"