file.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 file
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "github.com/rancher/os/config/cloudinit/datasource"
  20. )
  21. type LocalFile struct {
  22. path string
  23. lastError error
  24. }
  25. func NewDatasource(path string) *LocalFile {
  26. return &LocalFile{path, nil}
  27. }
  28. func (f *LocalFile) IsAvailable() bool {
  29. _, f.lastError = os.Stat(f.path)
  30. return !os.IsNotExist(f.lastError)
  31. }
  32. func (f *LocalFile) Finish() error {
  33. return nil
  34. }
  35. func (f *LocalFile) String() string {
  36. return fmt.Sprintf("%s: %s (lastError: %s)", f.Type(), f.path, f.lastError)
  37. }
  38. func (f *LocalFile) AvailabilityChanges() bool {
  39. return true
  40. }
  41. func (f *LocalFile) ConfigRoot() string {
  42. return ""
  43. }
  44. func (f *LocalFile) FetchMetadata() (datasource.Metadata, error) {
  45. return datasource.Metadata{}, nil
  46. }
  47. func (f *LocalFile) FetchUserdata() ([]byte, error) {
  48. return ioutil.ReadFile(f.path)
  49. }
  50. func (f *LocalFile) Type() string {
  51. return "local-file"
  52. }