vmware.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/pkg/log"
  22. "github.com/rancher/os/pkg/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: %v)", 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. dnsServers, _ := v.read("dns.servers")
  68. for _, val := range strings.Split(dnsServers, ",") {
  69. if val == "" {
  70. break
  71. }
  72. metadata.NetworkConfig.DNS.Nameservers = append(metadata.NetworkConfig.DNS.Nameservers, val)
  73. }
  74. for i := 0; ; i++ {
  75. //if domain := saveConfig("dns.domain.%d", i); domain == "" {
  76. val, _ := v.read("dns.domain.%d", i)
  77. if val == "" {
  78. break
  79. }
  80. metadata.NetworkConfig.DNS.Search = append(metadata.NetworkConfig.DNS.Search, val)
  81. }
  82. dnsDomains, _ := v.read("dns.domains")
  83. for _, val := range strings.Split(dnsDomains, ",") {
  84. if val == "" {
  85. break
  86. }
  87. metadata.NetworkConfig.DNS.Search = append(metadata.NetworkConfig.DNS.Search, val)
  88. }
  89. metadata.NetworkConfig.Interfaces = make(map[string]netconf.InterfaceConfig)
  90. found := true
  91. for i := 0; found; i++ {
  92. found = false
  93. ethName := fmt.Sprintf("eth%d", i)
  94. netDevice := netconf.InterfaceConfig{
  95. DHCP: true,
  96. Match: ethName,
  97. Addresses: []string{},
  98. }
  99. //found = (saveConfig("interface.%d.name", i) != "") || found
  100. if val, _ := v.read("interface.%d.name", i); val != "" {
  101. netDevice.Match = val
  102. found = true
  103. }
  104. //found = (saveConfig("interface.%d.mac", i) != "") || found
  105. if val, _ := v.read("interface.%d.mac", i); val != "" {
  106. netDevice.Match = "mac:" + val
  107. found = true
  108. }
  109. //found = (saveConfig("interface.%d.dhcp", i) != "") || found
  110. if val, _ := v.read("interface.%d.dhcp", i); val != "" {
  111. netDevice.DHCP = (strings.ToLower(val) != "no")
  112. found = true
  113. }
  114. role, _ := v.read("interface.%d.role", i)
  115. for a := 0; ; a++ {
  116. address, _ := v.read("interface.%d.ip.%d.address", i, a)
  117. if address == "" {
  118. break
  119. }
  120. netmask, _ := v.read("interface.%d.ip.%d.netmask", i, a)
  121. if netmask != "" {
  122. ones, _ := net.IPMask(net.ParseIP(netmask).To4()).Size()
  123. address = fmt.Sprintf("%s/%d", address, ones)
  124. }
  125. netDevice.Addresses = append(netDevice.Addresses, address)
  126. found = true
  127. netDevice.DHCP = false
  128. ip, _, err := net.ParseCIDR(address)
  129. if err != nil {
  130. log.Error(err)
  131. //return metadata, err
  132. }
  133. switch role {
  134. case "public":
  135. if ip.To4() != nil {
  136. metadata.PublicIPv4 = ip
  137. } else {
  138. metadata.PublicIPv6 = ip
  139. }
  140. case "private":
  141. if ip.To4() != nil {
  142. metadata.PrivateIPv4 = ip
  143. } else {
  144. metadata.PrivateIPv6 = ip
  145. }
  146. case "":
  147. default:
  148. //return metadata, fmt.Errorf("unrecognized role: %q", role)
  149. log.Error(err)
  150. }
  151. }
  152. for r := 0; ; r++ {
  153. gateway, _ := v.read("interface.%d.route.%d.gateway", i, r)
  154. // TODO: do we really not do anything but default routing?
  155. //destination, _ := v.read("interface.%d.route.%d.destination", i, r)
  156. destination := ""
  157. if gateway == "" && destination == "" {
  158. break
  159. } else {
  160. netDevice.Gateway = gateway
  161. found = true
  162. }
  163. }
  164. if found {
  165. metadata.NetworkConfig.Interfaces[ethName] = netDevice
  166. }
  167. }
  168. return
  169. }
  170. func (v VMWare) FetchUserdata() ([]byte, error) {
  171. encoding, err := v.readConfig("cloud-init.data.encoding")
  172. if err != nil {
  173. return nil, err
  174. }
  175. data, err := v.readConfig("cloud-init.config.data")
  176. if err != nil {
  177. return nil, err
  178. }
  179. // Try to fallback to url if no explicit data
  180. if data == "" {
  181. url, err := v.readConfig("cloud-init.config.url")
  182. if err != nil {
  183. return nil, err
  184. }
  185. if url != "" {
  186. rawData, err := v.urlDownload(url)
  187. if err != nil {
  188. return nil, err
  189. }
  190. data = string(rawData)
  191. }
  192. }
  193. if encoding != "" {
  194. return config.DecodeContent(data, encoding)
  195. }
  196. return []byte(data), nil
  197. }
  198. func (v VMWare) Type() string {
  199. return "VMWare"
  200. }