vmware_amd64.go 2.6 KB

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