From fe1abd055998e171b92d0e42df8063cab83a40ed Mon Sep 17 00:00:00 2001 From: Malfurious Date: Tue, 24 Oct 2023 22:05:05 -0400 Subject: acid: Don't use 'cychedelic' in file names As I add new components to the cychedelic system, it would be best to use more specific naming. 'cychedelic' is the name of the project, whereas 'acid' is the name of this program. Signed-off-by: Malfurious --- acid/Dockerfile | 2 +- acid/acid | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ acid/acid-build | 58 +++++++++++++++++++++++++++++++++++++++++++++ acid/acid-remove | 15 ++++++++++++ acid/acid-source | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ acid/cyche-build | 58 --------------------------------------------- acid/cyche-remove | 15 ------------ acid/cyche-source | 71 ------------------------------------------------------- acid/cychedelic | 68 ---------------------------------------------------- 9 files changed, 213 insertions(+), 213 deletions(-) create mode 100755 acid/acid create mode 100755 acid/acid-build create mode 100755 acid/acid-remove create mode 100755 acid/acid-source delete mode 100755 acid/cyche-build delete mode 100755 acid/cyche-remove delete mode 100755 acid/cyche-source delete mode 100755 acid/cychedelic diff --git a/acid/Dockerfile b/acid/Dockerfile index 1e27e63..7eecebe 100644 --- a/acid/Dockerfile +++ b/acid/Dockerfile @@ -10,4 +10,4 @@ RUN apk --no-cache add \ WORKDIR /app ENV PATH="${PATH}:/app" COPY . . -CMD ["cychedelic"] +CMD ["acid"] diff --git a/acid/acid b/acid/acid new file mode 100755 index 0000000..b4c0e1b --- /dev/null +++ b/acid/acid @@ -0,0 +1,68 @@ +#!/bin/bash + +# cychedelic ACID daemon +# cychedelic is an automated Continuous Deployment solution. See the README for +# more information. + +source config.sh + +now() { + date '+%s' +} + +should_restart() { + curtime=$(now) + runtime=$((curtime-starttime)) + [ $runtime -gt $CYCHE_MAINT_TIME ] +} + +update() { + forceopt="--force-self" + selfopt="--self" + + for line in "${CYCHE_SERVICES[@]}"; do + read -r -a arr <<< "$line" + hash=$(acid-source ${arr[@]:0:3}) + res=$? + + if [ $res -ne 2 ]; then + if [ $res -eq 1 ] || [ "$1" == "$forceopt" ]; then + acid-build "${arr[0]}" "${arr[3]}" "$hash" $selfopt + sleep "$CYCHE_JOB_TIME" + fi + fi + + forceopt="--force" + selfopt="" + done +} + +remove() { + next=$(printf '%s\n' "${CYCHE_SERVICES[@]}" | awk '{print $1}') + prev=$(ls -A '/services') + removed=$(printf '%s\n' "$next" "$next" "$prev" | sort | uniq -u) + + for name in $removed; do + acid-remove "$name" + sleep "$CYCHE_JOB_TIME" + done +} + +# startup: A restart of the ACID daemon usually indicates an elapse of the +# $CYCHE_MAINT_TIME period, so rebuild services and perform cleanup now. We +# sleep briefly at the beginning to allow the previous cychedelic instance time +# to die. +sleep "$CYCHE_STARTUP_TIME" +remove +update --force +docker system prune --force --all +starttime=$(now) + +# monitor source changes +while ! should_restart; do + sleep "$CYCHE_POLL_TIME" + update +done + +# force self-upgrade to trigger maintenance activities +update --force-self diff --git a/acid/acid-build b/acid/acid-build new file mode 100755 index 0000000..fd5b69d --- /dev/null +++ b/acid/acid-build @@ -0,0 +1,58 @@ +#!/bin/bash -e + +# acid-build [--self] +# +# (Re)build and deploy the service using the docker-compose configuration +# in (usually, docker-compose.yml). +# +# In the typical case, this is a straightforward process and `docker compose up` +# can properly recreate/start containers for the guest service. However, when +# rebuilding cychedelic itself, `--self` must be given, which modifies this +# script to support self-upgrade of the running compose service. +# +# In `--self` mode, we prepare and start the cychedelic service under an +# alternate project name each time, before taking down the existing (old) +# services. This is necessary because running `docker compose up` terminates +# old containers before starting new ones. Termination of the ACID service +# interrupts the `compose up` process before it can start the replacements. +# Normally, `docker compose down` is then not needed. +# +# We take (from acid-source) so we may commit it to the cache iff the +# deployment is successful. +# +# This file exits early on any failure (non-zero). On a successful run in +# `--self` mode, this file does not exit (we are killed by `compose down`). + +cd "/services/$1" + +# For , no absolute paths and no '../' +if ! echo "$2" | grep -Evq '\.\.|^/'; then + echo "Bad file path: $2" + exit 1 +fi + +if [ "$4" == "--self" ]; then + prev=$(cat '.git/previous_slug') + [ -z "$prev" ] && prev="$1" + name=$(echo -n "$1-"; tr -dc a-z .git/previous_hash + +if [ "$4" == "--self" ]; then + echo "$name" >.git/previous_slug + docker compose --project-name "$1" down + docker compose --project-name "$prev" down +fi diff --git a/acid/acid-remove b/acid/acid-remove new file mode 100755 index 0000000..6094a23 --- /dev/null +++ b/acid/acid-remove @@ -0,0 +1,15 @@ +#!/bin/bash + +# acid-remove +# +# Stop the service , which was previously managed by cychedelic, and +# remove associated files. + +name=$(cat "/services/$1/.git/previous_slug") +[ -z "$name" ] && name="$1" + +docker compose --project-name "$name" down + +echo "Removing files..." +rm -rf "/services/$1" +echo "Done." diff --git a/acid/acid-source b/acid/acid-source new file mode 100755 index 0000000..2d507d6 --- /dev/null +++ b/acid/acid-source @@ -0,0 +1,71 @@ +#!/bin/bash + +# acid-source +# +# Check a git source for new updates. A local cache repository is cloned if +# doesn't exist. If updates are found, we attempt to checkout the newest +# version according to (see from git-for-each-ref(1)). +# +# On a successful run, the new object hash is printed to stdout (or the previous +# hash, if no update occurred). This may be a commit hash. However, if +# matches an annotated tag, then the hash refers to the tag object. +# The caller should cache this hash value upon successful deployment. +# +# If a new version of the file tree is checked out, this file exits with code 1, +# otherwise a stale success exits with 0. On any error, no hash is printed, and +# we exit with 2. + +gethash() { + git for-each-ref \ + --count=1 \ + --format='%(objectname)' \ + --sort='-creatordate' \ + "$1" # pattern +} + +cd '/services' + +# For , limit to a-z, 0-9, -, _ +if ! echo "$1" | grep -Eq '^[-_a-z0-9]*$'; then + echo "Bad service name: $1" >&2 + exit 2 +fi + +if ! [ -d "$1" ]; then + git clone "$2" "$1" >&2 || exit 2 + touch "$1/.git/previous_hash" + touch "$1/.git/previous_slug" +fi + +cd "$1" + +git remote set-url origin "$2" >&2 # pick up url changes +git fetch --all --prune >&2 || exit 2 + +prev=$(cat '.git/previous_hash') +next=$(gethash "$3") + +if [ -z "$next" ]; then + echo "No refs match $3" >&2 + exit 2 +fi + +echo "$next" + +if [ "$prev" != "$next" ]; then + # update file tree + git submodule deinit --all --force >/dev/null 2>&1 + git reset . >/dev/null 2>&1 + git checkout . >/dev/null 2>&1 + git clean -xffd >/dev/null 2>&1 + + git checkout "$next" >/dev/null 2>&1 + git submodule update --init --recursive >&2 + + echo -n "Checked out " >&2 + git log -1 --oneline --no-abbrev-commit --no-decorate >&2 + + exit 1 +fi + +exit 0 diff --git a/acid/cyche-build b/acid/cyche-build deleted file mode 100755 index 6468276..0000000 --- a/acid/cyche-build +++ /dev/null @@ -1,58 +0,0 @@ -#!/bin/bash -e - -# cyche-build [--self] -# -# (Re)build and deploy the service using the docker-compose configuration -# in (usually, docker-compose.yml). -# -# In the typical case, this is a straightforward process and `docker compose up` -# can properly recreate/start containers for the guest service. However, when -# rebuilding cychedelic itself, `--self` must be given, which modifies this -# script to support self-upgrade of the running compose service. -# -# In `--self` mode, we prepare and start the cychedelic service under an -# alternate project name each time, before taking down the existing (old) -# services. This is necessary because running `docker compose up` terminates -# old containers before starting new ones. Termination of the ACID service -# interrupts the `compose up` process before it can start the replacements. -# Normally, `docker compose down` is then not needed. -# -# We take (from cyche-source) so we may commit it to the cache iff the -# deployment is successful. -# -# This file exits early on any failure (non-zero). On a successful run in -# `--self` mode, this file does not exit (we are killed by `compose down`). - -cd "/services/$1" - -# For , no absolute paths and no '../' -if ! echo "$2" | grep -Evq '\.\.|^/'; then - echo "Bad file path: $2" - exit 1 -fi - -if [ "$4" == "--self" ]; then - prev=$(cat '.git/previous_slug') - [ -z "$prev" ] && prev="$1" - name=$(echo -n "$1-"; tr -dc a-z .git/previous_hash - -if [ "$4" == "--self" ]; then - echo "$name" >.git/previous_slug - docker compose --project-name "$1" down - docker compose --project-name "$prev" down -fi diff --git a/acid/cyche-remove b/acid/cyche-remove deleted file mode 100755 index c988efe..0000000 --- a/acid/cyche-remove +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash - -# cyche-remove -# -# Stop the service , which was previously managed by cychedelic, and -# remove associated files. - -name=$(cat "/services/$1/.git/previous_slug") -[ -z "$name" ] && name="$1" - -docker compose --project-name "$name" down - -echo "Removing files..." -rm -rf "/services/$1" -echo "Done." diff --git a/acid/cyche-source b/acid/cyche-source deleted file mode 100755 index 7dca756..0000000 --- a/acid/cyche-source +++ /dev/null @@ -1,71 +0,0 @@ -#!/bin/bash - -# cyche-source -# -# Check a git source for new updates. A local cache repository is cloned if -# doesn't exist. If updates are found, we attempt to checkout the newest -# version according to (see from git-for-each-ref(1)). -# -# On a successful run, the new object hash is printed to stdout (or the previous -# hash, if no update occurred). This may be a commit hash. However, if -# matches an annotated tag, then the hash refers to the tag object. -# The caller should cache this hash value upon successful deployment. -# -# If a new version of the file tree is checked out, this file exits with code 1, -# otherwise a stale success exits with 0. On any error, no hash is printed, and -# we exit with 2. - -gethash() { - git for-each-ref \ - --count=1 \ - --format='%(objectname)' \ - --sort='-creatordate' \ - "$1" # pattern -} - -cd '/services' - -# For , limit to a-z, 0-9, -, _ -if ! echo "$1" | grep -Eq '^[-_a-z0-9]*$'; then - echo "Bad service name: $1" >&2 - exit 2 -fi - -if ! [ -d "$1" ]; then - git clone "$2" "$1" >&2 || exit 2 - touch "$1/.git/previous_hash" - touch "$1/.git/previous_slug" -fi - -cd "$1" - -git remote set-url origin "$2" >&2 # pick up url changes -git fetch --all --prune >&2 || exit 2 - -prev=$(cat '.git/previous_hash') -next=$(gethash "$3") - -if [ -z "$next" ]; then - echo "No refs match $3" >&2 - exit 2 -fi - -echo "$next" - -if [ "$prev" != "$next" ]; then - # update file tree - git submodule deinit --all --force >/dev/null 2>&1 - git reset . >/dev/null 2>&1 - git checkout . >/dev/null 2>&1 - git clean -xffd >/dev/null 2>&1 - - git checkout "$next" >/dev/null 2>&1 - git submodule update --init --recursive >&2 - - echo -n "Checked out " >&2 - git log -1 --oneline --no-abbrev-commit --no-decorate >&2 - - exit 1 -fi - -exit 0 diff --git a/acid/cychedelic b/acid/cychedelic deleted file mode 100755 index a73554a..0000000 --- a/acid/cychedelic +++ /dev/null @@ -1,68 +0,0 @@ -#!/bin/bash - -# cychedelic ACID daemon -# cychedelic is an automated Continuous Deployment solution. See the README for -# more information. - -source config.sh - -now() { - date '+%s' -} - -should_restart() { - curtime=$(now) - runtime=$((curtime-starttime)) - [ $runtime -gt $CYCHE_MAINT_TIME ] -} - -update() { - forceopt="--force-self" - selfopt="--self" - - for line in "${CYCHE_SERVICES[@]}"; do - read -r -a arr <<< "$line" - hash=$(cyche-source ${arr[@]:0:3}) - res=$? - - if [ $res -ne 2 ]; then - if [ $res -eq 1 ] || [ "$1" == "$forceopt" ]; then - cyche-build "${arr[0]}" "${arr[3]}" "$hash" $selfopt - sleep "$CYCHE_JOB_TIME" - fi - fi - - forceopt="--force" - selfopt="" - done -} - -remove() { - next=$(printf '%s\n' "${CYCHE_SERVICES[@]}" | awk '{print $1}') - prev=$(ls -A '/services') - removed=$(printf '%s\n' "$next" "$next" "$prev" | sort | uniq -u) - - for name in $removed; do - cyche-remove "$name" - sleep "$CYCHE_JOB_TIME" - done -} - -# startup: A restart of the ACID daemon usually indicates an elapse of the -# $CYCHE_MAINT_TIME period, so rebuild services and perform cleanup now. We -# sleep briefly at the beginning to allow the previous cychedelic instance time -# to die. -sleep "$CYCHE_STARTUP_TIME" -remove -update --force -docker system prune --force --all -starttime=$(now) - -# monitor source changes -while ! should_restart; do - sleep "$CYCHE_POLL_TIME" - update -done - -# force self-upgrade to trigger maintenance activities -update --force-self -- cgit v1.2.3