metadata.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/coreos/coreos-cloudinit/datasource"
  22. "github.com/coreos/coreos-cloudinit/datasource/metadata"
  23. )
  24. const (
  25. apiVersion = "computeMetadata/v1/"
  26. metadataPath = apiVersion
  27. userdataPath = apiVersion + "instance/attributes/user-data"
  28. )
  29. type MetadataService struct {
  30. metadata.MetadataService
  31. }
  32. func NewDatasource(root string) *MetadataService {
  33. return &MetadataService{metadata.NewDatasource(root, apiVersion, userdataPath, metadataPath, http.Header{"Metadata-Flavor": {"Google"}})}
  34. }
  35. func (ms MetadataService) FetchMetadata() (datasource.Metadata, error) {
  36. public, err := ms.fetchIP("instance/network-interfaces/0/access-configs/0/external-ip")
  37. if err != nil {
  38. return datasource.Metadata{}, err
  39. }
  40. local, err := ms.fetchIP("instance/network-interfaces/0/ip")
  41. if err != nil {
  42. return datasource.Metadata{}, err
  43. }
  44. hostname, err := ms.fetchString("instance/hostname")
  45. if err != nil {
  46. return datasource.Metadata{}, err
  47. }
  48. projectSSHKeys, err := ms.fetchString("project/attributes/sshKeys")
  49. if err != nil {
  50. return datasource.Metadata{}, err
  51. }
  52. instanceSSHKeys, err := ms.fetchString("instance/attributes/sshKeys")
  53. if err != nil {
  54. return datasource.Metadata{}, err
  55. }
  56. keyStrings := strings.Split(projectSSHKeys+"\n"+instanceSSHKeys, "\n")
  57. sshPublicKeys := map[string]string{}
  58. i := 0
  59. for _, keyString := range keyStrings {
  60. keySlice := strings.SplitN(keyString, ":", 2)
  61. if len(keySlice) == 2 {
  62. key := strings.TrimSpace(keySlice[1])
  63. if key != "" {
  64. sshPublicKeys[strconv.Itoa(i)] = strings.TrimSpace(keySlice[1])
  65. i++
  66. }
  67. }
  68. }
  69. return datasource.Metadata{
  70. PublicIPv4: public,
  71. PrivateIPv4: local,
  72. Hostname: hostname,
  73. SSHPublicKeys: sshPublicKeys,
  74. }, nil
  75. }
  76. func (ms MetadataService) Type() string {
  77. return "gce-metadata-service"
  78. }
  79. func (ms MetadataService) fetchString(key string) (string, error) {
  80. data, err := ms.FetchData(ms.MetadataUrl() + key)
  81. if err != nil {
  82. return "", err
  83. }
  84. return string(data), nil
  85. }
  86. func (ms MetadataService) fetchIP(key string) (net.IP, error) {
  87. str, err := ms.fetchString(key)
  88. if err != nil {
  89. return nil, err
  90. }
  91. if str == "" {
  92. return nil, nil
  93. }
  94. if ip := net.ParseIP(str); ip != nil {
  95. return ip, nil
  96. }
  97. return nil, fmt.Errorf("couldn't parse %q as IP address", str)
  98. }
  99. func (ms MetadataService) FetchUserdata() ([]byte, error) {
  100. data, err := ms.FetchData(ms.UserdataUrl())
  101. if err != nil {
  102. return nil, err
  103. }
  104. if len(data) == 0 {
  105. data, err = ms.FetchData(ms.MetadataUrl() + "instance/attributes/startup-script")
  106. if err != nil {
  107. return nil, err
  108. }
  109. }
  110. return data, nil
  111. }