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/api.go b/pkg/visor/api.go index 0b05465eaf..3817ca5c30 100644 --- a/pkg/visor/api.go +++ b/pkg/visor/api.go @@ -70,6 +70,8 @@ type API interface { UpdateAvailable(channel updater.Channel) (*updater.Version, error) UpdateStatus() (string, error) RuntimeLogs() (string, error) + + SetMinHops(uint16) error } // HealthCheckable resource returns its health status as an integer @@ -137,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. @@ -175,6 +178,7 @@ func (v *Visor) Summary() (*Summary, error) { Health: health, Uptime: uptime, Routes: extraRoutes, + MinHops: v.conf.Routing.MinHops, } return summary, nil @@ -734,3 +738,8 @@ func (v *Visor) RuntimeLogs() (string, error) { builder.WriteString("]") return builder.String(), 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..4b58652e42 100644 --- a/pkg/visor/hypervisor.go +++ b/pkg/visor/hypervisor.go @@ -258,6 +258,7 @@ 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.Post("/visors/{pk}/min-hops", hv.postMinHops()) }) }) @@ -1296,6 +1297,28 @@ func (hv *Hypervisor) getRuntimeLogs() http.HandlerFunc { }) } +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..43bb847653 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,42 +146,12 @@ 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") - } - - routes, err := r.visor.RoutingRules() + defer rpcutil.LogCall(r.log, "Summary", nil)(out, &err) + sum, 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 } - + *out = *sum return nil } @@ -546,3 +515,10 @@ func (r *RPC) RuntimeLogs(_ *struct{}, logs *string) (err error) { *logs, err = r.visor.RuntimeLogs() 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) + err = r.visor.SetMinHops(*n) + return +} diff --git a/pkg/visor/rpc_client.go b/pkg/visor/rpc_client.go index 449db7aa1d..7545c3ef37 100644 --- a/pkg/visor/rpc_client.go +++ b/pkg/visor/rpc_client.go @@ -331,13 +331,19 @@ 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 } +// 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 +430,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 +925,12 @@ func (mc *mockRPCClient) UpdateStatus() (string, error) { return "", nil } -// UpdateStatus implements API. +// RuntimeLogs implements API. func (mc *mockRPCClient) RuntimeLogs() (string, error) { return "", 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.