Merge "Revert "basic auth for so-monitoring""
[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       containers:
115       - name: {{ include "common.name" . }}
116         image: "{{ include "common.repository" . }}/{{ .Values.image }}"
117         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
118         command:
119         - /bin/bash
120         - -c
121         - >
122           mysql() { /usr/bin/mysql  -h ${DB_HOST} -P ${DB_PORT} "$@"; };
123           export -f mysql;
124           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 }};
125           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;
126           {{- if .Values.global.migration.enabled }}
127           mysql -vv --user=root --password=${MYSQL_ROOT_PASSWORD} < `ls -tr /var/data/mariadb/* | tail -1`;
128           {{- end }}
129         env:
130         - name: DB_HOST
131           valueFrom:
132             secretKeyRef:
133               name: {{ .Release.Name}}-so-db-secrets
134               key: mariadb.readwrite.host
135         - name: DB_PORT
136           valueFrom:
137             secretKeyRef:
138               name: {{ .Release.Name}}-so-db-secrets
139               key: mariadb.readwrite.port
140         - name: MYSQL_ROOT_PASSWORD
141           valueFrom:
142             secretKeyRef:
143               name: {{ template "common.fullname" . }}
144               key: db-root-password
145         volumeMounts:
146         - mountPath: /etc/localtime
147           name: localtime
148           readOnly: true
149         - name: docker-entrypoint-initdb-d-sh
150           mountPath: "/docker-entrypoint-initdb.d"
151         - name: docker-entrypoint-initdb-d-sql
152           mountPath: "/docker-entrypoint-initdb.d/db-sql-scripts"
153         {{- if .Values.global.migration.enabled }}
154         - name: backup-storage
155           mountPath: /var/data/mariadb
156         {{- end }}
157         resources:
158 {{ include "common.resources" . | indent 12 }}
159       {{- if .Values.nodeSelector }}
160       nodeSelector:
161 {{ toYaml .Values.nodeSelector | indent 10 }}
162       {{- end -}}
163       {{- if .Values.affinity }}
164       affinity:
165 {{ toYaml .Values.affinity | indent 10 }}
166       {{- end }}
167       volumes:
168       - name: localtime
169         hostPath:
170           path: /etc/localtime
171       - name: docker-entrypoint-initdb-d-sh
172         configMap:
173           name: {{ include "common.fullname" . }}-mariadb-sh
174       - name: docker-entrypoint-initdb-d-sql
175         configMap:
176           name: {{ include "common.fullname" . }}-mariadb-sql
177       {{- if .Values.global.migration.enabled }}
178       - name: backup-storage
179         persistentVolumeClaim:
180           claimName: {{ include "common.fullname" . }}-migration
181       {{- end }}
182       restartPolicy: Never
183       imagePullSecrets:
184       - name: "{{ include "common.namespace" . }}-docker-registry-key"