1
0
mirror of https://github.com/rancher/os.git synced 2025-08-31 06:11:12 +00:00

Tell the user if the interpreted cloud confg is invalid and then exit

Signed-off-by: Sven Dowideit <SvenDowideit@home.org.au>
This commit is contained in:
Sven Dowideit
2017-07-17 12:17:41 +10:00
parent 7fb9afe39c
commit cbcdb2628c
4 changed files with 19 additions and 3 deletions

View File

@@ -255,7 +255,7 @@ func validate(c *cli.Context) error {
if err != nil {
log.Fatal(err)
}
validationErrors, err := config.Validate(bytes)
validationErrors, err := config.ValidateBytes(bytes)
if err != nil {
log.Fatal(err)
}

View File

@@ -71,8 +71,19 @@ func LoadConfigWithPrefix(dirPrefix string) *CloudConfig {
cfg := &CloudConfig{}
if err := util.Convert(rawCfg, cfg); err != nil {
log.Errorf("Failed to parse configuration: %s", err)
log.Errorf("EXITING: Failed to parse configuration: %s", err)
log.Debugf("Bad cfg:\n%v\n", rawCfg)
// no point returning {}, it'll just sit there broken
// TODO: print some context around what failed..
validationErrors, err := ValidateRawCfg(rawCfg)
if err != nil {
log.Fatal(err)
}
for _, validationError := range validationErrors.Errors() {
log.Error(validationError)
}
// TODO: I'd love to panic & recover(), for issues on boot, but it doesn't work yet
os.Exit(-1)
return &CloudConfig{}
}
cfg = amendNils(cfg)

View File

@@ -31,11 +31,15 @@ func ConvertKeysToStrings(item interface{}) interface{} {
}
}
func Validate(bytes []byte) (*gojsonschema.Result, error) {
func ValidateBytes(bytes []byte) (*gojsonschema.Result, error) {
var rawCfg map[string]interface{}
if err := yaml.Unmarshal([]byte(bytes), &rawCfg); err != nil {
return nil, err
}
return ValidateRawCfg(rawCfg)
}
func ValidateRawCfg(rawCfg interface{}) (*gojsonschema.Result, error) {
rawCfg = ConvertKeysToStrings(rawCfg).(map[string]interface{})
loader := gojsonschema.NewGoLoader(rawCfg)
schemaLoader := gojsonschema.NewStringLoader(schema)

View File

@@ -94,6 +94,7 @@ func sysInit(c *config.CloudConfig) (*config.CloudConfig, error) {
func MainInit() {
log.InitDeferedLogger()
// TODO: this breaks and does nothing if the cfg is invalid (or is it due to threading?)
defer func() {
if r := recover(); r != nil {
fmt.Printf("Starting Recovery console: %v\n", r)