2021-02-15 20:37:16 +01:00
|
|
|
package edgeapps
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2021-02-16 17:30:01 +01:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
2021-02-15 20:37:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// EdgeApp : Struct representing an EdgeApp in the system
|
|
|
|
type EdgeApp struct {
|
2021-02-16 17:30:01 +01:00
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Status EdgeAppStatus `json:"status"`
|
|
|
|
Services []EdgeAppService `json:"services"`
|
|
|
|
NetworkURL []string `json:"network_url"`
|
2021-02-15 20:37:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// EdgeAppStatus : Struct representing possible EdgeApp statuses (code + description)
|
|
|
|
type EdgeAppStatus struct {
|
|
|
|
ID int `json:"id"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
}
|
|
|
|
|
2021-02-16 17:30:01 +01:00
|
|
|
// EdgeAppService : Struct representing a single container that can be part of an EdgeApp package
|
|
|
|
type EdgeAppService struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Isrunning bool `json:"is_running"`
|
|
|
|
}
|
|
|
|
|
2021-02-15 20:37:16 +01:00
|
|
|
// GetEdgeApps : Returns a list of EdgeApp struct filled with information
|
2021-02-16 17:30:01 +01:00
|
|
|
func GetEdgeApps() string { // []EdgeApp {
|
2021-02-15 20:37:16 +01:00
|
|
|
|
2021-02-16 17:30:01 +01:00
|
|
|
// var edgeApps []EdgeApp
|
2021-02-15 20:37:16 +01:00
|
|
|
|
|
|
|
// Building list of available edgeapps in the system.
|
|
|
|
configFilename := "edgebox-compose.yml"
|
|
|
|
edgeAppsPath := "/home/system/components/apps"
|
|
|
|
|
|
|
|
files, err := ioutil.ReadDir(edgeAppsPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
fmt.Println(f.Name())
|
|
|
|
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("/home/system/components/apps/" + f.Name() + "/" + configFilename)
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
// File exists. Start digging!
|
2021-02-16 17:30:01 +01:00
|
|
|
// edgeApp := EdgeApp{ID: f.Name(), Status: GetEdgeAppStatus(f.Name())}
|
|
|
|
// edgeApps = append(edgeApps, edgeApp)
|
|
|
|
GetEdgeAppServices(f.Name())
|
2021-02-15 20:37:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Querying to see which apps are running.
|
|
|
|
// cmdargs = []string{"ps", "-a"}
|
|
|
|
// executeCommand("docker", cmdargs)
|
|
|
|
// (...)
|
|
|
|
|
2021-02-16 17:30:01 +01:00
|
|
|
// return edgeApps
|
|
|
|
return "OK"
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEdgeAppStatus : Returns a struct representing the current status of the EdgeApp
|
|
|
|
// func GetEdgeAppStatus(ID string) EdgeAppStatus {
|
|
|
|
|
|
|
|
// // Possible states of an EdgeApp:
|
|
|
|
// // - All services running = EdgeApp running
|
|
|
|
// // - Some services running = Problem detected, needs restart
|
|
|
|
// // - No service running = EdgeApp is off
|
|
|
|
|
|
|
|
// services := GetEdgeAppServices(ID)
|
|
|
|
|
|
|
|
// return status
|
|
|
|
// }
|
|
|
|
|
|
|
|
// GetEdgeAppServices : Returns a
|
|
|
|
func GetEdgeAppServices(ID string) string {
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile("/home/system/components/apps/" + ID + "/edgebox-compose.yml")
|
|
|
|
|
|
|
|
// If this happens it means that no EdgeApp exists for the given ID. This func should not be called in that case.
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is application running?
|
|
|
|
|
|
|
|
t := make(map[string]interface{})
|
|
|
|
yaml.Unmarshal([]byte(data), &t)
|
|
|
|
fmt.Println(t["services"])
|
|
|
|
|
|
|
|
return "OK"
|
2021-02-15 20:37:16 +01:00
|
|
|
|
|
|
|
}
|