metadata.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 digitalocean
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "net"
  19. "strconv"
  20. "github.com/rancher/os/config/cloudinit/datasource"
  21. "github.com/rancher/os/config/cloudinit/datasource/metadata"
  22. "github.com/rancher/os/pkg/netconf"
  23. )
  24. const (
  25. DefaultAddress = "http://169.254.169.254/"
  26. apiVersion = "metadata/v1"
  27. userdataURL = apiVersion + "/user-data"
  28. metadataPath = apiVersion + ".json"
  29. )
  30. type Address struct {
  31. IPAddress string `json:"ip_address"`
  32. Netmask string `json:"netmask"`
  33. Cidr int `json:"cidr"`
  34. Gateway string `json:"gateway"`
  35. }
  36. type Interface struct {
  37. IPv4 *Address `json:"ipv4"`
  38. IPv6 *Address `json:"ipv6"`
  39. AnchorIPv4 *Address `json:"anchor_ipv4"`
  40. MAC string `json:"mac"`
  41. Type string `json:"type"`
  42. }
  43. type Interfaces struct {
  44. Public []Interface `json:"public"`
  45. Private []Interface `json:"private"`
  46. }
  47. type DNS struct {
  48. Nameservers []string `json:"nameservers"`
  49. }
  50. type Metadata struct {
  51. Hostname string `json:"hostname"`
  52. Interfaces Interfaces `json:"interfaces"`
  53. PublicKeys []string `json:"public_keys"`
  54. DNS DNS `json:"dns"`
  55. }
  56. type MetadataService struct {
  57. metadata.Service
  58. }
  59. func NewDatasource(root string) *MetadataService {
  60. if root == "" {
  61. root = DefaultAddress
  62. }
  63. return &MetadataService{Service: metadata.NewDatasource(root, apiVersion, userdataURL, metadataPath, nil)}
  64. }
  65. func (ms MetadataService) AvailabilityChanges() bool {
  66. // TODO: if it can't find the network, maybe we can start it?
  67. return false
  68. }
  69. // Parse IPv4 netmask written in IP form (e.g. "255.255.255.0").
  70. func ipmask(addr *Address) string {
  71. ip := net.ParseIP(addr.IPAddress)
  72. var mask net.IPMask
  73. if addr.Netmask != "" {
  74. mask = net.IPMask(net.ParseIP(addr.Netmask))
  75. } else {
  76. mask = net.CIDRMask(addr.Cidr, 32)
  77. }
  78. ipnet := net.IPNet{
  79. IP: ip,
  80. Mask: mask,
  81. }
  82. return ipnet.String()
  83. }
  84. func (ms *MetadataService) FetchMetadata() (metadata datasource.Metadata, err error) {
  85. var data []byte
  86. var m Metadata
  87. if data, err = ms.FetchData(ms.MetadataURL()); err != nil || len(data) == 0 {
  88. return
  89. }
  90. if err = json.Unmarshal(data, &m); err != nil {
  91. return
  92. }
  93. if len(m.Interfaces.Public) > 0 {
  94. if m.Interfaces.Public[0].IPv4 != nil {
  95. metadata.PublicIPv4 = net.ParseIP(m.Interfaces.Public[0].IPv4.IPAddress)
  96. }
  97. if m.Interfaces.Public[0].IPv6 != nil {
  98. metadata.PublicIPv6 = net.ParseIP(m.Interfaces.Public[0].IPv6.IPAddress)
  99. }
  100. }
  101. if len(m.Interfaces.Private) > 0 {
  102. if m.Interfaces.Private[0].IPv4 != nil {
  103. metadata.PrivateIPv4 = net.ParseIP(m.Interfaces.Private[0].IPv4.IPAddress)
  104. }
  105. if m.Interfaces.Private[0].IPv6 != nil {
  106. metadata.PrivateIPv6 = net.ParseIP(m.Interfaces.Private[0].IPv6.IPAddress)
  107. }
  108. }
  109. metadata.NetworkConfig.Interfaces = make(map[string]netconf.InterfaceConfig)
  110. ethNumber := 0
  111. for _, eth := range m.Interfaces.Public {
  112. network := netconf.InterfaceConfig{}
  113. if eth.IPv4 != nil {
  114. network.Gateway = eth.IPv4.Gateway
  115. network.Addresses = append(network.Addresses, ipmask(eth.IPv4))
  116. if metadata.PublicIPv4 == nil {
  117. metadata.PublicIPv4 = net.ParseIP(eth.IPv4.IPAddress)
  118. }
  119. }
  120. if eth.AnchorIPv4 != nil {
  121. network.Addresses = append(network.Addresses, ipmask(eth.AnchorIPv4))
  122. }
  123. if eth.IPv6 != nil {
  124. network.Addresses = append(network.Addresses, fmt.Sprintf("%s/%d", eth.IPv6.IPAddress, eth.IPv6.Cidr))
  125. network.GatewayIpv6 = eth.IPv6.Gateway
  126. if metadata.PublicIPv6 == nil {
  127. metadata.PublicIPv6 = net.ParseIP(eth.IPv6.IPAddress)
  128. }
  129. }
  130. metadata.NetworkConfig.Interfaces[fmt.Sprintf("eth%d", ethNumber)] = network
  131. ethNumber = ethNumber + 1
  132. }
  133. for _, eth := range m.Interfaces.Private {
  134. network := netconf.InterfaceConfig{}
  135. if eth.IPv4 != nil {
  136. network.Gateway = eth.IPv4.Gateway
  137. network.Addresses = append(network.Addresses, ipmask(eth.IPv4))
  138. if metadata.PrivateIPv4 == nil {
  139. metadata.PrivateIPv4 = net.ParseIP(eth.IPv6.IPAddress)
  140. }
  141. }
  142. if eth.AnchorIPv4 != nil {
  143. network.Addresses = append(network.Addresses, ipmask(eth.AnchorIPv4))
  144. }
  145. if eth.IPv6 != nil {
  146. network.Addresses = append(network.Addresses, fmt.Sprintf("%s/%d", eth.IPv6.IPAddress, eth.IPv6.Cidr))
  147. network.GatewayIpv6 = eth.IPv6.Gateway
  148. if metadata.PrivateIPv6 == nil {
  149. metadata.PrivateIPv6 = net.ParseIP(eth.IPv6.IPAddress)
  150. }
  151. }
  152. metadata.NetworkConfig.Interfaces[fmt.Sprintf("eth%d", ethNumber)] = network
  153. ethNumber = ethNumber + 1
  154. }
  155. metadata.NetworkConfig.DNS.Nameservers = m.DNS.Nameservers
  156. metadata.Hostname = m.Hostname
  157. metadata.SSHPublicKeys = map[string]string{}
  158. for i, key := range m.PublicKeys {
  159. metadata.SSHPublicKeys[strconv.Itoa(i)] = key
  160. }
  161. return
  162. }
  163. func (ms MetadataService) Type() string {
  164. return "digitalocean-metadata-service"
  165. }