-
Notifications
You must be signed in to change notification settings - Fork 20
/
vm_show.go
172 lines (146 loc) · 4.5 KB
/
vm_show.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
package cmd
import (
"encoding/base64"
"fmt"
"os"
"strings"
"github.com/dustin/go-humanize"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
type vmShowOutput struct {
ID string `json:"id"`
Name string `json:"name"`
CreationDate string `json:"creation_date"`
Size string `json:"size"`
DiskSize string `json:"disk_size"`
Template string `json:"template"`
Zone string `json:"zone"`
State string `json:"state"`
IPAddress string `json:"ip_address"`
ReverseDNS string `json:"reverse_dns"`
Username string `json:"username"`
SSHKey string `json:"ssh_key"`
SecurityGroups []string `json:"security_groups,omitempty"`
AntiAffinityGroups []string `json:"antiaffinity_groups,omitempty" outputLabel:"Anti-Affinity Groups"`
PrivateNetworks []string `json:"private_networks,omitempty"`
}
func (o *vmShowOutput) Type() string { return "Instance" }
func (o *vmShowOutput) toJSON() { outputJSON(o) }
func (o *vmShowOutput) toText() { outputText(o) }
func (o *vmShowOutput) toTable() { outputTable(o) }
func init() {
vmShowCmd := &cobra.Command{
Use: "show NAME|ID",
Short: "Show a Compute instance details",
Long: fmt.Sprintf(`This command shows a Compute instance details.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&vmShowOutput{}), ", ")),
Aliases: gShowAlias,
ValidArgsFunction: completeVMNames,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return cmd.Usage()
}
userDataOnly, err := cmd.Flags().GetBool("user-data")
if err != nil {
return err
}
if userDataOnly {
return showVMUserData(args[0])
}
return output(showVM(args[0]))
},
}
vmShowCmd.Flags().Bool("user-data", false, "Show current cloud-init user data configuration")
vmCmd.AddCommand(vmShowCmd)
}
func showVM(name string) (outputter, error) {
vm, err := getVirtualMachineByNameOrID(name)
if err != nil {
return nil, err
}
resp, err := cs.GetWithContext(gContext, &egoscale.Template{
IsFeatured: true,
ID: vm.TemplateID,
ZoneID: vm.ZoneID,
})
if err != nil {
return nil, err
}
template := resp.(*egoscale.Template)
resp, err = cs.GetWithContext(gContext, &egoscale.Volume{
VirtualMachineID: vm.ID,
Type: "ROOT",
})
if err != nil {
return nil, err
}
volume := resp.(*egoscale.Volume)
reverseDNS, err := cs.RequestWithContext(gContext, &egoscale.QueryReverseDNSForVirtualMachine{ID: vm.ID})
if err != nil {
return nil, err
}
out := vmShowOutput{
ID: vm.ID.String(),
Name: vm.DisplayName,
CreationDate: vm.Created,
Size: vm.ServiceOfferingName,
Template: vm.TemplateName,
Zone: vm.ZoneName,
State: vm.State,
DiskSize: humanize.IBytes(volume.Size),
IPAddress: vm.IP().String(),
ReverseDNS: func(vm *egoscale.VirtualMachine) string {
if len(vm.DefaultNic().ReverseDNS) > 0 {
return vm.DefaultNic().ReverseDNS[0].DomainName
}
return "n/a"
}(reverseDNS.(*egoscale.VirtualMachine)),
Username: "n/a",
SSHKey: vm.KeyPair,
SecurityGroups: make([]string, len(vm.SecurityGroup)),
AntiAffinityGroups: make([]string, len(vm.AffinityGroup)),
PrivateNetworks: make([]string, 0),
}
for i, sg := range vm.SecurityGroup {
out.SecurityGroups[i] = sg.Name
}
for i, aag := range vm.AffinityGroup {
out.AntiAffinityGroups[i] = aag.Name
}
for _, nic := range vm.Nic {
if nic.IsDefault {
continue
}
out.PrivateNetworks = append(out.PrivateNetworks, nic.NetworkName)
}
if username, ok := template.Details["username"]; ok {
out.Username = username
}
// If a single-use SSH keypair has been created for this instance,
// report the private key file location instead of the API SSH key name.
sshKeyPath := getKeyPairPath(vm.ID.String())
if _, err := os.Stat(sshKeyPath); err == nil && out.SSHKey == "" {
out.SSHKey = sshKeyPath
}
return &out, nil
}
func showVMUserData(name string) error {
vm, err := getVirtualMachineByNameOrID(name)
if err != nil {
return err
}
resp, err := cs.SyncRequestWithContext(gContext, &egoscale.GetVirtualMachineUserData{
VirtualMachineID: vm.ID,
})
if err != nil {
return err
}
userData, err := base64.StdEncoding.DecodeString(resp.(*egoscale.VirtualMachineUserData).UserData)
if err != nil {
return err
}
fmt.Println(string(userData))
return nil
}