-
Notifications
You must be signed in to change notification settings - Fork 265
/
container_run.go
126 lines (109 loc) · 3.69 KB
/
container_run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package dashboardexecute
import (
"context"
"fmt"
"github.com/turbot/steampipe/pkg/dashboard/dashboardtypes"
"github.com/turbot/steampipe/pkg/steampipeconfig/modconfig"
"log"
)
// DashboardContainerRun is a struct representing a container run
type DashboardContainerRun struct {
DashboardParentImpl
dashboardNode *modconfig.DashboardContainer
}
func (r *DashboardContainerRun) AsTreeNode() *dashboardtypes.SnapshotTreeNode {
res := &dashboardtypes.SnapshotTreeNode{
Name: r.Name,
NodeType: r.NodeType,
Children: make([]*dashboardtypes.SnapshotTreeNode, len(r.children)),
}
for i, c := range r.children {
res.Children[i] = c.AsTreeNode()
}
return res
}
func NewDashboardContainerRun(container *modconfig.DashboardContainer, parent dashboardtypes.DashboardParent, executionTree *DashboardExecutionTree) (*DashboardContainerRun, error) {
children := container.GetChildren()
r := &DashboardContainerRun{dashboardNode: container}
// create NewDashboardTreeRunImpl
// (we must create after creating the run as it requires a ref to the run)
// TODO [node_reuse] do this a different way https://github.com/turbot/steampipe/issues/2919
r.DashboardTreeRunImpl = NewDashboardTreeRunImpl(container, parent, r, executionTree)
if container.Title != nil {
r.Title = *container.Title
}
if container.Width != nil {
r.Width = *container.Width
}
r.childCompleteChan = make(chan dashboardtypes.DashboardTreeRun, len(children))
for _, child := range children {
var childRun dashboardtypes.DashboardTreeRun
var err error
switch i := child.(type) {
case *modconfig.DashboardContainer:
childRun, err = NewDashboardContainerRun(i, r, executionTree)
if err != nil {
return nil, err
}
case *modconfig.Dashboard:
childRun, err = NewDashboardRun(i, r, executionTree)
if err != nil {
return nil, err
}
case *modconfig.Benchmark, *modconfig.Control:
childRun, err = NewCheckRun(i.(modconfig.DashboardLeafNode), r, executionTree)
if err != nil {
return nil, err
}
default:
// ensure this item is a DashboardLeafNode
leafNode, ok := i.(modconfig.DashboardLeafNode)
if !ok {
return nil, fmt.Errorf("child %s does not implement DashboardLeafNode", i.Name())
}
childRun, err = NewLeafRun(leafNode, r, executionTree)
if err != nil {
return nil, err
}
}
// should never happen - container children must be either container or counter
if childRun == nil {
continue
}
// if our child has not completed, we have not completed
if childRun.GetRunStatus() == dashboardtypes.RunInitialized {
r.Status = dashboardtypes.RunInitialized
}
r.children = append(r.children, childRun)
}
// add r into execution tree
executionTree.runs[r.Name] = r
return r, nil
}
// Initialise implements DashboardTreeRun
func (r *DashboardContainerRun) Initialise(ctx context.Context) {
// initialise our children
if err := r.initialiseChildren(ctx); err != nil {
r.SetError(ctx, err)
}
}
// Execute implements DashboardTreeRun
// execute all children and wait for them to complete
func (r *DashboardContainerRun) Execute(ctx context.Context) {
// execute all children asynchronously
r.executeChildrenAsync(ctx)
// try to set status as running (will be set to blocked if any children are blocked)
r.setRunning(ctx)
// wait for children to complete
err := <-r.waitForChildrenAsync(ctx)
if err == nil {
log.Printf("[TRACE] %s Execute waitForChildrenAsync returned success", r.Name)
// set complete status on dashboard
r.SetComplete(ctx)
} else {
log.Printf("[TRACE] %s Execute waitForChildrenAsync returned err %s", r.Name, err.Error())
r.SetError(ctx, err)
}
}
// IsSnapshotPanel implements SnapshotPanel
func (*DashboardContainerRun) IsSnapshotPanel() {}