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.

186 lines
4.9 KiB

9 years ago
6 years ago
9 years ago
6 years ago
9 years ago
9 years ago
6 years ago
9 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
9 years ago
6 years ago
9 years ago
6 years ago
6 years ago
9 years ago
6 years ago
9 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
9 years ago
6 years ago
6 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. mk_docker_run_options() {
  44. docker_run_opts=("-v" "/var/run/docker.sock:/var/run/docker.sock")
  45. ## CACHE/DATA DIRS
  46. docker_run_opts+=("-v" "/var/lib/compose:/var/lib/compose")
  47. docker_run_opts+=("-v" "/var/cache/compose:/var/cache/compose")
  48. docker_run_opts+=("-v" "/etc/timezone:/etc/timezone:ro")
  49. ## current dir
  50. if parent=$(while true; do
  51. [ -e "./compose.yml" ] && {
  52. echo "$PWD"
  53. exit 0
  54. }
  55. [ "$PWD" == "/" ] && exit 1
  56. cd ..
  57. done
  58. ); then
  59. docker_path=/var/lib/compose/root/$(basename "$parent")
  60. docker_run_opts+=("-v" "$parent:$docker_path:ro" \
  61. "-w" "$docker_path")
  62. fi
  63. ##
  64. ## Load config files
  65. ##
  66. if [ -z "$DISABLE_SYSTEM_CONFIG_FILE" ]; then
  67. if [ -r /etc/default/charm ]; then
  68. docker_run_opts+=("-v" "/etc/default/charm:/etc/default/charm:ro")
  69. . /etc/default/charm
  70. fi
  71. if [ -r "/etc/default/compose" ]; then
  72. docker_run_opts+=("-v" "/etc/default/$exname:/etc/default/$exname:ro")
  73. . /etc/default/compose
  74. fi
  75. ## XXXvlab: should provide YML config opportunities in possible parent dirs ?
  76. ## userdir ? and global /etc/compose.yml ?
  77. for cfgfile in /etc/compose.conf /etc/compose.local.conf \
  78. /etc/default/compose /etc/compose/local.conf; do
  79. [ -e "$cfgfile" ] || continue
  80. docker_run_opts+=("-v" "$cfgfile:$cfgfile:ro")
  81. . "$cfgfile"
  82. done
  83. else
  84. docker_run_opts+=("-e" "DISABLE_SYSTEM_CONFIG_FILE=$DISABLE_SYSTEM_CONFIG_FILE")
  85. fi
  86. ##
  87. ## Checking vars
  88. ##
  89. ## CHARM_STORE
  90. CHARM_STORE=${CHARM_STORE:-/srv/charm-store}
  91. [ -L "$CHARM_STORE" ] && {
  92. CHARM_STORE=$(readlink "$CHARM_STORE") || exit 1
  93. }
  94. docker_run_opts+=("-v" "$CHARM_STORE:/srv/charm-store:ro")
  95. ## COMPOSE_YML_FILE
  96. if [ "${COMPOSE_YML_FILE+x}" ]; then
  97. if [ -e "${COMPOSE_YML_FILE}" ]; then
  98. docker_run_opts+=("-v" "$COMPOSE_YML_FILE:/tmp/compose.yml:ro")
  99. docker_run_opts+=("-e" "COMPOSE_YML_FILE=/tmp/compose.yml")
  100. fi
  101. fi
  102. ## DATASTORE
  103. if [ "${DATASTORE+x}" ]; then
  104. if [ -d "${DATASTORE}" ]; then
  105. docker_run_opts+=("-v" "$DATASTORE:/srv/datastore/data:rw")
  106. docker_run_opts+=("-e" "DATASTORE=/srv/datastore/data")
  107. fi
  108. fi
  109. ## CONFIGSTORE
  110. if [ "${CONFIGSTORE+x}" ]; then
  111. if [ -d "${CONFIGSTORE}" ]; then
  112. docker_run_opts+=("-v" "$CONFIGSTORE:/srv/datastore/config:rw")
  113. docker_run_opts+=("-e" "CONFIGSTORE=/srv/datastore/config")
  114. fi
  115. fi
  116. COMPOSE_LAUNCHER_BIN=$(readlink -f "${BASH_SOURCE[0]}")
  117. filename=$(mktemp -p /tmp/ -t launch_opts-XXXXXXXXXXXXXXXX)
  118. {
  119. printf "%s\0" "${docker_run_opts[@]}"
  120. } > "$filename"
  121. sha=$(sha256sum "$filename")
  122. sha=${sha:0:64}
  123. dest="/var/lib/compose/sessions/$sha"
  124. {
  125. printf "%s\0" "-v" "$dest:$dest"
  126. printf "%s\0" "-e" "COMPOSE_LAUNCHER_OPTS=$dest"
  127. printf "%s\0" "-e" "COMPOSE_LAUNCHER_BIN=$COMPOSE_LAUNCHER_BIN"
  128. } >> "$filename"
  129. mkdir -p /var/lib/compose/sessions
  130. mv "$filename" "$dest"
  131. echo "$dest"
  132. }
  133. run() {
  134. docker_run_opts=()
  135. if [ -z "$COMPOSE_LAUNCHER_OPTS" ]; then
  136. clean_unused_sessions
  137. COMPOSE_LAUNCHER_OPTS="$(mk_docker_run_options)"
  138. fi
  139. while read-0 opt; do
  140. docker_run_opts+=("$opt")
  141. done < <(cat "$COMPOSE_LAUNCHER_OPTS")
  142. COMPOSE_DOCKER_IMAGE=${COMPOSE_DOCKER_IMAGE:-docker.0k.io/compose}
  143. exec docker run -ti "${docker_run_opts[@]}" "${COMPOSE_DOCKER_IMAGE}" "$@"
  144. }
  145. run "$@"