From 102e7cbabf0e85b2f8c1ca13a250e6e8eb15189b Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Thu, 15 Jun 2023 15:23:41 +0200 Subject: [PATCH] new: [etherpad] add ``list`` and ``drop`` actions to list pads and drop a given pad --- etherpad/README.org | 13 +++++ etherpad/actions/drop | 63 ++++++++++++++++++++++++ etherpad/actions/list | 64 ++++++++++++++++++++++++ etherpad/lib/common | 111 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 251 insertions(+) create mode 100755 etherpad/actions/drop create mode 100755 etherpad/actions/list create mode 100644 etherpad/lib/common diff --git a/etherpad/README.org b/etherpad/README.org index 0d0101e..c10f0fb 100644 --- a/etherpad/README.org +++ b/etherpad/README.org @@ -31,3 +31,16 @@ and install plugins but this will not allow to reproduce an install easily. We can do this on the =compose.yml= side in a reproducible manner. +* Actions + +** list all pads + +#+begin_src sh +compose list etherpad +#+end_src + +** drop a pad + +#+begin_src sh +compose drop etherpad PAD_ID +#+end_src diff --git a/etherpad/actions/drop b/etherpad/actions/drop new file mode 100755 index 0000000..964d24a --- /dev/null +++ b/etherpad/actions/drop @@ -0,0 +1,63 @@ +#!/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 + + +version=0.1 +usage="$exname [-h|--help] PAD_ID" +help=" +USAGE: + +$usage + +DESCRIPTION: + +Delete pad identified by PAD_ID + +EXAMPLES: + + $exname list + +" + + +while [ "$1" ]; do + case "$1" in + "--help"|"-h") + print_help >&2 + exit 0 + ;; + --*|-*) + err "Unexpected optional argument '$1'" + print_usage >&2 + exit 1 + ;; + *) + [ -z "$PAD_ID" ] && { PAD_ID="$1" ; shift ; continue ; } + err "Unexpected positional argument '$1'" + print_usage >&2 + exit 1 + ;; + esac + shift +done + + +. "$CHARM_PATH"/lib/common + +set -e + +if [ -z "$PAD_ID" ]; then + err "Missing first positional argument PAD_ID to specify which pad to drop." + print_usage + exit 1 +fi + +etherpad:api:init || exit 1 + +etherpad:api deletePad --data-urlencode "padID=${PAD_ID}" >/dev/null || exit 1 + diff --git a/etherpad/actions/list b/etherpad/actions/list new file mode 100755 index 0000000..124def6 --- /dev/null +++ b/etherpad/actions/list @@ -0,0 +1,64 @@ +#!/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 + + +version=0.1 +usage="$exname [-h|--help]" +help=" +USAGE: + +$usage + +DESCRIPTION: + +Lists all pads. + +EXAMPLES: + + $exname list + +" + + +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 + + +. "$CHARM_PATH"/lib/common + +set -e + +etherpad:api:init || exit 1 + +out=$(etherpad:api listAllPads) || exit 1 + +if ! pads=$(e "$out" | jq -r ".padIDs[]"); then + err "Unexpected format from call to listAllPads API endpoint for ${DARKYELLOW}${SERVICE_NAME}${NORMAL}." + # echo "$out" | prefix " | " >&2 + exit 1 +fi + +e "$pads" +echo diff --git a/etherpad/lib/common b/etherpad/lib/common new file mode 100644 index 0000000..5840c29 --- /dev/null +++ b/etherpad/lib/common @@ -0,0 +1,111 @@ +# -*- mode: shell-script -*- + + +CONFIG_DIR="$SERVICE_CONFIGSTORE"/etc/etherpad +ORIG_CONFIG_FILE="$CONFIG_DIR/settings.json.orig" + +etherpad:ensure-orig-json-exists() { + if ! [ -e "$ORIG_CONFIG_FILE" ]; then + mkdir -p "${ORIG_CONFIG_FILE%/*}" || return 1 + ## Remove comments from container file and copy it in + ## configstore + if out=$(docker run --rm --entrypoint bash "$DOCKER_BASE_IMAGE" \ + -c "npm install mjson && node_modules/.bin/mjson.js < settings.json"); then + ## XXXvlab: output from mjson.js is still minified in my local install + ## lets keep this code if necessary later. + # if ! echo "$out" | jq . > "$ORIG_CONFIG_FILE"; then + # err "Jq failed with output from container." + # rm -fv "$ORIG_CONFIG_FILE" + # return 1 + # fi + echo "$out" > "$ORIG_CONFIG_FILE" + else + err "Could not out-source clean 'setting.json' from container." + return 1 + fi + fi +} + + +etherpad:api:init() { + container_network_ip=$(get_healthy_container_ip_for_service "$SERVICE_NAME" 9001 4) || { + echo " Please ensure that ${DARKYELLOW}${SERVICE_NAME}${NORMAL} is running before using '$exname'." >&2 + return 1 + } + + container_ip=${container_network_ip##*:} + container_network=${container_network_ip%%:*} + + export ETHERPAD_DOCKER_OPTS=(--network "$container_network") + + if ! API_KEY=$(cat "$SERVICE_DATASTORE/var/lib/etherpad/APIKEY.txt"); then + err "Can't find API KEY for ${DARKYELLOW}${SERVICE_NAME}${NORMAL}." + return 1 + fi + + export DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl} + export ETHERPAD_HOST="http://${container_ip}:9001" + + if ! out=$(etherpad:curl api/openapi.json); then + err "Couldn't get API description from ${DARKYELLOW}${SERVICE_NAME}${NORMAL}." + return 1 + fi + + if ! api_version=$(e "$out" | jq -r '.info.version'); then + err "Couldn't get version information from ${DARKYELLOW}${SERVICE_NAME}${NORMAL} API's description." + # echo "$out" | prefix " | " >&2 + return 1 + fi + + export ETHERPAD_API_BASE_PATH="api/${api_version}" +} + +etherpad:curl() { + local path="$1" cmd + shift + cmd=( + docker run --rm "${ETHERPAD_DOCKER_OPTS[@]}" + "$DEFAULT_CURL_IMAGE" + -sS + "$@" + "${ETHERPAD_HOST}/$path" + ) + + # debug "Launching: ${cmd[@]}" + "${cmd[@]}" +} + +etherpad:api() { + local method="$1" code out + shift + + if [ -z "$ETHERPAD_API_BASE_PATH" ]; then + etherpad:api:init + fi + + if ! out=$( + etherpad:curl "${ETHERPAD_API_BASE_PATH}/$method" -X POST -d "apikey=${API_KEY}" "$@" + ); then + err "Couldn't query API ${WHITE}$method${NORMAL} from ${DARKYELLOW}${SERVICE_NAME}${NORMAL}." + return 1 + fi + if ! code=$(e "$out" | jq -r ".code"); then + err "Unexpected response format from call to ${WHITE}$method${NORMAL} " \ + "API endpoint for ${DARKYELLOW}${SERVICE_NAME}${NORMAL}." + # echo "$out" | prefix " | " >&2 + exit 1 + fi + if [[ "$code" != "0" ]]; then + if ! message=$(e "$out" | jq -r ".message"); then + err "Unexpected response format from call to ${WHITE}$method${NORMAL} " \ + "API endpoint for ${DARKYELLOW}${SERVICE_NAME}${NORMAL}." + # echo "$out" | prefix " | " >&2 + exit 1 + fi + err "${WHITE}$method${NORMAL}: $message" + # echo "$out" | prefix " | " >&2 + exit 1 + fi + + e "$out" | jq '.data' +} \ No newline at end of file