-
Notifications
You must be signed in to change notification settings - Fork 221
/
instance.go
149 lines (121 loc) · 2.96 KB
/
instance.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package status
import (
"bytes"
"context"
"fmt"
"io"
"strconv"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/internal/app"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/format"
"github.com/superfly/flyctl/internal/render"
)
func newInstance() (cmd *cobra.Command) {
const (
long = `Show the instance's current status including checks and events.
`
short = "Show instance status"
usage = "instance <INSTANCE_ID>"
)
cmd = command.New(usage, short, long, runInstance,
command.RequireSession,
command.RequireAppName,
)
cmd.Args = cobra.ExactArgs(1)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
)
return
}
func runInstance(ctx context.Context) (err error) {
var (
appName = app.NameFromContext(ctx)
allocID = flag.FirstArg(ctx)
client = client.FromContext(ctx).API()
)
var alloc *api.AllocationStatus
if alloc, err = client.GetAllocationStatus(ctx, appName, allocID, 25); err != nil {
err = fmt.Errorf("failed retrieving allocation status for %s: %w", allocID, err)
return
} else if alloc == nil {
err = fmt.Errorf("could not find allocation with ID %s for app %s", allocID, appName)
return
}
out := iostreams.FromContext(ctx).Out
if config.FromContext(ctx).JSONOutput {
// TODO: checks & recent events are being outputted twice
err = render.JSON(out,
map[string]interface{}{
"Instance": alloc,
"Recent Events": alloc.Events,
"Checks": alloc.Checks,
},
)
return
}
var buf bytes.Buffer
if err = renderAllocationStatus(&buf, alloc); err != nil {
err = fmt.Errorf("failed rendering allocation status: %w", err)
return
}
if err = render.AllocationEvents(&buf, "Recent Events", alloc.Events...); err != nil {
err = fmt.Errorf("failed rendering allocation events: %w", err)
return
}
if err = renderChecks(&buf, alloc.Checks); err != nil {
err = fmt.Errorf("failed rendering checks: %w", err)
return
}
_, err = buf.WriteTo(out)
return
}
func renderAllocationStatus(w io.Writer, as *api.AllocationStatus) error {
obj := [][]string{
{
as.IDShort,
as.TaskName,
strconv.Itoa(as.Version),
as.Region,
as.DesiredStatus,
format.AllocStatus(as),
format.HealthChecksSummary(as),
strconv.Itoa(as.Restarts),
format.RelativeTime(as.CreatedAt),
},
}
return render.VerticalTable(w, "Instance", obj,
"ID",
"Process",
"Version",
"Region",
"Desired",
"Status",
"Health Checks",
"Restarts",
"Created",
)
}
func renderChecks(w io.Writer, checks []api.CheckState) error {
var rows [][]string
for _, check := range checks {
rows = append(rows, []string{
check.Name,
check.ServiceName,
check.Status,
check.Output,
})
}
return render.Table(w, "Checks", rows,
"ID",
"Service",
"State",
"Output",
)
}