-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathKubeInvaders.conf
704 lines (602 loc) · 24.7 KB
/
KubeInvaders.conf
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
# lua_package_path '/usr/share/lua/5.1/?.lua;;';
server {
listen 8080 default_server;
root /var/www/html/;
index index.html;
access_log off;
set_by_lua_block $application_url {
return os.getenv("APPLICATION_URL")
}
set_by_lua_block $disable_tls {
local env_var = os.getenv("DISABLE_TLS")
if env_var then
return env_var
else
return "false"
end
}
set_by_lua_block $demo_mode {
local env_var = os.getenv("PLATFORM_ENGINEERING_DEMO_MODE")
if env_var then
return env_var
else
return "false"
end
}
set_by_lua_block $selected_env_vars {
local selected_env_vars = {"KUBERNETES_SERVICE_HOST", "KUBERNETES_SERVICE_PORT_HTTPS", "NAMESPACE", "DISABLE_TLS", "APPLICATION_URL"}
local result = {}
for _, var_name in ipairs(selected_env_vars) do
local value = os.getenv(var_name)
if value then
table.insert(result, var_name .. " = " .. value)
else
table.insert(result, var_name .. " = nil")
end
end
return table.concat(result, ", ")
}
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
try_files $uri $uri/ =404;
add_header Last-Modified $date_gmt;
if_modified_since off;
expires off;
etag off;
sub_filter_types "*";
sub_filter demo_mode_placeholder $demo_mode;
sub_filter application_url_placeholder $application_url;
sub_filter disable_tls_placeholder $disable_tls;
sub_filter selected_env_vars_placeholder $selected_env_vars;
}
location /kube/ingresses {
access_by_lua_file "/usr/local/openresty/nginx/conf/kubeinvaders/ingress.lua";
}
location /kube/vm {
access_by_lua_file "/usr/local/openresty/nginx/conf/kubeinvaders/vm.lua";
}
location /kube/vm_reboot {
access_by_lua_file "/usr/local/openresty/nginx/conf/kubeinvaders/vm_reboot.lua";
}
location /kube/chaos/programming_mode {
access_by_lua_file "/usr/local/openresty/nginx/conf/kubeinvaders/programming_mode.lua";
}
location /kube/pods {
access_by_lua_file "/usr/local/openresty/nginx/conf/kubeinvaders/pod.lua";
}
location /kube/delete/pods {
access_by_lua_file "/usr/local/openresty/nginx/conf/kubeinvaders/pod.lua";
}
location /kube/nodes {
access_by_lua_file "/usr/local/openresty/nginx/conf/kubeinvaders/node.lua";
}
location /kube/chaos/nodes {
access_by_lua_file "/usr/local/openresty/nginx/conf/kubeinvaders/chaos-node.lua";
}
location /kube/kube-linter {
access_by_lua_file "/usr/local/openresty/nginx/conf/kubeinvaders/kube-linter.lua";
}
location /kube/endpoint {
content_by_lua_block {
ngx.header['Access-Control-Allow-Origin'] = '*'
ngx.header['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
ngx.header['Access-Control-Allow-Headers'] = 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'
ngx.header['Access-Control-Expose-Headers'] = 'Content-Length,Content-Range';
if os.getenv("APPLICATION_URL") == nil then
ngx.say("Error! APPLICATION_URL is nil!")
else
ngx.say(os.getenv("APPLICATION_URL"))
end
}
}
location /chaos/redis/get {
content_by_lua_block {
local redis = require "resty.redis"
local red = redis:new()
local okredis, errredis = red:connect("unix:/tmp/redis.sock")
ngx.header['Access-Control-Allow-Origin'] = '*'
ngx.header['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
ngx.header['Access-Control-Allow-Headers'] = 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'
ngx.header['Access-Control-Expose-Headers'] = 'Content-Length,Content-Range';
ngx.req.read_body()
local data = ngx.req.get_body_data()
local args = ngx.req.get_uri_args()
local key = args["key"]
if red:exists(key) == 0 then
ngx.say("Key not found")
else
ngx.say(tostring(red:get(key)))
end
}
}
location /chaos/redis/set {
content_by_lua_block {
local redis = require "resty.redis"
local red = redis:new()
local okredis, errredis = red:connect("unix:/tmp/redis.sock")
ngx.header['Access-Control-Allow-Origin'] = '*'
ngx.header['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
ngx.header['Access-Control-Allow-Headers'] = 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'
ngx.header['Access-Control-Expose-Headers'] = 'Content-Length,Content-Range';
ngx.req.read_body()
local data = ngx.req.get_body_data()
local args = ngx.req.get_uri_args()
local key = args["key"]
ngx.log(ngx.INFO, "[KUBEPING] Setting key " .. key .. " with data |" .. tostring(data) .. "|")
ngx.say(tostring(red:set(key, tostring(data))))
if key == "kubeping" and data == "1" then
local http = require("socket.http")
local function get_data(url)
local respbody = {}
local code, response = http.request(url)
if code == 200 then
local json = require("json")
respbody = json.decode(response)
else
print("Errore nella richiesta: " .. code)
end
return respbody
end
local data = get_data("https://devopstribe.it/kubeping?msg=" .. args["msg"])
print(data.result)
end
ngx.say("OK")
}
}
location /kube/namespaces {
content_by_lua_block {
ngx.header['Access-Control-Allow-Origin'] = '*'
ngx.header['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
ngx.header['Access-Control-Allow-Headers'] = 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'
ngx.header['Access-Control-Expose-Headers'] = 'Content-Length,Content-Range';
-- TO DO
-- Check when NAMESPACE is nil
ngx.say(os.getenv("NAMESPACE"))
}
}
location /codename {
content_by_lua_block {
local open = io.open
local function read_rand_line_from_file(path)
local handle = io.popen("shuf -n 1 " .. path)
local result = handle:read("*a")
local rc = handle:close()
return result
end
local random_word = read_rand_line_from_file("/usr/local/openresty/nginx/conf/kubeinvaders/data/codenames.txt")
local redis = require "resty.redis"
local red = redis:new()
local okredis, errredis = red:connect("unix:/tmp/redis.sock")
ngx.header['Access-Control-Allow-Origin'] = '*'
ngx.header['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
ngx.header['Access-Control-Allow-Headers'] = 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'
ngx.header['Access-Control-Expose-Headers'] = 'Content-Length,Content-Range';
while (red:get("latest_codename") == random_word)
do
random_word = read_rand_line_from_file("/usr/local/openresty/nginx/conf/kubeinvaders/data/codenames.txt")
end
red:set("latest_codename", random_word)
-- ngx.log(ngx.INFO, "[programming_mode_codename] Choosing random word " .. random_word)
ngx.say(random_word)
}
}
location /metrics {
default_type text/html;
content_by_lua_block {
local redis = require "resty.redis"
local red = redis:new()
local okredis, errredis = red:connect("unix:/tmp/redis.sock")
for i, res in ipairs(red:keys("*total*")) do
if string.find(res, "chaos_node_jobs_total_on") then
local node = string.gsub(res, "chaos_node_jobs_total_on_", "")
local metric = "chaos_jobs_node_count{node=\"".. node .."\"}"
ngx.say(metric .. " " .. red:get(res))
elseif string.find(res, "deleted_pods_total_on") then
local namespace = string.gsub(res, "deleted_pods_total_on_", "")
local metric = "deleted_namespace_pods_count{namespace=\"".. namespace .."\"}"
ngx.say(metric .. " " .. red:get(res))
end
end
for i, res in ipairs(red:keys("pods_match_regex:*")) do
ngx.say(res .. " " .. red:get(res))
end
local metrics = {
'chaos_node_jobs_total',
'deleted_pods_total',
'fewer_replicas_seconds',
'latest_fewer_replicas_seconds',
'pods_not_running_on_selected_ns',
'current_chaos_job_pod',
'pods_match_regex'
}
local metric_name = ""
local metric_value = ""
for key, value in ipairs(metrics) do
metric_name = value
if (red:exists(metric_name) == 1) then
metric_value = red:get(metric_name)
ngx.say(metric_name .. " " .. metric_value)
end
end
for i, res in ipairs(red:keys("chaos_jobs_status*")) do
ngx.say(res .. " " .. red:get(res))
end
}
}
location /chaos_jobs_pod_phase {
default_type text/html;
content_by_lua_block {
local redis = require "resty.redis"
local red = redis:new()
local okredis, errredis = red:connect("unix:/tmp/redis.sock")
for i, res in ipairs(red:keys("chaos_jobs_pod_phase*")) do
ngx.say(res .. " " .. red:get(res))
end
}
}
location /kube/chaos/containers {
lua_need_request_body 'on';
access_by_lua_file "/usr/local/openresty/nginx/conf/kubeinvaders/chaos-containers.lua";
}
location /chaos/logs/keepalive {
content_by_lua_block {
local redis = require "resty.redis"
local red = redis:new()
local args = ngx.req.get_uri_args()
ngx.header['Access-Control-Allow-Origin'] = '*'
ngx.header['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
ngx.header['Access-Control-Allow-Headers'] = 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'
ngx.header['Access-Control-Expose-Headers'] = 'Content-Length,Content-Range';
ngx.req.read_body()
local data = ngx.req.get_body_data()
local okredis, errredis = red:connect("unix:/tmp/redis.sock")
local logid = args["logid"]
red:set("do_not_clean_log:" .. logid, "1")
red:expire("do_not_clean_log:" .. logid, "20")
red:set("logs_enabled:" .. logid, "1")
red:expire("logs_enabled:" .. logid, "10")
if red:exists("log_status:".. logid) then
ngx.say(red:get("log_status:".. logid))
else
ngx.say('Waiting for log collector status...')
end
}
}
location /chaos/logs/count {
default_type text/html;
content_by_lua_block {
local redis = require "resty.redis"
local red = redis:new()
local args = ngx.req.get_uri_args()
ngx.header['Access-Control-Allow-Origin'] = '*'
ngx.header['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
ngx.header['Access-Control-Allow-Headers'] = 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'
ngx.header['Access-Control-Expose-Headers'] = 'Content-Length,Content-Range';
ngx.req.read_body()
local data = ngx.req.get_body_data()
local okredis, errredis = red:connect("unix:/tmp/redis.sock")
local logid = args["logid"]
local res, err = red:get("logs:chaoslogs-" .. logid .. "-count")
if res == ngx.null then
ngx.say("0")
else
ngx.say(res)
end
}
}
location /chaos/logs {
default_type text/html;
content_by_lua_block {
local redis = require "resty.redis"
local red = redis:new()
local args = ngx.req.get_uri_args()
ngx.header['Access-Control-Allow-Origin'] = '*'
ngx.header['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
ngx.header['Access-Control-Allow-Headers'] = 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'
ngx.header['Access-Control-Expose-Headers'] = 'Content-Length,Content-Range';
ngx.req.read_body()
local data = ngx.req.get_body_data()
local okredis, errredis = red:connect("unix:/tmp/redis.sock")
local logid = args["logid"]
local pos = args["pos"]
local res = red:get("logs:chaoslogs-" .. logid .. "-" .. pos)
ngx.say(res)
}
}
location /chaos/loadpreset {
content_by_lua_block {
local redis = require "resty.redis"
local red = redis:new()
local args = ngx.req.get_uri_args()
ngx.header['Access-Control-Allow-Origin'] = '*'
ngx.header['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
ngx.header['Access-Control-Allow-Headers'] = 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'
ngx.header['Access-Control-Expose-Headers'] = 'Content-Length,Content-Range';
ngx.req.read_body()
local okredis, errredis = red:connect("unix:/tmp/redis.sock")
local key_name = args["lang"] .. "_" .. args["name"]
local res, err = red:get(key_name)
if res == ngx.null then
ngx.say(err)
else
ngx.say(res)
end
}
}
location /chaos/loadpreset/reset {
content_by_lua_block {
local function mysplit (inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
local redis = require "resty.redis"
local red = redis:new()
local args = ngx.req.get_uri_args()
ngx.header['Access-Control-Allow-Origin'] = '*'
ngx.header['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
ngx.header['Access-Control-Allow-Headers'] = 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'
ngx.header['Access-Control-Expose-Headers'] = 'Content-Length,Content-Range';
ngx.req.read_body()
local okredis, errredis = red:connect("unix:/tmp/redis.sock")
local key_name = args["lang"] .. "_" .. args["name"]
-- ngx.log(ngx.INFO, "[PRESETS-RESET] The item " .. key_name .. " should be removed from the key presets_list")
local handle = io.popen("redis-cli --scan --pattern " .. key_name .. " | xargs redis-cli del")
local result = handle:read("*a")
local rc = handle:close()
local res = red:get("presets_list")
if res == ngx.null then
ngx.say("There are no presets saved yet")
else
local preset = "";
local new_preset_list = "";
local cnt = 0;
for key, value in ipairs(mysplit(res, ",")) do
-- ngx.log(ngx.INFO, "[PRESETS-RESET] Split the key presets_list. Current preset is: " .. value)
if value ~= key_name then
preset = value
if cnt > 0 then
new_preset_list = new_preset_list .. "," .. preset
else
new_preset_list = preset
end
end
cnt = cnt + 1
end
-- ngx.log(ngx.INFO, "[PRESETS-RESET] New key presets_list is: " .. new_preset_list)
red:set("presets_list", new_preset_list)
end
}
}
location /chaos/loadpreset/savedpresets {
default_type text/html;
content_by_lua_block {
local redis = require "resty.redis"
local red = redis:new()
local args = ngx.req.get_uri_args()
ngx.header['Access-Control-Allow-Origin'] = '*'
ngx.header['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
ngx.header['Access-Control-Allow-Headers'] = 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'
ngx.header['Access-Control-Expose-Headers'] = 'Content-Length,Content-Range';
local okredis, errredis = red:connect("unix:/tmp/redis.sock")
local res, err = red:get("presets_list")
if res == ngx.null then
ngx.say(err)
else
ngx.say(res)
-- ngx.log(ngx.INFO, "[PRESETS_LIST] " .. res)
end
}
}
location /chaos/report/keepalive {
content_by_lua_block {
local function check_if_string_contain_project(project_list, project)
if string.find(project_list, project) then
return true
end
return false
end
local function add_project(project_list, new_project)
local new_project_list = project_list .. "," .. new_project
return new_project_list
end
local function check_if_redis_key_exists(redis, key)
if redis:exists(key) == 1 then
return true
end
return false
end
local redis = require "resty.redis"
local red = redis:new()
local args = ngx.req.get_uri_args()
ngx.header['Access-Control-Allow-Origin'] = '*'
ngx.header['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
ngx.header['Access-Control-Allow-Headers'] = 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'
ngx.header['Access-Control-Expose-Headers'] = 'Content-Length,Content-Range';
ngx.req.read_body()
local data = ngx.req.get_body_data()
local okredis, errredis = red:connect("unix:/tmp/redis.sock")
if check_if_redis_key_exists(red, "chaos_report_project_list") then
if not check_if_string_contain_project(red:get("chaos_report_project_list"), args["project"]) then
red:set("chaos_report_project_list", add_project(red:get("chaos_report_project_list"), args["project"]))
end
else
red:set("chaos_report_project_list", args["project"])
end
red:expire("chaos_report_project_list", 5)
}
}
location /chaos/report/save {
content_by_lua_block {
local function check_if_string_contain_project(project_list, project)
if string.find(project_list, project) then
return true
end
return false
end
local function add_project(project_list, new_project)
local new_project_list = project_list .. "," .. new_project
return new_project_list
end
local function check_if_redis_key_exists(redis, key)
if redis:exists(key) == 1 then
return true
end
return false
end
local redis = require "resty.redis"
local red = redis:new()
local args = ngx.req.get_uri_args()
ngx.header['Access-Control-Allow-Origin'] = '*'
ngx.header['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
ngx.header['Access-Control-Allow-Headers'] = 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'
ngx.header['Access-Control-Expose-Headers'] = 'Content-Length,Content-Range';
ngx.req.read_body()
local data = ngx.req.get_body_data()
local okredis, errredis = red:connect("unix:/tmp/redis.sock")
red:set("chaos_report_project_" .. args["project"], data)
if check_if_redis_key_exists(red, "chaos_report_project_list") then
if not check_if_string_contain_project(red:get("chaos_report_project_list"), args["project"]) then
red:set("chaos_report_project_list", add_project(red:get("chaos_report_project_list"), args["project"]))
end
else
red:set("chaos_report_project_list", args["project"])
end
red:expire("chaos_report_project_list", 5)
-- ngx.log(ngx.INFO, "[CHAOS-REPORT-SAVE] " .. data)
}
}
location /chaos/loadpreset/save {
content_by_lua_block {
local redis = require "resty.redis"
local red = redis:new()
local args = ngx.req.get_uri_args()
ngx.header['Access-Control-Allow-Origin'] = '*'
ngx.header['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
ngx.header['Access-Control-Allow-Headers'] = 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'
ngx.header['Access-Control-Expose-Headers'] = 'Content-Length,Content-Range';
ngx.req.read_body()
local data = ngx.req.get_body_data()
local okredis, errredis = red:connect("unix:/tmp/redis.sock")
local key_name = args["lang"] .. "_" .. args["name"]
local all_presets_key = ""
if data ~= nil then
-- ngx.log(ngx.INFO, "[SAVE-PRESETS] lang:" .. args["lang"] .. ", name:" .. args["name"] .. ", payload:" .. data .. "\n key_name:" .. key_name .. "\n key_exists: " .. red:exists(key_name))
-- if (red:exists(key_name) == 0) then
red:set(key_name, data)
-- ngx.log(ngx.INFO, "[SAVE-PRESETS] key_name:" .. key_name .. ", data:" .. data)
local res, err = red:get(key_name)
if res == ngx.null then
ngx.say("error: " .. args["name"] .. " has not been saved.")
else
local file = io.open("/var/www/html/" .. key_name, "w")
io.output(file)
io.write(data)
io.close(file)
ngx.say(args["name"] .. " has been saved.")
local redis_presets_list = red:get("presets_list")
if redis_presets_list == ngx.null then
red:set("presets_list", key_name)
else
local presets_list = redis_presets_list .. "," .. key_name
red:set("presets_list", presets_list)
end
end
-- end
else
ngx.say("Error in payload sent by web interface.")
end
}
}
location /chaos/programs/json-flow {
content_by_lua_block {
local function key_exists(data, mykey)
for key, value in pairs(data) do
if key == mykey then
return true
end
end
return false
end
local function is_key_empty(data, mykey)
local cnt = 0
for key, value in pairs(data[mykey]) do
cnt = cnt + 1
end
if cnt > 0 then
return false
end
return true
end
local lyaml = require "lyaml"
local json = require 'lunajson'
math.randomseed(os.clock()*100000000000)
local rand = math.random(999, 9999)
local file_name = "/tmp/chaosprogram" .. rand
ngx.header['Access-Control-Allow-Origin'] = '*'
ngx.header['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
ngx.header['Access-Control-Allow-Headers'] = 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'
ngx.header['Access-Control-Expose-Headers'] = 'Content-Length,Content-Range';
ngx.req.read_body()
local data = ngx.req.get_body_data()
ngx.log(ngx.INFO, "[PROGRAMMING_MODE] Chaos program payload sent from client: " .. data)
ngx.log(ngx.INFO, "[PROGRAMMING_MODE] Write temp file: " .. file_name)
local yamlfile = io.open(file_name, "w")
yamlfile:write(data)
yamlfile:close()
ngx.log(ngx.INFO, "[PROGRAMMING_MODE] Checking if " .. file_name .. " is a valid YAML file")
local handle = io.popen("python3 -c 'import yaml, sys; print(yaml.safe_load(sys.stdin))' < " .. file_name .. " ; echo $? > " .. file_name .. ".check")
local result = handle:read("*a")
handle = io.popen("cat " .. file_name .. ".check")
result = handle:read("*a")
ngx.log(ngx.INFO, "[PROGRAMMING_MODE] Exit code for checking YAML syntax of " .. file_name .. " is " .. result)
if result == "0\n" then
ngx.log(ngx.INFO, "[PROGRAMMING_MODE] YAML Syntax is OK")
else
ngx.log(ngx.INFO, "[PROGRAMMING_MODE] YAML Syntax is NOT OK")
handle = io.popen("rm -f " .. file_name .. ".check")
handle:read("*a")
ngx.status = 400
ngx.say("Invalid YAML Chaos Program")
end
if data == nil then
error = "[PROGRAMMING_MODE] No chaos program already loaded."
ngx.status = 400
ngx.say(error)
else
local yaml_data = lyaml.load(data)
if not key_exists(yaml_data, "k8s_jobs") then
error = "[PROGRAMMING_MODE] Chaos program does not contain 'jobs' key."
ngx.status = 400
ngx.say(error)
elseif not key_exists(yaml_data, "experiments") then
error = "[PROGRAMMING_MODE] Chaos program does not contain 'experiments' key."
ngx.status = 400
ngx.say(error)
elseif is_key_empty(yaml_data, "k8s_jobs") then
error = "[PROGRAMMING_MODE] Chaos program does not contain valid 'jobs' key."
ngx.status = 400
ngx.say(error)
elseif is_key_empty(yaml_data, "experiments") then
error = "[PROGRAMMING_MODE] Chaos program does not contain valid 'experiments' key."
ngx.status = 400
ngx.say(error)
else
os.remove(file_name)
local response = json.encode(yaml_data)
ngx.log(ngx.INFO, response)
ngx.status = 200
ngx.say(response)
end
end
}
}
}