From 9411afab5628fe01ca7b0730f98b47a7cb7c9d77 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Tue, 31 Jul 2018 11:40:44 +0200 Subject: [PATCH] new: [letsencrypt] new charm. --- letsencrypt/actions/add | 46 ++++++++++++++++++++++++++ letsencrypt/hooks/init | 71 ++++++++++++++++++++++++++++++++++++++++ letsencrypt/metadata.yml | 7 ++++ 3 files changed, 124 insertions(+) create mode 100755 letsencrypt/actions/add create mode 100755 letsencrypt/hooks/init create mode 100644 letsencrypt/metadata.yml diff --git a/letsencrypt/actions/add b/letsencrypt/actions/add new file mode 100755 index 0000000..8b56f94 --- /dev/null +++ b/letsencrypt/actions/add @@ -0,0 +1,46 @@ +#!/bin/bash + +## Load action gets a first argument a DIRECTORY holding the necessary files. +## +## + +if [ -z "$SERVICE_DATASTORE" ]; then + echo "This script is meant to be run through 'compose' to work properly." >&2 + exit 1 +fi + +usage="$exname [-h|--help] DOMAIN [DOMAIN...]" + +domains=() +while [ "$1" ]; do + case "$1" in + "--help"|"-h") + print_usage + exit 0 + ;; + --*|-*) + err "Unexpected optional argument '$1'" + print_usage + exit 1 + ;; + *) + domains+=("$1") + ;; + esac + shift +done + +if [ -z "${domains[*]}" ]; then + err "You must provide at least one domain as positional argument." + print_usage + exit 1 +fi + +set -e + +## XXXvlab: should check that domain can be declared (with whois, check that the +## registrar is a provider that have config values declared in compose.conf) +mkdir -p "$SERVICE_DATASTORE/etc/letsencrypt" +echo "${domains[@]}" >> "$SERVICE_DATASTORE/etc/letsencrypt/domains.conf" + +info "Added '${domains[*]}' domains to letsencrypt domain lists." diff --git a/letsencrypt/hooks/init b/letsencrypt/hooks/init new file mode 100755 index 0000000..f129b61 --- /dev/null +++ b/letsencrypt/hooks/init @@ -0,0 +1,71 @@ +#!/bin/bash + +## Init is run on host +## For now it is run every time the script is launched, but +## it should be launched only once after build. + +## Accessible variables are: +## - SERVICE_NAME Name of current service +## - DOCKER_BASE_IMAGE Base image from which this service might be built if any +## - SERVICE_DATASTORE Location on host of the DATASTORE of this service +## - SERVICE_CONFIGSTORE Location on host of the CONFIGSTORE of this service + +set -e + +service_def=$(get_compose_service_def "$SERVICE_NAME") + +USER_EMAIL=$(echo "$service_def" | shyaml get-value options.email 2>/dev/null) || { + err "No ${WHITE}email${NORMAL} value in ${DARKYELLOW}$SERVICE_NAME${NORMAL} compose's ${WHITE}options${NORMAL}." + exit 1 +} + +yaml_opt_bash_env() { + local prefix="$1" key value + while read-0 key value; do + new_prefix="${prefix}_${key^^}" + if [[ "$(echo "$value" | shyaml get-type)" == "struct" ]]; then + echo "$value" | yaml_opt_bash_env "${new_prefix}" + else + printf "%s\0%s\0" "${new_prefix}" "$value" + fi + done < <(shyaml key-values-0) +} + +yaml_opt_bash_env_ignore_first_level() { + local prefix="$1" key value + while read-0 key value; do + new_prefix="${prefix}_${key^^}" + if [[ "$(echo "$value" | shyaml get-type)" == "struct" ]]; then + echo "$value" | yaml_opt_bash_env "${new_prefix}" + fi + done < <(shyaml key-values-0) +} + + +config=" +$SERVICE_NAME: + environment: + LETSENCRYPT_USER_MAIL: $USER_EMAIL" + + +while read-0 key value; do + config+="$(printf "\n %s: %s" "$key" "$value")" +done < <(yaml_opt_bash_env_ignore_first_level LEXICON < <(echo "$service_def" | shyaml -y get-value options)) + +## XXXvlab: this is very temporary, we should change image to support more +## than one provider (cf: https://github.com/adferrand/docker-letsencrypt-dns/issues/24) +first_key= +while read-0 key value; do + [[ "$(echo "$value" | shyaml get-type)" == "struct" ]] && { + first_key="$key" + break + } +done < <(echo "$service_def" | shyaml key-values-0 options) + +config+=$(echo -en "\n LEXICON_PROVIDER: $first_key") + +init-config-add "$config" + +mkdir -p "$SERVICE_DATASTORE/etc/letsencrypt" +touch "$SERVICE_DATASTORE/etc/letsencrypt/domains.conf" + diff --git a/letsencrypt/metadata.yml b/letsencrypt/metadata.yml new file mode 100644 index 0000000..fff6298 --- /dev/null +++ b/letsencrypt/metadata.yml @@ -0,0 +1,7 @@ +description: "Let's Encrypt" +maintainer: "Valentin Lab " +## XXXvlab: docker uses the 'build' directory or the 'image:' option here. +docker-image: adferrand/letsencrypt-dns +data-resources: + - /etc/letsencrypt + - /var/log/letsencrypt