-
Notifications
You must be signed in to change notification settings - Fork 221
/
list.go
189 lines (160 loc) · 4.12 KB
/
list.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package machine
import (
"context"
"fmt"
"github.com/samber/lo"
"github.com/spf13/cobra"
fly "github.com/superfly/fly-go"
"github.com/superfly/fly-go/flaps"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/flapsutil"
"github.com/superfly/flyctl/internal/render"
"github.com/superfly/flyctl/iostreams"
)
func newList() *cobra.Command {
const (
short = "List Fly machines"
long = short + "\n"
usage = "list"
)
cmd := command.New(usage, short, long, runMachineList,
command.RequireSession,
command.RequireAppName,
)
cmd.Aliases = []string{"ls"}
cmd.Args = cobra.NoArgs
flag.Add(
cmd,
flag.App(),
flag.AppConfig(),
flag.JSONOutput(),
flag.Bool{
Name: "quiet",
Shorthand: "q",
Description: "Only list machine ids",
},
)
return cmd
}
func runMachineList(ctx context.Context) (err error) {
var (
appName = appconfig.NameFromContext(ctx)
io = iostreams.FromContext(ctx)
silence = flag.GetBool(ctx, "quiet")
cfg = config.FromContext(ctx)
)
flapsClient, err := flapsutil.NewClientWithOptions(ctx, flaps.NewClientOpts{
AppName: appName,
})
if err != nil {
return fmt.Errorf("list of machines could not be retrieved: %w", err)
}
machines, err := flapsClient.List(ctx, "")
if err != nil {
return fmt.Errorf("machines could not be retrieved")
}
if cfg.JSONOutput {
return render.JSON(io.Out, machines)
}
if len(machines) == 0 {
if !silence {
fmt.Fprintf(io.Out, "No machines are available on this app %s\n", appName)
}
return nil
}
rows := [][]string{}
listOfMachinesLink := io.CreateLink("View them in the UI here", fmt.Sprintf("https://fly.io/apps/%s/machines/", appName))
if !silence {
fmt.Fprintf(io.Out, "%d machines have been retrieved from app %s.\n%s\n\n", len(machines), appName, listOfMachinesLink)
}
if silence {
for _, machine := range machines {
rows = append(rows, []string{machine.ID})
}
_ = render.Table(io.Out, "", rows)
} else {
unreachableMachines := false
for _, machine := range machines {
var volName string
if machine.Config != nil && len(machine.Config.Mounts) > 0 {
volName = machine.Config.Mounts[0].Volume
}
appPlatform := ""
machineProcessGroup := ""
size := ""
if machine.Config != nil {
if platformVersion, ok := machine.Config.Metadata[fly.MachineConfigMetadataKeyFlyPlatformVersion]; ok {
appPlatform = platformVersion
}
if processGroup := machine.ProcessGroup(); processGroup != "" {
machineProcessGroup = processGroup
}
if machine.Config.Guest != nil {
size = fmt.Sprintf("%s:%dMB", machine.Config.Guest.ToSize(), machine.Config.Guest.MemoryMB)
}
}
note := ""
unreachable := machine.HostStatus == "unreachable"
if unreachable {
unreachableMachines = true
note = "*"
}
checksTotal := 0
checksPassing := 0
role := ""
for _, c := range machine.Checks {
checksTotal += 1
if c.Status == "passing" {
checksPassing += 1
}
if c.Name == "role" {
role = c.Output
}
}
checksSummary := ""
if checksTotal > 0 {
checksSummary = fmt.Sprintf("%d/%d", checksPassing, checksTotal)
}
rows = append(rows, []string{
machine.ID + note,
machine.Name,
machine.State,
lo.Ternary(unreachable, "", checksSummary),
machine.Region,
role,
lo.Ternary(unreachable, "", machine.ImageRefWithVersion()),
lo.Ternary(unreachable, "", machine.PrivateIP),
volName,
lo.Ternary(unreachable, "", machine.CreatedAt),
lo.Ternary(unreachable, "", machine.UpdatedAt),
appPlatform,
machineProcessGroup,
size,
})
}
headers := []string{
"ID",
"Name",
"State",
"Checks",
"Region",
"Role",
"Image",
"IP Address",
"Volume",
"Created",
"Last Updated",
"App Platform",
"Process Group",
"Size",
}
_ = render.Table(io.Out, appName, rows, headers...)
if unreachableMachines {
fmt.Fprintln(io.Out, "* These Machines' hosts could not be reached.")
}
}
return nil
}