-
Notifications
You must be signed in to change notification settings - Fork 162
/
parser.go
133 lines (111 loc) · 4.07 KB
/
parser.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
package manifest
import (
"encoding/pem"
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
biproperty "github.com/cloudfoundry/bosh-utils/property"
boshsys "github.com/cloudfoundry/bosh-utils/system"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
"github.com/cppforlife/go-patch/patch"
"gopkg.in/yaml.v2"
biutil "github.com/cloudfoundry/bosh-cli/common/util"
boshtpl "github.com/cloudfoundry/bosh-cli/director/template"
birelsetmanifest "github.com/cloudfoundry/bosh-cli/release/set/manifest"
)
type Parser interface {
Parse(string, boshtpl.Variables, patch.Op, birelsetmanifest.Manifest) (Manifest, error)
}
type parser struct {
fs boshsys.FileSystem
uuidGenerator boshuuid.Generator
logger boshlog.Logger
logTag string
validator Validator
}
type manifest struct {
Name string
CloudProvider installation `yaml:"cloud_provider"`
}
type installation struct {
Template template
Properties map[interface{}]interface{}
SSHTunnel SSHTunnel `yaml:"ssh_tunnel"`
Mbus string
}
func (i installation) HasSSHTunnel() bool {
return i.SSHTunnel != SSHTunnel{}
}
type template struct {
Name string
Release string
}
func NewParser(fs boshsys.FileSystem, uuidGenerator boshuuid.Generator, logger boshlog.Logger, validator Validator) Parser {
return &parser{
fs: fs,
uuidGenerator: uuidGenerator,
logger: logger,
logTag: "deploymentParser",
validator: validator,
}
}
func (p *parser) Parse(path string, vars boshtpl.Variables, op patch.Op, releaseSetManifest birelsetmanifest.Manifest) (Manifest, error) {
contents, err := p.fs.ReadFile(path)
if err != nil {
return Manifest{}, bosherr.WrapErrorf(err, "Reading file %s", path)
}
tpl := boshtpl.NewTemplate(contents)
bytes, err := tpl.Evaluate(vars, op, boshtpl.EvaluateOpts{ExpectAllKeys: true})
if err != nil {
return Manifest{}, bosherr.WrapErrorf(err, "Evaluating manifest")
}
comboManifest := manifest{}
err = yaml.Unmarshal(bytes, &comboManifest)
if err != nil {
return Manifest{}, bosherr.WrapError(err, "Unmarshalling installation manifest")
}
p.logger.Debug(p.logTag, "Parsed installation manifest: %#v", comboManifest)
if comboManifest.CloudProvider.SSHTunnel.PrivateKey != "" {
if strings.HasPrefix(comboManifest.CloudProvider.SSHTunnel.PrivateKey, "-----BEGIN RSA PRIVATE KEY-----") {
pkey, _ := pem.Decode([]byte(comboManifest.CloudProvider.SSHTunnel.PrivateKey))
if pkey == nil {
return Manifest{}, bosherr.Error("Invalid private key for ssh tunnel")
}
} else {
absolutePath, err := biutil.AbsolutifyPath(path, comboManifest.CloudProvider.SSHTunnel.PrivateKey, p.fs)
if err != nil {
return Manifest{}, bosherr.WrapErrorf(err, "Expanding private_key path")
}
keyContents, err := p.fs.ReadFile(absolutePath)
if err != nil {
return Manifest{}, bosherr.WrapErrorf(err, "Reading private key from %s", absolutePath)
}
comboManifest.CloudProvider.SSHTunnel.PrivateKey = string(keyContents)
}
}
installationManifest := Manifest{
Name: comboManifest.Name,
Template: ReleaseJobRef{
Name: comboManifest.CloudProvider.Template.Name,
Release: comboManifest.CloudProvider.Template.Release,
},
Mbus: comboManifest.CloudProvider.Mbus,
}
properties, err := biproperty.BuildMap(comboManifest.CloudProvider.Properties)
if err != nil {
return Manifest{}, bosherr.WrapErrorf(err, "Parsing cloud_provider manifest properties: %#v", comboManifest.CloudProvider.Properties)
}
installationManifest.Properties = properties
if comboManifest.CloudProvider.HasSSHTunnel() {
password, err := p.uuidGenerator.Generate()
if err != nil {
return Manifest{}, bosherr.WrapError(err, "Generating registry password")
}
installationManifest.PopulateRegistry("registry", password, "127.0.0.1", 6901, comboManifest.CloudProvider.SSHTunnel)
}
err = p.validator.Validate(installationManifest, releaseSetManifest)
if err != nil {
return Manifest{}, bosherr.WrapError(err, "Validating installation manifest")
}
return installationManifest, nil
}