Skip to content

Commit

Permalink
Merge pull request #380 from nkryuchkov/feature/updater-improvements
Browse files Browse the repository at this point in the history
Updating improvements
  • Loading branch information
nkryuchkov authored Jun 11, 2020
2 parents 45f4526 + ab1bcee commit 48a25b9
Show file tree
Hide file tree
Showing 240 changed files with 49,385 additions and 258 deletions.
53 changes: 47 additions & 6 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# This is an example goreleaser.yaml file with some sane defaults.
# Make sure to check the documentation at http://goreleaser.com

# TODO: uncomment commented targets

before:
hooks:
- go mod tidy
Expand All @@ -22,7 +25,7 @@ builds:
env:
- CGO_ENABLED=0
main: ./cmd/skywire-visor/
ldflags: -s -w -X github.com/SkycoinProject/skywire-mainnet/pkg/util/buildinfo.version={{.Version}} -X github.com/SkycoinProject/skywire-mainnet/pkg/util/buildinfo.commit={{.ShortCommit}} -X github.com/SkycoinProject/skywire-mainnet/pkg/util/buildinfo.date={{.Date}}
ldflags: -s -w -X github.com/SkycoinProject/dmsg/buildinfo.version={{.Version}} -X github.com/SkycoinProject/dmsg/buildinfo.commit={{.ShortCommit}} -X github.com/SkycoinProject/dmsg/buildinfo.date={{.Date}}
- id: skywire-cli
binary: skywire-cli
goos:
Expand All @@ -41,7 +44,7 @@ builds:
env:
- CGO_ENABLED=0
main: ./cmd/skywire-cli/
ldflags: -s -w -X github.com/SkycoinProject/skywire-mainnet/pkg/util/buildinfo.version={{.Version}} -X github.com/SkycoinProject/skywire-mainnet/pkg/util/buildinfo.commit={{.ShortCommit}} -X github.com/SkycoinProject/skywire-mainnet/pkg/util/buildinfo.date={{.Date}}
ldflags: -s -w -X github.com/SkycoinProject/dmsg/buildinfo.version={{.Version}} -X github.com/SkycoinProject/dmsg/buildinfo.commit={{.ShortCommit}} -X github.com/SkycoinProject/dmsg/buildinfo.date={{.Date}}
- id: hypervisor
binary: hypervisor
goos:
Expand All @@ -60,7 +63,7 @@ builds:
env:
- CGO_ENABLED=0
main: ./cmd/hypervisor/
ldflags: -s -w -X github.com/SkycoinProject/skywire-mainnet/pkg/util/buildinfo.version={{.Version}} -X github.com/SkycoinProject/skywire-mainnet/pkg/util/buildinfo.commit={{.ShortCommit}} -X github.com/SkycoinProject/skywire-mainnet/pkg/util/buildinfo.date={{.Date}}
ldflags: -s -w -X github.com/SkycoinProject/dmsg/buildinfo.version={{.Version}} -X github.com/SkycoinProject/dmsg/buildinfo.commit={{.ShortCommit}} -X github.com/SkycoinProject/dmsg/buildinfo.date={{.Date}}
- id: skychat
binary: apps/skychat
goos:
Expand All @@ -79,7 +82,7 @@ builds:
env:
- CGO_ENABLED=0
main: ./cmd/apps/skychat/
ldflags: -s -w -X github.com/SkycoinProject/skywire-mainnet/pkg/util/buildinfo.version={{.Version}} -X github.com/SkycoinProject/skywire-mainnet/pkg/util/buildinfo.commit={{.ShortCommit}} -X github.com/SkycoinProject/skywire-mainnet/pkg/util/buildinfo.date={{.Date}}
ldflags: -s -w -X github.com/SkycoinProject/dmsg/buildinfo.version={{.Version}} -X github.com/SkycoinProject/dmsg/buildinfo.commit={{.ShortCommit}} -X github.com/SkycoinProject/dmsg/buildinfo.date={{.Date}}
- id: skysocks
binary: apps/skysocks
goos:
Expand All @@ -98,7 +101,7 @@ builds:
env:
- CGO_ENABLED=0
main: ./cmd/apps/skysocks/
ldflags: -s -w -X github.com/SkycoinProject/skywire-mainnet/pkg/util/buildinfo.version={{.Version}} -X github.com/SkycoinProject/skywire-mainnet/pkg/util/buildinfo.commit={{.ShortCommit}} -X github.com/SkycoinProject/skywire-mainnet/pkg/util/buildinfo.date={{.Date}}
ldflags: -s -w -X github.com/SkycoinProject/dmsg/buildinfo.version={{.Version}} -X github.com/SkycoinProject/dmsg/buildinfo.commit={{.ShortCommit}} -X github.com/SkycoinProject/dmsg/buildinfo.date={{.Date}}
- id: skysocks-client
binary: apps/skysocks-client
goos:
Expand All @@ -117,7 +120,45 @@ builds:
env:
- CGO_ENABLED=0
main: ./cmd/apps/skysocks-client/
ldflags: -s -w -X github.com/SkycoinProject/skywire-mainnet/pkg/util/buildinfo.version={{.Version}} -X github.com/SkycoinProject/skywire-mainnet/pkg/util/buildinfo.commit={{.ShortCommit}} -X github.com/SkycoinProject/skywire-mainnet/pkg/util/buildinfo.date={{.Date}}
ldflags: -s -w -X github.com/SkycoinProject/dmsg/buildinfo.version={{.Version}} -X github.com/SkycoinProject/dmsg/buildinfo.commit={{.ShortCommit}} -X github.com/SkycoinProject/dmsg/buildinfo.date={{.Date}}
- id: vpn-server
binary: apps/vpn-server
goos:
- linux
- darwin
goarch:
- amd64
- 386
- arm64
- arm
goarm:
- 7
ignore:
- goos: darwin
goarch: 386
env:
- CGO_ENABLED=0
main: ./cmd/apps/vpn-server/
ldflags: -s -w -X github.com/SkycoinProject/dmsg/buildinfo.version={{.Version}} -X github.com/SkycoinProject/dmsg/buildinfo.commit={{.ShortCommit}} -X github.com/SkycoinProject/dmsg/buildinfo.date={{.Date}}
- id: vpn-client
binary: apps/vpn-client
goos:
- linux
- darwin
goarch:
- amd64
- 386
- arm64
- arm
goarm:
- 7
ignore:
- goos: darwin
goarch: 386
env:
- CGO_ENABLED=0
main: ./cmd/apps/vpn-client/
ldflags: -s -w -X github.com/SkycoinProject/dmsg/buildinfo.version={{.Version}} -X github.com/SkycoinProject/dmsg/buildinfo.commit={{.ShortCommit}} -X github.com/SkycoinProject/dmsg/buildinfo.date={{.Date}}
archives:
- format: tar.gz
wrap_in_directory: false
Expand Down
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.DEFAULT_GOAL := help
.PHONY : check lint install-linters dep test
.PHONY : build clean install format bin

.PHONY : check lint lint-extra install-linters dep test
.PHONY : build clean install format bin
.PHONY : host-apps bin
.PHONY : run stop config
.PHONY : docker-image docker-clean docker-network
Expand Down Expand Up @@ -82,10 +83,13 @@ rerun: stop
perl -pi -e 's/localhost//g' ./skywire.json
./skywire-visor skywire.json


lint: ## Run linters. Use make install-linters first
${OPTS} golangci-lint run -c .golangci.yml ./...
# The govet version in golangci-lint is out of date and has spurious warnings, run it separately

lint-extra: ## Run linters with extra checks.
${OPTS} golangci-lint run --no-config --enable-all ./...
# The govet version in golangci-lint is out of date and has spurious warnings, run it separately
${OPTS} go vet -all ./...

vendorcheck: ## Run vendorcheck
Expand Down
1 change: 0 additions & 1 deletion cmd/apps/skychat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ func messageHandler(w http.ResponseWriter, req *http.Request) {

return
}

}

func sseHandler(w http.ResponseWriter, req *http.Request) {
Expand Down
22 changes: 21 additions & 1 deletion cmd/hypervisor/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"os"
"time"

"github.com/SkycoinProject/dmsg"
"github.com/SkycoinProject/dmsg/buildinfo"
Expand All @@ -14,6 +15,7 @@ import (

_ "github.com/SkycoinProject/skywire-mainnet/cmd/hypervisor/statik" // embedded static files
"github.com/SkycoinProject/skywire-mainnet/pkg/hypervisor"
"github.com/SkycoinProject/skywire-mainnet/pkg/restart"
"github.com/SkycoinProject/skywire-mainnet/pkg/util/pathutil"
)

Expand All @@ -29,6 +31,7 @@ var (
mockVisors int
mockMaxTps int
mockMaxRoutes int
delay string
)

// nolint:gochecknoinits
Expand All @@ -39,6 +42,7 @@ func init() {
rootCmd.Flags().IntVar(&mockVisors, "mock-visors", 5, "number of visors to have in mock mode")
rootCmd.Flags().IntVar(&mockMaxTps, "mock-max-tps", 10, "max number of transports per mock visor")
rootCmd.Flags().IntVar(&mockMaxRoutes, "mock-max-routes", 30, "max number of routes per visor")
rootCmd.Flags().StringVar(&delay, "delay", "0ns", "start delay (deprecated)") // deprecated
}

// nolint:gochecknoglobals
Expand All @@ -50,6 +54,17 @@ var rootCmd = &cobra.Command{
log.Printf("Failed to output build info: %v", err)
}

delayDuration, err := time.ParseDuration(delay)
if err != nil {
log.WithError(err).Error("Failed to parse delay duration.")
delayDuration = time.Duration(0)
}

time.Sleep(delayDuration)

restartCtx := restart.CaptureContext()
restartCtx.RegisterLogger(log)

conf := prepareConfig(args)

assets, err := fs.New()
Expand All @@ -58,10 +73,11 @@ var rootCmd = &cobra.Command{
}

// Prepare hypervisor.
hv, err := hypervisor.New(assets, conf)
hv, err := hypervisor.New(assets, conf, restartCtx)
if err != nil {
log.Fatalln("Failed to start hypervisor:", err)
}

if mock {
prepareMockData(hv)
} else {
Expand All @@ -72,15 +88,19 @@ var rootCmd = &cobra.Command{
log := log.
WithField("addr", conf.HTTPAddr).
WithField("tls", conf.EnableTLS)

log.Info("Serving hypervisor...")

if conf.EnableTLS {
err = http.ListenAndServeTLS(conf.HTTPAddr, conf.TLSCertFile, conf.TLSKeyFile, hv)
} else {
err = http.ListenAndServe(conf.HTTPAddr, hv)
}

if err != nil {
log.WithError(err).Fatal("Hypervisor exited with error.")
}

log.Info("Good bye!")
},
}
Expand Down
78 changes: 73 additions & 5 deletions cmd/skywire-visor/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"net/http"
_ "net/http/pprof" // nolint:gosec // https://golang.org/doc/diagnostics.html#profiling
"os"
"os/exec"
"strings"
"syscall"
"time"

"github.com/SkycoinProject/dmsg/buildinfo"
"github.com/SkycoinProject/dmsg/cmdutil"
Expand All @@ -23,37 +27,87 @@ import (

var restartCtx = restart.CaptureContext()

const (
configEnv = "SW_VISOR_CONFIG"
defaultConfigName = "skywire-config.json"
visorPathSystemd = "/usr/local/bin/skywire-visor"
workDirSystemd = "/"
)

var (
tag string
syslogAddr string
pprofMode string
pprofAddr string
confPath string
delay string
)

func init() {
rootCmd.Flags().StringVar(&tag, "tag", "skywire", "logging tag")
rootCmd.Flags().StringVar(&syslogAddr, "syslog", "", "syslog server address. E.g. localhost:514")
rootCmd.Flags().StringVarP(&pprofMode, "pprofmode", "p", "", "pprof profiling mode. Valid values: cpu, mem, mutex, block, trace, http")
rootCmd.Flags().StringVar(&pprofAddr, "pprofaddr", "localhost:6060", "pprof http port if mode is 'http'")
rootCmd.Flags().StringVarP(&confPath, "config", "c", "skywire-config.json", "config file location. If the value is 'STDIN', config file will be read from stdin.")
rootCmd.Flags().StringVarP(&confPath, "config", "c", "", "config file location. If the value is 'STDIN', config file will be read from stdin.")
rootCmd.Flags().StringVar(&delay, "delay", "0ns", "start delay (deprecated)") // deprecated
}

var rootCmd = &cobra.Command{
Use: "skywire-visor",
Short: "Skywire visor",
Run: func(_ *cobra.Command, args []string) {

log := initLogger(tag, syslogAddr)

delayDuration, err := time.ParseDuration(delay)
if err != nil {
log.WithError(err).Error("Failed to parse delay duration.")
delayDuration = time.Duration(0)
}

log.WithField("delay", delayDuration).
WithField("systemd", restartCtx.Systemd()).
WithField("parent_systemd", restartCtx.ParentSystemd()).
Debugf("Process info")

// Versions v0.2.3 and below return 0 exit-code after update and do not trigger systemd to restart a process
// and therefore do not support restart via systemd.
// If --delay flag is passed, version is v0.2.3 or below.
// Systemd has PID 1. If PPID is not 1 and PPID of parent process is 1, then
// this process is a child process that is run after updating by a skywire-visor that is run by systemd.
if delayDuration != 0 && !restartCtx.Systemd() && restartCtx.ParentSystemd() {
// As skywire-visor checks if new process is run successfully in `restart.DefaultCheckDelay` after update,
// new process should be alive after `restart.DefaultCheckDelay`.
time.Sleep(restart.DefaultCheckDelay)

// When a parent process exits, systemd kills child processes as well,
// so a child process can ask systemd to restart service between after restart.DefaultCheckDelay
// but before (restart.DefaultCheckDelay + restart.extraWaitingTime),
// because after that time a parent process would exit and then systemd would kill its children.
// In this case, systemd would kill both parent and child processes,
// then restart service using an updated binary.
cmd := exec.Command("systemctl", "restart", "skywire-visor") // nolint:gosec
if err := cmd.Run(); err != nil {
log.WithError(err).Errorf("Failed to restart skywire-visor service")
} else {
log.WithError(err).Infof("Restarted skywire-visor service")
}

// Detach child from parent. TODO: This may be unnecessary.
if _, err := syscall.Setsid(); err != nil {
log.WithError(err).Errorf("Failed to call setsid()")
}
}

time.Sleep(delayDuration)

if _, err := buildinfo.Get().WriteTo(log.Out); err != nil {
log.WithError(err).Error("Failed to output build info.")
}

stopPProf := initPProf(log, tag, pprofMode, pprofAddr)
defer stopPProf()

conf := initConfig(log, confPath)
conf := initConfig(log, args, confPath)

v, ok := visor.NewVisor(conf, restartCtx)
if !ok {
Expand All @@ -70,7 +124,7 @@ var rootCmd = &cobra.Command{
log.WithError(err).Error("Visor closed with error.")
}
},
Version: buildinfo.Get().Version,
Version: buildinfo.Version(),
}

// Execute executes root CLI command.
Expand Down Expand Up @@ -131,7 +185,7 @@ func initPProf(log *logging.MasterLogger, tag string, profMode string, profAddr
return stop
}

func initConfig(mLog *logging.MasterLogger, confPath string) *visorconfig.V1 {
func initConfig(mLog *logging.MasterLogger, args []string, confPath string) *visorconfig.V1 {
log := mLog.PackageLogger("visor:config")

var r io.Reader
Expand All @@ -140,6 +194,20 @@ func initConfig(mLog *logging.MasterLogger, confPath string) *visorconfig.V1 {
case visorconfig.StdinName:
log.Info("Reading config from STDIN.")
r = os.Stdin
case "":
// TODO: More robust solution.
for _, arg := range args {
if strings.HasSuffix(arg, ".json") {
confPath = arg
break
}
}

if confPath == "" {
confPath = defaultConfigName
}

fallthrough
default:
log.WithField("filepath", confPath).Info("Reading config from file.")
f, err := os.Open(confPath) //nolint:gosec
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ require (
github.com/SkycoinProject/dmsg v0.2.2
github.com/SkycoinProject/skycoin v0.27.0
github.com/SkycoinProject/yamux v0.0.0-20191213015001-a36efeefbf6a
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
github.com/go-chi/chi v4.0.2+incompatible
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-querystring v1.0.0 // indirect
github.com/google/uuid v1.1.1
github.com/gorilla/securecookie v1.1.1
github.com/klauspost/reedsolomon v1.9.9 // indirect
Expand All @@ -21,6 +25,7 @@ require (
github.com/prometheus/common v0.7.0
github.com/rakyll/statik v0.1.7
github.com/schollz/progressbar/v2 v2.15.0
github.com/shirou/gopsutil v2.20.5+incompatible
github.com/sirupsen/logrus v1.5.0
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8
github.com/spf13/cobra v0.0.5
Expand Down
Loading

0 comments on commit 48a25b9

Please sign in to comment.