metadata.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/log"
  23. )
  24. type Service struct {
  25. Root string
  26. Client pkg.Getter
  27. APIVersion string
  28. UserdataPath string
  29. MetadataPath string
  30. lastError error
  31. }
  32. func NewDatasource(root, apiVersion, userdataPath, metadataPath string, header http.Header) Service {
  33. if !strings.HasSuffix(root, "/") {
  34. root += "/"
  35. }
  36. return Service{root, pkg.NewHTTPClientHeader(header), apiVersion, userdataPath, metadataPath, nil}
  37. }
  38. func (ms Service) IsAvailable() bool {
  39. _, ms.lastError = ms.Client.Get(ms.Root + ms.APIVersion)
  40. if ms.lastError != nil {
  41. log.Errorf("%s: %s (lastError: %s)", "IsAvailable", ms.Root+":"+ms.UserdataPath, ms.lastError)
  42. }
  43. return (ms.lastError == nil)
  44. }
  45. func (ms *Service) Finish() error {
  46. return nil
  47. }
  48. func (ms *Service) String() string {
  49. return fmt.Sprintf("%s: %s (lastError: %s)", "metadata", ms.Root+ms.UserdataPath, ms.lastError)
  50. }
  51. func (ms Service) AvailabilityChanges() bool {
  52. return true
  53. }
  54. func (ms Service) ConfigRoot() string {
  55. return ms.Root
  56. }
  57. func (ms Service) FetchUserdata() ([]byte, error) {
  58. return ms.FetchData(ms.UserdataURL())
  59. }
  60. func (ms Service) FetchData(url string) ([]byte, error) {
  61. if data, err := ms.Client.GetRetry(url); err == nil {
  62. return data, err
  63. } else if _, ok := err.(pkg.ErrNotFound); ok {
  64. return []byte{}, nil
  65. } else {
  66. return data, err
  67. }
  68. }
  69. func (ms Service) MetadataURL() string {
  70. return (ms.Root + ms.MetadataPath)
  71. }
  72. func (ms Service) UserdataURL() string {
  73. return (ms.Root + ms.UserdataPath)
  74. }
  75. func (ms Service) FetchAttributes(key string) ([]string, error) {
  76. url := ms.MetadataURL() + key
  77. resp, err := ms.FetchData(url)
  78. if err != nil {
  79. return nil, err
  80. }
  81. scanner := bufio.NewScanner(bytes.NewBuffer(resp))
  82. data := make([]string, 0)
  83. for scanner.Scan() {
  84. data = append(data, scanner.Text())
  85. }
  86. return data, scanner.Err()
  87. }
  88. func (ms Service) FetchAttribute(key string) (string, error) {
  89. attrs, err := ms.FetchAttributes(key)
  90. if err == nil && len(attrs) > 0 {
  91. return attrs[0], nil
  92. }
  93. return "", err
  94. }