Skip to content

Check Servers and Update Configuration #3984

Check Servers and Update Configuration

Check Servers and Update Configuration #3984

name: Check Servers and Update Configuration
on:
schedule:
- cron: "30 * * * *" # Runs every 1 minutes
workflow_dispatch: # Allows manual triggering
jobs:
check-servers:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Check Server Status
id: check_status
run: |
primary_server="http://zimbor.go.ro"
servers=(
"http://peviitor.go.ro"
"http://srvalx.duckdns.org:8983"
"http://zimbor.go.ro:8985"
)
available_server=""
echo "Checking $primary_server..."
response=$(curl -s -L -o /dev/null -w "%{http_code}" "$primary_server" || true)
if [ "$response" -eq 200 ]; then
echo "$primary_server is up and running (HTTP 200 OK)"
else
echo "$primary_server is not available or returned status code $response"
fi
if [ -z "$available_server" ]; then
for server in "${servers[@]}"; do
echo "Checking $server..."
response=$(curl -s -L -o /dev/null -w "%{http_code}" "$server" || true)
if [ "$response" -eq 200 ]; then
echo "$server is up and running (HTTP 200 OK)"
available_server="${server#http://}"
available_server="${available_server%%/*}"
echo "Available server: $available_server"
break
else
echo "$server is not available or returned status code $response"
fi
echo "---------------------------------"
done
fi
echo "available_server=$available_server" >> $GITHUB_ENV
- name: Check Current Active Server
id: check_current_active
run: |
current_active_server=$(curl -s https://api.peviitor.ro/devops/solr/)
current_active_server="${current_active_server#http://}"
current_active_server="${current_active_server%%/*}"
echo "Current_active_server=$current_active_server"
echo "current_active_server=$current_active_server" >> $GITHUB_ENV
- name: Update configuration config.php
id: update_config
run: |
CONFIG_FILE="v3/config.php"
if [ "$current_active_server" != "zimbor.go.ro" ] && [ -n "$available_server" ]; then
if [ "$current_active_server" = "$available_server" ]; then
echo "The current active server is $available_server. No changes needed."
exit 0
fi
echo "Switching to available server: $available_server"
cp $CONFIG_FILE $CONFIG_FILE.bak
sed -i 's/\$server = '\''zimbor.go.ro'\'';/\/\/\$server = '\''zimbor.go.ro'\'';/' $CONFIG_FILE
sed -i "s/\/\/\$server = '$available_server';/\$server = '$available_server';/" $CONFIG_FILE
if ! cmp -s $CONFIG_FILE $CONFIG_FILE.bak; then
git checkout -b update-server-${{ github.sha }}
git config --global user.name "github-actions"
git config --global user.email "github-actions@github.com"
git add $CONFIG_FILE
git commit -m "Switched to $available_server"
git push origin update-server-${{ github.sha }}
echo "needs_update=true" >> $GITHUB_ENV
else
echo "No changes to commit."
echo "needs_update=false" >> $GITHUB_ENV
fi
else
echo "zimbor.go.ro server is available or no change needed. No changes made."
echo "needs_update=false" >> $GITHUB_ENV
fi
- name: Create PR
if: env.needs_update == 'true'
run: |
echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token
echo "Creating PR from update-server-${{ github.sha }} to master..."
gh pr create --title "Update server to $available_server" --body "This PR updates the server configuration to use $available_server." --head update-server-${{ github.sha }} --base master
- name: Approve PR
if: env.needs_update == 'true'
env:
PAT_TOKEN: ${{ secrets.PAT_TOKEN }}
run: |
echo "${{ secrets.PAT_TOKEN }}" | gh auth login --with-token
PR_NUMBER=$(gh pr list --head update-server-${{ github.sha }} --json number --jq '.[0].number')
if [ -n "$PR_NUMBER" ]; then
echo "Approving PR #$PR_NUMBER..."
gh pr review $PR_NUMBER --approve
else
echo "No PR found to approve."
exit 1
fi
- name: Merge PR
if: env.needs_update == 'true'
run: |
echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token
PR_NUMBER=$(gh pr list --head update-server-${{ github.sha }} --json number --jq '.[0].number')
if [ -n "$PR_NUMBER" ]; then
echo "Merging PR #$PR_NUMBER..."
gh pr merge $PR_NUMBER --squash --delete-branch
else
echo "No PR found to merge."
exit 1
fi