From 7e42e72bb2526cf57513e30647f826a190094a33 Mon Sep 17 00:00:00 2001 From: Paulo Truta Date: Tue, 3 Dec 2024 09:20:44 +0100 Subject: [PATCH] Implemented scaffolding for BrowserDev tasks and implemented taskGetBrowserDevPassword --- go.mod | 1 + go.sum | 2 ++ internal/system/system.go | 32 ++++++++++++++++++++++++++ internal/tasks/tasks.go | 47 +++++++++++++++++++++++++++++++++++++++ internal/utils/utils.go | 8 +++++++ 5 files changed, 90 insertions(+) diff --git a/go.mod b/go.mod index 1645bc4..8ebbfd8 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/dustin/go-humanize v1.0.0 // indirect github.com/go-ole/go-ole v1.2.5 // indirect github.com/go-sql-driver/mysql v1.5.0 + github.com/go-yaml/yaml v2.1.0+incompatible // indirect github.com/joho/godotenv v1.3.0 github.com/mattn/go-sqlite3 v1.14.7 // indirect github.com/shirou/gopsutil v3.21.4+incompatible // indirect diff --git a/go.sum b/go.sum index c25bdef..80280ae 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,8 @@ github.com/go-ole/go-ole v1.2.5 h1:t4MGB5xEDZvXI+0rMjjsfBsD7yAgp/s9ZDkL1JndXwY= github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/go-yaml/yaml v2.1.0+incompatible h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o= +github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= diff --git a/internal/system/system.go b/internal/system/system.go index 83fe96d..2249579 100644 --- a/internal/system/system.go +++ b/internal/system/system.go @@ -7,6 +7,7 @@ import ( "log" "os" "io" + "errors" "os/exec" "bufio" "path/filepath" @@ -17,6 +18,7 @@ import ( "github.com/joho/godotenv" "github.com/shirou/gopsutil/host" + "github.com/go-yaml/yaml" ) type cloudflaredTunnelJson struct { @@ -516,3 +518,33 @@ func ApplyUpdates() { utils.WriteOption("UPDATING_SYSTEM", "false") } +func FetchBrowserDevPasswordFromFile() (string, error) { + fmt.Println("Executing FetchBrowserDevPasswordFromFile") + + // Read the "password" entry on the yaml file + // Read the yaml file in system.GetPath(BrowserDevPasswordFileLocation) + yamlFile, err := ioutil.ReadFile(utils.GetPath(utils.BrowserDevPasswordFileLocation)) + if err != nil { + return "", err + } + + // Parse the yaml file and get the "password" entry + var yamlFileMap yaml.MapSlice + err = yaml.Unmarshal(yamlFile, &yamlFileMap) + if err != nil { + return "", err + } + + for _, item := range yamlFileMap { + key, value := item.Key, item.Value + if key == "password" { + if pwString, ok := value.(string); ok { + return pwString, nil + } else { + return "", errors.New("password value is not a string") + } + } + } + return "", errors.New("password key not found") +} + diff --git a/internal/tasks/tasks.go b/internal/tasks/tasks.go index 6474821..0df3c77 100644 --- a/internal/tasks/tasks.go +++ b/internal/tasks/tasks.go @@ -291,6 +291,11 @@ func ExecuteTask(task Task) Task { taskResult := taskStopShell() task.Result = sql.NullString{String: taskResult, Valid: true} + case "activate_browser_dev": + log.Println("Activating Browser Dev Environment") + taskResult := taskActivateBrowserDev() + task.Result = sql.NullString{String: taskResult, Valid: true} + case "install_edgeapp": log.Println("Installing EdgeApp...") @@ -495,6 +500,8 @@ func ExecuteSchedules(tick int) { if tick == 1 { + resultPassword := taskGetBrowserDevPassword() + ip := taskGetSystemIP() log.Println("System IP is: " + ip) @@ -1099,6 +1106,46 @@ func taskStopShell() string { } +func taskActivateBrowserDev() string { + fmt.Println("Executing taskActivateBrowserDev") + wsPath := utils.GetPath(utils.WsPath) + + // start the code-server process + // utils.Exec(wsPath, "killall", []string{"sshx"}) + utils.Exec(wsPath, "killall", []string{"code-server"}) + + utils.WriteOption("BROWSERDEV_STATUS", "running") + + return "{\"status\": \"ok\"}" +} + +func taskDeactivateBrowserDev() string { + fmt.Println("Executing taskDeactivateBrowserDev") + utils.WriteOption("BROWSERDEV_STATUS", "not_running") + return "{\"status\": \"ok\"}" +} + +func taskGetBrowserDevPassword() string { + fmt.Println("Executing taskGetBrowserDevPassword") + password := utils.ReadOption("BROWSERDEV_PASSWORD") + if password == "" { + password, err := system.FetchBrowserDevPasswordFromFile() + if err == nil { + utils.WriteOption("BROWSERDEV_PASSWORD", password) + } else { + fmt.Println("Error fetching browser dev password from file: " + err.Error()) + } + } + return password +} + +// func taskSetBrowserDevPassword(args taskSetBrowserDevPasswordArgs) string { +// fmt.Println("Executing taskSetBrowserDevPassword") +// system.SetBrowserDevPassword(args.Password) +// utils.WriteOption("BROWSERDEV_PASSWORD", args.Password) +// 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 8b43984..1ce0bfe 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -112,6 +112,7 @@ const EdgeAppsPath string = "edgeAppsPath" const EdgeAppsBackupPath string = "edgeAppsBackupPath" const WsPath string = "wsPath" const LoggerPath string = "loggerPath" +const BrowserDevPasswordFileLocation string = "browserDevPasswordFileLocation" // GetPath : Returns either the hardcoded path, or a overwritten value via .env file at project root. Register paths here for seamless working code between dev and prod environments ;) @@ -189,6 +190,13 @@ func GetPath(pathKey string) string { targetPath = "/home/system/components/backups/pw.txt" } + case BrowserDevPasswordFileLocation: + if env["BROWSERDEV_PASSWORD_FILE_LOCATION"] != "" { + targetPath = env["BROWSERDEV_PASSWORD_FILE_LOCATION"] + } else { + targetPath = "/root/.config/code-server/config.yaml" + } + default: log.Printf("path_key %s nonexistant in GetPath().\n", pathKey)