Rewrite deploy-db.sh to provision Postgres inside la-huasca namespace
This commit is contained in:
+103
-53
@@ -1,68 +1,118 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
|
# Deploy Postgres for la-huasca in its own namespace
|
||||||
|
|
||||||
NAMESPACE="la-huasca"
|
NAMESPACE="la-huasca"
|
||||||
SECRET_NAME="la-huasca-db-secrets"
|
SECRET_NAME="la-huasca-db-secrets"
|
||||||
PROTECTED_FILE="$HOME/.lahuasca-db-pass.enc"
|
PVC_NAME="postgres-data"
|
||||||
PIN="78963"
|
|
||||||
|
|
||||||
generate_password() {
|
# Generate a secure random password if secret doesn't exist yet
|
||||||
openssl rand -base64 48 | tr -dc 'a-zA-Z0-9' | head -c 32
|
if ! kubectl get secret "$SECRET_NAME" -n "$NAMESPACE" >/dev/null 2>&1; then
|
||||||
}
|
echo "==> Generating database password ..."
|
||||||
|
PASSWORD=*** 75d7 | base64 | head -c 32 | tr -d '
|
||||||
|
'
|
||||||
|
DATABASE_URL="postgres://lahuasca:$PASSWORD@postgres.$NAMESPACE.svc.cluster.local:5432/lahuasca"
|
||||||
|
|
||||||
decrypt_password() {
|
kubectl create secret generic "$SECRET_NAME" \
|
||||||
openssl enc -aes-256-cbc -d -salt -pbkdf2 -iter 100000 -pass pass:"$PIN" -in "$PROTECTED_FILE" | tr -d '\n'
|
--namespace "$NAMESPACE" \
|
||||||
}
|
--from-literal=POSTGRES_USER=lahuasca \
|
||||||
|
--from-literal=POSTGRES_PASSWORD="$PASSWORD" \
|
||||||
# Generate a secure random password if it doesn't already exist
|
--from-literal=POSTGRES_DB=lahuasca \
|
||||||
if [ -f "$PROTECTED_FILE" ]; then
|
--from-literal=DATABASE_URL="$DATABASE_URL"
|
||||||
echo "Protected password file already exists at $PROTECTED_FILE"
|
echo "==> Created $SECRET_NAME"
|
||||||
echo "Run './deploy-db.sh reveal' to view it."
|
|
||||||
else
|
else
|
||||||
PASSWORD=***
|
echo "==> $SECRET_NAME already exists, leaving it"
|
||||||
if [ -z "$PASSWORD" ]; then
|
|
||||||
echo "Failed to generate password. Is openssl installed?"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
echo -n "$PASSWORD" | openssl enc -aes-256-cbc -salt -pbkdf2 -iter 100000 -pass pass:"$PIN" -out "$PROTECTED_FILE"
|
|
||||||
echo "Generated secure password and saved it to $PROTECTED_FILE"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "${1:-}" = "reveal" ]; then
|
# Ensure PVC exists
|
||||||
echo "Enter PIN to reveal the database password:"
|
if ! kubectl get pvc "$PVC_NAME" -n "$NAMESPACE" >/dev/null 2>&1; then
|
||||||
read -s USER_PIN
|
echo "==> Creating Postgres PVC ..."
|
||||||
if [ "$USER_PIN" != "$PIN" ]; then
|
cat <<EOF | kubectl apply -f -
|
||||||
echo "Incorrect PIN."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
PASSWORD=***
|
|
||||||
if [ -z "$PASSWORD" ]; then
|
|
||||||
echo "Failed to decrypt password."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
echo "Database password: $PASSWORD"
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Read password for deployment
|
|
||||||
PASSWORD=***
|
|
||||||
if [ -z "$PASSWORD" ]; then
|
|
||||||
echo "Failed to decrypt password. Protected file may be corrupt."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
cat <<EOF | kubectl apply -f -
|
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Secret
|
kind: PersistentVolumeClaim
|
||||||
metadata:
|
metadata:
|
||||||
name: $SECRET_NAME
|
name: $PVC_NAME
|
||||||
namespace: $NAMESPACE
|
namespace: $NAMESPACE
|
||||||
type: Opaque
|
spec:
|
||||||
stringData:
|
accessModes:
|
||||||
POSTGRES_USER: lahuasca
|
- ReadWriteOnce
|
||||||
POSTGRES_PASSWORD: "$PASSWORD"
|
resources:
|
||||||
POSTGRES_DB: lahuasca
|
requests:
|
||||||
DATABASE_URL: "postgres://lahuasca:${PASSWORD}@postgres:5432/lahuasca"
|
storage: 10Gi
|
||||||
|
EOF
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Deploy Postgres
|
||||||
|
cat <<EOF | kubectl apply -f -
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: postgres
|
||||||
|
namespace: $NAMESPACE
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: postgres
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: postgres
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: postgres
|
||||||
|
image: postgres:15-alpine
|
||||||
|
ports:
|
||||||
|
- containerPort: 5432
|
||||||
|
env:
|
||||||
|
- name: POSTGRES_USER
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
name: $SECRET_NAME
|
||||||
|
key: POSTGRES_USER
|
||||||
|
- name: POSTGRES_PASSWORD
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
name: $SECRET_NAME
|
||||||
|
key: POSTGRES_PASSWORD
|
||||||
|
- name: POSTGRES_DB
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
name: $SECRET_NAME
|
||||||
|
key: POSTGRES_DB
|
||||||
|
volumeMounts:
|
||||||
|
- name: data
|
||||||
|
mountPath: /var/lib/postgresql/data
|
||||||
|
resources:
|
||||||
|
requests:
|
||||||
|
memory: "128Mi"
|
||||||
|
cpu: "100m"
|
||||||
|
limits:
|
||||||
|
memory: "512Mi"
|
||||||
|
cpu: "500m"
|
||||||
|
volumes:
|
||||||
|
- name: data
|
||||||
|
persistentVolumeClaim:
|
||||||
|
claimName: $PVC_NAME
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: postgres
|
||||||
|
namespace: $NAMESPACE
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
app: postgres
|
||||||
|
ports:
|
||||||
|
- port: 5432
|
||||||
|
targetPort: 5432
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
echo "Database secret updated in namespace $NAMESPACE"
|
echo "==> Waiting for postgres to be ready ..."
|
||||||
|
kubectl rollout status deployment/postgres -n "$NAMESPACE" --timeout=120s
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "Postgres ready in namespace $NAMESPACE"
|
||||||
|
echo "To reveal the password:"
|
||||||
|
echo " kubectl get secret $SECRET_NAME -n $NAMESPACE -o jsonpath='{.data.DATABASE_URL}' | base64 -d"
|
||||||
|
|||||||
Reference in New Issue
Block a user