/
scripts.go
746 lines (597 loc) · 20 KB
/
scripts.go
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
package indexmanagement
const indexManagementClient = `
#!/bin/python
import os, sys, ast
import json
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, Q
from ssl import create_default_context
def getEsClient():
esService = os.environ['ES_SERVICE']
connectTimeout = os.getenv('CONNECT_TIMEOUT', 30)
tokenFile = open('/var/run/secrets/kubernetes.io/serviceaccount/token', 'r')
bearer_token = tokenFile.read()
context = create_default_context(cafile='/etc/indexmanagement/keys/admin-ca')
es_client = Elasticsearch([esService],
timeout=connectTimeout,
max_retries=5,
retry_on_timeout=True,
headers={"authorization": f"Bearer {bearer_token}"},
ssl_context=context)
return es_client
def getAlias(alias):
try:
es_client = getEsClient()
return json.dumps(es_client.indices.get_alias(name=alias))
except:
return ""
def getIndicesAgeForAlias(alias):
try:
es_client = getEsClient()
return json.dumps(es_client.indices.get_settings(index=alias, name="index.creation_date"))
except:
return ""
def deleteIndices(index):
original_stdout = sys.stdout
try:
es_client = getEsClient()
response = es_client.indices.delete(index=index)
return True
except Exception as e:
sys.stdout = open('/tmp/response.txt', 'w')
print(e)
sys.stdout = original_stdout
return False
def deleteByQuery(index, namespaceSpecs, defaultAge):
original_stdout = sys.stdout
try:
es_client = getEsClient()
#Extract string values of namespace and minAge from namespaceSpecs to feed into dsl-query
namespaceSpecs = json.loads(namespaceSpecs)
for namespaceName in namespaceSpecs:
s = Search(using=es_client)
s = s.query('prefix', kubernetes__namespace_name=namespaceName)
minAge = namespaceSpecs[namespaceName]
if minAge == "":
s = s.filter('range', **{'@timestamp': {'lt': 'now-{}'.format(defaultAge)}})
else:
s = s.filter('range', **{'@timestamp': {'lt': 'now-{}'.format(minAge)}})
response = es_client.delete_by_query(index=index, body=s.to_dict(), doc_type="_doc", conflicts="proceed")
return True
except Exception as e:
sys.stdout = open('/tmp/response.txt', 'w')
print(e)
sys.stdout = original_stdout
return False
def removeAsWriteIndexForAlias(index, alias):
es_client = getEsClient()
response = es_client.indices.update_aliases({
"actions": [
{"add":{"index": f"{index}", "alias": f"{alias}", "is_write_index": False}}
]
})
return response['acknowledged']
def catWriteAliases(policy):
try:
es_client = getEsClient()
alias_name = f"{policy}*-write"
response = es_client.cat.aliases(name=alias_name, h="alias")
response_list = list(response.split("\n"))
return " ".join(sorted(set(response_list)))
except:
return ""
def docsCountInIndex(alias):
try:
es_client = getEsClient()
docs_count = es_client.cat.count(index=alias, h="count")
return docs_count
except:
return "-1"
def rolloverForPolicy(alias, decoded):
original_stdout = sys.stdout
try:
es_client = getEsClient()
response = es_client.indices.rollover(alias=alias, body=decoded)
sys.stdout = open('/tmp/response.txt', 'w')
print(json.dumps(response))
sys.stdout = original_stdout
return True
except:
return False
def checkIndexExists(index):
original_stdout = sys.stdout
try:
es_client = getEsClient()
return es_client.indices.exists(index=index)
except Exception as e:
sys.stdout = open('/tmp/response.txt', 'w')
print(e)
sys.stdout = original_stdout
return False
def updateWriteIndex(currentIndex, nextIndex, alias):
original_stdout = sys.stdout
try:
es_client = getEsClient()
response = es_client.indices.update_aliases({
"actions": [
{"add":{"index": f"{currentIndex}", "alias": f"{alias}", "is_write_index": False}},
{"add":{"index": f"{nextIndex}", "alias": f"{alias}", "is_write_index": True}}
]
})
return response['acknowledged']
except Exception as e:
sys.stdout = open('/tmp/response.txt', 'w')
print(e)
sys.stdout = original_stdout
return False
def indexBelongsToAlias(index, alias):
original_stdout = sys.stdout
try:
es_client = getEsClient()
response = es_client.indices.get_alias(index=index)
writeIndexName = list(key for key, value in response[index]["aliases"].items() if '-write' in key)[0]
return alias+"-write" == writeIndexName
except Exception as e:
sys.stdout = open('/tmp/response.txt', 'w')
print (e)
sys.stdout = original_stdout
return False
def isWriteIndex(index):
original_stdout = sys.stdout
try:
es_client = getEsClient()
writeIndexName = index.rpartition('-')[0] + '-write'
indexInfo = es_client.indices.get(index=index, include_type_name=False)
isWriteIndex = indexInfo[index]['aliases'][writeIndexName].get('is_write_index')
return isWriteIndex
except Exception as e:
sys.stdout = open('/tmp/response.txt', 'w')
print (e)
sys.stdout = original_stdout
return False
def getMaxAllowedSize(diskThreshold):
# Returns the total disk space of all nodes combined multiplied by the pre-defined threshold
original_stdout = sys.stdout
try:
es_client = getEsClient()
response = es_client.cat.allocation(format="JSON", h="disk.total", bytes="kb")
totalSize = 0
for i in range(len(response)):
totalSize = totalSize + int(response[i]["disk.total"])
return int(diskThreshold/100.0 * totalSize)
except Exception as e:
sys.stdout = open('/tmp/response.txt', 'w')
print(e)
sys.stdout = original_stdout
return -1
def getDeletableIndices(index, alias, maxAllowedSize, indicesSizeCounter, indicesToDelete):
# Returns a list of indices from [alias] (infra, app, or audit), that should be deleted
# traverse through indices from newest to oldest, adding up their size. When the sum of sizes exceeds
# maxAllowedSize, start adding indices to the list
original_stdout = sys.stdout
try:
sizeCounter = indicesSizeCounter
indices = indicesToDelete
expectedSize = int(index['store.size']) + sizeCounter
if expectedSize < maxAllowedSize:
sizeCounter = expectedSize
else:
if indexBelongsToAlias(index['index'], alias):
indices.append(index['index'])
return indices, sizeCounter
except Exception as e:
sys.stdout = open('/tmp/response.txt', 'w')
print(e)
sys.stdout = original_stdout
return -1
def deleteByPercentage(alias, diskThreshold):
original_stdout = sys.stdout
try:
es_client = getEsClient()
# Get the maximum allowed size, that if exceeded, old indices are deleted
maxAllowedSize = getMaxAllowedSize(int(diskThreshold))
if maxAllowedSize == -1:
return False
indices = es_client.cat.indices(format="json",h="index,creation.date,store.size", s="creation.date", bytes="kb")
indicesToDelete = []
indicesSizeCounter = 0
for index in sorted(indices, key=lambda x: x['creation.date'], reverse=True):
indicesToDelete, indicesSizeCounter = getDeletableIndices(index, alias, maxAllowedSize, indicesSizeCounter, indicesToDelete)
for index in indicesToDelete:
#check whether the current index in the list is a write-index
if isWriteIndex(index):
print ("Cannot delete write index ", index)
else:
es_client.indices.delete(index=index)
print ("Index ", index, " deleted")
return True
except Exception as e:
sys.stdout = open('/tmp/response.txt', 'w')
print(e)
sys.stdout = original_stdout
return False
`
const checkRollover = `
#!/bin/python
import json,sys
try:
fileToRead = sys.argv[1]
currentIndex = sys.argv[2]
except IndexError:
raise SystemExit(f"Usage: {sys.argv[0]} <file_to_read> <current_index>")
try:
with open(fileToRead) as f:
data = json.load(f)
except ValueError:
raise SystemExit(f"Invalid JSON: {f}")
try:
if not data['acknowledged']:
if not data['rolled_over']:
for condition in data['conditions']:
if data['conditions'][condition]:
print(f"Index was not rolled over despite meeting conditions to do so: {data['conditions']}")
sys.exit(1)
print(data['old_index'])
sys.exit(0)
if data['old_index'] != currentIndex:
print(f"old index {data['old_index']} does not match expected index {currentIndex}")
sys.exit(1)
print(data['new_index'])
except KeyError as e:
raise SystemExit(f"Unable to check rollover for {data}: missing key {e}")
`
const getWriteIndex = `
#!/bin/python
import json,sys
try:
alias = sys.argv[1]
output = sys.argv[2]
except IndexError:
raise SystemExit(f"Usage: {sys.argv[0]} <write_alias> <json_response>")
try:
response = json.loads(output)
except ValueError:
raise SystemExit(f"Invalid JSON: {output}")
lastIndex = len(response) - 1
if 'error' in response[lastIndex]:
print(f"Error while attemping to determine the active write alias: {response[lastIndex]}")
sys.exit(1)
data = response[lastIndex]
try:
writeIndex = [index for index in data if data[index]['aliases'][alias].get('is_write_index')]
if len(writeIndex) > 0:
writeIndex.sort(reverse=True)
print(" ".join(writeIndex))
except:
e = sys.exc_info()[0]
raise SystemExit(f"Error trying to determine the 'write' index from {data}: {e}")
`
const getNext25Indices = `
#!/bin/python
from __future__ import print_function
import json,sys
try:
minAgeFromEpoc = sys.argv[1]
writeIndex = sys.argv[2]
output = sys.argv[3]
except IndexError:
raise SystemExit(f"Usage: {sys.argv[0]} <minAgeFromEpoc> <writeIndex> <json_response>")
try:
response = json.loads(output)
except ValueError:
raise SystemExit(f"Invalid JSON: {output}")
lastIndex = len(response) - 1
if 'error' in response[lastIndex]:
print(f"Error while attemping to determine index creation dates: {response[lastIndex]}")
sys.exit(1)
r = response[lastIndex]
indices = []
for index in r:
try:
if 'settings' in r[index]:
settings = r[index]['settings']
if 'index' in settings:
meta = settings['index']
if 'creation_date' in meta:
creation_date = meta['creation_date']
if int(creation_date) < int(minAgeFromEpoc):
indices.append(index)
else:
sys.stderr.write("'creation_date' missing from index settings: %r" % (meta))
else:
sys.stderr.write("'index' missing from setting: %r" % (settings))
else:
sys.stderr.write("'settings' missing for %r" % (index))
except:
e = sys.exc_info()[0]
sys.stderr.write("Error trying to evaluate index from '%r': %r" % (r,e))
if writeIndex in indices:
indices.remove(writeIndex)
for i in range(0, len(indices), 25):
print(','.join(indices[i:i+25]))
`
const indexManagement = `
CONNECT_TIMEOUT=${CONNECT_TIMEOUT:-30}
function getWriteIndex() {
local policy="$1"
# find out the current write index for ${POLICY_MAPPING}-write and check if there is the next generation of it
aliasResponse="$(getAlias "${policy}")"
if [ -z "$aliasResponse" ]; then
echo "Received an empty response from elasticsearch -- server may not be ready"
return 1
fi
jsonResponse="$(echo [$aliasResponse] | sed 's/}{/},{/g')"
if ! writeIndices="$(python /tmp/scripts/getWriteIndex.py "${policy}" "$jsonResponse")" ; then
echo $writeIndices
return 1
fi
writeIndex="$(ensureOneWriteIndex "$policy" "$writeIndices")"
echo $writeIndex
}
function ensureOneWriteIndex() {
local policy="$1"
local writeIndices="$2"
# first index received is the latest one
writeIndex=""
for index in $writeIndices; do
if [ -z "$writeIndex" ]; then
writeIndex="$index"
else
# extra write index -- mark it as not a write index
removeAsWriteIndexForAlias "$index" "$policy"
fi
done
echo $writeIndex
}
function getWriteAliases() {
local policy="$1"
# "_cat/aliases/${policy}*-write?h=alias" | uniq
aliasResponse="$(catWriteAliases "$policy")"
if [ -z "$aliasResponse" ]; then
echo "Received an empty response from elasticsearch -- server may not be ready"
return 1
fi
echo $aliasResponse
}
function rollover() {
local policy="$1"
local decoded="$2"
echo "========================"
echo "Index management rollover process starting for $policy"
echo ""
# get current write index
if ! writeIndex="$(getWriteIndex "${policy}-write")" ; then
echo $writeIndex
return 1
fi
echo "Current write index for ${policy}-write: $writeIndex"
# do not rollover if index is empty
echo "Checking if $writeIndex is empty"
docs_count="$(docsCountInIndex "$writeIndex")"
if [[ $docs_count -eq 0 ]] ; then
echo "Skipping rollover because index is empty"
return 0
elif [[ $docs_count -eq -1 ]] ; then
echo "There was an error calculating the number of documents"
return 1
fi
# try to rollover
responseRollover="$(rolloverForPolicy "${policy}-write" "$decoded")"
echo "Checking results from _rollover call"
if [ "$responseRollover" == False ] ; then
# already in bad state
echo "Calculating next write index based on current write index..."
indexGeneration="$(echo ${writeIndex##*-})"
writeBase="$(echo ${writeIndex%-*})"
# if we don't strip off the leading 0s it does math wrong...
generation="$(echo $indexGeneration | sed 's/^0*//')"
# pad the index name again with 0s
nextGeneration="$(printf '%06g' $(($generation + 1)))"
nextIndex="$writeBase-$nextGeneration"
else
# check response to see if it did roll over (field in response)
if ! nextIndex="$(python /tmp/scripts/checkRollover.py "/tmp/response.txt" "$writeIndex")" ; then
echo $nextIndex
return 1
fi
fi
echo "Next write index for ${policy}-write: $nextIndex"
echo "Checking if $nextIndex exists"
# if true, ensure next index was created and
# cluster permits operations on it, e.g. not in read-only
# state because of low disk space.
indexExists="$(checkIndexExists "$nextIndex")"
if [ "$indexExists" == False ] ; then
cat /tmp/response.txt
return 1
fi
echo "Checking if $nextIndex is the write index for ${policy}-write"
## if true, ensure write-alias points to next index
if ! writeIndex="$(getWriteIndex "${policy}-write")" ; then
echo $writeIndex
return 1
fi
if [ "$nextIndex" == "$writeIndex" ] ; then
echo "Done!"
return 0
fi
echo "Updating alias for ${policy}-write"
# else - try to update alias to be correct
responseUpdateWriteIndex="$(updateWriteIndex "$writeIndex" "$nextIndex" "${policy}-write")"
if [ "$responseUpdateWriteIndex" == True ] ; then
echo "Done!"
return 0
fi
cat /tmp/response.txt
return 1
}
function delete() {
local policy="$1"
ERRORS="$(mktemp /tmp/delete-XXXXXX)"
echo "========================"
echo "Index management delete process starting for $policy"
echo ""
if ! writeIndex="$(getWriteIndex "${policy}-write")" ; then
echo $writeIndex
return 1
fi
indices="$(getIndicesAgeForAlias "${policy}-write")"
echo "indices = [$indices]"
if [ -z "$indices" ]; then
echo "Received an empty response from elasticsearch -- server may not be ready"
return 1
fi
# Delete indices based on disk usage
if [ ! "$DISK_THRESHOLD" -eq "0" ]; then
if ! response=$(deleteByPercentage $policy 2 >>$ERRORS) ; then
cat $ERRORS
rm $ERRORS
return 1
fi
fi
jsonResponse="$(echo [$indices] | sed 's/}{/},{/g')"
# Delete in batches of 25 for cases where there are a large number of indices to remove
nowInMillis=$(date +%s%3N)
minAgeFromEpoc=$(($nowInMillis - $MIN_AGE))
if ! indices=$(python /tmp/scripts/getNext25Indices.py "$minAgeFromEpoc" "$writeIndex" "$jsonResponse" 2>>$ERRORS) ; then
cat $ERRORS
rm $ERRORS
return 1
fi
# Dump any findings to stdout but don't error
if [ -s $ERRORS ]; then
cat $ERRORS
rm $ERRORS
fi
if [ "${indices}" == "" ] ; then
echo "No indices to delete"
return 0
else
echo deleting indices: "${indices}"
fi
for sets in ${indices}; do
response="$(deleteIndices "${sets}")"
if [ "$response" == False ] ; then
cat /tmp/response.txt
return 1
fi
done
echo "Done!"
}
function deleteByPercentage() {
local alias=$1
python -c 'import indexManagementClient; print(indexManagementClient.deleteByPercentage("'$alias'", "'$DISK_THRESHOLD'"))'
}
function getAlias() {
local alias="$1"
python -c 'import indexManagementClient; print(indexManagementClient.getAlias("'$alias'"))'
}
function getIndicesAgeForAlias() {
local alias="$1"
python -c 'import indexManagementClient; print(indexManagementClient.getIndicesAgeForAlias("'$alias'"))'
}
function deleteIndices() {
local index="$1"
python -c 'import indexManagementClient; print(indexManagementClient.deleteIndices("'$index'"))'
}
function pruneNamespaces() {
local indexMappings="$1"
local namespacespec="$2"
local defaultAge="$3"
echo "========================"
echo "Index management prune process starting for $indexMappings"
echo "namespace_spec = $namespacespec"
python -c 'import indexManagementClient; print(indexManagementClient.deleteByQuery("'$indexMappings'","'$namespacespec'","'$defaultAge'"))'
}
function removeAsWriteIndexForAlias() {
local index="$1"
local alias="$2"
python -c 'import indexManagementClient; print(indexManagementClient.removeAsWriteIndexForAlias("'$index'","'$alias'"))' > /tmp/response.txt
}
function catWriteAliases() {
local policy="$1"
python -c 'import indexManagementClient; print(indexManagementClient.catWriteAliases("'$policy'"))'
}
function docsCountInIndex() {
local policy="$1"
python -c 'import indexManagementClient; print(indexManagementClient.docsCountInIndex("'$policy'"))'
}
function rolloverForPolicy() {
local policy="$1"
local decoded="$2"
python -c 'import indexManagementClient; print(indexManagementClient.rolloverForPolicy("'$policy'",'$decoded'))'
}
function checkIndexExists() {
local index="$1"
python -c 'import indexManagementClient; print(indexManagementClient.checkIndexExists("'$index'"))'
}
function updateWriteIndex() {
currentIndex="$1"
nextIndex="$2"
alias="$3"
python -c 'import indexManagementClient; print(indexManagementClient.updateWriteIndex("'$currentIndex'","'$nextIndex'","'$alias'"))'
}
`
const rolloverScript = `
set -euo pipefail
source /tmp/scripts/indexManagement
decoded=$(echo $PAYLOAD | base64 -d)
# need to get a list of all mappings under ${POLICY_MAPPING}, drop suffix '-write' iterate over
writeAliases="$(getWriteAliases "$POLICY_MAPPING")"
for aliasBase in $writeAliases; do
alias="$(echo $aliasBase | sed 's/-write$//g')"
if ! rollover "$alias" "$decoded" ; then
exit 1
fi
done
`
const deleteScript = `
set -uo pipefail
source /tmp/scripts/indexManagement
# need to get a list of all mappings under ${POLICY_MAPPING}, drop suffix '-write' iterate over
writeAliases="$(getWriteAliases "$POLICY_MAPPING")"
for aliasBase in $writeAliases; do
alias="$(echo $aliasBase | sed 's/-write$//g')"
if ! delete "$alias" ; then
exit 1
fi
done
`
const deleteThenRolloverScript = `
set -uo pipefail
/tmp/scripts/delete
delete_rc=$?
/tmp/scripts/rollover
rollover_rc=$?
if [ $delete_rc -ne 0 ] || [ $rollover_rc -ne 0 ]; then
exit 1
fi
exit 0
`
const pruneNamespacesScript = `
set -uo pipefail
source /tmp/scripts/indexManagement
DEFAULT_AGE="7d"
if [ -z "$NAMESPACE_SPECS" ] ; then
echo "No namespaces to prune"
exit 1
fi
namespaceSpec="$(echo $NAMESPACE_SPECS | sed 's/\"/\\\"/g')"
# Prune namespaces runs on all current index patterns
indexMappings="$(echo $POLICY_MAPPING*)"
if ! pruneNamespaces "$indexMappings" "$namespaceSpec" "$DEFAULT_AGE" ; then
exit 1
fi
exit 0
`
var scriptMap = map[string]string{
"delete": deleteScript,
"rollover": rolloverScript,
"delete-then-rollover": deleteThenRolloverScript,
"prune-namespaces": pruneNamespacesScript,
"indexManagement": indexManagement,
"getWriteIndex.py": getWriteIndex,
"checkRollover.py": checkRollover,
"getNext25Indices.py": getNext25Indices,
"indexManagementClient.py": indexManagementClient,
}