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} \
-X ${PROJECT}/internal/diagnostics.Commit=${COMMIT} \
-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.Dbname=${DBNAME} \
-X ${PROJECT}/internal/tasks.Dbuser=${DBUSER} \

5
go.mod
View File

@ -2,9 +2,14 @@ module github.com/edgebox-iot/sysctl
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/edgebix-iot/sysctl/internal/edgeapps => ./internal/edgeapps
require (
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
)

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
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"log"
"os/exec"
"strconv"
"github.com/edgebox-iot/sysctl/internal/diagnostics"
"github.com/edgebox-iot/sysctl/internal/utils"
_ "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)
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.")
} else {
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.
func ExecuteSchedules(tick int) {
if tick == 1 {
// Executing on startup (first tick). Schedules run before tasks in the SystemIterator
taskGetEdgeApps()
}
if tick%30 == 0 {
// Executing every 30 ticks
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 {
fmt.Println("Executing taskSetupTunnel")
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"}
executeCommand("systemctl", cmdargs)
utils.Exec("systemctl", cmdargs)
cmdargs = []string{"enable", "tinc@dnet"}
executeCommand("systemctl", cmdargs)
utils.Exec("systemctl", cmdargs)
output := "OK" // Better check / logging of command execution result.
return output
@ -211,13 +192,6 @@ func taskGetEdgeApps() string {
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.
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()
}