@intentsolutionsio/fairdb-operations-kit 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,446 @@
1
+ ---
2
+ name: fairdb-onboard-customer
3
+ description: Complete customer onboarding workflow for FairDB PostgreSQL service
4
+ model: sonnet
5
+ ---
6
+
7
+ # FairDB Customer Onboarding Workflow
8
+
9
+ You are onboarding a new customer for FairDB PostgreSQL as a Service. This comprehensive workflow creates their database, users, configures access, sets up backups, and provides connection details.
10
+
11
+ ## Step 1: Gather Customer Information
12
+
13
+ Collect these details:
14
+ 1. **Customer Name**: Company/organization name
15
+ 2. **Database Name**: Preferred database name (lowercase, no spaces)
16
+ 3. **Primary Contact**: Name and email
17
+ 4. **Plan Type**: Starter/Professional/Enterprise
18
+ 5. **IP Allowlist**: Customer IP addresses for access
19
+ 6. **Special Requirements**: Extensions, configurations, etc.
20
+
21
+ ## Step 2: Validate Resources
22
+
23
+ ```bash
24
+ # Check available resources
25
+ df -h /var/lib/postgresql
26
+ free -h
27
+ sudo -u postgres psql -c "SELECT count(*) as database_count FROM pg_database WHERE datistemplate = false;"
28
+
29
+ # Check current connections
30
+ sudo -u postgres psql -c "SELECT count(*) FROM pg_stat_activity;"
31
+ ```
32
+
33
+ ## Step 3: Create Customer Database
34
+
35
+ ```bash
36
+ # Set customer variables
37
+ CUSTOMER_NAME="customer_name" # Replace with actual
38
+ DB_NAME="${CUSTOMER_NAME}_db"
39
+ DB_OWNER="${CUSTOMER_NAME}_owner"
40
+ DB_USER="${CUSTOMER_NAME}_user"
41
+ DB_READONLY="${CUSTOMER_NAME}_readonly"
42
+
43
+ # Generate secure passwords
44
+ DB_OWNER_PASS=$(openssl rand -base64 32)
45
+ DB_USER_PASS=$(openssl rand -base64 32)
46
+ DB_READONLY_PASS=$(openssl rand -base64 32)
47
+
48
+ # Create database and users
49
+ sudo -u postgres psql << EOF
50
+ -- Create database owner role
51
+ CREATE ROLE ${DB_OWNER} WITH LOGIN PASSWORD '${DB_OWNER_PASS}'
52
+ CREATEDB CREATEROLE CONNECTION LIMIT 5;
53
+
54
+ -- Create application user
55
+ CREATE ROLE ${DB_USER} WITH LOGIN PASSWORD '${DB_USER_PASS}'
56
+ CONNECTION LIMIT 50;
57
+
58
+ -- Create read-only user
59
+ CREATE ROLE ${DB_READONLY} WITH LOGIN PASSWORD '${DB_READONLY_PASS}'
60
+ CONNECTION LIMIT 10;
61
+
62
+ -- Create customer database
63
+ CREATE DATABASE ${DB_NAME}
64
+ WITH OWNER = ${DB_OWNER}
65
+ ENCODING = 'UTF8'
66
+ LC_COLLATE = 'en_US.UTF-8'
67
+ LC_CTYPE = 'en_US.UTF-8'
68
+ TEMPLATE = template0
69
+ CONNECTION LIMIT = 100;
70
+
71
+ -- Configure database
72
+ \c ${DB_NAME}
73
+
74
+ -- Create schema
75
+ CREATE SCHEMA IF NOT EXISTS ${CUSTOMER_NAME} AUTHORIZATION ${DB_OWNER};
76
+
77
+ -- Grant permissions
78
+ GRANT CONNECT ON DATABASE ${DB_NAME} TO ${DB_USER}, ${DB_READONLY};
79
+ GRANT USAGE ON SCHEMA ${CUSTOMER_NAME} TO ${DB_USER}, ${DB_READONLY};
80
+ GRANT CREATE ON SCHEMA ${CUSTOMER_NAME} TO ${DB_USER};
81
+
82
+ -- Default privileges for tables
83
+ ALTER DEFAULT PRIVILEGES FOR ROLE ${DB_OWNER} IN SCHEMA ${CUSTOMER_NAME}
84
+ GRANT ALL ON TABLES TO ${DB_USER};
85
+
86
+ ALTER DEFAULT PRIVILEGES FOR ROLE ${DB_OWNER} IN SCHEMA ${CUSTOMER_NAME}
87
+ GRANT SELECT ON TABLES TO ${DB_READONLY};
88
+
89
+ -- Default privileges for sequences
90
+ ALTER DEFAULT PRIVILEGES FOR ROLE ${DB_OWNER} IN SCHEMA ${CUSTOMER_NAME}
91
+ GRANT ALL ON SEQUENCES TO ${DB_USER};
92
+
93
+ ALTER DEFAULT PRIVILEGES FOR ROLE ${DB_OWNER} IN SCHEMA ${CUSTOMER_NAME}
94
+ GRANT SELECT ON SEQUENCES TO ${DB_READONLY};
95
+
96
+ -- Enable useful extensions
97
+ CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
98
+ CREATE EXTENSION IF NOT EXISTS pgcrypto;
99
+ CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
100
+ CREATE EXTENSION IF NOT EXISTS citext;
101
+ EOF
102
+
103
+ echo "Database ${DB_NAME} created successfully"
104
+ ```
105
+
106
+ ## Step 4: Configure Network Access
107
+
108
+ ```bash
109
+ # Add customer IP to pg_hba.conf
110
+ CUSTOMER_IP="203.0.113.0/32" # Replace with actual customer IP
111
+
112
+ # Backup pg_hba.conf
113
+ sudo cp /etc/postgresql/16/main/pg_hba.conf /etc/postgresql/16/main/pg_hba.conf.$(date +%Y%m%d)
114
+
115
+ # Add customer access rules
116
+ cat << EOF | sudo tee -a /etc/postgresql/16/main/pg_hba.conf
117
+
118
+ # Customer: ${CUSTOMER_NAME}
119
+ hostssl ${DB_NAME} ${DB_OWNER} ${CUSTOMER_IP} scram-sha-256
120
+ hostssl ${DB_NAME} ${DB_USER} ${CUSTOMER_IP} scram-sha-256
121
+ hostssl ${DB_NAME} ${DB_READONLY} ${CUSTOMER_IP} scram-sha-256
122
+ EOF
123
+
124
+ # Update firewall
125
+ sudo ufw allow from ${CUSTOMER_IP} to any port 5432 comment "FairDB: ${CUSTOMER_NAME}"
126
+
127
+ # Reload PostgreSQL configuration
128
+ sudo systemctl reload postgresql
129
+ ```
130
+
131
+ ## Step 5: Set Resource Limits
132
+
133
+ ```bash
134
+ # Configure per-database resource limits based on plan
135
+ case "${PLAN_TYPE}" in
136
+ "starter")
137
+ MAX_CONN=50
138
+ WORK_MEM="4MB"
139
+ SHARED_BUFFERS="256MB"
140
+ ;;
141
+ "professional")
142
+ MAX_CONN=100
143
+ WORK_MEM="8MB"
144
+ SHARED_BUFFERS="1GB"
145
+ ;;
146
+ "enterprise")
147
+ MAX_CONN=200
148
+ WORK_MEM="16MB"
149
+ SHARED_BUFFERS="4GB"
150
+ ;;
151
+ esac
152
+
153
+ # Apply database-specific settings
154
+ sudo -u postgres psql -d ${DB_NAME} << EOF
155
+ -- Set connection limit
156
+ ALTER DATABASE ${DB_NAME} CONNECTION LIMIT ${MAX_CONN};
157
+
158
+ -- Set database parameters
159
+ ALTER DATABASE ${DB_NAME} SET work_mem = '${WORK_MEM}';
160
+ ALTER DATABASE ${DB_NAME} SET maintenance_work_mem = '${WORK_MEM}';
161
+ ALTER DATABASE ${DB_NAME} SET effective_cache_size = '${SHARED_BUFFERS}';
162
+ ALTER DATABASE ${DB_NAME} SET random_page_cost = 1.1;
163
+ ALTER DATABASE ${DB_NAME} SET log_statement = 'all';
164
+ ALTER DATABASE ${DB_NAME} SET log_duration = on;
165
+ EOF
166
+ ```
167
+
168
+ ## Step 6: Configure Backup Policy
169
+
170
+ ```bash
171
+ # Create customer-specific backup configuration
172
+ cat << EOF | sudo tee -a /opt/fairdb/configs/backup-${CUSTOMER_NAME}.conf
173
+ # Backup configuration for ${CUSTOMER_NAME}
174
+ DATABASE=${DB_NAME}
175
+ BACKUP_RETENTION_DAYS=30
176
+ BACKUP_SCHEDULE="0 3 * * *" # Daily at 3 AM
177
+ BACKUP_TYPE="full"
178
+ S3_PREFIX="${CUSTOMER_NAME}/"
179
+ EOF
180
+
181
+ # Add to pgBackRest configuration
182
+ sudo tee -a /etc/pgbackrest/pgbackrest.conf << EOF
183
+
184
+ [${CUSTOMER_NAME}]
185
+ pg1-path=/var/lib/postgresql/16/main
186
+ pg1-database=${DB_NAME}
187
+ pg1-port=5432
188
+ backup-user=backup_user
189
+ process-max=2
190
+ repo1-retention-full=4
191
+ repo1-retention-diff=7
192
+ EOF
193
+
194
+ # Create backup stanza for customer
195
+ sudo -u postgres pgbackrest --stanza=${CUSTOMER_NAME} stanza-create
196
+
197
+ # Schedule customer backup
198
+ echo "0 3 * * * postgres pgbackrest --stanza=${CUSTOMER_NAME} --type=full backup" | \
199
+ sudo tee -a /etc/cron.d/fairdb-customer-${CUSTOMER_NAME}
200
+ ```
201
+
202
+ ## Step 7: Setup Monitoring
203
+
204
+ ```bash
205
+ # Create monitoring user and grants
206
+ sudo -u postgres psql -d ${DB_NAME} << EOF
207
+ -- Grant monitoring permissions
208
+ GRANT pg_monitor TO ${DB_READONLY};
209
+ GRANT EXECUTE ON FUNCTION pg_stat_statements_reset() TO ${DB_OWNER};
210
+ EOF
211
+
212
+ # Create customer monitoring script
213
+ cat << 'EOF' | sudo tee /opt/fairdb/scripts/monitor-${CUSTOMER_NAME}.sh
214
+ #!/bin/bash
215
+ # Monitoring script for ${CUSTOMER_NAME}
216
+
217
+ DB_NAME="${DB_NAME}"
218
+ ALERT_THRESHOLD_CONNECTIONS=80
219
+ ALERT_THRESHOLD_SIZE_GB=100
220
+
221
+ # Check connection usage
222
+ CONN_USAGE=$(sudo -u postgres psql -t -c "
223
+ SELECT (count(*) * 100.0 / setting::int)::int as pct
224
+ FROM pg_stat_activity, pg_settings
225
+ WHERE name = 'max_connections'
226
+ AND datname = '${DB_NAME}'
227
+ GROUP BY setting;")
228
+
229
+ if [ ${CONN_USAGE:-0} -gt $ALERT_THRESHOLD_CONNECTIONS ]; then
230
+ echo "ALERT: Connection usage at ${CONN_USAGE}% for ${CUSTOMER_NAME}"
231
+ fi
232
+
233
+ # Check database size
234
+ DB_SIZE_GB=$(sudo -u postgres psql -t -c "
235
+ SELECT pg_database_size('${DB_NAME}') / 1024 / 1024 / 1024;")
236
+
237
+ if [ ${DB_SIZE_GB:-0} -gt $ALERT_THRESHOLD_SIZE_GB ]; then
238
+ echo "ALERT: Database size is ${DB_SIZE_GB}GB for ${CUSTOMER_NAME}"
239
+ fi
240
+
241
+ # Check for long-running queries
242
+ sudo -u postgres psql -d ${DB_NAME} -c "
243
+ SELECT pid, now() - pg_stat_activity.query_start AS duration, query
244
+ FROM pg_stat_activity
245
+ WHERE (now() - pg_stat_activity.query_start) > interval '5 minutes'
246
+ AND state = 'active';"
247
+ EOF
248
+
249
+ sudo chmod +x /opt/fairdb/scripts/monitor-${CUSTOMER_NAME}.sh
250
+
251
+ # Add to monitoring cron
252
+ echo "*/10 * * * * root /opt/fairdb/scripts/monitor-${CUSTOMER_NAME}.sh" | \
253
+ sudo tee -a /etc/cron.d/fairdb-monitor-${CUSTOMER_NAME}
254
+ ```
255
+
256
+ ## Step 8: Generate SSL Certificates
257
+
258
+ ```bash
259
+ # Create customer SSL certificate
260
+ sudo mkdir -p /etc/postgresql/16/main/ssl/${CUSTOMER_NAME}
261
+ cd /etc/postgresql/16/main/ssl/${CUSTOMER_NAME}
262
+
263
+ # Generate customer-specific SSL cert
264
+ sudo openssl req -new -x509 -days 365 -nodes \
265
+ -out server.crt -keyout server.key \
266
+ -subj "/C=US/ST=State/L=City/O=FairDB/OU=${CUSTOMER_NAME}/CN=${CUSTOMER_NAME}.fairdb.io"
267
+
268
+ # Set permissions
269
+ sudo chmod 600 server.key
270
+ sudo chown postgres:postgres server.*
271
+
272
+ # Create client certificate
273
+ sudo openssl req -new -nodes \
274
+ -out client.csr -keyout client.key \
275
+ -subj "/C=US/ST=State/L=City/O=FairDB/OU=${CUSTOMER_NAME}/CN=${DB_USER}"
276
+
277
+ sudo openssl x509 -req -CAcreateserial \
278
+ -in client.csr -CA server.crt -CAkey server.key \
279
+ -out client.crt -days 365
280
+
281
+ # Package client certificates
282
+ tar czf /tmp/${CUSTOMER_NAME}-ssl-bundle.tar.gz client.crt client.key server.crt
283
+ ```
284
+
285
+ ## Step 9: Create Connection Documentation
286
+
287
+ ```bash
288
+ # Generate connection details document
289
+ cat << EOF > /tmp/${CUSTOMER_NAME}-connection-details.md
290
+ # FairDB PostgreSQL Connection Details
291
+ ## Customer: ${CUSTOMER_NAME}
292
+
293
+ ### Database Information
294
+ - **Database Name**: ${DB_NAME}
295
+ - **Host**: fairdb-prod.example.com
296
+ - **Port**: 5432
297
+ - **SSL Required**: Yes
298
+
299
+ ### User Credentials
300
+ #### Database Owner (DDL Operations)
301
+ - **Username**: ${DB_OWNER}
302
+ - **Password**: ${DB_OWNER_PASS}
303
+ - **Connection Limit**: 5
304
+ - **Permissions**: Full database owner
305
+
306
+ #### Application User (DML Operations)
307
+ - **Username**: ${DB_USER}
308
+ - **Password**: ${DB_USER_PASS}
309
+ - **Connection Limit**: 50
310
+ - **Permissions**: CRUD operations on all tables
311
+
312
+ #### Read-Only User (Reporting)
313
+ - **Username**: ${DB_READONLY}
314
+ - **Password**: ${DB_READONLY_PASS}
315
+ - **Connection Limit**: 10
316
+ - **Permissions**: SELECT only
317
+
318
+ ### Connection Strings
319
+ \`\`\`
320
+ # Standard connection
321
+ postgresql://${DB_USER}:${DB_USER_PASS}@fairdb-prod.example.com:5432/${DB_NAME}?sslmode=require
322
+
323
+ # With SSL certificate
324
+ postgresql://${DB_USER}:${DB_USER_PASS}@fairdb-prod.example.com:5432/${DB_NAME}?sslmode=require&sslcert=client.crt&sslkey=client.key&sslrootcert=server.crt
325
+
326
+ # JDBC URL
327
+ jdbc:postgresql://fairdb-prod.example.com:5432/${DB_NAME}?ssl=true&sslmode=require
328
+
329
+ # psql command
330
+ psql "host=fairdb-prod.example.com port=5432 dbname=${DB_NAME} user=${DB_USER} sslmode=require"
331
+ \`\`\`
332
+
333
+ ### Resource Limits
334
+ - **Plan**: ${PLAN_TYPE}
335
+ - **Max Connections**: ${MAX_CONN}
336
+ - **Storage Quota**: Unlimited (pay per GB)
337
+ - **Backup Retention**: 30 days
338
+ - **Backup Schedule**: Daily at 3:00 AM UTC
339
+
340
+ ### Support Information
341
+ - **Email**: support@fairdb.io
342
+ - **Emergency**: +1-xxx-xxx-xxxx
343
+ - **Documentation**: https://docs.fairdb.io
344
+ - **Status Page**: https://status.fairdb.io
345
+
346
+ ### Important Notes
347
+ 1. Always use SSL connections
348
+ 2. Rotate passwords every 90 days
349
+ 3. Monitor connection pool usage
350
+ 4. Test restore procedures quarterly
351
+ 5. Keep IP allowlist updated
352
+
353
+ ### Next Steps
354
+ 1. Download SSL certificates: ${CUSTOMER_NAME}-ssl-bundle.tar.gz
355
+ 2. Test connection with provided credentials
356
+ 3. Configure application connection pool
357
+ 4. Set up monitoring dashboards
358
+ 5. Review security best practices
359
+
360
+ Generated: $(date)
361
+ EOF
362
+
363
+ echo "Connection details saved to /tmp/${CUSTOMER_NAME}-connection-details.md"
364
+ ```
365
+
366
+ ## Step 10: Final Verification
367
+
368
+ ```bash
369
+ # Test all user connections
370
+ echo "Testing database connections..."
371
+
372
+ # Test owner connection
373
+ PGPASSWORD=${DB_OWNER_PASS} psql -h localhost -U ${DB_OWNER} -d ${DB_NAME} -c "SELECT current_user, current_database();"
374
+
375
+ # Test app user connection
376
+ PGPASSWORD=${DB_USER_PASS} psql -h localhost -U ${DB_USER} -d ${DB_NAME} -c "SELECT current_user, current_database();"
377
+
378
+ # Test readonly connection
379
+ PGPASSWORD=${DB_READONLY_PASS} psql -h localhost -U ${DB_READONLY} -d ${DB_NAME} -c "SELECT current_user, current_database();"
380
+
381
+ # Verify backup configuration
382
+ sudo -u postgres pgbackrest --stanza=${CUSTOMER_NAME} check
383
+
384
+ # Check monitoring
385
+ /opt/fairdb/scripts/monitor-${CUSTOMER_NAME}.sh
386
+
387
+ # Generate onboarding summary
388
+ echo "
389
+ ===========================================
390
+ FairDB Customer Onboarding Complete
391
+ ===========================================
392
+ Customer: ${CUSTOMER_NAME}
393
+ Database: ${DB_NAME}
394
+ Created: $(date)
395
+ Plan: ${PLAN_TYPE}
396
+
397
+ Files Generated:
398
+ - /tmp/${CUSTOMER_NAME}-connection-details.md
399
+ - /tmp/${CUSTOMER_NAME}-ssl-bundle.tar.gz
400
+
401
+ Next Actions:
402
+ 1. Send connection details to customer
403
+ 2. Schedule onboarding call
404
+ 3. Monitor initial usage
405
+ 4. Follow up in 24 hours
406
+ ===========================================
407
+ "
408
+ ```
409
+
410
+ ## Onboarding Checklist
411
+
412
+ Verify completion:
413
+ - [ ] Database created
414
+ - [ ] Users created with secure passwords
415
+ - [ ] Network access configured
416
+ - [ ] Resource limits applied
417
+ - [ ] Backup policy configured
418
+ - [ ] Monitoring enabled
419
+ - [ ] SSL certificates generated
420
+ - [ ] Documentation created
421
+ - [ ] Connection tests passed
422
+ - [ ] Customer notified
423
+
424
+ ## Rollback Procedure
425
+
426
+ If onboarding fails:
427
+ ```bash
428
+ # Remove database and users
429
+ sudo -u postgres psql << EOF
430
+ DROP DATABASE IF EXISTS ${DB_NAME};
431
+ DROP ROLE IF EXISTS ${DB_OWNER};
432
+ DROP ROLE IF EXISTS ${DB_USER};
433
+ DROP ROLE IF EXISTS ${DB_READONLY};
434
+ EOF
435
+
436
+ # Remove configurations
437
+ sudo rm -f /etc/cron.d/fairdb-customer-${CUSTOMER_NAME}
438
+ sudo rm -f /etc/cron.d/fairdb-monitor-${CUSTOMER_NAME}
439
+ sudo rm -f /opt/fairdb/scripts/monitor-${CUSTOMER_NAME}.sh
440
+ sudo rm -rf /etc/postgresql/16/main/ssl/${CUSTOMER_NAME}
441
+
442
+ # Remove firewall rule
443
+ sudo ufw delete allow from ${CUSTOMER_IP} to any port 5432
444
+
445
+ echo "Customer ${CUSTOMER_NAME} rollback complete"
446
+ ```