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.

76 lines
2.5 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. config="
  14. $SERVICE_NAME:
  15. environment:
  16. "
  17. if USER_EMAIL=$(echo "$service_def" | shyaml get-value options.email 2>/dev/null); then
  18. config+=" LETSENCRYPT_USER_MAIL: $USER_EMAIL"
  19. fi
  20. if environment_def="$(printf "%s" "$service_def" | shyaml -y get-value options.env 2>/dev/null)"; then
  21. while read-0 key value; do
  22. config+="$(printf "\n %s: %s" "$key" "$value")"
  23. done < <(printf "%s" "$environment_def" | yaml_opt_bash_env_ignore_first_level LEXICON)
  24. if ! provider=$(printf "%s" "$environment_def" | shyaml -y get-value provider 2>/dev/null); then
  25. provider=
  26. ## If no provider is given, we fallback on the first found
  27. while read-0 key value; do
  28. [[ "$(echo "$value" | shyaml get-type)" == "struct" ]] && {
  29. provider="$key"
  30. break
  31. }
  32. done < <(echo "$environment_def" | shyaml key-values-0)
  33. warn "No ${WHITE}provider${NORMAL} key given, had to infer it, chose '$key'."
  34. fi
  35. config+=$(echo -en "\n LEXICON_PROVIDER: $provider")
  36. fi
  37. if ! challenge_type=$(printf "%s" "$service_def" | shyaml get-value "options.challenge-type" 2>/dev/null); then
  38. warn "No ${WHITE}challenge-type${NORMAL} provided, defaulting to 'http'."
  39. challenge_type=http
  40. fi
  41. config+=$(echo -en "\n CHALLENGE_TYPE: $challenge_type")
  42. aimport remainder_args
  43. if [ "$challenge_type" == "http" ] &&
  44. [ "${remainder_args[0]}" == "crt" ] &&
  45. [ "${remainder_args[1]}" == "create" ] &&
  46. ! [ -d "$SERVICE_DATASTORE/etc/letsencrypt/live/${remainder_args[2]}" ]; then
  47. while read container_id; do
  48. docker stop -t 5 "$container_id"
  49. done < <(docker ps \
  50. --filter label="compose.project=$PROJECT_NAME" \
  51. --filter publish=80 \
  52. --format "{{.ID}}"
  53. )
  54. config+=$(echo -en "\n ports:
  55. - \"0.0.0.0:80:80\"")
  56. fi
  57. init-config-add "$config"
  58. mkdir -p "$SERVICE_DATASTORE/etc/letsencrypt"