Skip to content

Commit

Permalink
Merge pull request #10986 from hmislk/development
Browse files Browse the repository at this point in the history
Update to Latest
  • Loading branch information
DARKDRAGON-LK authored Mar 8, 2025
2 parents ba1782e + 105168a commit 7de1d74
Show file tree
Hide file tree
Showing 286 changed files with 18,350 additions and 5,205 deletions.
169 changes: 169 additions & 0 deletions .github/scripts/db_export_import_scheduler.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
#!/bin/bash

# Ensure script is executed with two arguments
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <fromEnv> <toEnv>"
exit 1
fi

FROM_ENV=$1
TO_ENV=$2

CONFIG_FILE="/home/azureuser/utils/secrets/server_config.json"

# Ensure the JSON file exists
if [[ ! -f "$CONFIG_FILE" ]]; then
echo "Error: Configuration file $CONFIG_FILE not found."
exit 1
fi

# Ensure TO_ENV is either QA or COOP_Test
if [[ "$TO_ENV" != "QA" && "$TO_ENV" != "COOP_Test" ]]; then
echo "Error: TO_ENV must be QA or COOP_Test for now."
exit 1
fi

FROM_SSH_KEY=$(jq -r ".server_ssh_keys[\"$FROM_ENV\"]" "$CONFIG_FILE")
TO_SSH_KEY=$(jq -r ".server_ssh_keys[\"$TO_ENV\"]" "$CONFIG_FILE")
FROM_SERVER_IP=$(jq -r ".server_ips[\"$FROM_ENV\"]" "$CONFIG_FILE")
TO_SERVER_IP=$(jq -r ".server_ips[\"$TO_ENV\"]" "$CONFIG_FILE")
FROM_DB_IP=$(jq -r ".db_ips[\"$FROM_ENV\"]" "$CONFIG_FILE")
TO_DB_IP=$(jq -r ".db_ips[\"$TO_ENV\"]" "$CONFIG_FILE")
FROM_DB_USERNAME=$(jq -r ".db_usernames[\"$FROM_ENV\"]" "$CONFIG_FILE")
TO_DB_USERNAME=$(jq -r ".db_usernames[\"$TO_ENV\"]" "$CONFIG_FILE")
FROM_DB_PASSWORD=$(jq -r ".db_passwords[\"$FROM_ENV\"]" "$CONFIG_FILE")
TO_DB_PASSWORD=$(jq -r ".db_passwords[\"$TO_ENV\"]" "$CONFIG_FILE")
FROM_DB_NAME=$(jq -r ".db_names[\"$FROM_ENV\"]" "$CONFIG_FILE")
TO_DB_NAME=$(jq -r ".db_names[\"$TO_ENV\"]" "$CONFIG_FILE")

# Ensure SSH key files exist
if [[ ! -f "$FROM_SSH_KEY" || ! -f "$TO_SSH_KEY" ]]; then
echo "Error: One or more SSH key files are missing."
exit 1
fi

echo "Dumping database from $FROM_ENV and replacing in $TO_ENV on date $DATE"

# Function to manage backup files on a given server
manage_backup() {
local SERVER_IP=$1
local SSH_KEY=$2
local DB_IP=$3
local DB_USERNAME=$4
local DB_PASSWORD=$5
local DB_NAME=$6

ssh -o StrictHostKeyChecking=no -i "$SSH_KEY" azureuser@"$SERVER_IP" \
DB_IP="$DB_IP" DB_USERNAME="$DB_USERNAME" DB_PASSWORD="$DB_PASSWORD" DB_NAME="$DB_NAME" 'bash -s' << 'EOF'
if [ ! -d /opt/db_export_import_backups ]; then
sudo mkdir -p /opt/db_export_import_backups
sudo chown azureuser:azureuser /opt/db_export_import_backups
fi
mkdir -p /opt/db_export_import_backups/myBackup /opt/db_export_import_backups/importedBackup
if [ -f /opt/db_export_import_backups/myBackup/backup.sql ]; then
mv /opt/db_export_import_backups/myBackup/backup.sql /opt/db_export_import_backups/myBackup/backup-old.sql
fi
# DB dump command
mysqldump -h "$DB_IP" -u "$DB_USERNAME" -p"$DB_PASSWORD" "$DB_NAME" > /opt/db_export_import_backups/myBackup/backup.sql
sudo chown azureuser:azureuser /opt/db_export_import_backups/myBackup/backup.sql
sudo chmod 644 /opt/db_export_import_backups/myBackup/backup.sql
sudo chown -R azureuser:azureuser /opt/db_export_import_backups
EOF
}
# Log into QA and Dev servers to rename /opt/db_export_import_backups contents
restore_database() {
local SERVER_IP=$1
local SSH_KEY=$2
local DB_IP=$3
local DB_USERNAME=$4
local DB_PASSWORD=$5
local DB_NAME=$6

echo "Restoring database $DB_NAME on $SERVER_IP..."

ssh -o StrictHostKeyChecking=no -i "$SSH_KEY" azureuser@"$SERVER_IP" \
DB_IP="$DB_IP" DB_USERNAME="$DB_USERNAME" DB_PASSWORD="$DB_PASSWORD" DB_NAME="$DB_NAME" 'bash -s' << 'EOF'
# Drop and recreate the database
echo "Dropping existing database if it exists..."
mysql -h "$DB_IP" -u "$DB_USERNAME" -p"$DB_PASSWORD" -e "DROP DATABASE IF EXISTS \`$DB_NAME\`;"
echo "Creating new database..."
mysql -h "$DB_IP" -u "$DB_USERNAME" -p"$DB_PASSWORD" -e "CREATE DATABASE \`$DB_NAME\`;"
echo "Importing backup into $DB_NAME..."
mysql -h "$DB_IP" -u "$DB_USERNAME" -p"$DB_PASSWORD" "$DB_NAME" < /opt/db_export_import_backups/importedBackup/backup.sql
echo "Database $DB_NAME restored successfully!"
EOF
}

# Function to send an email
send_email() {
local SUBJECT=$1
local BODY=$2
shift 2 # Remove first two arguments and keep the rest as recipients
local RECIPIENTS=("$@")
local EMAIL_API_URL="http://localhost:8081/messenger/api/email/send"

# Convert recipients array to JSON format
local EMAIL_JSON
EMAIL_JSON=$(printf '[%s]' "$(printf '"%s",' "${RECIPIENTS[@]}" | sed 's/,$//')")

echo "Sending email notification..."
curl -X POST "$EMAIL_API_URL" \
-H "Content-Type: application/json" \
-d '{
"subject": "'"$SUBJECT"'",
"body": "'"$BODY"'",
"recipients": '"$EMAIL_JSON"',
"isHtml": false
}'

echo "Email sent successfully!"
}

TRANSFER_SUCCESS=false
RESTORE_SUCCESS=false

# Manage backups on source and target servers
if manage_backup "$FROM_SERVER_IP" "$FROM_SSH_KEY" "$FROM_DB_IP" "$FROM_DB_USERNAME" "$FROM_DB_PASSWORD" "$FROM_DB_NAME" &&
manage_backup "$TO_SERVER_IP" "$TO_SSH_KEY" "$TO_DB_IP" "$TO_DB_USERNAME" "$TO_DB_PASSWORD" "$TO_DB_NAME"; then
echo "Backup management successful."
else
echo "Backup management failed."
send_email "Database Restore Failed" "Backup management failed while transferring from $FROM_ENV to $TO_ENV." "[email protected]" "[email protected]" "[email protected]" "[email protected]"
exit 1
fi

# Copy backup file from source to target
echo "Transferring backup file..."
if scp -o StrictHostKeyChecking=no -i "$FROM_SSH_KEY" azureuser@"$FROM_SERVER_IP":/opt/db_export_import_backups/myBackup/backup.sql /home/azureuser/backup.sql &&
scp -o StrictHostKeyChecking=no -i "$TO_SSH_KEY" /home/azureuser/backup.sql azureuser@"$TO_SERVER_IP":/opt/db_export_import_backups/importedBackup/backup.sql; then
TRANSFER_SUCCESS=true
else
send_email "Database Restore Failed" "Backup file transfer from $FROM_ENV to $TO_ENV failed." "[email protected]" "[email protected]" "[email protected]" "[email protected]"
exit 1
fi

# Restore database on target server
if restore_database "$TO_SERVER_IP" "$TO_SSH_KEY" "$TO_DB_IP" "$TO_DB_USERNAME" "$TO_DB_PASSWORD" "$TO_DB_NAME"; then
RESTORE_SUCCESS=true
else
send_email "Database Restore Failed" "Database restoration on $TO_ENV failed." "[email protected]" "[email protected]" "[email protected]" "[email protected]"
exit 1
fi

# Cleanup
rm -f /home/azureuser/backup.sql

if $TRANSFER_SUCCESS && $RESTORE_SUCCESS; then
send_email "Database Restore Completed" \
"Database backup successfully transferred from $FROM_ENV to $TO_ENV." \
"[email protected]" "[email protected]" "[email protected]" "[email protected]"
fi

echo "Database backup successfully transferred from $FROM_ENV to $TO_ENV."
101 changes: 101 additions & 0 deletions .github/scripts/restart_all_servers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/bin/bash

# Ensure script is executed with one argument
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <excludedServers>"
exit 1
fi

# Check if logged into Azure
if ! az account show &> /dev/null; then
printf "Not logged into Azure. Attempting to login...\n"
if ! az login --identity &> /dev/null; then
printf "Azure login failed. Please login manually and retry.\n"
exit 1
fi
printf "Azure login successful.\n"
fi

EXCLUDED_SERVERS=$1

CONFIG_FILE="/home/azureuser/utils/secrets/server_config.json"

# Ensure the JSON file exists
if [[ ! -f "$CONFIG_FILE" ]]; then
echo "Error: Configuration file $CONFIG_FILE not found."
exit 1
fi

IFS=',' read -r -a EXCLUDED_ARRAY <<< "$EXCLUDED_SERVERS"

# Define the included servers
INCLUDED_ARRAY=("Development(4.240.39.63)" "QA(4.240.43.211)")

# Remove excluded servers from included array
for excluded in "${EXCLUDED_ARRAY[@]}"; do
for i in "${!INCLUDED_ARRAY[@]}"; do
if [[ "${INCLUDED_ARRAY[i]}" == *"$excluded"* ]]; then
unset 'INCLUDED_ARRAY[i]'
fi
done
done

echo "Excluded Servers:"
for server in "${EXCLUDED_ARRAY[@]}"; do
echo "$server"
done

echo "Included Servers:"
for server in "${INCLUDED_ARRAY[@]}"; do
echo "$server"
done

SUCCESS_FILE=$(mktemp)
FAILED_FILE=$(mktemp)

restart_vm() {
local SERVER_NAME=$1

local VM_NAME
VM_NAME=$(jq -r ".vm_names[\"$SERVER_NAME\"]" "$CONFIG_FILE")

local RESOURCE_GROUP
RESOURCE_GROUP=$(jq -r ".vm_resource_groups[\"$SERVER_NAME\"]" "$CONFIG_FILE")

if [[ -z "$VM_NAME" || "$VM_NAME" == "null" ]]; then
echo "Error: Could not retrieve VM name for $SERVER_NAME."
echo "$SERVER_NAME" >> "$FAILED_FILE"
return 1
fi

if [[ -z "$RESOURCE_GROUP" || "$RESOURCE_GROUP" == "null" ]]; then
echo "Error: Could not retrieve resource group for $SERVER_NAME."
echo "$SERVER_NAME" >> "$FAILED_FILE"
return 1
fi

echo "Restarting VM $VM_NAME in resource group $RESOURCE_GROUP..."
if az vm restart --name "$VM_NAME" --resource-group "$RESOURCE_GROUP" --no-wait; then
echo "$SERVER_NAME" >> "$SUCCESS_FILE"
else
echo "$SERVER_NAME" >> "$FAILED_FILE"
fi &
}

# Restart each VM in the included servers list in parallel
for server in "${INCLUDED_ARRAY[@]}"; do
restart_vm "$server"
done

wait

printf "\nRestarted VMs:\n"
cat "$SUCCESS_FILE"

printf "\nFailed to restart VMs:\n"
cat "$FAILED_FILE"

# Clean up temporary files
rm -f "$SUCCESS_FILE" "$FAILED_FILE"

printf "\nOperation completed successfully."
81 changes: 81 additions & 0 deletions .github/scripts/restart_individual_servers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/bin/bash

# Ensure script is executed with two arguments
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <selectedAction> <selectedServer>"
exit 1
fi

SELECTED_ACTION=$1
SELECTED_SERVER=$2

CONFIG_FILE="/home/azureuser/utils/secrets/server_config.json"

# Ensure the JSON file exists
if [[ ! -f "$CONFIG_FILE" ]]; then
echo "Error: Configuration file $CONFIG_FILE not found."
exit 1
fi

SERVER_IP=$(jq -r ".vm_ips[\"$SELECTED_SERVER\"]" "$CONFIG_FILE")
SERVER_SSH_KEY=$(jq -r ".vm_ssh_keys[\"$SELECTED_SERVER\"]" "$CONFIG_FILE")
SERVER_NAME=$(jq -r ".vm_names[\"$SELECTED_SERVER\"]" "$CONFIG_FILE")
SERVER_RESOURCE_GROUP=$(jq -r ".vm_resource_groups[\"$SELECTED_SERVER\"]" "$CONFIG_FILE")

# Ensure required values are not empty
if [[ -z "$SERVER_IP" || "$SERVER_IP" == "null" ]]; then
echo "Error: No IP found for server '$SELECTED_SERVER'. Check configuration."
exit 1
fi

if [[ -z "$SERVER_SSH_KEY" || "$SERVER_SSH_KEY" == "null" ]]; then
echo "Error: No SSH key found for server '$SELECTED_SERVER'. Check configuration."
exit 1
fi

# Ensure SSH key file exists
if [[ ! -f "$SERVER_SSH_KEY" ]]; then
echo "Error: SSH key file '$SERVER_SSH_KEY' not found."
exit 1
fi

echo "Executing action '$SELECTED_ACTION' on server '$SELECTED_SERVER' ($SERVER_IP)..."

if [[ "$SELECTED_ACTION" == "NGINX" || "$SELECTED_ACTION" == "PAYARA" ]]; then
ssh -o StrictHostKeyChecking=no -i "$SERVER_SSH_KEY" azureuser@"$SERVER_IP" "
if [[ '$SELECTED_ACTION' == 'NGINX' ]]; then
echo 'Reloading NGINX...'
sudo systemctl reload nginx && echo 'NGINX reloaded successfully' || echo 'NGINX reload failed'
elif [[ '$SELECTED_ACTION' == 'PAYARA' ]]; then
echo 'Restarting Payara...'
sudo systemctl restart payara_domain1.service && echo 'Payara restarted successfully' || echo 'Payara restart failed'
else
echo 'Error: Unknown action.'
exit 1
fi
"
elif [ "$SELECTED_ACTION" == "VM" ]; then
echo "Restarting VM..."
# Check if logged into Azure
if ! az account show &> /dev/null; then
echo "Not logged into Azure. Attempting to login..."
if ! az login --identity &> /dev/null; then
echo "Azure login failed. Please login manually and retry."
exit 1
fi
echo "Azure login successful."
fi

# Restart the VM
if az vm restart --name "$SERVER_NAME" --resource-group "$SERVER_RESOURCE_GROUP" --no-wait; then
echo "VM restart initiated successfully."
else
echo "Error: Failed to restart VM '$SERVER_NAME' in resource group '$SERVER_RESOURCE_GROUP'."
exit 1
fi
else
echo "Error: Unknown action."
exit 1
fi

echo "Operation completed successfully."
Loading

0 comments on commit 7de1d74

Please sign in to comment.