Added utils, remaining tunnel tasks, cleanup

pull/29/head
Paulo Truta 2023-03-19 18:56:19 +00:00
parent 86322dea61
commit 1c9adc1afd
6 changed files with 118 additions and 17 deletions

3
.gitignore vendored
View File

@ -21,3 +21,6 @@ main
# Build
/bin
# Logs from executed scripts
/scripts/output.log

View File

@ -4,6 +4,13 @@ import (
"fmt"
"strconv"
"strings"
"log"
"os"
"os/exec"
"bufio"
"path/filepath"
"io/ioutil"
"encoding/json"
"github.com/edgebox-iot/edgeboxctl/internal/utils"
@ -11,6 +18,12 @@ import (
"github.com/shirou/gopsutil/host"
)
type cloudflaredTunnelJson struct {
AccountTag string `json:"AccountTag"`
TunnelSecret string `json:"TunnelSecret"`
TunnelID string `json:"TunnelID"`
}
// GetUptimeInSeconds: Returns a value (as string) of the total system uptime
func GetUptimeInSeconds() string {
uptime, _ := host.Uptime()
@ -88,6 +101,7 @@ func SetupCloudOptions() {
// StartService: Starts a service
func StartService(serviceID string) {
wsPath := utils.GetPath(utils.WsPath)
fmt.Println("Starting" + serviceID + "service")
cmdargs := []string{"start", serviceID}
utils.Exec(wsPath, "systemctl", cmdargs)
@ -95,6 +109,7 @@ func StartService(serviceID string) {
// StopService: Stops a service
func StopService(serviceID string) {
wsPath := utils.GetPath(utils.WsPath)
fmt.Println("Stopping" + serviceID + "service")
cmdargs := []string{"stop", "cloudflared"}
utils.Exec(wsPath, "systemctl", cmdargs)
@ -118,12 +133,12 @@ func GetServiceStatus(serviceID string) string {
// CreateTunnel: Creates a tunnel via cloudflared, needs to be authenticated first
func CreateTunnel(configDestination string) {
fmt.Println("Creating Tunnel for Edgebox.")
cmd = exec.Command("sh", "/home/system/components/edgeboxctl/scripts/cloudflared_tunnel_create.sh")
stdout, err = cmd.StdoutPipe()
cmd := exec.Command("sh", "/home/system/components/edgeboxctl/scripts/cloudflared_tunnel_create.sh")
stdout, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
scanner = bufio.NewScanner(stdout)
scanner := bufio.NewScanner(stdout)
err = cmd.Start()
if err != nil {
panic(err)
@ -249,7 +264,7 @@ func DeleteTunnel() {
// InstallTunnelService: Installs the tunnel service
func InstallTunnelService(config string) {
fmt.Println("Installing cloudflared service.")
cmd = exec.Command("cloudflared", "--config", config, "service", "install")
cmd := exec.Command("cloudflared", "--config", config, "service", "install")
cmd.Start()
cmd.Wait()
}
@ -263,7 +278,7 @@ func RemoveTunnelService() {
cmd.Wait()
fmt.Println("Removing cloudflared files")
cmdargs = []string{"-rf", "/home/system/.cloudflared"}
cmdargs := []string{"-rf", "/home/system/.cloudflared"}
utils.Exec(wsPath, "rm", cmdargs)
cmdargs = []string{"-rf", "/etc/cloudflared/config.yml"}
utils.Exec(wsPath, "rm", cmdargs)

View File

@ -10,15 +10,14 @@ import (
"os/exec"
"strings"
"os"
"io/ioutil"
"bufio"
"path/filepath"
"github.com/edgebox-iot/edgeboxctl/internal/diagnostics"
"github.com/edgebox-iot/edgeboxctl/internal/edgeapps"
"github.com/edgebox-iot/edgeboxctl/internal/storage"
"github.com/edgebox-iot/edgeboxctl/internal/system"
"github.com/edgebox-iot/edgeboxctl/internal/utils"
_ "github.com/go-sql-driver/mysql" // Mysql Driver
_ "github.com/mattn/go-sqlite3" // SQlite Driver
)
@ -67,12 +66,6 @@ type taskEnablePublicDashboardArgs struct {
InternetURL string `json:"internet_url"`
}
type cloudflaredTunnelJson struct {
AccountTag string `json:"AccountTag"`
TunnelSecret string `json:"TunnelSecret"`
TunnelID string `json:"TunnelID"`
}
const STATUS_CREATED int = 0
const STATUS_EXECUTING int = 1
@ -156,6 +149,24 @@ func ExecuteTask(task Task) Task {
task.Result = sql.NullString{String: taskResult, Valid: true}
}
case "start_tunnel":
log.Println("Starting Cloudflare Tunnel...")
taskResult := taskStartTunnel()
task.Result = sql.NullString{String: taskResult, Valid: true}
case "stop_tunnel":
log.Println("Stopping Cloudflare Tunnel...")
taskResult := taskStopTunnel()
task.Result = sql.NullString{String: taskResult, Valid: true}
case "disable_tunnel":
log.Println("Disabling Cloudflare Tunnel...")
taskResult := taskDisableTunnel()
task.Result = sql.NullString{String: taskResult, Valid: true}
case "install_edgeapp":
log.Println("Installing EdgeApp...")
@ -349,10 +360,10 @@ func taskSetupTunnel(args taskSetupTunnelArgs) string {
system.RemoveTunnelService()
fmt.Println("Creating cloudflared folder")
cmdargs = []string{"/home/system/.cloudflared"}
cmdargs := []string{"/home/system/.cloudflared"}
utils.Exec(wsPath, "mkdir", cmdargs)
cmd = exec.Command("sh", "/home/system/components/edgeboxctl/scripts/cloudflared_login.sh")
cmd := exec.Command("sh", "/home/system/components/edgeboxctl/scripts/cloudflared_login.sh")
stdout, err := cmd.StdoutPipe()
if err != nil {
panic(err)
@ -449,6 +460,34 @@ func taskSetupTunnel(args taskSetupTunnelArgs) string {
return "{\"url\": \"" + url + "\"}"
}
func taskStartTunnel() string {
fmt.Println("Executing taskStartTunnel")
system.StartService("cloudflared")
domainName := utils.ReadOption("DOMAIN_NAME")
status := "{\"status\": \"connected\", \"domain\": \"" + domainName + "\"}"
utils.WriteOption("TUNNEL_STATUS", status)
return "{\"status\": \"ok\"}"
}
func taskStopTunnel() string {
fmt.Println("Executing taskStopTunnel")
system.StopService("cloudflared")
domainName := utils.ReadOption("DOMAIN_NAME")
status := "{\"status\": \"stopped\", \"domain\": \"" + domainName + "\"}"
utils.WriteOption("TUNNEL_STATUS", status)
return "{\"status\": \"ok\"}"
}
func taskDisableTunnel() string {
fmt.Println("Executing taskDisableTunnel")
system.StopService("cloudflared")
system.DeleteTunnel()
system.RemoveTunnelService()
utils.DeleteOption("DOMAIN_NAME")
utils.DeleteOption("TUNNEL_STATUS")
return "{\"status\": \"ok\"}"
}
func taskInstallEdgeApp(args taskInstallEdgeAppArgs) string {
fmt.Println("Executing taskInstallEdgeApp for " + args.ID)

View File

@ -192,3 +192,47 @@ func WriteOption(optionKey string, optionValue string) {
db.Close()
}
// ReadOption : Reads a key value pair option from the api shared database
func ReadOption(optionKey string) string {
db, err := sql.Open("sqlite3", GetSQLiteDbConnectionDetails())
if err != nil {
log.Fatal(err.Error())
}
var optionValue string
err = db.QueryRow("SELECT value FROM option WHERE name = ?", optionKey).Scan(&optionValue)
if err != nil {
log.Fatal(err.Error())
}
db.Close()
return optionValue
}
// DeleteOption : Deletes a key value pair option from the api shared database
func DeleteOption(optionKey string) {
db, err := sql.Open("sqlite3", GetSQLiteDbConnectionDetails())
if err != nil {
log.Fatal(err.Error())
}
statement, err := db.Prepare("DELETE FROM option WHERE name = ?;") // Prepare SQL Statement
if err != nil {
log.Fatal(err.Error())
}
_, err = statement.Exec(optionKey) // Execute SQL Statement
if err != nil {
log.Fatal(err.Error())
}
db.Close()
}

View File

@ -2,6 +2,6 @@
echo "Starting script create"
# script -q -c "cloudflared tunnel login 2>&1 | tee /app/output.log" &
cloudflared tunnel create edgebox 2>&1 | tee /home/system/components/edgeboxctl/scripts/create_output.log
cloudflared tunnel create edgebox 2>&1 | tee /home/system/components/edgeboxctl/scripts/output.log
echo "sleeping 5 seconds"
sleep 5

View File

@ -2,6 +2,6 @@
echo "Starting script delete"
TUNNEL_ORIGIN_CERT=/home/system/.cloudflared/cert.pem
cloudflared tunnel delete edgebox 2>&1 | tee /home/system/components/edgeboxctl/scripts/delete_output.log &
cloudflared tunnel delete edgebox 2>&1 | tee /home/system/components/edgeboxctl/scripts/output.log &
echo "sleeping 5 seconds"
sleep 5