Files
kubeinvaders/main/spaceship.script
Eugenio Marzo a40c7ea851 create bundle
2021-01-02 20:42:00 +01:00

293 lines
8.4 KiB
Plaintext

current_pods = {}
conf = {}
check_current_pods = false
endpoint = ""
token = ""
namespace = ""
namespace_list = {}
namespace_cnt = 0
namespace_total = 0
automatic = false
last_namespace_change = false
last_hit_time = os.clock()
alien_proximity_factor = 15
hit_cpu_time_rate_limit = 0
pod_update_time = 0.5
kubernetes_pods = {}
last_pod_log = false
last_pod_log_time = false
start_chaos = false
bullet_collision_action = "delete_pod"
-- KubeLinter attributes
kubelinter_url = ""
kubelinter_pod = false
kubelinter_table = ""
kubelinter_read = false
kubelinter_index = 0
local COLLISION_RESPONSE = hash("collision_response")
local timer = require "main.mymodules.timer"
local spaceship = require "main.mymodules.spaceship"
local help = require "main.mymodules.help"
local pod_mod = require "main.mymodules.pod"
local pod_api = require "main.mymodules.pod_api"
local press_play = require "main.mymodules.press_play"
local kubelinter = require "main.mymodules.kubelinter"
function start_chaos()
msg.post("ui#gui", "mode",{ mode = "Press \"a\" to use automatic pilot" })
local path = os.getenv("HOME") .. "/.KubeInv.json"
local contents = ""
conf = {}
local file = io.open( path, "r" )
if file then
local contents = file:read( "*a" )
conf = json.decode(contents);
io.close(file)
else
conf["token"] = os.getenv("TOKEN")
conf["endpoint"] = os.getenv("ENDPOINT")
conf["namespace"] = os.getenv("NAMESPACE")
if os.getenv("ALIENPROXIMITY") ~= nil then
alien_proximity_factor = tonumber(os.getenv("ALIENPROXIMITY"))
end
if os.getenv("HITSLIMIT") ~= nil then
hit_cpu_time_rate_limit = tonumber(os.getenv("HITSLIMIT"))
end
if os.getenv("UPDATETIME") ~= nil then
pod_update_time = tonumber(os.getenv("UPDATETIME"))
end
end
if conf["token"] == nil or conf["endpoint"] == nil or conf["namespace"] == nil then
msg.post("ui#gui", "howto",{ howto_message = "Please create .KubeInv.json in " .. os.getenv("HOME") .. "\nThe file .KubeInv.json should be like this \n{\"endpoint\": \"http://ocmaster39\",\"token\": \"xxxx\",\"namespace\": \"foobar\"}"})
else
print( "token: ", conf["token"] )
print( "endpoint: ", conf["endpoint"] )
print( "namespace: ", conf["namespace"] )
endpoint = conf["endpoint"]
token = conf["token"]
namespace_cnt = 0
for i in string.gmatch(conf["namespace"], "[^,]+") do
namespace_list[namespace_cnt] = i
namespace_cnt = namespace_cnt + 1
end
namespace_total = table.getn(namespace_list)
namespace_cnt = 0
namespace = namespace_list[0]
info = "Endpoint: " .. conf["endpoint"] .. "\n\n" .. "Namespace: " .. namespace
kubelinter_url = endpoint .. "/kube-linter"
msg.post("ui#gui", "info",{ infomessage = info })
msg.post(".", "acquire_input_focus")
headers = {
["Accept"] = "application/json",
["Content-Type"] = "application/json",
["Authorization"] = "Bearer " .. token
}
timer.repeat_seconds(pod_update_time, function()
pod_api.update_pod()
pod_mod.swap_pod(kubernetes_pods)
end)
timer.repeat_seconds(1, function()
msg.post("ui#gui","pod_name",{ pod_name = false, position = go.get_position()})
if kubelinter_pod then
kubelinter.run(pod_name)
kubelinter_pod = false
end
end)
timer.repeat_seconds(1, function()
spaceship.auto()
end)
pod_api.set_pods()
end
started_chaos = true
go.delete('/start_chaos')
end
function reverse(tbl)
for i=1, math.floor(#tbl / 2) do
local tmp = tbl[i]
tbl[i] = tbl[#tbl - i + 1]
tbl[#tbl - i + 1] = tmp
end
end
function init(self)
msg.post(".", "acquire_input_focus")
end
function update(self, dt)
timer.update(dt)
end
function on_input(self, action_id, action)
p = go.get_position()
if action_id == hash("up") then
msg.post("ui#gui","pod_log",{infomessage = ''})
last_pod_log = false
if p.y < 1000 then
p.y = p.y + 4
go.set_position(p)
end
elseif action_id == hash("down") then
msg.post("ui#gui","pod_log",{infomessage = ''})
last_pod_log = false
if p.y > 1 then
p.y = p.y - 4
go.set_position(p)
end
elseif action_id == hash("left") then
msg.post("ui#gui","pod_log",{infomessage = ''})
last_pod_log = false
if p.x > 1 then
p.x = p.x - 4
go.set_position(p)
end
elseif action_id == hash("right") then
msg.post("ui#gui","pod_log",{infomessage = ''})
last_pod_log = false
if p.x < 1800 then
p.x = p.x + 4
go.set_position(p)
end
elseif action_id == hash("infopod") then
pod_mod.print_pod_name()
elseif action_id == hash("refresh_pod_log") then
last_pod_log = false
elseif action_id == hash("help") then
print "help!"
help.get_help()
elseif action_id == hash("quit_help") then
help.quit_help()
elseif action_id == hash("automatic") then
automatic = true
msg.post("ui#gui", "mode",{ mode = "Press \"m\" to switch to manual mode"})
elseif action_id == hash("manual") then
print ("[change_mode] manual")
automatic = false
msg.post("ui#gui", "mode",{ mode = "Press \"a\" to use automatic pilot"})
elseif action_id == hash("namespace") then
print ("[change_namespace] change namespaces requested by user")
msg.post("ui#gui", "error",{ errormessage = "changing namespace!" })
-- Returns an approximation of the amount in seconds of CPU time used by the program.
current_time = os.clock()
if last_namespace_change ~= false then
diff_time = current_time - last_namespace_change
print ("[change_namespace] diff_time ".. diff_time)
else
diff_time = 99
end
if diff_time > 0.1 then
for i,value in ipairs(current_pods) do
go.delete(value["id"])
end
current_pods = {}
if namespace_cnt > (namespace_total - 1) then
print ("[change_namespace] return to first namespace")
namespace = namespace_list[0]
namespace_cnt = 0
else
namespace_cnt = namespace_cnt + 1
print ("[change_namespace] go to namespace " .. namespace_cnt)
namespace = namespace_list[namespace_cnt]
end
end
last_namespace_change = os.clock()
info = "Endpoint: " .. conf["endpoint"] .. "\n\n" .. "Namespace: " .. namespace
msg.post("ui#gui", "info",{ infomessage = info })
elseif ((action_id == hash("space")) or (action_id == hash("kubelinter"))) and action.pressed then
if action_id == hash("space") then
bullet_collision_action = "delete_pod"
elseif hash("kubelinter") then
bullet_collision_action = "kubelinter"
end
if kubelinter_read then
print("Reading kubelinter table")
print("Current Kubelinter table index is " .. kubelinter_index)
print("Kubelinter table size is " .. table.getn(kubelinter_table))
if table.getn(kubelinter_table) == 0 then
kubelinter_read = false
end
for i,value in ipairs(kubelinter_table) do
if value ~= "" then
if i > kubelinter_index then
msg.post("ui#gui", "set_kubelinter_text",{ kubelintermessage = value })
kubelinter_index = kubelinter_index + 1
break
end
if kubelinter_index == (table.getn(kubelinter_table) - 1) then
kubelinter_read = false
msg.post("ui#gui", "set_kubelinter_text",{ kubelintermessage = "" })
msg.post("ui#gui","open_kubelinter_box",{ position = vmath.vector3(-850, -750, 0) })
end
end
end
return
end
msg.post("ui#gui", "error",{ errormessage = "" })
-- Returns an approximation of the amount in seconds of CPU time used by the program.
current_hit_time = os.clock()
if automatic == false then
shot = true
if ((current_hit_time - last_hit_time) < hit_cpu_time_rate_limit) then
shot = false
end
if shot == true then
local pos = go.get_position()
local to = pos.y + 2 * 64
local bullet = factory.create("/bullet#bulletfactory", pos)
go.animate(bullet, "position.y", go.PLAYBACK_ONCE_FORWARD, 1200, go.EASING_INQUAD, 1,0,function()
go.delete(bullet)
end)
last_hit_time = os.clock()
end
end
end
end
function on_message(self, message_id, message, sender)
current_time = os.clock()
print('DEBUG' .. message_id)
if message_id == COLLISION_RESPONSE and started_chaos then
for i,value in ipairs(current_pods) do
if (message["other_id"] == value["id"]) then
pod_name = value["pod_name"]
end
end
if last_pod_log_time ~= false then
diff_time = current_time - last_pod_log_time
else
diff_time = 99
end
if last_pod_log ~= pod_name and diff_time > 0.2 then
last_pod_log = pod_mod.print_pod_log(message["other_id"])
print("last_pod_log " .. last_pod_log)
print("pod_name " .. pod_name)
last_pod_log_time = os.clock()
end
elseif message_id == hash('start_chaos') then
start_chaos()
end
end