This repository has been archived by the owner on Aug 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
restore-db.job.ts
229 lines (207 loc) · 5.96 KB
/
restore-db.job.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { generate } from "@socialgouv/env-slug";
import { getDefaultPgParams } from "@socialgouv/kosko-charts/components/azure-pg";
import { azureProjectVolume } from "@socialgouv/kosko-charts/components/azure-storage/azureProjectVolume";
import environments from "@socialgouv/kosko-charts/environments";
import { addInitContainer } from "@socialgouv/kosko-charts/utils/addInitContainer";
import { waitForPostgres } from "@socialgouv/kosko-charts/utils/waitForPostgres";
import ok from "assert";
import type { IObjectMeta } from "kubernetes-models/apimachinery/pkg/apis/meta/v1";
import { Job } from "kubernetes-models/batch/v1";
import type { EnvVar } from "kubernetes-models/v1";
import {
ConfigMap,
EnvFromSource,
Volume,
VolumeMount,
} from "kubernetes-models/v1";
interface RestoreDbJobArgs {
project: string;
env: EnvVar[];
envFrom?: EnvFromSource[];
postRestoreScript?: string;
}
const SOCIALGOUV_DOCKER_IMAGE = "ghcr.io/socialgouv/docker/azure-db";
// renovate: datasource=docker depName=ghcr.io/socialgouv/docker/azure-db versioning=6.56.1
const SOCIALGOUV_DOCKER_VERSION = "6.56.1";
const restoreScript = `
echo "starting restore into $PGHOST/$PGDATABASE"
[ ! -z $PGDATABASE ] || (echo "No PGDATABASE"; exit 1)
[ ! -z $PGHOST ] || (echo "No PGHOST"; exit 1)
[ ! -z $PGUSER ] || (echo "No PGUSER"; exit 1)
[ ! -z $PGPASSWORD ] || (echo "No PGPASSWORD"; exit 1)
[ ! -z $OWNER ] || (echo "No OWNER"; exit 1)
# get latest backup folder
LATEST=$(ls -1Fr /mnt/data | head -n 1);
DUMP="/mnt/data/\${LATEST}\${FILE}"
echo "Restore \${DUMP} into \${PGDATABASE}";
pg_isready;
pg_restore \
--dbname \${PGDATABASE} \
--clean --if-exists \
--exclude-schema=audit \
--no-owner \
--role \${OWNER} \
--no-acl \
--verbose \
\${DUMP};
psql -v ON_ERROR_STOP=1 \${PGDATABASE} -c "ALTER SCHEMA public owner to \${OWNER};"
[ -f "/mnt/scripts/post-restore.sql" ] && psql -v ON_ERROR_STOP=1 -a < /mnt/scripts/post-restore.sql
`;
const getProjectSecretNamespace = (project: string) => `${project}-secret`;
const getAzureProdVolumeSecretName = (project: string) =>
`azure-${project.replace(/-/g, "")}prod-volume`;
export const restoreDbJob = ({
project,
env = [],
envFrom = [],
postRestoreScript,
}: RestoreDbJobArgs): { metadata?: IObjectMeta; kind: string }[] => {
const ciEnv = environments(process.env);
const secretNamespace = getProjectSecretNamespace(project);
const azureSecretName = getAzureProdVolumeSecretName(project);
const azureFileShareName = `${project}-backup-restore`;
const [pvc, pv] = azureProjectVolume(azureFileShareName, {
storage: "128Gi",
});
ok(pvc.metadata?.name, "Missing pvc.metadata.name");
ok(pv.metadata?.name, "Missing pv.metadata.name");
// NOTE(douglasduteil): lock the pvc and the pc on the existing secret prod namepace
ok(pv.spec?.azureFile, "Missing pv.spec?.azureFile");
pvc.metadata.name = azureFileShareName;
pvc.metadata.namespace = secretNamespace;
pv.metadata.namespace = pv.spec.azureFile.secretNamespace = secretNamespace;
pv.spec.azureFile.secretName = azureSecretName;
const backupsVolume = new Volume({
name: "backups",
persistentVolumeClaim: {
claimName: pvc.metadata.name,
readOnly: true,
},
});
const manifests = [];
const jobSpec = {
containers: [
{
command: ["sh", "-c", restoreScript],
env,
envFrom: [
new EnvFromSource({
secretRef: {
name: "azure-pg-admin-user-dev",
},
}),
...envFrom,
],
image: `${SOCIALGOUV_DOCKER_IMAGE}:${SOCIALGOUV_DOCKER_VERSION}`,
imagePullPolicy: "IfNotPresent",
name: "restore-db",
resources: {
limits: {
cpu: "300m",
memory: "512Mi",
},
requests: {
cpu: "100m",
memory: "64Mi",
},
},
volumeMounts: [
{
mountPath: "/mnt/data",
name: backupsVolume.name,
},
],
},
],
restartPolicy: "OnFailure",
volumes: [backupsVolume],
};
if (postRestoreScript) {
const name = "scripts";
const configMapName = generate(
`post-restore-script-configmap-${ciEnv.branch}`
);
jobSpec.containers[0].volumeMounts.push(
new VolumeMount({
mountPath: "/mnt/scripts",
name,
})
);
jobSpec.volumes.push(
new Volume({
configMap: {
name: configMapName,
},
name,
})
);
const configMap = new ConfigMap({
data: {
"post-restore.sql": postRestoreScript,
},
metadata: {
name: configMapName,
namespace: secretNamespace,
},
});
manifests.push(configMap);
}
const jobName = generate(`restore-db-${ciEnv.branch}`);
const job = new Job({
metadata: {
annotations: {
"kapp.k14s.io/update-strategy": "skip",
},
labels: {
component: "restore-db",
...ciEnv.metadata.labels,
},
name: jobName,
namespace: secretNamespace,
},
spec: {
backoffLimit: 0,
template: {
metadata: {
annotations: {
"kapp.k14s.io/deploy-logs": "for-new-or-existing",
},
},
spec: jobSpec,
},
ttlSecondsAfterFinished: 86400,
},
});
const initContainer = waitForPostgres({
secretRefName: "azure-pg-admin-user-dev",
});
initContainer.envFrom = [];
const defaultParams = getDefaultPgParams();
initContainer.env = [
{
name: "PGHOST",
value: defaultParams.host,
},
{
name: "PGDATABASE",
value: defaultParams.database,
},
{
name: "PGPASSWORD",
value: defaultParams.password,
},
{
name: "PGUSER",
value: `${defaultParams.user}@${defaultParams.host}`,
},
{
name: "PGSSLMODE",
value: "require",
},
];
addInitContainer(job, initContainer);
manifests.push(job);
manifests.push(pvc);
manifests.push(pv);
return manifests;
};