Reading json arguments for task running, setup_bootnode task, don't run actual commands for dev release
parent
c3d283c243
commit
db3b31091e
|
@ -2,12 +2,23 @@ package tasks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
_ "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
|
||||||
|
|
||||||
|
@ -31,6 +42,12 @@ type Task struct {
|
||||||
Updated string `json:"updated"`
|
Updated string `json:"updated"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type taskSetupBootnodeArgs struct {
|
||||||
|
BootnodeAddress string `json:"bootnode_address"`
|
||||||
|
BootnodeToken string `json:"bootnode_token"`
|
||||||
|
AssignedAddress string `json:"assigned_address"`
|
||||||
|
}
|
||||||
|
|
||||||
// GetNextTask : Performs a MySQL query over the device's Edgebox API
|
// GetNextTask : Performs a MySQL query over the device's Edgebox API
|
||||||
func GetNextTask() Task {
|
func GetNextTask() Task {
|
||||||
|
|
||||||
|
@ -92,17 +109,30 @@ func ExecuteTask(task Task) Task {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch task.Task {
|
if Version == "dev" {
|
||||||
case "setup_bootnode":
|
fmt.Println("Dev environemnt. Not executing tasks.")
|
||||||
fmt.Println("Setting up bootnode connection...")
|
} else {
|
||||||
// ...
|
switch task.Task {
|
||||||
task.Result = sql.NullString{String: "{status: 'success'}", Valid: true}
|
case "setup_bootnode":
|
||||||
case "start_edgeapp":
|
|
||||||
fmt.Println("Starting EdgeApp...")
|
fmt.Println("Setting up bootnode connection...")
|
||||||
// ...
|
var args taskSetupBootnodeArgs
|
||||||
case "stop_edgeapp":
|
err := json.Unmarshal([]byte(task.Args), &args)
|
||||||
fmt.Println("Stopping EdgeApp...")
|
if err != nil {
|
||||||
// ...
|
fmt.Println("Error reading arguments of setup_bootnode task: %s", err)
|
||||||
|
} else {
|
||||||
|
task.Result = sql.NullString{String: taskSetupBootnode(args), Valid: true}
|
||||||
|
}
|
||||||
|
|
||||||
|
case "start_edgeapp":
|
||||||
|
fmt.Println("Starting EdgeApp...")
|
||||||
|
task.Result = sql.NullString{String: taskStartEdgeApp(), Valid: true}
|
||||||
|
// ...
|
||||||
|
case "stop_edgeapp":
|
||||||
|
fmt.Println("Stopping EdgeApp...")
|
||||||
|
task.Result = sql.NullString{String: taskStopEdgeApp(), Valid: true}
|
||||||
|
// ...
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if task.Result.Valid {
|
if task.Result.Valid {
|
||||||
|
@ -120,3 +150,42 @@ func ExecuteTask(task Task) Task {
|
||||||
return returnTask
|
return returnTask
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Internal functions, for each task
|
||||||
|
|
||||||
|
func taskSetupBootnode(args taskSetupBootnodeArgs) string {
|
||||||
|
|
||||||
|
out, err := exec.Command("tinc-boot", "gen", "--token "+args.BootnodeToken, args.BootnodeAddress+":8655", "--prefix "+args.AssignedAddress).Output()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("%s", err)
|
||||||
|
}
|
||||||
|
output := string(out[:])
|
||||||
|
fmt.Println(output)
|
||||||
|
|
||||||
|
out, err = exec.Command("systemctl", "start", "tinc@dnet").Output()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("%s", err)
|
||||||
|
}
|
||||||
|
output = string(out[:])
|
||||||
|
fmt.Println(output)
|
||||||
|
|
||||||
|
out, err = exec.Command("systemctl", "enable", "tinc@dnet").Output()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("%s", err)
|
||||||
|
}
|
||||||
|
output = string(out[:])
|
||||||
|
fmt.Println(output)
|
||||||
|
|
||||||
|
return output
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func taskStartEdgeApp() string {
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
|
|
||||||
|
func taskStopEdgeApp() string {
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue