diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 4ec623d..c438932 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -42,7 +42,7 @@ }, { "ImportPath": "github.com/rancher/go-rancher-metadata/metadata", - "Rev": "3235034fa02c64bf78855c2673386260b5c43cc7" + "Rev": "9f075a094b0c421ae795af18dba1c8ee45d81ff7" }, { "ImportPath": "github.com/rancher/go-rancher/client", diff --git a/Godeps/_workspace/src/github.com/rancher/go-rancher-metadata/metadata/metadata.go b/Godeps/_workspace/src/github.com/rancher/go-rancher-metadata/metadata/metadata.go index 28b93e2..97047a0 100644 --- a/Godeps/_workspace/src/github.com/rancher/go-rancher-metadata/metadata/metadata.go +++ b/Godeps/_workspace/src/github.com/rancher/go-rancher-metadata/metadata/metadata.go @@ -33,6 +33,11 @@ func (m *Client) SendRequest(path string) ([]byte, error) { if err != nil { return nil, err } + + if resp.StatusCode != 200 { + return nil, fmt.Errorf("Error %v accessing %v path", resp.StatusCode, path) + } + defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { @@ -117,6 +122,22 @@ func (m *Client) GetContainers() ([]Container, error) { return containers, nil } +func (m *Client) GetServiceContainers(serviceName string, stackName string) ([]Container, error) { + var serviceContainers = []Container{} + containers, err := m.GetContainers() + if err != nil { + return serviceContainers, err + } + + for _, container := range containers { + if container.StackName == stackName && container.ServiceName == serviceName { + serviceContainers = append(serviceContainers, container) + } + } + + return serviceContainers, nil +} + func (m *Client) GetHosts() ([]Host, error) { resp, err := m.SendRequest("/hosts") var hosts []Host diff --git a/Godeps/_workspace/src/github.com/rancher/go-rancher-metadata/metadata/types.go b/Godeps/_workspace/src/github.com/rancher/go-rancher-metadata/metadata/types.go index 7d63401..92ec4a7 100644 --- a/Godeps/_workspace/src/github.com/rancher/go-rancher-metadata/metadata/types.go +++ b/Godeps/_workspace/src/github.com/rancher/go-rancher-metadata/metadata/types.go @@ -7,12 +7,14 @@ type Stack struct { } type Service struct { + Scale int `json:"scale"` Name string `json:"name"` StackName string `json:"stack_name"` Kind string `json:"kind"` Hostname string `json:"hostname"` Vip string `json:"vip"` - CreateIndex string `json:"create_index"` + CreateIndex int `json:"create_index"` + UUID string `json:"uuid"` ExternalIps []string `json:"external_ips"` Sidekicks []string `json:"sidekicks"` Containers []string `json:"containers"` diff --git a/metadata/metadata.go b/metadata/metadata.go index 00910a3..b2b22ef 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -1,13 +1,15 @@ package metadata import ( + "fmt" "github.com/Sirupsen/logrus" "github.com/rancher/external-dns/dns" "github.com/rancher/go-rancher-metadata/metadata" + "time" ) const ( - metadataUrl = "http://rancher-metadata/latest" + metadataUrl = "http://rancher-metadata/2015-07-25" ) type MetadataClient struct { @@ -15,19 +17,36 @@ type MetadataClient struct { EnvironmentName string } +func getEnvironmentName(m *metadata.Client) (string, error) { + timeout := 30 * time.Second + var err error + var stack metadata.Stack + for i := 1 * time.Second; i < timeout; i *= time.Duration(2) { + stack, err = m.GetSelfStack() + if err != nil { + logrus.Errorf("Error reading stack info: %v...will retry", err) + time.Sleep(i) + } else { + return stack.EnvironmentName, nil + } + } + return "", fmt.Errorf("Error reading stack info: %v", err) +} + func NewMetadataClient() (*MetadataClient, error) { m, err := metadata.NewClientAndWait(metadataUrl) if err != nil { logrus.Fatalf("Failed to configure rancher-metadata: %v", err) } - selfStack, err := m.GetSelfStack() + + envName, err := getEnvironmentName(m) if err != nil { logrus.Fatalf("Error reading stack info: %v", err) } return &MetadataClient{ MetadataClient: m, - EnvironmentName: selfStack.EnvironmentName, + EnvironmentName: envName, }, nil }