forked from coreos/ignition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmware_amd64.go
100 lines (84 loc) · 3.04 KB
/
vmware_amd64.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// 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.
// The vmware provider fetches a configuration from the VMware Guest Info
// interface.
package vmware
import (
"github.com/coreos/ignition/config/validate/report"
"github.com/coreos/ignition/internal/config/types"
"github.com/coreos/ignition/internal/providers"
"github.com/coreos/ignition/internal/providers/util"
"github.com/coreos/ignition/internal/resource"
"github.com/sigma/vmw-guestinfo/rpcvmx"
"github.com/sigma/vmw-guestinfo/vmcheck"
"github.com/vmware/vmw-ovflib"
)
func FetchConfig(f resource.Fetcher) (types.Config, report.Report, error) {
if !vmcheck.IsVirtualWorld() {
return types.Config{}, report.Report{}, providers.ErrNoProvider
}
config, err := fetchRawConfig(f)
if err != nil {
return types.Config{}, report.Report{}, err
}
decodedData, err := decodeConfig(config)
if err != nil {
f.Logger.Debug("failed to decode config: %v", err)
return types.Config{}, report.Report{}, err
}
f.Logger.Debug("config successfully fetched")
return util.ParseConfig(f.Logger, decodedData)
}
func fetchRawConfig(f resource.Fetcher) (config, error) {
info := rpcvmx.NewConfig()
var ovfData string
var ovfEncoding string
var ovfDataKey string
var ovfEncodingKey string
ovfEnv, err := info.String("ovfenv", "")
if err != nil {
f.Logger.Warning("failed to fetch ovfenv: %v. Continuing...", err)
} else if ovfEnv != "" {
f.Logger.Debug("using OVF environment from guestinfo")
env, err := ovf.ReadEnvironment([]byte(ovfEnv))
if err != nil {
f.Logger.Warning("failed to parse OVF environment: %v. Continuing...", err)
}
if _, ok := env.Properties["guestinfo.coreos.config.data"]; ok {
ovfDataKey = "guestinfo.coreos.config.data"
ovfEncodingKey = "guestinfo.coreos.config.data.encoding"
} else if _, ok := env.Properties["guestinfo.ignition.config.data"]; ok {
ovfDataKey = "guestinfo.ignition.config.data"
ovfEncodingKey = "guestinfo.ignition.config.data.encoding"
} else {
f.Logger.Debug("failed to find guestinfo ignition properties")
}
ovfData = env.Properties[ovfDataKey]
ovfEncoding = env.Properties[ovfEncodingKey]
}
data, err := info.String(ovfDataKey[len("guestinfo."):], ovfData)
if err != nil {
f.Logger.Debug("failed to fetch config: %v", err)
return config{}, err
}
encoding, err := info.String(ovfEncodingKey[len("guestinfo."):], ovfEncoding)
if err != nil {
f.Logger.Debug("failed to fetch config encoding: %v", err)
return config{}, err
}
return config{
data: data,
encoding: encoding,
}, nil
}