vmware.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. "fmt"
  17. "net"
  18. "github.com/rancher/os/config/cloudinit/config"
  19. "github.com/rancher/os/config/cloudinit/datasource"
  20. )
  21. type readConfigFunction func(key string) (string, error)
  22. type urlDownloadFunction func(url string) ([]byte, error)
  23. type VMWare struct {
  24. ovfFileName string
  25. readConfig readConfigFunction
  26. urlDownload urlDownloadFunction
  27. lastError error
  28. }
  29. func (v VMWare) Finish() error {
  30. return nil
  31. }
  32. func (v VMWare) String() string {
  33. return fmt.Sprintf("%s: %s (lastError: %s)", v.Type(), v.ovfFileName, v.lastError)
  34. }
  35. func (v VMWare) AvailabilityChanges() bool {
  36. return false
  37. }
  38. func (v VMWare) ConfigRoot() string {
  39. return "/"
  40. }
  41. func (v VMWare) FetchMetadata() (metadata datasource.Metadata, err error) {
  42. metadata.Hostname, _ = v.readConfig("hostname")
  43. netconf := map[string]string{}
  44. saveConfig := func(key string, args ...interface{}) string {
  45. key = fmt.Sprintf(key, args...)
  46. val, _ := v.readConfig(key)
  47. if val != "" {
  48. netconf[key] = val
  49. }
  50. return val
  51. }
  52. for i := 0; ; i++ {
  53. if nameserver := saveConfig("dns.server.%d", i); nameserver == "" {
  54. break
  55. }
  56. }
  57. for i := 0; ; i++ {
  58. if domain := saveConfig("dns.domain.%d", i); domain == "" {
  59. break
  60. }
  61. }
  62. found := true
  63. for i := 0; found; i++ {
  64. found = false
  65. found = (saveConfig("interface.%d.name", i) != "") || found
  66. found = (saveConfig("interface.%d.mac", i) != "") || found
  67. found = (saveConfig("interface.%d.dhcp", i) != "") || found
  68. role, _ := v.readConfig(fmt.Sprintf("interface.%d.role", i))
  69. for a := 0; ; a++ {
  70. address := saveConfig("interface.%d.ip.%d.address", i, a)
  71. if address == "" {
  72. break
  73. } else {
  74. found = true
  75. }
  76. ip, _, err := net.ParseCIDR(address)
  77. if err != nil {
  78. return metadata, err
  79. }
  80. switch role {
  81. case "public":
  82. if ip.To4() != nil {
  83. metadata.PublicIPv4 = ip
  84. } else {
  85. metadata.PublicIPv6 = ip
  86. }
  87. case "private":
  88. if ip.To4() != nil {
  89. metadata.PrivateIPv4 = ip
  90. } else {
  91. metadata.PrivateIPv6 = ip
  92. }
  93. case "":
  94. default:
  95. return metadata, fmt.Errorf("unrecognized role: %q", role)
  96. }
  97. }
  98. for r := 0; ; r++ {
  99. gateway := saveConfig("interface.%d.route.%d.gateway", i, r)
  100. destination := saveConfig("interface.%d.route.%d.destination", i, r)
  101. if gateway == "" && destination == "" {
  102. break
  103. } else {
  104. found = true
  105. }
  106. }
  107. }
  108. // metadata.NetworkConfig = netconf
  109. return
  110. }
  111. func (v VMWare) FetchUserdata() ([]byte, error) {
  112. encoding, err := v.readConfig("coreos.config.data.encoding")
  113. if err != nil {
  114. return nil, err
  115. }
  116. data, err := v.readConfig("coreos.config.data")
  117. if err != nil {
  118. return nil, err
  119. }
  120. // Try to fallback to url if no explicit data
  121. if data == "" {
  122. url, err := v.readConfig("coreos.config.url")
  123. if err != nil {
  124. return nil, err
  125. }
  126. if url != "" {
  127. rawData, err := v.urlDownload(url)
  128. if err != nil {
  129. return nil, err
  130. }
  131. data = string(rawData)
  132. }
  133. }
  134. if encoding != "" {
  135. return config.DecodeContent(data, encoding)
  136. }
  137. return []byte(data), nil
  138. }
  139. func (v VMWare) Type() string {
  140. return "VMWare"
  141. }