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.

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