-
Notifications
You must be signed in to change notification settings - Fork 247
/
engine.go
294 lines (259 loc) · 9.07 KB
/
engine.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// 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 exec
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"os"
"time"
"github.com/coreos/ignition/config/shared/errors"
configUtil "github.com/coreos/ignition/config/util"
"github.com/coreos/ignition/config/validate/report"
"github.com/coreos/ignition/internal/config"
"github.com/coreos/ignition/internal/config/types"
"github.com/coreos/ignition/internal/exec/stages"
"github.com/coreos/ignition/internal/log"
"github.com/coreos/ignition/internal/oem"
"github.com/coreos/ignition/internal/providers"
"github.com/coreos/ignition/internal/providers/cmdline"
"github.com/coreos/ignition/internal/providers/system"
"github.com/coreos/ignition/internal/resource"
"github.com/coreos/ignition/internal/util"
)
const (
DefaultFetchTimeout = time.Minute
)
// Engine represents the entity that fetches and executes a configuration.
type Engine struct {
ConfigCache string
FetchTimeout time.Duration
Logger *log.Logger
Root string
OEMConfig oem.Config
Fetcher *resource.Fetcher
}
// Run executes the stage of the given name. It returns true if the stage
// successfully ran and false if there were any errors.
func (e Engine) Run(stageName string) bool {
if e.Fetcher == nil || e.Logger == nil {
fmt.Fprintf(os.Stderr, "engine incorrectly configured\n")
return false
}
baseConfig := types.Config{
Ignition: types.Ignition{Version: types.MaxVersion.String()},
Storage: types.Storage{
Filesystems: []types.Filesystem{{
Name: "root",
Path: configUtil.StrToPtr(e.Root),
}},
},
}
systemBaseConfig, r, err := system.FetchBaseConfig(e.Logger)
e.logReport(r)
if err != nil && err != providers.ErrNoProvider {
e.Logger.Crit("failed to acquire system base config: %v", err)
return false
}
cfg, err := e.acquireConfig()
switch err {
case nil:
case errors.ErrCloudConfig, errors.ErrScript, errors.ErrEmpty:
e.Logger.Info("%v: ignoring user-provided config", err)
cfg, r, err = system.FetchDefaultConfig(e.Logger)
e.logReport(r)
if err != nil && err != providers.ErrNoProvider {
e.Logger.Crit("failed to acquire default config: %v", err)
return false
}
default:
e.Logger.Crit("failed to acquire config: %v", err)
return false
}
e.Logger.PushPrefix(stageName)
defer e.Logger.PopPrefix()
return stages.Get(stageName).Create(e.Logger, e.Root, *e.Fetcher).Run(config.Append(baseConfig, config.Append(systemBaseConfig, cfg)))
}
// acquireConfig returns the configuration, first checking a local cache
// before attempting to fetch it from the provider.
func (e *Engine) acquireConfig() (cfg types.Config, err error) {
// First try read the config @ e.ConfigCache.
b, err := ioutil.ReadFile(e.ConfigCache)
if err == nil {
if err = json.Unmarshal(b, &cfg); err != nil {
e.Logger.Crit("failed to parse cached config: %v", err)
}
// Create an http client and fetcher with the timeouts from the cached
// config
err = e.Fetcher.UpdateHttpTimeoutsAndCAs(cfg.Ignition.Timeouts, cfg.Ignition.Security.TLS.CertificateAuthorities)
if err != nil {
e.Logger.Crit("failed to update timeouts and CAs for fetcher: %v", err)
return
}
return
}
// Create a new http client and fetcher with the timeouts set via the flags,
// since we don't have a config with timeout values we can use
timeout := int(e.FetchTimeout.Seconds())
err = e.Fetcher.UpdateHttpTimeoutsAndCAs(types.Timeouts{HTTPTotal: &timeout}, nil)
if err != nil {
e.Logger.Crit("failed to update timeouts and CAs for fetcher: %v", err)
return
}
// (Re)Fetch the config if the cache is unreadable.
cfg, err = e.fetchProviderConfig()
if err != nil {
e.Logger.Warning("failed to fetch config: %s", err)
return
}
// Update the http client to use the timeouts and CAs from the newly fetched
// config
err = e.Fetcher.UpdateHttpTimeoutsAndCAs(cfg.Ignition.Timeouts, cfg.Ignition.Security.TLS.CertificateAuthorities)
if err != nil {
e.Logger.Crit("failed to update timeouts and CAs for fetcher: %v", err)
return
}
err = e.Fetcher.RewriteCAsWithDataUrls(cfg.Ignition.Security.TLS.CertificateAuthorities)
if err != nil {
e.Logger.Crit("error handling CAs: %v", err)
return
}
// Populate the config cache.
b, err = json.Marshal(cfg)
if err != nil {
e.Logger.Crit("failed to marshal cached config: %v", err)
return
}
if err = ioutil.WriteFile(e.ConfigCache, b, 0640); err != nil {
e.Logger.Crit("failed to write cached config: %v", err)
return
}
return
}
// fetchProviderConfig returns the externally-provided configuration. It first
// checks to see if the command-line option is present. If so, it uses that
// source for the configuration. If the command-line option is not present, it
// checks for a user config in the system config dir. If that is also missing,
// it checks the config engine's provider. An error is returned if the provider
// is unavailable. This will also render the config (see renderConfig) before
// returning.
func (e *Engine) fetchProviderConfig() (types.Config, error) {
fetchers := []providers.FuncFetchConfig{
cmdline.FetchConfig,
system.FetchConfig,
e.OEMConfig.FetchFunc(),
}
var cfg types.Config
var r report.Report
var err error
for _, fetcher := range fetchers {
cfg, r, err = fetcher(*e.Fetcher)
if err != providers.ErrNoProvider {
// successful, or failed on another error
break
}
}
e.logReport(r)
if err != nil {
return types.Config{}, err
}
// Replace the HTTP client in the fetcher to be configured with the
// timeouts of the config
err = e.Fetcher.UpdateHttpTimeoutsAndCAs(cfg.Ignition.Timeouts, cfg.Ignition.Security.TLS.CertificateAuthorities)
if err != nil {
return types.Config{}, err
}
return e.renderConfig(cfg)
}
// renderConfig evaluates "ignition.config.replace" and "ignition.config.append"
// in the given config and returns the result. If "ignition.config.replace" is
// set, the referenced and evaluted config will be returned. Otherwise, if
// "ignition.config.append" is set, each of the referenced configs will be
// evaluated and appended to the provided config. If neither option is set, the
// provided config will be returned unmodified. An updated fetcher will be
// returned with any new timeouts set.
func (e *Engine) renderConfig(cfg types.Config) (types.Config, error) {
if cfgRef := cfg.Ignition.Config.Replace; cfgRef != nil {
newCfg, err := e.fetchReferencedConfig(*cfgRef)
if err != nil {
return types.Config{}, err
}
// Replace the HTTP client in the fetcher to be configured with the
// timeouts of the new config
err = e.Fetcher.UpdateHttpTimeoutsAndCAs(newCfg.Ignition.Timeouts, newCfg.Ignition.Security.TLS.CertificateAuthorities)
if err != nil {
return types.Config{}, err
}
return e.renderConfig(newCfg)
}
appendedCfg := cfg
for _, cfgRef := range cfg.Ignition.Config.Append {
newCfg, err := e.fetchReferencedConfig(cfgRef)
if err != nil {
return types.Config{}, err
}
// Append the old config with the new config before the new config has
// been rendered, so we can use the new config's timeouts and CAs when
// fetching more configs.
cfgForFetcherSettings := config.Append(appendedCfg, newCfg)
err = e.Fetcher.UpdateHttpTimeoutsAndCAs(cfgForFetcherSettings.Ignition.Timeouts, cfgForFetcherSettings.Ignition.Security.TLS.CertificateAuthorities)
if err != nil {
return types.Config{}, err
}
newCfg, err = e.renderConfig(newCfg)
if err != nil {
return types.Config{}, err
}
appendedCfg = config.Append(appendedCfg, newCfg)
}
return appendedCfg, nil
}
// fetchReferencedConfig fetches and parses the requested config.
func (e *Engine) fetchReferencedConfig(cfgRef types.ConfigReference) (types.Config, error) {
u, err := url.Parse(cfgRef.Source)
if err != nil {
return types.Config{}, err
}
rawCfg, err := e.Fetcher.FetchToBuffer(*u, resource.FetchOptions{
Headers: resource.ConfigHeaders,
})
if err != nil {
return types.Config{}, err
}
e.Logger.Debug("fetched referenced config: %s", string(rawCfg))
if err := util.AssertValid(cfgRef.Verification, rawCfg); err != nil {
return types.Config{}, err
}
cfg, r, err := config.Parse(rawCfg)
e.logReport(r)
if err != nil {
return types.Config{}, err
}
return cfg, nil
}
func (e Engine) logReport(r report.Report) {
for _, entry := range r.Entries {
switch entry.Kind {
case report.EntryError:
e.Logger.Crit("%v", entry)
case report.EntryWarning:
e.Logger.Warning("%v", entry)
case report.EntryDeprecated:
e.Logger.Warning("%v: the provided config format is deprecated and will not be supported in the future.", entry)
case report.EntryInfo:
e.Logger.Info("%v", entry)
}
}
}