mirror of
https://github.com/rancher/os.git
synced 2025-09-09 02:31:36 +00:00
move coreos-cloudinit into config/cloudinit
Signed-off-by: Sven Dowideit <SvenDowideit@home.org.au>
This commit is contained in:
159
config/cloudinit/datasource/vmware/vmware.go
Normal file
159
config/cloudinit/datasource/vmware/vmware.go
Normal file
@@ -0,0 +1,159 @@
|
||||
// Copyright 2015 CoreOS, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package vmware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/rancher/os/config/cloudinit/config"
|
||||
"github.com/rancher/os/config/cloudinit/datasource"
|
||||
)
|
||||
|
||||
type readConfigFunction func(key string) (string, error)
|
||||
type urlDownloadFunction func(url string) ([]byte, error)
|
||||
|
||||
type vmware struct {
|
||||
ovfFileName string
|
||||
readConfig readConfigFunction
|
||||
urlDownload urlDownloadFunction
|
||||
}
|
||||
|
||||
func (v vmware) AvailabilityChanges() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (v vmware) ConfigRoot() string {
|
||||
return "/"
|
||||
}
|
||||
|
||||
func (v vmware) FetchMetadata() (metadata datasource.Metadata, err error) {
|
||||
metadata.Hostname, _ = v.readConfig("hostname")
|
||||
|
||||
netconf := map[string]string{}
|
||||
saveConfig := func(key string, args ...interface{}) string {
|
||||
key = fmt.Sprintf(key, args...)
|
||||
val, _ := v.readConfig(key)
|
||||
if val != "" {
|
||||
netconf[key] = val
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
for i := 0; ; i++ {
|
||||
if nameserver := saveConfig("dns.server.%d", i); nameserver == "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; ; i++ {
|
||||
if domain := saveConfig("dns.domain.%d", i); domain == "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
found := true
|
||||
for i := 0; found; i++ {
|
||||
found = false
|
||||
|
||||
found = (saveConfig("interface.%d.name", i) != "") || found
|
||||
found = (saveConfig("interface.%d.mac", i) != "") || found
|
||||
found = (saveConfig("interface.%d.dhcp", i) != "") || found
|
||||
|
||||
role, _ := v.readConfig(fmt.Sprintf("interface.%d.role", i))
|
||||
for a := 0; ; a++ {
|
||||
address := saveConfig("interface.%d.ip.%d.address", i, a)
|
||||
if address == "" {
|
||||
break
|
||||
} else {
|
||||
found = true
|
||||
}
|
||||
|
||||
ip, _, err := net.ParseCIDR(address)
|
||||
if err != nil {
|
||||
return metadata, err
|
||||
}
|
||||
|
||||
switch role {
|
||||
case "public":
|
||||
if ip.To4() != nil {
|
||||
metadata.PublicIPv4 = ip
|
||||
} else {
|
||||
metadata.PublicIPv6 = ip
|
||||
}
|
||||
case "private":
|
||||
if ip.To4() != nil {
|
||||
metadata.PrivateIPv4 = ip
|
||||
} else {
|
||||
metadata.PrivateIPv6 = ip
|
||||
}
|
||||
case "":
|
||||
default:
|
||||
return metadata, fmt.Errorf("unrecognized role: %q", role)
|
||||
}
|
||||
}
|
||||
|
||||
for r := 0; ; r++ {
|
||||
gateway := saveConfig("interface.%d.route.%d.gateway", i, r)
|
||||
destination := saveConfig("interface.%d.route.%d.destination", i, r)
|
||||
|
||||
if gateway == "" && destination == "" {
|
||||
break
|
||||
} else {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
}
|
||||
metadata.NetworkConfig = netconf
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (v vmware) FetchUserdata() ([]byte, error) {
|
||||
encoding, err := v.readConfig("coreos.config.data.encoding")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data, err := v.readConfig("coreos.config.data")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Try to fallback to url if no explicit data
|
||||
if data == "" {
|
||||
url, err := v.readConfig("coreos.config.url")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if url != "" {
|
||||
rawData, err := v.urlDownload(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data = string(rawData)
|
||||
}
|
||||
}
|
||||
|
||||
if encoding != "" {
|
||||
return config.DecodeContent(data, encoding)
|
||||
}
|
||||
return []byte(data), nil
|
||||
}
|
||||
|
||||
func (v vmware) Type() string {
|
||||
return "vmware"
|
||||
}
|
101
config/cloudinit/datasource/vmware/vmware_amd64.go
Normal file
101
config/cloudinit/datasource/vmware/vmware_amd64.go
Normal file
@@ -0,0 +1,101 @@
|
||||
// Copyright 2015 CoreOS, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package vmware
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/rancher/os/config/cloudinit/pkg"
|
||||
|
||||
"github.com/sigma/vmw-guestinfo/rpcvmx"
|
||||
"github.com/sigma/vmw-guestinfo/vmcheck"
|
||||
"github.com/sigma/vmw-ovflib"
|
||||
)
|
||||
|
||||
type ovfWrapper struct {
|
||||
env *ovf.OvfEnvironment
|
||||
}
|
||||
|
||||
func (ovf ovfWrapper) readConfig(key string) (string, error) {
|
||||
return ovf.env.Properties["guestinfo."+key], nil
|
||||
}
|
||||
|
||||
func NewDatasource(fileName string) *vmware {
|
||||
// read from provided ovf environment document (typically /media/ovfenv/ovf-env.xml)
|
||||
if fileName != "" {
|
||||
log.Printf("Using OVF environment from %s\n", fileName)
|
||||
ovfEnv, err := ioutil.ReadFile(fileName)
|
||||
if err != nil {
|
||||
ovfEnv = make([]byte, 0)
|
||||
}
|
||||
return &vmware{
|
||||
ovfFileName: fileName,
|
||||
readConfig: getOvfReadConfig(ovfEnv),
|
||||
urlDownload: urlDownload,
|
||||
}
|
||||
}
|
||||
|
||||
// try to read ovf environment from VMware tools
|
||||
data, err := readConfig("ovfenv")
|
||||
if err == nil && data != "" {
|
||||
log.Printf("Using OVF environment from guestinfo\n")
|
||||
return &vmware{
|
||||
readConfig: getOvfReadConfig([]byte(data)),
|
||||
urlDownload: urlDownload,
|
||||
}
|
||||
}
|
||||
|
||||
// if everything fails, fallback to directly reading variables from the backdoor
|
||||
log.Printf("Using guestinfo variables\n")
|
||||
return &vmware{
|
||||
readConfig: readConfig,
|
||||
urlDownload: urlDownload,
|
||||
}
|
||||
}
|
||||
|
||||
func (v vmware) IsAvailable() bool {
|
||||
if v.ovfFileName != "" {
|
||||
_, err := os.Stat(v.ovfFileName)
|
||||
return !os.IsNotExist(err)
|
||||
}
|
||||
return vmcheck.IsVirtualWorld()
|
||||
}
|
||||
|
||||
func readConfig(key string) (string, error) {
|
||||
data, err := rpcvmx.NewConfig().String(key, "")
|
||||
if err == nil {
|
||||
log.Printf("Read from %q: %q\n", key, data)
|
||||
} else {
|
||||
log.Printf("Failed to read from %q: %v\n", key, err)
|
||||
}
|
||||
return data, err
|
||||
}
|
||||
|
||||
func getOvfReadConfig(ovfEnv []byte) readConfigFunction {
|
||||
env := &ovf.OvfEnvironment{}
|
||||
if len(ovfEnv) != 0 {
|
||||
env = ovf.ReadEnvironment(ovfEnv)
|
||||
}
|
||||
|
||||
wrapper := ovfWrapper{env}
|
||||
return wrapper.readConfig
|
||||
}
|
||||
|
||||
func urlDownload(url string) ([]byte, error) {
|
||||
client := pkg.NewHttpClient()
|
||||
return client.GetRetry(url)
|
||||
}
|
298
config/cloudinit/datasource/vmware/vmware_test.go
Normal file
298
config/cloudinit/datasource/vmware/vmware_test.go
Normal file
@@ -0,0 +1,298 @@
|
||||
// Copyright 2015 CoreOS, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package vmware
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/rancher/os/config/cloudinit/datasource"
|
||||
)
|
||||
|
||||
type MockHypervisor map[string]string
|
||||
|
||||
func (h MockHypervisor) ReadConfig(key string) (string, error) {
|
||||
return h[key], nil
|
||||
}
|
||||
|
||||
var fakeDownloader urlDownloadFunction = func(url string) ([]byte, error) {
|
||||
mapping := map[string]struct {
|
||||
data []byte
|
||||
err error
|
||||
}{
|
||||
"http://good.example.com": {[]byte("test config"), nil},
|
||||
"http://bad.example.com": {nil, errors.New("Not found")},
|
||||
}
|
||||
val := mapping[url]
|
||||
return val.data, val.err
|
||||
}
|
||||
|
||||
func TestFetchMetadata(t *testing.T) {
|
||||
tests := []struct {
|
||||
variables MockHypervisor
|
||||
|
||||
metadata datasource.Metadata
|
||||
err error
|
||||
}{
|
||||
{
|
||||
variables: map[string]string{
|
||||
"interface.0.mac": "test mac",
|
||||
"interface.0.dhcp": "yes",
|
||||
},
|
||||
metadata: datasource.Metadata{
|
||||
NetworkConfig: map[string]string{
|
||||
"interface.0.mac": "test mac",
|
||||
"interface.0.dhcp": "yes",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
variables: map[string]string{
|
||||
"interface.0.name": "test name",
|
||||
"interface.0.dhcp": "yes",
|
||||
},
|
||||
metadata: datasource.Metadata{
|
||||
NetworkConfig: map[string]string{
|
||||
"interface.0.name": "test name",
|
||||
"interface.0.dhcp": "yes",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
variables: map[string]string{
|
||||
"hostname": "test host",
|
||||
"interface.0.mac": "test mac",
|
||||
"interface.0.role": "private",
|
||||
"interface.0.ip.0.address": "fe00::100/64",
|
||||
"interface.0.route.0.gateway": "fe00::1",
|
||||
"interface.0.route.0.destination": "::",
|
||||
},
|
||||
metadata: datasource.Metadata{
|
||||
Hostname: "test host",
|
||||
PrivateIPv6: net.ParseIP("fe00::100"),
|
||||
NetworkConfig: map[string]string{
|
||||
"interface.0.mac": "test mac",
|
||||
"interface.0.ip.0.address": "fe00::100/64",
|
||||
"interface.0.route.0.gateway": "fe00::1",
|
||||
"interface.0.route.0.destination": "::",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
variables: map[string]string{
|
||||
"hostname": "test host",
|
||||
"interface.0.name": "test name",
|
||||
"interface.0.role": "public",
|
||||
"interface.0.ip.0.address": "10.0.0.100/24",
|
||||
"interface.0.ip.1.address": "10.0.0.101/24",
|
||||
"interface.0.route.0.gateway": "10.0.0.1",
|
||||
"interface.0.route.0.destination": "0.0.0.0",
|
||||
"interface.1.mac": "test mac",
|
||||
"interface.1.role": "private",
|
||||
"interface.1.route.0.gateway": "10.0.0.2",
|
||||
"interface.1.route.0.destination": "0.0.0.0",
|
||||
"interface.1.ip.0.address": "10.0.0.102/24",
|
||||
},
|
||||
metadata: datasource.Metadata{
|
||||
Hostname: "test host",
|
||||
PublicIPv4: net.ParseIP("10.0.0.101"),
|
||||
PrivateIPv4: net.ParseIP("10.0.0.102"),
|
||||
NetworkConfig: map[string]string{
|
||||
"interface.0.name": "test name",
|
||||
"interface.0.ip.0.address": "10.0.0.100/24",
|
||||
"interface.0.ip.1.address": "10.0.0.101/24",
|
||||
"interface.0.route.0.gateway": "10.0.0.1",
|
||||
"interface.0.route.0.destination": "0.0.0.0",
|
||||
"interface.1.mac": "test mac",
|
||||
"interface.1.route.0.gateway": "10.0.0.2",
|
||||
"interface.1.route.0.destination": "0.0.0.0",
|
||||
"interface.1.ip.0.address": "10.0.0.102/24",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
v := vmware{readConfig: tt.variables.ReadConfig}
|
||||
metadata, err := v.FetchMetadata()
|
||||
if !reflect.DeepEqual(tt.err, err) {
|
||||
t.Errorf("bad error (#%d): want %v, got %v", i, tt.err, err)
|
||||
}
|
||||
if !reflect.DeepEqual(tt.metadata, metadata) {
|
||||
t.Errorf("bad metadata (#%d): want %#v, got %#v", i, tt.metadata, metadata)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetchUserdata(t *testing.T) {
|
||||
tests := []struct {
|
||||
variables MockHypervisor
|
||||
|
||||
userdata string
|
||||
err error
|
||||
}{
|
||||
{},
|
||||
{
|
||||
variables: map[string]string{"coreos.config.data": "test config"},
|
||||
userdata: "test config",
|
||||
},
|
||||
{
|
||||
variables: map[string]string{
|
||||
"coreos.config.data.encoding": "",
|
||||
"coreos.config.data": "test config",
|
||||
},
|
||||
userdata: "test config",
|
||||
},
|
||||
{
|
||||
variables: map[string]string{
|
||||
"coreos.config.data.encoding": "base64",
|
||||
"coreos.config.data": "dGVzdCBjb25maWc=",
|
||||
},
|
||||
userdata: "test config",
|
||||
},
|
||||
{
|
||||
variables: map[string]string{
|
||||
"coreos.config.data.encoding": "gzip+base64",
|
||||
"coreos.config.data": "H4sIABaoWlUAAytJLS5RSM7PS8tMBwCQiHNZCwAAAA==",
|
||||
},
|
||||
userdata: "test config",
|
||||
},
|
||||
{
|
||||
variables: map[string]string{
|
||||
"coreos.config.data.encoding": "test encoding",
|
||||
},
|
||||
err: errors.New(`Unsupported encoding "test encoding"`),
|
||||
},
|
||||
{
|
||||
variables: map[string]string{
|
||||
"coreos.config.url": "http://good.example.com",
|
||||
},
|
||||
userdata: "test config",
|
||||
},
|
||||
{
|
||||
variables: map[string]string{
|
||||
"coreos.config.url": "http://bad.example.com",
|
||||
},
|
||||
err: errors.New("Not found"),
|
||||
},
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
v := vmware{
|
||||
readConfig: tt.variables.ReadConfig,
|
||||
urlDownload: fakeDownloader,
|
||||
}
|
||||
userdata, err := v.FetchUserdata()
|
||||
if !reflect.DeepEqual(tt.err, err) {
|
||||
t.Errorf("bad error (#%d): want %v, got %v", i, tt.err, err)
|
||||
}
|
||||
if tt.userdata != string(userdata) {
|
||||
t.Errorf("bad userdata (#%d): want %q, got %q", i, tt.userdata, userdata)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetchUserdataError(t *testing.T) {
|
||||
testErr := errors.New("test error")
|
||||
_, err := vmware{readConfig: func(_ string) (string, error) { return "", testErr }}.FetchUserdata()
|
||||
|
||||
if testErr != err {
|
||||
t.Errorf("bad error: want %v, got %v", testErr, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOvfTransport(t *testing.T) {
|
||||
tests := []struct {
|
||||
document string
|
||||
|
||||
metadata datasource.Metadata
|
||||
userdata []byte
|
||||
}{
|
||||
{
|
||||
document: `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Environment xmlns="http://schemas.dmtf.org/ovf/environment/1"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:oe="http://schemas.dmtf.org/ovf/environment/1"
|
||||
oe:id="CoreOS-vmw">
|
||||
<PlatformSection>
|
||||
<Kind>VMware ESXi</Kind>
|
||||
<Version>5.5.0</Version>
|
||||
<Vendor>VMware, Inc.</Vendor>
|
||||
<Locale>en</Locale>
|
||||
</PlatformSection>
|
||||
<PropertySection>
|
||||
<Property oe:key="foo" oe:value="42"/>
|
||||
<Property oe:key="guestinfo.coreos.config.url" oe:value="http://good.example.com"/>
|
||||
<Property oe:key="guestinfo.hostname" oe:value="test host"/>
|
||||
<Property oe:key="guestinfo.interface.0.name" oe:value="test name"/>
|
||||
<Property oe:key="guestinfo.interface.0.role" oe:value="public"/>
|
||||
<Property oe:key="guestinfo.interface.0.ip.0.address" oe:value="10.0.0.100/24"/>
|
||||
<Property oe:key="guestinfo.interface.0.ip.1.address" oe:value="10.0.0.101/24"/>
|
||||
<Property oe:key="guestinfo.interface.0.route.0.gateway" oe:value="10.0.0.1"/>
|
||||
<Property oe:key="guestinfo.interface.0.route.0.destination" oe:value="0.0.0.0"/>
|
||||
<Property oe:key="guestinfo.interface.1.mac" oe:value="test mac"/>
|
||||
<Property oe:key="guestinfo.interface.1.role" oe:value="private"/>
|
||||
<Property oe:key="guestinfo.interface.1.route.0.gateway" oe:value="10.0.0.2"/>
|
||||
<Property oe:key="guestinfo.interface.1.route.0.destination" oe:value="0.0.0.0"/>
|
||||
<Property oe:key="guestinfo.interface.1.ip.0.address" oe:value="10.0.0.102/24"/>
|
||||
</PropertySection>
|
||||
</Environment>`,
|
||||
metadata: datasource.Metadata{
|
||||
Hostname: "test host",
|
||||
PublicIPv4: net.ParseIP("10.0.0.101"),
|
||||
PrivateIPv4: net.ParseIP("10.0.0.102"),
|
||||
NetworkConfig: map[string]string{
|
||||
"interface.0.name": "test name",
|
||||
"interface.0.ip.0.address": "10.0.0.100/24",
|
||||
"interface.0.ip.1.address": "10.0.0.101/24",
|
||||
"interface.0.route.0.gateway": "10.0.0.1",
|
||||
"interface.0.route.0.destination": "0.0.0.0",
|
||||
"interface.1.mac": "test mac",
|
||||
"interface.1.route.0.gateway": "10.0.0.2",
|
||||
"interface.1.route.0.destination": "0.0.0.0",
|
||||
"interface.1.ip.0.address": "10.0.0.102/24",
|
||||
},
|
||||
},
|
||||
userdata: []byte("test config"),
|
||||
},
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
file, err := ioutil.TempFile(os.TempDir(), "ovf")
|
||||
if err != nil {
|
||||
t.Errorf("error creating ovf file (#%d)", i)
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
|
||||
file.WriteString(tt.document)
|
||||
v := NewDatasource(file.Name())
|
||||
v.urlDownload = fakeDownloader
|
||||
|
||||
metadata, err := v.FetchMetadata()
|
||||
userdata, err := v.FetchUserdata()
|
||||
|
||||
if !reflect.DeepEqual(tt.metadata, metadata) {
|
||||
t.Errorf("bad metadata (#%d): want %#v, got %#v", i, tt.metadata, metadata)
|
||||
}
|
||||
if !reflect.DeepEqual(tt.userdata, userdata) {
|
||||
t.Errorf("bad userdata (#%d): want %#v, got %#v", i, tt.userdata, userdata)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
25
config/cloudinit/datasource/vmware/vmware_unsupported.go
Normal file
25
config/cloudinit/datasource/vmware/vmware_unsupported.go
Normal file
@@ -0,0 +1,25 @@
|
||||
// Copyright 2015 CoreOS, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// +build !amd64
|
||||
|
||||
package vmware
|
||||
|
||||
func NewDatasource(fileName string) *vmware {
|
||||
return &vmware{}
|
||||
}
|
||||
|
||||
func (v vmware) IsAvailable() bool {
|
||||
return false
|
||||
}
|
Reference in New Issue
Block a user