forked from krystal/guvnor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
95 lines (80 loc) · 1.93 KB
/
status.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
package guvnor
import (
"context"
"fmt"
"sort"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
)
type StatusArgs struct {
ServiceName string
}
type ContainerStatus struct {
ContainerName string
ContainerID string
Status string
}
type ProcessStatus struct {
WantReplicas int
Containers []ContainerStatus
}
type StatusResult struct {
DeploymentID int
LastDeployedAt time.Time
Processes ProcessStatuses
}
type ProcessStatuses map[string]ProcessStatus
func (ps ProcessStatuses) OrderedKeys() []string {
keys := make([]string, 0, len(ps))
for k := range ps {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func (e *Engine) Status(
ctx context.Context, args StatusArgs,
) (*StatusResult, error) {
svc, err := e.loadServiceConfig(args.ServiceName)
if err != nil {
return nil, err
}
svcState, err := e.state.LoadServiceState(svc.Name)
if err != nil {
return nil, err
}
e.log.Debug("fetching container list for service")
containers, err := e.docker.ContainerList(ctx, types.ContainerListOptions{
All: true,
Filters: filters.NewArgs(
filters.Arg("label", fmt.Sprintf("%s=%s", serviceLabel, svc.Name)),
),
})
if err != nil {
return nil, err
}
processStatuses := map[string]ProcessStatus{}
for processName, process := range svc.Processes {
ps := ProcessStatus{
WantReplicas: process.GetQuantity(),
Containers: []ContainerStatus{},
}
for _, container := range containers {
containerProcess := container.Labels[processLabel]
if containerProcess == processName {
ps.Containers = append(ps.Containers, ContainerStatus{
ContainerName: container.Names[0],
ContainerID: container.ID,
Status: container.State,
})
}
}
processStatuses[processName] = ps
}
return &StatusResult{
DeploymentID: svcState.DeploymentID,
LastDeployedAt: svcState.LastDeployedAt,
Processes: processStatuses,
}, nil
}