Skip to content

Commit 20e4c87

Browse files
committed
add admin interface to zos-api-light
1 parent e71cf04 commit 20e4c87

File tree

3 files changed

+98
-2
lines changed

3 files changed

+98
-2
lines changed

docs/manual/api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ Shows information about all open connections in the node
210210
Where
211211

212212
```json
213-
Args {
213+
args {
214214
"twin_id": "uint32",
215215
"workload_id": "uint64",
216216
}
@@ -227,7 +227,7 @@ Stops a workload
227227
Where
228228

229229
```json
230-
Args {
230+
args {
231231
"twin_id": "uint32",
232232
"workload_id": "uint64",
233233
}

pkg/zos_api_light/admin.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"os"
8+
"path/filepath"
9+
10+
"github.com/rs/zerolog/log"
11+
"github.com/threefoldtech/zosbase/pkg/zinit"
712
)
813

914
func (g *ZosAPI) adminInterfacesHandler(ctx context.Context, payload []byte) (interface{}, error) {
@@ -44,5 +49,85 @@ func (g *ZosAPI) adminSetPublicNICHandler(ctx context.Context, payload []byte) (
4449
return nil, fmt.Errorf("failed to decode input, expecting string: %w", err)
4550
}
4651
return nil, fmt.Errorf("not supported")
52+
}
53+
54+
func (g *ZosAPI) adminRebootHandler(ctx context.Context, payload []byte) (interface{}, error) {
55+
zinit := zinit.Default()
56+
57+
return nil, zinit.Reboot()
58+
}
59+
60+
func (g *ZosAPI) adminRestartServiceHandler(ctx context.Context, payload []byte) (interface{}, error) {
61+
var service string
62+
if err := json.Unmarshal(payload, &service); err != nil {
63+
return nil, fmt.Errorf("failed to decode input, expecting string: %w", err)
64+
}
65+
66+
zinit := zinit.Default()
67+
68+
return nil, zinit.Restart(service)
69+
}
70+
71+
func (g *ZosAPI) adminRestartAllHandler(ctx context.Context, payload []byte) (interface{}, error) {
72+
zinit := zinit.Default()
73+
74+
services, err := zinit.List()
75+
if err != nil {
76+
return nil, fmt.Errorf("failed to list node services, expecting string: %w", err)
77+
}
78+
79+
for service := range services {
80+
log.Debug().Str("service", service).Send()
81+
if err := zinit.Restart(service); err != nil {
82+
return nil, fmt.Errorf("failed to reboot service %s, expecting string: %w", service, err)
83+
}
84+
}
85+
86+
return nil, nil
87+
}
88+
89+
func (g *ZosAPI) adminShowLogsHandler(ctx context.Context, payload []byte) (interface{}, error) {
90+
var n int
91+
if err := json.Unmarshal(payload, &n); err != nil {
92+
return nil, fmt.Errorf("failed to decode input, expecting string: %w", err)
93+
}
94+
95+
zinit := zinit.Default()
96+
97+
return zinit.Log(n)
98+
}
99+
100+
func (g *ZosAPI) adminShowResolveHandler(ctx context.Context, payload []byte) (interface{}, error) {
101+
path := filepath.Join("/etc", "resolv.conf")
102+
return os.ReadFile(path)
103+
}
104+
105+
func (g *ZosAPI) adminShowOpenConnectionsHandler(ctx context.Context, payload []byte) (interface{}, error) {
106+
return g.statisticsStub.OpenConnections(ctx)
107+
}
108+
109+
func (g *ZosAPI) adminStopWorkloadHandler(ctx context.Context, payload []byte) (interface{}, error) {
110+
var args struct {
111+
TwinID uint32 `json:"twin_id"`
112+
WorkloadID uint64 `json:"workload_id"`
113+
}
114+
115+
if err := json.Unmarshal(payload, &args); err != nil {
116+
return nil, fmt.Errorf("failed to decode input, expecting twin id and workload id: %w", err)
117+
}
118+
119+
return nil, g.provisionStub.Pause(ctx, args.TwinID, args.WorkloadID)
120+
}
121+
122+
func (g *ZosAPI) adminResumeWorkloadHandler(ctx context.Context, payload []byte) (interface{}, error) {
123+
var args struct {
124+
TwinID uint32 `json:"twin_id"`
125+
WorkloadID uint64 `json:"workload_id"`
126+
}
127+
128+
if err := json.Unmarshal(payload, &args); err != nil {
129+
return nil, fmt.Errorf("failed to decode input, expecting twin id and workload id: %w", err)
130+
}
47131

132+
return nil, g.provisionStub.Resume(ctx, args.TwinID, args.WorkloadID)
48133
}

pkg/zos_api_light/routes.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ func (g *ZosAPI) SetupRoutes(router *peer.Router) {
4949
admin.WithHandler("set_public_nic", g.adminSetPublicNICHandler)
5050
admin.WithHandler("get_public_nic", g.adminGetPublicNICHandler)
5151

52+
admin.WithHandler("reboot", g.adminRebootHandler)
53+
admin.WithHandler("restart", g.adminRestartServiceHandler)
54+
admin.WithHandler("restart_all", g.adminRestartAllHandler)
55+
56+
admin.WithHandler("show_logs", g.adminShowLogsHandler)
57+
admin.WithHandler("show_resolve", g.adminShowResolveHandler)
58+
admin.WithHandler("show_open_connections", g.adminShowOpenConnectionsHandler)
59+
60+
admin.WithHandler("stop_workload", g.adminStopWorkloadHandler)
61+
admin.WithHandler("resume_workload", g.adminResumeWorkloadHandler)
62+
5263
location := root.SubRoute("location")
5364
location.WithHandler("get", g.locationGet)
5465
}

0 commit comments

Comments
 (0)