-
Notifications
You must be signed in to change notification settings - Fork 242
/
system.go
108 lines (96 loc) · 3.73 KB
/
system.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
// Copyright 2017 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 system
import (
"os"
"path/filepath"
latest "github.com/coreos/ignition/v2/config/v3_5_experimental"
"github.com/coreos/ignition/v2/config/v3_5_experimental/types"
"github.com/coreos/ignition/v2/internal/distro"
"github.com/coreos/ignition/v2/internal/log"
"github.com/coreos/ignition/v2/internal/platform"
"github.com/coreos/ignition/v2/internal/providers/util"
"github.com/coreos/ignition/v2/internal/resource"
"github.com/coreos/vcontext/report"
)
const (
userFilename = "user.ign"
)
var (
// we are a special-cased system provider; don't register ourselves
// for lookup by name
Config = platform.NewConfig(platform.Provider{
Name: "system",
Fetch: fetchConfig,
})
)
// FetchBaseConfig fetches base config fragments from the `base.d` and platform config fragments from
// the `base.platform.d/platform`(if available), and merge them in the right order.
func FetchBaseConfig(logger *log.Logger, platformName string) (types.Config, report.Report, error) {
fullBaseConfig, fullReport, err := fetchBaseDirectoryConfig(logger, "base.d")
if err != nil {
return types.Config{}, fullReport, err
}
platformDir := filepath.Join("base.platform.d", platformName)
basePlatformDConfig, basePlatformDReport, err := fetchBaseDirectoryConfig(logger, platformDir)
if err != nil {
logger.Info("no config at %q: %v", platformDir, err)
}
fullBaseConfig = latest.Merge(fullBaseConfig, basePlatformDConfig)
fullReport.Merge(basePlatformDReport)
return fullBaseConfig, fullReport, nil
}
func fetchConfig(f *resource.Fetcher) (types.Config, report.Report, error) {
return doFetchConfig(f.Logger, userFilename)
}
func doFetchConfig(logger *log.Logger, filename string) (types.Config, report.Report, error) {
path := filepath.Join(distro.SystemConfigDir(), filename)
logger.Info("reading system config file %q", path)
rawConfig, err := os.ReadFile(path)
if os.IsNotExist(err) {
logger.Info("no config at %q", path)
return types.Config{}, report.Report{}, platform.ErrNoProvider
} else if err != nil {
logger.Err("couldn't read config %q: %v", path, err)
return types.Config{}, report.Report{}, err
}
return util.ParseConfig(logger, rawConfig)
}
// fetchBaseDirectoryConfig is a helper function to merge all the base config fragments inside of a particular directory.
func fetchBaseDirectoryConfig(logger *log.Logger, dir string) (types.Config, report.Report, error) {
var baseConfig types.Config
var report report.Report
path := filepath.Join(distro.SystemConfigDir(), dir)
configs, err := os.ReadDir(path)
if os.IsNotExist(err) {
logger.Info("no config dir at %q", path)
return types.Config{}, report, nil
} else if err != nil {
logger.Err("couldn't read config dir %q: %v", path, err)
return types.Config{}, report, err
}
if len(configs) == 0 {
logger.Info("no configs at %q", path)
return types.Config{}, report, nil
}
for _, config := range configs {
intermediateConfig, intermediateReport, err := doFetchConfig(logger, filepath.Join(dir, config.Name()))
if err != nil {
return types.Config{}, intermediateReport, err
}
baseConfig = latest.Merge(baseConfig, intermediateConfig)
report.Merge(intermediateReport)
}
return baseConfig, report, nil
}