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.

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