diff --git a/cmd/skywire-visor/commands/nosystray.go b/cmd/skywire-visor/commands/nosystray.go index caf412b013..f2c0e3be1e 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 d4f9e5399f..ef28848b7d 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 = "" } @@ -261,13 +260,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 +275,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 +394,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 @@ -416,68 +415,63 @@ 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 - } + if !compat { + log.Error("config version does not match visor version") + log.WithField("skywire version: ", skyenv.BuildInfo.Version).Error() + var updstr string + 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 { + updstr = "go run cmd/skywire-cli/skywire-cli.go config gen -b" } - 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 = "\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 + } + } + + 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 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 1ce793d907..3f5e8eefad 100644 --- a/pkg/visor/visorconfig/parse.go +++ b/pkg/visor/visorconfig/parse.go @@ -1,14 +1,12 @@ package visorconfig import ( - "bytes" - "encoding/json" "errors" - "fmt" + "io" + "strings" "github.com/skycoin/skycoin/src/util/logging" - utilenv "github.com/skycoin/skywire-utilities/pkg/skyenv" "github.com/skycoin/skywire/pkg/skyenv" ) @@ -18,46 +16,19 @@ 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 } + log.WithField("config version: ", conf.Version).Info() - if err := json.Unmarshal(raw, cc); err != nil { - return nil, fmt.Errorf("failed to obtain config version: %w", err) + // we check if the version of the visor and config are the same + if (conf.Version != "unknown") && (skyenv.BuildInfo.Version != "unknown") { + compat = strings.Contains(strings.Split(skyenv.BuildInfo.Version, "-")[0], strings.Split(conf.Version, "-")[0]) } - 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) - } - 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 c17edd1557..0000000000 --- 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)) - }) -}