metadata.go 3.8 KB

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