Tidy up (less verbosity), actual DB update for EDGEAPPS_LIST
parent
2817628fef
commit
0bd61ccf75
|
@ -6,6 +6,8 @@ import (
|
|||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
|
||||
"github.com/edgebox-iot/sysctl/internal/utils"
|
||||
)
|
||||
|
||||
|
@ -31,6 +33,7 @@ type EdgeAppService struct {
|
|||
}
|
||||
|
||||
const configFilename = "/edgebox-compose.yml"
|
||||
const envFilename = "/edgebox.env"
|
||||
|
||||
// GetEdgeApps : Returns a list of EdgeApp struct filled with information
|
||||
func GetEdgeApps() []EdgeApp {
|
||||
|
@ -51,7 +54,19 @@ func GetEdgeApps() []EdgeApp {
|
|||
_, err := os.Stat(utils.GetPath("edgeAppsPath") + f.Name() + configFilename)
|
||||
if !os.IsNotExist(err) {
|
||||
// File exists. Start digging!
|
||||
edgeAppName := "Nextcloud"
|
||||
|
||||
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"]
|
||||
}
|
||||
}
|
||||
|
||||
edgeApp := EdgeApp{ID: f.Name(), Name: edgeAppName, Status: GetEdgeAppStatus(f.Name()), Services: GetEdgeAppServices(f.Name()), NetworkURL: f.Name() + ".edgebox.local"}
|
||||
edgeApps = append(edgeApps, edgeApp)
|
||||
}
|
||||
|
@ -95,10 +110,6 @@ func GetEdgeAppStatus(ID string) EdgeAppStatus {
|
|||
// GetEdgeAppServices : Returns a
|
||||
func GetEdgeAppServices(ID string) []EdgeAppService {
|
||||
|
||||
log.Println("Finding " + ID + " EdgeApp Services")
|
||||
|
||||
// strConfigFile := string(configFile) // convert content to a 'string'
|
||||
|
||||
cmdArgs := []string{"-r", ".services | keys[]", utils.GetPath("edgeAppsPath") + ID + configFilename}
|
||||
servicesString := utils.Exec("yq", cmdArgs)
|
||||
serviceSlices := strings.Split(servicesString, "\n")
|
||||
|
@ -106,7 +117,6 @@ func GetEdgeAppServices(ID string) []EdgeAppService {
|
|||
var edgeAppServices []EdgeAppService
|
||||
|
||||
for _, serviceID := range serviceSlices {
|
||||
log.Println(serviceID)
|
||||
cmdArgs = []string{"-f", utils.GetPath("wsPath") + "/docker-compose.yml", "ps", "-q", serviceID}
|
||||
cmdResult := utils.Exec("docker-compose", cmdArgs)
|
||||
isRunning := false
|
||||
|
|
|
@ -141,12 +141,12 @@ func ExecuteSchedules(tick int) {
|
|||
|
||||
if tick == 1 {
|
||||
// Executing on startup (first tick). Schedules run before tasks in the SystemIterator
|
||||
taskGetEdgeApps()
|
||||
log.Println(taskGetEdgeApps())
|
||||
}
|
||||
|
||||
if tick%30 == 0 {
|
||||
// Executing every 30 ticks
|
||||
taskGetEdgeApps()
|
||||
log.Println(taskGetEdgeApps())
|
||||
|
||||
}
|
||||
|
||||
|
@ -183,28 +183,27 @@ func taskGetEdgeApps() string {
|
|||
|
||||
edgeApps := edgeapps.GetEdgeApps()
|
||||
edgeAppsJSON, _ := json.Marshal(edgeApps)
|
||||
log.Println(string(edgeAppsJSON))
|
||||
|
||||
// db, err := sql.Open("mysql", utils.GetMySQLDbConnectionDetails())
|
||||
db, err := sql.Open("mysql", utils.GetMySQLDbConnectionDetails())
|
||||
|
||||
// if err != nil {
|
||||
// panic(err.Error())
|
||||
// }
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
// defer db.Close()
|
||||
defer db.Close()
|
||||
|
||||
// statusUpdate, err := db.Query("UPDATE options SET value = '" + + "' WHERE name = 'EDGEAPPS_LIST'")
|
||||
statusUpdate, err := db.Query("REPLACE into options (name, value) VALUES ('EDGEAPPS_LIST','" + string(edgeAppsJSON) + "');")
|
||||
|
||||
// if err != nil {
|
||||
// panic(err.Error())
|
||||
// }
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
// for statusUpdate.Next() {
|
||||
for statusUpdate.Next() {
|
||||
|
||||
// }
|
||||
}
|
||||
|
||||
return string(edgeAppsJSON)
|
||||
|
||||
// Saving information in the "options" table.
|
||||
return "OK"
|
||||
}
|
||||
|
||||
func taskStartEdgeApp() string {
|
||||
|
|
|
@ -2,7 +2,6 @@ package utils
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"os/exec"
|
||||
|
||||
|
@ -18,10 +17,11 @@ func Exec(command string, args []string) string {
|
|||
cmd.Stderr = &stderr
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
log.Println(fmt.Sprint(err) + ": " + stderr.String())
|
||||
// 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("Result: " + out.String())
|
||||
// log.Println("Result: " + out.String()) ... Silence ...
|
||||
|
||||
return out.String()
|
||||
|
||||
|
|
Loading…
Reference in New Issue