From 06c91d245f28a17595d16968bece97be5a12d2f4 Mon Sep 17 00:00:00 2001 From: Boris Gallet Date: Tue, 22 Oct 2024 16:23:27 +0200 Subject: [PATCH] new: [zato] Add actions to start, stop and restart Zato --- zato/actions/restart-zato | 25 +++++++++++++++++++++++++ zato/actions/start-zato | 24 ++++++++++++++++++++++++ zato/actions/stop-zato | 21 +++++++++++++++++++++ zato/lib/common | 24 ++++++++++++++++++++++-- 4 files changed, 92 insertions(+), 2 deletions(-) create mode 100755 zato/actions/restart-zato create mode 100755 zato/actions/start-zato create mode 100755 zato/actions/stop-zato diff --git a/zato/actions/restart-zato b/zato/actions/restart-zato new file mode 100755 index 0000000..a29516f --- /dev/null +++ b/zato/actions/restart-zato @@ -0,0 +1,25 @@ +#!/bin/bash + + +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" + +# Combined commands to be run as zato user +local zato_commands=" + /opt/zato/current/bin/zato stop /opt/zato/env/qs-1/server1/ && + cd /opt/zato/env/qs-1 && + ./start-server-fg.sh & +" + +# Execute commands as zato user +if ! exec_as_zato_in_container "$zato_commands"; then + printf "Error: Failed to execute zato commands in container '%s'.\n" "$CONTAINER_NAME" >&2 + return 1 +fi + +printf "Zato restarted successfully in container '%s'.\n" "$CONTAINER_NAME" >&2 + diff --git a/zato/actions/start-zato b/zato/actions/start-zato new file mode 100755 index 0000000..2dc55fd --- /dev/null +++ b/zato/actions/start-zato @@ -0,0 +1,24 @@ +#!/bin/bash + + +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" + +# Combined commands to be run as zato user +local zato_commands=" + cd /opt/zato/env/qs-1 && + ./start-server-fg.sh & +" + +# Execute commands as zato user +if ! exec_as_zato_in_container "$zato_commands"; then + printf "Error: Failed to execute zato commands in container '%s'.\n" "$CONTAINER_NAME" >&2 + return 1 +fi + +printf "Zato started successfully in container '%s'.\n" "$CONTAINER_NAME" >&2 + diff --git a/zato/actions/stop-zato b/zato/actions/stop-zato new file mode 100755 index 0000000..b9a072b --- /dev/null +++ b/zato/actions/stop-zato @@ -0,0 +1,21 @@ +#!/bin/bash + + +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" + +# Combined commands to be run as zato user +local zato_commands="/opt/zato/current/bin/zato stop /opt/zato/env/qs-1/server1/" + +# Execute commands as zato user +if ! exec_as_zato_in_container "$zato_commands"; then + printf "Error: Failed to execute zato commands in container '%s'.\n" "$CONTAINER_NAME" >&2 + return 1 +fi + +printf "Zato stopped successfully in container '%s'.\n" "$CONTAINER_NAME" >&2 + diff --git a/zato/lib/common b/zato/lib/common index 09f18d8..048b792 100644 --- a/zato/lib/common +++ b/zato/lib/common @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/bash generate_or_get_secret() { local secret_file="$1" @@ -16,4 +16,24 @@ generate_or_get_secret() { fi echo "$secret_value" -} \ No newline at end of file +} + +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 +}