Skip to content

Commit 63fa90f

Browse files
Merge pull request #6 from CloudNinjaDev/infra
feat: Add MongoDB restore playbook and related roles
2 parents 4275032 + e94c3f3 commit 63fa90f

19 files changed

Lines changed: 2096 additions & 1 deletion

File tree

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
name: Restore Staging MongoDB from Production
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
anonymize_data:
7+
description: 'Anonymize PII data after restore'
8+
required: true
9+
type: boolean
10+
default: true
11+
skip_snapshot:
12+
description: 'Skip snapshot creation (use existing volume)'
13+
required: false
14+
type: boolean
15+
default: false
16+
17+
env:
18+
AWS_REGION: us-west-2
19+
20+
jobs:
21+
restore-mongodb:
22+
name: Clone Production EBS to Staging
23+
runs-on: ubuntu-latest
24+
25+
steps:
26+
- name: Checkout repository
27+
uses: actions/checkout@v3
28+
29+
- name: Configure AWS credentials
30+
uses: aws-actions/configure-aws-credentials@v2
31+
with:
32+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
33+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
34+
aws-region: ${{ env.AWS_REGION }}
35+
36+
- name: Verify AWS authentication
37+
run: |
38+
aws sts get-caller-identity
39+
echo "✅ AWS authentication successful"
40+
41+
- name: Install Ansible
42+
run: |
43+
sudo apt-get update
44+
sudo apt-get install -y ansible
45+
ansible --version
46+
47+
- name: Verify prerequisites
48+
run: |
49+
echo "Checking AWS CLI..."
50+
aws --version
51+
52+
echo "Checking Python..."
53+
python3 --version
54+
55+
echo "✅ All prerequisites met"
56+
57+
- name: Run MongoDB restore playbook
58+
working-directory: ansible/playbook
59+
run: |
60+
ansible-playbook -i staging/inventory mongodb-restore.yml \
61+
-e "anonymize_data=${{ github.event.inputs.anonymize_data }}" \
62+
-v
63+
timeout-minutes: 30
64+
65+
- name: Get restore summary
66+
if: success()
67+
id: summary
68+
run: |
69+
echo "restore_complete=true" >> $GITHUB_OUTPUT
70+
echo "timestamp=$(date -u +'%Y-%m-%d %H:%M:%S UTC')" >> $GITHUB_OUTPUT
71+
72+
- name: Post results to summary
73+
if: success()
74+
run: |
75+
cat >> $GITHUB_STEP_SUMMARY << 'EOF'
76+
## ✅ MongoDB Staging Restore Completed
77+
78+
**Timestamp:** ${{ steps.summary.outputs.timestamp }}
79+
80+
**Configuration:**
81+
- Source: Production MongoDB EBS Volume
82+
- Destination: Staging MongoDB Instance
83+
- Region: ${{ env.AWS_REGION }}
84+
- Data Anonymized: ${{ github.event.inputs.anonymize_data }}
85+
86+
**Next Steps:**
87+
1. Verify MongoDB is running on staging
88+
2. Test application connectivity
89+
3. Verify data integrity
90+
4. Clean up old volumes if needed
91+
92+
**Useful Commands:**
93+
```bash
94+
# SSH into staging
95+
ssh ec2-user@<staging-ip>
96+
97+
# Check MongoDB status
98+
sudo systemctl status mongod
99+
100+
# Verify data
101+
mongosh
102+
use userdb
103+
db.users.countDocuments()
104+
```
105+
EOF
106+
107+
- name: Handle failure
108+
if: failure()
109+
run: |
110+
cat >> $GITHUB_STEP_SUMMARY << 'EOF'
111+
## ❌ MongoDB Staging Restore Failed
112+
113+
Please check the workflow logs for detailed error information.
114+
115+
**Common Issues:**
116+
- AWS permissions insufficient
117+
- SSM Agent not running on instances
118+
- MongoDB not installed on staging
119+
- Network connectivity issues
120+
121+
**Troubleshooting:**
122+
1. Check AWS IAM permissions
123+
2. Verify EC2 instance tags
124+
3. Check SSM Agent status
125+
4. Review Ansible output logs
126+
EOF
127+
128+
notify:
129+
name: Send Notification
130+
runs-on: ubuntu-latest
131+
needs: restore-mongodb
132+
if: always()
133+
134+
steps:
135+
- name: Notify on success
136+
if: needs.restore-mongodb.result == 'success'
137+
run: |
138+
echo "✅ MongoDB staging restore completed successfully"
139+
# Add your notification logic here (Slack, Teams, email, etc.)
140+
141+
- name: Notify on failure
142+
if: needs.restore-mongodb.result == 'failure'
143+
run: |
144+
echo "❌ MongoDB staging restore failed"
145+
# Add your notification logic here (Slack, Teams, email, etc.)

0 commit comments

Comments
 (0)