diff --git a/runner/runner.py b/runner/runner.py index 3f0cb6d..9039e50 100644 --- a/runner/runner.py +++ b/runner/runner.py @@ -32,11 +32,12 @@ async def main(): print(dumps(online_devices,indent=4)) else: print(dumps(online_devices)) - else: - print("No LO flag.") if args.run: - print("run command") + if not args.command or not args.nodeids: + return + + print(args.nodeids) await session.close() diff --git a/server/ghostrunner-server b/server/ghostrunner-server index 8d09835..c035592 100755 Binary files a/server/ghostrunner-server and b/server/ghostrunner-server differ diff --git a/server/python.sh b/server/python.sh new file mode 100755 index 0000000..414809c --- /dev/null +++ b/server/python.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +./../runner/venv/bin/python3 ./../runner/runner.py diff --git a/server/src/main.go b/server/src/main.go index bae8bf4..2e0809b 100644 --- a/server/src/main.go +++ b/server/src/main.go @@ -7,7 +7,6 @@ import ( "ghostrunner-server/modules/restapi" "ghostrunner-server/modules/timekeeper" "ghostrunner-server/modules/utilities" - "ghostrunner-server/modules/wrapper" "log" ) @@ -31,15 +30,5 @@ func main() { log.Println(utilities.InfoTag, "Letting TimeKeeper take over...") log.Println(utilities.InfoTag, fmt.Sprintf("Interval set at: %d seconds.", cfg.Interval)) - onDevices, err := wrapper.PyListOnline(cfg.PyVenvName) - if err != nil { - log.Println(utilities.ErrTag, err) - } - - for index, device := range onDevices.OnlineDevices { - log.Println(index, device) - } - log.Println(len(onDevices.OnlineDevices)) - - timekeeper.KeepTime(cfg.Interval) + timekeeper.KeepTime(cfg.Interval, cfg) } diff --git a/server/src/modules/timekeeper/routine.go b/server/src/modules/timekeeper/routine.go new file mode 100644 index 0000000..11f10ea --- /dev/null +++ b/server/src/modules/timekeeper/routine.go @@ -0,0 +1,35 @@ +package timekeeper + +import ( + "ghostrunner-server/modules/database" + "ghostrunner-server/modules/utilities" + "log" +) + +func routine(cfg utilities.ConfigStruct, pyListArgs []string) { + d := listDevices(cfg, pyListArgs) // Retrieve the Online devices. + curTasks := database.RetrieveTasks() + + for index, device := range d.OnlineDevices { + log.Println(index, device) + } + + for index, task := range curTasks { + log.Println(index, task) + for _, nodeid := range task.Nodeids { + if isNodeOnline(nodeid, d.OnlineDevices) { + log.Printf("NodeID %s is online\n", nodeid) + } + } + } + +} + +func isNodeOnline(nodeid string, onlineDevices []utilities.Device) bool { + for _, device := range onlineDevices { + if device.NodeID == nodeid { + return true + } + } + return false +} diff --git a/server/src/modules/timekeeper/timekeeper.go b/server/src/modules/timekeeper/timekeeper.go index 969310e..cfed3ec 100644 --- a/server/src/modules/timekeeper/timekeeper.go +++ b/server/src/modules/timekeeper/timekeeper.go @@ -2,11 +2,17 @@ package timekeeper import ( "ghostrunner-server/modules/utilities" + "ghostrunner-server/modules/wrapper" "log" + "strings" "time" ) -func KeepTime(interval int) { +var ( // Debugging + pyListArgs = strings.Fields("-lo") +) + +func KeepTime(interval int, cfg utilities.ConfigStruct) { transInterval := time.Duration(interval) * time.Second ticker := time.NewTicker(transInterval) @@ -14,5 +20,16 @@ func KeepTime(interval int) { for t := range ticker.C { log.Println(utilities.InfoTag, "Tick at:", t) + log.Println(utilities.InfoTag, "Starting Routine.") + routine(cfg, pyListArgs) } } + +func listDevices(cfg utilities.ConfigStruct, pyArgs []string) utilities.PyOnlineDevices { + onDevices, err := wrapper.PyListOnline(cfg.PyVenvName, pyArgs) + if err != nil { + log.Println(utilities.ErrTag, err) + } + + return onDevices +} diff --git a/server/src/modules/utilities/structs.go b/server/src/modules/utilities/structs.go index 1cc60d5..09d040b 100644 --- a/server/src/modules/utilities/structs.go +++ b/server/src/modules/utilities/structs.go @@ -55,6 +55,7 @@ type Device struct { } type PyOnlineDevices struct { - OnlineDevices []Device `json:"online_devices"` - TotalDevices int `json:"total_devices"` + OnlineDevices []Device `json:"online_devices"` + OfflineDevices []Device `json:"offline_devices"` + TotalDevices int `json:"total_devices"` } diff --git a/server/src/modules/wrapper/python.go b/server/src/modules/wrapper/python.go index dc84f05..e2cd1a7 100644 --- a/server/src/modules/wrapper/python.go +++ b/server/src/modules/wrapper/python.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "ghostrunner-server/modules/utilities" + "log" "os" "os/exec" ) @@ -12,11 +13,14 @@ const ( pyFile = "./../runner/runner.py" ) -func PyListOnline(venvName string) (utilities.PyOnlineDevices, error) { +func PyListOnline(venvName string, pyArgs []string) (utilities.PyOnlineDevices, error) { pyBin := fmt.Sprintf("./../runner/%s/bin/python", venvName) - cmd := exec.Command(pyBin, pyFile, "-lo") + runtimeArgs := append([]string{pyFile}, pyArgs...) + + cmd := exec.Command(pyBin, runtimeArgs...) rawData, err := cmd.CombinedOutput() + log.Println(string(rawData)) if err != nil { cwd, _ := os.Getwd() return utilities.PyOnlineDevices{}, fmt.Errorf("python execution failed, working directory: %s", cwd)