forked from vmware/vic
/
update.go
93 lines (79 loc) · 3.09 KB
/
update.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
// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
//
// 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 validate
import (
"context"
"github.com/vmware/vic/lib/config"
"github.com/vmware/vic/pkg/errors"
"github.com/vmware/vic/pkg/trace"
"github.com/vmware/vic/pkg/version"
)
// MigrateConfig migrate old VCH configuration to new version. Currently check required fields only
func (v *Validator) ValidateMigratedConfig(ctx context.Context, conf *config.VirtualContainerHostConfigSpec) (*config.VirtualContainerHostConfigSpec, error) {
defer trace.End(trace.Begin(conf.Name, ctx))
v.assertTarget(ctx, conf)
v.assertDatastore(ctx, conf)
v.assertNetwork(ctx, conf)
if err := v.ListIssues(ctx); err != nil {
return conf, err
}
return v.migrateData(ctx, conf)
}
func (v *Validator) migrateData(ctx context.Context, conf *config.VirtualContainerHostConfigSpec) (*config.VirtualContainerHostConfigSpec, error) {
conf.Version = version.GetBuild()
return conf, nil
}
func (v *Validator) assertNetwork(ctx context.Context, conf *config.VirtualContainerHostConfigSpec) {
// minimum network configuration check
}
// assertDatastore check required datastore configuration only
func (v *Validator) assertDatastore(ctx context.Context, conf *config.VirtualContainerHostConfigSpec) {
defer trace.End(trace.Begin("", ctx))
if len(conf.ImageStores) == 0 {
v.NoteIssue(errors.New("Image store is not set"))
}
}
func (v *Validator) assertTarget(ctx context.Context, conf *config.VirtualContainerHostConfigSpec) {
defer trace.End(trace.Begin("", ctx))
if conf.Target == "" {
v.NoteIssue(errors.New("target is not set"))
}
if conf.Username == "" {
v.NoteIssue(errors.New("target username is not set"))
}
if conf.Token == "" {
v.NoteIssue(errors.New("target token is not set"))
}
}
func (v *Validator) AssertVersion(ctx context.Context, conf *config.VirtualContainerHostConfigSpec) (err error) {
defer trace.End(trace.Begin("", ctx))
defer func() {
err = v.ListIssues(ctx)
}()
if conf.Version == nil {
v.NoteIssue(errors.Errorf("Unknown version of VCH %q", conf.Name))
return err
}
var older bool
installerBuild := version.GetBuild()
if older, err = conf.Version.IsOlder(installerBuild); err != nil {
v.NoteIssue(errors.Errorf("Failed to compare VCH version %q with installer version %q: %s", conf.Version.ShortVersion(), installerBuild.ShortVersion(), err))
return err
}
if !older {
v.NoteIssue(errors.Errorf("%q has same or newer version %s than installer version %s. No upgrade is available.", conf.Name, conf.Version.ShortVersion(), installerBuild.ShortVersion()))
return err
}
return nil
}