forked from linuxkit/linuxkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider_openstack.go
134 lines (113 loc) · 3.63 KB
/
provider_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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"time"
)
// ProviderOpenstack is the type implementing the Provider interface for OpenStack
type ProviderOpenstack struct {
}
// NewOpenstack returns a new ProviderOpenstack
func NewOpenstack() *ProviderOpenstack {
return &ProviderOpenstack{}
}
func (p *ProviderOpenstack) String() string {
return "openstack"
}
// Probe checks if we are running on OpenStack
func (p *ProviderOpenstack) Probe() bool {
// Getting the hostname should always work...
_, err := openstackGet(metaDataURL + "hostname")
return (err == nil)
}
// Extract gets both the OpenStack specific and generic userdata
func (p *ProviderOpenstack) Extract() ([]byte, error) {
// Get host name. This must not fail
hostname, err := openstackGet(metaDataURL + "hostname")
if err != nil {
return nil, err
}
err = ioutil.WriteFile(path.Join(ConfigPath, Hostname), hostname, 0644)
if err != nil {
return nil, fmt.Errorf("OpenStack: Failed to write hostname: %s", err)
}
// public ipv4
openstackMetaGet("public-ipv4", "public_ipv4", 0644)
// private ipv4
openstackMetaGet("local-ipv4", "local_ipv4", 0644)
// availability zone
openstackMetaGet("placement/availability-zone", "availability_zone", 0644)
// instance type
openstackMetaGet("instance-type", "instance_type", 0644)
// instance-id
openstackMetaGet("instance-id", "instance_id", 0644)
// local-hostname
openstackMetaGet("local-hostname", "local_hostname", 0644)
// ssh
if err := p.handleSSH(); err != nil {
log.Printf("OpenStack: Failed to get ssh data: %s", err)
}
// Generic userdata
userData, err := openstackGet(userDataURL)
if err != nil {
log.Printf("OpenStack: Failed to get user-data: %s", err)
// This is not an error
return nil, nil
}
return userData, nil
}
// lookup a value (lookupName) in OpenStack's metaservice and store in given fileName
func openstackMetaGet(lookupName string, fileName string, fileMode os.FileMode) {
if lookupValue, err := openstackGet(metaDataURL + lookupName); err == nil {
// we got a value from the metadata server, now save to filesystem
err = ioutil.WriteFile(path.Join(ConfigPath, fileName), lookupValue, fileMode)
if err != nil {
// we couldn't save the file for some reason
log.Printf("OpenStack: Failed to write %s:%s %s", fileName, lookupValue, err)
}
} else {
// we did not get a value back from the metadata server
log.Printf("OpenStack: Failed to get %s: %s", lookupName, err)
}
}
// openstackGet requests and extracts the requested URL
func openstackGet(url string) ([]byte, error) {
var client = &http.Client{
Timeout: time.Second * 2,
}
req, err := http.NewRequest("", url, nil)
if err != nil {
return nil, fmt.Errorf("OpenStack: http.NewRequest failed: %s", err)
}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("OpenStack: Could not contact metadata service: %s", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("OpenStack: Status not ok: %d", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("OpenStack: Failed to read http response: %s", err)
}
return body, nil
}
// SSH keys:
func (p *ProviderOpenstack) handleSSH() error {
sshKeys, err := openstackGet(metaDataURL + "public-keys/0/openssh-key")
if err != nil {
return fmt.Errorf("Failed to get sshKeys: %s", err)
}
if err := os.Mkdir(path.Join(ConfigPath, SSH), 0755); err != nil {
return fmt.Errorf("Failed to create %s: %s", SSH, err)
}
err = ioutil.WriteFile(path.Join(ConfigPath, SSH, "authorized_keys"), sshKeys, 0600)
if err != nil {
return fmt.Errorf("Failed to write ssh keys: %s", err)
}
return nil
}