+
@@ -308,6 +308,12 @@ experiments:
function startProgrammingMode() {
+ if (programming_mode_switch) {
+ programming_mode_switch = false;
+ } else {
+ programming_mode_switch = true;
+ }
+
if (chaos_program_screen.style.display === "none") {
chaos_program_screen.style.display = "block";
} else {
diff --git a/html5/kubeinvaders.js b/html5/kubeinvaders.js
index 8d0dc3a..37723cf 100644
--- a/html5/kubeinvaders.js
+++ b/html5/kubeinvaders.js
@@ -22,6 +22,7 @@ var randomFactor = 10;
// pods list from kubernetes
var pods = [];
var game_mode_switch = false;
+var programming_mode_switch = false;
var game_buttons = document.getElementById("game-buttons");
var game_screen = document.getElementById("game-screen");
@@ -206,7 +207,7 @@ function runChaosProgram() {
oReq.onreadystatechange = function () {
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
//console.log(this.responseText);
- $('#alert_placeholder2').text('New Chaos Program has been started.');
+ $('#alert_placeholder').text(this.responseText);
}
};;
oReq.setRequestHeader("Content-Type", "application/json");
@@ -703,7 +704,7 @@ window.setInterval(function setAliens() {
}, 1000)
window.setInterval(function metrics() {
- if (game_mode_switch) {
+ if (game_mode_switch || programming_mode_switch) {
getMetrics()
}
}, 2000)
diff --git a/scripts/programming_mode.lua b/scripts/programming_mode.lua
index 88538a9..76a50d5 100644
--- a/scripts/programming_mode.lua
+++ b/scripts/programming_mode.lua
@@ -21,12 +21,15 @@ local body_data = ngx.req.get_body_data()
ngx.log(ngx.ERR, "[programming_mode]" .. body_data)
-file = io.open("/tmp/experiments.yaml", "w")
+math.randomseed(os.clock()*100000000000)
+local rand = math.random(999, 9999)
+file_name = "/tmp/experiments-".. rand ..".yaml"
+file = io.open(file_name, "w")
io.output(file)
io.write(body_data)
io.close(file)
-local handle = io.popen("python3 /opt/programming_mode/start.py")
+local handle = io.popen("python3 /opt/programming_mode/start.py " .. file_name .. " " .. k8s_url)
local result = handle:read("*a")
ngx.say("Chaos program has been started...")
diff --git a/scripts/programming_mode/requirements.txt b/scripts/programming_mode/requirements.txt
index 53e3485..6abc4e4 100644
--- a/scripts/programming_mode/requirements.txt
+++ b/scripts/programming_mode/requirements.txt
@@ -1,3 +1,4 @@
-yaml
+redis
+pyyaml
requests
kubernetes
diff --git a/scripts/programming_mode/start.py b/scripts/programming_mode/start.py
index 67e5be0..7c3091f 100644
--- a/scripts/programming_mode/start.py
+++ b/scripts/programming_mode/start.py
@@ -2,12 +2,14 @@ from asyncio.log import logger
import yaml
import logging
import os
+import sys
from kubernetes import client, config
from kubernetes.client.rest import ApiException
import requests
from string import Template
import string
import random
+import redis
def create_container(image, name, command, args):
container = client.V1Container(
@@ -50,17 +52,19 @@ def create_job(job_name, pod_template):
logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))
logging.info('Starting script for KubeInvaders programming mode')
-with open("./experiments.yaml", 'r') as stream:
+with open(sys.argv[1], 'r') as stream:
try:
parsed_yaml=yaml.safe_load(stream)
logging.info(f"Parsed yaml => {parsed_yaml}")
except yaml.YAMLError as exc:
print(exc)
+r = redis.Redis(unix_socket_path='/tmp/redis.sock')
+
configuration = client.Configuration()
token = os.environ["TOKEN"]
configuration.api_key = {"authorization": f"Bearer {token}"}
-configuration.host = os.environ["ENDPOINT"]
+configuration.host = sys.argv[2]
configuration.insecure_skip_tls_verify = True
configuration.verify_ssl = False
@@ -97,4 +101,16 @@ for exp in parsed_yaml["experiments"]:
pod_template = create_pod_template(exp["name"], container)
logging.info(f"Creating job {job_name}")
job_def = create_job(job_name, pod_template)
- batch_api.create_namespaced_job('kubeinvaders', job_def)
\ No newline at end of file
+
+ try:
+ batch_api.create_namespaced_job('kubeinvaders', job_def)
+ except ApiException as e:
+ logging.info(e)
+ quit()
+
+ if r.exists('chaos_node_jobs_total') == 1:
+ r.incr('chaos_node_jobs_total')
+ else:
+ r.set("chaos_node_jobs_total", 1)
+
+os.remove(sys.argv[1])