mirror of
https://github.com/edgebox-iot/edgeboxctl.git
synced 2026-09-25 06:13:03 +02:00
[WIP] Support for cloud version (#17)
* Added build-cloud to Makefile * Fix env var key GOOS * Added taskSetReleaseVersion for edgeboxctl version option write into DB * taskGetSystemUptime to run every 5 ticks * Added support for detecting main disk in cloud version * Added missing import * Finding main device in test * Refactored GetDevices and test to use release_version * Added utils.getIP function, set to run as a 60 tick scheduled task * Added taskEnablePublicDashboard and taskDisablePublicDashboard * Added utils.WriteOption and refactor, added enablePublicDashb0oard * Finalizing online support for dashboard, cloud version support * Added task SetupCloudOptions * Added couple of func description comments * Early return statement (thanks @inverse) * Using IsPublicDashboard in DisablePublicDashboard func * Added constants for release version and device types, added diagnostics.GetReleaseVersion * Removed codecov from test action
This commit is contained in:
+100
-95
@@ -60,6 +60,10 @@ type taskDisableOnlineArgs struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
type taskEnablePublicDashboardArgs struct {
|
||||
InternetURL string `json:"internet_url"`
|
||||
}
|
||||
|
||||
const STATUS_CREATED int = 0
|
||||
const STATUS_EXECUTING int = 1
|
||||
const STATUS_FINISHED int = 2
|
||||
@@ -122,10 +126,11 @@ func ExecuteTask(task Task) Task {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
|
||||
if diagnostics.Version == "dev" {
|
||||
if diagnostics.GetReleaseVersion() == diagnostics.DEV_VERSION {
|
||||
log.Printf("Dev environemnt. Not executing tasks.")
|
||||
} else {
|
||||
log.Println("Task: " + task.Task)
|
||||
log.Println("Args: " + task.Args.String)
|
||||
switch task.Task {
|
||||
case "setup_tunnel":
|
||||
|
||||
@@ -211,6 +216,24 @@ func ExecuteTask(task Task) Task {
|
||||
task.Result = sql.NullString{String: taskResult, Valid: true}
|
||||
}
|
||||
|
||||
case "enable_public_dashboard":
|
||||
|
||||
log.Println("Enabling online access to Dashboard...")
|
||||
var args taskEnablePublicDashboardArgs
|
||||
err := json.Unmarshal([]byte(task.Args.String), &args)
|
||||
if err != nil {
|
||||
log.Printf("Error reading arguments of enable_public_dashboard task: %s", err)
|
||||
} else {
|
||||
taskResult := taskEnablePublicDashboard(args)
|
||||
task.Result = sql.NullString{String: taskResult, Valid: true}
|
||||
}
|
||||
|
||||
case "disable_public_dashboard":
|
||||
|
||||
log.Println("Disabling online access to Dashboard...")
|
||||
taskResult := taskDisablePublicDashboard()
|
||||
task.Result = sql.NullString{String: taskResult, Valid: true}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -230,7 +253,6 @@ func ExecuteTask(task Task) Task {
|
||||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
|
||||
} else {
|
||||
_, err = statement.Exec(STATUS_ERROR, "Error", formatedDatetime, strconv.Itoa(task.ID)) // Execute SQL Statement with Error info
|
||||
if err != nil {
|
||||
@@ -255,6 +277,26 @@ func ExecuteSchedules(tick int) {
|
||||
|
||||
if tick == 1 {
|
||||
|
||||
ip := taskGetSystemIP()
|
||||
log.Println("System IP is: " + ip)
|
||||
|
||||
release := taskSetReleaseVersion()
|
||||
log.Println("Setting api option flag for Edgeboxctl (" + release + " version)")
|
||||
|
||||
hostname := taskGetHostname()
|
||||
log.Println("Hostname is " + hostname)
|
||||
|
||||
// if diagnostics.Version == "cloud" && !edgeapps.IsPublicDashboard() {
|
||||
// taskEnablePublicDashboard(taskEnablePublicDashboardArgs{
|
||||
// InternetURL: hostname + ".myedge.app",
|
||||
// })
|
||||
// }
|
||||
|
||||
if diagnostics.GetReleaseVersion() == diagnostics.CLOUD_VERSION {
|
||||
log.Println("Setting up cloud version options (name, email, api token)")
|
||||
taskSetupCloudOptions()
|
||||
}
|
||||
|
||||
// Executing on startup (first tick). Schedules run before tasks in the SystemIterator
|
||||
uptime := taskGetSystemUptime()
|
||||
log.Println("Uptime is " + uptime + " seconds (" + system.GetUptimeFormatted() + ")")
|
||||
@@ -266,18 +308,18 @@ func ExecuteSchedules(tick int) {
|
||||
|
||||
if tick%5 == 0 {
|
||||
// Executing every 5 ticks
|
||||
taskGetSystemUptime()
|
||||
log.Println(taskGetStorageDevices())
|
||||
}
|
||||
|
||||
if tick%30 == 0 {
|
||||
// Executing every 30 ticks
|
||||
log.Println(taskGetEdgeApps())
|
||||
|
||||
}
|
||||
|
||||
if tick%60 == 0 {
|
||||
// Every 60 ticks...
|
||||
|
||||
ip := taskGetSystemIP()
|
||||
log.Println("System IP is: " + ip)
|
||||
}
|
||||
|
||||
// Just add a schedule here if you need a custom one (every "tick hour", every "tick day", etc...)
|
||||
@@ -285,7 +327,6 @@ func ExecuteSchedules(tick int) {
|
||||
}
|
||||
|
||||
func taskSetupTunnel(args taskSetupTunnelArgs) string {
|
||||
|
||||
fmt.Println("Executing taskSetupTunnel")
|
||||
|
||||
cmdargs := []string{"gen", "--name", args.NodeName, "--token", args.BootnodeToken, args.BootnodeAddress + ":8655", "--prefix", args.AssignedAddress}
|
||||
@@ -299,181 +340,145 @@ func taskSetupTunnel(args taskSetupTunnelArgs) string {
|
||||
|
||||
output := "OK" // Better check / logging of command execution result.
|
||||
return output
|
||||
|
||||
}
|
||||
|
||||
func taskInstallEdgeApp(args taskInstallEdgeAppArgs) string {
|
||||
|
||||
fmt.Println("Executing taskInstallEdgeApp for " + args.ID)
|
||||
|
||||
result := edgeapps.SetEdgeAppInstalled(args.ID)
|
||||
|
||||
resultJSON, _ := json.Marshal(result)
|
||||
|
||||
taskGetEdgeApps()
|
||||
|
||||
return string(resultJSON)
|
||||
|
||||
}
|
||||
|
||||
func taskRemoveEdgeApp(args taskRemoveEdgeAppArgs) string {
|
||||
|
||||
fmt.Println("Executing taskRemoveEdgeApp for " + args.ID)
|
||||
|
||||
// Making sure the application is stopped before setting it as removed.
|
||||
edgeapps.StopEdgeApp(args.ID)
|
||||
result := edgeapps.SetEdgeAppNotInstalled(args.ID)
|
||||
|
||||
resultJSON, _ := json.Marshal(result)
|
||||
|
||||
taskGetEdgeApps()
|
||||
|
||||
return string(resultJSON)
|
||||
|
||||
}
|
||||
|
||||
func taskStartEdgeApp(args taskStartEdgeAppArgs) string {
|
||||
|
||||
fmt.Println("Executing taskStartEdgeApp for " + args.ID)
|
||||
|
||||
result := edgeapps.RunEdgeApp(args.ID)
|
||||
|
||||
resultJSON, _ := json.Marshal(result)
|
||||
|
||||
taskGetEdgeApps() // This task will imediatelly update the entry in the api database.
|
||||
|
||||
return string(resultJSON)
|
||||
|
||||
}
|
||||
|
||||
func taskStopEdgeApp(args taskStopEdgeAppArgs) string {
|
||||
|
||||
fmt.Println("Executing taskStopEdgeApp for " + args.ID)
|
||||
|
||||
result := edgeapps.StopEdgeApp(args.ID)
|
||||
|
||||
resultJSON, _ := json.Marshal(result)
|
||||
|
||||
taskGetEdgeApps() // This task will imediatelly update the entry in the api database.
|
||||
|
||||
return string(resultJSON)
|
||||
|
||||
}
|
||||
|
||||
func taskEnableOnline(args taskEnableOnlineArgs) string {
|
||||
|
||||
fmt.Println("Executing taskEnableOnline for " + args.ID)
|
||||
|
||||
result := edgeapps.EnableOnline(args.ID, args.InternetURL)
|
||||
|
||||
resultJSON, _ := json.Marshal(result)
|
||||
|
||||
taskGetEdgeApps()
|
||||
|
||||
return string(resultJSON)
|
||||
|
||||
}
|
||||
|
||||
func taskDisableOnline(args taskDisableOnlineArgs) string {
|
||||
|
||||
fmt.Println("Executing taskDisableOnline for " + args.ID)
|
||||
|
||||
result := edgeapps.DisableOnline(args.ID)
|
||||
|
||||
resultJSON, _ := json.Marshal(result)
|
||||
|
||||
taskGetEdgeApps()
|
||||
|
||||
return string(resultJSON)
|
||||
}
|
||||
|
||||
func taskEnablePublicDashboard(args taskEnablePublicDashboardArgs) string {
|
||||
fmt.Println("Enabling taskEnablePublicDashboard")
|
||||
result := edgeapps.EnablePublicDashboard(args.InternetURL)
|
||||
if result {
|
||||
|
||||
utils.WriteOption("PUBLIC_DASHBOARD", args.InternetURL)
|
||||
return "{result: true}"
|
||||
|
||||
}
|
||||
|
||||
return "{result: false}"
|
||||
}
|
||||
|
||||
func taskDisablePublicDashboard() string {
|
||||
fmt.Println("Executing taskDisablePublicDashboard")
|
||||
result := edgeapps.DisablePublicDashboard()
|
||||
utils.WriteOption("PUBLIC_DASHBOARD", "")
|
||||
if result {
|
||||
return "{result: true}"
|
||||
}
|
||||
return "{result: false}"
|
||||
}
|
||||
|
||||
func taskSetReleaseVersion() string {
|
||||
|
||||
fmt.Println("Executing taskSetReleaseVersion")
|
||||
|
||||
utils.WriteOption("RELEASE_VERSION", diagnostics.Version)
|
||||
|
||||
return diagnostics.Version
|
||||
}
|
||||
|
||||
func taskGetEdgeApps() string {
|
||||
|
||||
fmt.Println("Executing taskGetEdgeApps")
|
||||
|
||||
edgeApps := edgeapps.GetEdgeApps()
|
||||
edgeAppsJSON, _ := json.Marshal(edgeApps)
|
||||
|
||||
db, err := sql.Open("sqlite3", utils.GetSQLiteDbConnectionDetails())
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
|
||||
statement, err := db.Prepare("REPLACE into option (name, value, created, updated) VALUES (?, ?, ?, ?);") // Prepare SQL Statement
|
||||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
|
||||
formatedDatetime := utils.GetSQLiteFormattedDateTime(time.Now())
|
||||
|
||||
_, err = statement.Exec("EDGEAPPS_LIST", string(edgeAppsJSON), formatedDatetime, formatedDatetime) // Execute SQL Statement
|
||||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
|
||||
db.Close()
|
||||
|
||||
utils.WriteOption("EDGEAPPS_LIST", string(edgeAppsJSON))
|
||||
return string(edgeAppsJSON)
|
||||
|
||||
}
|
||||
|
||||
func taskGetSystemUptime() string {
|
||||
fmt.Println("Executing taskGetSystemUptime")
|
||||
|
||||
uptime := system.GetUptimeInSeconds()
|
||||
|
||||
db, err := sql.Open("sqlite3", utils.GetSQLiteDbConnectionDetails())
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
|
||||
statement, err := db.Prepare("REPLACE into option (name, value, created, updated) VALUES (?, ?, ?, ?);") // Prepare SQL Statement
|
||||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
|
||||
formatedDatetime := utils.GetSQLiteFormattedDateTime(time.Now())
|
||||
|
||||
_, err = statement.Exec("SYSTEM_UPTIME", uptime, formatedDatetime, formatedDatetime) // Execute SQL Statement
|
||||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
|
||||
db.Close()
|
||||
|
||||
utils.WriteOption("SYSTEM_UPTIME", uptime)
|
||||
return uptime
|
||||
|
||||
}
|
||||
|
||||
func taskGetStorageDevices() string {
|
||||
fmt.Println("Executing taskGetStorageDevices")
|
||||
|
||||
devices := storage.GetDevices()
|
||||
devices := storage.GetDevices(diagnostics.GetReleaseVersion())
|
||||
devicesJSON, _ := json.Marshal(devices)
|
||||
|
||||
db, err := sql.Open("sqlite3", utils.GetSQLiteDbConnectionDetails())
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
|
||||
statement, err := db.Prepare("REPLACE into option (name, value, created, updated) VALUES (?, ?, ?, ?);") // Prepare SQL Statement
|
||||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
|
||||
formatedDatetime := utils.GetSQLiteFormattedDateTime(time.Now())
|
||||
|
||||
_, err = statement.Exec("STORAGE_DEVICES_LIST", devicesJSON, formatedDatetime, formatedDatetime) // Execute SQL Statement
|
||||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
|
||||
db.Close()
|
||||
utils.WriteOption("STORAGE_DEVICES_LIST", string(devicesJSON))
|
||||
|
||||
return string(devicesJSON)
|
||||
}
|
||||
|
||||
func taskGetSystemIP() string {
|
||||
fmt.Println("Executing taskGetStorageDevices")
|
||||
ip := system.GetIP()
|
||||
utils.WriteOption("IP_ADDRESS", ip)
|
||||
return ip
|
||||
}
|
||||
|
||||
func taskGetHostname() string {
|
||||
fmt.Println("Executing taskGetHostname")
|
||||
hostname := system.GetHostname()
|
||||
utils.WriteOption("HOSTNAME", hostname)
|
||||
return hostname
|
||||
}
|
||||
|
||||
func taskSetupCloudOptions() {
|
||||
fmt.Println("Executing taskSetupCloudOptions")
|
||||
system.SetupCloudOptions()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user