-
Notifications
You must be signed in to change notification settings - Fork 224
/
status.go
143 lines (117 loc) · 3.22 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
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
package machine
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/alecthomas/chroma/quick"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/flaps"
"github.com/superfly/flyctl/internal/app"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/render"
"github.com/superfly/flyctl/iostreams"
)
func newStatus() *cobra.Command {
const (
short = "Show current status of a running machine"
long = short + "\n"
usage = "status <id>"
)
cmd := command.New(usage, short, long, runMachineStatus,
command.RequireSession,
command.LoadAppNameIfPresent,
)
cmd.Args = cobra.ExactArgs(1)
flag.Add(
cmd,
flag.App(),
flag.AppConfig(),
flag.Bool{
Name: "display-config",
Description: "Display the machine config as JSON",
Shorthand: "d",
},
)
return cmd
}
func runMachineStatus(ctx context.Context) (err error) {
var (
io = iostreams.FromContext(ctx)
)
var (
appName = app.NameFromContext(ctx)
machineID = flag.FirstArg(ctx)
)
app, err := appFromMachineOrName(ctx, machineID, appName)
if err != nil {
return err
}
flapsClient, err := flaps.New(ctx, app)
if err != nil {
return fmt.Errorf("could not make flaps client: %w", err)
}
machine, err := flapsClient.Get(ctx, machineID)
if err != nil {
switch {
case strings.Contains(err.Error(), "status"):
return fmt.Errorf("retrieve machine failed %s", err)
default:
return fmt.Errorf("machine %s could not be retrieved", machineID)
}
}
fmt.Fprintf(io.Out, "Machine ID: %s\n", machine.ID)
fmt.Fprintf(io.Out, "Instance ID: %s\n", machine.InstanceID)
fmt.Fprintf(io.Out, "State: %s\n\n", machine.State)
obj := [][]string{
{
machine.ID,
machine.InstanceID,
machine.State,
machine.FullImageRef(),
machine.Name,
machine.PrivateIP,
machine.Region,
machine.CreatedAt,
machine.UpdatedAt,
},
}
var cols []string = []string{"ID", "Instance ID", "State", "Image", "Name", "Private IP", "Region", "Created", "Updated"}
if len(machine.Config.Mounts) > 0 {
cols = append(cols, "Volume")
obj[0] = append(obj[0], machine.Config.Mounts[0].Volume)
}
if err = render.VerticalTable(io.Out, "VM", obj, cols...); err != nil {
return
}
eventLogs := [][]string{}
for _, event := range machine.Events {
timeInUTC := time.Unix(0, event.Timestamp*int64(time.Millisecond))
fields := []string{
event.Status,
event.Type,
event.Source,
timeInUTC.Format(time.RFC3339Nano),
}
if event.Request != nil && event.Request.ExitEvent != nil {
exitEvent := event.Request.ExitEvent
fields = append(fields, fmt.Sprintf("exit_code=%d,oom_killed=%t,requested_stop=%t",
exitEvent.GuestExitCode, exitEvent.OOMKilled, exitEvent.RequestedStop))
}
eventLogs = append(eventLogs, fields)
}
_ = render.Table(io.Out, "Event Logs", eventLogs, "State", "Event", "Source", "Timestamp", "Info")
if flag.GetBool(ctx, "display-config") {
var prettyConfig []byte
prettyConfig, err = json.MarshalIndent(machine.Config, "", " ")
if err != nil {
return err
}
fmt.Fprint(io.Out, "\nConfig:\n")
err = quick.Highlight(io.Out, string(prettyConfig), "json", "terminal", "monokai")
fmt.Fprintln(io.Out)
}
return
}