-
Notifications
You must be signed in to change notification settings - Fork 235
/
vm.go
128 lines (104 loc) · 2.91 KB
/
vm.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
package cmd
import (
"fmt"
"io"
"github.com/logrusorgru/aurora"
"github.com/segmentio/textio"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/cmd/presenters"
"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/docstrings"
"github.com/superfly/flyctl/logs"
)
func newVMCommand(client *client.Client) *Command {
vmCmd := BuildCommandKS(nil, nil, docstrings.Get("vm"), client)
vmRestartCmd := BuildCommandKS(vmCmd, runVMRestart, docstrings.Get("vm.restart"), client, requireSession, requireAppName)
vmRestartCmd.Args = cobra.ExactArgs(1)
vmStopCmd := BuildCommandKS(vmCmd, runVMStop, docstrings.Get("vm.stop"), client, requireSession, requireAppName)
vmStopCmd.Args = cobra.ExactArgs(1)
vmStatusCmd := BuildCommandKS(vmCmd, runVMStatus, docstrings.Get("vm.status"), client, requireSession, requireAppName)
vmStatusCmd.Args = cobra.ExactArgs(1)
return vmCmd
}
func runVMRestart(cmdctx *cmdctx.CmdContext) error {
ctx := cmdctx.Command.Context()
appName := cmdctx.AppName
allocID := cmdctx.Args[0]
err := cmdctx.Client.API().RestartAllocation(ctx, appName, allocID)
if err != nil {
return err
}
fmt.Printf("VM %s is being restarted\n", allocID)
return nil
}
func runVMStop(cmdctx *cmdctx.CmdContext) error {
ctx := cmdctx.Command.Context()
appName := cmdctx.AppName
allocID := cmdctx.Args[0]
err := cmdctx.Client.API().StopAllocation(ctx, appName, allocID)
if err != nil {
return err
}
fmt.Printf("VM %s is being stopped\n", allocID)
return nil
}
func runVMStatus(cmdCtx *cmdctx.CmdContext) error {
ctx := cmdCtx.Command.Context()
alloc, err := cmdCtx.Client.API().GetAllocationStatus(ctx, cmdCtx.AppName, cmdCtx.Args[0], 25)
if err != nil {
return err
}
if alloc == nil {
return api.ErrNotFound
}
err = cmdCtx.Frender(
cmdctx.PresenterOption{
Title: "Instance",
Presentable: &presenters.Allocations{
Allocations: []*api.AllocationStatus{alloc},
},
Vertical: true,
},
cmdctx.PresenterOption{
Title: "Recent Events",
Presentable: &presenters.AllocationEvents{
Events: alloc.Events,
},
},
cmdctx.PresenterOption{
Title: "Checks",
Presentable: &presenters.AllocationChecks{
Checks: alloc.Checks,
},
},
)
if err != nil {
return err
}
var p io.Writer
var pw *textio.PrefixWriter
if !cmdCtx.OutputJSON() {
fmt.Println(aurora.Bold("Recent Logs"))
pw = textio.NewPrefixWriter(cmdCtx.Out, " ")
p = pw
} else {
p = cmdCtx.Out
}
logPresenter := presenters.LogPresenter{HideAllocID: true, HideRegion: true, RemoveNewlines: true}
for _, entry := range alloc.RecentLogs {
logPresenter.FPrint(p, cmdCtx.OutputJSON(), logs.LogEntry{
Level: entry.Level,
Instance: entry.Instance,
Message: entry.Message,
Region: entry.Region,
Timestamp: entry.Timestamp,
Meta: entry.Meta,
})
}
if p != cmdCtx.Out {
_ = pw.Flush()
}
return nil
}