vmware_amd64.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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/log"
  19. "github.com/rancher/os/util"
  20. "github.com/rancher/os/config/cloudinit/pkg"
  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. return ovf.env.Properties["guestinfo."+key], nil
  30. }
  31. func NewDatasource(fileName string) *VMWare {
  32. if util.GetHypervisor() != "vmware" {
  33. return nil
  34. }
  35. // read from provided ovf environment document (typically /media/ovfenv/ovf-env.xml)
  36. if fileName != "" {
  37. log.Printf("Using OVF environment from %s\n", fileName)
  38. ovfEnv, err := ioutil.ReadFile(fileName)
  39. if err != nil {
  40. ovfEnv = make([]byte, 0)
  41. }
  42. return &VMWare{
  43. ovfFileName: fileName,
  44. readConfig: getOvfReadConfig(ovfEnv),
  45. urlDownload: urlDownload,
  46. }
  47. }
  48. // try to read ovf environment from VMware tools
  49. data, err := readConfig("ovfenv")
  50. if err == nil && data != "" {
  51. log.Printf("Using OVF environment from guestinfo\n")
  52. return &VMWare{
  53. readConfig: getOvfReadConfig([]byte(data)),
  54. urlDownload: urlDownload,
  55. }
  56. }
  57. // if everything fails, fallback to directly reading variables from the backdoor
  58. log.Printf("Using guestinfo variables\n")
  59. return &VMWare{
  60. readConfig: readConfig,
  61. urlDownload: urlDownload,
  62. }
  63. }
  64. func (v VMWare) IsAvailable() bool {
  65. if util.GetHypervisor() != "vmware" {
  66. return false
  67. }
  68. if v.ovfFileName != "" {
  69. _, v.lastError = os.Stat(v.ovfFileName)
  70. return !os.IsNotExist(v.lastError)
  71. }
  72. return vmcheck.IsVirtualWorld()
  73. }
  74. func readConfig(key string) (string, error) {
  75. data, err := rpcvmx.NewConfig().String(key, "")
  76. if err == nil {
  77. log.Printf("Read from %q: %q\n", key, data)
  78. } else {
  79. log.Printf("Failed to read from %q: %v\n", key, err)
  80. }
  81. return data, err
  82. }
  83. func getOvfReadConfig(ovfEnv []byte) readConfigFunction {
  84. env := &ovf.OvfEnvironment{}
  85. if len(ovfEnv) != 0 {
  86. env = ovf.ReadEnvironment(ovfEnv)
  87. }
  88. wrapper := ovfWrapper{env}
  89. return wrapper.readConfig
  90. }
  91. func urlDownload(url string) ([]byte, error) {
  92. client := pkg.NewHTTPClient()
  93. return client.GetRetry(url)
  94. }