metadata.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2016 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 gce
  15. import (
  16. "fmt"
  17. "net"
  18. "net/http"
  19. "strconv"
  20. "strings"
  21. //"github.com/rancher/os/netconf"
  22. "github.com/rancher/os/config/cloudinit/datasource"
  23. "github.com/rancher/os/config/cloudinit/datasource/metadata"
  24. )
  25. const (
  26. DefaultAddress = "http://metadata.google.internal/"
  27. apiVersion = "computeMetadata/v1/"
  28. metadataPath = apiVersion
  29. userdataPath = apiVersion + "instance/attributes/user-data"
  30. )
  31. type MetadataService struct {
  32. metadata.Service
  33. }
  34. func NewDatasource(root string) *MetadataService {
  35. if root == "" {
  36. root = DefaultAddress
  37. }
  38. return &MetadataService{metadata.NewDatasource(root, apiVersion, userdataPath, metadataPath, http.Header{"Metadata-Flavor": {"Google"}})}
  39. }
  40. func (ms MetadataService) FetchMetadata() (datasource.Metadata, error) {
  41. public, err := ms.fetchIP("instance/network-interfaces/0/access-configs/0/external-ip")
  42. if err != nil {
  43. return datasource.Metadata{}, err
  44. }
  45. local, err := ms.fetchIP("instance/network-interfaces/0/ip")
  46. if err != nil {
  47. return datasource.Metadata{}, err
  48. }
  49. hostname, err := ms.fetchString("instance/hostname")
  50. if err != nil {
  51. return datasource.Metadata{}, err
  52. }
  53. projectSSHKeys, err := ms.fetchString("project/attributes/sshKeys")
  54. if err != nil {
  55. return datasource.Metadata{}, err
  56. }
  57. instanceSSHKeys, err := ms.fetchString("instance/attributes/sshKeys")
  58. if err != nil {
  59. return datasource.Metadata{}, err
  60. }
  61. md := datasource.Metadata{
  62. PublicIPv4: public,
  63. PrivateIPv4: local,
  64. Hostname: hostname,
  65. SSHPublicKeys: nil,
  66. }
  67. /* Disabled, using DHCP like in pre-0.9.1 - missing gateway and netmask, and testing time
  68. addresses := []string{}
  69. if public != nil {
  70. addresses = append(addresses, public.String())
  71. }
  72. if local != nil {
  73. addresses = append(addresses, local.String())
  74. }
  75. if len(addresses) > 0 {
  76. network := netconf.InterfaceConfig{
  77. Addresses: addresses,
  78. }
  79. md.NetworkConfig.Interfaces = make(map[string]netconf.InterfaceConfig)
  80. md.NetworkConfig.Interfaces["eth0"] = network
  81. }
  82. */
  83. keyStrings := strings.Split(projectSSHKeys+"\n"+instanceSSHKeys, "\n")
  84. i := 0
  85. for _, keyString := range keyStrings {
  86. keySlice := strings.SplitN(keyString, ":", 2)
  87. if len(keySlice) == 2 {
  88. key := strings.TrimSpace(keySlice[1])
  89. if key != "" {
  90. if md.SSHPublicKeys == nil {
  91. md.SSHPublicKeys = map[string]string{}
  92. }
  93. md.SSHPublicKeys[strconv.Itoa(i)] = strings.TrimSpace(keySlice[1])
  94. i++
  95. }
  96. }
  97. }
  98. return md, nil
  99. }
  100. func (ms MetadataService) Type() string {
  101. return "gce-metadata-service"
  102. }
  103. func (ms MetadataService) fetchString(key string) (string, error) {
  104. data, err := ms.FetchData(ms.MetadataURL() + key)
  105. if err != nil {
  106. return "", err
  107. }
  108. return string(data), nil
  109. }
  110. func (ms MetadataService) fetchIP(key string) (net.IP, error) {
  111. str, err := ms.fetchString(key)
  112. if err != nil {
  113. return nil, err
  114. }
  115. if str == "" {
  116. return nil, nil
  117. }
  118. if ip := net.ParseIP(str); ip != nil {
  119. return ip, nil
  120. }
  121. return nil, fmt.Errorf("couldn't parse %q as IP address", str)
  122. }
  123. func (ms MetadataService) FetchUserdata() ([]byte, error) {
  124. // see https://github.com/number5/cloud-init/blob/master/cloudinit/sources/DataSourceGCE.py
  125. data, err := ms.FetchData(ms.UserdataURL())
  126. if err != nil {
  127. return nil, err
  128. }
  129. if len(data) == 0 {
  130. // see https://cloud.google.com/deployment-manager/docs/step-by-step-guide/setting-metadata-and-startup-scripts
  131. data, err = ms.FetchData(ms.MetadataURL() + "instance/attributes/startup-script")
  132. if err != nil {
  133. return nil, err
  134. }
  135. }
  136. return data, nil
  137. }