-
Notifications
You must be signed in to change notification settings - Fork 227
/
monitor.go
135 lines (111 loc) · 3.64 KB
/
monitor.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
127
128
129
130
131
132
133
134
135
package cmd
import (
"context"
"fmt"
"os"
"sync"
"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/cmd/presenters"
"github.com/superfly/flyctl/docstrings"
"github.com/superfly/flyctl/internal/deployment"
)
func newMonitorCommand() *Command {
ks := docstrings.Get("monitor")
return BuildCommand(nil, runMonitor, ks.Usage, ks.Short, ks.Long, os.Stdout, requireSession, requireAppName)
}
func runMonitor(commandContext *cmdctx.CmdContext) error {
//var oldds *api.DeploymentStatus
app, err := commandContext.Client.API().GetApp(commandContext.AppName)
if err != nil {
return fmt.Errorf("Failed to get app from context")
}
commandContext.Statusf("monitor", cmdctx.STITLE, "Monitoring Deployments for %s\n", app.Name)
for {
monitorDeployment(context.Background(), commandContext)
}
}
func monitorDeployment(ctx context.Context, commandContext *cmdctx.CmdContext) error {
monitor := deployment.NewDeploymentMonitor(commandContext.Client.API(), commandContext.AppName)
monitor.DeploymentStarted = func(idx int, d *api.DeploymentStatus) error {
if idx > 0 {
commandContext.StatusLn()
}
commandContext.Status("monitor", cmdctx.SINFO, presenters.FormatDeploymentSummary(d))
return nil
}
monitor.DeploymentUpdated = func(d *api.DeploymentStatus, updatedAllocs []*api.AllocationStatus) error {
commandContext.Status("monitor", cmdctx.SINFO, presenters.FormatDeploymentAllocSummary(d))
if commandContext.GlobalConfig.GetBool("verbose") {
for _, alloc := range updatedAllocs {
commandContext.Status("monitor", cmdctx.SINFO, presenters.FormatAllocSummary(alloc))
}
}
return nil
}
monitor.DeploymentFailed = func(d *api.DeploymentStatus, failedAllocs []*api.AllocationStatus) error {
commandContext.Statusf("monitor", cmdctx.SINFO, "v%d %s - %s\n", d.Version, d.Status, d.Description)
if len(failedAllocs) > 0 {
commandContext.StatusLn()
commandContext.Status("monitor", cmdctx.SERROR, "Failed Allocations")
x := make(chan *api.AllocationStatus)
var wg sync.WaitGroup
wg.Add(len(failedAllocs))
for _, a := range failedAllocs {
a := a
go func() {
defer wg.Done()
alloc, err := commandContext.Client.API().GetAllocationStatus(commandContext.AppName, a.ID, 20)
if err != nil {
commandContext.Status("monitor", cmdctx.SERROR, "Error fetching alloc", a.ID, err)
return
}
x <- alloc
}()
}
go func() {
wg.Wait()
close(x)
}()
count := 0
for alloc := range x {
count++
commandContext.Statusf("monitor", cmdctx.SERROR, "\n Failure #%d\n", count)
err := commandContext.FrenderPrefix(" ",
cmdctx.PresenterOption{
Title: "Allocation",
Presentable: &presenters.Allocations{
Allocations: []*api.AllocationStatus{alloc},
},
Vertical: true,
},
cmdctx.PresenterOption{
Title: "Recent Events",
Presentable: &presenters.AllocationEvents{
Events: alloc.Events,
},
},
)
if err != nil {
return err
}
commandContext.Status("monitor", cmdctx.STITLE, "Recent Logs")
logPresenter := presenters.LogPresenter{HideAllocID: true, HideRegion: true, RemoveNewlines: true}
logPresenter.FPrint(commandContext.Out, commandContext.OutputJSON(), alloc.RecentLogs)
}
}
return nil
}
monitor.DeploymentSucceeded = func(d *api.DeploymentStatus) error {
fmt.Fprintf(commandContext.Out, "v%d deployed successfully\n", d.Version)
return nil
}
monitor.Start(ctx)
if err := monitor.Error(); err != nil {
fmt.Fprintf(commandContext.Out, "Monitor Error: %s", err)
}
if !monitor.Success() {
return ErrAbort
}
return nil
}