Skip to content

Add admin interface #27

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

Draft
wants to merge 4 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
79 changes: 76 additions & 3 deletions client/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (n *NodeClient) NetworkListInterfaces(ctx context.Context) (result map[stri

// 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"
const cmd = "zos.admin.interfaces"

err = n.bus.Call(ctx, n.nodeTwin, cmd, nil, &result)

Expand All @@ -199,19 +199,92 @@ func (n *NodeClient) NetworkListAllInterfaces(ctx context.Context) (result map[s
// NetworkSetPublicExitDevice select which physical interface to use as an exit device
// setting `iface` to `zos` will then make node run in a single nic setup.
func (n *NodeClient) NetworkSetPublicExitDevice(ctx context.Context, iface string) error {
const cmd = "zos.network.admin.set_public_nic"
const cmd = "zos.admin.set_public_nic"

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

// NetworkGetPublicExitDevice gets the current dual nic setup of the node.
func (n *NodeClient) NetworkGetPublicExitDevice(ctx context.Context) (exit ExitDevice, err error) {
const cmd = "zos.network.admin.get_public_nic"
const cmd = "zos.admin.get_public_nic"

err = n.bus.Call(ctx, n.nodeTwin, cmd, nil, &exit)
return
}

// AdminRebootNode stops all the running services and reboots the 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 restarts a zinit service
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)
}

// AdminRestartAll restarts all zinit services
func (n *NodeClient) AdminRestartAll(ctx context.Context) error {
const cmd = "zos.admin.restart_all"

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

// AdminShowLogs returns l lines of zinit logs
func (n *NodeClient) AdminShowLogs(ctx context.Context, l int) (logs []byte, err error) {
const cmd = "zos.admin.show_logs"

err = n.bus.Call(ctx, n.nodeTwin, cmd, l, &logs)
return
}

// AdminShowResolve return the content of /etc/resolv.conf
func (n *NodeClient) AdminShowResolve(ctx context.Context) (res []byte, err error) {
const cmd = "zos.admin.show_resolve"

err = n.bus.Call(ctx, n.nodeTwin, cmd, nil, &res)
return
}

// AdminShowOpenConnections return information about all open connections in the node
func (n *NodeClient) AdminShowOpenConnections(ctx context.Context) (res []byte, err error) {
const cmd = "zos.admin.show_open_connections"

err = n.bus.Call(ctx, n.nodeTwin, cmd, nil, &res)
return
}

// AdminStopWorkload stops a workload
func (n *NodeClient) AdminStopWorkload(ctx context.Context, twinID uint32, wlID uint64) error {
const cmd = "zos.admin.stop_workload"
args := struct {
TwinID uint32 `json:"twin_id"`
WorkloadID uint64 `json:"workload_id"`
}{
TwinID: twinID,
WorkloadID: wlID,
}

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

// AdminResumeWorkload stops a workload
func (n *NodeClient) AdminResumeWorkload(ctx context.Context, twinID uint32, wlID uint64) error {
const cmd = "zos.admin.resume_workload"
args := struct {
TwinID uint32 `json:"twin_id"`
WorkloadID uint64 `json:"workload_id"`
}{
TwinID: twinID,
WorkloadID: wlID,
}

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

// NetworkListPublicIPs list taken public IPs on the node
func (n *NodeClient) NetworkListPublicIPs(ctx context.Context) ([]string, error) {
const cmd = "zos.network.list_public_ips"
Expand Down
93 changes: 87 additions & 6 deletions docs/manual/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,95 @@ it means it can act like an access node to user private networks

## Admin

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

### Reboot Node

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

Stops all services then reboots the node

### Restart Service

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

Restarts a service running on the node

### Restart All Services

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

Restarts all zinit services running on the node

### Show Logs

| command |body| return|
|---|---|---|
| `zos.admin.show_logs` | int | []byte |

Shows a number of lines of zinit logs

### Show Resolve

| command |body| return|
|---|---|---|
| `zos.admin.show_resolve` | - | []byte |

Shows the content of /etc/resolv.conf

### Show Open Connections

| command |body| return|
|---|---|---|
| `zos.admin.show_open_connections` | - | []byte |

Shows information about all open connections in the node

### Stop Workload

| command |body| return|
|---|---|---|
| `zos.admin.Stop` | `Args` | - |

Where

```json
args {
"twin_id": "uint32",
"workload_id": "uint64",
}
```

Stops a workload

### Resume Workload

| command |body| return|
|---|---|---|
| `zos.admin.resume` | `Args` | - |

Where

```json
args {
"twin_id": "uint32",
"workload_id": "uint64",
}
```

Resumes a stopped workload

### List Physical Interfaces

| command |body| return|
|---|---|---|
| `zos.network.admin.interfaces` | - |`map[string]Interface` |
| `zos.admin.interfaces` | - |`map[string]Interface` |

Where

Expand All @@ -175,7 +257,7 @@ Those interfaces then can be used as an input to `set_public_nic`

| command |body| return|
|---|---|---|
| `zos.network.admin.get_public_nic` | - |`ExitDevice` |
| `zos.admin.get_public_nic` | - |`ExitDevice` |

Where

Expand All @@ -193,9 +275,9 @@ returns the interface used by public traffic (for user workloads)

| command |body| return|
|---|---|---|
| `zos.network.admin.set_public_nic` | `name` |- |
| `zos.admin.set_public_nic` | `name` |- |

name must be one of (free) names returned by `zos.network.admin.interfaces`
name must be one of (free) names returned by `zos.admin.interfaces`

## System

Expand Down Expand Up @@ -223,7 +305,6 @@ name must be one of (free) names returned by `zos.network.admin.interfaces`
|---|---|---|
| `zos.system.node_features_get` | - |`[]NodeFeature` |


Where

```json
Expand Down
6 changes: 4 additions & 2 deletions pkg/primitives/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ func (s *statsStream) ListGPUs() ([]pkg.GPUInfo, error) {
}
for _, dl := range active.Deployments {
for _, wl := range dl.Workloads {

if wl.Type == zos.ZMachineType || wl.Type == zos.ZMachineLightType {
var vm vmType
if err := json.Unmarshal(wl.Data, &vm); err != nil {
Expand All @@ -294,7 +293,6 @@ func (s *statsStream) ListGPUs() ([]pkg.GPUInfo, error) {
gpus[string(gpu)] = dl.ContractID
}
}

}
}
return gpus, nil
Expand Down Expand Up @@ -347,3 +345,7 @@ func (s *statsStream) openConnectionsCount() (int, error) {
}
return strconv.Atoi(strings.TrimSpace(string(out)))
}

func (s *statsStream) OpenConnections() ([]byte, error) {
return exec.Command("ss", "-ptn", "state", "established").Output()
}
3 changes: 3 additions & 0 deletions pkg/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Provision interface {
Changes(twin uint32, contractID uint64) ([]gridtypes.Workload, error)
ListPublicIPs() ([]string, error)
ListPrivateIPs(twin uint32, network gridtypes.Name) ([]string, error)
Pause(twin uint32, id uint64) error
Resume(twin uint32, id uint64) error
}

type Statistics interface {
Expand All @@ -29,6 +31,7 @@ type Statistics interface {
Workloads() (int, error)
GetCounters() (Counters, error)
ListGPUs() ([]GPUInfo, error)
OpenConnections() ([]byte, error)
}

type Counters struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/provision/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func (e *NativeEngine) Provision(ctx context.Context, deployment gridtypes.Deplo
}

// Pause deployment
func (e *NativeEngine) Pause(ctx context.Context, twin uint32, id uint64) error {
func (e *NativeEngine) Pause(twin uint32, id uint64) error {
deployment, err := e.storage.Get(twin, id)
if err != nil {
return err
Expand All @@ -384,7 +384,7 @@ func (e *NativeEngine) Pause(ctx context.Context, twin uint32, id uint64) error
}

// Resume deployment
func (e *NativeEngine) Resume(ctx context.Context, twin uint32, id uint64) error {
func (e *NativeEngine) Resume(twin uint32, id uint64) error {
deployment, err := e.storage.Get(twin, id)
if err != nil {
return err
Expand Down
16 changes: 9 additions & 7 deletions pkg/provision/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type Engine interface {
// and will be processes later
Provision(ctx context.Context, wl gridtypes.Deployment) error
Deprovision(ctx context.Context, twin uint32, id uint64, reason string) error
Pause(ctx context.Context, twin uint32, id uint64) error
Resume(ctx context.Context, twin uint32, id uint64) error
Pause(twin uint32, id uint64) error
Resume(twin uint32, id uint64) error
Update(ctx context.Context, update gridtypes.Deployment) error
Storage() Storage
Twins() Twins
Expand Down Expand Up @@ -61,7 +61,7 @@ var (
// ErrDeploymentConflict returned if deployment cannot be stored because
// it conflicts with another deployment
ErrDeploymentConflict = fmt.Errorf("conflict")
//ErrDeploymentNotExists returned if object not exists
// ErrDeploymentNotExists returned if object not exists
ErrDeploymentNotExists = fmt.Errorf("deployment does not exist")
// ErrWorkloadNotExist returned by storage if workload does not exist
ErrWorkloadNotExist = fmt.Errorf("workload does not exist")
Expand All @@ -78,10 +78,12 @@ var (
)

// Field interface
type Field interface{}
type VersionField struct {
Version uint32
}
type (
Field interface{}
VersionField struct {
Version uint32
}
)

type DescriptionField struct {
Description string
Expand Down
30 changes: 30 additions & 0 deletions pkg/stubs/provision_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,33 @@ func (s *ProvisionStub) ListPublicIPs(ctx context.Context) (ret0 []string, ret1
}
return
}

func (s *ProvisionStub) Pause(ctx context.Context, arg0 uint32, arg1 uint64) (ret0 error) {
args := []interface{}{arg0, arg1}
result, err := s.client.RequestContext(ctx, s.module, s.object, "Pause", args...)
if err != nil {
panic(err)
}
result.PanicOnError()
ret0 = result.CallError()
loader := zbus.Loader{}
if err := result.Unmarshal(&loader); err != nil {
panic(err)
}
return
}

func (s *ProvisionStub) Resume(ctx context.Context, arg0 uint32, arg1 uint64) (ret0 error) {
args := []interface{}{arg0, arg1}
result, err := s.client.RequestContext(ctx, s.module, s.object, "Resume", args...)
if err != nil {
panic(err)
}
result.PanicOnError()
ret0 = result.CallError()
loader := zbus.Loader{}
if err := result.Unmarshal(&loader); err != nil {
panic(err)
}
return
}
17 changes: 17 additions & 0 deletions pkg/stubs/statistics_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ func (s *StatisticsStub) ListGPUs(ctx context.Context) (ret0 []pkg.GPUInfo, ret1
return
}

func (s *StatisticsStub) OpenConnections(ctx context.Context) (ret0 []uint8, ret1 error) {
args := []interface{}{}
result, err := s.client.RequestContext(ctx, s.module, s.object, "OpenConnections", args...)
if err != nil {
panic(err)
}
result.PanicOnError()
ret1 = result.CallError()
loader := zbus.Loader{
&ret0,
}
if err := result.Unmarshal(&loader); err != nil {
panic(err)
}
return
}

func (s *StatisticsStub) ReservedStream(ctx context.Context) (<-chan gridtypes.Capacity, error) {
ch := make(chan gridtypes.Capacity, 1)
recv, err := s.client.Stream(ctx, s.module, s.object, "ReservedStream")
Expand Down
Loading
Loading