metadata.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. 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. // With C5 and M5 instances, EBS volumes are exposed as NVMe block devices.
  127. // http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/nvme-ebs-volumes.html
  128. metadata.RootDisk = defaultXVRootDisk
  129. if instanceType, err := ms.FetchAttribute("instance-type"); err == nil {
  130. if strings.HasPrefix(instanceType, "m5") || strings.HasPrefix(instanceType, "c5") {
  131. metadata.RootDisk = defaultNVMeRootDisk
  132. }
  133. } else if _, ok := err.(pkg.ErrNotFound); !ok {
  134. return metadata, err
  135. }
  136. return metadata, nil
  137. }
  138. func (ms MetadataService) Type() string {
  139. return "ec2-metadata-service"
  140. }