metadata.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ec2
  15. import (
  16. "bufio"
  17. "bytes"
  18. "fmt"
  19. "log"
  20. "net"
  21. "strings"
  22. "github.com/rancher/os/netconf"
  23. "github.com/rancher/os/config/cloudinit/datasource"
  24. "github.com/rancher/os/config/cloudinit/datasource/metadata"
  25. "github.com/rancher/os/config/cloudinit/pkg"
  26. )
  27. const (
  28. DefaultAddress = "http://169.254.169.254/"
  29. apiVersion = "latest/"
  30. userdataPath = apiVersion + "user-data/"
  31. metadataPath = apiVersion + "meta-data/"
  32. )
  33. type MetadataService struct {
  34. metadata.Service
  35. }
  36. func NewDatasource(root string) *MetadataService {
  37. if root == "" {
  38. root = DefaultAddress
  39. }
  40. return &MetadataService{metadata.NewDatasource(root, apiVersion, userdataPath, metadataPath, nil)}
  41. }
  42. func (ms MetadataService) AvailabilityChanges() bool {
  43. // TODO: if it can't find the network, maybe we can start it?
  44. return false
  45. }
  46. func (ms MetadataService) FetchMetadata() (datasource.Metadata, error) {
  47. // see http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html
  48. metadata := datasource.Metadata{}
  49. metadata.NetworkConfig = netconf.NetworkConfig{}
  50. if keynames, err := ms.fetchAttributes("public-keys"); err == nil {
  51. keyIDs := make(map[string]string)
  52. for _, keyname := range keynames {
  53. tokens := strings.SplitN(keyname, "=", 2)
  54. if len(tokens) != 2 {
  55. return metadata, fmt.Errorf("malformed public key: %q", keyname)
  56. }
  57. keyIDs[tokens[1]] = tokens[0]
  58. }
  59. metadata.SSHPublicKeys = map[string]string{}
  60. for name, id := range keyIDs {
  61. sshkey, err := ms.fetchAttribute(fmt.Sprintf("public-keys/%s/openssh-key", id))
  62. if err != nil {
  63. return metadata, err
  64. }
  65. metadata.SSHPublicKeys[name] = sshkey
  66. log.Printf("Found SSH key for %q\n", name)
  67. }
  68. } else if _, ok := err.(pkg.ErrNotFound); !ok {
  69. return metadata, err
  70. }
  71. if hostname, err := ms.fetchAttribute("hostname"); err == nil {
  72. metadata.Hostname = strings.Split(hostname, " ")[0]
  73. } else if _, ok := err.(pkg.ErrNotFound); !ok {
  74. return metadata, err
  75. }
  76. // TODO: these are only on the first interface - it looks like you can have as many as you need...
  77. if localAddr, err := ms.fetchAttribute("local-ipv4"); err == nil {
  78. metadata.PrivateIPv4 = net.ParseIP(localAddr)
  79. } else if _, ok := err.(pkg.ErrNotFound); !ok {
  80. return metadata, err
  81. }
  82. if publicAddr, err := ms.fetchAttribute("public-ipv4"); err == nil {
  83. metadata.PublicIPv4 = net.ParseIP(publicAddr)
  84. } else if _, ok := err.(pkg.ErrNotFound); !ok {
  85. return metadata, err
  86. }
  87. metadata.NetworkConfig.Interfaces = make(map[string]netconf.InterfaceConfig)
  88. if macs, err := ms.fetchAttributes("network/interfaces/macs"); err != nil {
  89. for _, mac := range macs {
  90. if deviceNumber, err := ms.fetchAttribute(fmt.Sprintf("network/interfaces/macs/%s/device-number", mac)); err != nil {
  91. network := netconf.InterfaceConfig{
  92. DHCP: true,
  93. }
  94. /* Looks like we must use DHCP for aws
  95. // private ipv4
  96. if subnetCidrBlock, err := ms.fetchAttribute(fmt.Sprintf("network/interfaces/macs/%s/subnet-ipv4-cidr-block", mac)); err != nil {
  97. cidr := strings.Split(subnetCidrBlock, "/")
  98. if localAddr, err := ms.fetchAttributes(fmt.Sprintf("network/interfaces/macs/%s/local-ipv4s", mac)); err != nil {
  99. for _, addr := range localAddr {
  100. network.Addresses = append(network.Addresses, addr+"/"+cidr[1])
  101. }
  102. }
  103. }
  104. // ipv6
  105. if localAddr, err := ms.fetchAttributes(fmt.Sprintf("network/interfaces/macs/%s/ipv6s", mac)); err != nil {
  106. if subnetCidrBlock, err := ms.fetchAttributes(fmt.Sprintf("network/interfaces/macs/%s/subnet-ipv6-cidr-block", mac)); err != nil {
  107. for i, addr := range localAddr {
  108. cidr := strings.Split(subnetCidrBlock[i], "/")
  109. network.Addresses = append(network.Addresses, addr+"/"+cidr[1])
  110. }
  111. }
  112. }
  113. */
  114. // disabled - it looks to me like you don't actually put the public IP on the eth device
  115. /* if publicAddr, err := ms.fetchAttributes(fmt.Sprintf("network/interfaces/macs/%s/public-ipv4s", mac)); err != nil {
  116. if vpcCidrBlock, err := ms.fetchAttribute(fmt.Sprintf("network/interfaces/macs/%s/vpc-ipv4-cidr-block", mac)); err != nil {
  117. cidr := strings.Split(vpcCidrBlock, "/")
  118. network.Addresses = append(network.Addresses, publicAddr+"/"+cidr[1])
  119. }
  120. }
  121. */
  122. metadata.NetworkConfig.Interfaces["eth"+deviceNumber] = network
  123. }
  124. }
  125. }
  126. return metadata, nil
  127. }
  128. func (ms MetadataService) Type() string {
  129. return "ec2-metadata-service"
  130. }
  131. func (ms MetadataService) fetchAttributes(key string) ([]string, error) {
  132. url := ms.MetadataURL() + key
  133. resp, err := ms.FetchData(url)
  134. if err != nil {
  135. return nil, err
  136. }
  137. scanner := bufio.NewScanner(bytes.NewBuffer(resp))
  138. data := make([]string, 0)
  139. for scanner.Scan() {
  140. data = append(data, scanner.Text())
  141. }
  142. return data, scanner.Err()
  143. }
  144. func (ms MetadataService) fetchAttribute(key string) (string, error) {
  145. attrs, err := ms.fetchAttributes(key)
  146. if err == nil && len(attrs) > 0 {
  147. return attrs[0], nil
  148. }
  149. return "", err
  150. }