vmware.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. "strings"
  19. "github.com/rancher/os/config/cloudinit/config"
  20. "github.com/rancher/os/config/cloudinit/datasource"
  21. "github.com/rancher/os/log"
  22. "github.com/rancher/os/netconf"
  23. )
  24. type readConfigFunction func(key string) (string, error)
  25. type urlDownloadFunction func(url string) ([]byte, error)
  26. type VMWare struct {
  27. ovfFileName string
  28. readConfig readConfigFunction
  29. urlDownload urlDownloadFunction
  30. lastError error
  31. }
  32. func (v VMWare) Finish() error {
  33. return nil
  34. }
  35. func (v VMWare) String() string {
  36. return fmt.Sprintf("%s: %s (lastError: %s)", v.Type(), v.ovfFileName, v.lastError)
  37. }
  38. func (v VMWare) AvailabilityChanges() bool {
  39. return false
  40. }
  41. func (v VMWare) ConfigRoot() string {
  42. return "/"
  43. }
  44. func (v VMWare) read(keytmpl string, args ...interface{}) (string, error) {
  45. key := fmt.Sprintf(keytmpl, args...)
  46. return v.readConfig(key)
  47. }
  48. func (v VMWare) FetchMetadata() (metadata datasource.Metadata, err error) {
  49. metadata.NetworkConfig = netconf.NetworkConfig{}
  50. metadata.Hostname, _ = v.readConfig("hostname")
  51. //netconf := map[string]string{}
  52. //saveConfig := func(key string, args ...interface{}) string {
  53. // key = fmt.Sprintf(key, args...)
  54. // val, _ := v.readConfig(key)
  55. // if val != "" {
  56. // netconf[key] = val
  57. // }
  58. // return val
  59. //}
  60. for i := 0; ; i++ {
  61. val, _ := v.read("dns.server.%d", i)
  62. if val == "" {
  63. break
  64. }
  65. metadata.NetworkConfig.DNS.Nameservers = append(metadata.NetworkConfig.DNS.Nameservers, val)
  66. }
  67. for i := 0; ; i++ {
  68. //if domain := saveConfig("dns.domain.%d", i); domain == "" {
  69. val, _ := v.read("dns.domain.%d", i)
  70. if val == "" {
  71. break
  72. }
  73. metadata.NetworkConfig.DNS.Search = append(metadata.NetworkConfig.DNS.Search, val)
  74. }
  75. metadata.NetworkConfig.Interfaces = make(map[string]netconf.InterfaceConfig)
  76. found := true
  77. for i := 0; found; i++ {
  78. found = false
  79. ethName := fmt.Sprintf("eth%d", i)
  80. netDevice := netconf.InterfaceConfig{
  81. DHCP: true,
  82. Match: ethName,
  83. Addresses: []string{},
  84. }
  85. //found = (saveConfig("interface.%d.name", i) != "") || found
  86. if val, _ := v.read("interface.%d.name", i); val != "" {
  87. netDevice.Match = val
  88. found = true
  89. }
  90. //found = (saveConfig("interface.%d.mac", i) != "") || found
  91. if val, _ := v.read("interface.%d.mac", i); val != "" {
  92. netDevice.Match = "mac:" + val
  93. found = true
  94. }
  95. //found = (saveConfig("interface.%d.dhcp", i) != "") || found
  96. if val, _ := v.read("interface.%d.dhcp", i); val != "" {
  97. netDevice.DHCP = (strings.ToLower(val) != "no")
  98. found = true
  99. }
  100. role, _ := v.read("interface.%d.role", i)
  101. for a := 0; ; a++ {
  102. address, _ := v.read("interface.%d.ip.%d.address", i, a)
  103. if address == "" {
  104. break
  105. }
  106. netDevice.Addresses = append(netDevice.Addresses, address)
  107. found = true
  108. netDevice.DHCP = false
  109. ip, _, err := net.ParseCIDR(address)
  110. if err != nil {
  111. log.Error(err)
  112. //return metadata, err
  113. }
  114. switch role {
  115. case "public":
  116. if ip.To4() != nil {
  117. metadata.PublicIPv4 = ip
  118. } else {
  119. metadata.PublicIPv6 = ip
  120. }
  121. case "private":
  122. if ip.To4() != nil {
  123. metadata.PrivateIPv4 = ip
  124. } else {
  125. metadata.PrivateIPv6 = ip
  126. }
  127. case "":
  128. default:
  129. //return metadata, fmt.Errorf("unrecognized role: %q", role)
  130. log.Error(err)
  131. }
  132. }
  133. for r := 0; ; r++ {
  134. gateway, _ := v.read("interface.%d.route.%d.gateway", i, r)
  135. // TODO: do we really not do anything but default routing?
  136. //destination, _ := v.read("interface.%d.route.%d.destination", i, r)
  137. destination := ""
  138. if gateway == "" && destination == "" {
  139. break
  140. } else {
  141. netDevice.Gateway = gateway
  142. found = true
  143. }
  144. }
  145. if found {
  146. metadata.NetworkConfig.Interfaces[ethName] = netDevice
  147. }
  148. }
  149. return
  150. }
  151. func (v VMWare) FetchUserdata() ([]byte, error) {
  152. encoding, err := v.readConfig("cloud-init.data.encoding")
  153. if err != nil {
  154. return nil, err
  155. }
  156. data, err := v.readConfig("cloud-init.config.data")
  157. if err != nil {
  158. return nil, err
  159. }
  160. // Try to fallback to url if no explicit data
  161. if data == "" {
  162. url, err := v.readConfig("cloud-init.config.url")
  163. if err != nil {
  164. return nil, err
  165. }
  166. if url != "" {
  167. rawData, err := v.urlDownload(url)
  168. if err != nil {
  169. return nil, err
  170. }
  171. data = string(rawData)
  172. }
  173. }
  174. if encoding != "" {
  175. return config.DecodeContent(data, encoding)
  176. }
  177. return []byte(data), nil
  178. }
  179. func (v VMWare) Type() string {
  180. return "VMWare"
  181. }