Skip to content

Commit f169b04

Browse files
committed
add OpenConnections function to primitives
1 parent 2ffb769 commit f169b04

File tree

7 files changed

+101
-15
lines changed

7 files changed

+101
-15
lines changed

pkg/primitives/statistics.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,6 @@ func (s *statsStream) ListGPUs() ([]pkg.GPUInfo, error) {
283283
}
284284
for _, dl := range active.Deployments {
285285
for _, wl := range dl.Workloads {
286-
287286
if wl.Type == zos.ZMachineType || wl.Type == zos.ZMachineLightType {
288287
var vm vmType
289288
if err := json.Unmarshal(wl.Data, &vm); err != nil {
@@ -294,7 +293,6 @@ func (s *statsStream) ListGPUs() ([]pkg.GPUInfo, error) {
294293
gpus[string(gpu)] = dl.ContractID
295294
}
296295
}
297-
298296
}
299297
}
300298
return gpus, nil
@@ -347,3 +345,7 @@ func (s *statsStream) openConnectionsCount() (int, error) {
347345
}
348346
return strconv.Atoi(strings.TrimSpace(string(out)))
349347
}
348+
349+
func (s *statsStream) OpenConnections() ([]byte, error) {
350+
return exec.Command("ss", "-ptn", "state", "established").Output()
351+
}

pkg/provision.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ type Provision interface {
2020
Changes(twin uint32, contractID uint64) ([]gridtypes.Workload, error)
2121
ListPublicIPs() ([]string, error)
2222
ListPrivateIPs(twin uint32, network gridtypes.Name) ([]string, error)
23+
Pause(twin uint32, id uint64) error
24+
Resume(twin uint32, id uint64) error
2325
}
2426

2527
type Statistics interface {
@@ -29,6 +31,7 @@ type Statistics interface {
2931
Workloads() (int, error)
3032
GetCounters() (Counters, error)
3133
ListGPUs() ([]GPUInfo, error)
34+
OpenConnections() ([]byte, error)
3235
}
3336

3437
type Counters struct {

pkg/provision/engine.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ func (e *NativeEngine) Provision(ctx context.Context, deployment gridtypes.Deplo
364364
}
365365

366366
// Pause deployment
367-
func (e *NativeEngine) Pause(ctx context.Context, twin uint32, id uint64) error {
367+
func (e *NativeEngine) Pause(twin uint32, id uint64) error {
368368
deployment, err := e.storage.Get(twin, id)
369369
if err != nil {
370370
return err
@@ -384,7 +384,7 @@ func (e *NativeEngine) Pause(ctx context.Context, twin uint32, id uint64) error
384384
}
385385

386386
// Resume deployment
387-
func (e *NativeEngine) Resume(ctx context.Context, twin uint32, id uint64) error {
387+
func (e *NativeEngine) Resume(twin uint32, id uint64) error {
388388
deployment, err := e.storage.Get(twin, id)
389389
if err != nil {
390390
return err

pkg/provision/interface.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ type Engine interface {
1919
// and will be processes later
2020
Provision(ctx context.Context, wl gridtypes.Deployment) error
2121
Deprovision(ctx context.Context, twin uint32, id uint64, reason string) error
22-
Pause(ctx context.Context, twin uint32, id uint64) error
23-
Resume(ctx context.Context, twin uint32, id uint64) error
22+
Pause(twin uint32, id uint64) error
23+
Resume(twin uint32, id uint64) error
2424
Update(ctx context.Context, update gridtypes.Deployment) error
2525
Storage() Storage
2626
Twins() Twins
@@ -61,7 +61,7 @@ var (
6161
// ErrDeploymentConflict returned if deployment cannot be stored because
6262
// it conflicts with another deployment
6363
ErrDeploymentConflict = fmt.Errorf("conflict")
64-
//ErrDeploymentNotExists returned if object not exists
64+
// ErrDeploymentNotExists returned if object not exists
6565
ErrDeploymentNotExists = fmt.Errorf("deployment does not exist")
6666
// ErrWorkloadNotExist returned by storage if workload does not exist
6767
ErrWorkloadNotExist = fmt.Errorf("workload does not exist")
@@ -78,10 +78,12 @@ var (
7878
)
7979

8080
// Field interface
81-
type Field interface{}
82-
type VersionField struct {
83-
Version uint32
84-
}
81+
type (
82+
Field interface{}
83+
VersionField struct {
84+
Version uint32
85+
}
86+
)
8587

8688
type DescriptionField struct {
8789
Description string

pkg/stubs/provision_stub.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,33 @@ func (s *ProvisionStub) ListPublicIPs(ctx context.Context) (ret0 []string, ret1
159159
}
160160
return
161161
}
162+
163+
func (s *ProvisionStub) Pause(ctx context.Context, arg0 uint32, arg1 uint64) (ret0 error) {
164+
args := []interface{}{arg0, arg1}
165+
result, err := s.client.RequestContext(ctx, s.module, s.object, "Pause", args...)
166+
if err != nil {
167+
panic(err)
168+
}
169+
result.PanicOnError()
170+
ret0 = result.CallError()
171+
loader := zbus.Loader{}
172+
if err := result.Unmarshal(&loader); err != nil {
173+
panic(err)
174+
}
175+
return
176+
}
177+
178+
func (s *ProvisionStub) Resume(ctx context.Context, arg0 uint32, arg1 uint64) (ret0 error) {
179+
args := []interface{}{arg0, arg1}
180+
result, err := s.client.RequestContext(ctx, s.module, s.object, "Resume", args...)
181+
if err != nil {
182+
panic(err)
183+
}
184+
result.PanicOnError()
185+
ret0 = result.CallError()
186+
loader := zbus.Loader{}
187+
if err := result.Unmarshal(&loader); err != nil {
188+
panic(err)
189+
}
190+
return
191+
}

pkg/stubs/statistics_stub.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,23 @@ func (s *StatisticsStub) ListGPUs(ctx context.Context) (ret0 []pkg.GPUInfo, ret1
7979
return
8080
}
8181

82+
func (s *StatisticsStub) OpenConnections(ctx context.Context) (ret0 []uint8, ret1 error) {
83+
args := []interface{}{}
84+
result, err := s.client.RequestContext(ctx, s.module, s.object, "OpenConnections", args...)
85+
if err != nil {
86+
panic(err)
87+
}
88+
result.PanicOnError()
89+
ret1 = result.CallError()
90+
loader := zbus.Loader{
91+
&ret0,
92+
}
93+
if err := result.Unmarshal(&loader); err != nil {
94+
panic(err)
95+
}
96+
return
97+
}
98+
8299
func (s *StatisticsStub) ReservedStream(ctx context.Context) (<-chan gridtypes.Capacity, error) {
83100
ch := make(chan gridtypes.Capacity, 1)
84101
recv, err := s.client.Stream(ctx, s.module, s.object, "ReservedStream")

pkg/zinit/commands.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ const (
4545
ServiceStateSuccess = "success"
4646
// ServiceStateError is return when we a service exit with an error (exit code != 0)
4747
ServiceStateError = "error"
48-
//ServiceStateFailure is set of zinit can not spawn a service in the first place
49-
//due to a missing executable for example. Unlike `error` which is returned if the
50-
//service itself exits with an error.
48+
// ServiceStateFailure is set of zinit can not spawn a service in the first place
49+
// due to a missing executable for example. Unlike `error` which is returned if the
50+
// service itself exits with an error.
5151
ServiceStateFailure = "failure"
5252
)
5353

@@ -204,7 +204,6 @@ type ServiceStatus struct {
204204
func (c *Client) List() (out map[string]ServiceState, err error) {
205205
err = c.cmd("list", &out)
206206
return
207-
208207
}
209208

210209
// Status returns the status of a service
@@ -611,3 +610,36 @@ func (c *Client) Destroy(timeout time.Duration, services ...string) error {
611610

612611
return nil
613612
}
613+
614+
// List returns all the service monitored and their status
615+
func (c *Client) Log(n int) (out []byte, err error) {
616+
cmd1 := exec.Command("zinit", "log", "-s")
617+
cmd2 := exec.Command("tail", "-n", fmt.Sprint(n))
618+
619+
cmd2.Stdin, err = cmd1.StdoutPipe()
620+
if err != nil {
621+
return
622+
}
623+
624+
err = cmd1.Start()
625+
if err != nil {
626+
return
627+
}
628+
629+
output, err := cmd2.Output()
630+
if err != nil {
631+
return
632+
}
633+
634+
err = cmd1.Wait()
635+
if err != nil {
636+
return
637+
}
638+
639+
return output, err
640+
}
641+
642+
// Restart restarts a service.
643+
func (c *Client) Restart(service string) error {
644+
return c.cmd(fmt.Sprintf("restart %s", service), nil)
645+
}

0 commit comments

Comments
 (0)