-
Notifications
You must be signed in to change notification settings - Fork 21
/
ps.go
207 lines (192 loc) · 5.45 KB
/
ps.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (c) 2016 by António Meireles <antonio.meireles@reformi.st>.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package main
import (
"encoding/json"
"fmt"
"os"
"text/tabwriter"
"github.com/TheNewNormal/corectl/components/host/session"
"github.com/TheNewNormal/corectl/components/server"
"github.com/TheNewNormal/corectl/release"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
)
var (
psCmd = &cobra.Command{
Use: "ps",
Short: "Lists running CoreOS instances",
PreRunE: defaultPreRunE,
RunE: psCommand,
}
queryCmd = &cobra.Command{
Use: "query [VMids]",
Aliases: []string{"q"},
Short: "Display information about the running CoreOS instances",
PreRunE: func(cmd *cobra.Command, args []string) (err error) {
session.Caller.CmdLine.BindPFlags(cmd.Flags())
if (session.Caller.CmdLine.GetBool("ip") ||
session.Caller.CmdLine.GetBool("tty") ||
session.Caller.CmdLine.GetBool("online") ||
session.Caller.CmdLine.GetBool("up") ||
session.Caller.CmdLine.GetBool("uuid") ||
session.Caller.CmdLine.GetBool("log")) && len(args) != 1 {
err = fmt.Errorf("Incorrect Usage: only one argument " +
"expected (a VM's name or UUID)")
}
return err
},
RunE: queryCommand,
}
)
func psCommand(cmd *cobra.Command, args []string) (err error) {
var (
cli = session.Caller.CmdLine
reply = &server.RPCreply{}
srv *release.Info
pp []byte
)
if srv, err = server.Daemon.Running(); err != nil {
return
}
if reply, err = server.RPCQuery("ActiveVMs", &server.RPCquery{}); err != nil {
return
}
running := reply.Running
if cli.GetBool("json") {
if pp, err = json.MarshalIndent(running, "", " "); err == nil {
fmt.Println(string(pp))
}
return
}
fmt.Println("\nServer:")
srv.PrettyPrint(true)
totalV, totalM, totalC := len(running), 0, 0
for _, vm := range running {
totalC, totalM = totalC+vm.Cpus, totalM+vm.Memory
}
fmt.Printf("Activity:\n Active VMs:\t%v\n "+
"Total Memory:\t%v\n Total vCores:\t%v\n",
totalV, totalM, totalC)
for _, vm := range running {
vm.PrettyPrint()
}
return
}
func queryCommand(cmd *cobra.Command, args []string) (err error) {
var (
pp []byte
reply = &server.RPCreply{}
cli = session.Caller.CmdLine
selected map[string]*server.VMInfo
vm *server.VMInfo
tabP = func(selected map[string]*server.VMInfo) {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 5, 0, 1, ' ', 0)
fmt.Fprintf(w, "name\tchannel/version\tip\tonline\tcpu(s)\tram\t"+
"uuid\tpid\tuptime\tvols\n")
for _, vm := range selected {
fmt.Fprintf(w, "%v\t%v/%v\t%v\t%t\t%v\t%v\t%v\t%v\t%v\t%v\n",
vm.Name, vm.Channel, vm.Version, vm.PublicIP,
vm.NotIsolated, vm.Cpus, vm.Memory, vm.UUID, vm.Pid,
humanize.Time(vm.CreationTime), len(vm.Storage.HardDrives))
}
w.Flush()
}
)
if _, err = server.Daemon.Running(); err != nil {
return session.ErrServerUnreachable
}
if reply, err =
server.RPCQuery("ActiveVMs", &server.RPCquery{}); err != nil {
return
}
running := reply.Running
if len(args) == 1 {
if vm, err = vmInfo(args[0]); err != nil {
if cli.GetBool("up") {
fmt.Println(false)
return nil
}
return
}
if cli.GetBool("ip") {
fmt.Println(vm.PublicIP)
return
} else if cli.GetBool("uuid") {
fmt.Println(vm.UUID)
return
} else if cli.GetBool("tty") {
fmt.Println(vm.TTY())
return
} else if cli.GetBool("log") {
fmt.Println(vm.Log())
return
} else if cli.GetBool("online") {
fmt.Println(vm.NotIsolated)
return
} else if cli.GetBool("up") {
fmt.Println(true)
return
}
}
if len(args) == 0 {
selected = running
} else {
selected = make(map[string]*server.VMInfo)
for _, target := range args {
if vm, err = vmInfo(target); err != nil {
return
}
selected[vm.UUID] = vm
}
}
if cli.GetBool("json") {
if pp, err = json.MarshalIndent(selected, "", " "); err == nil {
fmt.Println(string(pp))
}
} else if cli.GetBool("all") {
tabP(selected)
} else {
for _, vm := range selected {
fmt.Println(vm.Name)
}
}
return
}
func init() {
psCmd.Flags().BoolP("json", "j", false,
"outputs in JSON for easy 3rd party integration")
queryCmd.Flags().BoolP("json", "j", false,
"outputs in JSON for easy 3rd party integration")
queryCmd.Flags().BoolP("all", "a", false,
"display a table with extended information about running "+
"CoreOS instances")
queryCmd.Flags().BoolP("ip", "i", false,
"displays given instance IP address")
queryCmd.Flags().BoolP("tty", "t", false,
"displays given instance tty's location")
queryCmd.Flags().BoolP("log", "l", false,
"displays given instance boot logs location")
queryCmd.Flags().BoolP("online", "o", false,
"tells if at boot time VM had connectivity to outter world")
queryCmd.Flags().BoolP("up", "u", false,
"tells if a given VM is up or not")
queryCmd.Flags().BoolP("uuid", "U", false,
"returns VM's UUID")
if session.AppName() != "corectld" {
rootCmd.AddCommand(psCmd, queryCmd)
}
}