Skip to content

Commit 788e9dc

Browse files
rancher-sy-botniusmallnan
authored andcommitted
Add config validation when reboot & shutdown
1 parent eb762f9 commit 788e9dc

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

cmd/power/power.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,23 @@ func reboot(name string, force bool, code uint) {
131131
log.Fatalf("%s: Need to be root", os.Args[0])
132132
}
133133

134-
// Add shutdown timeout
135134
cfg := config.LoadConfig()
135+
136+
// Validate config
137+
if !force {
138+
_, validationErrors, err := config.LoadConfigWithError()
139+
if err != nil {
140+
log.Fatal(err)
141+
}
142+
if validationErrors != nil && !validationErrors.Valid() {
143+
for _, validationError := range validationErrors.Errors() {
144+
log.Error(validationError)
145+
}
146+
return
147+
}
148+
}
149+
150+
// Add shutdown timeout
136151
timeoutValue := cfg.Rancher.ShutdownTimeout
137152
if timeoutValue == 0 {
138153
timeoutValue = 60

config/disk.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
yaml "github.com/cloudfoundry-incubator/candiedyaml"
1919
"github.com/docker/engine-api/types"
2020
composeConfig "github.com/docker/libcompose/config"
21+
"github.com/xeipuuv/gojsonschema"
2122
)
2223

2324
func ReadConfig(bytes []byte, substituteMetadataVars bool, files ...string) (*CloudConfig, error) {
@@ -48,6 +49,21 @@ func loadRawDiskConfig(dirPrefix string, full bool) map[interface{}]interface{}
4849
return util.Merge(rawCfg, additionalCfgs)
4950
}
5051

52+
func loadRawDiskConfigWithError(dirPrefix string, full bool) (map[interface{}]interface{}, error) {
53+
var rawCfg map[interface{}]interface{}
54+
rawCfg, err := readConfigs(nil, true, true, OsConfigFile, OemConfigFile)
55+
if err != nil {
56+
return nil, err
57+
}
58+
files := CloudConfigDirFiles(dirPrefix)
59+
files = append(files, path.Join(dirPrefix, CloudConfigFile))
60+
additionalCfgs, err := readConfigs(nil, true, true, files...)
61+
if err != nil {
62+
return nil, err
63+
}
64+
return util.Merge(rawCfg, additionalCfgs), nil
65+
}
66+
5167
func loadRawConfig(dirPrefix string, full bool) map[interface{}]interface{} {
5268
rawCfg := loadRawDiskConfig(dirPrefix, full)
5369
procCmdline, err := cmdline.Read(false)
@@ -60,6 +76,21 @@ func loadRawConfig(dirPrefix string, full bool) map[interface{}]interface{} {
6076
return mergeMetadata(rawCfg, readMetadata())
6177
}
6278

79+
func loadRawConfigWithError(dirPrefix string, full bool) (map[interface{}]interface{}, error) {
80+
rawCfg, err := loadRawDiskConfigWithError(dirPrefix, full)
81+
if err != nil {
82+
return nil, err
83+
}
84+
procCmdline, err := cmdline.Read(false)
85+
if err != nil {
86+
log.WithFields(log.Fields{"err": err}).Error("Failed to read kernel params")
87+
}
88+
rawCfg = util.Merge(rawCfg, procCmdline)
89+
rawCfg = util.Merge(rawCfg, readElidedCmdline(rawCfg))
90+
rawCfg = applyDebugFlags(rawCfg)
91+
return mergeMetadata(rawCfg, readMetadata()), nil
92+
}
93+
6394
func LoadConfig() *CloudConfig {
6495
cfg := LoadConfigWithPrefix("")
6596

@@ -72,6 +103,21 @@ func LoadConfig() *CloudConfig {
72103
return cfg
73104
}
74105

106+
func LoadConfigWithError() (*CloudConfig, *gojsonschema.Result, error) {
107+
rawCfg, err := loadRawConfigWithError("", true)
108+
if err != nil {
109+
return &CloudConfig{}, nil, err
110+
}
111+
cfg := &CloudConfig{}
112+
if err := util.Convert(rawCfg, cfg); err != nil {
113+
validationErrors, err := ValidateRawCfg(rawCfg)
114+
return &CloudConfig{}, validationErrors, err
115+
}
116+
cfg = amendNils(cfg)
117+
cfg = amendContainerNames(cfg)
118+
return cfg, nil, nil
119+
}
120+
75121
func LoadConfigWithPrefix(dirPrefix string) *CloudConfig {
76122
rawCfg := loadRawConfig(dirPrefix, true)
77123

0 commit comments

Comments
 (0)