fork 0k-charms
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.

78 lines
2.6 KiB

  1. #!/bin/bash
  2. ## Init is run on host
  3. ## For now it is run every time the script is launched, but
  4. ## it should be launched only once after build.
  5. ## Accessible variables are:
  6. ## - SERVICE_NAME Name of current service
  7. ## - DOCKER_BASE_IMAGE Base image from which this service might be built if any
  8. ## - SERVICE_DATASTORE Location on host of the DATASTORE of this service
  9. ## - SERVICE_CONFIGSTORE Location on host of the CONFIGSTORE of this service
  10. . lib/common || exit 1
  11. set -e
  12. service_def=$(get_compose_service_def "$SERVICE_NAME")
  13. USER_EMAIL=$(echo "$service_def" | shyaml get-value options.email 2>/dev/null) || {
  14. err "No ${WHITE}email${NORMAL} value in ${DARKYELLOW}$SERVICE_NAME${NORMAL} compose's ${WHITE}options${NORMAL}."
  15. exit 1
  16. }
  17. config="
  18. $SERVICE_NAME:
  19. environment:
  20. LETSENCRYPT_USER_MAIL: $USER_EMAIL"
  21. if environment_def="$(printf "%s" "$service_def" | shyaml -y get-value options.env 2>/dev/null)"; then
  22. while read-0 key value; do
  23. config+="$(printf "\n %s: %s" "$key" "$value")"
  24. done < <(printf "%s" "$environment_def" | yaml_opt_bash_env_ignore_first_level LEXICON)
  25. if ! provider=$(printf "%s" "$environment_def" | shyaml -y get-value provider 2>/dev/null); then
  26. provider=
  27. ## If no provider is given, we fallback on the first found
  28. while read-0 key value; do
  29. [[ "$(echo "$value" | shyaml get-type)" == "struct" ]] && {
  30. provider="$key"
  31. break
  32. }
  33. done < <(echo "$environment_def" | shyaml key-values-0)
  34. warn "No ${WHITE}provider${NORMAL} key given, had to infer it, chose '$key'."
  35. fi
  36. config+=$(echo -en "\n LEXICON_PROVIDER: $provider")
  37. fi
  38. if ! challenge_type=$(printf "%s" "$service_def" | shyaml get-value "options.challenge-type" 2>/dev/null); then
  39. warn "No ${WHITE}challenge-type${NORMAL} provided, defaulting to 'http'."
  40. challenge_type=http
  41. fi
  42. config+=$(echo -en "\n CHALLENGE_TYPE: $challenge_type")
  43. aimport remainder_args
  44. if [ "$challenge_type" == "http" ] &&
  45. [ "${remainder_args[0]}" == "crt" ] &&
  46. [ "${remainder_args[1]}" == "create" ] &&
  47. ! [ -d "$SERVICE_DATASTORE/etc/letsencrypt/live/${remainder_args[2]}" ]; then
  48. while read container_id; do
  49. docker stop -t 5 "$container_id"
  50. done < <(docker ps \
  51. --filter label="compose.project=$PROJECT_NAME" \
  52. --filter publish=80 \
  53. --format "{{.ID}}"
  54. )
  55. config+=$(echo -en "\n ports:
  56. - \"0.0.0.0:80:80\"")
  57. fi
  58. init-config-add "$config"
  59. mkdir -p "$SERVICE_DATASTORE/etc/letsencrypt"