url.go 1.9 KB

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