metadata.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 metadata
  15. import (
  16. "bufio"
  17. "bytes"
  18. "fmt"
  19. "net/http"
  20. "strings"
  21. "github.com/rancher/os/config/cloudinit/pkg"
  22. "github.com/rancher/os/pkg/log"
  23. )
  24. type Service struct {
  25. Root string
  26. Client pkg.Getter
  27. APIVersion string
  28. IsAvailableCheckPath string
  29. UserdataPath string
  30. MetadataPath string
  31. lastError error
  32. }
  33. // NewDatasource creates as HTTP based cloud-data service with the corresponding paths for the user-data and meta-data.
  34. // To check the available in IsAvailable, the apiVersion is used as path.
  35. func NewDatasource(root, apiVersion, userdataPath, metadataPath string, header http.Header) Service {
  36. return NewDatasourceWithCheckPath(root, apiVersion, apiVersion, userdataPath, metadataPath, header)
  37. }
  38. // NewDatasourceWithCheckPath creates as HTTP based cloud-data service with the corresponding paths for the user-data and meta-data.
  39. func NewDatasourceWithCheckPath(root, apiVersion, isAvailableCheckPath, userdataPath, metadataPath string, header http.Header) Service {
  40. if !strings.HasSuffix(root, "/") {
  41. root += "/"
  42. }
  43. return Service{root, pkg.NewHTTPClientHeader(header), apiVersion, isAvailableCheckPath, userdataPath, metadataPath, nil}
  44. }
  45. func (ms Service) IsAvailable() bool {
  46. checkURL := ms.Root + ms.IsAvailableCheckPath
  47. _, ms.lastError = ms.Client.Get(checkURL)
  48. if ms.lastError != nil {
  49. log.Errorf("%s: %s (lastError: %v)", "IsAvailable", checkURL, ms.lastError)
  50. }
  51. return (ms.lastError == nil)
  52. }
  53. func (ms *Service) Finish() error {
  54. return nil
  55. }
  56. func (ms *Service) String() string {
  57. return fmt.Sprintf("%s: %s (lastError: %v)", "metadata", ms.UserdataURL(), ms.lastError)
  58. }
  59. func (ms Service) AvailabilityChanges() bool {
  60. return true
  61. }
  62. func (ms Service) ConfigRoot() string {
  63. return ms.Root
  64. }
  65. func (ms Service) FetchUserdata() ([]byte, error) {
  66. return ms.FetchData(ms.UserdataURL())
  67. }
  68. func (ms Service) FetchData(url string) ([]byte, error) {
  69. if data, err := ms.Client.GetRetry(url); err == nil {
  70. return data, err
  71. } else if _, ok := err.(pkg.ErrNotFound); ok {
  72. return []byte{}, nil
  73. } else {
  74. return data, err
  75. }
  76. }
  77. func (ms Service) MetadataURL() string {
  78. return (ms.Root + ms.MetadataPath)
  79. }
  80. func (ms Service) UserdataURL() string {
  81. return (ms.Root + ms.UserdataPath)
  82. }
  83. func (ms Service) FetchAttributes(key string) ([]string, error) {
  84. url := ms.MetadataURL() + key
  85. resp, err := ms.FetchData(url)
  86. if err != nil {
  87. return nil, err
  88. }
  89. scanner := bufio.NewScanner(bytes.NewBuffer(resp))
  90. data := make([]string, 0)
  91. for scanner.Scan() {
  92. data = append(data, scanner.Text())
  93. }
  94. return data, scanner.Err()
  95. }
  96. func (ms Service) FetchAttribute(key string) (string, error) {
  97. attrs, err := ms.FetchAttributes(key)
  98. if err == nil && len(attrs) > 0 {
  99. return attrs[0], nil
  100. }
  101. return "", err
  102. }