@@ -4,6 +4,11 @@ import (
4
4
"context"
5
5
"encoding/json"
6
6
"fmt"
7
+ "os"
8
+ "path/filepath"
9
+
10
+ "github.com/rs/zerolog/log"
11
+ "github.com/threefoldtech/zosbase/pkg/zinit"
7
12
)
8
13
9
14
func (g * ZosAPI ) adminInterfacesHandler (ctx context.Context , payload []byte ) (interface {}, error ) {
@@ -44,5 +49,85 @@ func (g *ZosAPI) adminSetPublicNICHandler(ctx context.Context, payload []byte) (
44
49
return nil , fmt .Errorf ("failed to decode input, expecting string: %w" , err )
45
50
}
46
51
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
+ }
47
131
132
+ return nil , g .provisionStub .Resume (ctx , args .TwinID , args .WorkloadID )
48
133
}
0 commit comments