From f64e47ac8a668da54b064016b8c6bfa210cbbebb Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Thu, 7 Apr 2022 07:21:50 +0430 Subject: [PATCH 1/5] make exported ParseOptions fileds --- pkg/visor/visorconfig/parse.go | 12 ++++++------ pkg/visor/visorconfig/parse_test.go | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pkg/visor/visorconfig/parse.go b/pkg/visor/visorconfig/parse.go index 1ce793d90..832f52ce1 100644 --- a/pkg/visor/visorconfig/parse.go +++ b/pkg/visor/visorconfig/parse.go @@ -33,7 +33,7 @@ func Parse(log *logging.MasterLogger, raw []byte, options *ParseOptions) (*V1, e } func parseV1(cc *Common, raw []byte, options *ParseOptions) (*V1, error) { - conf := MakeBaseConfig(cc, options.testEnv, options.dmsgHTTP, options.services) + conf := MakeBaseConfig(cc, options.TestEnv, options.DmsgHTTP, options.Services) dec := json.NewDecoder(bytes.NewReader(raw)) if err := dec.Decode(&conf); err != nil { return nil, err @@ -44,7 +44,7 @@ func parseV1(cc *Common, raw []byte, options *ParseOptions) (*V1, error) { } conf = ensureAppDisc(conf) conf.Version = skyenv.Version() - return conf, conf.flush(conf, options.path) + return conf, conf.flush(conf, options.Path) } func ensureAppDisc(conf *V1) *V1 { @@ -56,8 +56,8 @@ func ensureAppDisc(conf *V1) *V1 { // ParseOptions is passed to Parse type ParseOptions struct { - path string - testEnv bool - dmsgHTTP bool - services *Services + Path string + TestEnv bool + DmsgHTTP bool + Services *Services } diff --git a/pkg/visor/visorconfig/parse_test.go b/pkg/visor/visorconfig/parse_test.go index c17edd155..dd2209bdc 100644 --- a/pkg/visor/visorconfig/parse_test.go +++ b/pkg/visor/visorconfig/parse_test.go @@ -41,10 +41,10 @@ func TestParse(t *testing.T) { // check: obtained config contains all base values. options := &ParseOptions{ - path: filename, - testEnv: false, - dmsgHTTP: true, - services: nil, + Path: filename, + TestEnv: false, + DmsgHTTP: true, + Services: nil, } conf, err := Parse(nil, raw, options) require.NoError(t, err) From b5d87abc89b4de12492c563ae341058851561e64 Mon Sep 17 00:00:00 2001 From: ersonp Date: Thu, 7 Apr 2022 12:01:03 +0530 Subject: [PATCH 2/5] Fix Parse This commit fixes the Parse file by removing the funcs parseV1, ensureAppDisc and the strut ParseOptions because they are not used anymore and we do not write the config to file on startup. Parse_test.go is deleted because it is not needed since we do not write to the config with Parse. Parse now only reads from the io reader provided to ght the conf in V1 and compares the version of the config to the version of the visor and sends back the result in the new variable compat. --- cmd/skywire-visor/commands/root.go | 100 +++++++++++++--------------- pkg/visor/visorconfig/parse.go | 59 +++++----------- pkg/visor/visorconfig/parse_test.go | 59 ---------------- 3 files changed, 65 insertions(+), 153 deletions(-) delete mode 100644 pkg/visor/visorconfig/parse_test.go diff --git a/cmd/skywire-visor/commands/root.go b/cmd/skywire-visor/commands/root.go index d4f9e5399..f8d24364d 100644 --- a/cmd/skywire-visor/commands/root.go +++ b/cmd/skywire-visor/commands/root.go @@ -416,68 +416,64 @@ func initConfig(mLog *logging.MasterLogger, args []string) *visorconfig.V1 { //n r = bytes.NewReader(f) } - conf, err := visorconfig.Reader(r) + conf, compat, err := visorconfig.Parse(log, r) if err != nil { log.WithError(err).Fatal("Failed to read in config.") } log.WithField("config version: ", conf.Version).Info() - if (conf.Version != "unknown") && (skyenv.BuildInfo.Version != "unknown") { - if compat, err := regexp.MatchString(strings.Split(skyenv.BuildInfo.Version, "-")[0], conf.Version); err == nil { - if !compat { - log.Error("config version does not match visor version") - log.WithField("skywire version: ", skyenv.BuildInfo.Version).Error() - var updstr string - if match, err := regexp.MatchString("/tmp/", skyenv.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 - } - } - var pkgenv bool - if pkgenv, err = regexp.MatchString("/opt/skywire/apps", conf.Launcher.BinPath); err == nil { - if pkgenv { - updstr = updstr + "p" - } + if !compat { + log.Error("config version does not match visor version") + log.WithField("skywire version: ", skyenv.BuildInfo.Version).Error() + var updstr string + if match, err := regexp.MatchString("/tmp/", skyenv.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" } - //there is no config *file* with stdin - if skyenv.ConfigName != visorconfig.StdinName { - if _, err = exec.LookPath("stat"); err == nil { - if owner, err := script.Exec(`stat -c '%U' ` + skyenv.ConfigName).String(); err == nil { - if (owner == "root") || (owner == "root\n") { - updstr = "sudo " + updstr - } - } - } - updstr = "\n " + updstr + "ro " + skyenv.ConfigName + "\n" - } else { - updstr = "\n " + updstr + "n" + " | go run cmd/skywire-visor/skywire-visor.go -n" - if launchBrowser { - updstr = updstr + "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 + } + } + var pkgenv bool + if pkgenv, err = regexp.MatchString("/opt/skywire/apps", conf.Launcher.BinPath); err == nil { + if pkgenv { + updstr = updstr + "p" + } + } + //there is no config *file* with stdin + if skyenv.ConfigName != visorconfig.StdinName { + if _, err = exec.LookPath("stat"); err == nil { + if owner, err := script.Exec(`stat -c '%U' ` + skyenv.ConfigName).String(); err == nil { + if (owner == "root") || (owner == "root\n") { + updstr = "sudo " + updstr } - 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") } + updstr = "\n " + updstr + "ro " + skyenv.ConfigName + "\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") } if hypervisorUI { config := hypervisorconfig.GenerateWorkDirConfig(false) diff --git a/pkg/visor/visorconfig/parse.go b/pkg/visor/visorconfig/parse.go index 832f52ce1..612f3680a 100644 --- a/pkg/visor/visorconfig/parse.go +++ b/pkg/visor/visorconfig/parse.go @@ -1,14 +1,13 @@ package visorconfig import ( - "bytes" - "encoding/json" "errors" - "fmt" + "io" + "regexp" + "strings" "github.com/skycoin/skycoin/src/util/logging" - utilenv "github.com/skycoin/skywire-utilities/pkg/skyenv" "github.com/skycoin/skywire/pkg/skyenv" ) @@ -18,46 +17,22 @@ var ( ) // Parse parses the visor config from a given reader. -// If the config file is not the most recent version, it is upgraded and written back to 'path'. -func Parse(log *logging.MasterLogger, raw []byte, options *ParseOptions) (*V1, error) { +// The config version is checked against the visor's version and if not the same we send back the +// error as well as compat(compatibility) as false. +func Parse(log *logging.Logger, r io.Reader) (conf *V1, compat bool, err error) { - cc, err := NewCommon(log, nil) + conf, err = Reader(r) if err != nil { - return nil, err + return nil, compat, err } - - if err := json.Unmarshal(raw, cc); err != nil { - return nil, fmt.Errorf("failed to obtain config version: %w", err) - } - return parseV1(cc, raw, options) -} - -func parseV1(cc *Common, raw []byte, options *ParseOptions) (*V1, error) { - conf := MakeBaseConfig(cc, options.TestEnv, options.DmsgHTTP, options.Services) - dec := json.NewDecoder(bytes.NewReader(raw)) - if err := dec.Decode(&conf); err != nil { - return nil, err - } - - if err := conf.ensureKeys(); err != nil { - return nil, fmt.Errorf("%v: %w", ErrInvalidSK, err) + log.WithField("config version: ", conf.Version).Info() + + // we check if the version of the visor and config are the same + if (conf.Version != "unknown") && (skyenv.BuildInfo.Version != "unknown") { + compat, err = regexp.MatchString(strings.Split(skyenv.BuildInfo.Version, "-")[0], conf.Version) + if err != nil { + return nil, compat, err + } } - conf = ensureAppDisc(conf) - conf.Version = skyenv.Version() - return conf, conf.flush(conf, options.Path) -} - -func ensureAppDisc(conf *V1) *V1 { - if conf.Launcher.ServiceDisc == "" { - conf.Launcher.ServiceDisc = utilenv.ServiceDiscAddr - } - return conf -} - -// ParseOptions is passed to Parse -type ParseOptions struct { - Path string - TestEnv bool - DmsgHTTP bool - Services *Services + return conf, compat, nil } diff --git a/pkg/visor/visorconfig/parse_test.go b/pkg/visor/visorconfig/parse_test.go deleted file mode 100644 index dd2209bdc..000000000 --- a/pkg/visor/visorconfig/parse_test.go +++ /dev/null @@ -1,59 +0,0 @@ -package visorconfig - -import ( - "encoding/json" - "fmt" - "io/ioutil" - "os" - "testing" - - "github.com/stretchr/testify/require" - - "github.com/skycoin/skywire-utilities/pkg/cipher" - "github.com/skycoin/skywire/pkg/skyenv" -) - -func TestParse(t *testing.T) { - jsonString := func(v interface{}) string { - j, err := json.Marshal(v) - require.NoError(t, err) - return string(j) - } - - // Given a config file with only the 'version' and 'sk' fields defined, - // 'visorconfig.Parse' SHOULD fill the file with base values - as defined in 'visorconf.BaseConfig'. - t.Run("parseV1_fill_base", func(t *testing.T) { - // init - f, err := ioutil.TempFile(os.TempDir(), "*.json") - require.NoError(t, err) - - filename := f.Name() - defer func() { require.NoError(t, os.Remove(filename)) }() - - _, sk := cipher.GenerateKeyPair() - version := skyenv.Version() - raw := []byte(fmt.Sprintf(`{"version":"%s","sk":"%s"}`, version, sk.String())) - n, err := f.Write(raw) - require.NoError(t, err) - require.Len(t, raw, n) - require.NoError(t, f.Close()) - - // check: obtained config contains all base values. - - options := &ParseOptions{ - Path: filename, - TestEnv: false, - DmsgHTTP: true, - Services: nil, - } - conf, err := Parse(nil, raw, options) - require.NoError(t, err) - conf.Common.SK = sk - require.JSONEq(t, jsonString(MakeBaseConfig(conf.Common, false, true, services)), jsonString(conf)) - - // check: saved config contains all base values. - raw2, err := ioutil.ReadFile(filename) //nolint:gosec - require.NoError(t, err) - require.JSONEq(t, jsonString(MakeBaseConfig(conf.Common, false, true, services)), string(raw2)) - }) -} From e4ab76e6b6bd19e27603e8b3554c447af3c62383 Mon Sep 17 00:00:00 2001 From: ersonp Date: Thu, 7 Apr 2022 12:02:29 +0530 Subject: [PATCH 3/5] Remove args This commit removes the parameter from func runApp, runVisor and initConfig as it is no longer used. --- cmd/skywire-visor/commands/nosystray.go | 4 ++-- cmd/skywire-visor/commands/root.go | 11 +++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/cmd/skywire-visor/commands/nosystray.go b/cmd/skywire-visor/commands/nosystray.go index caf412b01..f2c0e3be1 100644 --- a/cmd/skywire-visor/commands/nosystray.go +++ b/cmd/skywire-visor/commands/nosystray.go @@ -13,8 +13,8 @@ func extraFlags() { } -func runApp(args []string) { - runVisor(args) +func runApp() { + runVisor() } // setStopFunction sets the stop function diff --git a/cmd/skywire-visor/commands/root.go b/cmd/skywire-visor/commands/root.go index f8d24364d..161185014 100644 --- a/cmd/skywire-visor/commands/root.go +++ b/cmd/skywire-visor/commands/root.go @@ -261,13 +261,13 @@ var rootCmd = &cobra.Command{ log.WithField("branch: ", branch).Info() } }, - Run: func(_ *cobra.Command, args []string) { - runApp(args) + Run: func(_ *cobra.Command, _ []string) { + runApp() }, Version: buildinfo.Version(), } -func runVisor(args []string) { +func runVisor() { var ok bool log := initLogger(tag, syslogAddr) store, hook := logstore.MakeStore(runtimeLogMaxEntries) @@ -276,7 +276,7 @@ func runVisor(args []string) { stopPProf := initPProf(log, tag, pprofMode, pprofAddr) defer stopPProf() - conf := initConfig(log, args) + conf := initConfig(log) if disableHypervisorPKs { conf.Hypervisors = []cipher.PubKey{} @@ -395,7 +395,7 @@ func initPProf(log *logging.MasterLogger, tag string, profMode string, profAddr return stop } -func initConfig(mLog *logging.MasterLogger, args []string) *visorconfig.V1 { //nolint +func initConfig(mLog *logging.MasterLogger) *visorconfig.V1 { //nolint log := mLog.PackageLogger("visor:config") var r io.Reader @@ -420,7 +420,6 @@ func initConfig(mLog *logging.MasterLogger, args []string) *visorconfig.V1 { //n if err != nil { log.WithError(err).Fatal("Failed to read in config.") } - log.WithField("config version: ", conf.Version).Info() if !compat { log.Error("config version does not match visor version") From 8ea44904dfb258c202004e1739d1aaf77f497268 Mon Sep 17 00:00:00 2001 From: ersonp Date: Thu, 7 Apr 2022 17:18:06 +0530 Subject: [PATCH 4/5] Fix parse --- pkg/visor/visorconfig/parse.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pkg/visor/visorconfig/parse.go b/pkg/visor/visorconfig/parse.go index 612f3680a..3f5e8eefa 100644 --- a/pkg/visor/visorconfig/parse.go +++ b/pkg/visor/visorconfig/parse.go @@ -3,7 +3,6 @@ package visorconfig import ( "errors" "io" - "regexp" "strings" "github.com/skycoin/skycoin/src/util/logging" @@ -29,10 +28,7 @@ func Parse(log *logging.Logger, r io.Reader) (conf *V1, compat bool, err error) // we check if the version of the visor and config are the same if (conf.Version != "unknown") && (skyenv.BuildInfo.Version != "unknown") { - compat, err = regexp.MatchString(strings.Split(skyenv.BuildInfo.Version, "-")[0], conf.Version) - if err != nil { - return nil, compat, err - } + compat = strings.Contains(strings.Split(skyenv.BuildInfo.Version, "-")[0], strings.Split(conf.Version, "-")[0]) } return conf, compat, nil } From 755a3edcf507c2248fc4c0bfc3da2d834b92c9c9 Mon Sep 17 00:00:00 2001 From: ersonp Date: Thu, 7 Apr 2022 17:26:58 +0530 Subject: [PATCH 5/5] Use strings.Contains instead of regexp.MatchString --- cmd/skywire-visor/commands/root.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/cmd/skywire-visor/commands/root.go b/cmd/skywire-visor/commands/root.go index 161185014..ef28848b7 100644 --- a/cmd/skywire-visor/commands/root.go +++ b/cmd/skywire-visor/commands/root.go @@ -13,7 +13,6 @@ import ( "os" "os/exec" "os/user" - "regexp" "strings" "sync" "time" @@ -202,7 +201,7 @@ var rootCmd = &cobra.Command{ //retrieve build info skyenv.BuildInfo = buildinfo.Get() if skyenv.BuildInfo.Version == "unknown" { - if match, err := regexp.MatchString("/tmp/", skyenv.Skywire); err == nil { + if match := strings.Contains("/tmp/", skyenv.Skywire); err == nil { if match { log.Info("executed with go run") log.WithField("binary: ", skyenv.Skywire).Info() @@ -227,7 +226,7 @@ var rootCmd = &cobra.Command{ fork = strings.ReplaceAll(fork, "github.com/", "") fork = strings.ReplaceAll(fork, ":/", "") fork = strings.ReplaceAll(fork, "\n", "") - if nofork, err := regexp.MatchString(fork, "skycoin/skywire"); err == nil { + if nofork := strings.Contains(fork, "skycoin/skywire"); err == nil { if !nofork { fork = "" } @@ -425,7 +424,7 @@ func initConfig(mLog *logging.MasterLogger) *visorconfig.V1 { //nolint log.Error("config version does not match visor version") log.WithField("skywire version: ", skyenv.BuildInfo.Version).Error() var updstr string - if match, err := regexp.MatchString("/tmp/", skyenv.Skywire); err == nil { + if match := strings.Contains("/tmp/", skyenv.Skywire); err == nil { log.Info("match:", match) if match { if _, err := os.Stat("cmd/skywire-cli/skywire-cli.go"); err == nil { @@ -447,8 +446,8 @@ func initConfig(mLog *logging.MasterLogger) *visorconfig.V1 { //nolint break } } - var pkgenv bool - if pkgenv, err = regexp.MatchString("/opt/skywire/apps", conf.Launcher.BinPath); err == nil { + + if pkgenv := strings.Contains("/opt/skywire/apps", conf.Launcher.BinPath); err == nil { if pkgenv { updstr = updstr + "p" }