2014-03-18 09:00:41 -07:00
|
|
|
package datasource
|
2014-03-04 16:36:05 -08:00
|
|
|
|
2014-06-20 21:11:57 -07:00
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2014-06-30 16:23:46 -07:00
|
|
|
"fmt"
|
2014-06-20 21:11:57 -07:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/coreos/coreos-cloudinit/pkg"
|
|
|
|
)
|
2014-05-21 13:13:20 -04:00
|
|
|
|
2014-06-24 17:56:05 -07:00
|
|
|
// metadataService retrieves metadata from either an OpenStack[1] (2012-08-10)
|
|
|
|
// or EC2[2] (2009-04-04) compatible endpoint. It will first attempt to
|
|
|
|
// directly retrieve a JSON blob from the OpenStack endpoint. If that fails
|
|
|
|
// with a 404, it then attempts to retrieve metadata bit-by-bit from the EC2
|
|
|
|
// endpoint, and populates that into an equivalent JSON blob. metadataService
|
|
|
|
// also checks for userdata from EC2 and, if that fails with a 404, OpenStack.
|
2014-06-24 19:26:07 -07:00
|
|
|
//
|
|
|
|
// [1] http://docs.openstack.org/grizzly/openstack-compute/admin/content/metadata-service.html
|
|
|
|
// [2] http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html#instancedata-data-categories
|
2014-06-24 17:56:05 -07:00
|
|
|
|
|
|
|
const (
|
|
|
|
BaseUrl = "http://169.254.169.254/"
|
|
|
|
Ec2UserdataUrl = BaseUrl + Ec2ApiVersion + "/user-data"
|
2014-06-30 16:23:46 -07:00
|
|
|
Ec2MetadataUrl = BaseUrl + Ec2ApiVersion + "/meta-data"
|
2014-07-19 20:18:25 -07:00
|
|
|
OpenstackUserdataUrl = BaseUrl + "openstack/" + OpenstackApiVersion + "/user_data"
|
2014-06-24 17:56:05 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type metadataService struct{}
|
2014-03-04 16:36:05 -08:00
|
|
|
|
2014-06-20 21:11:57 -07:00
|
|
|
type getter interface {
|
2014-06-26 14:58:32 -07:00
|
|
|
GetRetry(string) ([]byte, error)
|
2014-06-20 21:11:57 -07:00
|
|
|
}
|
|
|
|
|
2014-06-24 17:56:05 -07:00
|
|
|
func NewMetadataService() *metadataService {
|
|
|
|
return &metadataService{}
|
2014-03-04 16:36:05 -08:00
|
|
|
}
|
|
|
|
|
2014-06-26 15:17:53 -07:00
|
|
|
func (ms *metadataService) IsAvailable() bool {
|
|
|
|
client := pkg.NewHttpClient()
|
|
|
|
_, err := client.Get(BaseUrl)
|
|
|
|
return (err == nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *metadataService) AvailabilityChanges() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-06-20 21:11:57 -07:00
|
|
|
func (ms *metadataService) ConfigRoot() string {
|
2014-06-18 11:36:06 -07:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2014-06-20 21:11:57 -07:00
|
|
|
func (ms *metadataService) FetchMetadata() ([]byte, error) {
|
2014-06-24 17:56:05 -07:00
|
|
|
return fetchMetadata(pkg.NewHttpClient())
|
2014-06-18 11:58:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *metadataService) FetchUserdata() ([]byte, error) {
|
2014-05-22 14:37:19 -04:00
|
|
|
client := pkg.NewHttpClient()
|
2014-06-26 14:58:32 -07:00
|
|
|
if data, err := client.GetRetry(Ec2UserdataUrl); err == nil {
|
2014-06-24 17:56:05 -07:00
|
|
|
return data, err
|
|
|
|
} else if _, ok := err.(pkg.ErrTimeout); ok {
|
|
|
|
return data, err
|
|
|
|
}
|
2014-07-01 15:58:35 -07:00
|
|
|
|
|
|
|
if data, err := client.GetRetry(OpenstackUserdataUrl); err == nil {
|
|
|
|
return data, err
|
|
|
|
} else if _, ok := err.(pkg.ErrNotFound); ok {
|
|
|
|
return []byte{}, nil
|
|
|
|
} else {
|
|
|
|
return data, err
|
|
|
|
}
|
2014-03-04 16:36:05 -08:00
|
|
|
}
|
|
|
|
|
2014-03-18 09:00:41 -07:00
|
|
|
func (ms *metadataService) Type() string {
|
|
|
|
return "metadata-service"
|
|
|
|
}
|
2014-06-20 21:11:57 -07:00
|
|
|
|
2014-06-24 17:56:05 -07:00
|
|
|
func fetchMetadata(client getter) ([]byte, error) {
|
2014-06-30 16:23:46 -07:00
|
|
|
attrs := make(map[string]interface{})
|
|
|
|
if keynames, err := fetchAttributes(client, fmt.Sprintf("%s/public-keys", Ec2MetadataUrl)); err == nil {
|
|
|
|
keyIDs := make(map[string]string)
|
|
|
|
for _, keyname := range keynames {
|
|
|
|
tokens := strings.SplitN(keyname, "=", 2)
|
|
|
|
if len(tokens) != 2 {
|
|
|
|
return nil, fmt.Errorf("malformed public key: %q\n", keyname)
|
|
|
|
}
|
|
|
|
keyIDs[tokens[1]] = tokens[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
keys := make(map[string]string)
|
|
|
|
for name, id := range keyIDs {
|
|
|
|
sshkey, err := fetchAttribute(client, fmt.Sprintf("%s/public-keys/%s/openssh-key", Ec2MetadataUrl, id))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
keys[name] = sshkey
|
|
|
|
fmt.Printf("Found SSH key for %q\n", name)
|
|
|
|
}
|
|
|
|
attrs["public_keys"] = keys
|
|
|
|
} else if _, ok := err.(pkg.ErrNotFound); !ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if hostname, err := fetchAttribute(client, fmt.Sprintf("%s/hostname", Ec2MetadataUrl)); err == nil {
|
|
|
|
attrs["hostname"] = hostname
|
|
|
|
} else if _, ok := err.(pkg.ErrNotFound); !ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-07-01 14:23:14 -07:00
|
|
|
if localAddr, err := fetchAttribute(client, fmt.Sprintf("%s/local-ipv4", Ec2MetadataUrl)); err == nil {
|
|
|
|
attrs["local-ipv4"] = localAddr
|
|
|
|
} else if _, ok := err.(pkg.ErrNotFound); !ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if publicAddr, err := fetchAttribute(client, fmt.Sprintf("%s/public-ipv4", Ec2MetadataUrl)); err == nil {
|
|
|
|
attrs["public-ipv4"] = publicAddr
|
|
|
|
} else if _, ok := err.(pkg.ErrNotFound); !ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-30 16:23:46 -07:00
|
|
|
if content_path, err := fetchAttribute(client, fmt.Sprintf("%s/network_config/content_path", Ec2MetadataUrl)); err == nil {
|
|
|
|
attrs["network_config"] = map[string]string{
|
|
|
|
"content_path": content_path,
|
|
|
|
}
|
|
|
|
} else if _, ok := err.(pkg.ErrNotFound); !ok {
|
2014-06-20 21:11:57 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-06-30 16:23:46 -07:00
|
|
|
|
2014-06-20 21:11:57 -07:00
|
|
|
return json.Marshal(attrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchAttributes(client getter, url string) ([]string, error) {
|
2014-06-26 14:58:32 -07:00
|
|
|
resp, err := client.GetRetry(url)
|
2014-06-20 21:11:57 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(bytes.NewBuffer(resp))
|
|
|
|
data := make([]string, 0)
|
|
|
|
for scanner.Scan() {
|
2014-06-30 16:23:46 -07:00
|
|
|
data = append(data, scanner.Text())
|
2014-06-20 21:11:57 -07:00
|
|
|
}
|
|
|
|
return data, scanner.Err()
|
|
|
|
}
|
|
|
|
|
2014-06-30 16:23:46 -07:00
|
|
|
func fetchAttribute(client getter, url string) (string, error) {
|
2014-06-24 17:56:05 -07:00
|
|
|
if attrs, err := fetchAttributes(client, url); err == nil && len(attrs) > 0 {
|
2014-06-20 21:11:57 -07:00
|
|
|
return attrs[0], nil
|
|
|
|
} else {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|