SO Rolling upgrade using helm hooks
[oom.git] / kubernetes / so / charts / so-mariadb / templates / job.yaml
1 # Copyright © 2017 Amdocs, Bell Canada
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #       http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 {{- if .Values.global.migration.enabled }}
15 apiVersion: batch/v1
16 kind: Job
17 metadata:
18   name: {{ include "common.fullname" . }}-backup
19   namespace: {{ include "common.namespace" . }}
20   labels:
21     app: {{ include "common.name" . }}
22     chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
23     release: {{ .Release.Name }}
24     heritage: {{ .Release.Service }}
25   annotations:
26     "helm.sh/hook": pre-upgrade,pre-install
27     "helm.sh/hook-weight": "1"
28     "helm.sh/hook-delete-policy": before-hook-creation
29 spec:
30   backoffLimit: 20
31   template:
32     metadata:
33       labels:
34         app: {{ include "common.name" . }}
35         release: {{ .Release.Name }}
36       name: {{ include "common.name" . }}
37     spec:
38       containers:
39       - name: {{ include "common.fullname" . }}
40         image: "{{ include "common.repository" . }}/{{ .Values.image }}"
41         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
42         env:
43         - name: DB_HOST
44           value: {{ .Values.global.migration.dbHost }}
45         - name: DB_USER
46           value: {{ .Values.global.migration.dbUser }}
47         - name: DB_PORT
48           value: "{{ .Values.global.migration.dbPort }}"
49         - name: DB_PASS
50           valueFrom:
51             secretKeyRef:
52               name: {{ template "common.fullname" . }}-migration
53               key: db-root-password-backup
54         command:
55         - /bin/bash
56         - -c
57         - mysqldump -vv --user=${DB_USER} --password=${DB_PASS} --host=${DB_HOST} --port=${DB_PORT} --databases --single-transaction --quick --lock-tables=false catalogdb requestdb > /var/data/mariadb/backup-`date +%s`.sql
58         volumeMounts:
59         - mountPath: /etc/localtime
60           name: localtime
61           readOnly: true
62         - name: backup-storage
63           mountPath: /var/data/mariadb
64       volumes:
65       - name: localtime
66         hostPath:
67           path: /etc/localtime
68       - name: backup-storage
69         persistentVolumeClaim:
70           claimName: {{ include "common.fullname" . }}-migration
71       imagePullSecrets:
72         - name: "{{ include "common.namespace" . }}-docker-registry-key"
73       restartPolicy: Never
74 ---
75 {{- end }}
76 apiVersion: batch/v1
77 kind: Job
78 metadata:
79   name: {{ include "common.fullname" . }}-config-job
80   namespace: {{ include "common.namespace" . }}
81   labels:
82     app: {{ include "common.name" . }}
83     chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
84     release: {{ .Release.Name }}
85     heritage: {{ .Release.Service }}
86   annotations:
87     "helm.sh/hook": post-upgrade,post-rollback,post-install
88     "helm.sh/hook-weight": "0"
89     "helm.sh/hook-delete-policy": before-hook-creation
90 spec:
91   backoffLimit: 20
92   template:
93     metadata:
94       labels:
95         app: {{ include "common.name" . }}-job
96         release: {{ .Release.Name }}
97       name: {{ include "common.name" . }}
98     spec:
99       initContainers:
100       - name: {{ include "common.name" . }}-readiness
101         command:
102         - /root/ready.py
103         args:
104         - --container-name
105         - {{ .Values.global.mariadbGalera.nameOverride }}
106         env:
107         - name: NAMESPACE
108           valueFrom:
109             fieldRef:
110               apiVersion: v1
111               fieldPath: metadata.namespace
112         image: "{{ .Values.global.readinessRepository }}/{{ .Values.global.readinessImage }}"
113         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
114       - name: {{ include "common.name" . }}-inject-testlab-project
115         command:
116         - /bin/bash
117         - -c
118         - >
119           git clone -b {{ .Values.config.gerritBranch }} --single-branch {{ .Values.config.gerritProject }} /tmp/gerrit;
120           echo "Clone complete. Copying from /tmp/gerrit/volumes/mariadb/docker-entrypoint-initdb.d to /docker-entrypoint-initdb.d";
121           cp -rf /tmp/gerrit/volumes/mariadb/docker-entrypoint-initdb.d/* /docker-entrypoint-initdb.d;
122           chmod -R 755 /docker-entrypoint-initdb.d;
123           echo "Done.";
124         image: "{{ .Values.global.ubuntuInitRepository }}/{{ .Values.ubuntuInitImage }}"
125         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
126         volumeMounts:
127         - name: docker-entrypoint-initdb-d
128           mountPath: "/docker-entrypoint-initdb.d"
129       containers:
130       - name: {{ include "common.name" . }}
131         image: "{{ include "common.repository" . }}/{{ .Values.image }}"
132         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
133         command:
134         - /bin/bash
135         - -c
136         - >
137           mysql() { /usr/bin/mysql  -h ${DB_HOST} -P ${DB_PORT} "$@"; };
138           export -f mysql;
139           mysql --user=root --password=${MYSQL_ROOT_PASSWORD} requestdb -e exit > /dev/null 2>&1 {{ if not .Values.global.migration.enabled }} && echo "Database already initialized!!!" && exit 0 {{ end }};
140           for f in /docker-entrypoint-initdb.d/*; do case "$f" in  *.sh) echo "$0: running $f"; . "$f" ;;  *.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;; *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;  *) echo "$0: ignoring $f" ;;  esac; echo; done;
141           {{- if .Values.global.migration.enabled }}
142           mysql -vv --user=root --password=${MYSQL_ROOT_PASSWORD} < `ls -tr /var/data/mariadb/* | tail -1`;
143           {{- end }}
144         env:
145         - name: DB_HOST
146           valueFrom:
147             secretKeyRef:
148               name: {{ .Release.Name}}-so-db-secrets
149               key: mariadb.readwrite.host
150         - name: DB_PORT
151           valueFrom:
152             secretKeyRef:
153               name: {{ .Release.Name}}-so-db-secrets
154               key: mariadb.readwrite.port
155         - name: MYSQL_ROOT_PASSWORD
156           valueFrom:
157             secretKeyRef:
158               name: {{ template "common.fullname" . }}
159               key: db-root-password
160         volumeMounts:
161         - mountPath: /etc/localtime
162           name: localtime
163           readOnly: true
164         - name: docker-entrypoint-initdb-d
165           mountPath: "/docker-entrypoint-initdb.d"
166         {{- if .Values.global.migration.enabled }}
167         - name: backup-storage
168           mountPath: /var/data/mariadb
169         {{- end }}
170         resources:
171 {{ include "common.resources" . | indent 12 }}
172       {{- if .Values.nodeSelector }}
173       nodeSelector:
174 {{ toYaml .Values.nodeSelector | indent 10 }}
175       {{- end -}}
176       {{- if .Values.affinity }}
177       affinity:
178 {{ toYaml .Values.affinity | indent 10 }}
179       {{- end }}
180       volumes:
181       - name: localtime
182         hostPath:
183           path: /etc/localtime
184       - name: docker-entrypoint-initdb-d
185         emptyDir: {}
186       {{- if .Values.global.migration.enabled }}
187       - name: backup-storage
188         persistentVolumeClaim:
189           claimName: {{ include "common.fullname" . }}-migration
190       {{- end }}
191       restartPolicy: Never
192       imagePullSecrets:
193       - name: "{{ include "common.namespace" . }}-docker-registry-key"