49 lines
1.1 KiB
Bash
Executable File
49 lines
1.1 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
|
|
DATABASE=mainscnt
|
|
LOGIN=sink
|
|
PASSWORD=`openssl rand -base64 24`
|
|
SUPERUSER_LOGIN=$(kubectl get secret -n mainscnt database -o jsonpath="{.data.superuser-username}" | base64 --decode)
|
|
SUPERUSER_PASSWORD=$(kubectl get secret -n mainscnt database -o jsonpath="{.data.superuser-password}" | base64 --decode)
|
|
|
|
|
|
kubectl run psql \
|
|
--stdin=true \
|
|
--tty=true \
|
|
--rm=true \
|
|
--image=postgres:15-alpine \
|
|
-n $NAMESPACE \
|
|
--restart=Never \
|
|
--env="PGPASSWORD=$SUPERUSER_PASSWORD" \
|
|
--env="PGUSER=$SUPERUSER_LOGIN" \
|
|
-- \
|
|
psql -h database.mainscnt.svc.cluster.local <<EOF
|
|
do
|
|
\$\$
|
|
begin
|
|
if not exists (SELECT * FROM pg_database WHERE datname = '$DATABASE') then
|
|
CREATE DATABASE $DATABASE;
|
|
end if;
|
|
if not exists (SELECT * FROM pg_user WHERE usename = '$LOGIN') then
|
|
CREATE USER $LOGIN WITH PASSWORD '$PASSWORD';
|
|
else
|
|
ALTER USER $LOGIN WITH PASSWORD '$PASSWORD';
|
|
end if;
|
|
GRANT ALL PRIVILEGES ON DATABASE $DATABASE TO $LOGIN;
|
|
end
|
|
\$\$
|
|
;
|
|
commit;
|
|
EOF
|
|
|
|
kubectl create secret generic sinkserver-secret \
|
|
--dry-run=client \
|
|
-o yaml \
|
|
--save-config \
|
|
--from-literal=dbpass="$PASSWORD" | \
|
|
kubectl apply -f - -n $NAMESPACE
|
|
|
|
|
|
|