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

auto-peering visors to the hypervisor (skybian) #1309

Merged
merged 22 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
20 changes: 10 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -334,43 +334,43 @@ prepare-systray: prepare

## Run skywire from source, without compiling binaries - requires skywire cloned
run-source: prepare
go run ./cmd/skywire-cli/skywire-cli.go config gen -in | go run ./cmd/skywire-visor/skywire-visor.go -nb || true
go run ./cmd/skywire-cli/skywire-cli.go config gen -in | sudo go run ./cmd/skywire-visor/skywire-visor.go -n || true

## Run skywire from source, with vpn server enabled
run-systray: prepare-systray
go run -tags systray ./cmd/skywire-cli/skywire-cli.go config gen -ni | go run -tags systray ./cmd/skywire-visor/skywire-visor.go -nb || true
go run -tags systray ./cmd/skywire-cli/skywire-cli.go config gen -ni | sudo go run -tags systray ./cmd/skywire-visor/skywire-visor.go -n || true

## Run skywire from source, without compiling binaries - requires skywire cloned
run-vpnsrv: prepare
go run ./cmd/skywire-cli/skywire-cli.go config gen -in --servevpn | go run ./cmd/skywire-visor/skywire-visor.go -nb || true
go run ./cmd/skywire-cli/skywire-cli.go config gen -in --servevpn | sudo go run ./cmd/skywire-visor/skywire-visor.go -n || true

## Run skywire from source with test endpoints
run-source-test: prepare
go run ./cmd/skywire-cli/skywire-cli.go config gen -nit | go run ./cmd/skywire-visor/skywire-visor.go -nb || true
go run ./cmd/skywire-cli/skywire-cli.go config gen -nit | sudo go run ./cmd/skywire-visor/skywire-visor.go -n || true

## Run skywire from source, with vpn server enabled
run-vpnsrv-test: prepare
go run ./cmd/skywire-cli/skywire-cli.go config gen -nit --servevpn | go run ./cmd/skywire-visor/skywire-visor.go -nb || true
go run ./cmd/skywire-cli/skywire-cli.go config gen -nit --servevpn | sudo go run ./cmd/skywire-visor/skywire-visor.go -n || true

## Run skywire from source, with vpn server enabled
run-systray-test: prepare-systray
go run -tags systray ./cmd/skywire-cli/skywire-cli.go config gen -nit | go run -tags systray ./cmd/skywire-visor/skywire-visor.go -nb || true
go run -tags systray ./cmd/skywire-cli/skywire-cli.go config gen -nit | sudo go run -tags systray ./cmd/skywire-visor/skywire-visor.go -nb || true

## Run skywire from source with dmsghttp config
run-source-dmsghttp: prepare
go run ./cmd/skywire-cli/skywire-cli.go config gen -din | go run ./cmd/skywire-visor/skywire-visor.go -nb || true
go run ./cmd/skywire-cli/skywire-cli.go config gen -din | sudo go run ./cmd/skywire-visor/skywire-visor.go -nb || true

## Run skywire from source with dmsghttp config and vpn server
run-vpnsrv-dmsghttp: prepare
go run ./cmd/skywire-cli/skywire-cli.go config gen -din --servevpn | go run ./cmd/skywire-visor/skywire-visor.go -nb || true
go run ./cmd/skywire-cli/skywire-cli.go config gen -din --servevpn | sudo go run ./cmd/skywire-visor/skywire-visor.go -nb || true

## Run skywire from source with dmsghttp config and test endpoints
run-source-dmsghttp-test: prepare
go run ./cmd/skywire-cli/skywire-cli.go config gen -dint | go run ./cmd/skywire-visor/skywire-visor.go -nb || true
go run ./cmd/skywire-cli/skywire-cli.go config gen -dint | sudo go run ./cmd/skywire-visor/skywire-visor.go -nb || true

## Run skywire from source with dmsghttp config, vpn server, and test endpoints
run-vpnsrv-dmsghttp-test: prepare
go run ./cmd/skywire-cli/skywire-cli.go config gen -dint --servevpn | go run ./cmd/skywire-visor/skywire-visor.go -nb || true
go run ./cmd/skywire-cli/skywire-cli.go config gen -dint --servevpn | sudo go run ./cmd/skywire-visor/skywire-visor.go -nb || true

lint-ui: ## Lint the UI code
cd $(MANAGER_UI_DIR) && npm run lint
Expand Down
4 changes: 4 additions & 0 deletions cmd/skywire-cli/commands/config/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func init() {
ghiddenflags = append(ghiddenflags, "hide")
genConfigCmd.Flags().BoolVarP(&retainHypervisors, "retainhv", "x", false, "retain existing hypervisors with regen")
ghiddenflags = append(ghiddenflags, "retainhv")
if os.Getenv("SKYBIAN") == "true" {
genConfigCmd.Flags().BoolVarP(&autopeer, "autopeer", "y", false, "automatically peer to hypervisor")
ghiddenflags = append(ghiddenflags, "retainhv")
}
genConfigCmd.Flags().StringVar(&ver, "version", "", "custom version testing override")
ghiddenflags = append(ghiddenflags, "version")
genConfigCmd.Flags().BoolVar(&all, "all", false, "show all flags")
Expand Down
1 change: 1 addition & 0 deletions cmd/skywire-cli/commands/config/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
output string
confPath string
configName string
autopeer bool
stdout bool
regen bool
retainHypervisors bool
Expand Down
47 changes: 47 additions & 0 deletions cmd/skywire-cli/commands/hv/hvpk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package clihv

import (
"fmt"
"net"
"os"
"strings"

"github.com/spf13/cobra"

"github.com/skycoin/skywire-utilities/pkg/netutil"
"github.com/skycoin/skywire/pkg/visor"
)

func init() {
if os.Getenv("SKYBIAN") == "true" {
RootCmd.AddCommand(pkCmd)
}
localIPs, err = netutil.DefaultNetworkInterfaceIPs()
if err != nil {
logger.WithError(err).Fatalln("Could not determine network interface IP address")
}
if len(localIPs) == 0 {
jdknives marked this conversation as resolved.
Show resolved Hide resolved
localIPs = append(localIPs, net.ParseIP("192.168.0.1"))
}

pkCmd.Flags().StringVarP(&ipAddr, "ip", "i", trimStringFromDot(localIPs[0].String())+".2:7998", "ip:port to query")
jdknives marked this conversation as resolved.
Show resolved Hide resolved
}

func trimStringFromDot(s string) string {
if idx := strings.LastIndex(s, "."); idx != -1 {
return s[:idx]
}
return s
}
0pcom marked this conversation as resolved.
Show resolved Hide resolved

var pkCmd = &cobra.Command{
Use: "pk",
Short: "Fetch Hypervisor Public Key",
Run: func(_ *cobra.Command, _ []string) {
s, err := visor.FetchHvPk(ipAddr)
if err != nil {
logger.WithError(err).Fatalln("failed to fetch hypervisor public key")
}
fmt.Printf("%s\n", s)
},
}
15 changes: 9 additions & 6 deletions cmd/skywire-cli/commands/hv/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ import (
)

var (
logger = logging.MustGetLogger("skywire-cli")
rpcAddr string
path string
pk string
url string
pkg bool
logger = logging.MustGetLogger("skywire-cli")
rpcAddr string
path string
pk string
url string
pkg bool
ipAddr string
localIPs []net.IP
err error
)

// RootCmd contains commands that interact with the skywire-visor
Expand Down
19 changes: 13 additions & 6 deletions cmd/skywire-cli/commands/visor/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package clivisor
import (
"fmt"
"log"
"net/http"
"os"
"unicode/utf8"

"github.com/spf13/cobra"

Expand All @@ -14,16 +14,18 @@ import (
var path string
var pkg bool
var web bool
var webPort string
var pk string

func init() {
RootCmd.AddCommand(pkCmd)
pkCmd.Flags().StringVarP(&path, "input", "i", "", "path of input config file.")
pkCmd.Flags().BoolVarP(&pkg, "pkg", "p", false, "read from /opt/skywire/skywire.json")
pkCmd.Flags().BoolVarP(&web, "http", "w", false, "format as http response")
pkCmd.Flags().BoolVarP(&web, "http", "w", false, "serve public key via http")
pkCmd.Flags().StringVarP(&webPort, "prt", "x", "7998", "serve public key via http")
RootCmd.AddCommand(hvpkCmd)
hvpkCmd.Flags().StringVarP(&path, "input", "i", "", "path of input config file.")
hvpkCmd.Flags().BoolVarP(&pkg, "pkg", "p", false, "read from /opt/skywire/skywire.json")
hvpkCmd.Flags().BoolVarP(&web, "http", "w", false, "format as http response")
RootCmd.AddCommand(summaryCmd)
RootCmd.AddCommand(buildInfoCmd)
}
Expand All @@ -47,10 +49,11 @@ var pkCmd = &cobra.Command{
if err != nil {
logger.Fatal("Failed to connect:", err)
}
pk = overview.PubKey.String()
if web {
rc := utf8.RuneCountInString(overview.PubKey.String())
header := fmt.Sprintf("HTTP/1.0 200 OK\r\nContent-Length: %d\r\n", rc)
fmt.Println(header)
http.HandleFunc("/", srvpk)
logger.Info("\nServing public key " + pk + " on port " + webPort)
http.ListenAndServe(":"+webPort, nil) //nolint
}
fmt.Println(overview.PubKey)
}
Expand Down Expand Up @@ -110,3 +113,7 @@ var buildInfoCmd = &cobra.Command{
}
},
}

func srvpk(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintf(w, pk) //nolint
}
57 changes: 55 additions & 2 deletions cmd/skywire-visor/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"io/fs"
"io/ioutil"
"net"
"net/http"
_ "net/http/pprof" // nolint:gosec // https://golang.org/doc/diagnostics.html#profiling
"os"
Expand All @@ -28,6 +29,7 @@ import (
"github.com/skycoin/skywire-utilities/pkg/cipher"
"github.com/skycoin/skywire-utilities/pkg/cmdutil"
"github.com/skycoin/skywire-utilities/pkg/logging"
"github.com/skycoin/skywire-utilities/pkg/netutil"
"github.com/skycoin/skywire/pkg/restart"
"github.com/skycoin/skywire/pkg/skyenv"
"github.com/skycoin/skywire/pkg/syslog"
Expand All @@ -45,6 +47,7 @@ const (
)

var (
logger = logging.MustGetLogger("skywire-visor")
tag string
syslogAddr string
pprofMode string
Expand All @@ -55,13 +58,16 @@ var (
hypervisorUI bool
remoteHypervisorPKs string
disableHypervisorPKs bool
isAutoPeer bool
autoPeerCmd string
stopVisorFn func()
stopVisorWg sync.WaitGroup
completion string
hiddenflags []string
all bool
pkg bool
usr bool
localIPs []net.IP
// root indicates process is run with root permissions
root bool // nolint:unused
// visorBuildInfo holds information about the build
Expand All @@ -77,6 +83,14 @@ func init() {
root = true
}

localIPs, err = netutil.DefaultNetworkInterfaceIPs()
if err != nil {
logger.WithError(err).Warn("Could not determine network interface IP address")
if len(localIPs) == 0 {
localIPs = append(localIPs, net.ParseIP("192.168.0.1"))
}
}

rootCmd.Flags().SortFlags = false

rootCmd.Flags().StringVarP(&confPath, "config", "c", "", "config file to use (default): "+skyenv.ConfigName)
Expand All @@ -88,6 +102,12 @@ func init() {
hiddenflags = append(hiddenflags, "hv")
rootCmd.Flags().BoolVarP(&disableHypervisorPKs, "xhv", "k", false, "disable remote hypervisors set in config file")
hiddenflags = append(hiddenflags, "xhv")
if os.Getenv("SKYBIAN") == "true" {
rootCmd.Flags().StringVarP(&autoPeerCmd, "hvip", "l", trimStringFromDot(localIPs[0].String())+".2:7998", "set hypervisor by ip")
hiddenflags = append(hiddenflags, "hvip")
rootCmd.Flags().BoolVarP(&isAutoPeer, "autopeer", "m", false, "enable autopeering")
hiddenflags = append(hiddenflags, "autopeer")
}
rootCmd.Flags().BoolVarP(&stdin, "stdin", "n", false, "read config from stdin")
hiddenflags = append(hiddenflags, "stdin")
if root {
Expand Down Expand Up @@ -118,6 +138,13 @@ func init() {
}
}

func trimStringFromDot(s string) string {
if idx := strings.LastIndex(s, "."); idx != -1 {
return s[:idx]
}
return s
}

var rootCmd = &cobra.Command{
Use: "skywire-visor",
Short: "Skywire Visor",
Expand All @@ -129,6 +156,7 @@ var rootCmd = &cobra.Command{
// --all unhide flags and print help menu
if all {
for _, j := range hiddenflags {
fmt.Println(j)
0pcom marked this conversation as resolved.
Show resolved Hide resolved
f := cmd.Flags().Lookup(j) //nolint
f.Hidden = false
}
Expand Down Expand Up @@ -250,10 +278,10 @@ func runVisor(conf *visorconfig.V1) {
conf.Hypervisors = []cipher.PubKey{}
}

pubkey := cipher.PubKey{}
if remoteHypervisorPKs != "" {
hypervisorPKsSlice := strings.Split(remoteHypervisorPKs, ",")
for _, pubkeyString := range hypervisorPKsSlice {
pubkey := cipher.PubKey{}
if err := pubkey.Set(pubkeyString); err != nil {
log.Warnf("Cannot add %s PK as remote hypervisor PK due to: %s", pubkeyString, err)
continue
Expand All @@ -262,9 +290,34 @@ func runVisor(conf *visorconfig.V1) {
conf.Hypervisors = append(conf.Hypervisors, pubkey)
}
}
//autopeering should only happen when there is no local or remote hypervisor set in the config.
if conf.Hypervisors != nil {
0pcom marked this conversation as resolved.
Show resolved Hide resolved
if conf.Hypervisor != nil {
isAutoPeer = false
}
}
if isAutoPeer {
log.Infof("autopeering...")
var hvkey string
Copy link
Member

Choose a reason for hiding this comment

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

No need to declare this explicitly here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

should be fixed with latest commits

Copy link
Member

Choose a reason for hiding this comment

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

Still left...

hvkey, err := script.Exec(autoPeerCmd).String()
if err != nil {
log.Error("error autopeering")
0pcom marked this conversation as resolved.
Show resolved Hide resolved
} else {
hvkey = strings.TrimSpace(hvkey)
hypervisorPKsSlice := strings.Split(hvkey, ",")
0pcom marked this conversation as resolved.
Show resolved Hide resolved
for _, pubkeyString := range hypervisorPKsSlice {
if err := pubkey.Set(pubkeyString); err != nil {
log.Warnf("Cannot add %s PK as remote hypervisor PK due to: %s", pubkeyString, err)
continue
}
log.Infof("%s PK added as remote hypervisor PK", pubkeyString)
conf.Hypervisors = append(conf.Hypervisors, pubkey)
}
}
}

ctx, cancel := cmdutil.SignalContext(context.Background(), log)
vis, ok := visor.NewVisor(ctx, conf, restartCtx)
vis, ok := visor.NewVisor(ctx, conf, restartCtx, isAutoPeer, autoPeerCmd)
if !ok {
select {
case <-ctx.Done():
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -474,19 +474,13 @@ github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMB
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/skycoin/dmsg v0.0.0-20220617100223-c17f98a92a47 h1:d/KdILjeiZOj3QFOm8KkOwIr5wwx9zWCl+oGVXPln1o=
github.com/skycoin/dmsg v0.0.0-20220617100223-c17f98a92a47/go.mod h1:7ixxeJVjbe3lxDkI4Yizj/TWoafYxs8cPJfxjlDeG+w=
github.com/skycoin/dmsg v0.0.0-20220704102949-fece1bd9c40c h1:v7d+0yOp066U8FmcUdQ0Nh9Q+qshBO7+w3ZybGJlBnk=
github.com/skycoin/dmsg v0.0.0-20220704102949-fece1bd9c40c/go.mod h1:7ixxeJVjbe3lxDkI4Yizj/TWoafYxs8cPJfxjlDeG+w=
github.com/skycoin/noise v0.0.0-20180327030543-2492fe189ae6 h1:1Nc5EBY6pjfw1kwW0duwyG+7WliWz5u9kgk1h5MnLuA=
github.com/skycoin/noise v0.0.0-20180327030543-2492fe189ae6/go.mod h1:UXghlricA7J3aRD/k7p/zBObQfmBawwCxIVPVjz2Q3o=
github.com/skycoin/skycoin v0.27.1 h1:HatxsRwVSPaV4qxH6290xPBmkH/HgiuAoY2qC+e8C9I=
github.com/skycoin/skycoin v0.27.1/go.mod h1:78nHjQzd8KG0jJJVL/j0xMmrihXi70ti63fh8vXScJw=
github.com/skycoin/skywire-utilities v0.0.0-20220617085111-5c8c8d3ced14/go.mod h1:B63p56igl38Ha+zjqi26d2om6XEe9jozwB6kzAWMnm0=
github.com/skycoin/skywire-utilities v0.0.0-20220630144749-6ea8913bf1e8 h1:xUPi4duqObtDt4BYiNhbwssiUOFTor67Nftqx1F6/uc=
github.com/skycoin/skywire-utilities v0.0.0-20220630144749-6ea8913bf1e8/go.mod h1:B63p56igl38Ha+zjqi26d2om6XEe9jozwB6kzAWMnm0=
github.com/skycoin/skywire-utilities v0.0.0-20220706133229-02cc62ac2323 h1:W+LSTySKEg3/UyVZXziQYyIdIo66g28VuKBk7sl0718=
github.com/skycoin/skywire-utilities v0.0.0-20220706133229-02cc62ac2323/go.mod h1:B63p56igl38Ha+zjqi26d2om6XEe9jozwB6kzAWMnm0=
github.com/skycoin/skywire-utilities v0.0.0-20220712142443-abafa30105ce h1:+AkZeazhMXpzdPhAG2tn71ubVSPwUECMXYK/LIIVQSU=
github.com/skycoin/skywire-utilities v0.0.0-20220712142443-abafa30105ce/go.mod h1:B63p56igl38Ha+zjqi26d2om6XEe9jozwB6kzAWMnm0=
github.com/skycoin/systray v1.10.1-0.20220630135132-48d2a1fb85d8 h1:2IA2Iw6rgNpbd/rogzBbmNpfGLPhf9zYE68RY8CmU4k=
Expand Down
2 changes: 1 addition & 1 deletion pkg/visor/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ func initHypervisors(ctx context.Context, v *Visor, log *logging.Logger) error {

go func(hvErrs chan error) {
defer wg.Done()
ServeRPCClient(ctx, log, v.dmsgC, rpcS, addr, hvErrs)
ServeRPCClient(ctx, log, v.autoPeer, v.autoPeerCmd, v.dmsgC, rpcS, addr, hvErrs)
}(hvErrs)

v.pushCloseStack("hypervisor."+hvPK.String()[:shortHashLen], func() error {
Expand Down
23 changes: 23 additions & 0 deletions pkg/visor/remote_hypervisor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package visor

import (
"io/ioutil"
"net/http"
)

// FetchHvPk fetches the hypervisor public key from the ip:port passed to it.
func FetchHvPk(ipPort string) (string, error) {
req, err := http.NewRequest(http.MethodGet, "http://"+ipPort, nil)
if err != nil {
return "", err
}
res, err := http.DefaultClient.Do(req)
0pcom marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return "", err
}
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
return string(resBody), nil
}
Loading