From c4b66c56696fa43cfebdfa84c1d03d1c85188eea Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Sat, 29 Oct 2022 23:45:45 +0200 Subject: [PATCH] new: [hugo] add ``hugo`` charm Signed-off-by: Valentin Lab --- hugo/actions/generate | 53 +++++++++++++++++++++ hugo/hooks/init | 21 +++++++++ hugo/hooks/pre_deploy | 12 +++++ hugo/hooks/publish_dir-relation-joined | 12 +++++ hugo/lib/common | 65 ++++++++++++++++++++++++++ hugo/metadata.yml | 40 ++++++++++++++++ 6 files changed, 203 insertions(+) create mode 100755 hugo/actions/generate create mode 100755 hugo/hooks/init create mode 100755 hugo/hooks/pre_deploy create mode 100755 hugo/hooks/publish_dir-relation-joined create mode 100644 hugo/lib/common create mode 100644 hugo/metadata.yml diff --git a/hugo/actions/generate b/hugo/actions/generate new file mode 100755 index 0000000..17878e8 --- /dev/null +++ b/hugo/actions/generate @@ -0,0 +1,53 @@ +#!/bin/bash +## compose: no-hooks + +if [ -z "$SERVICE_DATASTORE" ]; then + echo "This script is meant to be run through 'compose' to work properly." >&2 + exit 1 +fi + +. "$CHARM_PATH/lib/common" + + +version=0.1 +usage="$exname [-h|--help]" +help=" +USAGE: + +$usage + +DESCRIPTION: + +Generate website from current content. + +EXAMPLES: + + $exname + +" + + +dbname= +output= +while [ "$1" ]; do + case "$1" in + "--help"|"-h") + print_help >&2 + exit 0 + ;; + --*|-*) + err "Unexpected optional argument '$1'" + print_usage >&2 + exit 1 + ;; + *) + err "Unexpected positional argument '$1'" + print_usage >&2 + exit 1 + ;; + esac + shift +done + + +hugo:generate "$HOST_HUGO_SOURCE" "$HOST_HUGO_OUTPUT" \ No newline at end of file diff --git a/hugo/hooks/init b/hugo/hooks/init new file mode 100755 index 0000000..aec2944 --- /dev/null +++ b/hugo/hooks/init @@ -0,0 +1,21 @@ +#!/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 + + +. lib/common + +set -e + +if ! [ -d "$HOST_HUGO_SOURCE" ]; then + hugo:init-dir || exit 1 +fi + diff --git a/hugo/hooks/pre_deploy b/hugo/hooks/pre_deploy new file mode 100755 index 0000000..bb9060d --- /dev/null +++ b/hugo/hooks/pre_deploy @@ -0,0 +1,12 @@ +#!/bin/bash +## Should be executable N time in a row with same result. + +set -e + +. lib/common + + +if ! [ -d "$HOST_HUGO_OUTPUT" ]; then + hugo:generate "$HOST_HUGO_SOURCE" "$HOST_HUGO_OUTPUT" || exit 1 +fi + diff --git a/hugo/hooks/publish_dir-relation-joined b/hugo/hooks/publish_dir-relation-joined new file mode 100755 index 0000000..45152a2 --- /dev/null +++ b/hugo/hooks/publish_dir-relation-joined @@ -0,0 +1,12 @@ +#!/bin/bash + +. lib/common + +set -e + +url=$(relation-get url) || exit 1 + +mkdir -p "${HOST_HUGO_CONFIG}" +cat < "${HOST_HUGO_CONFIG}"/01-baseurl.yaml +baseURL: $url +EOF diff --git a/hugo/lib/common b/hugo/lib/common new file mode 100644 index 0000000..91ace61 --- /dev/null +++ b/hugo/lib/common @@ -0,0 +1,65 @@ +# -*- mode: shell-script -*- + +HUGO_CONFIG="/etc/hugo" +HUGO_BASE="/opt/apps/hugo" +HUGO_SOURCE="$HUGO_BASE/src" +HUGO_OUTPUT="$HUGO_BASE/output" +HUGO_THEME="$HUGO_BASE/theme" + + +HOST_HUGO_SOURCE="$SERVICE_DATASTORE$HUGO_BASE/src" +HOST_HUGO_OUTPUT="$SERVICE_DATASTORE$HUGO_BASE/output" +HOST_HUGO_THEME="$SERVICE_DATASTORE$HUGO_BASE/theme" +HOST_HUGO_CONFIG="$SERVICE_CONFIGSTORE$HUGO_CONFIG" + +HUGO_BUILDER_DEFAULT_IMAGE="docker.0k.io/hugo:0.88.1" + + +hugo:init-dir() { + mkdir -p "$HOST_HUGO_SOURCE" || return 1 + cd "$HOST_HUGO_SOURCE" && + git config --global user.email "compose@mail.com" && + git config --global user.name "compose" && + docker run --rm -v "$HOST_HUGO_SOURCE":"$HUGO_SOURCE" \ + -w "$HUGO_SOURCE" "$HUGO_BUILDER_DEFAULT_IMAGE" \ + new site -f yaml . && + docker run --rm -v "$HOST_HUGO_SOURCE":"$HUGO_SOURCE" \ + -w "$HUGO_SOURCE" "$HUGO_BUILDER_DEFAULT_IMAGE" \ + new posts/my-first-post.md && + sed -ri '/^baseURL:/d' config.yaml && + git init && + git add -A . && + git commit -m "First import" +} + + +hugo:generate() { + local src="$1" dst="$2" configs c + + theme_url=$(options-get theme-url 2>/dev/null) || true + theme_url=${theme_url:-https://github.com/theNewDynamic/gohugo-theme-ananke.git} + + if ! [ -d "$HOST_HUGO_THEME/default" ]; then + mkdir -p "$HOST_HUGO_THEME" + git clone "${theme_url}" "$HOST_HUGO_THEME/default" || { + err "Could not clone theme '$HOST_HUGO_THEME'." + return 1 + } + fi + echo "theme: default" > "$HOST_HUGO_CONFIG/80-theme.yaml" + config_files=("$HOST_HUGO_CONFIG/"*.yaml) + configs="" + for c in "${config_files[@]}"; do + configs+="config${c#$SERVICE_CONFIGSTORE$HUGO_CONFIG}," + done + mkdir -p "$dst" || return 1 + docker run --rm -v "$src":"$HUGO_SOURCE" \ + -v "$dst":"$HUGO_OUTPUT" \ + -v "$HOST_HUGO_CONFIG":"$HUGO_CONFIG" \ + -v "$HOST_HUGO_THEME":"$HUGO_SOURCE/themes" \ + -v "$HOST_HUGO_CONFIG":"$HUGO_SOURCE/config" \ + -w "$HUGO_SOURCE" "$HUGO_BUILDER_DEFAULT_IMAGE" \ + -D --config "${configs}config.yaml" \ + -d "${dst#$SERVICE_DATASTORE}" && + find "$HOST_HUGO_SOURCE" -depth -type d -empty -delete +} \ No newline at end of file diff --git a/hugo/metadata.yml b/hugo/metadata.yml new file mode 100644 index 0000000..5e1d745 --- /dev/null +++ b/hugo/metadata.yml @@ -0,0 +1,40 @@ +description: Hugo +subordinate: true +requires: + web-publishing-directory: + interface: publish-dir + scope: container +data-resources: + - /opt/apps/hugo/output + +default-options: + # config: + # language-code: "en-us" + # title: "My Hugo Site" + # theme: "ananke" + +uses: + publish-dir: + #constraint: required | recommended | optional + #auto: pair | summon | none ## default: pair + scope: container + constraint: required + auto: summon + solves: + container: "main running server" + default-options: + location: !var-expand "$DATASTORE/$BASE_SERVICE_NAME/opt/apps/hugo/output" + backup: + constraint: recommended + auto: pair + solves: + backup: "Automatic regular backup" + default-options: + ## First pattern matching wins, no pattern matching includes. + ## include-patterns are checked first, then exclude-patterns + ## Patterns rules: + ## - ending / for directory + ## - '*' authorized + ## - must start with a '/', will start from $SERVICE_DATASTORE + exclude-patterns: + - "/opt/apps/hugo/output/"