-
Notifications
You must be signed in to change notification settings - Fork 221
/
status.go
127 lines (104 loc) · 3.04 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
package machine
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/alecthomas/chroma/quick"
"github.com/spf13/cobra"
"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.RangeArgs(0, 1)
flag.Add(
cmd,
flag.App(),
flag.AppConfig(),
selectFlag,
flag.Bool{
Name: "display-config",
Description: "Display the machine config as JSON",
Shorthand: "d",
},
)
return cmd
}
func runMachineStatus(ctx context.Context) (err error) {
io := iostreams.FromContext(ctx)
machineID := flag.FirstArg(ctx)
haveMachineID := len(flag.Args(ctx)) > 0
machine, ctx, err := selectOneMachine(ctx, nil, machineID, haveMachineID)
if err != nil {
return err
}
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.ImageRefWithVersion(),
machine.Name,
machine.PrivateIP,
machine.Region,
machine.ProcessGroup(),
fmt.Sprint(machine.Config.Guest.CPUKind),
fmt.Sprint(machine.Config.Guest.CPUs),
fmt.Sprint(machine.Config.Guest.MemoryMB),
machine.CreatedAt,
machine.UpdatedAt,
strings.Join(machine.Config.Init.Cmd, " "),
},
}
var cols []string = []string{"ID", "Instance ID", "State", "Image", "Name", "Private IP", "Region", "Process Group", "CPU Kind", "vCPUs", "Memory", "Created", "Updated", "Command"}
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.ExitCode, 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
}