2014-07-31 01:23:10 +04:00
|
|
|
package ec2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2014-08-16 05:13:37 +04:00
|
|
|
"github.com/coreos/coreos-cloudinit/datasource/metadata"
|
2014-07-31 01:23:10 +04:00
|
|
|
"github.com/coreos/coreos-cloudinit/pkg"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DefaultAddress = "http://169.254.169.254/"
|
|
|
|
apiVersion = "2009-04-04"
|
2014-08-16 05:13:37 +04:00
|
|
|
userdataPath = apiVersion + "/user-data"
|
|
|
|
metadataPath = apiVersion + "/meta-data"
|
2014-07-31 01:23:10 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type metadataService struct {
|
2014-08-16 05:13:37 +04:00
|
|
|
metadata.MetadataService
|
2014-07-31 01:23:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDatasource(root string) *metadataService {
|
2014-08-16 05:13:37 +04:00
|
|
|
return &metadataService{metadata.NewDatasource(root, apiVersion, userdataPath, metadataPath)}
|
2014-07-31 01:23:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ms metadataService) FetchMetadata() ([]byte, error) {
|
|
|
|
attrs := make(map[string]interface{})
|
2014-08-16 05:13:37 +04:00
|
|
|
if keynames, err := ms.fetchAttributes(fmt.Sprintf("%s/public-keys", ms.MetadataUrl())); err == nil {
|
2014-07-31 01:23:10 +04:00
|
|
|
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", keyname)
|
|
|
|
}
|
|
|
|
keyIDs[tokens[1]] = tokens[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
keys := make(map[string]string)
|
|
|
|
for name, id := range keyIDs {
|
2014-08-16 05:13:37 +04:00
|
|
|
sshkey, err := ms.fetchAttribute(fmt.Sprintf("%s/public-keys/%s/openssh-key", ms.MetadataUrl(), id))
|
2014-07-31 01:23:10 +04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-08-16 05:13:37 +04:00
|
|
|
if hostname, err := ms.fetchAttribute(fmt.Sprintf("%s/hostname", ms.MetadataUrl())); err == nil {
|
2014-07-31 01:23:10 +04:00
|
|
|
attrs["hostname"] = hostname
|
|
|
|
} else if _, ok := err.(pkg.ErrNotFound); !ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-08-16 05:13:37 +04:00
|
|
|
if localAddr, err := ms.fetchAttribute(fmt.Sprintf("%s/local-ipv4", ms.MetadataUrl())); err == nil {
|
2014-07-31 01:23:10 +04:00
|
|
|
attrs["local-ipv4"] = localAddr
|
|
|
|
} else if _, ok := err.(pkg.ErrNotFound); !ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-08-16 05:13:37 +04:00
|
|
|
if publicAddr, err := ms.fetchAttribute(fmt.Sprintf("%s/public-ipv4", ms.MetadataUrl())); err == nil {
|
2014-07-31 01:23:10 +04:00
|
|
|
attrs["public-ipv4"] = publicAddr
|
|
|
|
} else if _, ok := err.(pkg.ErrNotFound); !ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-08-16 05:13:37 +04:00
|
|
|
if content_path, err := ms.fetchAttribute(fmt.Sprintf("%s/network_config/content_path", ms.MetadataUrl())); err == nil {
|
2014-07-31 01:23:10 +04:00
|
|
|
attrs["network_config"] = map[string]string{
|
|
|
|
"content_path": content_path,
|
|
|
|
}
|
|
|
|
} else if _, ok := err.(pkg.ErrNotFound); !ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(attrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms metadataService) Type() string {
|
|
|
|
return "ec2-metadata-service"
|
|
|
|
}
|
|
|
|
|
2014-08-16 05:13:37 +04:00
|
|
|
func (ms metadataService) fetchAttributes(url string) ([]string, error) {
|
|
|
|
resp, err := ms.FetchData(url)
|
2014-07-31 01:23:10 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(bytes.NewBuffer(resp))
|
|
|
|
data := make([]string, 0)
|
|
|
|
for scanner.Scan() {
|
|
|
|
data = append(data, scanner.Text())
|
|
|
|
}
|
|
|
|
return data, scanner.Err()
|
|
|
|
}
|
|
|
|
|
2014-08-16 05:13:37 +04:00
|
|
|
func (ms metadataService) fetchAttribute(url string) (string, error) {
|
|
|
|
if attrs, err := ms.fetchAttributes(url); err == nil && len(attrs) > 0 {
|
2014-07-31 01:23:10 +04:00
|
|
|
return attrs[0], nil
|
|
|
|
} else {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|