-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenstack.go
325 lines (274 loc) · 6.94 KB
/
openstack.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/mitchellh/go-homedir"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack"
"github.com/rackspace/gophercloud/openstack/compute/v2/servers"
"github.com/rackspace/gophercloud/openstack/identity/v2/tokens"
"github.com/rackspace/gophercloud/pagination"
log "github.com/sirupsen/logrus"
)
const (
CREDENTIAL_FILE = ".novassh"
CREDENTIAL_FILE_MODE = 0600
)
type machine struct {
Name string
Ipaddr string
Uuid string
}
func newMachine(s servers.Server, interfaceName string) (*machine, error) {
m := &machine{
Name: s.Name,
Uuid: s.ID,
}
// For ConoHa
for key, value := range s.Metadata {
if key == "instance_name_tag" {
m.Name = value.(string)
}
}
//
// Detect public IP address for connectiong SSH
//
// Try to detect any public IP addresses
for name, addressSet := range s.Addresses {
// Response of Rackspace API has the key either "public" or "private".
// Response of ConoHa API has the prefix either "ext-" or "local-"
if name == "public" || strings.HasPrefix(name, "ext-") || name == interfaceName {
as, ok := addressSet.([]interface{})
if !ok {
return nil, fmt.Errorf("Invalid address set(type assertion failed).")
}
for _, v := range as {
addr, ok := v.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("Invalid address set(type assertion failed).")
}
// TODO: support IPv6 address
if addr["version"] == 4.0 {
m.Ipaddr = addr["addr"].(string)
goto DETECTED
}
}
}
}
// Try to detect any private floating IP address.
for name, addressSet := range s.Addresses {
if name == "private" {
as, ok := addressSet.([]interface{})
if !ok {
return nil, fmt.Errorf("Invalid address set(type assertion failed).")
}
for _, v := range as {
addr, ok := v.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("Invalid address set(type assertion failed).")
}
if addr["OS-EXT-IPS:type"] == "floating" {
m.Ipaddr = addr["addr"].(string)
goto DETECTED
}
}
}
}
// Choose the first one when we can not detect.
for _, addressSet := range s.Addresses {
as, ok := addressSet.([]interface{})
if !ok {
return nil, fmt.Errorf("Invalid address set(type assertion failed).")
}
addr, ok := as[0].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("Invalid address set(type assertion failed).")
}
m.Ipaddr = addr["addr"].(string)
goto DETECTED
}
DETECTED:
return m, nil
}
type nova struct {
machines []*machine
provider *gophercloud.ServiceClient
networkInterface string
}
type Credential struct {
ComputeEndpoint string
*tokens.Token
}
func NewNova(nicname string) *nova {
nova := &nova{
networkInterface: nicname,
machines: nil,
}
return nova
}
func (n *nova) Init(authcache bool) (err error) {
// Credentials from env
opts, err := openstack.AuthOptionsFromEnv()
if err != nil {
return err
}
// Endpoint options
eo := gophercloud.EndpointOpts{
Type: "compute",
Region: os.Getenv("OS_REGION_NAME"),
}
// Try to use cached credential if file exists.
_, err = os.Stat(n.credentialCachePath())
if err == nil {
// Use cache file
strdata, err := ioutil.ReadFile(n.credentialCachePath())
if err != nil {
log.Warnf("Failed to load cache file: %v", err)
goto AUTH
}
cred := &Credential{Token: &tokens.Token{}}
if err = json.Unmarshal(strdata, cred); err != nil {
log.Warnf("Failed to unmarchal the cache data: %v", err)
goto AUTH
}
if cred.ExpiresAt.After(time.Now()) {
log.Debugf("Token has expired. Try to reauth.")
goto AUTH
}
client, err := openstack.NewClient(opts.IdentityEndpoint)
client.EndpointLocator = func(o gophercloud.EndpointOpts) (string, error) {
return cred.ComputeEndpoint, nil
}
client.TokenID = cred.ID
// Set service client
n.provider, err = openstack.NewComputeV2(client, eo)
return nil
}
AUTH:
client, err := openstack.AuthenticatedClient(opts)
if err != nil {
return err
}
// Set service client
n.provider, err = openstack.NewComputeV2(client, eo)
if n.provider == nil {
return fmt.Errorf("Could not found the Compute endpoint")
}
// Store credential to cache file
if authcache {
cred := &Credential{
ComputeEndpoint: n.provider.Endpoint,
Token: &tokens.Token{
ID: client.TokenID,
ExpiresAt: time.Now(),
},
}
strdata, err := json.Marshal(cred)
if err != nil {
return err
}
if err = ioutil.WriteFile(n.credentialCachePath(), strdata, CREDENTIAL_FILE_MODE); err != nil {
log.Warnf("Can not write the credential cache file: %s", n.credentialCachePath())
}
}
return nil
}
func (n *nova) Find(name string) (m *machine, err error) {
if n.machines == nil {
n.machines, err = n.List()
if err != nil {
return nil, err
}
}
for _, machine := range n.machines {
if strings.ToLower(machine.Name) == strings.ToLower(name) {
return machine, nil
}
}
return nil, nil
}
func (n *nova) List() ([]*machine, error) {
pager := servers.List(n.provider, servers.ListOpts{})
machines := []*machine{}
pager.EachPage(func(page pagination.Page) (bool, error) {
ss, err := servers.ExtractServers(page)
if err != nil {
return false, err
}
for _, s := range ss {
m, err := newMachine(s, n.networkInterface)
if err != nil {
return false, err
}
log.Debugf("Machine found: name=%s, ipaddr=%s", m.Name, m.Ipaddr)
for name, _ := range s.Addresses {
log.Debugf("InterfaceName: %s", name)
}
machines = append(machines, m)
}
return true, nil
})
return machines, nil
}
func (n *nova) GetConsoleUrl(m *machine) (string, error) {
data, err := json.Marshal(map[string]interface{}{
"os-getSerialConsole": map[string]string{
"type": "serial",
},
})
if err != nil {
return "", err
}
url := n.provider.ServiceURL("servers", m.Uuid, "action")
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/json")
for k, v := range n.provider.AuthenticatedHeaders() {
req.Header.Set(k, v)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
var response struct {
Console struct {
Type string `json:"type"`
Url string `json:"url"`
} `json:"console"`
}
if err := json.Unmarshal(body, &response); err != nil {
return "", err
}
return response.Console.Url, nil
}
func (n *nova) credentialCachePath() string {
d, err := homedir.Dir()
if err == nil {
return fmt.Sprintf("%s%c%s", d, filepath.Separator, CREDENTIAL_FILE)
} else {
return CREDENTIAL_FILE
}
}
func (n *nova) RemoveCredentialCache() error {
_, err := os.Stat(n.credentialCachePath())
if err == nil {
return os.Remove(n.credentialCachePath())
} else {
return nil
}
}