metadata.go 4.2 KB

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