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.

273 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 -f "$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. DATASTORE=${DATASTORE:-/srv/datastore/data}
  134. docker_run_opts+=(
  135. "-v" "$DATASTORE:/srv/datastore/data:rw"
  136. "-e" "DATASTORE=/srv/datastore/data"
  137. "-e" "HOST_DATASTORE=$DATASTORE"
  138. )
  139. ## CONFIGSTORE
  140. CONFIGSTORE=${CONFIGSTORE:-/srv/datastore/config}
  141. docker_run_opts+=(
  142. "-v" "$CONFIGSTORE:/srv/datastore/config:rw"
  143. "-e" "CONFIGSTORE=/srv/datastore/config"
  144. "-e" "HOST_CONFIGSTORE=$CONFIGSTORE"
  145. )
  146. docker_run_opts+=("-v" "$HOME/.docker:/root/.docker")
  147. COMPOSE_DOCKER_IMAGE=${COMPOSE_DOCKER_IMAGE:-docker.0k.io/compose}
  148. docker_run_opts+=("-e" "COMPOSE_DOCKER_IMAGE=$COMPOSE_DOCKER_IMAGE")
  149. COMPOSE_LAUNCHER_BIN=$(readlink -f "${BASH_SOURCE[0]}")
  150. docker_run_opts+=("-v" "$COMPOSE_LAUNCHER_BIN:/usr/local/bin/compose")
  151. filename=$(mktemp -p /tmp/ -t launch_opts-XXXXXXXXXXXXXXXX)
  152. {
  153. printf "%s\0" "${docker_run_opts[@]}"
  154. } > "$filename"
  155. sha=$(sha256sum "$filename")
  156. sha=${sha:0:64}
  157. dest="$COMPOSE_VAR/sessions/$sha"
  158. {
  159. printf "%s\0" "-v" "$dest:$dest"
  160. printf "%s\0" "-e" "COMPOSE_LAUNCHER_OPTS=$dest"
  161. printf "%s\0" "-e" "COMPOSE_LAUNCHER_BIN=$COMPOSE_LAUNCHER_BIN"
  162. } >> "$filename"
  163. mkdir -p "$COMPOSE_VAR"/sessions
  164. mv "$filename" "$dest"
  165. echo "$dest"
  166. }
  167. run() {
  168. local os docker_run_opts
  169. ## Order matters, files get to override vars
  170. compose_config_files=(
  171. /etc/default/charm
  172. /etc/default/datastore
  173. /etc/compose.conf
  174. /etc/compose.local.conf
  175. /etc/default/compose
  176. ~/.compose/etc/local.conf
  177. /etc/compose/local.conf
  178. )
  179. os=$(get_os)
  180. case "$os" in
  181. linux)
  182. COMPOSE_VAR=/var/lib/compose
  183. COMPOSE_CACHE=/var/cache/compose
  184. TZ_PATH=/etc/timezone
  185. ;;
  186. mac)
  187. if ! [ -e "$HOME/.compose/etc/timezone" ]; then
  188. TZ=${TZ:-"Europe/Paris"}
  189. echo "$TZ" > "$HOME"/.compose/etc/timezone
  190. fi
  191. TZ_PATH="$HOME"/.compose/etc/timezone
  192. COMPOSE_VAR="$HOME"/.compose/lib
  193. COMPOSE_CACHE="$HOME"/.compose/cache
  194. ;;
  195. *)
  196. echo "System '$os' not supported yet." >&2
  197. exit 1
  198. ;;
  199. esac
  200. docker_run_opts=()
  201. if [ -z "$COMPOSE_LAUNCHER_OPTS" ]; then
  202. clean_unused_sessions
  203. COMPOSE_LAUNCHER_OPTS="$(mk_docker_run_options)"
  204. fi
  205. while read-0 opt; do
  206. docker_run_opts+=("$opt")
  207. ## catch COMPOSE_DOCKER_IMAGE
  208. if [[ "$env" == "true" && "$opt" == "COMPOSE_DOCKER_IMAGE="* ]]; then
  209. COMPOSE_DOCKER_IMAGE=${opt##COMPOSE_DOCKER_IMAGE=}
  210. elif [ "$opt" == "-e" ]; then
  211. env=true
  212. else
  213. env=
  214. fi
  215. done < <(cat "$COMPOSE_LAUNCHER_OPTS")
  216. if [ -t 0 ]; then
  217. docker_run_opts+=("-i")
  218. fi
  219. if [ -t 1 ]; then
  220. docker_run_opts+=("-t")
  221. fi
  222. exec docker run --rm "${docker_run_opts[@]}" "${COMPOSE_DOCKER_IMAGE}" "$@"
  223. }
  224. run "$@"