Files
elabore-charms/zato/lib/common

52 lines
1.7 KiB
Bash

#!/bin/bash
generate_or_get_secret() {
local secret_file="$1"
local secret_value
if ! [ -f "$secret_file" ]; then
info "Generating secret password for ${secret_file##*/}"
mkdir -p "${secret_file%/*}"
umask 077
secret_value=$(openssl rand -hex 32)
echo "$secret_value" > "$secret_file"
else
info "Using existing secret from ${secret_file##*/}"
secret_value=$(cat "$secret_file")
fi
echo "$secret_value"
}
get_container_name(){
containers="$(get_running_containers_for_service "$SERVICE_NAME")"
if [ -z "$containers" ]; then
error "No running containers found for service $SERVICE_NAME"
exit 1
fi
container="$(echo "$containers" | head -n 1)"
echo "$container"
}
# Function to execute all commands sequentially as the zato user inside the Docker container
exec_as_zato_in_container() {
CONTAINER_NAME=$(get_container_name)
local cmd="$1"
if ! docker exec -i "$CONTAINER_NAME" bash -c "su - zato -c '$cmd'"; then
printf "Error: Failed to execute command '%s' as zato user in container '%s'\n" "$cmd" "$CONTAINER_NAME" >&2
return 1
fi
}
## merge certificate for zato HapProxy to handle https API calls
merge_crt_letsencrypt(){
local DOMAIN="$1"
DEST_LETSENCRYPT_FULLCHAIN="$SERVICE_DATASTORE/opt/zato/letsencrypt-fullchain.pem"
mkdir -p "${DEST_LETSENCRYPT_FULLCHAIN%/*}"
cat $DATASTORE/letsencrypt/etc/letsencrypt/live/$DOMAIN/{fullchain,privkey}.pem > "$DEST_LETSENCRYPT_FULLCHAIN" || return 1
info "Letsencrypt {fullchain,privkey}.pem have been concat to /opt/zato/letsencrypt-fullchain.pem for zato hapProxy conf"
}