diff --git a/.gitignore b/.gitignore index d571598..964bc3f 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,6 @@ main # Build /bin + +# Logs from executed scripts +/scripts/output.log diff --git a/internal/system/system.go b/internal/system/system.go index 79ea704..95a6538 100644 --- a/internal/system/system.go +++ b/internal/system/system.go @@ -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) diff --git a/internal/tasks/tasks.go b/internal/tasks/tasks.go index 9e799d4..dcf3dd9 100644 --- a/internal/tasks/tasks.go +++ b/internal/tasks/tasks.go @@ -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) diff --git a/internal/utils/utils.go b/internal/utils/utils.go index e35ac41..e56b7a7 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -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() +} diff --git a/scripts/cloudflared_tunnel_create.sh b/scripts/cloudflared_tunnel_create.sh index 136e815..097d6b0 100755 --- a/scripts/cloudflared_tunnel_create.sh +++ b/scripts/cloudflared_tunnel_create.sh @@ -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 \ No newline at end of file diff --git a/scripts/cloudflared_tunnel_delete.sh b/scripts/cloudflared_tunnel_delete.sh index 33594ea..5a237bb 100755 --- a/scripts/cloudflared_tunnel_delete.sh +++ b/scripts/cloudflared_tunnel_delete.sh @@ -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 \ No newline at end of file