From 2ee7e4a06fe7302e02e679098fa9dd75c5cc6871 Mon Sep 17 00:00:00 2001 From: rawdaGastan Date: Thu, 14 Nov 2024 14:40:29 +0200 Subject: [PATCH 01/13] add reboot to admin access --- docs/manual/api.md | 8 ++++++++ pkg/zos_api/admin.go | 8 ++++++++ pkg/zos_api/routes.go | 1 + 3 files changed, 17 insertions(+) diff --git a/docs/manual/api.md b/docs/manual/api.md index 250eb00f2..3262e1a87 100644 --- a/docs/manual/api.md +++ b/docs/manual/api.md @@ -153,6 +153,14 @@ 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 + +| command |body| return| +|---|---|---| +| `zos.admin.reboot` | - | - | + +Stops all services then reboot the node + ### List Physical Interfaces | command |body| return| diff --git a/pkg/zos_api/admin.go b/pkg/zos_api/admin.go index 4b53237e6..5e3add845 100644 --- a/pkg/zos_api/admin.go +++ b/pkg/zos_api/admin.go @@ -4,8 +4,16 @@ import ( "context" "encoding/json" "fmt" + + "github.com/threefoldtech/zos/pkg/zinit" ) +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 { diff --git a/pkg/zos_api/routes.go b/pkg/zos_api/routes.go index 8006f23b7..84fe227d4 100644 --- a/pkg/zos_api/routes.go +++ b/pkg/zos_api/routes.go @@ -45,6 +45,7 @@ func (g *ZosAPI) SetupRoutes(router *peer.Router) { admin := root.SubRoute("admin") admin.Use(g.authorized) + admin.WithHandler("reboot", g.adminRebootHandler) admin.WithHandler("interfaces", g.adminInterfacesHandler) admin.WithHandler("set_public_nic", g.adminSetPublicNICHandler) admin.WithHandler("get_public_nic", g.adminGetPublicNICHandler) From 030081cc7feed6f48164a1a873189882eb95c525 Mon Sep 17 00:00:00 2001 From: Eslam-Nawara Date: Thu, 14 Nov 2024 15:07:22 +0200 Subject: [PATCH 02/13] add restart service endpoint --- docs/manual/api.md | 10 +++++++++- pkg/zos_api/admin.go | 15 +++++++++++++++ pkg/zos_api/routes.go | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/docs/manual/api.md b/docs/manual/api.md index 3262e1a87..9a9c5ef15 100644 --- a/docs/manual/api.md +++ b/docs/manual/api.md @@ -153,7 +153,7 @@ 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 +### Reboot Node | command |body| return| |---|---|---| @@ -161,6 +161,14 @@ The next set of commands are ONLY possible to be called by the `farmer` only. 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| diff --git a/pkg/zos_api/admin.go b/pkg/zos_api/admin.go index 5e3add845..2474e6b0e 100644 --- a/pkg/zos_api/admin.go +++ b/pkg/zos_api/admin.go @@ -8,6 +8,21 @@ import ( "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() diff --git a/pkg/zos_api/routes.go b/pkg/zos_api/routes.go index 84fe227d4..dd9aa06f3 100644 --- a/pkg/zos_api/routes.go +++ b/pkg/zos_api/routes.go @@ -46,6 +46,7 @@ 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) From ada96ac26092fbef45bc8701acf4f1cd71d1e2a7 Mon Sep 17 00:00:00 2001 From: Eslam-Nawara Date: Thu, 14 Nov 2024 15:16:32 +0200 Subject: [PATCH 03/13] add Admin reboot and restart endpoints to node client --- client/node.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/client/node.go b/client/node.go index 455f448fb..d96ff4ae1 100644 --- a/client/node.go +++ b/client/node.go @@ -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" From 4bb02f29fa3062d526a2408c065cd69dce9ee795 Mon Sep 17 00:00:00 2001 From: Eslam-Nawara Date: Sun, 22 Dec 2024 12:56:34 +0200 Subject: [PATCH 04/13] add zinit restart command --- pkg/zinit/commands.go | 12 ++++++++---- pkg/zos_api/admin.go | 6 +----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/zinit/commands.go b/pkg/zinit/commands.go index 6efd3847c..a057441ce 100644 --- a/pkg/zinit/commands.go +++ b/pkg/zinit/commands.go @@ -45,9 +45,9 @@ const ( ServiceStateSuccess = "success" // ServiceStateError is return when we a service exit with an error (exit code != 0) ServiceStateError = "error" - //ServiceStateFailure is set of zinit can not spawn a service in the first place - //due to a missing executable for example. Unlike `error` which is returned if the - //service itself exits with an error. + // ServiceStateFailure is set of zinit can not spawn a service in the first place + // due to a missing executable for example. Unlike `error` which is returned if the + // service itself exits with an error. ServiceStateFailure = "failure" ) @@ -204,7 +204,6 @@ type ServiceStatus struct { func (c *Client) List() (out map[string]ServiceState, err error) { err = c.cmd("list", &out) return - } // Status returns the status of a service @@ -310,6 +309,11 @@ func (c *Client) Stop(service string) error { return c.cmd(fmt.Sprintf("stop %s", service), nil) } +// Re restarts a service. +func (c *Client) Restart(service string) error { + return c.cmd(fmt.Sprintf("restart %s", service), nil) +} + // StartWait starts a service and wait until its running, or until the timeout // (seconds) pass. If timedout, the method returns an error if the service is not running // timout of 0 means no wait. (similar to Stop) diff --git a/pkg/zos_api/admin.go b/pkg/zos_api/admin.go index 2474e6b0e..018b7a428 100644 --- a/pkg/zos_api/admin.go +++ b/pkg/zos_api/admin.go @@ -16,11 +16,7 @@ func (g *ZosAPI) adminRestartHandler(ctx context.Context, payload []byte) (inter zinit := zinit.Default() - if err := zinit.Stop(service); err != nil { - return nil, err - } - - return nil, zinit.Start(service) + return nil, zinit.Restart(service) } func (g *ZosAPI) adminRebootHandler(ctx context.Context, payload []byte) (interface{}, error) { From 826ca2e8b94caa69b4e596b630ee554f0e01de3a Mon Sep 17 00:00:00 2001 From: Eslam-Nawara Date: Sun, 22 Dec 2024 13:30:49 +0200 Subject: [PATCH 05/13] add restart all services --- cmds/modules/api_gateway/main.go | 10 ++++++---- pkg/zos_api/admin.go | 19 ++++++++++++++++++- pkg/zos_api/routes.go | 4 +++- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/cmds/modules/api_gateway/main.go b/cmds/modules/api_gateway/main.go index b0f1f8cc2..55fc3a6c8 100644 --- a/cmds/modules/api_gateway/main.go +++ b/cmds/modules/api_gateway/main.go @@ -68,7 +68,6 @@ func action(cli *cli.Context) error { return fmt.Errorf("failed to create substrate manager: %w", err) } - router := peer.NewRouter() gw, err := substrategw.NewSubstrateGateway(manager, id) if err != nil { return fmt.Errorf("failed to create api gateway: %w", err) @@ -96,6 +95,8 @@ func action(cli *cli.Context) error { if err != nil { return fmt.Errorf("failed to create zos api: %w", err) } + + router := peer.NewRouter() api.SetupRoutes(router) pair, err := id.KeyPair() @@ -105,7 +106,7 @@ func action(cli *cli.Context) error { bo := backoff.NewExponentialBackOff() bo.MaxElapsedTime = 0 - backoff.Retry(func() error { + if err = backoff.Retry(func() error { _, err = peer.NewPeer( ctx, hex.EncodeToString(pair.Seed()), @@ -117,9 +118,10 @@ func action(cli *cli.Context) error { if err != nil { return fmt.Errorf("failed to start a new rmb peer: %w", err) } - return nil - }, bo) + }, bo); err != nil { + return err + } log.Info(). Str("broker", msgBrokerCon). diff --git a/pkg/zos_api/admin.go b/pkg/zos_api/admin.go index 018b7a428..e6b7af6f7 100644 --- a/pkg/zos_api/admin.go +++ b/pkg/zos_api/admin.go @@ -8,7 +8,7 @@ import ( "github.com/threefoldtech/zos/pkg/zinit" ) -func (g *ZosAPI) adminRestartHandler(ctx context.Context, payload []byte) (interface{}, error) { +func (g *ZosAPI) adminRestartServiceHandler(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) @@ -25,6 +25,23 @@ func (g *ZosAPI) adminRebootHandler(ctx context.Context, payload []byte) (interf return nil, zinit.Reboot() } +func (g *ZosAPI) adminRestartAllHandler(ctx context.Context, payload []byte) (interface{}, error) { + zinit := zinit.Default() + + services, err := zinit.List() + if err != nil { + return nil, fmt.Errorf("failed to list node services, expecting string: %w", err) + } + + for _, service := range services { + if err := zinit.Restart(service.String()); err != nil { + return nil, fmt.Errorf("failed to reboot service %s, expecting string: %w", service.String(), err) + } + } + + return nil, nil +} + func (g *ZosAPI) adminInterfacesHandler(ctx context.Context, payload []byte) (interface{}, error) { // list all interfaces on node type Interface struct { diff --git a/pkg/zos_api/routes.go b/pkg/zos_api/routes.go index dd9aa06f3..7562f9a5a 100644 --- a/pkg/zos_api/routes.go +++ b/pkg/zos_api/routes.go @@ -46,7 +46,9 @@ 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("restart", g.adminRestartServiceHandler) + admin.WithHandler("restart_all", g.adminRestartAllHandler) + admin.WithHandler("interfaces", g.adminInterfacesHandler) admin.WithHandler("set_public_nic", g.adminSetPublicNICHandler) admin.WithHandler("get_public_nic", g.adminGetPublicNICHandler) From 2a0c6293844db93fa34c3c37f4b8edb702c1f0d2 Mon Sep 17 00:00:00 2001 From: Eslam-Nawara Date: Sun, 22 Dec 2024 18:03:10 +0200 Subject: [PATCH 06/13] add show logs endpoint --- pkg/zinit/commands.go | 28 ++++++++++++++++++++++++++++ pkg/zos_api/admin.go | 11 +++++++++++ pkg/zos_api/routes.go | 2 ++ 3 files changed, 41 insertions(+) diff --git a/pkg/zinit/commands.go b/pkg/zinit/commands.go index a057441ce..c62e29748 100644 --- a/pkg/zinit/commands.go +++ b/pkg/zinit/commands.go @@ -206,6 +206,34 @@ func (c *Client) List() (out map[string]ServiceState, err error) { return } +// List returns all the service monitored and their status +func (c *Client) Log(n int) (out string, err error) { + cmd1 := exec.Command("zinit", "log", "-s") + cmd2 := exec.Command("tail", "-n", fmt.Sprint(n)) + + cmd2.Stdin, err = cmd1.StdoutPipe() + if err != nil { + return + } + + err = cmd1.Start() + if err != nil { + return + } + + output, err := cmd2.Output() + if err != nil { + return + } + + err = cmd1.Wait() + if err != nil { + return + } + + return string(output), err +} + // Status returns the status of a service func (c *Client) Status(service string) (result ServiceStatus, err error) { err = c.cmd(fmt.Sprintf("status %s", service), &result) diff --git a/pkg/zos_api/admin.go b/pkg/zos_api/admin.go index e6b7af6f7..15c87faf0 100644 --- a/pkg/zos_api/admin.go +++ b/pkg/zos_api/admin.go @@ -42,6 +42,17 @@ func (g *ZosAPI) adminRestartAllHandler(ctx context.Context, payload []byte) (in return nil, nil } +func (g *ZosAPI) adminShowLogs(ctx context.Context, payload []byte) (interface{}, error) { + var n int + if err := json.Unmarshal(payload, &n); err != nil { + return nil, fmt.Errorf("failed to decode input, expecting string: %w", err) + } + + zinit := zinit.Default() + + return zinit.Log(n) +} + func (g *ZosAPI) adminInterfacesHandler(ctx context.Context, payload []byte) (interface{}, error) { // list all interfaces on node type Interface struct { diff --git a/pkg/zos_api/routes.go b/pkg/zos_api/routes.go index 7562f9a5a..558785d07 100644 --- a/pkg/zos_api/routes.go +++ b/pkg/zos_api/routes.go @@ -49,6 +49,8 @@ func (g *ZosAPI) SetupRoutes(router *peer.Router) { admin.WithHandler("restart", g.adminRestartServiceHandler) admin.WithHandler("restart_all", g.adminRestartAllHandler) + admin.WithHandler("show_logs", g.adminShowLogs) + admin.WithHandler("interfaces", g.adminInterfacesHandler) admin.WithHandler("set_public_nic", g.adminSetPublicNICHandler) admin.WithHandler("get_public_nic", g.adminGetPublicNICHandler) From 9d82197b394ac95c64300a2f1adbf803f8422b73 Mon Sep 17 00:00:00 2001 From: Eslam-Nawara Date: Thu, 26 Dec 2024 11:06:15 +0200 Subject: [PATCH 07/13] show /etc/resolv.conf --- pkg/zos_api/admin.go | 9 ++++++++- pkg/zos_api/routes.go | 3 ++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/pkg/zos_api/admin.go b/pkg/zos_api/admin.go index 15c87faf0..31b9c56eb 100644 --- a/pkg/zos_api/admin.go +++ b/pkg/zos_api/admin.go @@ -4,6 +4,8 @@ import ( "context" "encoding/json" "fmt" + "os" + "path/filepath" "github.com/threefoldtech/zos/pkg/zinit" ) @@ -42,7 +44,7 @@ func (g *ZosAPI) adminRestartAllHandler(ctx context.Context, payload []byte) (in return nil, nil } -func (g *ZosAPI) adminShowLogs(ctx context.Context, payload []byte) (interface{}, error) { +func (g *ZosAPI) adminShowLogsHandler(ctx context.Context, payload []byte) (interface{}, error) { var n int if err := json.Unmarshal(payload, &n); err != nil { return nil, fmt.Errorf("failed to decode input, expecting string: %w", err) @@ -53,6 +55,11 @@ func (g *ZosAPI) adminShowLogs(ctx context.Context, payload []byte) (interface{} return zinit.Log(n) } +func (g *ZosAPI) adminShowResolveHandler(ctx context.Context, payload []byte) (interface{}, error) { + path := filepath.Join("/etc", "resolv.conf") + return os.ReadFile(path) +} + func (g *ZosAPI) adminInterfacesHandler(ctx context.Context, payload []byte) (interface{}, error) { // list all interfaces on node type Interface struct { diff --git a/pkg/zos_api/routes.go b/pkg/zos_api/routes.go index 558785d07..aed39dfec 100644 --- a/pkg/zos_api/routes.go +++ b/pkg/zos_api/routes.go @@ -49,7 +49,8 @@ func (g *ZosAPI) SetupRoutes(router *peer.Router) { admin.WithHandler("restart", g.adminRestartServiceHandler) admin.WithHandler("restart_all", g.adminRestartAllHandler) - admin.WithHandler("show_logs", g.adminShowLogs) + admin.WithHandler("show_logs", g.adminShowLogsHandler) + admin.WithHandler("show_resolve", g.adminShowResolveHandler) admin.WithHandler("interfaces", g.adminInterfacesHandler) admin.WithHandler("set_public_nic", g.adminSetPublicNICHandler) From 6a1fe10e8cd035f7fe51eb34506b2f8de7649171 Mon Sep 17 00:00:00 2001 From: Eslam-Nawara Date: Sun, 29 Dec 2024 14:28:02 +0200 Subject: [PATCH 08/13] add total number of open connections to node statistics --- pkg/primitives/statistics.go | 42 +++++++++++++++++++++++++---------- pkg/provision.go | 3 +++ pkg/stubs/api_gateway_stub.go | 39 +++++++++++++++----------------- pkg/stubs/statistics_stub.go | 17 ++++++++++++++ 4 files changed, 68 insertions(+), 33 deletions(-) diff --git a/pkg/primitives/statistics.go b/pkg/primitives/statistics.go index 41aa534bd..dfd089ca5 100644 --- a/pkg/primitives/statistics.go +++ b/pkg/primitives/statistics.go @@ -4,6 +4,9 @@ import ( "context" "encoding/json" "fmt" + "os/exec" + "strconv" + "strings" "time" "github.com/pkg/errors" @@ -31,9 +34,7 @@ func GetCapacity(ctx context.Context) gridtypes.Capacity { return val.(gridtypes.Capacity) } -var ( - _ provision.Provisioner = (*Statistics)(nil) -) +var _ provision.Provisioner = (*Statistics)(nil) type Reserved func() (gridtypes.Capacity, error) @@ -146,7 +147,6 @@ func (s *Statistics) hasEnoughCapacity(wl *gridtypes.WorkloadWithID) (gridtypes. id, _ := gridtypes.NewWorkloadID(dl_.TwinID, dl_.ContractID, wl_.Name) return id == wl.ID }) - if err != nil { return used, errors.Wrap(err, "failed to get available memory") } @@ -155,7 +155,7 @@ func (s *Statistics) hasEnoughCapacity(wl *gridtypes.WorkloadWithID) (gridtypes. return used, fmt.Errorf("cannot fulfil required memory size %d bytes out of usable %d bytes", required.MRU, usable) } - //check other resources as well? + // check other resources as well? return used, nil } @@ -235,6 +235,21 @@ func (s *statsStream) Total() gridtypes.Capacity { return s.stats.Total() } +func (s *statsStream) OpenConnections() ([]byte, error) { + args := []string{"-ptn", "state", "established"} + cmd := exec.Command("ss", args...) + return cmd.Output() +} + +func (s *statsStream) openConnectionsCount() (int, error) { + cmd := exec.Command("/bin/sh", "-c", "ss -ptn state established | wc -l") + out, err := cmd.Output() + if err != nil { + return 0, err + } + return strconv.Atoi(strings.TrimSpace(string(out))) +} + func (s *statsStream) Workloads() (int, error) { capacity, err := s.stats.storage.Capacity() if err != nil { @@ -253,10 +268,17 @@ func (s *statsStream) GetCounters() (pkg.Counters, error) { if err != nil { return pkg.Counters{}, err } + + connCount, err := s.openConnectionsCount() + if err != nil { + return pkg.Counters{}, err + } + return pkg.Counters{ - Total: s.stats.Total(), - Used: activeCounters.cap, - System: reserved, + Total: s.stats.Total(), + Used: activeCounters.cap, + System: reserved, + OpenConnecions: connCount, Users: pkg.UsersCounters{ Deployments: activeCounters.deployments, Workloads: activeCounters.workloads, @@ -298,10 +320,6 @@ func (s *statsStream) ListGPUs() ([]pkg.GPUInfo, error) { return nil, errors.Wrap(err, "failed to list available devices") } - if err != nil { - return nil, errors.Wrap(err, "failed to list active deployments") - } - used, err := usedGpus() if err != nil { return nil, errors.Wrap(err, "failed to list used gpus") diff --git a/pkg/provision.go b/pkg/provision.go index c0d2ee898..0a69aebd7 100644 --- a/pkg/provision.go +++ b/pkg/provision.go @@ -29,6 +29,7 @@ type Statistics interface { Workloads() (int, error) GetCounters() (Counters, error) ListGPUs() ([]GPUInfo, error) + OpenConnections() ([]byte, error) } type Counters struct { @@ -40,6 +41,8 @@ type Counters struct { System gridtypes.Capacity `json:"system"` // Users statistics by zos Users UsersCounters `json:"users"` + // OpenConnecions number of open connections in the node + OpenConnecions int `json:"open_connections"` } // UsersCounters the expected counters for deployments and workloads diff --git a/pkg/stubs/api_gateway_stub.go b/pkg/stubs/api_gateway_stub.go index 498e2e536..97ebbb831 100644 --- a/pkg/stubs/api_gateway_stub.go +++ b/pkg/stubs/api_gateway_stub.go @@ -6,12 +6,11 @@ package stubs import ( "context" - "time" - types "github.com/centrifuge/go-substrate-rpc-client/v4/types" tfchainclientgo "github.com/threefoldtech/tfchain/clients/tfchain-client-go" zbus "github.com/threefoldtech/zbus" pkg "github.com/threefoldtech/zos/pkg" + "time" ) type SubstrateGatewayStub struct { @@ -31,25 +30,6 @@ func NewSubstrateGatewayStub(client zbus.Client) *SubstrateGatewayStub { } } -func (s *SubstrateGatewayStub) GetZosVersion(ctx context.Context) (ret0 string, ret1 error) { - args := []interface{}{} - result, err := s.client.RequestContext(ctx, s.module, s.object, "GetZosVersion", 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 *SubstrateGatewayStub) CreateNode(ctx context.Context, arg0 tfchainclientgo.Node) (ret0 uint32, ret1 error) { args := []interface{}{arg0} result, err := s.client.RequestContext(ctx, s.module, s.object, "CreateNode", args...) @@ -305,6 +285,23 @@ func (s *SubstrateGatewayStub) GetTwinByPubKey(ctx context.Context, arg0 []uint8 return } +func (s *SubstrateGatewayStub) GetZosVersion(ctx context.Context) (ret0 string, ret1 error) { + args := []interface{}{} + result, err := s.client.RequestContext(ctx, s.module, s.object, "GetZosVersion", 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 *SubstrateGatewayStub) Report(ctx context.Context, arg0 []tfchainclientgo.NruConsumption) (ret0 types.Hash, ret1 error) { args := []interface{}{arg0} result, err := s.client.RequestContext(ctx, s.module, s.object, "Report", args...) diff --git a/pkg/stubs/statistics_stub.go b/pkg/stubs/statistics_stub.go index cc7c51dfb..7353cb2a6 100644 --- a/pkg/stubs/statistics_stub.go +++ b/pkg/stubs/statistics_stub.go @@ -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") From e8aa47fe00959bcbb52e1b35d8654888e8bfcde0 Mon Sep 17 00:00:00 2001 From: Eslam-Nawara Date: Sun, 29 Dec 2024 14:28:55 +0200 Subject: [PATCH 09/13] add get open connections endpoint --- pkg/zos_api/admin.go | 4 ++++ pkg/zos_api/routes.go | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/pkg/zos_api/admin.go b/pkg/zos_api/admin.go index 31b9c56eb..1c465992d 100644 --- a/pkg/zos_api/admin.go +++ b/pkg/zos_api/admin.go @@ -60,6 +60,10 @@ func (g *ZosAPI) adminShowResolveHandler(ctx context.Context, payload []byte) (i return os.ReadFile(path) } +func (g *ZosAPI) adminShowOpenConnectionsHandler(ctx context.Context, payload []byte) (interface{}, error) { + return g.statisticsStub.OpenConnections(ctx) +} + func (g *ZosAPI) adminInterfacesHandler(ctx context.Context, payload []byte) (interface{}, error) { // list all interfaces on node type Interface struct { diff --git a/pkg/zos_api/routes.go b/pkg/zos_api/routes.go index aed39dfec..ad5b417f3 100644 --- a/pkg/zos_api/routes.go +++ b/pkg/zos_api/routes.go @@ -49,8 +49,9 @@ func (g *ZosAPI) SetupRoutes(router *peer.Router) { admin.WithHandler("restart", g.adminRestartServiceHandler) admin.WithHandler("restart_all", g.adminRestartAllHandler) - admin.WithHandler("show_logs", g.adminShowLogsHandler) + admin.WithHandler("logs", g.adminShowLogsHandler) admin.WithHandler("show_resolve", g.adminShowResolveHandler) + admin.WithHandler("get_open_connections", g.adminShowOpenConnectionsHandler) admin.WithHandler("interfaces", g.adminInterfacesHandler) admin.WithHandler("set_public_nic", g.adminSetPublicNICHandler) From cf3a13840c3da0661685819209bb41fe94977333 Mon Sep 17 00:00:00 2001 From: Eslam-Nawara Date: Wed, 1 Jan 2025 13:56:31 +0200 Subject: [PATCH 10/13] add endpoint to stop and resume a workload --- cmds/modules/provisiond/events.go | 4 ++-- pkg/provision.go | 3 +++ pkg/provision/engine.go | 4 ++-- pkg/provision/interface.go | 16 +++++++++------- pkg/stubs/provision_stub.go | 30 ++++++++++++++++++++++++++++++ pkg/zos_api/admin.go | 26 ++++++++++++++++++++++++++ pkg/zos_api/routes.go | 3 +++ 7 files changed, 75 insertions(+), 11 deletions(-) diff --git a/cmds/modules/provisiond/events.go b/cmds/modules/provisiond/events.go index c14d23b05..797f96205 100644 --- a/cmds/modules/provisiond/events.go +++ b/cmds/modules/provisiond/events.go @@ -107,7 +107,7 @@ func (r *ContractEventHandler) sync(ctx context.Context) error { action = r.engine.Pause } - if err := action(ctx, dl.TwinID, dl.ContractID); err != nil { + if err := action(dl.TwinID, dl.ContractID); err != nil { log.Error().Err(err).Msg("failed to change contract state") } } @@ -176,7 +176,7 @@ func (r *ContractEventHandler) Run(ctx context.Context) error { action = r.engine.Pause } - if err := action(ctx, event.TwinId, event.Contract); err != nil { + if err := action(event.TwinId, event.Contract); err != nil { log.Error().Err(err). Uint32("twin", event.TwinId). Uint64("contract", event.Contract). diff --git a/pkg/provision.go b/pkg/provision.go index 0a69aebd7..55838f900 100644 --- a/pkg/provision.go +++ b/pkg/provision.go @@ -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 { @@ -30,6 +32,7 @@ type Statistics interface { GetCounters() (Counters, error) ListGPUs() ([]GPUInfo, error) OpenConnections() ([]byte, error) + // Pause(id gridtypes.WorkloadID) error } type Counters struct { diff --git a/pkg/provision/engine.go b/pkg/provision/engine.go index 9842b5307..ca1eb0a6b 100644 --- a/pkg/provision/engine.go +++ b/pkg/provision/engine.go @@ -361,7 +361,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 @@ -381,7 +381,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 diff --git a/pkg/provision/interface.go b/pkg/provision/interface.go index 9a1a0e1c6..7fa228dc5 100644 --- a/pkg/provision/interface.go +++ b/pkg/provision/interface.go @@ -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 @@ -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") @@ -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 diff --git a/pkg/stubs/provision_stub.go b/pkg/stubs/provision_stub.go index 077bdf6a8..314def388 100644 --- a/pkg/stubs/provision_stub.go +++ b/pkg/stubs/provision_stub.go @@ -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 +} diff --git a/pkg/zos_api/admin.go b/pkg/zos_api/admin.go index 1c465992d..3fbd97d83 100644 --- a/pkg/zos_api/admin.go +++ b/pkg/zos_api/admin.go @@ -64,6 +64,32 @@ func (g *ZosAPI) adminShowOpenConnectionsHandler(ctx context.Context, payload [] return g.statisticsStub.OpenConnections(ctx) } +func (g *ZosAPI) adminStopWorkloadHandler(ctx context.Context, payload []byte) (interface{}, error) { + var args struct { + TwinID uint32 `json:"twin_id"` + WorkloadID uint64 `json:"workload_id"` + } + + if err := json.Unmarshal(payload, &args); err != nil { + return nil, fmt.Errorf("failed to decode input, expecting twin id and workload id: %w", err) + } + + return nil, g.provisionStub.Pause(ctx, args.TwinID, args.WorkloadID) +} + +func (g *ZosAPI) adminResumeWorkloadHandler(ctx context.Context, payload []byte) (interface{}, error) { + var args struct { + TwinID uint32 `json:"twin_id"` + WorkloadID uint64 `json:"workload_id"` + } + + if err := json.Unmarshal(payload, &args); err != nil { + return nil, fmt.Errorf("failed to decode input, expecting twin id and workload id: %w", err) + } + + return nil, g.provisionStub.Resume(ctx, args.TwinID, args.WorkloadID) +} + func (g *ZosAPI) adminInterfacesHandler(ctx context.Context, payload []byte) (interface{}, error) { // list all interfaces on node type Interface struct { diff --git a/pkg/zos_api/routes.go b/pkg/zos_api/routes.go index ad5b417f3..5f5539d46 100644 --- a/pkg/zos_api/routes.go +++ b/pkg/zos_api/routes.go @@ -53,6 +53,9 @@ func (g *ZosAPI) SetupRoutes(router *peer.Router) { admin.WithHandler("show_resolve", g.adminShowResolveHandler) admin.WithHandler("get_open_connections", g.adminShowOpenConnectionsHandler) + admin.WithHandler("stop_workload", g.adminStopWorkloadHandler) + admin.WithHandler("resume_workload", g.adminResumeWorkloadHandler) + admin.WithHandler("interfaces", g.adminInterfacesHandler) admin.WithHandler("set_public_nic", g.adminSetPublicNICHandler) admin.WithHandler("get_public_nic", g.adminGetPublicNICHandler) From f1c0924f970a3b1ec1ef1da7f788e88e39fd7ebc Mon Sep 17 00:00:00 2001 From: Eslam-Nawara Date: Thu, 2 Jan 2025 15:33:23 +0200 Subject: [PATCH 11/13] add admin endpoint to node client --- client/node.go | 69 +++++++++++++++++++++++++++++++++++++++---- pkg/zos_api/routes.go | 4 +-- 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/client/node.go b/client/node.go index d96ff4ae1..5b42e1467 100644 --- a/client/node.go +++ b/client/node.go @@ -259,23 +259,82 @@ func (n *NodeClient) NetworkListInterfaces(ctx context.Context) (result map[stri return } -// AdminRebootNode return all physical devices on a node +// 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 return all physical devices on a node +// 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 a 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) +} + // 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) @@ -285,14 +344,14 @@ 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 diff --git a/pkg/zos_api/routes.go b/pkg/zos_api/routes.go index 5f5539d46..ef1da4690 100644 --- a/pkg/zos_api/routes.go +++ b/pkg/zos_api/routes.go @@ -49,9 +49,9 @@ func (g *ZosAPI) SetupRoutes(router *peer.Router) { admin.WithHandler("restart", g.adminRestartServiceHandler) admin.WithHandler("restart_all", g.adminRestartAllHandler) - admin.WithHandler("logs", g.adminShowLogsHandler) + admin.WithHandler("show_logs", g.adminShowLogsHandler) admin.WithHandler("show_resolve", g.adminShowResolveHandler) - admin.WithHandler("get_open_connections", g.adminShowOpenConnectionsHandler) + admin.WithHandler("show_open_connections", g.adminShowOpenConnectionsHandler) admin.WithHandler("stop_workload", g.adminStopWorkloadHandler) admin.WithHandler("resume_workload", g.adminResumeWorkloadHandler) From 4d9bf34709e1eba9b722f46aa0bac24bfb0a7be3 Mon Sep 17 00:00:00 2001 From: Eslam-Nawara Date: Thu, 2 Jan 2025 17:11:36 +0200 Subject: [PATCH 12/13] add admin workload docs --- client/node.go | 2 +- docs/manual/api.md | 82 ++++++++++++++++++++++++++++++++---- pkg/primitives/statistics.go | 4 +- pkg/provision.go | 1 - pkg/zinit/commands.go | 4 +- 5 files changed, 78 insertions(+), 15 deletions(-) diff --git a/client/node.go b/client/node.go index 5b42e1467..949aa3692 100644 --- a/client/node.go +++ b/client/node.go @@ -280,7 +280,7 @@ func (n *NodeClient) AdminRestartAll(ctx context.Context) error { return n.bus.Call(ctx, n.nodeTwin, cmd, nil, nil) } -// AdminShowLogs returns a l lines of zinit logs +// 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" diff --git a/docs/manual/api.md b/docs/manual/api.md index 9a9c5ef15..6baf7cc22 100644 --- a/docs/manual/api.md +++ b/docs/manual/api.md @@ -84,7 +84,8 @@ so `used = user_used + system`, while `system` is only the amount of resourced r | `zos.storage.pools` | - |`[]Pool`| List all node pools with their types, size and used space -where + +Where ```json Pool { @@ -151,7 +152,7 @@ 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 @@ -159,21 +160,87 @@ The next set of commands are ONLY possible to be called by the `farmer` only. |---|---|---| | `zos.admin.reboot` | - | - | -Stops all services then reboot the node +Stops all services then reboots the node ### Restart Service | command |body| return| |---|---|---| -| `zos.admin.reboot` | string | - | +| `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 @@ -191,7 +258,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 @@ -209,7 +276,7 @@ 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` @@ -239,7 +306,6 @@ name must be one of (free) names returned by `zos.network.admin.interfaces` |---|---|---| | `zos.system.node_features_get` | - |`[]NodeFeature` | - Where ```json diff --git a/pkg/primitives/statistics.go b/pkg/primitives/statistics.go index dfd089ca5..441d9c2cd 100644 --- a/pkg/primitives/statistics.go +++ b/pkg/primitives/statistics.go @@ -236,9 +236,7 @@ func (s *statsStream) Total() gridtypes.Capacity { } func (s *statsStream) OpenConnections() ([]byte, error) { - args := []string{"-ptn", "state", "established"} - cmd := exec.Command("ss", args...) - return cmd.Output() + return exec.Command("ss", "-ptn", "state", "established").Output() } func (s *statsStream) openConnectionsCount() (int, error) { diff --git a/pkg/provision.go b/pkg/provision.go index 55838f900..6fae7d98d 100644 --- a/pkg/provision.go +++ b/pkg/provision.go @@ -32,7 +32,6 @@ type Statistics interface { GetCounters() (Counters, error) ListGPUs() ([]GPUInfo, error) OpenConnections() ([]byte, error) - // Pause(id gridtypes.WorkloadID) error } type Counters struct { diff --git a/pkg/zinit/commands.go b/pkg/zinit/commands.go index c62e29748..e857b2bc1 100644 --- a/pkg/zinit/commands.go +++ b/pkg/zinit/commands.go @@ -207,7 +207,7 @@ func (c *Client) List() (out map[string]ServiceState, err error) { } // List returns all the service monitored and their status -func (c *Client) Log(n int) (out string, err error) { +func (c *Client) Log(n int) (out []byte, err error) { cmd1 := exec.Command("zinit", "log", "-s") cmd2 := exec.Command("tail", "-n", fmt.Sprint(n)) @@ -231,7 +231,7 @@ func (c *Client) Log(n int) (out string, err error) { return } - return string(output), err + return output, err } // Status returns the status of a service From 6844a1540eadc02d174445bd1ffac1950a42cbda Mon Sep 17 00:00:00 2001 From: Eslam-Nawara Date: Mon, 6 Jan 2025 13:10:23 +0200 Subject: [PATCH 13/13] fix restart service --- pkg/zinit/commands.go | 2 +- pkg/zos_api/admin.go | 20 +++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/pkg/zinit/commands.go b/pkg/zinit/commands.go index e857b2bc1..20218a323 100644 --- a/pkg/zinit/commands.go +++ b/pkg/zinit/commands.go @@ -337,7 +337,7 @@ func (c *Client) Stop(service string) error { return c.cmd(fmt.Sprintf("stop %s", service), nil) } -// Re restarts a service. +// Restart restarts a service. func (c *Client) Restart(service string) error { return c.cmd(fmt.Sprintf("restart %s", service), nil) } diff --git a/pkg/zos_api/admin.go b/pkg/zos_api/admin.go index 3fbd97d83..c1eba978d 100644 --- a/pkg/zos_api/admin.go +++ b/pkg/zos_api/admin.go @@ -7,9 +7,16 @@ import ( "os" "path/filepath" + "github.com/rs/zerolog/log" "github.com/threefoldtech/zos/pkg/zinit" ) +func (g *ZosAPI) adminRebootHandler(ctx context.Context, payload []byte) (interface{}, error) { + zinit := zinit.Default() + + return nil, zinit.Reboot() +} + func (g *ZosAPI) adminRestartServiceHandler(ctx context.Context, payload []byte) (interface{}, error) { var service string if err := json.Unmarshal(payload, &service); err != nil { @@ -21,12 +28,6 @@ func (g *ZosAPI) adminRestartServiceHandler(ctx context.Context, payload []byte) return nil, zinit.Restart(service) } -func (g *ZosAPI) adminRebootHandler(ctx context.Context, payload []byte) (interface{}, error) { - zinit := zinit.Default() - - return nil, zinit.Reboot() -} - func (g *ZosAPI) adminRestartAllHandler(ctx context.Context, payload []byte) (interface{}, error) { zinit := zinit.Default() @@ -35,9 +36,10 @@ func (g *ZosAPI) adminRestartAllHandler(ctx context.Context, payload []byte) (in return nil, fmt.Errorf("failed to list node services, expecting string: %w", err) } - for _, service := range services { - if err := zinit.Restart(service.String()); err != nil { - return nil, fmt.Errorf("failed to reboot service %s, expecting string: %w", service.String(), err) + for service := range services { + log.Debug().Str("service", service).Send() + if err := zinit.Restart(service); err != nil { + return nil, fmt.Errorf("failed to reboot service %s, expecting string: %w", service, err) } }