/
usersync-cronjob.yaml
249 lines (248 loc) · 10.5 KB
/
usersync-cronjob.yaml
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# Note: change to batch/v1beta1 once we bump to k8s 1.8
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: usersync
spec:
schedule: "@hourly"
successfulJobsHistoryLimit: 2
failedJobsHistoryLimit: 2
jobTemplate:
spec:
# not yet supported - backOffLimit: 3
template:
metadata:
labels:
app: gen3job
spec:
serviceAccountName: useryaml-job
volumes:
- name: shared-data
emptyDir: {}
# -----------------------------------------------------------------------------
# DEPRECATED! Remove when all commons are no longer using local_settings.py
# for fence.
# -----------------------------------------------------------------------------
- name: old-config-volume
secret:
secretName: "fence-secret"
- name: json-secret-volume
secret:
secretName: "fence-json-secret"
- name: creds-volume
secret:
secretName: "fence-creds"
- name: config-helper
configMap:
name: config-helper
# -----------------------------------------------------------------------------
- name: config-volume
secret:
secretName: "fence-config"
- name: fence-google-app-creds-secret-volume
secret:
secretName: "fence-google-app-creds-secret"
- name: fence-google-storage-creds-secret-volume
secret:
secretName: "fence-google-storage-creds-secret"
- name: fence-yaml
configMap:
name: fence
- name: fence-ssh-keys
secret:
secretName: "fence-ssh-keys"
defaultMode: 0400
- name: fence-sshconfig
configMap:
name: "fence-sshconfig"
- name: projects
configMap:
name: "projects"
containers:
- name: fence
GEN3_FENCE_IMAGE
imagePullPolicy: Always
env:
- name: PYTHONPATH
value: /var/www/fence
- name: SYNC_FROM_DBGAP
valueFrom:
configMapKeyRef:
name: manifest-global
key: sync_from_dbgap
- name: SLACK_SEND_DBGAP
valueFrom:
configMapKeyRef:
name: manifest-global
key: slack_send_dbgap
optional: true
- name: slackWebHook
valueFrom:
configMapKeyRef:
name: global
key: slack_webhook
optional: true
- name: gen3Env
valueFrom:
configMapKeyRef:
name: global
key: environment
volumeMounts:
- name: shared-data
mountPath: /mnt/shared
# -----------------------------------------------------------------------------
# DEPRECATED! Remove when all commons are no longer using local_settings.py
# for fence.
# -----------------------------------------------------------------------------
- name: "old-config-volume"
readOnly: true
mountPath: "/var/www/fence/local_settings.py"
subPath: local_settings.py
- name: "json-secret-volume"
readOnly: true
mountPath: "/var/www/fence/fence_credentials.json"
subPath: fence_credentials.json
- name: "creds-volume"
readOnly: true
mountPath: "/var/www/fence/creds.json"
subPath: creds.json
- name: "config-helper"
readOnly: true
mountPath: "/var/www/fence/config_helper.py"
subPath: config_helper.py
# -----------------------------------------------------------------------------
- name: "config-volume"
readOnly: true
mountPath: "/var/www/fence/fence-config.yaml"
subPath: fence-config.yaml
- name: "fence-google-app-creds-secret-volume"
readOnly: true
mountPath: "/var/www/fence/fence_google_app_creds_secret.json"
subPath: fence_google_app_creds_secret.json
- name: "fence-google-storage-creds-secret-volume"
readOnly: true
mountPath: "/var/www/fence/fence_google_storage_creds_secret.json"
subPath: fence_google_storage_creds_secret.json
- name: "fence-yaml"
mountPath: "/var/www/fence/user.yaml"
subPath: user.yaml
- name: "fence-ssh-keys"
mountPath: "/root/.ssh/id_rsa"
subPath: "id_rsa"
- name: "fence-ssh-keys"
mountPath: "/root/.ssh/id_rsa.pub"
subPath: "id_rsa.pub"
- name: "fence-sshconfig"
mountPath: "/root/.ssh/config"
subPath: "config"
- name: "projects"
mountPath: "/var/www/fence/projects.yaml"
subPath: "projects.yaml"
command: ["/bin/bash" ]
args:
- "-c"
# Script always succeeds if it runs (echo exits with 0)
- |
echo 'options use-vc' >> /etc/resolv.conf
let count=0
while [[ ! -f /mnt/shared/user.yaml && $count -lt 50 ]]; do
echo "fence container waiting for /mnt/shared/user.yaml";
sleep 2
let count=$count+1
done
if [ ! "$SYNC_FROM_DBGAP" = True ]; then
if [[ -f /mnt/shared/user.yaml ]]; then
echo "fence container user.yaml diff ..."
diff /var/www/fence/user.yaml /mnt/shared/user.yaml
if [[ $? == 1 || "${userYamlS3Path}" = 'none' ]]; then
echo "running fence-create"
fence-create sync --arborist http://arborist-service --yaml /mnt/shared/user.yaml
else
echo "no difference found"
fi
else
echo "/mnt/shared/user.yaml did not appear within timeout :-("
fi
exitcode=$?
else
if [[ -f /mnt/shared/user.yaml ]]; then
output=$(fence-create sync --arborist http://arborist-service --sync_from_dbgap "$SYNC_FROM_DBGAP" --projects /var/www/fence/projects.yaml --yaml /mnt/shared/user.yaml 2>&1)
else
output=$(fence-create sync --arborist http://arborist-service --sync_from_dbgap "$SYNC_FROM_DBGAP" --projects /var/www/fence/projects.yaml 2>&1)
fi
exitcode=$?
echo "$output"
# Echo what files we are seeing on dbgap ftp to Slack
# We only do this step every 12 hours and not on weekends to reduce noise
if [[ -n "$SLACK_SEND_DBGAP" && "$SLACK_SEND_DBGAP" = True ]]; then
files=$(echo "$output" | grep "Reading file")
let hour=$(date -u +10#%H)
let dow=$(date -u +10#%u)
if ! (( hour % 12 )) && (( dow < 6 )); then
if [ "${slackWebHook}" != 'None' ]; then
curl -X POST --data-urlencode "payload={\"text\": \"FenceHelper: \n\`\`\`\n${files}\n\`\`\`\"}" "${slackWebHook}"
fi
fi
fi
fi
if [[ $exitcode -ne 0 && "${slackWebHook}" != 'None' ]]; then
emptyfile=$(echo "$output" | grep "EnvironmentError:")
if [ ! -z "$emptyfile" ]; then
curl -X POST --data-urlencode "payload={\"text\": \"JOBSKIPPED: User sync skipped on ${gen3Env} ${emptyfile}\"}" "${slackWebHook}";
else
curl -X POST --data-urlencode "payload={\"text\": \"JOBFAIL: User sync failed on ${gen3Env}\"}" "${slackWebHook}"
fi
fi
echo "Exit code: $exitcode"
- name: awshelper
image: quay.io/cdis/awshelper:master
imagePullPolicy: Always
volumeMounts:
- name: "fence-yaml"
mountPath: "/var/www/fence/user.yaml"
subPath: user.yaml
- name: shared-data
mountPath: /mnt/shared
command: ["/bin/bash" ]
args:
- "-c"
- |
if [ "${userYamlS3Path}" = 'none' ]; then
echo "s3 yaml not provided, using local yaml"
cp /var/www/fence/user.yaml /mnt/shared/user.yaml
else
echo "awshelper downloading ${userYamlS3Path} to /mnt/shared/useryaml";
aws s3 cp "${userYamlS3Path}" /mnt/shared/user.yaml;
if [[ -f /mnt/shared/user.yaml ]]; then
echo "awshelper container user.yaml diff ..."
diff /var/www/fence/user.yaml /mnt/shared/user.yaml
if [[ $? == 1 ]]; then
echo "awshelper updating fence configmap"
kubectl delete configmap fence
kubectl create configmap fence --from-file=/mnt/shared/user.yaml
if [ "${slackWebHook}" != 'None' ]; then
curl -X POST --data-urlencode "payload={\"text\": \"AWSHelper: Syncing users on ${gen3Env}\"}" "${slackWebHook}"
fi
else
echo "no difference found"
fi
fi
fi
echo "Helper exit code: $?"
env:
- name: userYamlS3Path
valueFrom:
configMapKeyRef:
name: manifest-global
key: useryaml_s3path
- name: gen3Env
valueFrom:
configMapKeyRef:
name: global
key: environment
- name: slackWebHook
valueFrom:
configMapKeyRef:
name: global
key: slack_webhook
restartPolicy: Never