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

Add min hops manipulation endpoint #780

Merged
merged 7 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
28 changes: 28 additions & 0 deletions cmd/skywire-cli/commands/visor/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func init() {
ruleCmd,
jdknives marked this conversation as resolved.
Show resolved Hide resolved
rmRuleCmd,
addRuleCmd,
getMinHopsCmd,
setMinHopsCmd,
)
}

Expand Down Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions pkg/visor/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
42 changes: 42 additions & 0 deletions pkg/visor/hypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
})

Expand Down Expand Up @@ -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 >>>
*/
Expand Down
14 changes: 14 additions & 0 deletions pkg/visor/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,3 +546,17 @@ 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(_ *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)
err = r.visor.SetMinHops(*n)
return
}
29 changes: 26 additions & 3 deletions pkg/visor/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
9 changes: 9 additions & 0 deletions pkg/visor/visorconfig/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down