vmware_amd64.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 vmware
  15. import (
  16. "io/ioutil"
  17. "os"
  18. "github.com/rancher/os/config/cloudinit/pkg"
  19. "github.com/rancher/os/pkg/log"
  20. "github.com/rancher/os/pkg/util"
  21. "github.com/sigma/vmw-guestinfo/rpcvmx"
  22. "github.com/sigma/vmw-guestinfo/vmcheck"
  23. ovf "github.com/sigma/vmw-ovflib"
  24. )
  25. type ovfWrapper struct {
  26. env *ovf.OvfEnvironment
  27. }
  28. func (ovf ovfWrapper) readConfig(key string) (string, error) {
  29. if val := ovf.env.Properties["guestinfo."+key]; val != "" {
  30. return val, nil
  31. }
  32. return readConfig(key)
  33. }
  34. func NewDatasource(fileName string) *VMWare {
  35. if util.GetHypervisor() != "vmware" {
  36. return nil
  37. }
  38. // read from provided ovf environment document (typically /media/ovfenv/ovf-env.xml)
  39. if fileName != "" {
  40. log.Printf("Using OVF environment from %s\n", fileName)
  41. ovfEnv, err := ioutil.ReadFile(fileName)
  42. if err != nil {
  43. ovfEnv = make([]byte, 0)
  44. }
  45. return &VMWare{
  46. ovfFileName: fileName,
  47. readConfig: getOvfReadConfig(ovfEnv),
  48. urlDownload: urlDownload,
  49. }
  50. }
  51. // try to read ovf environment from VMware tools
  52. data, err := readConfig("ovfenv")
  53. if err == nil && data != "" {
  54. log.Printf("Using OVF environment from guestinfo\n")
  55. return &VMWare{
  56. readConfig: getOvfReadConfig([]byte(data)),
  57. urlDownload: urlDownload,
  58. }
  59. }
  60. // if everything fails, fallback to directly reading variables from the backdoor
  61. log.Printf("Using guestinfo variables\n")
  62. return &VMWare{
  63. readConfig: readConfig,
  64. urlDownload: urlDownload,
  65. }
  66. }
  67. func (v VMWare) IsAvailable() bool {
  68. if util.GetHypervisor() != "vmware" {
  69. return false
  70. }
  71. if v.ovfFileName != "" {
  72. _, v.lastError = os.Stat(v.ovfFileName)
  73. if !os.IsNotExist(v.lastError) {
  74. // when GuestInfo is empty, the DataSource should not be available.
  75. return v.checkGuestInfo()
  76. }
  77. return false
  78. }
  79. // when GuestInfo is empty, the DataSource should not be available.
  80. return vmcheck.IsVirtualWorld() && v.checkGuestInfo()
  81. }
  82. func readConfig(key string) (string, error) {
  83. data, err := rpcvmx.NewConfig().String(key, "")
  84. if err == nil {
  85. log.Printf("Read from %q: %q\n", key, data)
  86. } else {
  87. log.Printf("Failed to read from %q: %v\n", key, err)
  88. }
  89. return data, err
  90. }
  91. func getOvfReadConfig(ovfEnv []byte) readConfigFunction {
  92. env := &ovf.OvfEnvironment{}
  93. if len(ovfEnv) != 0 {
  94. env = ovf.ReadEnvironment(ovfEnv)
  95. }
  96. wrapper := ovfWrapper{env}
  97. return wrapper.readConfig
  98. }
  99. func urlDownload(url string) ([]byte, error) {
  100. client := pkg.NewHTTPClient()
  101. return client.GetRetry(url)
  102. }
  103. func (v VMWare) checkGuestInfo() bool {
  104. userData, err := v.FetchUserdata()
  105. if err == nil && string(userData) != "" {
  106. return true
  107. }
  108. metadata, err := v.FetchMetadata()
  109. if err == nil {
  110. if metadata.Hostname != "" {
  111. return true
  112. }
  113. if len(metadata.NetworkConfig.DNS.Nameservers) > 0 || len(metadata.NetworkConfig.DNS.Search) > 0 {
  114. return true
  115. }
  116. if len(metadata.NetworkConfig.Interfaces) > 0 {
  117. return true
  118. }
  119. }
  120. return false
  121. }