-
Notifications
You must be signed in to change notification settings - Fork 234
/
status.go
96 lines (77 loc) · 2.26 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
96
package vm
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/render"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/logs"
)
func newStatus() *cobra.Command {
const (
short = "V1 APPS ONLY: Show a VM's status"
long = short + "\t" + "including logs, checks, and events." + "\n"
usage = "status <vm-id>"
)
cmd := command.New(usage, short, long, runStatus,
command.RequireSession,
command.RequireAppName,
)
cmd.Args = cobra.ExactArgs(1)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
)
return cmd
}
func runStatus(ctx context.Context) (err error) {
var (
io = iostreams.FromContext(ctx)
appName = appconfig.NameFromContext(ctx)
client = client.FromContext(ctx).API()
logLimit = 25
)
// vm status is not supported for machines
isMachine, err := command.IsMachinesPlatform(ctx, appName)
if err != nil {
return fmt.Errorf("failed to check platform version %w", err)
}
if isMachine {
return fmt.Errorf("it looks like your app is running on v2 of our platform, and does not support this legacy command: try running fly machine status instead")
}
allocID := flag.FirstArg(ctx)
alloc, err := client.GetAllocationStatus(ctx, appName, allocID, logLimit)
if err != nil {
return fmt.Errorf("failed to fetch allocation status: %w", err)
}
if alloc == nil {
return fmt.Errorf("allocation '%s' was not found in app '%s'", allocID, appName)
}
if err = render.AllocationStatus(io.Out, "Instance", alloc); err != nil {
return
}
if err = render.AllocationEvents(io.Out, "Events", alloc.Events...); err != nil {
return
}
if err = render.AllocationChecks(io.Out, "Checks", alloc.Checks...); err != nil {
return
}
// render recent logs
var entries []logs.LogEntry
// convert alloc.RecentLogs to type logs.LogEntry and add them to entries
for _, e := range alloc.RecentLogs {
entries = append(entries, logs.LogEntry{
Timestamp: e.Timestamp,
Message: e.Message,
Level: e.Level,
})
}
if err = render.AllocationLogs(io.Out, "Recent Logs", entries); err != nil {
return
}
return
}