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 7 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
10 changes: 10 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 Expand Up @@ -222,6 +226,12 @@ var genConfigCmd = &cobra.Command{
if testEnv {
serviceConfURL = testconf
}
if autopeer {
ip, err := script.Exec(`ip route show | grep -i 'default via'| awk '{print $3 }'`).String()
0pcom marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
logger.Error("cannot determine gateway IP: " + ip)
}
}
//fetch the service endpoints
services = visorconfig.Fetch(mLog, serviceConfURL, stdout)
// Read in old config and obtain old secret key or generate a new random secret key
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
31 changes: 31 additions & 0 deletions cmd/skywire-cli/commands/hv/hvpk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package clihv

import (
"fmt"

"github.com/bitfield/script"
"github.com/spf13/cobra"
)

func init() {
ipaddr, err := script.Exec(`bash -c "_gateway=$(ip route show | grep -i 'default via'| awk '{print $3 }'); _ip=${_gateway%.*}.2; printf ${_ip}"`).String()
0pcom marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
err.Error()
0pcom marked this conversation as resolved.
Show resolved Hide resolved
}

RootCmd.AddCommand(pkCmd)
pkCmd.Flags().IntVarP(&port, "port", "p", 7998, "port to query")
pkCmd.Flags().StringVarP(&ipadd, "ip", "i", ipaddr, "ip address to query")
}
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) {
hvpk, err := script.Exec(`bash -c "curl ` + fmt.Sprintf("%s:%d", ipadd, port) + `"`).String()
0pcom marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
err.Error()
0pcom marked this conversation as resolved.
Show resolved Hide resolved
}
fmt.Printf("%s", hvpk)
0pcom marked this conversation as resolved.
Show resolved Hide resolved
},
}
2 changes: 2 additions & 0 deletions cmd/skywire-cli/commands/hv/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ var (
pk string
url string
pkg bool
port int
ipadd string
0pcom marked this conversation as resolved.
Show resolved Hide resolved
)

// 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
0pcom marked this conversation as resolved.
Show resolved Hide resolved
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
}
35 changes: 34 additions & 1 deletion cmd/skywire-visor/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ var (
hypervisorUI bool
remoteHypervisorPKs string
disableHypervisorPKs bool
autopeer bool
0pcom marked this conversation as resolved.
Show resolved Hide resolved
peercmd string
stopVisorFn func()
stopVisorWg sync.WaitGroup
completion string
Expand Down Expand Up @@ -88,6 +90,10 @@ func init() {
hiddenflags = append(hiddenflags, "hv")
rootCmd.Flags().BoolVarP(&disableHypervisorPKs, "xhv", "k", false, "disable remote hypervisors set in config file")
hiddenflags = append(hiddenflags, "xhv")
rootCmd.Flags().StringVarP(&peercmd, "peercmd", "l", "skywire-cli hv pk", "command to run which returns the hypervisor key used with autopeer")
hiddenflags = append(hiddenflags, "peercmd")
rootCmd.Flags().BoolVarP(&autopeer, "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 @@ -250,10 +256,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,6 +268,33 @@ 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 {
autopeer = false
}
}
if autopeer {
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(peercmd).String() //TODO: mrpalide this needs to run again on loss of connection to the hypervisor within the same constraints
if err != nil {
log.Error("error autopeering")
0pcom marked this conversation as resolved.
Show resolved Hide resolved
} else {
hvkey = strings.TrimSuffix(hvkey, "\n")
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)
skyenv.AutoPeer = true
0pcom marked this conversation as resolved.
Show resolved Hide resolved
skyenv.AutoPeercmd = peercmd
}
}
}

ctx, cancel := cmdutil.SignalContext(context.Background(), log)
vis, ok := visor.NewVisor(ctx, conf, restartCtx)
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -474,17 +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=
Expand Down
6 changes: 6 additions & 0 deletions pkg/skyenv/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,9 @@ func HomePath() string {
dir, _ := os.UserHomeDir() //nolint
return dir
}

// AutoPeer corresponds to the -m flag for skywire-visor
var AutoPeer bool

// AutoPeercmd coresponds to the -l flag for skywire-visor and is only set when -m flag is used
var AutoPeercmd string
23 changes: 22 additions & 1 deletion pkg/visor/rpc_client_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import (
"context"
"net"
"net/rpc"
"strings"
"time"

"github.com/bitfield/script"
"github.com/sirupsen/logrus"
"github.com/skycoin/dmsg/pkg/dmsg"

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

func isDone(ctx context.Context) bool {
Expand All @@ -25,12 +29,29 @@ func isDone(ctx context.Context) bool {
func ServeRPCClient(ctx context.Context, log logrus.FieldLogger, dmsgC *dmsg.Client, rpcS *rpc.Server, rAddr dmsg.Addr, errCh chan<- error) {
const maxBackoff = time.Second * 5
retry := netutil.NewRetrier(log, netutil.DefaultInitBackoff, maxBackoff, netutil.DefaultTries, netutil.DefaultFactor)

pubkey := cipher.PubKey{}
for {
var conn net.Conn
err := retry.Do(ctx, func() (rErr error) {
log.Info("Dialing...")
addr := dmsg.Addr{PK: rAddr.PK, Port: rAddr.Port}
if skyenv.AutoPeer {
var hvkey string
hvkey, err := script.Exec(skyenv.AutoPeercmd).String()
if err != nil {
log.Error("error autopeering")
} else {
hvkey = strings.TrimSuffix(hvkey, "\n")
hypervisorPKsSlice := strings.Split(hvkey, ",")
for _, pubkeyString := range hypervisorPKsSlice {
jdknives marked this conversation as resolved.
Show resolved Hide resolved
if err := pubkey.Set(pubkeyString); err != nil {
jdknives marked this conversation as resolved.
Show resolved Hide resolved
log.Warnf("Cannot add %s PK as remote hypervisor PK due to: %s", pubkeyString, err)
continue
}
addr.PK = pubkey
}
}
}
conn, rErr = dmsgC.Dial(ctx, addr)
return rErr
})
Expand Down