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