Various improvements and developments to tasks execution logic
parent
461a9e6558
commit
1e9be94851
|
@ -1,6 +1,7 @@
|
||||||
package tasks
|
package tasks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -43,7 +44,7 @@ type Task struct {
|
||||||
Updated string `json:"updated"`
|
Updated string `json:"updated"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type taskSetupBootnodeArgs struct {
|
type taskSetupTunnelArgs struct {
|
||||||
BootnodeAddress string `json:"bootnode_address"`
|
BootnodeAddress string `json:"bootnode_address"`
|
||||||
BootnodeToken string `json:"bootnode_token"`
|
BootnodeToken string `json:"bootnode_token"`
|
||||||
AssignedAddress string `json:"assigned_address"`
|
AssignedAddress string `json:"assigned_address"`
|
||||||
|
@ -114,16 +115,17 @@ func ExecuteTask(task Task) Task {
|
||||||
if Version == "dev" {
|
if Version == "dev" {
|
||||||
log.Printf("Dev environemnt. Not executing tasks.")
|
log.Printf("Dev environemnt. Not executing tasks.")
|
||||||
} else {
|
} else {
|
||||||
|
log.Println("Task: " + task.Task)
|
||||||
switch task.Task {
|
switch task.Task {
|
||||||
case "setup_bootnode":
|
case "setup_tunnel":
|
||||||
|
|
||||||
log.Println("Setting up bootnode connection...")
|
log.Println("Setting up bootnode connection...")
|
||||||
var args taskSetupBootnodeArgs
|
var args taskSetupTunnelArgs
|
||||||
err := json.Unmarshal([]byte(task.Args), &args)
|
err := json.Unmarshal([]byte(task.Args), &args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error reading arguments of setup_bootnode task: %s", err)
|
log.Printf("Error reading arguments of setup_bootnode task: %s", err)
|
||||||
} else {
|
} else {
|
||||||
taskResult = taskSetupBootnode(args)
|
taskResult := taskSetupTunnel(args)
|
||||||
task.Result = sql.NullString{String: taskResult, Valid: true}
|
task.Result = sql.NullString{String: taskResult, Valid: true}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,33 +156,35 @@ func ExecuteTask(task Task) Task {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Internal functions, for each task
|
func executeCommand(command string, args []string) string {
|
||||||
|
cmd := exec.Command(command, args...)
|
||||||
func taskSetupBootnode(args taskSetupBootnodeArgs) string {
|
var out bytes.Buffer
|
||||||
|
var stderr bytes.Buffer
|
||||||
fmt.Println("Executing taskSetupBootnode")
|
cmd.Stdout = &out
|
||||||
|
cmd.Stderr = &stderr
|
||||||
out, err := exec.Command("tinc-boot", "gen", "--name "+args.NodeName, "--token "+args.BootnodeToken, args.BootnodeAddress+":8655", "--prefix "+args.AssignedAddress).Output()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error: %s", err)
|
log.Println(fmt.Sprint(err) + ": " + stderr.String())
|
||||||
}
|
}
|
||||||
output := string(out[:])
|
log.Println("Result: " + out.String())
|
||||||
fmt.Println(output)
|
|
||||||
|
|
||||||
out, err = exec.Command("systemctl", "start", "tinc@dnet").Output()
|
return out.String()
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error: %s", err)
|
|
||||||
}
|
}
|
||||||
output = string(out[:])
|
|
||||||
log.Printf(output)
|
|
||||||
|
|
||||||
out, err = exec.Command("systemctl", "enable", "tinc@dnet").Output()
|
func taskSetupTunnel(args taskSetupTunnelArgs) string {
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error: %s", err)
|
|
||||||
}
|
|
||||||
output = string(out[:])
|
|
||||||
log.Printf(output)
|
|
||||||
|
|
||||||
|
fmt.Println("Executing taskSetupTunnel")
|
||||||
|
|
||||||
|
cmdargs := []string{"gen", "--name", args.NodeName, "--token", args.BootnodeToken, args.BootnodeAddress+":8655", "--prefix", args.AssignedAddress}
|
||||||
|
executeCommand("tinc-boot", cmdargs)
|
||||||
|
|
||||||
|
cmdargs = []string{"start", "tinc@dnet"}
|
||||||
|
executeCommand("systemctl", cmdargs)
|
||||||
|
|
||||||
|
cmdargs = []string{"enable", "tinc@dnet"}
|
||||||
|
executeCommand("systemctl", cmdargs)
|
||||||
|
|
||||||
|
output := "OK" // Better check / logging of command execution result.
|
||||||
return output
|
return output
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue