Skip to content

Commit 6d1db60

Browse files
committed
refactor: add debug commands for deployment retrieval, listing, and VM info
1 parent cd7f426 commit 6d1db60

File tree

6 files changed

+696
-622
lines changed

6 files changed

+696
-622
lines changed

pkg/debugcmd/deployment_get.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package debugcmd
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
8+
"github.com/threefoldtech/zosbase/pkg/gridtypes"
9+
)
10+
11+
type DeploymentGetRequest struct {
12+
TwinID uint32 `json:"twin_id"`
13+
ContractID uint64 `json:"contract_id"`
14+
WithHistory bool `json:"withhistory"`
15+
}
16+
17+
type WorkloadTransaction struct {
18+
Seq int `json:"seq"`
19+
Type string `json:"type"`
20+
Name string `json:"name"`
21+
Created gridtypes.Timestamp `json:"created"`
22+
State gridtypes.ResultState `json:"state"`
23+
Message string `json:"message"`
24+
}
25+
26+
type DeploymentGetResponse struct {
27+
Deployment gridtypes.Deployment `json:"deployment"`
28+
History []WorkloadTransaction `json:"history,omitempty"`
29+
}
30+
31+
func ParseDeploymentGetRequest(payload []byte) (DeploymentGetRequest, error) {
32+
var req DeploymentGetRequest
33+
if err := json.Unmarshal(payload, &req); err != nil {
34+
return req, err
35+
}
36+
return req, nil
37+
}
38+
39+
func DeploymentGet(ctx context.Context, deps Deps, req DeploymentGetRequest) (DeploymentGetResponse, error) {
40+
if req.TwinID == 0 {
41+
return DeploymentGetResponse{}, fmt.Errorf("twin_id is required")
42+
}
43+
if req.ContractID == 0 {
44+
return DeploymentGetResponse{}, fmt.Errorf("contract_id is required")
45+
}
46+
47+
deployment, err := deps.Provision.Get(ctx, req.TwinID, req.ContractID)
48+
if err != nil {
49+
return DeploymentGetResponse{}, err
50+
}
51+
if !req.WithHistory {
52+
return DeploymentGetResponse{Deployment: deployment}, nil
53+
}
54+
55+
history, err := deps.Provision.Changes(ctx, req.TwinID, req.ContractID)
56+
if err != nil {
57+
return DeploymentGetResponse{}, err
58+
}
59+
60+
transactions := make([]WorkloadTransaction, 0, len(history))
61+
for idx, wl := range history {
62+
transactions = append(transactions, WorkloadTransaction{
63+
Seq: idx + 1,
64+
Type: string(wl.Type),
65+
Name: string(wl.Name),
66+
Created: wl.Result.Created,
67+
State: wl.Result.State,
68+
Message: wl.Result.Error,
69+
})
70+
}
71+
72+
return DeploymentGetResponse{Deployment: deployment, History: transactions}, nil
73+
}

pkg/debugcmd/deployments_list.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package debugcmd
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
)
7+
8+
type DeploymentsListRequest struct {
9+
TwinID uint32 `json:"twin_id"`
10+
}
11+
12+
type DeploymentsListWorkload struct {
13+
Type string `json:"type"`
14+
Name string `json:"name"`
15+
State string `json:"state"`
16+
}
17+
18+
type DeploymentsListItem struct {
19+
TwinID uint32 `json:"twin_id"`
20+
ContractID uint64 `json:"contract_id"`
21+
Workloads []DeploymentsListWorkload `json:"workloads"`
22+
}
23+
24+
type DeploymentsListResponse struct {
25+
Items []DeploymentsListItem `json:"items"`
26+
}
27+
28+
func ParseDeploymentsListRequest(payload []byte) (DeploymentsListRequest, error) {
29+
var req DeploymentsListRequest
30+
if len(payload) == 0 {
31+
return req, nil
32+
}
33+
// optional payload
34+
_ = json.Unmarshal(payload, &req)
35+
return req, nil
36+
}
37+
38+
func DeploymentsList(ctx context.Context, deps Deps, req DeploymentsListRequest) (DeploymentsListResponse, error) {
39+
twins := []uint32{req.TwinID}
40+
if req.TwinID == 0 {
41+
var err error
42+
twins, err = deps.Provision.ListTwins(ctx)
43+
if err != nil {
44+
return DeploymentsListResponse{}, err
45+
}
46+
}
47+
48+
items := make([]DeploymentsListItem, 0)
49+
for _, twin := range twins {
50+
deployments, err := deps.Provision.List(ctx, twin)
51+
if err != nil {
52+
return DeploymentsListResponse{}, err
53+
}
54+
for _, d := range deployments {
55+
workloads := make([]DeploymentsListWorkload, 0, len(d.Workloads))
56+
for _, wl := range d.Workloads {
57+
workloads = append(workloads, DeploymentsListWorkload{
58+
Type: string(wl.Type),
59+
Name: string(wl.Name),
60+
State: string(wl.Result.State),
61+
})
62+
}
63+
items = append(items, DeploymentsListItem{
64+
TwinID: d.TwinID,
65+
ContractID: d.ContractID,
66+
Workloads: workloads,
67+
})
68+
}
69+
}
70+
71+
return DeploymentsListResponse{Items: items}, nil
72+
}

pkg/debugcmd/deps.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package debugcmd
2+
3+
import (
4+
"context"
5+
6+
"github.com/threefoldtech/zosbase/pkg"
7+
"github.com/threefoldtech/zosbase/pkg/gridtypes"
8+
"github.com/threefoldtech/zosbase/pkg/gridtypes/zos"
9+
)
10+
11+
// Provision is the subset of the provision zbus interface used by debug commands.
12+
type Provision interface {
13+
ListTwins(ctx context.Context) ([]uint32, error)
14+
List(ctx context.Context, twin uint32) ([]gridtypes.Deployment, error)
15+
Get(ctx context.Context, twin uint32, contract uint64) (gridtypes.Deployment, error)
16+
Changes(ctx context.Context, twin uint32, contract uint64) ([]gridtypes.Workload, error)
17+
}
18+
19+
// VM is the subset of the vmd zbus interface used by debug commands.
20+
type VM interface {
21+
Exists(ctx context.Context, id string) bool
22+
Inspect(ctx context.Context, id string) (pkg.VMInfo, error)
23+
Logs(ctx context.Context, id string) (string, error)
24+
LogsFull(ctx context.Context, id string) (string, error)
25+
}
26+
27+
// Network is the subset of the network zbus interface used by debug commands.
28+
type Network interface {
29+
Namespace(ctx context.Context, id zos.NetID) string
30+
}
31+
32+
type Deps struct {
33+
Provision Provision
34+
VM VM
35+
Network Network
36+
}

0 commit comments

Comments
 (0)