Implemented actual tested RunEdgeApp and StopEdgeApp commands, added sleep time after container operation and before checking status
parent
0bd61ccf75
commit
3cc670b823
|
@ -5,6 +5,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
|
|
||||||
|
@ -34,6 +35,7 @@ type EdgeAppService struct {
|
||||||
|
|
||||||
const configFilename = "/edgebox-compose.yml"
|
const configFilename = "/edgebox-compose.yml"
|
||||||
const envFilename = "/edgebox.env"
|
const envFilename = "/edgebox.env"
|
||||||
|
const defaultContainerOperationSleepTime time.Duration = time.Second * 10
|
||||||
|
|
||||||
// GetEdgeApps : Returns a list of EdgeApp struct filled with information
|
// GetEdgeApps : Returns a list of EdgeApp struct filled with information
|
||||||
func GetEdgeApps() []EdgeApp {
|
func GetEdgeApps() []EdgeApp {
|
||||||
|
@ -117,7 +119,7 @@ func GetEdgeAppServices(ID string) []EdgeAppService {
|
||||||
var edgeAppServices []EdgeAppService
|
var edgeAppServices []EdgeAppService
|
||||||
|
|
||||||
for _, serviceID := range serviceSlices {
|
for _, serviceID := range serviceSlices {
|
||||||
cmdArgs = []string{"-f", utils.GetPath("wsPath") + "/docker-compose.yml", "ps", "-q", serviceID}
|
cmdArgs = []string{"-f", utils.GetPath("wsPath") + "/docker-compose.yml", "exec", "-T", serviceID, "echo", "'Service Check'"}
|
||||||
cmdResult := utils.Exec("docker-compose", cmdArgs)
|
cmdResult := utils.Exec("docker-compose", cmdArgs)
|
||||||
isRunning := false
|
isRunning := false
|
||||||
if cmdResult != "" {
|
if cmdResult != "" {
|
||||||
|
@ -133,8 +135,19 @@ func GetEdgeAppServices(ID string) []EdgeAppService {
|
||||||
// RunEdgeApp : Run an EdgeApp and return its most current status
|
// RunEdgeApp : Run an EdgeApp and return its most current status
|
||||||
func RunEdgeApp(ID string) EdgeAppStatus {
|
func RunEdgeApp(ID string) EdgeAppStatus {
|
||||||
|
|
||||||
cmdArgs := []string{"-f", utils.GetPath("wsPath") + "/docker-compose.yml", "up", ID}
|
services := GetEdgeAppServices(ID)
|
||||||
utils.Exec("docker-compose", cmdArgs)
|
|
||||||
|
cmdArgs := []string{}
|
||||||
|
|
||||||
|
for _, service := range services {
|
||||||
|
|
||||||
|
cmdArgs = []string{"-f", utils.GetPath("wsPath") + "/docker-compose.yml", "start", service.ID}
|
||||||
|
utils.Exec("docker-compose", cmdArgs)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for it to settle up before continuing...
|
||||||
|
time.Sleep(defaultContainerOperationSleepTime)
|
||||||
|
|
||||||
return GetEdgeAppStatus(ID)
|
return GetEdgeAppStatus(ID)
|
||||||
|
|
||||||
|
@ -143,8 +156,19 @@ func RunEdgeApp(ID string) EdgeAppStatus {
|
||||||
// StopEdgeApp : Stops an EdgeApp and return its most current status
|
// StopEdgeApp : Stops an EdgeApp and return its most current status
|
||||||
func StopEdgeApp(ID string) EdgeAppStatus {
|
func StopEdgeApp(ID string) EdgeAppStatus {
|
||||||
|
|
||||||
cmdArgs := []string{"-f", utils.GetPath("wsPath") + "/docker-compose.yml", "down", ID}
|
services := GetEdgeAppServices(ID)
|
||||||
utils.Exec("docker-compose", cmdArgs)
|
|
||||||
|
cmdArgs := []string{}
|
||||||
|
|
||||||
|
for _, service := range services {
|
||||||
|
|
||||||
|
cmdArgs = []string{"-f", utils.GetPath("wsPath") + "/docker-compose.yml", "stop", service.ID}
|
||||||
|
utils.Exec("docker-compose", cmdArgs)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for it to settle up before continuing...
|
||||||
|
time.Sleep(defaultContainerOperationSleepTime)
|
||||||
|
|
||||||
return GetEdgeAppStatus(ID)
|
return GetEdgeAppStatus(ID)
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,14 @@ type taskSetupTunnelArgs struct {
|
||||||
NodeName string `json:"node_name"`
|
NodeName string `json:"node_name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type taskStartEdgeAppArgs struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type taskStopEdgeAppArgs struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
// GetNextTask : Performs a MySQL query over the device's Edgebox API
|
// GetNextTask : Performs a MySQL query over the device's Edgebox API
|
||||||
func GetNextTask() Task {
|
func GetNextTask() Task {
|
||||||
|
|
||||||
|
@ -110,13 +118,29 @@ func ExecuteTask(task Task) Task {
|
||||||
}
|
}
|
||||||
|
|
||||||
case "start_edgeapp":
|
case "start_edgeapp":
|
||||||
log.Printf("Starting EdgeApp...")
|
|
||||||
task.Result = sql.NullString{String: taskStartEdgeApp(), Valid: true}
|
log.Println("Starting EdgeApp...")
|
||||||
// ...
|
var args taskStartEdgeAppArgs
|
||||||
|
err := json.Unmarshal([]byte(task.Args), &args)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error reading arguments of start_edgeapp task: %s", err)
|
||||||
|
} else {
|
||||||
|
taskResult := taskStartEdgeApp(args)
|
||||||
|
task.Result = sql.NullString{String: taskResult, Valid: true}
|
||||||
|
}
|
||||||
|
|
||||||
case "stop_edgeapp":
|
case "stop_edgeapp":
|
||||||
log.Printf("Stopping EdgeApp...")
|
|
||||||
task.Result = sql.NullString{String: taskStopEdgeApp(), Valid: true}
|
log.Println("Stopping EdgeApp...")
|
||||||
// ...
|
var args taskStopEdgeAppArgs
|
||||||
|
err := json.Unmarshal([]byte(task.Args), &args)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error reading arguments of stop_edgeapp task: %s", err)
|
||||||
|
} else {
|
||||||
|
taskResult := taskStopEdgeApp(args)
|
||||||
|
task.Result = sql.NullString{String: taskResult, Valid: true}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -177,6 +201,34 @@ func taskSetupTunnel(args taskSetupTunnelArgs) string {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func taskStartEdgeApp(args taskStartEdgeAppArgs) string {
|
||||||
|
|
||||||
|
fmt.Println("Executing taskStartEdgeApp for " + args.ID)
|
||||||
|
|
||||||
|
result := edgeapps.RunEdgeApp(args.ID)
|
||||||
|
|
||||||
|
resultJSON, _ := json.Marshal(result)
|
||||||
|
|
||||||
|
taskGetEdgeApps() // This task will imediatelly update the entry in the api database.
|
||||||
|
|
||||||
|
return string(resultJSON)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func taskStopEdgeApp(args taskStopEdgeAppArgs) string {
|
||||||
|
|
||||||
|
fmt.Println("Executing taskStopEdgeApp for " + args.ID)
|
||||||
|
|
||||||
|
result := edgeapps.StopEdgeApp(args.ID)
|
||||||
|
|
||||||
|
resultJSON, _ := json.Marshal(result)
|
||||||
|
|
||||||
|
taskGetEdgeApps() // This task will imediatelly update the entry in the api database.
|
||||||
|
|
||||||
|
return string(resultJSON)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func taskGetEdgeApps() string {
|
func taskGetEdgeApps() string {
|
||||||
|
|
||||||
fmt.Println("Executing taskGetEdgeApps")
|
fmt.Println("Executing taskGetEdgeApps")
|
||||||
|
@ -205,13 +257,3 @@ func taskGetEdgeApps() string {
|
||||||
return string(edgeAppsJSON)
|
return string(edgeAppsJSON)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func taskStartEdgeApp() string {
|
|
||||||
|
|
||||||
return "OK"
|
|
||||||
}
|
|
||||||
|
|
||||||
func taskStopEdgeApp() string {
|
|
||||||
|
|
||||||
return "OK"
|
|
||||||
}
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"log"
|
"log"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
)
|
)
|
||||||
|
@ -18,10 +19,10 @@ func Exec(command string, args []string) string {
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO: Deal with possibility of error in command, allow explicit error handling and return proper formatted stderr
|
// TODO: Deal with possibility of error in command, allow explicit error handling and return proper formatted stderr
|
||||||
// log.Println(fmt.Sprint(err) + ": " + stderr.String()) ... Silence...
|
log.Println(fmt.Sprint(err) + ": " + stderr.String()) // ... Silence...
|
||||||
}
|
}
|
||||||
|
|
||||||
// log.Println("Result: " + out.String()) ... Silence ...
|
log.Println("Result: " + out.String()) // ... Silence ...
|
||||||
|
|
||||||
return out.String()
|
return out.String()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue