Merge pull request #14 from hashicorp/aws-env

Spike on AWS ENV fingerprint
This commit is contained in:
Clint
2015-08-31 15:10:33 -05:00
3 changed files with 240 additions and 4 deletions

View File

@@ -0,0 +1,78 @@
package fingerprint
import (
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/nomad/structs"
)
// EnvAWSFingerprint is used to fingerprint the CPU
type EnvAWSFingerprint struct {
logger *log.Logger
}
// NewEnvAWSFingerprint is used to create a CPU fingerprint
func NewEnvAWSFingerprint(logger *log.Logger) Fingerprint {
f := &EnvAWSFingerprint{logger: logger}
return f
}
func (f *EnvAWSFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
if node.Links == nil {
node.Links = make(map[string]string)
}
metadataURL := os.Getenv("AWS_ENV_URL")
if metadataURL == "" {
metadataURL = "http://169.254.169.254/latest/meta-data/"
}
// assume 2 seconds is enough time for inside AWS network
client := &http.Client{
Timeout: 2 * time.Second,
}
keys := []string{
"ami-id",
"hostname",
"instance-id",
"instance-type",
"local-hostname",
"local-ipv4",
"public-hostname",
"public-ipv4",
"placement/availability-zone",
}
for _, k := range keys {
res, err := client.Get(metadataURL + k)
if err != nil {
// if it's a URL error, assume we're not in an AWS environment
// TODO: better way to detect AWS? Check xen virtualization?
if _, ok := err.(*url.Error); ok {
return false, nil
}
// not sure what other errors it would return
return false, err
}
resp, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
// assume we want blank entries
key := strings.Replace(k, "/", ".", -1)
node.Attributes["platform.aws."+key] = strings.Trim(string(resp), "\n")
}
// populate links
node.Links["aws.ec2"] = node.Attributes["platform.aws.placement.availability-zone"] + "." + node.Attributes["platform.aws.instance-id"]
return true, nil
}

View File

@@ -0,0 +1,147 @@
package fingerprint
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/nomad/structs"
)
func TestEnvAWSFingerprint_nonAws(t *testing.T) {
f := NewEnvAWSFingerprint(testLogger())
node := &structs.Node{
Attributes: make(map[string]string),
}
ok, err := f.Fingerprint(&config.Config{}, node)
if err != nil {
t.Fatalf("err: %v", err)
}
if ok {
t.Fatalf("Should be false without test server")
}
}
func TestEnvAWSFingerprint_aws(t *testing.T) {
f := NewEnvAWSFingerprint(testLogger())
node := &structs.Node{
Attributes: make(map[string]string),
}
// configure mock server with fixture routes, data
routes := routes{}
if err := json.Unmarshal([]byte(aws_routes), &routes); err != nil {
t.Fatalf("Failed to unmarshal JSON in AWS ENV test: %s", err)
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, e := range routes.Endpoints {
if r.RequestURI == e.Uri {
w.Header().Set("Content-Type", e.ContentType)
fmt.Fprintln(w, e.Body)
}
}
}))
defer ts.Close()
os.Setenv("AWS_ENV_URL", ts.URL+"/latest/meta-data/")
ok, err := f.Fingerprint(&config.Config{}, node)
if err != nil {
t.Fatalf("err: %v", err)
}
if !ok {
t.Fatalf("Expected AWS attributes and Links")
}
keys := []string{
"platform.aws.ami-id",
"platform.aws.hostname",
"platform.aws.instance-id",
"platform.aws.instance-type",
"platform.aws.local-hostname",
"platform.aws.local-ipv4",
"platform.aws.public-hostname",
"platform.aws.public-ipv4",
"platform.aws.placement.availability-zone",
}
for _, k := range keys {
assertNodeAttributeContains(t, node, k)
}
if len(node.Links) == 0 {
t.Fatalf("Empty links for Node in AWS Fingerprint test")
}
// confirm we have at least instance-id and ami-id
for _, k := range []string{"aws.ec2"} {
assertNodeLinksContains(t, node, k)
}
}
type routes struct {
Endpoints []*endpoint `json:"endpoints"`
}
type endpoint struct {
Uri string `json:"uri"`
ContentType string `json:"content-type"`
Body string `json:"body"`
}
const aws_routes = `
{
"endpoints": [
{
"uri": "/latest/meta-data/ami-id",
"content-type": "text/plain",
"body": "ami-1234"
},
{
"uri": "/latest/meta-data/hostname",
"content-type": "text/plain",
"body": "ip-10-0-0-207.us-west-2.compute.internal"
},
{
"uri": "/latest/meta-data/placement/availability-zone",
"content-type": "text/plain",
"body": "us-west-2a"
},
{
"uri": "/latest/meta-data/instance-id",
"content-type": "text/plain",
"body": "i-b3ba3875"
},
{
"uri": "/latest/meta-data/instance-type",
"content-type": "text/plain",
"body": "m3.large"
},
{
"uri": "/latest/meta-data/local-hostname",
"content-type": "text/plain",
"body": "ip-10-0-0-207.us-west-2.compute.internal"
},
{
"uri": "/latest/meta-data/local-ipv4",
"content-type": "text/plain",
"body": "10.0.0.207"
},
{
"uri": "/latest/meta-data/public-hostname",
"content-type": "text/plain",
"body": "ec2-54-191-117-175.us-west-2.compute.amazonaws.com"
},
{
"uri": "/latest/meta-data/public-ipv4",
"content-type": "text/plain",
"body": "54.191.117.175"
}
]
}
`

View File

@@ -28,21 +28,32 @@ func assertFingerprintOK(t *testing.T, fp Fingerprint, node *structs.Node) {
func assertNodeAttributeContains(t *testing.T, node *structs.Node, attribute string) {
actual, found := node.Attributes[attribute]
if !found {
t.Errorf("Expected to find `%s`\n\n[DEBUG] %#v", attribute, node)
t.Errorf("Expected to find Attribute `%s`\n\n[DEBUG] %#v", attribute, node)
return
}
if actual == "" {
t.Errorf("Expected non-empty value for `%s`\n\n[DEBUG] %#v", attribute, node)
t.Errorf("Expected non-empty Attribute value for `%s`\n\n[DEBUG] %#v", attribute, node)
}
}
func assertNodeAttributeEquals(t *testing.T, node *structs.Node, attribute string, expected string) {
actual, found := node.Attributes[attribute]
if !found {
t.Errorf("Expected to find `%s`; unable to check value\n\n[DEBUG] %#v", attribute, node)
t.Errorf("Expected to find Attribute `%s`; unable to check value\n\n[DEBUG] %#v", attribute, node)
return
}
if expected != actual {
t.Errorf("Expected `%s` to be `%s`, found `%s`\n\n[DEBUG] %#v", attribute, expected, actual, node)
t.Errorf("Expected `%s` Attribute to be `%s`, found `%s`\n\n[DEBUG] %#v", attribute, expected, actual, node)
}
}
func assertNodeLinksContains(t *testing.T, node *structs.Node, link string) {
actual, found := node.Links[link]
if !found {
t.Errorf("Expected to find Link `%s`\n\n[DEBUG] %#v", link, node)
return
}
if actual == "" {
t.Errorf("Expected non-empty Link value for `%s`\n\n[DEBUG] %#v", link, node)
}
}