configdrive.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 configdrive
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "io/ioutil"
  19. "os"
  20. "path"
  21. "syscall"
  22. "github.com/rancher/os/log"
  23. "github.com/docker/docker/pkg/mount"
  24. "github.com/rancher/os/config/cloudinit/datasource"
  25. "github.com/rancher/os/util"
  26. )
  27. const (
  28. configDevName = "config-2"
  29. configDev = "LABEL=" + configDevName
  30. configDevMountPoint = "/media/config-2"
  31. openstackAPIVersion = "latest"
  32. )
  33. type ConfigDrive struct {
  34. root string
  35. readFile func(filename string) ([]byte, error)
  36. lastError error
  37. availabilityChanges bool
  38. }
  39. func NewDatasource(root string) *ConfigDrive {
  40. return &ConfigDrive{root, ioutil.ReadFile, nil, true}
  41. }
  42. func (cd *ConfigDrive) IsAvailable() bool {
  43. if cd.root == configDevMountPoint {
  44. cd.lastError = MountConfigDrive()
  45. if cd.lastError != nil {
  46. log.Error(cd.lastError)
  47. // Don't keep retrying if we can't mount
  48. cd.availabilityChanges = false
  49. return false
  50. }
  51. defer cd.Finish()
  52. }
  53. _, cd.lastError = os.Stat(cd.root)
  54. return !os.IsNotExist(cd.lastError)
  55. // TODO: consider changing IsNotExists to not-available _and_ does not change
  56. }
  57. func (cd *ConfigDrive) Finish() error {
  58. return UnmountConfigDrive()
  59. }
  60. func (cd *ConfigDrive) String() string {
  61. if cd.lastError != nil {
  62. return fmt.Sprintf("%s: %s (lastError: %s)", cd.Type(), cd.root, cd.lastError)
  63. }
  64. return fmt.Sprintf("%s: %s", cd.Type(), cd.root)
  65. }
  66. func (cd *ConfigDrive) AvailabilityChanges() bool {
  67. return cd.availabilityChanges
  68. }
  69. func (cd *ConfigDrive) ConfigRoot() string {
  70. return cd.openstackRoot()
  71. }
  72. func (cd *ConfigDrive) FetchMetadata() (metadata datasource.Metadata, err error) {
  73. var data []byte
  74. var m struct {
  75. SSHAuthorizedKeyMap map[string]string `json:"public_keys"`
  76. Hostname string `json:"hostname"`
  77. NetworkConfig struct {
  78. ContentPath string `json:"content_path"`
  79. } `json:"network_config"`
  80. }
  81. if data, err = cd.tryReadFile(path.Join(cd.openstackVersionRoot(), "meta_data.json")); err != nil || len(data) == 0 {
  82. return
  83. }
  84. if err = json.Unmarshal([]byte(data), &m); err != nil {
  85. return
  86. }
  87. metadata.SSHPublicKeys = m.SSHAuthorizedKeyMap
  88. metadata.Hostname = m.Hostname
  89. // TODO: I don't think we've used this for anything
  90. /* if m.NetworkConfig.ContentPath != "" {
  91. metadata.NetworkConfig, err = cd.tryReadFile(path.Join(cd.openstackRoot(), m.NetworkConfig.ContentPath))
  92. }
  93. */
  94. return
  95. }
  96. func (cd *ConfigDrive) FetchUserdata() ([]byte, error) {
  97. return cd.tryReadFile(path.Join(cd.openstackVersionRoot(), "user_data"))
  98. }
  99. func (cd *ConfigDrive) Type() string {
  100. return "cloud-drive"
  101. }
  102. func (cd *ConfigDrive) openstackRoot() string {
  103. return path.Join(cd.root, "openstack")
  104. }
  105. func (cd *ConfigDrive) openstackVersionRoot() string {
  106. return path.Join(cd.openstackRoot(), openstackAPIVersion)
  107. }
  108. func (cd *ConfigDrive) tryReadFile(filename string) ([]byte, error) {
  109. if cd.root == configDevMountPoint {
  110. cd.lastError = MountConfigDrive()
  111. if cd.lastError != nil {
  112. log.Error(cd.lastError)
  113. return nil, cd.lastError
  114. }
  115. defer cd.Finish()
  116. }
  117. log.Debugf("Attempting to read from %q\n", filename)
  118. data, err := cd.readFile(filename)
  119. if os.IsNotExist(err) {
  120. err = nil
  121. }
  122. if err != nil {
  123. log.Errorf("ERROR read cloud-config file(%s) - err: %q", filename, err)
  124. }
  125. return data, err
  126. }
  127. func MountConfigDrive() error {
  128. if err := os.MkdirAll(configDevMountPoint, 700); err != nil {
  129. return err
  130. }
  131. configDev := util.ResolveDevice(configDev)
  132. if configDev == "" {
  133. return mount.Mount(configDevName, configDevMountPoint, "9p", "trans=virtio,version=9p2000.L")
  134. }
  135. fsType, err := util.GetFsType(configDev)
  136. if err != nil {
  137. return err
  138. }
  139. return mount.Mount(configDev, configDevMountPoint, fsType, "ro")
  140. }
  141. func UnmountConfigDrive() error {
  142. return syscall.Unmount(configDevMountPoint, 0)
  143. }