2014-03-18 20:00:41 +04:00
|
|
|
package datasource
|
2014-03-05 04:36:05 +04:00
|
|
|
|
2014-06-21 08:11:57 +04:00
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/coreos/coreos-cloudinit/pkg"
|
|
|
|
)
|
2014-05-21 21:13:20 +04:00
|
|
|
|
2014-06-25 04:56:05 +04: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-25 06:26:07 +04: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-25 04:56:05 +04:00
|
|
|
|
|
|
|
const (
|
|
|
|
BaseUrl = "http://169.254.169.254/"
|
|
|
|
Ec2ApiVersion = "2009-04-04"
|
|
|
|
Ec2UserdataUrl = BaseUrl + Ec2ApiVersion + "/user-data"
|
|
|
|
Ec2MetadataUrl = BaseUrl + Ec2ApiVersion + "/meta-data/"
|
|
|
|
OpenstackApiVersion = "openstack/2012-08-10"
|
|
|
|
OpenstackUserdataUrl = BaseUrl + OpenstackApiVersion + "/user_data"
|
|
|
|
OpenstackMetadataUrl = BaseUrl + OpenstackApiVersion + "/meta_data.json"
|
|
|
|
)
|
|
|
|
|
|
|
|
type metadataService struct{}
|
2014-03-05 04:36:05 +04:00
|
|
|
|
2014-06-21 08:11:57 +04:00
|
|
|
type getter interface {
|
2014-06-27 01:58:32 +04:00
|
|
|
GetRetry(string) ([]byte, error)
|
2014-06-21 08:11:57 +04:00
|
|
|
}
|
|
|
|
|
2014-06-25 04:56:05 +04:00
|
|
|
func NewMetadataService() *metadataService {
|
|
|
|
return &metadataService{}
|
2014-03-05 04:36:05 +04:00
|
|
|
}
|
|
|
|
|
2014-06-21 08:11:57 +04:00
|
|
|
func (ms *metadataService) ConfigRoot() string {
|
2014-06-18 22:36:06 +04:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2014-06-21 08:11:57 +04:00
|
|
|
func (ms *metadataService) FetchMetadata() ([]byte, error) {
|
2014-06-25 04:56:05 +04:00
|
|
|
return fetchMetadata(pkg.NewHttpClient())
|
2014-06-18 22:58:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *metadataService) FetchUserdata() ([]byte, error) {
|
2014-05-22 22:37:19 +04:00
|
|
|
client := pkg.NewHttpClient()
|
2014-06-27 01:58:32 +04:00
|
|
|
if data, err := client.GetRetry(Ec2UserdataUrl); err == nil {
|
2014-06-25 04:56:05 +04:00
|
|
|
return data, err
|
|
|
|
} else if _, ok := err.(pkg.ErrTimeout); ok {
|
|
|
|
return data, err
|
|
|
|
}
|
2014-06-27 01:58:32 +04:00
|
|
|
return client.GetRetry(OpenstackUserdataUrl)
|
2014-03-05 04:36:05 +04:00
|
|
|
}
|
|
|
|
|
2014-03-18 20:00:41 +04:00
|
|
|
func (ms *metadataService) Type() string {
|
|
|
|
return "metadata-service"
|
|
|
|
}
|
2014-06-21 08:11:57 +04:00
|
|
|
|
2014-06-25 04:56:05 +04:00
|
|
|
func fetchMetadata(client getter) ([]byte, error) {
|
2014-06-27 01:58:32 +04:00
|
|
|
if metadata, err := client.GetRetry(OpenstackMetadataUrl); err == nil {
|
2014-06-21 08:11:57 +04:00
|
|
|
return metadata, nil
|
|
|
|
} else if _, ok := err.(pkg.ErrTimeout); ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-25 04:56:05 +04:00
|
|
|
attrs, err := fetchChildAttributes(client, Ec2MetadataUrl)
|
2014-06-21 08:11:57 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return json.Marshal(attrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchAttributes(client getter, url string) ([]string, error) {
|
2014-06-27 01:58:32 +04:00
|
|
|
resp, err := client.GetRetry(url)
|
2014-06-21 08:11:57 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(bytes.NewBuffer(resp))
|
|
|
|
data := make([]string, 0)
|
|
|
|
for scanner.Scan() {
|
|
|
|
data = append(data, strings.Split(scanner.Text(), "=")[0])
|
|
|
|
}
|
|
|
|
return data, scanner.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchAttribute(client getter, url string) (interface{}, error) {
|
2014-06-25 04:56:05 +04:00
|
|
|
if attrs, err := fetchAttributes(client, url); err == nil && len(attrs) > 0 {
|
2014-06-21 08:11:57 +04:00
|
|
|
return attrs[0], nil
|
|
|
|
} else {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchChildAttributes(client getter, url string) (interface{}, error) {
|
|
|
|
attrs := make(map[string]interface{})
|
|
|
|
attrList, err := fetchAttributes(client, url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, attr := range attrList {
|
|
|
|
var fetchFunc func(getter, string) (interface{}, error)
|
|
|
|
if strings.HasSuffix(attr, "/") {
|
|
|
|
fetchFunc = fetchChildAttributes
|
|
|
|
} else {
|
|
|
|
fetchFunc = fetchAttribute
|
|
|
|
}
|
|
|
|
if value, err := fetchFunc(client, url+attr); err == nil {
|
|
|
|
attrs[strings.TrimSuffix(attr, "/")] = value
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return attrs, nil
|
|
|
|
}
|