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 reboot to admin access #2489

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions client/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,20 @@ func (n *NodeClient) NetworkListInterfaces(ctx context.Context) (result map[stri
return
}

// AdminRebootNode return all physical devices on a node
func (n *NodeClient) AdminRebootNode(ctx context.Context) error {
const cmd = "zos.admin.reboot"

return n.bus.Call(ctx, n.nodeTwin, cmd, nil, nil)
}

// AdminRestartService return all physical devices on a node
func (n *NodeClient) AdminRestartService(ctx context.Context, service string) error {
const cmd = "zos.admin.restart"

return n.bus.Call(ctx, n.nodeTwin, cmd, service, nil)
}

// NetworkListAllInterfaces return all physical devices on a node
func (n *NodeClient) NetworkListAllInterfaces(ctx context.Context) (result map[string]Interface, err error) {
const cmd = "zos.network.admin.interfaces"
Expand Down
16 changes: 16 additions & 0 deletions docs/manual/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ it means it can act like an access node to user private networks

The next set of commands are ONLY possible to be called by the `farmer` only.

### Reboot Node

| command |body| return|
|---|---|---|
| `zos.admin.reboot` | - | - |

Stops all services then reboot the node

### Restart Service

| command |body| return|
|---|---|---|
| `zos.admin.reboot` | string | - |

Restarts a service running on the node

### List Physical Interfaces

| command |body| return|
Expand Down
23 changes: 23 additions & 0 deletions pkg/zos_api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,31 @@ import (
"context"
"encoding/json"
"fmt"

"github.com/threefoldtech/zos/pkg/zinit"
)

func (g *ZosAPI) adminRestartHandler(ctx context.Context, payload []byte) (interface{}, error) {
var service string
if err := json.Unmarshal(payload, &service); err != nil {
return nil, fmt.Errorf("failed to decode input, expecting string: %w", err)
}

zinit := zinit.Default()

if err := zinit.Stop(service); err != nil {
return nil, err
}

return nil, zinit.Start(service)
}

func (g *ZosAPI) adminRebootHandler(ctx context.Context, payload []byte) (interface{}, error) {
zinit := zinit.Default()

return nil, zinit.Reboot()
}

func (g *ZosAPI) adminInterfacesHandler(ctx context.Context, payload []byte) (interface{}, error) {
// list all interfaces on node
type Interface struct {
Expand Down
2 changes: 2 additions & 0 deletions pkg/zos_api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func (g *ZosAPI) SetupRoutes(router *peer.Router) {

admin := root.SubRoute("admin")
admin.Use(g.authorized)
admin.WithHandler("reboot", g.adminRebootHandler)
admin.WithHandler("restart", g.adminRestartHandler)
admin.WithHandler("interfaces", g.adminInterfacesHandler)
admin.WithHandler("set_public_nic", g.adminSetPublicNICHandler)
admin.WithHandler("get_public_nic", g.adminGetPublicNICHandler)
Expand Down
Loading