forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
108 lines (89 loc) · 3 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
package manifest
import (
"os"
"os/user"
"path"
"strings"
"github.com/cloudfoundry-incubator/candiedyaml"
bosherr "github.com/cloudfoundry/bosh-agent/errors"
boshlog "github.com/cloudfoundry/bosh-agent/logger"
boshsys "github.com/cloudfoundry/bosh-agent/system"
biproperty "github.com/cloudfoundry/bosh-init/common/property"
)
type Parser interface {
Parse(path string) (Manifest, error)
}
type parser struct {
fs boshsys.FileSystem
logger boshlog.Logger
logTag string
}
type manifest struct {
Name string
CloudProvider installation `yaml:"cloud_provider"`
}
type installation struct {
Template template
Properties map[interface{}]interface{}
Registry Registry
AgentEnvService string `yaml:"agent_env_service"`
SSHTunnel SSHTunnel `yaml:"ssh_tunnel"`
Mbus string
}
type template struct {
Name string
Release string
}
func NewParser(fs boshsys.FileSystem, logger boshlog.Logger) Parser {
return &parser{
fs: fs,
logger: logger,
logTag: "deploymentParser",
}
}
func (p *parser) Parse(path string) (Manifest, error) {
contents, err := p.fs.ReadFile(path)
if err != nil {
return Manifest{}, bosherr.WrapErrorf(err, "Reading file %s", path)
}
comboManifest := manifest{}
err = candiedyaml.Unmarshal(contents, &comboManifest)
if err != nil {
return Manifest{}, bosherr.WrapError(err, "Unmarshalling installation manifest")
}
p.logger.Debug(p.logTag, "Parsed installation manifest: %#v", comboManifest)
expandedPrivateKeyPath := p.expandTilde(comboManifest.CloudProvider.SSHTunnel.PrivateKey)
comboManifest.CloudProvider.SSHTunnel.PrivateKey = expandedPrivateKeyPath
installationManifest := Manifest{
Name: comboManifest.Name,
Template: ReleaseJobRef{
Name: comboManifest.CloudProvider.Template.Name,
Release: comboManifest.CloudProvider.Template.Release,
},
Registry: comboManifest.CloudProvider.Registry,
AgentEnvService: comboManifest.CloudProvider.AgentEnvService,
SSHTunnel: comboManifest.CloudProvider.SSHTunnel,
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
return installationManifest, nil
}
// special case handling for linux/darwin where the tilde character resolves to home
func (p *parser) expandTilde(rawPath string) string {
currentUser, err := user.Current()
if err != nil {
p.logger.Warn(p.logTag, "Unable to get current user, cannot expand '~' in paths")
return rawPath
}
if strings.IndexRune(rawPath, '~') != 0 {
return rawPath
}
sep := "~" + string(os.PathSeparator)
currentUserHome := currentUser.HomeDir + string(os.PathSeparator) // we'll clean up any extra path separators later
expandedPath := path.Clean(strings.Replace(rawPath, sep, currentUserHome, 1))
return path.Clean(expandedPath)
}