metadata.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 packet
  15. import (
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. "github.com/rancher/os/config/cloudinit/datasource"
  20. "github.com/rancher/os/config/cloudinit/datasource/metadata"
  21. "github.com/rancher/os/log"
  22. "github.com/rancher/os/netconf"
  23. yaml "github.com/cloudfoundry-incubator/candiedyaml"
  24. packetMetadata "github.com/packethost/packngo/metadata"
  25. )
  26. const (
  27. DefaultAddress = "https://metadata.packet.net/"
  28. apiVersion = ""
  29. userdataURL = "userdata"
  30. metadataPath = "metadata"
  31. )
  32. type MetadataService struct {
  33. metadata.Service
  34. }
  35. func NewDatasource(root string) *MetadataService {
  36. if root == "" {
  37. root = DefaultAddress
  38. }
  39. return &MetadataService{Service: metadata.NewDatasource(root, apiVersion, userdataURL, metadataPath, nil)}
  40. }
  41. func (ms *MetadataService) FetchMetadata() (metadata datasource.Metadata, err error) {
  42. m, err := packetMetadata.GetMetadata()
  43. if err != nil {
  44. log.Errorf("Failed to get Packet metadata: %v", err)
  45. return
  46. }
  47. bondCfg := netconf.InterfaceConfig{
  48. Addresses: []string{},
  49. BondOpts: map[string]string{
  50. "lacp_rate": "1",
  51. "xmit_hash_policy": "layer3+4",
  52. "downdelay": "200",
  53. "updelay": "200",
  54. "miimon": "100",
  55. "mode": "4",
  56. },
  57. }
  58. netCfg := netconf.NetworkConfig{
  59. Interfaces: map[string]netconf.InterfaceConfig{},
  60. }
  61. for _, iface := range m.Network.Interfaces {
  62. netCfg.Interfaces["mac="+iface.MAC] = netconf.InterfaceConfig{
  63. Bond: "bond0",
  64. }
  65. }
  66. for _, addr := range m.Network.Addresses {
  67. bondCfg.Addresses = append(bondCfg.Addresses, fmt.Sprintf("%s/%d", addr.Address, addr.NetworkBits))
  68. if addr.Gateway != nil && len(addr.Gateway) > 0 {
  69. if addr.Family == packetMetadata.IPv4 {
  70. if addr.Public {
  71. bondCfg.Gateway = addr.Gateway.String()
  72. }
  73. } else {
  74. bondCfg.GatewayIpv6 = addr.Gateway.String()
  75. }
  76. }
  77. if addr.Family == packetMetadata.IPv4 && strings.HasPrefix(addr.Gateway.String(), "10.") {
  78. bondCfg.PostUp = append(bondCfg.PostUp, "ip route add 10.0.0.0/8 via "+addr.Gateway.String())
  79. }
  80. }
  81. netCfg.Interfaces["bond0"] = bondCfg
  82. b, _ := yaml.Marshal(netCfg)
  83. log.Debugf("Generated network config: %s", string(b))
  84. // the old code var data []byte
  85. /* var m Metadata
  86. if data, err = ms.FetchData(ms.MetadataURL()); err != nil || len(data) == 0 {
  87. return
  88. }
  89. if err = json.Unmarshal(data, &m); err != nil {
  90. return
  91. }
  92. if len(m.NetworkData.Netblocks) > 0 {
  93. for _, Netblock := range m.NetworkData.Netblocks {
  94. if Netblock.AddressFamily == 4 {
  95. if Netblock.Public == true {
  96. metadata.PublicIPv4 = Netblock.Address
  97. } else {
  98. metadata.PrivateIPv4 = Netblock.Address
  99. }
  100. } else {
  101. metadata.PublicIPv6 = Netblock.Address
  102. }
  103. }
  104. }
  105. */
  106. metadata.Hostname = m.Hostname
  107. metadata.SSHPublicKeys = map[string]string{}
  108. for i, key := range m.SSHKeys {
  109. metadata.SSHPublicKeys[strconv.Itoa(i)] = key
  110. }
  111. metadata.NetworkConfig = netCfg
  112. // This is not really the right place - perhaps we should add a call-home function in each datasource to be called after the network is applied
  113. //(see the original in cmd/cloudsave/packet)
  114. //if _, err = http.Post(m.PhoneHomeURL, "application/json", bytes.NewReader([]byte{})); err != nil {
  115. //log.Errorf("Failed to post to Packet phone home URL: %v", err)
  116. //}
  117. return
  118. }
  119. func (ms MetadataService) Type() string {
  120. return "packet-metadata-service"
  121. }