url.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 url
  15. import (
  16. "fmt"
  17. "github.com/rancher/os/config/cloudinit/datasource"
  18. "github.com/rancher/os/config/cloudinit/pkg"
  19. )
  20. type RemoteFile struct {
  21. url string
  22. lastError error
  23. }
  24. func NewDatasource(url string) *RemoteFile {
  25. return &RemoteFile{url, nil}
  26. }
  27. func (f *RemoteFile) IsAvailable() bool {
  28. client := pkg.NewHTTPClient()
  29. _, f.lastError = client.Get(f.url)
  30. return (f.lastError == nil)
  31. }
  32. func (f *RemoteFile) Finish() error {
  33. return nil
  34. }
  35. func (f *RemoteFile) String() string {
  36. return fmt.Sprintf("%s: %s (lastError: %s)", f.Type(), f.url, f.lastError)
  37. }
  38. func (f *RemoteFile) AvailabilityChanges() bool {
  39. return false
  40. // TODO: we should trigger something to change the network state
  41. /* if f.lastError != nil {
  42. // if we have a Network error, then we should retry.
  43. // otherwise, we've made a request to the server, and its said nope.
  44. if _, ok := f.lastError.(pkg.ErrNetwork); !ok {
  45. return false
  46. }
  47. }
  48. return true
  49. */
  50. }
  51. func (f *RemoteFile) ConfigRoot() string {
  52. return ""
  53. }
  54. func (f *RemoteFile) FetchMetadata() (datasource.Metadata, error) {
  55. return datasource.Metadata{}, nil
  56. }
  57. func (f *RemoteFile) FetchUserdata() ([]byte, error) {
  58. client := pkg.NewHTTPClient()
  59. return client.GetRetry(f.url)
  60. }
  61. func (f *RemoteFile) Type() string {
  62. return "url"
  63. }