Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix version check #1157

Merged
merged 9 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions cmd/skywire-cli/commands/config/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
regen bool
retainHypervisors bool
testEnv bool
ptext string
pkgEnv bool
usrEnv bool
hypervisor bool
Expand Down Expand Up @@ -82,7 +83,16 @@ 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)
if skyenv.OS == "win" {
ptext = "use .msi installation path: "
}
if skyenv.OS == "linux" {
ptext = "use path for package: "
}
if skyenv.OS == "mac" {
ptext = "use mac installation path: "
}
genConfigCmd.Flags().BoolVarP(&pkgEnv, "pkg", "p", false, ptext+skyenv.SkywirePath)
homepath := skyenv.HomePath()
if homepath != "" {
genConfigCmd.Flags().BoolVarP(&usrEnv, "user", "u", false, "use paths for user space: "+homepath)
Expand Down Expand Up @@ -204,15 +214,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 {
Expand Down
226 changes: 49 additions & 177 deletions cmd/skywire-visor/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"os/exec"
"os/user"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -62,56 +61,49 @@ var (
hiddenflags []string
all bool
pkg bool
pkg1 bool
usr bool
// skywire is the path to the running visor binary
skywire string
// workDir is the working directory where skywire-visor was executed
workDir string // nolint:unused
// root indicates process is run with root permissions
root bool // nolint:unused
// visorBuildInfo holds information about the build
visorBuildInfo *buildinfo.Info
)

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")
Expand Down Expand Up @@ -173,22 +165,18 @@ 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")

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
Expand All @@ -207,88 +195,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) {
Expand All @@ -309,7 +218,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", "")
Expand Down Expand Up @@ -355,7 +263,7 @@ func runVisor(conf *visorconfig.V1) {
vis.SetLogstore(store)

if launchBrowser {
runBrowser(conf, log)
runBrowser(log, conf)
}

ctx, cancel := cmdutil.SignalContext(context.Background(), log)
Expand Down Expand Up @@ -396,19 +304,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()) {
Expand Down Expand Up @@ -471,59 +377,8 @@ func initConfig(mLog *logging.MasterLogger, confPath string) *visorconfig.V1 { /
if err != nil {
log.WithError(err).Fatal("Failed to read in config.")
}

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be one line of just using log.Fatalf() for instance. Seems overkill to me to assign a new package logger and then log error and fatal separately.

Copy link
Collaborator Author

@0pcom 0pcom Apr 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, separate func was vestigial - fixed in cd88cfe
image

}
if hypervisorUI {
config := hypervisorconfig.GenerateWorkDirConfig(false)
Expand All @@ -536,7 +391,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
Expand All @@ -553,7 +410,7 @@ func runBrowser(conf *visorconfig.V1, log *logging.MasterLogger) {
}
}
go func() {
if !checkHvIsRunning(addr, 5) {
if !isHvRunning(addr, 5) {
log.Error("Cannot open hypervisor in browser: status check failed")
return
}
Expand All @@ -563,7 +420,7 @@ func runBrowser(conf *visorconfig.V1, log *logging.MasterLogger) {
}()
}

func checkHvIsRunning(addr string, retries int) bool {
func isHvRunning(addr string, retries int) bool {
url := addr + "/api/ping"
for i := 0; i < retries; i++ {
time.Sleep(500 * time.Millisecond)
Expand All @@ -581,3 +438,18 @@ func checkHvIsRunning(addr string, retries int) bool {
}
jdknives marked this conversation as resolved.
Show resolved Hide resolved
return false
}

func updateConfig(mLog *logging.MasterLogger) {
log := mLog.PackageLogger("visor:update-config")
log.Error("config version incompatible - please update your config")
log.Fatal("failed to start skywire")

}

func logBuildInfo(mLog *logging.MasterLogger) {
log := mLog.PackageLogger("buildinfo")
visorBuildInfo = buildinfo.Get()
if visorBuildInfo.Version != "unknown" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put these logs on one line?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done with 0e6b703

log.WithField(" version", visorBuildInfo.Version).WithField("built on", visorBuildInfo.Date).WithField("commit", visorBuildInfo.Commit).Info()
}
}
Loading