Skip to content

Commit

Permalink
Implement saving changed visor settings to json config
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Jan 6, 2020
1 parent d9a73f4 commit 8a0bd26
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 34 deletions.
33 changes: 30 additions & 3 deletions cmd/skywire-visor/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type runCfg struct {
port string
startDelay string
args []string
configPath *string

profileStop func()
logger *logging.Logger
Expand Down Expand Up @@ -90,6 +91,7 @@ func Execute() {

func (cfg *runCfg) startProfiler() *runCfg {
var option func(*profile.Profile)

switch cfg.profileMode {
case "none":
cfg.profileStop = func() {}
Expand All @@ -98,7 +100,9 @@ func (cfg *runCfg) startProfiler() *runCfg {
go func() {
log.Println(http.ListenAndServe(fmt.Sprintf("localhost:%v", cfg.port), nil))
}()

cfg.profileStop = func() {}

return cfg
case "cpu":
option = profile.CPUProfile
Expand All @@ -111,7 +115,9 @@ func (cfg *runCfg) startProfiler() *runCfg {
case "trace":
option = profile.TraceProfile
}

cfg.profileStop = profile.Start(profile.ProfilePath("./logs/"+cfg.tag), option).Stop

return cfg
}

Expand All @@ -128,18 +134,32 @@ func (cfg *runCfg) startLogger() *runCfg {
cfg.masterLogger.Out = ioutil.Discard
}
}

return cfg
}

func (cfg *runCfg) readConfig() *runCfg {
var rdr io.Reader
var err error

if !cfg.cfgFromStdin {
configPath := pathutil.FindConfigPath(cfg.args, 0, configEnv, pathutil.NodeDefaults())
rdr, err = os.Open(filepath.Clean(configPath))
configPath = filepath.Clean(configPath)

file, err := os.Open(configPath)
if err != nil {
cfg.logger.Fatalf("Failed to open config: %s", err)
}

defer func() {
if err := file.Close(); err != nil {
cfg.logger.Warnf("Failed to close config file: %v", err)
}
}()

cfg.logger.Info("Reading config from %v", configPath)

rdr = file
cfg.configPath = &configPath
} else {
cfg.logger.Info("Reading config from STDIN")
rdr = bufio.NewReader(os.Stdin)
Expand All @@ -149,14 +169,17 @@ func (cfg *runCfg) readConfig() *runCfg {
if err := json.NewDecoder(rdr).Decode(&cfg.conf); err != nil {
cfg.logger.Fatalf("Failed to decode %s: %s", rdr, err)
}

fmt.Println("TCP Factory conf:", cfg.conf.STCP)

return cfg
}

func (cfg *runCfg) runNode() *runCfg {
startDelay, err := time.ParseDuration(cfg.startDelay)
if err != nil {
cfg.logger.Warnf("Using no visor start delay due to parsing failure: %v", err)

startDelay = time.Duration(0)
}

Expand All @@ -166,7 +189,7 @@ func (cfg *runCfg) runNode() *runCfg {

time.Sleep(startDelay)

node, err := visor.NewNode(&cfg.conf, cfg.masterLogger, cfg.restartCtx)
node, err := visor.NewNode(&cfg.conf, cfg.masterLogger, cfg.restartCtx, cfg.configPath)
if err != nil {
cfg.logger.Fatal("Failed to initialize node: ", err)
}
Expand Down Expand Up @@ -207,18 +230,21 @@ func (cfg *runCfg) runNode() *runCfg {

func (cfg *runCfg) stopNode() *runCfg {
defer cfg.profileStop()

if err := cfg.node.Close(); err != nil {
if !strings.Contains(err.Error(), "closed") {
cfg.logger.Fatal("Failed to close node: ", err)
}
}

return cfg
}

func (cfg *runCfg) waitOsSignals() *runCfg {
ch := make(chan os.Signal, 2)
signal.Notify(ch, []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT}...)
<-ch

go func() {
select {
case <-time.After(time.Duration(cfg.conf.ShutdownTimeout)):
Expand All @@ -227,5 +253,6 @@ func (cfg *runCfg) waitOsSignals() *runCfg {
cfg.logger.Fatalf("Received signal %s: terminating", s)
}
}()

return cfg
}
2 changes: 2 additions & 0 deletions pkg/visor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestTransportDiscovery(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.NoError(t, json.NewEncoder(w).Encode(&httpauth.NextNonceResponse{Edge: pk, NextNonce: 1}))
}))

defer srv.Close()

conf := Config{}
Expand All @@ -54,6 +55,7 @@ func TestTransportDiscovery(t *testing.T) {

func TestTransportLogStore(t *testing.T) {
dir := filepath.Join(os.TempDir(), "foo")

defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
Expand Down
7 changes: 7 additions & 0 deletions pkg/visor/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ type AddTransportIn struct {
// AddTransport creates a transport for the node.
func (r *RPC) AddTransport(in *AddTransportIn, out *TransportSummary) error {
ctx := context.Background()

if in.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Second*20)
Expand All @@ -289,6 +290,7 @@ func (r *RPC) AddTransport(in *AddTransportIn, out *TransportSummary) error {
if err != nil {
return err
}

*out = *newTransportSummary(r.node.tm, tp, false, r.node.router.SetupIsTrusted(tp.Remote()))
return nil
}
Expand All @@ -309,10 +311,12 @@ func (r *RPC) DiscoverTransportsByPK(pk *cipher.PubKey, out *[]*transport.EntryW
if err != nil {
return err
}

entries, err := tpD.GetTransportsByEdge(context.Background(), *pk)
if err != nil {
return err
}

*out = entries
return nil
}
Expand All @@ -323,10 +327,12 @@ func (r *RPC) DiscoverTransportByID(id *uuid.UUID, out *transport.EntryWithStatu
if err != nil {
return err
}

entry, err := tpD.GetTransportByID(context.Background(), *id)
if err != nil {
return err
}

*out = *entry
return nil
}
Expand Down Expand Up @@ -385,6 +391,7 @@ func (r *RPC) Loops(_ *struct{}, out *[]LoopInfo) error {
if err != nil {
return err
}

loops = append(loops, LoopInfo{
ConsumeRule: rule,
FwdRule: rule,
Expand Down
1 change: 1 addition & 0 deletions pkg/visor/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ func (mc *mockRPCClient) RemoveRoutingRule(key routing.RouteID) error {
// Loops implements RPCClient.
func (mc *mockRPCClient) Loops() ([]LoopInfo, error) {
var loops []LoopInfo

rules := mc.rt.AllRules()
for _, rule := range rules {
if rule.Type() != routing.RuleConsume {
Expand Down
Loading

0 comments on commit 8a0bd26

Please sign in to comment.