From 613cf4c511e3704c83aa8c0848a1db7004cf7434 Mon Sep 17 00:00:00 2001 From: Moses Narrow Date: Sun, 17 Apr 2022 14:58:04 -0500 Subject: [PATCH 1/5] update -p --pkg flag for visor --- cmd/skywire-cli/commands/config/gen.go | 15 +- cmd/skywire-visor/commands/root.go | 347 +++++++++++++------------ pkg/skyenv/values.go | 6 - pkg/skyenv/values_darwin.go | 4 + pkg/skyenv/values_linux.go | 4 + pkg/skyenv/values_windows.go | 4 + 6 files changed, 194 insertions(+), 186 deletions(-) diff --git a/cmd/skywire-cli/commands/config/gen.go b/cmd/skywire-cli/commands/config/gen.go index b545ff20dd..f8cecae506 100644 --- a/cmd/skywire-cli/commands/config/gen.go +++ b/cmd/skywire-cli/commands/config/gen.go @@ -82,7 +82,7 @@ func init() { genConfigCmd.Flags().BoolVarP(&stdout, "stdout", "n", false, "write config to stdout") hiddenflags = append(hiddenflags, "stdout") genConfigCmd.Flags().StringVarP(&output, "out", "o", "", "output config: "+skyenv.ConfigName) - genConfigCmd.Flags().BoolVarP(&pkgEnv, "pkg", "p", false, "use paths for package: "+skyenv.SkywirePath) + genConfigCmd.Flags().BoolVarP(&pkgEnv, "pkg", "p", false, skyenv.Ptext) homepath := skyenv.HomePath() if homepath != "" { genConfigCmd.Flags().BoolVarP(&usrEnv, "user", "u", false, "use paths for user space: "+homepath) @@ -204,15 +204,10 @@ var genConfigCmd = &cobra.Command{ } //fetch the service endpoints services = visorconfig.Fetch(mLog, serviceConfURL, stdout) - // skywire-cli config gen -ip || skywire-cli config gen -p - if !stdout && outunset && (selectedOS == "linux") { - if pkgEnv { - if hypervisor { - //default config hypervisor - configName = skyenv.Skywirejson - } else { - configName = skyenv.Skywirevisorjson - } + // skywire-cli config gen -p + if !stdout && outunset { + if pkgEnv && (selectedOS == "linux") { + configName = skyenv.Configjson confPath = skyenv.SkywirePath + "/" + configName } if usrEnv { diff --git a/cmd/skywire-visor/commands/root.go b/cmd/skywire-visor/commands/root.go index aea706c9b6..99fb54d36d 100644 --- a/cmd/skywire-visor/commands/root.go +++ b/cmd/skywire-visor/commands/root.go @@ -62,7 +62,6 @@ var ( hiddenflags []string all bool pkg bool - pkg1 bool usr bool // skywire is the path to the running visor binary skywire string @@ -75,43 +74,41 @@ var ( ) func init() { - thisUser, err := user.Current() + usrLvl, err := user.Current() if err != nil { panic(err) } - if thisUser.Username == "root" { + if usrLvl.Username == "root" { root = true } rootCmd.Flags().SortFlags = false rootCmd.Flags().StringVarP(&confPath, "config", "c", skyenv.ConfigName, "config file to use") - rootCmd.Flags().BoolVarP(&hypervisorUI, "hvui", "i", false, "run as hypervisor") if ((skyenv.OS == "linux") && !root) || ((skyenv.OS == "mac") && !root) || (skyenv.OS == "win") { rootCmd.Flags().BoolVarP(&launchBrowser, "browser", "b", false, "open hypervisor ui in default web browser") } + rootCmd.Flags().BoolVarP(&hypervisorUI, "hvui", "i", false, "run as hypervisor") rootCmd.Flags().StringVarP(&remoteHypervisorPKs, "hv", "j", "", "add remote hypervisor PKs at runtime") hiddenflags = append(hiddenflags, "hv") rootCmd.Flags().BoolVarP(&disableHypervisorPKs, "xhv", "k", false, "disable remote hypervisors set in config file") hiddenflags = append(hiddenflags, "xhv") rootCmd.Flags().BoolVarP(&stdin, "stdin", "n", false, "read config from stdin") hiddenflags = append(hiddenflags, "stdin") - if skyenv.OS == "linux" { - if _, err := os.Stat(skyenv.SkywirePath + "/" + skyenv.Skywirejson); err == nil { - rootCmd.Flags().BoolVar(&pkg, "ph", false, "use package config "+skyenv.SkywirePath+"/"+skyenv.Skywirejson) - hiddenflags = append(hiddenflags, "ph") - } - if _, err := os.Stat(skyenv.SkywirePath + "/" + skyenv.Skywirevisorjson); err == nil { - rootCmd.Flags().BoolVar(&pkg1, "pv", false, "use package config "+skyenv.SkywirePath+"/"+skyenv.Skywirevisorjson) - hiddenflags = append(hiddenflags, "pv") + if root { + if _, err := os.Stat(skyenv.SkywirePath + "/" + skyenv.Configjson); err == nil { + rootCmd.Flags().BoolVarP(&pkg, "pkg", "p", false, "use package config "+skyenv.SkywirePath+"/"+skyenv.Configjson) + hiddenflags = append(hiddenflags, "pkg") } } - if _, err := os.Stat(skyenv.HomePath() + "/" + skyenv.ConfigName); err == nil { - rootCmd.Flags().BoolVarP(&usr, "user", "u", false, "use config at: $HOME/"+skyenv.ConfigName) + if !root { + if _, err := os.Stat(skyenv.HomePath() + "/" + skyenv.ConfigName); err == nil { + rootCmd.Flags().BoolVarP(&usr, "user", "u", false, "use config at: $HOME/"+skyenv.ConfigName) + } } - rootCmd.Flags().StringVarP(&pprofMode, "pprofmode", "p", "", "pprof mode: cpu, mem, mutex, block, trace, http") + rootCmd.Flags().StringVarP(&pprofMode, "pprofmode", "q", "", "pprof mode: cpu, mem, mutex, block, trace, http") hiddenflags = append(hiddenflags, "pprofmode") - rootCmd.Flags().StringVarP(&pprofAddr, "pprofaddr", "q", "localhost:6060", "pprof http port") + rootCmd.Flags().StringVarP(&pprofAddr, "pprofaddr", "r", "localhost:6060", "pprof http port") hiddenflags = append(hiddenflags, "pprofaddr") rootCmd.Flags().StringVarP(&tag, "tag", "t", "skywire", "logging tag") hiddenflags = append(hiddenflags, "tag") @@ -173,22 +170,20 @@ var rootCmd = &cobra.Command{ os.Exit(1) } //log for initial checks - log := initLogger(tag, syslogAddr) - _, hook := logstore.MakeStore(runtimeLogMaxEntries) - log.AddHook(hook) + mLog := initLogger(tag, syslogAddr) + log := mLog.PackageLogger("pre-run") + + //_, hook := logstore.MakeStore(runtimeLogMaxEntries) + //log.AddHook(hook) if !stdin { //error on multiple configs from flags - if (pkg && pkg1) || (pkg && usr) || (pkg1 && usr) || ((pkg || pkg1) && (confPath != "")) { + if (pkg && usr) || ((pkg || usr) && (confPath != "")) { fmt.Println("Error: multiple configs specified") os.Exit(1) } - //use package hypervisor config + //use package config if pkg { - confPath = skyenv.SkywirePath + "/" + skyenv.Skywirejson - } - //use package visor config - if pkg1 { - confPath = skyenv.SkywirePath + "/" + skyenv.Skywirevisorjson + confPath = skyenv.SkywirePath + "/" + skyenv.Configjson } if usr { confPath = skyenv.HomePath() + "/" + skyenv.ConfigName @@ -207,88 +202,9 @@ var rootCmd = &cobra.Command{ } else { confPath = visorconfig.StdinName } - var fork string - var branch string - var nocommit string - //indicates how skywire was started - skywire = os.Args[0] - //indicates where skywire was started - path, err := os.Getwd() - if err != nil { - log.WithError(err).Fatal() - } - workDir = path - //retrieve build info - visorBuildInfo = buildinfo.Get() - if visorBuildInfo.Version == "unknown" { - if match := strings.Contains("/tmp/", skywire); err == nil { - if match { - log.Info("executed with go run") - log.WithField("binary: ", skywire).Info() - } - } - //check for .git folder for versioning - if _, err := os.Stat(".git"); err == nil { - //attempt to version from git sources - if _, err = exec.LookPath("git"); err == nil { - if version, err := script.Exec(`git describe`).String(); err == nil { - visorBuildInfo.Version = strings.ReplaceAll(version, "\n", "") - if visorBuildInfo.Commit == "unknown" { - if nocommit, err = script.Exec(`git diff-index HEAD --`).String(); err == nil { - if commit, err := script.Exec(`git rev-list -1 HEAD`).String(); err == nil { - visorBuildInfo.Commit = strings.ReplaceAll(commit, "\n", "") - } - } - } - if fork, err = script.Exec(`git config --get remote.origin.url`).String(); err == nil { - fork = strings.ReplaceAll(fork, "ssh://", "") - fork = strings.ReplaceAll(fork, "git@", "") - fork = strings.ReplaceAll(fork, "https://", "") - fork = strings.ReplaceAll(fork, "http://", "") - fork = strings.ReplaceAll(fork, "github.com/", "") - fork = strings.ReplaceAll(fork, ":/", "") - fork = strings.ReplaceAll(fork, "\n", "") - nofork, err := regexp.MatchString("skycoin/skywire", fork) - if err != nil { - log.Error(err) - } else { - log.Info(nofork) - if !nofork { - fork = "" - } - } - } - if branch, err = script.Exec(`git rev-parse --abbrev-ref HEAD`).String(); err == nil { - branch = strings.ReplaceAll(branch, "\n", "") - if _, err = exec.LookPath("date"); err == nil { - if visorBuildInfo.Date == "unknown" { - if date, err := script.Exec(`date -u +%Y-%m-%dT%H:%M:%SZ`).String(); err == nil { - visorBuildInfo.Date = strings.ReplaceAll(date, "\n", "") - } - } - } - } - } - } - } - } - log.WithField("version", visorBuildInfo.Version).Info() - if visorBuildInfo.Date != "unknown" && visorBuildInfo.Date != "" { - log.WithField("built on", visorBuildInfo.Date).Info() - } - if visorBuildInfo.Commit != "unknown" && visorBuildInfo.Commit != "" { - if (nocommit != "") && (nocommit != "\n") { - log.Info("with changes since commit") - log.WithField("commit", visorBuildInfo.Commit).Info() - } else { - log.WithField("against commit", visorBuildInfo.Commit).Info() - } - if fork != "" { - log.WithField("fork", fork).Info() - } - } - if branch != "unknown" && branch != "" { - log.WithField("branch", branch).Info() + logBuildInfo(mLog) + if launchBrowser { + hypervisorUI = true } }, Run: func(_ *cobra.Command, _ []string) { @@ -299,7 +215,8 @@ var rootCmd = &cobra.Command{ func runVisor(conf *visorconfig.V1) { var ok bool - log := initLogger(tag, syslogAddr) + mLog := initLogger(tag, syslogAddr) + log := mLog store, hook := logstore.MakeStore(runtimeLogMaxEntries) log.AddHook(hook) @@ -309,7 +226,6 @@ func runVisor(conf *visorconfig.V1) { if conf == nil { conf = initConfig(log, confPath) } - //warn about creating files & directories as root in non root-owned dir if _, err := exec.LookPath("stat"); err == nil { pathtolocalpath := strings.ReplaceAll(conf.LocalPath, "local", "") @@ -355,7 +271,7 @@ func runVisor(conf *visorconfig.V1) { vis.SetLogstore(store) if launchBrowser { - runBrowser(conf, log) + runBrowser(log, conf) } ctx, cancel := cmdutil.SignalContext(context.Background(), log) @@ -396,19 +312,17 @@ func Execute(ui embed.FS) { } func initLogger(tag string, syslogAddr string) *logging.MasterLogger { - log := logging.NewMasterLogger() - + mLog := logging.NewMasterLogger() if syslogAddr != "" { hook, err := syslog.SetupHook(syslogAddr, tag) if err != nil { - log.WithError(err).Error("Failed to connect to the syslog daemon.") + mLog.WithError(err).Error("Failed to connect to the syslog daemon.") } else { - log.AddHook(hook) - log.Out = ioutil.Discard + mLog.AddHook(hook) + mLog.Out = ioutil.Discard } } - - return log + return mLog } func initPProf(log *logging.MasterLogger, tag string, profMode string, profAddr string) (stop func()) { @@ -473,57 +387,7 @@ func initConfig(mLog *logging.MasterLogger, confPath string) *visorconfig.V1 { / } if !compat { - log.Error("config version does not match visor version") - log.WithField("skywire version: ", visorBuildInfo.Version).Error() - var updstr string - if match := strings.Contains("/tmp/", skywire); err == nil { - log.Info("match:", match) - if match { - if _, err := os.Stat("cmd/skywire-cli/skywire-cli.go"); err == nil { - updstr = "go run cmd/skywire-cli/skywire-cli.go config gen -b" - } - log.Info("updstr:", updstr) - - } - } - if updstr == "" { - updstr = "skywire-cli config gen -b" - } - if conf.Hypervisor != nil { - updstr = updstr + "i" - } - for _, j := range conf.Hypervisors { - if fmt.Sprintf("\t%s\n", j) != "" { - updstr = updstr + "x" - break - } - } - - if pkgenv := strings.Contains("/opt/skywire/apps", conf.Launcher.BinPath); err == nil { - if pkgenv { - updstr = updstr + "p" - } - } - //there is no config *file* with stdin - if confPath != visorconfig.StdinName { - if _, err = exec.LookPath("stat"); err == nil { - if owner, err := script.Exec(`stat -c '%U' ` + confPath).String(); err == nil { - if (owner == "root") || (owner == "root\n") { - updstr = "sudo " + updstr - } - } - } - updstr = "\n " + updstr + "ro " + confPath + "\n" - } else { - updstr = "\n " + updstr + "n" + " | go run cmd/skywire-visor/skywire-visor.go -n" - if launchBrowser { - updstr = updstr + "b" - } - updstr = updstr + "\n" - } - updstr = "\n " + updstr + "\n" - log.Info("please update your config with the following command:\n", updstr) - log.Fatal("failed to start skywire") + updateConfig(mLog, conf) } if hypervisorUI { config := hypervisorconfig.GenerateWorkDirConfig(false) @@ -536,7 +400,9 @@ func initConfig(mLog *logging.MasterLogger, confPath string) *visorconfig.V1 { / } // runBrowser opens the hypervisor interface in the browser -func runBrowser(conf *visorconfig.V1, log *logging.MasterLogger) { +func runBrowser(mLog *logging.MasterLogger, conf *visorconfig.V1) { + log := mLog.PackageLogger("visor:launch-browser") + if conf.Hypervisor == nil { log.Errorln("Hypervisor not started - cannot start browser with a regular visor") return @@ -581,3 +447,144 @@ func checkHvIsRunning(addr string, retries int) bool { } return false } + +func updateConfig(mLog *logging.MasterLogger, conf *visorconfig.V1) { + log := mLog.PackageLogger("visor:update-config") + + log.Error("config version does not match visor version") + log.WithField("skywire version: ", visorBuildInfo.Version).Error() + var updstr string + match := strings.Contains("/tmp/", skywire) + log.Info("match:", match) + if match { + if _, err := os.Stat("cmd/skywire-cli/skywire-cli.go"); err == nil { + updstr = "go run cmd/skywire-cli/skywire-cli.go config gen -b" + } + log.Info("updstr:", updstr) + } + if updstr == "" { + updstr = "skywire-cli config gen -b" + } + if conf.Hypervisor != nil { + updstr = updstr + "i" + } + for _, j := range conf.Hypervisors { + if fmt.Sprintf("\t%s\n", j) != "" { + updstr = updstr + "x" + break + } + } + pkgenv := strings.Contains("/opt/skywire/apps", conf.Launcher.BinPath) + if pkgenv { + updstr = updstr + "p" + } + //there is no config *file* with stdin + if confPath != visorconfig.StdinName { + if _, err := exec.LookPath("stat"); err == nil { + if owner, err := script.Exec(`stat -c '%U' ` + confPath).String(); err == nil { + if (owner == "root") || (owner == "root\n") { + updstr = "sudo " + updstr + } + } + } + updstr = "\n " + updstr + "ro " + confPath + "\n" + } else { + updstr = "\n " + updstr + "n" + " | go run cmd/skywire-visor/skywire-visor.go -n" + if launchBrowser { + updstr = updstr + "b" + } + updstr = updstr + "\n" + } + updstr = "\n " + updstr + "\n" + log.Info("please update your config with the following command:\n", updstr) + log.Fatal("failed to start skywire") + +} + +func logBuildInfo(mLog *logging.MasterLogger) { + log := mLog.PackageLogger("buildinfo") + var fork string + var branch string + var nocommit string + //indicates how skywire was started + skywire = os.Args[0] + //indicates where skywire was started + path, err := os.Getwd() + if err != nil { + log.WithError(err).Fatal() + } + workDir = path + //retrieve build info + visorBuildInfo = buildinfo.Get() + if visorBuildInfo.Version == "unknown" { + if match := strings.Contains("/tmp/", skywire); err == nil { + if match { + log.Info("executed with go run") + log.WithField("binary: ", skywire).Info() + } + } + //check for .git folder for versioning + if _, err := os.Stat(".git"); err == nil { + //attempt to version from git sources + if _, err = exec.LookPath("git"); err == nil { + if version, err := script.Exec(`git describe`).String(); err == nil { + visorBuildInfo.Version = strings.ReplaceAll(version, "\n", "") + } + if visorBuildInfo.Commit == "unknown" { + if nocommit, err = script.Exec(`git diff-index HEAD --`).String(); err == nil { + if commit, err := script.Exec(`git rev-list -1 HEAD`).String(); err == nil { + visorBuildInfo.Commit = strings.ReplaceAll(commit, "\n", "") + } + } + } + if fork, err = script.Exec(`git config --get remote.origin.url`).String(); err == nil { + fork = strings.ReplaceAll(fork, "ssh://", "") + fork = strings.ReplaceAll(fork, "git@", "") + fork = strings.ReplaceAll(fork, "https://", "") + fork = strings.ReplaceAll(fork, "http://", "") + fork = strings.ReplaceAll(fork, "github.com/", "") + fork = strings.ReplaceAll(fork, ":/", "") + fork = strings.ReplaceAll(fork, "\n", "") + nofork, err := regexp.MatchString("skycoin/skywire", fork) + if err != nil { + log.Error(err) + } else { + log.Info(nofork) + if !nofork { + fork = "" + } + } + } + if branch, err = script.Exec(`git rev-parse --abbrev-ref HEAD`).String(); err == nil { + branch = strings.ReplaceAll(branch, "\n", "") + if _, err = exec.LookPath("date"); err == nil { + if visorBuildInfo.Date == "unknown" { + if date, err := script.Exec(`date -u +%Y-%m-%dT%H:%M:%SZ`).String(); err == nil { + visorBuildInfo.Date = strings.ReplaceAll(date, "\n", "") + } + } + } + } + } + } + } + + log.WithField("version", visorBuildInfo.Version).Info() + if visorBuildInfo.Date != "unknown" && visorBuildInfo.Date != "" { + log.WithField("built on", visorBuildInfo.Date).Info() + } + if visorBuildInfo.Commit != "unknown" && visorBuildInfo.Commit != "" { + if (nocommit != "") && (nocommit != "\n") { + log.Info("with changes since commit") + log.WithField("commit", visorBuildInfo.Commit).Info() + } else { + log.WithField("against commit", visorBuildInfo.Commit).Info() + } + if fork != "" { + log.WithField("fork", fork).Info() + } + } + if branch != "unknown" && branch != "" { + log.WithField("branch", branch).Info() + } +} diff --git a/pkg/skyenv/values.go b/pkg/skyenv/values.go index 03f1e2ae08..214b49686b 100644 --- a/pkg/skyenv/values.go +++ b/pkg/skyenv/values.go @@ -20,12 +20,6 @@ const ( // DMSGHTTPName is the default dmsghttp config name DMSGHTTPName = "dmsghttp-config.json" - - // Skywirejson is the Hypervisor config - Skywirejson = "skywire.json" - - // Skywirevisorjson is the visor config - Skywirevisorjson = "skywire-visor.json" ) // Constants for skywire root directories. diff --git a/pkg/skyenv/values_darwin.go b/pkg/skyenv/values_darwin.go index 9c42b076ea..bf154125ac 100644 --- a/pkg/skyenv/values_darwin.go +++ b/pkg/skyenv/values_darwin.go @@ -8,6 +8,10 @@ const ( OS = "mac" // SkywirePath is the path to the installation folder. SkywirePath = "/Library/Application Support/Skywire" + // Ptext is the description to use for the -p flag for config gen and for the visor + Ptext = "use mac installation path: " + SkywirePath + // Configjson is the config name generated by the script included with the installation on mac + Configjson = "skywire.json" ) // PackageConfig contains installation paths (for mac) diff --git a/pkg/skyenv/values_linux.go b/pkg/skyenv/values_linux.go index b30a0febf8..ffb0651f04 100644 --- a/pkg/skyenv/values_linux.go +++ b/pkg/skyenv/values_linux.go @@ -8,6 +8,10 @@ const ( OS = "linux" // SkywirePath is the path to the installation folder for the linux packages. SkywirePath = "/opt/skywire" + // Ptext is the description to use for the -p flag for config gen and for the visor + Ptext = "use path for package: " + SkywirePath + // Configjson is the config name generated by the skywire-autocofig script in the linux packages + Configjson = "skywire.json" ) // PackageConfig contains installation paths (for linux) diff --git a/pkg/skyenv/values_windows.go b/pkg/skyenv/values_windows.go index 7329fa49c6..0884ec6a59 100644 --- a/pkg/skyenv/values_windows.go +++ b/pkg/skyenv/values_windows.go @@ -8,6 +8,10 @@ const ( OS = "win" // SkywirePath is the path to the installation folder for the .msi SkywirePath = "C:/Program Files/Skywire" + // Ptext is the description to use for the -p flag for config gen and for the visor + Ptext = "use .msi installation path: " + SkywirePath + // Configjson is the config name generated by the batch file included with the windows .msi + Configjson = "skywire.json" ) // PackageConfig contains installation paths (for windows) From 7b2026d62b545247e30de6e01f19b734f117b9cb Mon Sep 17 00:00:00 2001 From: Moses Narrow Date: Mon, 18 Apr 2022 22:37:44 -0500 Subject: [PATCH 2/5] change fail on incompatible version behavior per @jdknives specifications --- cmd/skywire-visor/commands/root.go | 1 - pkg/visor/visorconfig/parse.go | 11 ++++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/cmd/skywire-visor/commands/root.go b/cmd/skywire-visor/commands/root.go index 99fb54d36d..c09008fda8 100644 --- a/cmd/skywire-visor/commands/root.go +++ b/cmd/skywire-visor/commands/root.go @@ -385,7 +385,6 @@ func initConfig(mLog *logging.MasterLogger, confPath string) *visorconfig.V1 { / if err != nil { log.WithError(err).Fatal("Failed to read in config.") } - if !compat { updateConfig(mLog, conf) } diff --git a/pkg/visor/visorconfig/parse.go b/pkg/visor/visorconfig/parse.go index 8ca05211e7..7b0abc6349 100644 --- a/pkg/visor/visorconfig/parse.go +++ b/pkg/visor/visorconfig/parse.go @@ -28,7 +28,16 @@ func Parse(log *logging.Logger, r io.Reader, confPath string, visorBuildInfo *bu // we check if the version of the visor and config are the same if (conf.Version != "unknown") && (visorBuildInfo.Version != "unknown") { - compat = strings.Contains(strings.Split(visorBuildInfo.Version, "-")[0], strings.Split(conf.Version, "-")[0]) + cver := strings.Split(visorBuildInfo.Version, "-")[0] //v0.6.0 + cver0 := strings.Split(cver, ".")[0] //v0 + cver1 := strings.Split(cver, ".")[1] //6 + vver := strings.Split(conf.Version, "-")[0] //v0.6.0 + vver0 := strings.Split(vver, ".")[0] //v0 + vver1 := strings.Split(vver, ".")[1] //6 + compat = strings.Contains(vver0, cver0) + if compat { + compat = strings.Contains(vver1, cver1) + } } return conf, compat, nil } From 73a1b46a6bf49a46f96990979231088895d46721 Mon Sep 17 00:00:00 2001 From: Moses Narrow Date: Tue, 19 Apr 2022 16:03:18 -0500 Subject: [PATCH 3/5] fix config update --- .../commands/config/update/root.go | 47 ++++++++++++++++--- .../commands/config/update/update.go | 27 +++++++++++ pkg/visor/visorconfig/parse.go | 1 + 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/cmd/skywire-cli/commands/config/update/root.go b/cmd/skywire-cli/commands/config/update/root.go index 834648c38a..f4009e27fe 100644 --- a/cmd/skywire-cli/commands/config/update/root.go +++ b/cmd/skywire-cli/commands/config/update/root.go @@ -2,6 +2,8 @@ package update import ( "encoding/json" + "os" + "os/user" "path/filepath" "strings" @@ -38,13 +40,23 @@ var ( setPublicAutoconnect string serviceConfURL string minHops int - svcconf = strings.ReplaceAll(utilenv.ServiceConfAddr, "http://", "") //skyenv.DefaultServiceConfAddr - testconf = strings.ReplaceAll(utilenv.TestServiceConfAddr, "http://", "") //skyenv.DefaultServiceConfAddr + svcconf = strings.ReplaceAll(utilenv.ServiceConfAddr, "http://", "") + testconf = strings.ReplaceAll(utilenv.TestServiceConfAddr, "http://", "") + root bool + usr bool + hiddenflags []string ) var logger = logging.MustGetLogger("skywire-cli") func init() { + usrLvl, err := user.Current() + if err != nil { + panic(err) + } + if usrLvl.Username == "root" { + root = true + } RootCmd.Flags().SortFlags = false RootCmd.Flags().BoolVarP(&updateEndpoints, "endpoints", "a", false, "update server endpoints") RootCmd.Flags().StringVarP(&serviceConfURL, "url", "b", "", "service config URL: "+svcconf) @@ -53,7 +65,21 @@ func init() { RootCmd.Flags().IntVar(&minHops, "set-minhop", -1, "change min hops value") RootCmd.PersistentFlags().StringVarP(&input, "input", "i", "", "path of input config file.") RootCmd.PersistentFlags().StringVarP(&output, "output", "o", "", "config file to output") - RootCmd.PersistentFlags().BoolVarP(&pkg, "pkg", "p", false, "read from /opt/skywire/skywire.json") + if root { + if _, err := os.Stat(skyenv.SkywirePath + "/" + skyenv.Configjson); err == nil { + RootCmd.PersistentFlags().BoolVarP(&pkg, "pkg", "p", false, "update package config "+skyenv.SkywirePath+"/"+skyenv.Configjson) + hiddenflags = append(hiddenflags, "pkg") + } + } + if !root { + if _, err := os.Stat(skyenv.HomePath() + "/" + skyenv.ConfigName); err == nil { + RootCmd.PersistentFlags().BoolVarP(&usr, "user", "u", false, "update config at: $HOME/"+skyenv.ConfigName) + } + } + + for _, j := range hiddenflags { + RootCmd.Flags().MarkHidden(j) //nolint + } } // RootCmd contains commands that update the config @@ -65,6 +91,13 @@ var RootCmd = &cobra.Command{ if output == "" { output = skyenv.ConfigName } + if updateEndpoints && (serviceConfURL == "") { + if !testEnv { + serviceConfURL = svcconf + } else { + serviceConfURL = testconf + } + } var err error if output, err = filepath.Abs(output); err != nil { logger.WithError(err).Fatal("Invalid config output.") @@ -83,7 +116,11 @@ var RootCmd = &cobra.Command{ if ok != nil { mLog.WithError(ok).Fatal("Failed to parse config.") } - + cc, err := visorconfig.NewCommon(mLog, output, &conf.SK) + if err != nil { + mLog.WithError(ok).Fatal("Failed to regenerate config.") + } + conf.Common = cc if updateEndpoints { if testEnv { serviceConfURL = testconf @@ -107,7 +144,6 @@ var RootCmd = &cobra.Command{ Addr: services.UptimeTracker, //utilenv.DefaultUptimeTrackerAddr, } conf.StunServers = services.StunServers //utilenv.GetStunServers() - } switch setPublicAutoconnect { @@ -120,7 +156,6 @@ var RootCmd = &cobra.Command{ default: logger.Fatal("Unrecognized public autoconnect value: ", setPublicAutoconnect) } - if minHops >= 0 { conf.Routing.MinHops = uint16(minHops) } diff --git a/cmd/skywire-cli/commands/config/update/update.go b/cmd/skywire-cli/commands/config/update/update.go index 8146892922..bce100dde3 100644 --- a/cmd/skywire-cli/commands/config/update/update.go +++ b/cmd/skywire-cli/commands/config/update/update.go @@ -62,6 +62,11 @@ var hyperVisorUpdateCmd = &cobra.Command{ if ok != nil { mLog.WithError(ok).Fatal("Failed to parse config.") } + cc, err := visorconfig.NewCommon(mLog, output, &conf.SK) + if err != nil { + mLog.WithError(ok).Fatal("Failed to regenerate config.") + } + conf.Common = cc if addHypervisorPKs != "" { keys := strings.Split(addHypervisorPKs, ",") for _, key := range keys { @@ -95,6 +100,11 @@ var skySocksClientUpdateCmd = &cobra.Command{ if ok != nil { mLog.WithError(ok).Fatal("Failed to parse config.") } + cc, err := visorconfig.NewCommon(mLog, output, &conf.SK) + if err != nil { + mLog.WithError(ok).Fatal("Failed to regenerate config.") + } + conf.Common = cc if addSkysocksClientSrv != "" { keyParsed, err := coinCipher.PubKeyFromHex(strings.TrimSpace(addSkysocksClientSrv)) if err != nil { @@ -125,6 +135,12 @@ var skySocksServerUpdateCmd = &cobra.Command{ if ok != nil { mLog.WithError(ok).Fatal("Failed to parse config.") } + cc, err := visorconfig.NewCommon(mLog, output, &conf.SK) + if err != nil { + mLog.WithError(ok).Fatal("Failed to regenerate config.") + } + conf.Common = cc + if skysocksPasscode != "" { changeAppsConfig(conf, "skysocks", "--passcode", skysocksPasscode) } @@ -152,6 +168,11 @@ var vpnClientUpdateCmd = &cobra.Command{ if ok != nil { mLog.WithError(ok).Fatal("Failed to parse config.") } + cc, err := visorconfig.NewCommon(mLog, output, &conf.SK) + if err != nil { + mLog.WithError(ok).Fatal("Failed to regenerate config.") + } + conf.Common = cc switch setVPNClientKillswitch { case "true": changeAppsConfig(conf, "vpn-client", "--killswitch", setVPNClientKillswitch) @@ -197,6 +218,12 @@ var vpnServerUpdateCmd = &cobra.Command{ if ok != nil { mLog.WithError(ok).Fatal("Failed to parse config.") } + cc, err := visorconfig.NewCommon(mLog, output, &conf.SK) + if err != nil { + mLog.WithError(ok).Fatal("Failed to regenerate config.") + } + conf.Common = cc + if addVPNServerPasscode != "" { changeAppsConfig(conf, "vpn-server", "--passcode", addVPNServerPasscode) } diff --git a/pkg/visor/visorconfig/parse.go b/pkg/visor/visorconfig/parse.go index 7b0abc6349..2d83418758 100644 --- a/pkg/visor/visorconfig/parse.go +++ b/pkg/visor/visorconfig/parse.go @@ -39,5 +39,6 @@ func Parse(log *logging.Logger, r io.Reader, confPath string, visorBuildInfo *bu compat = strings.Contains(vver1, cver1) } } + conf.path = confPath return conf, compat, nil } From c6d733aba4c862f5e0247518beb8dfd7ef3c98d5 Mon Sep 17 00:00:00 2001 From: Moses Narrow Date: Tue, 19 Apr 2022 16:05:38 -0500 Subject: [PATCH 4/5] make format check --- cmd/skywire-cli/commands/config/update/root.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/skywire-cli/commands/config/update/root.go b/cmd/skywire-cli/commands/config/update/root.go index f4009e27fe..db83b4c6ea 100644 --- a/cmd/skywire-cli/commands/config/update/root.go +++ b/cmd/skywire-cli/commands/config/update/root.go @@ -42,9 +42,9 @@ var ( minHops int svcconf = strings.ReplaceAll(utilenv.ServiceConfAddr, "http://", "") testconf = strings.ReplaceAll(utilenv.TestServiceConfAddr, "http://", "") - root bool - usr bool - hiddenflags []string + root bool + usr bool + hiddenflags []string ) var logger = logging.MustGetLogger("skywire-cli") From 843529e59e3c27bb322d3125eaeabf525351b8c7 Mon Sep 17 00:00:00 2001 From: Moses Narrow Date: Wed, 20 Apr 2022 18:21:59 -0500 Subject: [PATCH 5/5] fix issue cited by @mrpalide --- .../commands/config/update/root.go | 84 +++++++++++----- .../commands/config/update/update.go | 97 +++---------------- 2 files changed, 77 insertions(+), 104 deletions(-) diff --git a/cmd/skywire-cli/commands/config/update/root.go b/cmd/skywire-cli/commands/config/update/root.go index db83b4c6ea..6664ec6a4d 100644 --- a/cmd/skywire-cli/commands/config/update/root.go +++ b/cmd/skywire-cli/commands/config/update/root.go @@ -40,6 +40,7 @@ var ( setPublicAutoconnect string serviceConfURL string minHops int + conf *visorconfig.V1 svcconf = strings.ReplaceAll(utilenv.ServiceConfAddr, "http://", "") testconf = strings.ReplaceAll(utilenv.TestServiceConfAddr, "http://", "") root bool @@ -64,6 +65,7 @@ func init() { RootCmd.Flags().StringVar(&setPublicAutoconnect, "public-autoconn", "", "change public autoconnect configuration") RootCmd.Flags().IntVar(&minHops, "set-minhop", -1, "change min hops value") RootCmd.PersistentFlags().StringVarP(&input, "input", "i", "", "path of input config file.") + hiddenflags = append(hiddenflags, "input") RootCmd.PersistentFlags().StringVarP(&output, "output", "o", "", "config file to output") if root { if _, err := os.Stat(skyenv.SkywirePath + "/" + skyenv.Configjson); err == nil { @@ -87,10 +89,6 @@ var RootCmd = &cobra.Command{ Use: "update", Short: "update a config file", PreRun: func(_ *cobra.Command, _ []string) { - //set default output filename - if output == "" { - output = skyenv.ConfigName - } if updateEndpoints && (serviceConfURL == "") { if !testEnv { serviceConfURL = svcconf @@ -98,33 +96,20 @@ var RootCmd = &cobra.Command{ serviceConfURL = testconf } } - var err error - if output, err = filepath.Abs(output); err != nil { - logger.WithError(err).Fatal("Invalid config output.") - } + setDefaults() + checkConfig() }, Run: func(cmd *cobra.Command, _ []string) { - mLog := logging.NewMasterLogger() - mLog.SetLevel(logrus.InfoLevel) if cmd.Flags().Changed("serviceConfURL") { updateEndpoints = true } - if input == "" { - input = output - } - conf, ok := visorconfig.ReadFile(input) - if ok != nil { - mLog.WithError(ok).Fatal("Failed to parse config.") - } - cc, err := visorconfig.NewCommon(mLog, output, &conf.SK) - if err != nil { - mLog.WithError(ok).Fatal("Failed to regenerate config.") - } - conf.Common = cc + conf = initUpdate() if updateEndpoints { if testEnv { serviceConfURL = testconf } + mLog := logging.NewMasterLogger() + mLog.SetLevel(logrus.InfoLevel) services := visorconfig.Fetch(mLog, serviceConfURL, stdout) conf.Dmsg = &dmsgc.DmsgConfig{ Discovery: services.DmsgDiscovery, //utilenv.DefaultDmsgDiscAddr, @@ -204,3 +189,58 @@ func saveConfig(conf *visorconfig.V1) { } logger.Infof("Updated file '%s' to: %s", output, j) } + +func initUpdate() (conf *visorconfig.V1) { + mLog := logging.NewMasterLogger() + mLog.SetLevel(logrus.InfoLevel) + if input == "" { + input = output + } + conf, ok := visorconfig.ReadFile(input) + if ok != nil { + mLog.WithError(ok).Fatal("Failed to parse config.") + } + cc, err := visorconfig.NewCommon(mLog, output, &conf.SK) + if err != nil { + mLog.WithError(ok).Fatal("Failed to regenerate config.") + } + conf.Common = cc + return conf +} + +func checkConfig() { + //set default output filename + if output == "" { + output = skyenv.ConfigName + } + var err error + if output, err = filepath.Abs(output); err != nil { + logger.WithError(err).Fatal("Invalid config output.") + } + if _, err := os.Stat(output); err != nil { + logger.WithError(err).Fatal("Invalid config output.") + } + if (input != output) && (input != "") { + if input, err = filepath.Abs(input); err != nil { + logger.WithError(err).Fatal("Invalid config input.") + } + if _, err := os.Stat(input); err != nil { + logger.WithError(err).Fatal("Invalid config input.") + } + } +} + +func setDefaults() { + if (input != "") && (output == "") { + output = input + } + if pkg { + output = skyenv.SkywirePath + "/" + skyenv.Configjson + input = skyenv.SkywirePath + "/" + skyenv.Configjson + } + if usr { + output = skyenv.HomePath() + "/" + skyenv.ConfigName + input = skyenv.HomePath() + "/" + skyenv.ConfigName + } + +} diff --git a/cmd/skywire-cli/commands/config/update/update.go b/cmd/skywire-cli/commands/config/update/update.go index bce100dde3..a307eddcba 100644 --- a/cmd/skywire-cli/commands/config/update/update.go +++ b/cmd/skywire-cli/commands/config/update/update.go @@ -1,16 +1,12 @@ package update import ( - "path/filepath" "strings" - "github.com/sirupsen/logrus" coinCipher "github.com/skycoin/skycoin/src/cipher" - "github.com/skycoin/skycoin/src/util/logging" "github.com/spf13/cobra" "github.com/skycoin/skywire-utilities/pkg/cipher" - "github.com/skycoin/skywire/pkg/visor/visorconfig" ) var pkg bool @@ -50,23 +46,11 @@ var hyperVisorUpdateCmd = &cobra.Command{ Use: "hv", Short: "update hypervisor config", PreRun: func(_ *cobra.Command, _ []string) { - var err error - if output, err = filepath.Abs(output); err != nil { - logger.WithError(err).Fatal("Invalid config output.") - } + setDefaults() + checkConfig() }, Run: func(_ *cobra.Command, _ []string) { - mLog := logging.NewMasterLogger() - mLog.SetLevel(logrus.InfoLevel) - conf, ok := visorconfig.ReadFile(input) - if ok != nil { - mLog.WithError(ok).Fatal("Failed to parse config.") - } - cc, err := visorconfig.NewCommon(mLog, output, &conf.SK) - if err != nil { - mLog.WithError(ok).Fatal("Failed to regenerate config.") - } - conf.Common = cc + conf = initUpdate() if addHypervisorPKs != "" { keys := strings.Split(addHypervisorPKs, ",") for _, key := range keys { @@ -88,23 +72,11 @@ var skySocksClientUpdateCmd = &cobra.Command{ Use: "sc", Short: "update skysocks-client config", PreRun: func(_ *cobra.Command, _ []string) { - var err error - if output, err = filepath.Abs(output); err != nil { - logger.WithError(err).Fatal("Invalid config output.") - } + setDefaults() + checkConfig() }, Run: func(_ *cobra.Command, _ []string) { - mLog := logging.NewMasterLogger() - mLog.SetLevel(logrus.InfoLevel) - conf, ok := visorconfig.ReadFile(input) - if ok != nil { - mLog.WithError(ok).Fatal("Failed to parse config.") - } - cc, err := visorconfig.NewCommon(mLog, output, &conf.SK) - if err != nil { - mLog.WithError(ok).Fatal("Failed to regenerate config.") - } - conf.Common = cc + conf = initUpdate() if addSkysocksClientSrv != "" { keyParsed, err := coinCipher.PubKeyFromHex(strings.TrimSpace(addSkysocksClientSrv)) if err != nil { @@ -123,24 +95,12 @@ var skySocksServerUpdateCmd = &cobra.Command{ Use: "ss", Short: "update skysocks-server config", PreRun: func(_ *cobra.Command, _ []string) { - var err error - if output, err = filepath.Abs(output); err != nil { - logger.WithError(err).Fatal("Invalid config output.") - } + setDefaults() + checkConfig() }, Run: func(_ *cobra.Command, _ []string) { - mLog := logging.NewMasterLogger() - mLog.SetLevel(logrus.InfoLevel) - conf, ok := visorconfig.ReadFile(input) - if ok != nil { - mLog.WithError(ok).Fatal("Failed to parse config.") - } - cc, err := visorconfig.NewCommon(mLog, output, &conf.SK) - if err != nil { - mLog.WithError(ok).Fatal("Failed to regenerate config.") - } - conf.Common = cc + conf = initUpdate() if skysocksPasscode != "" { changeAppsConfig(conf, "skysocks", "--passcode", skysocksPasscode) } @@ -155,24 +115,11 @@ var vpnClientUpdateCmd = &cobra.Command{ Use: "vpnc", Short: "update vpn-client config", PreRun: func(_ *cobra.Command, _ []string) { - var err error - if output, err = filepath.Abs(output); err != nil { - logger.WithError(err).Fatal("Invalid config output.") - } + setDefaults() + checkConfig() }, Run: func(_ *cobra.Command, _ []string) { - mLog := logging.NewMasterLogger() - mLog.SetLevel(logrus.InfoLevel) - - conf, ok := visorconfig.ReadFile(input) - if ok != nil { - mLog.WithError(ok).Fatal("Failed to parse config.") - } - cc, err := visorconfig.NewCommon(mLog, output, &conf.SK) - if err != nil { - mLog.WithError(ok).Fatal("Failed to regenerate config.") - } - conf.Common = cc + conf = initUpdate() switch setVPNClientKillswitch { case "true": changeAppsConfig(conf, "vpn-client", "--killswitch", setVPNClientKillswitch) @@ -205,25 +152,11 @@ var vpnServerUpdateCmd = &cobra.Command{ Use: "vpns", Short: "update vpn-server config", PreRun: func(_ *cobra.Command, _ []string) { - var err error - if output, err = filepath.Abs(output); err != nil { - logger.WithError(err).Fatal("Invalid config output.") - } + setDefaults() + checkConfig() }, Run: func(_ *cobra.Command, _ []string) { - mLog := logging.NewMasterLogger() - mLog.SetLevel(logrus.InfoLevel) - - conf, ok := visorconfig.ReadFile(input) - if ok != nil { - mLog.WithError(ok).Fatal("Failed to parse config.") - } - cc, err := visorconfig.NewCommon(mLog, output, &conf.SK) - if err != nil { - mLog.WithError(ok).Fatal("Failed to regenerate config.") - } - conf.Common = cc - + conf = initUpdate() if addVPNServerPasscode != "" { changeAppsConfig(conf, "vpn-server", "--passcode", addVPNServerPasscode) }