Added edgeapps package, cleaned up redundancies

loop_loop_execution
Paulo Truta 2021-02-15 20:37:16 +01:00
parent 9f20211bf4
commit ad28e1a505
8 changed files with 111 additions and 41 deletions

View File

@ -20,9 +20,6 @@ build:
-trimpath -ldflags "-s -w -X ${PROJECT}/internal/diagnostics.Version=${RELEASE} \ -trimpath -ldflags "-s -w -X ${PROJECT}/internal/diagnostics.Version=${RELEASE} \
-X ${PROJECT}/internal/diagnostics.Commit=${COMMIT} \ -X ${PROJECT}/internal/diagnostics.Commit=${COMMIT} \
-X ${PROJECT}/internal/diagnostics.BuildDate=${BUILD_DATE} \ -X ${PROJECT}/internal/diagnostics.BuildDate=${BUILD_DATE} \
-X ${PROJECT}/internal/tasks.Version=${RELEASE} \
-X ${PROJECT}/internal/tasks.Commit=${COMMIT} \
-X ${PROJECT}/internal/tasks.BuildDate=${BUILD_DATE} \
-X ${PROJECT}/internal/tasks.Dbhost=${DBHOST} \ -X ${PROJECT}/internal/tasks.Dbhost=${DBHOST} \
-X ${PROJECT}/internal/tasks.Dbname=${DBNAME} \ -X ${PROJECT}/internal/tasks.Dbname=${DBNAME} \
-X ${PROJECT}/internal/tasks.Dbuser=${DBUSER} \ -X ${PROJECT}/internal/tasks.Dbuser=${DBUSER} \

View File

@ -102,7 +102,7 @@ func isDatabaseReady() bool {
} }
func systemIterator(name *string, tick int) { func systemIterator(name *string, tick int) {
log.Printf("Tick is %d", tick) log.Printf("Tick is %d", tick)
if isSystemReady() { if isSystemReady() {

5
go.mod
View File

@ -2,9 +2,14 @@ module github.com/edgebox-iot/sysctl
go 1.15 go 1.15
replace github.com/edgebox-iot/sysctl/internal/utils => ./internal/utils
replace github.com/edgebox-iot/sysctl/internal/tasks => ./internal/tasks replace github.com/edgebox-iot/sysctl/internal/tasks => ./internal/tasks
replace github.com/edgebix-iot/sysctl/internal/edgeapps => ./internal/edgeapps
require ( require (
github.com/edgebox-iot/sysctl/internal/tasks v0.0.0-00010101000000-000000000000 github.com/edgebox-iot/sysctl/internal/tasks v0.0.0-00010101000000-000000000000
github.com/edgebox-iot/sysctl/internal/utils v0.0.0-00010101000000-000000000000 // indirect
github.com/go-sql-driver/mysql v1.5.0 // indirect github.com/go-sql-driver/mysql v1.5.0 // indirect
) )

View File

@ -0,0 +1,64 @@
package edgeapps
import (
"fmt"
"io/ioutil"
"log"
"os"
)
// EdgeApp : Struct representing an EdgeApp in the system
type EdgeApp struct {
ID string `json:"id"`
Name string `json:"name"`
Status EdgeAppStatus `json:"status"`
InternetAccessible bool `json:"internet_accessible"`
InternetURL string `json:"internet_url"`
NetworkURL string `json:"network_url"`
}
// EdgeAppStatus : Struct representing possible EdgeApp statuses (code + description)
type EdgeAppStatus struct {
ID int `json:"id"`
Description string `json:"description"`
}
// GetEdgeApps : Returns a list of EdgeApp struct filled with information
func GetEdgeApps() []EdgeApp {
var edgeApps []EdgeApp
// Building list of available edgeapps in the system.
configFilename := "edgebox-compose.yml"
// envFilename := "edgebox.env"
// postinstallFilename := "edgebox-postinstall.txt"
edgeAppsPath := "/home/system/components/apps"
var edgeAppsList []string
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!
edgeAppsList = append(edgeAppsList, f.Name())
}
}
}
// Querying to see which apps are running.
// cmdargs = []string{"ps", "-a"}
// executeCommand("docker", cmdargs)
// (...)
return edgeApps
}

View File

@ -0,0 +1,3 @@
module github.com/edgebox-iot/sysctl/internal/edgeapps
go 1.15

View File

@ -1,26 +1,17 @@
package tasks package tasks
import ( import (
"bytes"
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"os/exec"
"strconv" "strconv"
"github.com/edgebox-iot/sysctl/internal/diagnostics"
"github.com/edgebox-iot/sysctl/internal/utils"
_ "github.com/go-sql-driver/mysql" // Mysql Driver _ "github.com/go-sql-driver/mysql" // Mysql Driver
) )
// Version : The release version
var Version string
// Commit : The commit of this release
var Commit string
// BuildDate : The release build date
var BuildDate string
// Dbhost : Database host (can be tweaked in makefile) // Dbhost : Database host (can be tweaked in makefile)
var Dbhost string var Dbhost string
@ -112,7 +103,7 @@ func ExecuteTask(task Task) Task {
} }
if Version == "dev" { if diagnostics.Version == "dev" {
log.Printf("Dev environemnt. Not executing tasks.") log.Printf("Dev environemnt. Not executing tasks.")
} else { } else {
log.Println("Task: " + task.Task) log.Println("Task: " + task.Task)
@ -159,6 +150,11 @@ func ExecuteTask(task Task) Task {
// ExecuteSchedules - Run Specific tasks without input each multiple x of ticks. // ExecuteSchedules - Run Specific tasks without input each multiple x of ticks.
func ExecuteSchedules(tick int) { func ExecuteSchedules(tick int) {
if tick == 1 {
// Executing on startup (first tick). Schedules run before tasks in the SystemIterator
taskGetEdgeApps()
}
if tick%30 == 0 { if tick%30 == 0 {
// Executing every 30 ticks // Executing every 30 ticks
taskGetEdgeApps() taskGetEdgeApps()
@ -174,33 +170,18 @@ func ExecuteSchedules(tick int) {
} }
func executeCommand(command string, args []string) string {
cmd := exec.Command(command, args...)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
log.Println(fmt.Sprint(err) + ": " + stderr.String())
}
log.Println("Result: " + out.String())
return out.String()
}
func taskSetupTunnel(args taskSetupTunnelArgs) string { func taskSetupTunnel(args taskSetupTunnelArgs) string {
fmt.Println("Executing taskSetupTunnel") fmt.Println("Executing taskSetupTunnel")
cmdargs := []string{"gen", "--name", args.NodeName, "--token", args.BootnodeToken, args.BootnodeAddress + ":8655", "--prefix", args.AssignedAddress} cmdargs := []string{"gen", "--name", args.NodeName, "--token", args.BootnodeToken, args.BootnodeAddress + ":8655", "--prefix", args.AssignedAddress}
executeCommand("tinc-boot", cmdargs) utils.Exec("tinc-boot", cmdargs)
cmdargs = []string{"start", "tinc@dnet"} cmdargs = []string{"start", "tinc@dnet"}
executeCommand("systemctl", cmdargs) utils.Exec("systemctl", cmdargs)
cmdargs = []string{"enable", "tinc@dnet"} cmdargs = []string{"enable", "tinc@dnet"}
executeCommand("systemctl", cmdargs) utils.Exec("systemctl", cmdargs)
output := "OK" // Better check / logging of command execution result. output := "OK" // Better check / logging of command execution result.
return output return output
@ -211,13 +192,6 @@ func taskGetEdgeApps() string {
fmt.Println("Executing taskGetEdgeApps") fmt.Println("Executing taskGetEdgeApps")
// Building list of available edgeapps in the system.
// Querying to see which apps are running.
// cmdargs = []string{"ps", "-a"}
// executeCommand("docker", cmdargs)
// (...)
// Saving information in the "options" table. // Saving information in the "options" table.
return "OK" return "OK"
} }

View File

@ -0,0 +1,3 @@
module github.com/edgebox-iot/sysctl/internal/utils
go 1.15

View File

@ -0,0 +1,24 @@
package utils
import (
"bytes"
"fmt"
"log"
"os/exec"
)
// Exec : Runs a terminal Command, catches and logs errors, returns the result.
func Exec(command string, args []string) string {
cmd := exec.Command(command, args...)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
log.Println(fmt.Sprint(err) + ": " + stderr.String())
}
log.Println("Result: " + out.String())
return out.String()
}