Added utils, remaining tunnel tasks, cleanup
parent
86322dea61
commit
1c9adc1afd
|
@ -21,3 +21,6 @@ main
|
||||||
# Build
|
# Build
|
||||||
|
|
||||||
/bin
|
/bin
|
||||||
|
|
||||||
|
# Logs from executed scripts
|
||||||
|
/scripts/output.log
|
||||||
|
|
|
@ -4,6 +4,13 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"bufio"
|
||||||
|
"path/filepath"
|
||||||
|
"io/ioutil"
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
"github.com/edgebox-iot/edgeboxctl/internal/utils"
|
"github.com/edgebox-iot/edgeboxctl/internal/utils"
|
||||||
|
|
||||||
|
@ -11,6 +18,12 @@ import (
|
||||||
"github.com/shirou/gopsutil/host"
|
"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
|
// GetUptimeInSeconds: Returns a value (as string) of the total system uptime
|
||||||
func GetUptimeInSeconds() string {
|
func GetUptimeInSeconds() string {
|
||||||
uptime, _ := host.Uptime()
|
uptime, _ := host.Uptime()
|
||||||
|
@ -88,6 +101,7 @@ func SetupCloudOptions() {
|
||||||
|
|
||||||
// StartService: Starts a service
|
// StartService: Starts a service
|
||||||
func StartService(serviceID string) {
|
func StartService(serviceID string) {
|
||||||
|
wsPath := utils.GetPath(utils.WsPath)
|
||||||
fmt.Println("Starting" + serviceID + "service")
|
fmt.Println("Starting" + serviceID + "service")
|
||||||
cmdargs := []string{"start", serviceID}
|
cmdargs := []string{"start", serviceID}
|
||||||
utils.Exec(wsPath, "systemctl", cmdargs)
|
utils.Exec(wsPath, "systemctl", cmdargs)
|
||||||
|
@ -95,6 +109,7 @@ func StartService(serviceID string) {
|
||||||
|
|
||||||
// StopService: Stops a service
|
// StopService: Stops a service
|
||||||
func StopService(serviceID string) {
|
func StopService(serviceID string) {
|
||||||
|
wsPath := utils.GetPath(utils.WsPath)
|
||||||
fmt.Println("Stopping" + serviceID + "service")
|
fmt.Println("Stopping" + serviceID + "service")
|
||||||
cmdargs := []string{"stop", "cloudflared"}
|
cmdargs := []string{"stop", "cloudflared"}
|
||||||
utils.Exec(wsPath, "systemctl", cmdargs)
|
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
|
// CreateTunnel: Creates a tunnel via cloudflared, needs to be authenticated first
|
||||||
func CreateTunnel(configDestination string) {
|
func CreateTunnel(configDestination string) {
|
||||||
fmt.Println("Creating Tunnel for Edgebox.")
|
fmt.Println("Creating Tunnel for Edgebox.")
|
||||||
cmd = exec.Command("sh", "/home/system/components/edgeboxctl/scripts/cloudflared_tunnel_create.sh")
|
cmd := exec.Command("sh", "/home/system/components/edgeboxctl/scripts/cloudflared_tunnel_create.sh")
|
||||||
stdout, err = cmd.StdoutPipe()
|
stdout, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
scanner = bufio.NewScanner(stdout)
|
scanner := bufio.NewScanner(stdout)
|
||||||
err = cmd.Start()
|
err = cmd.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -249,7 +264,7 @@ func DeleteTunnel() {
|
||||||
// InstallTunnelService: Installs the tunnel service
|
// InstallTunnelService: Installs the tunnel service
|
||||||
func InstallTunnelService(config string) {
|
func InstallTunnelService(config string) {
|
||||||
fmt.Println("Installing cloudflared service.")
|
fmt.Println("Installing cloudflared service.")
|
||||||
cmd = exec.Command("cloudflared", "--config", config, "service", "install")
|
cmd := exec.Command("cloudflared", "--config", config, "service", "install")
|
||||||
cmd.Start()
|
cmd.Start()
|
||||||
cmd.Wait()
|
cmd.Wait()
|
||||||
}
|
}
|
||||||
|
@ -263,7 +278,7 @@ func RemoveTunnelService() {
|
||||||
cmd.Wait()
|
cmd.Wait()
|
||||||
|
|
||||||
fmt.Println("Removing cloudflared files")
|
fmt.Println("Removing cloudflared files")
|
||||||
cmdargs = []string{"-rf", "/home/system/.cloudflared"}
|
cmdargs := []string{"-rf", "/home/system/.cloudflared"}
|
||||||
utils.Exec(wsPath, "rm", cmdargs)
|
utils.Exec(wsPath, "rm", cmdargs)
|
||||||
cmdargs = []string{"-rf", "/etc/cloudflared/config.yml"}
|
cmdargs = []string{"-rf", "/etc/cloudflared/config.yml"}
|
||||||
utils.Exec(wsPath, "rm", cmdargs)
|
utils.Exec(wsPath, "rm", cmdargs)
|
||||||
|
|
|
@ -10,15 +10,14 @@ import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"os"
|
"os"
|
||||||
"io/ioutil"
|
|
||||||
"bufio"
|
"bufio"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/edgebox-iot/edgeboxctl/internal/diagnostics"
|
"github.com/edgebox-iot/edgeboxctl/internal/diagnostics"
|
||||||
"github.com/edgebox-iot/edgeboxctl/internal/edgeapps"
|
"github.com/edgebox-iot/edgeboxctl/internal/edgeapps"
|
||||||
"github.com/edgebox-iot/edgeboxctl/internal/storage"
|
"github.com/edgebox-iot/edgeboxctl/internal/storage"
|
||||||
"github.com/edgebox-iot/edgeboxctl/internal/system"
|
"github.com/edgebox-iot/edgeboxctl/internal/system"
|
||||||
"github.com/edgebox-iot/edgeboxctl/internal/utils"
|
"github.com/edgebox-iot/edgeboxctl/internal/utils"
|
||||||
|
|
||||||
_ "github.com/go-sql-driver/mysql" // Mysql Driver
|
_ "github.com/go-sql-driver/mysql" // Mysql Driver
|
||||||
_ "github.com/mattn/go-sqlite3" // SQlite Driver
|
_ "github.com/mattn/go-sqlite3" // SQlite Driver
|
||||||
)
|
)
|
||||||
|
@ -67,12 +66,6 @@ type taskEnablePublicDashboardArgs struct {
|
||||||
InternetURL string `json:"internet_url"`
|
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_CREATED int = 0
|
||||||
const STATUS_EXECUTING int = 1
|
const STATUS_EXECUTING int = 1
|
||||||
|
@ -156,6 +149,24 @@ func ExecuteTask(task Task) Task {
|
||||||
task.Result = sql.NullString{String: taskResult, Valid: true}
|
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":
|
case "install_edgeapp":
|
||||||
|
|
||||||
log.Println("Installing EdgeApp...")
|
log.Println("Installing EdgeApp...")
|
||||||
|
@ -349,10 +360,10 @@ func taskSetupTunnel(args taskSetupTunnelArgs) string {
|
||||||
system.RemoveTunnelService()
|
system.RemoveTunnelService()
|
||||||
|
|
||||||
fmt.Println("Creating cloudflared folder")
|
fmt.Println("Creating cloudflared folder")
|
||||||
cmdargs = []string{"/home/system/.cloudflared"}
|
cmdargs := []string{"/home/system/.cloudflared"}
|
||||||
utils.Exec(wsPath, "mkdir", cmdargs)
|
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()
|
stdout, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -449,6 +460,34 @@ func taskSetupTunnel(args taskSetupTunnelArgs) string {
|
||||||
return "{\"url\": \"" + url + "\"}"
|
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 {
|
func taskInstallEdgeApp(args taskInstallEdgeAppArgs) string {
|
||||||
fmt.Println("Executing taskInstallEdgeApp for " + args.ID)
|
fmt.Println("Executing taskInstallEdgeApp for " + args.ID)
|
||||||
|
|
||||||
|
|
|
@ -192,3 +192,47 @@ func WriteOption(optionKey string, optionValue string) {
|
||||||
|
|
||||||
db.Close()
|
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()
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,6 @@
|
||||||
|
|
||||||
echo "Starting script create"
|
echo "Starting script create"
|
||||||
# script -q -c "cloudflared tunnel login 2>&1 | tee /app/output.log" &
|
# 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"
|
echo "sleeping 5 seconds"
|
||||||
sleep 5
|
sleep 5
|
|
@ -2,6 +2,6 @@
|
||||||
|
|
||||||
echo "Starting script delete"
|
echo "Starting script delete"
|
||||||
TUNNEL_ORIGIN_CERT=/home/system/.cloudflared/cert.pem
|
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"
|
echo "sleeping 5 seconds"
|
||||||
sleep 5
|
sleep 5
|
Loading…
Reference in New Issue