Added full support for enable_online and disable_online tasks

loop_loop_execution
Paulo Truta 2021-02-20 14:26:23 +00:00
parent 6c0b011e51
commit 7857985fe4
2 changed files with 169 additions and 36 deletions

View File

@ -23,6 +23,12 @@ type EdgeApp struct {
InternetURL string `json:"internet_url"`
}
// MaybeEdgeApp : Boolean flag for validation of edgeapp existance
type MaybeEdgeApp struct {
EdgeApp EdgeApp `json:"edge_app"`
Valid bool `json:"valid"`
}
// EdgeAppStatus : Struct representing possible EdgeApp statuses (code + description)
type EdgeAppStatus struct {
ID int `json:"id"`
@ -40,7 +46,61 @@ const envFilename = "/edgebox.env"
const myEdgeAppServiceEnvFilename = "/myedgeapp.env"
const defaultContainerOperationSleepTime time.Duration = time.Second * 10
// GetEdgeApps : Returns a list of EdgeApp struct filled with information
// GetEdgeApp : Returns a EdgeApp struct with the current application information
func GetEdgeApp(ID string) MaybeEdgeApp {
result := MaybeEdgeApp{
EdgeApp: EdgeApp{},
Valid: false,
}
_, err := os.Stat(utils.GetPath("edgeAppsPath") + ID + configFilename)
if !os.IsNotExist(err) {
// File exists. Start digging!
edgeAppName := ID
edgeAppEnv, err := godotenv.Read(utils.GetPath("edgeAppsPath") + ID + envFilename)
if err != nil {
log.Println("Error loading .env file for edgeapp " + edgeAppName)
} else {
if edgeAppEnv["EDGEAPP_NAME"] != "" {
edgeAppName = edgeAppEnv["EDGEAPP_NAME"]
}
}
edgeAppInternetAccessible := false
edgeAppInternetURL := ""
myEdgeAppServiceEnv, err := godotenv.Read(utils.GetPath("edgeAppsPath") + ID + myEdgeAppServiceEnvFilename)
if err != nil {
if myEdgeAppServiceEnv["INTERNET_URL"] != "" {
edgeAppInternetAccessible = true
edgeAppInternetURL = myEdgeAppServiceEnv["INTERNET_URL"]
}
}
result = MaybeEdgeApp{
EdgeApp: EdgeApp{
ID: ID,
Name: edgeAppName,
Status: GetEdgeAppStatus(ID),
Services: GetEdgeAppServices(ID),
InternetAccessible: edgeAppInternetAccessible,
NetworkURL: ID + ".edgebox.local",
InternetURL: edgeAppInternetURL,
},
Valid: true,
}
}
return result
}
// GetEdgeApps : Returns a list of all available EdgeApps in structs filled with information
func GetEdgeApps() []EdgeApp {
var edgeApps []EdgeApp
@ -56,43 +116,12 @@ func GetEdgeApps() []EdgeApp {
if f.IsDir() {
// It is a folder that most probably contains an EdgeApp.
// To be fully sure, test that edgebox-compose.yml file exists in the target directory.
_, err := os.Stat(utils.GetPath("edgeAppsPath") + f.Name() + configFilename)
if !os.IsNotExist(err) {
// File exists. Start digging!
maybeEdgeApp := GetEdgeApp(f.Name())
if maybeEdgeApp.Valid {
edgeAppName := f.Name()
edgeAppEnv, err := godotenv.Read(utils.GetPath("edgeAppsPath") + f.Name() + envFilename)
if err != nil {
log.Println("Error loading .env file for edgeapp " + f.Name())
} else {
if edgeAppEnv["EDGEAPP_NAME"] != "" {
edgeAppName = edgeAppEnv["EDGEAPP_NAME"]
}
}
edgeAppInternetAccessible := false
edgeAppInternetURL := ""
myEdgeAppServiceEnv, err := godotenv.Read(utils.GetPath("edgeAppsPath") + f.Name() + myEdgeAppServiceEnvFilename)
if err != nil {
if myEdgeAppServiceEnv["URL"] != "" {
edgeAppInternetAccessible = true
edgeAppInternetURL = myEdgeAppServiceEnv["URL"]
}
}
edgeApp := EdgeApp{
ID: f.Name(),
Name: edgeAppName,
Status: GetEdgeAppStatus(f.Name()),
Services: GetEdgeAppServices(f.Name()),
InternetAccessible: edgeAppInternetAccessible,
NetworkURL: f.Name() + ".edgebox.local",
InternetURL: edgeAppInternetURL,
}
edgeApp := maybeEdgeApp.EdgeApp
edgeApps = append(edgeApps, edgeApp)
}
}
@ -195,3 +224,45 @@ func StopEdgeApp(ID string) EdgeAppStatus {
return GetEdgeAppStatus(ID)
}
// EnableOnline : Write environment file and rebuild the necessary containers. Rebuilds containers in project (in case of change only)
func EnableOnline(ID string, InternetURL string) MaybeEdgeApp {
maybeEdgeApp := GetEdgeApp(ID)
if maybeEdgeApp.Valid { // We're only going to do this operation if the EdgeApp actually exists.
// Create the myedgeapp.env file and add the InternetURL entry to it
envFilePath := utils.GetPath("edgeAppsPath") + ID + myEdgeAppServiceEnvFilename
env, _ := godotenv.Unmarshal("INTERNET_URL=" + InternetURL)
_ = godotenv.Write(env, envFilePath)
}
buildFrameworkContainers()
return GetEdgeApp(ID) // Return refreshed information
}
// DisableOnline : Removes env files necessary for system external access config. Rebuilds containers in project (in case of change only).
func DisableOnline(ID string) MaybeEdgeApp {
envFilePath := utils.GetPath("edgeAppsPath") + ID + myEdgeAppServiceEnvFilename
_, err := godotenv.Read(envFilePath)
if err != nil {
cmdArgs := []string{envFilePath}
utils.Exec("rm", cmdArgs)
}
buildFrameworkContainers()
return GetEdgeApp(ID)
}
func buildFrameworkContainers() {
cmdArgs := []string{"--build"}
utils.Exec("./"+utils.GetPath("wsPath")+"/ws", cmdArgs)
time.Sleep(defaultContainerOperationSleepTime)
}

View File

@ -39,6 +39,15 @@ type taskStopEdgeAppArgs struct {
ID string `json:"id"`
}
type taskEnableOnlineArgs struct {
ID string `json:"id"`
InternetURL string `json:"internet_url"`
}
type taskDisableOnlineArgs struct {
ID string `json:"id"`
}
// GetNextTask : Performs a MySQL query over the device's Edgebox API
func GetNextTask() Task {
@ -137,7 +146,32 @@ func ExecuteTask(task Task) Task {
task.Result = sql.NullString{String: taskResult, Valid: true}
}
case "enable_online":
log.Println("Enabling online access to EdgeApp...")
var args taskEnableOnlineArgs
err := json.Unmarshal([]byte(task.Args), &args)
if err != nil {
log.Printf("Error reading arguments of enable_online task: %s", err)
} else {
taskResult := taskEnableOnline(args)
task.Result = sql.NullString{String: taskResult, Valid: true}
}
case "disable_online":
log.Println("Disabling online access to EdgeApp...")
var args taskDisableOnlineArgs
err := json.Unmarshal([]byte(task.Args), &args)
if err != nil {
log.Printf("Error reading arguments of enable_online task: %s", err)
} else {
taskResult := taskDisableOnline(args)
task.Result = sql.NullString{String: taskResult, Valid: true}
}
}
}
if task.Result.Valid {
@ -225,6 +259,34 @@ func taskStopEdgeApp(args taskStopEdgeAppArgs) string {
}
func taskEnableOnline(args taskEnableOnlineArgs) string {
fmt.Println("Executing taskEnableOnline for " + args.ID)
result := edgeapps.EnableOnline(args.ID, args.InternetURL)
resultJSON, _ := json.Marshal(result)
taskGetEdgeApps()
return string(resultJSON)
}
func taskDisableOnline(args taskDisableOnlineArgs) string {
fmt.Println("Executing taskEnableOnline for " + args.ID)
result := edgeapps.DisableOnline(args.ID)
resultJSON, _ := json.Marshal(result)
taskGetEdgeApps()
return string(resultJSON)
}
func taskGetEdgeApps() string {
fmt.Println("Executing taskGetEdgeApps")