-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Database swap github action (#10447)
* database swap github action * database swap github action * database swap github action * database swap script * Rename directories used in db import/export scripts * restart nginx and vms action file * restart nginx and vms action file * restart nginx and vms action file * restart individual servers * database swap action changes * script refactors * Add minor changes * restart all servers * restart all servers * db swap changes --------- Co-authored-by: imexh <[email protected]> Co-authored-by: Geeth Sandaru Madhushan <[email protected]>
- Loading branch information
1 parent
8cd3219
commit a745d0a
Showing
6 changed files
with
407 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
#!/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 QA | ||
if [[ "$TO_ENV" != "QA" ]]; then | ||
echo "Error: TO_ENV must be QA 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 | ||
} | ||
|
||
# Manage backups on source and target servers | ||
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" | ||
|
||
# Copy backup file from source to target | ||
echo "Transferring backup file..." | ||
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 | ||
|
||
# Restore database on target server | ||
restore_database "$TO_SERVER_IP" "$TO_SSH_KEY" "$TO_DB_IP" "$TO_DB_USERNAME" "$TO_DB_PASSWORD" "$TO_DB_NAME" | ||
|
||
# Cleanup | ||
rm -f /home/azureuser/backup.sql | ||
|
||
echo "Database backup successfully transferred from $FROM_ENV to $TO_ENV on $DATE." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#!/bin/bash | ||
|
||
if [ "$#" -ne 1 ]; then | ||
echo "Usage: $0 <excludedServers>" | ||
exit 1 | ||
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 | ||
|
||
restart_vm() { | ||
local SERVER_NAME=$1 | ||
|
||
local SERVER_IP | ||
SERVER_IP=$(jq -r ".vm_ips[\"$SERVER_NAME\"]" "$CONFIG_FILE") | ||
|
||
local SSH_KEY | ||
SSH_KEY=$(jq -r ".vm_ssh_keys[\"$SERVER_NAME\"]" "$CONFIG_FILE") | ||
|
||
if [[ -z "$SERVER_IP" || "$SERVER_IP" == "null" ]]; then | ||
echo "Error: Could not retrieve IP for $SERVER_NAME." | ||
echo "VM $SERVER_NAME failed to restart." | ||
return 1 | ||
fi | ||
|
||
if [[ -z "$SSH_KEY" || "$SSH_KEY" == "null" ]]; then | ||
echo "Error: Could not retrieve SSH key for $SERVER_NAME." | ||
echo "VM $SERVER_NAME failed to restart." | ||
return 1 | ||
fi | ||
|
||
echo "Restarting VM $SERVER_NAME on $SERVER_IP..." | ||
|
||
if ! ssh -o StrictHostKeyChecking=no -i "$SSH_KEY" azureuser@"$SERVER_IP" <<EOF | ||
echo 'Restarting VM...' | ||
sudo reboot | ||
EOF | ||
then | ||
echo "VM $SERVER_NAME failed to restart." | ||
return 1 | ||
fi | ||
|
||
echo "VM $SERVER_NAME restarted successfully." | ||
} | ||
|
||
# Restart each VM in the included servers list | ||
for server in "${INCLUDED_ARRAY[@]}"; do | ||
restart_vm "$server" || continue | ||
done | ||
|
||
echo "Operation completed successfully." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/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") | ||
|
||
# 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)..." | ||
|
||
# Perform the operation | ||
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' == 'VM' ]]; then | ||
echo 'Restarting VM...' | ||
sudo reboot | ||
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 | ||
" | ||
|
||
echo "Operation completed successfully." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
name: Database Export and Import Scheduler | ||
|
||
on: | ||
workflow_dispatch: | ||
inputs: | ||
fromEnv: | ||
description: 'Select Environment to Export' | ||
required: true | ||
type: choice | ||
options: | ||
- COOP_Dev | ||
- COOP_Prod | ||
- Ruhunu_Prod | ||
- MP_Prod | ||
toEnv: | ||
description: 'Select Environment to Import' | ||
required: true | ||
type: choice | ||
options: | ||
- QA | ||
date: | ||
description: 'Select Scheduling Date (YYYY-MM-DD)' | ||
required: true | ||
|
||
jobs: | ||
schedule_export_import: | ||
name: Schedule Export & Import | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Run Scheduler on Remote Server | ||
env: | ||
SERVER_IP: ${{ secrets.OBSERVABILITY_SERVER_IP }} | ||
SERVER_SSH_KEY: ${{ secrets.OBSERVABILITY_SSH_PRIVATE_KEY }} | ||
FROM_ENV: ${{ inputs.fromEnv }} | ||
TO_ENV: ${{ inputs.toEnv }} | ||
DATE: ${{ inputs.date }} | ||
run: | | ||
echo "$SERVER_SSH_KEY" > private_key.pem | ||
chmod 600 private_key.pem | ||
UTC_TIME=$(date -d "${DATE}T02:00:00+05:30" -u "+%H:%M %Y-%m-%d") | ||
HOUR=$(echo "$UTC_TIME" | awk '{print $1}' | cut -d':' -f1) | ||
MINUTE=$(echo "$UTC_TIME" | awk '{print $1}' | cut -d':' -f2) | ||
UTC_DATE=$(echo "$UTC_TIME" | awk '{print $2}') | ||
ssh -o StrictHostKeyChecking=no -i private_key.pem azureuser@$SERVER_IP " | ||
echo '/home/azureuser/utils/db_utils/db_export_import_scheduler.sh $FROM_ENV $TO_ENV' | at $HOUR:$MINUTE $UTC_DATE | ||
" | ||
rm private_key.pem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
name: Restart All Servers | ||
|
||
on: | ||
workflow_dispatch: | ||
inputs: | ||
excludeDevelopment: | ||
description: 'Exclude Development(4.240.39.63)?' | ||
required: true | ||
type: boolean | ||
excludeQA: | ||
description: 'Exclude QA(4.240.43.211)?' | ||
required: true | ||
type: boolean | ||
|
||
jobs: | ||
run_script_remote_server: | ||
name: Run Script on Remote Server | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Generate Excluded Servers List | ||
id: exclude_list | ||
run: | | ||
EXCLUDED_SERVERS="" | ||
if [[ "${{ inputs.excludeDevelopment }}" == "true" ]]; then | ||
EXCLUDED_SERVERS+="Development(4.240.39.63)," | ||
fi | ||
if [[ "${{ inputs.excludeQA }}" == "true" ]]; then | ||
EXCLUDED_SERVERS+="QA(4.240.43.211)," | ||
fi | ||
EXCLUDED_SERVERS=${EXCLUDED_SERVERS%,} | ||
echo "EXCLUDED_SERVERS=$EXCLUDED_SERVERS" >> $GITHUB_ENV | ||
- name: Run Script on Remote Server | ||
env: | ||
SERVER_IP: ${{ secrets.OBSERVABILITY_SERVER_IP }} | ||
SERVER_SSH_KEY: ${{ secrets.OBSERVABILITY_SSH_PRIVATE_KEY }} | ||
run: | | ||
echo "$SERVER_SSH_KEY" > private_key.pem | ||
chmod 600 private_key.pem | ||
ssh -o StrictHostKeyChecking=no -i private_key.pem azureuser@$SERVER_IP " | ||
/home/azureuser/utils/server_utils/restart_all_servers.sh \"$EXCLUDED_SERVERS\" | ||
" | ||
rm private_key.pem |
Oops, something went wrong.