metadata.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. "fmt"
  17. "log"
  18. "net"
  19. "strings"
  20. "github.com/rancher/os/netconf"
  21. "github.com/rancher/os/config/cloudinit/datasource"
  22. "github.com/rancher/os/config/cloudinit/datasource/metadata"
  23. "github.com/rancher/os/config/cloudinit/pkg"
  24. )
  25. const (
  26. DefaultAddress = "http://169.254.169.254/"
  27. apiVersion = "latest/"
  28. userdataPath = apiVersion + "user-data"
  29. metadataPath = apiVersion + "meta-data/"
  30. defaultXVRootDisk = "/dev/xvda"
  31. defaultNVMeRootDisk = "/dev/nvme0n1"
  32. )
  33. var (
  34. nvmeInstanceTypes = []string{"c5", "c5d", "i3.metal", "m5", "m5d", "r5", "r5d", "t3", "z1d"}
  35. )
  36. type MetadataService struct {
  37. metadata.Service
  38. }
  39. func NewDatasource(root string) *MetadataService {
  40. if root == "" {
  41. root = DefaultAddress
  42. }
  43. return &MetadataService{metadata.NewDatasource(root, apiVersion, userdataPath, metadataPath, nil)}
  44. }
  45. func (ms MetadataService) AvailabilityChanges() bool {
  46. // TODO: if it can't find the network, maybe we can start it?
  47. return false
  48. }
  49. func (ms MetadataService) FetchMetadata() (datasource.Metadata, error) {
  50. // see http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html
  51. metadata := datasource.Metadata{}
  52. metadata.NetworkConfig = netconf.NetworkConfig{}
  53. if keynames, err := ms.FetchAttributes("public-keys"); err == nil {
  54. keyIDs := make(map[string]string)
  55. for _, keyname := range keynames {
  56. tokens := strings.SplitN(keyname, "=", 2)
  57. if len(tokens) != 2 {
  58. return metadata, fmt.Errorf("malformed public key: %q", keyname)
  59. }
  60. keyIDs[tokens[1]] = tokens[0]
  61. }
  62. metadata.SSHPublicKeys = map[string]string{}
  63. for name, id := range keyIDs {
  64. sshkey, err := ms.FetchAttribute(fmt.Sprintf("public-keys/%s/openssh-key", id))
  65. if err != nil {
  66. return metadata, err
  67. }
  68. metadata.SSHPublicKeys[name] = sshkey
  69. log.Printf("Found SSH key for %q\n", name)
  70. }
  71. } else if _, ok := err.(pkg.ErrNotFound); !ok {
  72. return metadata, err
  73. }
  74. if hostname, err := ms.FetchAttribute("hostname"); err == nil {
  75. metadata.Hostname = strings.Split(hostname, " ")[0]
  76. } else if _, ok := err.(pkg.ErrNotFound); !ok {
  77. return metadata, err
  78. }
  79. // TODO: these are only on the first interface - it looks like you can have as many as you need...
  80. if localAddr, err := ms.FetchAttribute("local-ipv4"); err == nil {
  81. metadata.PrivateIPv4 = net.ParseIP(localAddr)
  82. } else if _, ok := err.(pkg.ErrNotFound); !ok {
  83. return metadata, err
  84. }
  85. if publicAddr, err := ms.FetchAttribute("public-ipv4"); err == nil {
  86. metadata.PublicIPv4 = net.ParseIP(publicAddr)
  87. } else if _, ok := err.(pkg.ErrNotFound); !ok {
  88. return metadata, err
  89. }
  90. metadata.NetworkConfig.Interfaces = make(map[string]netconf.InterfaceConfig)
  91. if macs, err := ms.FetchAttributes("network/interfaces/macs"); err != nil {
  92. for _, mac := range macs {
  93. if deviceNumber, err := ms.FetchAttribute(fmt.Sprintf("network/interfaces/macs/%s/device-number", mac)); err != nil {
  94. network := netconf.InterfaceConfig{
  95. DHCP: true,
  96. }
  97. /* Looks like we must use DHCP for aws
  98. // private ipv4
  99. if subnetCidrBlock, err := ms.FetchAttribute(fmt.Sprintf("network/interfaces/macs/%s/subnet-ipv4-cidr-block", mac)); err != nil {
  100. cidr := strings.Split(subnetCidrBlock, "/")
  101. if localAddr, err := ms.FetchAttributes(fmt.Sprintf("network/interfaces/macs/%s/local-ipv4s", mac)); err != nil {
  102. for _, addr := range localAddr {
  103. network.Addresses = append(network.Addresses, addr+"/"+cidr[1])
  104. }
  105. }
  106. }
  107. // ipv6
  108. if localAddr, err := ms.FetchAttributes(fmt.Sprintf("network/interfaces/macs/%s/ipv6s", mac)); err != nil {
  109. if subnetCidrBlock, err := ms.FetchAttributes(fmt.Sprintf("network/interfaces/macs/%s/subnet-ipv6-cidr-block", mac)); err != nil {
  110. for i, addr := range localAddr {
  111. cidr := strings.Split(subnetCidrBlock[i], "/")
  112. network.Addresses = append(network.Addresses, addr+"/"+cidr[1])
  113. }
  114. }
  115. }
  116. */
  117. // disabled - it looks to me like you don't actually put the public IP on the eth device
  118. /* if publicAddr, err := ms.FetchAttributes(fmt.Sprintf("network/interfaces/macs/%s/public-ipv4s", mac)); err != nil {
  119. if vpcCidrBlock, err := ms.FetchAttribute(fmt.Sprintf("network/interfaces/macs/%s/vpc-ipv4-cidr-block", mac)); err != nil {
  120. cidr := strings.Split(vpcCidrBlock, "/")
  121. network.Addresses = append(network.Addresses, publicAddr+"/"+cidr[1])
  122. }
  123. }
  124. */
  125. metadata.NetworkConfig.Interfaces["eth"+deviceNumber] = network
  126. }
  127. }
  128. }
  129. // With C5 and M5 instances, EBS volumes are exposed as NVMe block devices.
  130. // http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/nvme-ebs-volumes.html
  131. metadata.RootDisk = defaultXVRootDisk
  132. if instanceType, err := ms.FetchAttribute("instance-type"); err == nil {
  133. for _, nvmeType := range nvmeInstanceTypes {
  134. if strings.HasPrefix(instanceType, nvmeType) {
  135. metadata.RootDisk = defaultNVMeRootDisk
  136. break
  137. }
  138. }
  139. } else if _, ok := err.(pkg.ErrNotFound); !ok {
  140. return metadata, err
  141. }
  142. return metadata, nil
  143. }
  144. func (ms MetadataService) Type() string {
  145. return "ec2-metadata-service"
  146. }