From 8e4461de81df1629aad7244c43b2dc3b8bbde72d Mon Sep 17 00:00:00 2001 From: Alexander Adhyatma Date: Fri, 28 May 2021 21:36:17 +0700 Subject: [PATCH 1/8] wip: min_hops api endpoint --- pkg/visor/api.go | 13 ++++++++++++ pkg/visor/hypervisor.go | 42 +++++++++++++++++++++++++++++++++++++ pkg/visor/rpc.go | 12 +++++++++++ pkg/visor/rpc_client.go | 29 ++++++++++++++++++++++--- pkg/visor/visorconfig/v1.go | 9 ++++++++ 5 files changed, 102 insertions(+), 3 deletions(-) diff --git a/pkg/visor/api.go b/pkg/visor/api.go index 0b05465eaf..c3e4180faf 100644 --- a/pkg/visor/api.go +++ b/pkg/visor/api.go @@ -70,6 +70,9 @@ type API interface { UpdateAvailable(channel updater.Channel) (*updater.Version, error) UpdateStatus() (string, error) RuntimeLogs() (string, error) + + GetMinHops() (uint16, error) + SetMinHops(uint16) error } // HealthCheckable resource returns its health status as an integer @@ -734,3 +737,13 @@ func (v *Visor) RuntimeLogs() (string, error) { builder.WriteString("]") return builder.String(), nil } + +// GetMinHops gets min_hops routing config of visor +func (v *Visor) GetMinHops() (uint16, error) { + return v.conf.Routing.MinHops, nil +} + +// SetMinHops sets min_hops routing config of visor +func (v *Visor) SetMinHops(in uint16) error { + return v.conf.UpdateMinHops(in) +} diff --git a/pkg/visor/hypervisor.go b/pkg/visor/hypervisor.go index c408bb62dc..07fff15fe2 100644 --- a/pkg/visor/hypervisor.go +++ b/pkg/visor/hypervisor.go @@ -258,6 +258,8 @@ func (hv *Hypervisor) makeMux() chi.Router { r.Get("/visors/{pk}/update/available", hv.visorUpdateAvailable()) r.Get("/visors/{pk}/update/available/{channel}", hv.visorUpdateAvailable()) r.Get("/visors/{pk}/runtime-logs", hv.getRuntimeLogs()) + r.Get("/visors/{pk}/min-hops", hv.getMinHops()) + r.Post("/visors/{pk}/min-hops", hv.postMinHops()) }) }) @@ -1296,6 +1298,46 @@ func (hv *Hypervisor) getRuntimeLogs() http.HandlerFunc { }) } +func (hv *Hypervisor) getMinHops() http.HandlerFunc { + return hv.withCtx(hv.visorCtx, func(w http.ResponseWriter, r *http.Request, ctx *httpCtx) { + hops, err := ctx.API.GetMinHops() + if err != nil { + httputil.WriteJSON(w, r, http.StatusInternalServerError, err) + return + } + + hopsResp := struct { + MinHops uint16 `json:"min_hops"` + }{ + MinHops: hops, + } + + httputil.WriteJSON(w, r, http.StatusOK, hopsResp) + }) +} + +func (hv *Hypervisor) postMinHops() http.HandlerFunc { + return hv.withCtx(hv.visorCtx, func(w http.ResponseWriter, r *http.Request, ctx *httpCtx) { + var reqBody struct { + MinHops uint16 `json:"min_hops"` + } + + if err := httputil.ReadJSON(r, &reqBody); err != nil { + if err != io.EOF { + hv.log(r).Warnf("postMinHops request: %v", err) + } + httputil.WriteJSON(w, r, http.StatusBadRequest, usermanager.ErrMalformedRequest) + return + } + + if err := ctx.API.SetMinHops(reqBody.MinHops); err != nil { + httputil.WriteJSON(w, r, http.StatusInternalServerError, err) + return + } + httputil.WriteJSON(w, r, http.StatusOK, struct{}{}) + }) +} + /* <<< Helper functions >>> */ diff --git a/pkg/visor/rpc.go b/pkg/visor/rpc.go index 87d687ce0f..277852e15d 100644 --- a/pkg/visor/rpc.go +++ b/pkg/visor/rpc.go @@ -546,3 +546,15 @@ func (r *RPC) RuntimeLogs(_ *struct{}, logs *string) (err error) { *logs, err = r.visor.RuntimeLogs() return } + +// GetMinHops gets min_hops from visor's routing config +func (r *RPC) GetMinHops() (n uint16, err error) { + defer rpcutil.LogCall(r.log, "GetMinHops", nil) + return r.visor.GetMinHops() +} + +// SetMinHops sets min_hops from visor's routing config +func (r *RPC) SetMinHops(n uint16) error { + defer rpcutil.LogCall(r.log, "SetMinHops", n) + return r.visor.SetMinHops(n) +} diff --git a/pkg/visor/rpc_client.go b/pkg/visor/rpc_client.go index 449db7aa1d..6dbc17d98c 100644 --- a/pkg/visor/rpc_client.go +++ b/pkg/visor/rpc_client.go @@ -331,13 +331,26 @@ func (rc *rpcClient) Update(config updater.UpdateConfig) (bool, error) { return updated, err } -// Update calls Update. +// RuntimeLogs calls RuntimeLogs. func (rc *rpcClient) RuntimeLogs() (string, error) { var logs string err := rc.Call("RuntimeLogs", &struct{}{}, &logs) return logs, err } +// GetMinHops gets min_hops from visor routing config +func (rc *rpcClient) GetMinHops() (uint16, error) { + var hops uint16 + err := rc.Call("GetMinHops", &struct{}{}, &hops) + return hops, err +} + +// SetMinHops sets the min_hops from visor routing config +func (rc *rpcClient) SetMinHops(hops uint16) error { + err := rc.Call("SetMinHops", &hops, &struct{}{}) + return err +} + // StatusMessage defines a status of visor update. type StatusMessage struct { Text string @@ -424,7 +437,7 @@ func (rc *rpcClient) UpdateAvailable(channel updater.Channel) (*updater.Version, return &version, err } -// UpdateAvailable calls UpdateAvailable. +// UpdateStatus calls UpdateStatus func (rc *rpcClient) UpdateStatus() (string, error) { var result string err := rc.Call("UpdateStatus", &struct{}{}, &result) @@ -919,7 +932,17 @@ func (mc *mockRPCClient) UpdateStatus() (string, error) { return "", nil } -// UpdateStatus implements API. +// RuntimeLogs implements API. func (mc *mockRPCClient) RuntimeLogs() (string, error) { return "", nil } + +// GetMinHops implements API. +func (mc *mockRPCClient) GetMinHops() (uint16, error) { + return uint16(0), nil +} + +// SetMinHops implements API +func (mc *mockRPCClient) SetMinHops(n uint16) error { + return nil +} diff --git a/pkg/visor/visorconfig/v1.go b/pkg/visor/visorconfig/v1.go index 59ec1fdd9e..b01750fe2f 100644 --- a/pkg/visor/visorconfig/v1.go +++ b/pkg/visor/visorconfig/v1.go @@ -174,6 +174,15 @@ func (v1 *V1) UpdateAppArg(launch *launcher.Launcher, appName, argName string, v return v1.flush(v1) } +// UpdateMinHops updates min_hops config +func (v1 *V1) UpdateMinHops(hops uint16) error { + v1.mu.Lock() + v1.Routing.MinHops = hops + v1.mu.Unlock() + + return v1.flush(v1) +} + // updateStringArg updates the cli non-boolean flag of the specified app config and also within the launcher. // It removes argName from app args if value is an empty string. // The updated config gets flushed to file if there are any changes. From 0c6f20723a24a08d658fbca6678da2c0ab9d7c12 Mon Sep 17 00:00:00 2001 From: Alexander Adhyatma Date: Fri, 28 May 2021 21:55:19 +0700 Subject: [PATCH 2/8] added cobra command for rpc client --- cmd/skywire-cli/commands/visor/routes.go | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/cmd/skywire-cli/commands/visor/routes.go b/cmd/skywire-cli/commands/visor/routes.go index 17590e7e56..d397486d72 100644 --- a/cmd/skywire-cli/commands/visor/routes.go +++ b/cmd/skywire-cli/commands/visor/routes.go @@ -22,6 +22,8 @@ func init() { ruleCmd, rmRuleCmd, addRuleCmd, + getMinHopsCmd, + setMinHopsCmd, ) } @@ -118,6 +120,32 @@ var addRuleCmd = &cobra.Command{ }, } +var getMinHopsCmd = &cobra.Command{ + Use: "get-min-hops", + Short: "Gets local visor's min_hops routing config", + Run: func(_ *cobra.Command, _ []string) { + hops, err := rpcClient().GetMinHops() + internal.Catch(err) + + _, err = fmt.Fprintf(os.Stdout, "Visor's min hops: %d\n", hops) + internal.Catch(err) + }, +} + +var setMinHopsCmd = &cobra.Command{ + Use: "set-min-hops", + Short: "Sets the local visor's min_hops routing config", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + hops, err := strconv.ParseUint(args[0], 10, 16) + internal.Catch(err) + + err = rpcClient().SetMinHops(uint16(hops)) + internal.Catch(err) + fmt.Printf("Successfully sets min_hops to: %d\n", hops) + }, +} + func printRoutingRules(rules ...routing.Rule) { printConsumeRule := func(w io.Writer, id routing.RouteID, s *routing.RuleSummary) { _, err := fmt.Fprintf(w, "%d\t%s\t%d\t%d\t%s\t%s\t%s\t%s\t%s\n", id, s.Type, From 2c55814a714012d864bf0f64a3e47bedba36b146 Mon Sep 17 00:00:00 2001 From: Alexander Adhyatma Date: Fri, 28 May 2021 22:08:45 +0700 Subject: [PATCH 3/8] fixes rpc calling signature --- pkg/visor/rpc.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pkg/visor/rpc.go b/pkg/visor/rpc.go index 277852e15d..d9183fd7af 100644 --- a/pkg/visor/rpc.go +++ b/pkg/visor/rpc.go @@ -548,13 +548,15 @@ func (r *RPC) RuntimeLogs(_ *struct{}, logs *string) (err error) { } // GetMinHops gets min_hops from visor's routing config -func (r *RPC) GetMinHops() (n uint16, err error) { +func (r *RPC) GetMinHops(_ *struct{}, hops *uint16) (err error) { defer rpcutil.LogCall(r.log, "GetMinHops", nil) - return r.visor.GetMinHops() + *hops, err = r.visor.GetMinHops() + return } // SetMinHops sets min_hops from visor's routing config -func (r *RPC) SetMinHops(n uint16) error { - defer rpcutil.LogCall(r.log, "SetMinHops", n) - return r.visor.SetMinHops(n) +func (r *RPC) SetMinHops(n *uint16, _ *struct{}) (err error) { + defer rpcutil.LogCall(r.log, "SetMinHops", *n) + err = r.visor.SetMinHops(*n) + return } From ecd81eb8e558793539ec7a53efe67933eff09b87 Mon Sep 17 00:00:00 2001 From: Alexander Adhyatma Date: Sat, 29 May 2021 14:45:12 +0700 Subject: [PATCH 4/8] removed getminhops and setminhops command --- cmd/skywire-cli/commands/visor/routes.go | 28 ------------------------ 1 file changed, 28 deletions(-) diff --git a/cmd/skywire-cli/commands/visor/routes.go b/cmd/skywire-cli/commands/visor/routes.go index d397486d72..17590e7e56 100644 --- a/cmd/skywire-cli/commands/visor/routes.go +++ b/cmd/skywire-cli/commands/visor/routes.go @@ -22,8 +22,6 @@ func init() { ruleCmd, rmRuleCmd, addRuleCmd, - getMinHopsCmd, - setMinHopsCmd, ) } @@ -120,32 +118,6 @@ var addRuleCmd = &cobra.Command{ }, } -var getMinHopsCmd = &cobra.Command{ - Use: "get-min-hops", - Short: "Gets local visor's min_hops routing config", - Run: func(_ *cobra.Command, _ []string) { - hops, err := rpcClient().GetMinHops() - internal.Catch(err) - - _, err = fmt.Fprintf(os.Stdout, "Visor's min hops: %d\n", hops) - internal.Catch(err) - }, -} - -var setMinHopsCmd = &cobra.Command{ - Use: "set-min-hops", - Short: "Sets the local visor's min_hops routing config", - Args: cobra.ExactArgs(1), - Run: func(cmd *cobra.Command, args []string) { - hops, err := strconv.ParseUint(args[0], 10, 16) - internal.Catch(err) - - err = rpcClient().SetMinHops(uint16(hops)) - internal.Catch(err) - fmt.Printf("Successfully sets min_hops to: %d\n", hops) - }, -} - func printRoutingRules(rules ...routing.Rule) { printConsumeRule := func(w io.Writer, id routing.RouteID, s *routing.RuleSummary) { _, err := fmt.Fprintf(w, "%d\t%s\t%d\t%d\t%s\t%s\t%s\t%s\t%s\n", id, s.Type, From ac31e2d6ce8b3531f6a5463011cdf2c86ba15de1 Mon Sep 17 00:00:00 2001 From: Senyoret1 <34079003+Senyoret1@users.noreply.github.com> Date: Mon, 31 May 2021 16:43:26 -0400 Subject: [PATCH 5/8] Fix a bug in the app list --- .../apps/node-apps-list/node-apps-list.component.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/static/skywire-manager-src/src/app/components/pages/node/apps/node-apps-list/node-apps-list.component.ts b/static/skywire-manager-src/src/app/components/pages/node/apps/node-apps-list/node-apps-list.component.ts index 90933e3c61..6d6e4974bf 100644 --- a/static/skywire-manager-src/src/app/components/pages/node/apps/node-apps-list/node-apps-list.component.ts +++ b/static/skywire-manager-src/src/app/components/pages/node/apps/node-apps-list/node-apps-list.component.ts @@ -211,12 +211,18 @@ export class NodeAppsListComponent implements OnDestroy { let port = '8001'; // Try to get the port from the config array. - for (let i = 0; i < app.args.length; i++) { - if (app.args[i] === '-addr' && i + 1 < app.args.length) { - port = app.args[i + 1]; + if (app.args) { + for (let i = 0; i < app.args.length; i++) { + if (app.args[i] === '-addr' && i + 1 < app.args.length) { + port = (app.args[i + 1] as string).trim(); + } } } + if (!port.startsWith(':')) { + port = ':' + port; + } + return 'http://' + this.nodeIp + port; } From 642fa408854192ae6aea8e91dab1a091cf5bedad Mon Sep 17 00:00:00 2001 From: Alexander Adhyatma Date: Tue, 1 Jun 2021 10:15:08 +0700 Subject: [PATCH 6/8] min_hops within summary endpoint --- pkg/visor/api.go | 8 ++------ pkg/visor/hypervisor.go | 19 ------------------ pkg/visor/rpc.go | 43 +++-------------------------------------- pkg/visor/rpc_client.go | 7 ------- 4 files changed, 5 insertions(+), 72 deletions(-) diff --git a/pkg/visor/api.go b/pkg/visor/api.go index c3e4180faf..3817ca5c30 100644 --- a/pkg/visor/api.go +++ b/pkg/visor/api.go @@ -71,7 +71,6 @@ type API interface { UpdateStatus() (string, error) RuntimeLogs() (string, error) - GetMinHops() (uint16, error) SetMinHops(uint16) error } @@ -140,6 +139,7 @@ type Summary struct { IsHypervisor bool `json:"is_hypervisor,omitempty"` DmsgStats *dmsgtracker.DmsgClientSummary `json:"dmsg_stats"` Online bool `json:"online"` + MinHops uint16 `json:"min_hops"` } // Summary implements API. @@ -178,6 +178,7 @@ func (v *Visor) Summary() (*Summary, error) { Health: health, Uptime: uptime, Routes: extraRoutes, + MinHops: v.conf.Routing.MinHops, } return summary, nil @@ -738,11 +739,6 @@ func (v *Visor) RuntimeLogs() (string, error) { return builder.String(), nil } -// GetMinHops gets min_hops routing config of visor -func (v *Visor) GetMinHops() (uint16, error) { - return v.conf.Routing.MinHops, nil -} - // SetMinHops sets min_hops routing config of visor func (v *Visor) SetMinHops(in uint16) error { return v.conf.UpdateMinHops(in) diff --git a/pkg/visor/hypervisor.go b/pkg/visor/hypervisor.go index 07fff15fe2..4b58652e42 100644 --- a/pkg/visor/hypervisor.go +++ b/pkg/visor/hypervisor.go @@ -258,7 +258,6 @@ func (hv *Hypervisor) makeMux() chi.Router { r.Get("/visors/{pk}/update/available", hv.visorUpdateAvailable()) r.Get("/visors/{pk}/update/available/{channel}", hv.visorUpdateAvailable()) r.Get("/visors/{pk}/runtime-logs", hv.getRuntimeLogs()) - r.Get("/visors/{pk}/min-hops", hv.getMinHops()) r.Post("/visors/{pk}/min-hops", hv.postMinHops()) }) }) @@ -1298,24 +1297,6 @@ func (hv *Hypervisor) getRuntimeLogs() http.HandlerFunc { }) } -func (hv *Hypervisor) getMinHops() http.HandlerFunc { - return hv.withCtx(hv.visorCtx, func(w http.ResponseWriter, r *http.Request, ctx *httpCtx) { - hops, err := ctx.API.GetMinHops() - if err != nil { - httputil.WriteJSON(w, r, http.StatusInternalServerError, err) - return - } - - hopsResp := struct { - MinHops uint16 `json:"min_hops"` - }{ - MinHops: hops, - } - - httputil.WriteJSON(w, r, http.StatusOK, hopsResp) - }) -} - func (hv *Hypervisor) postMinHops() http.HandlerFunc { return hv.withCtx(hv.visorCtx, func(w http.ResponseWriter, r *http.Request, ctx *httpCtx) { var reqBody struct { diff --git a/pkg/visor/rpc.go b/pkg/visor/rpc.go index d9183fd7af..58d1ec12d0 100644 --- a/pkg/visor/rpc.go +++ b/pkg/visor/rpc.go @@ -1,7 +1,6 @@ package visor import ( - "encoding/hex" "errors" "fmt" "net/rpc" @@ -147,40 +146,11 @@ func newTransportSummary(tm *transport.Manager, tp *transport.ManagedTransport, // Summary provides an extra summary of the AppNode. func (r *RPC) Summary(_ *struct{}, out *Summary) (err error) { - overview, err := r.visor.Overview() - if err != nil { - return fmt.Errorf("summary") - } - - health, err := r.visor.Health() - if err != nil { - return fmt.Errorf("health") - } - - uptime, err := r.visor.Uptime() - if err != nil { - return fmt.Errorf("uptime") - } + defer rpcutil.LogCall(r.log, "Summary", nil)(out, &err) - routes, err := r.visor.RoutingRules() + out, err = r.visor.Summary() if err != nil { - return fmt.Errorf("routes") - } - - extraRoutes := make([]routingRuleResp, 0, len(routes)) - for _, route := range routes { - extraRoutes = append(extraRoutes, routingRuleResp{ - Key: route.KeyRouteID(), - Rule: hex.EncodeToString(route), - Summary: route.Summary(), - }) - } - - *out = Summary{ - Overview: overview, - Health: health, - Uptime: uptime, - Routes: extraRoutes, + return err } return nil @@ -547,13 +517,6 @@ func (r *RPC) RuntimeLogs(_ *struct{}, logs *string) (err error) { return } -// GetMinHops gets min_hops from visor's routing config -func (r *RPC) GetMinHops(_ *struct{}, hops *uint16) (err error) { - defer rpcutil.LogCall(r.log, "GetMinHops", nil) - *hops, err = r.visor.GetMinHops() - return -} - // SetMinHops sets min_hops from visor's routing config func (r *RPC) SetMinHops(n *uint16, _ *struct{}) (err error) { defer rpcutil.LogCall(r.log, "SetMinHops", *n) diff --git a/pkg/visor/rpc_client.go b/pkg/visor/rpc_client.go index 6dbc17d98c..4bb3bac017 100644 --- a/pkg/visor/rpc_client.go +++ b/pkg/visor/rpc_client.go @@ -338,13 +338,6 @@ func (rc *rpcClient) RuntimeLogs() (string, error) { return logs, err } -// GetMinHops gets min_hops from visor routing config -func (rc *rpcClient) GetMinHops() (uint16, error) { - var hops uint16 - err := rc.Call("GetMinHops", &struct{}{}, &hops) - return hops, err -} - // SetMinHops sets the min_hops from visor routing config func (rc *rpcClient) SetMinHops(hops uint16) error { err := rc.Call("SetMinHops", &hops, &struct{}{}) From adde96a21edadd7158094c2a17a3da3318214183 Mon Sep 17 00:00:00 2001 From: Alexander Adhyatma Date: Tue, 1 Jun 2021 10:20:10 +0700 Subject: [PATCH 7/8] removed getminhops from mock rpc client too --- pkg/visor/rpc_client.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pkg/visor/rpc_client.go b/pkg/visor/rpc_client.go index 4bb3bac017..7545c3ef37 100644 --- a/pkg/visor/rpc_client.go +++ b/pkg/visor/rpc_client.go @@ -930,11 +930,6 @@ func (mc *mockRPCClient) RuntimeLogs() (string, error) { return "", nil } -// GetMinHops implements API. -func (mc *mockRPCClient) GetMinHops() (uint16, error) { - return uint16(0), nil -} - // SetMinHops implements API func (mc *mockRPCClient) SetMinHops(n uint16) error { return nil From b2d3432c951438221574083f1e831398b0af4afe Mon Sep 17 00:00:00 2001 From: Alexander Adhyatma Date: Tue, 1 Jun 2021 10:24:27 +0700 Subject: [PATCH 8/8] revive in travis --- .travis.yml | 2 +- pkg/visor/rpc.go | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 073f9050f3..f304f68781 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,7 +27,7 @@ before_install: - nvm install 10.16 install: - - curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $GOPATH/bin v1.31.0 + - curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $GOPATH/bin v1.40.1 - make dep before_script: diff --git a/pkg/visor/rpc.go b/pkg/visor/rpc.go index 58d1ec12d0..43bb847653 100644 --- a/pkg/visor/rpc.go +++ b/pkg/visor/rpc.go @@ -147,12 +147,11 @@ func newTransportSummary(tm *transport.Manager, tp *transport.ManagedTransport, // Summary provides an extra summary of the AppNode. func (r *RPC) Summary(_ *struct{}, out *Summary) (err error) { defer rpcutil.LogCall(r.log, "Summary", nil)(out, &err) - - out, err = r.visor.Summary() + sum, err := r.visor.Summary() if err != nil { return err } - + *out = *sum return nil }