40 lines
1.2 KiB
Bash
40 lines
1.2 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 -it "$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
|
|
}
|