-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource.go
213 lines (181 loc) · 4.33 KB
/
resource.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package smutje
import (
"fmt"
"log"
"net"
"github.com/gfrey/gconn"
"github.com/gfrey/smutje/hypervisor"
"github.com/gfrey/smutje/parser"
"github.com/pkg/errors"
)
type Resource struct {
ID string
Name string
Blueprint string
Attributes Attributes
Packages []*smPackage
client gconn.Client
hypervisor hypervisor.Client
uuid string
address string
username string
isVirtual bool
}
func NewResource(path string, n *parser.AstNode) (*Resource, error) {
res := new(Resource)
res.ID = n.ID
res.Name = n.Name
res.Attributes = Attributes{}
res.Attributes["Hostname"] = n.ID
for _, child := range n.Children {
switch child.Type {
case parser.AstBlueprint:
blueprint, err := newBlueprint(child)
if err != nil {
return nil, err
}
res.Blueprint = blueprint
default:
pkgs, err := handleChild("", path, res.Attributes, child)
if err != nil {
return nil, err
}
res.Packages = append(res.Packages, pkgs...)
}
}
return res, nil
}
func (res *Resource) Prepare(l *log.Logger) error {
l = tagLogger(l, res.ID)
if err := res.initializeClient(); err != nil {
return err
}
for _, pkg := range res.Packages {
if err := pkg.Prepare(res.client, res.Attributes); err != nil {
return err
}
}
return nil
}
func (res *Resource) Generate(l *log.Logger) (err error) {
if res.isVirtual && res.client == nil {
if res.uuid == "" {
res.Blueprint, err = renderString(res.ID+"/blueprint", res.Blueprint, res.Attributes)
if err != nil {
return err
}
res.uuid, err = res.hypervisor.Create(l, res.Blueprint)
if err != nil {
return err
}
}
res.client, err = res.hypervisor.ConnectVRes(res.uuid)
if err != nil {
return err
}
}
sess, err := res.client.NewSession("/usr/bin/env", "bash", "-c", `"mkdir -p /tmp/smutje && mkdir -p /var/lib/smutje"`)
if err != nil {
return err
}
defer sess.Close()
return sess.Run()
}
func (res *Resource) Provision(l *log.Logger) (err error) {
l = tagLogger(l, res.ID)
for _, pkg := range res.Packages {
if err := pkg.Provision(l, res.client); err != nil {
return err
}
}
return nil
}
func (res *Resource) initializeClient() (err error) {
var hypervisorType string
hypervisorType, res.isVirtual = res.Attributes["Hypervisor"]
if !res.isVirtual && res.Blueprint != "" {
return errors.Errorf("hypervisor must be set, for blueprint to be supported!")
}
if err := res.setAddress(); err != nil {
return err
}
var ok bool
res.username, ok = res.Attributes["Username"]
if !ok {
res.username = "root"
}
switch {
case res.isVirtual:
switch hypervisorType {
case "smartos":
res.hypervisor, err = hypervisor.SmartOS(res.address)
if err != nil {
return err
}
default:
return errors.Errorf("hypervisor %s not supported", hypervisorType)
}
res.uuid, err = res.hypervisor.UUID(res.ID)
if err == nil && res.uuid != "" {
res.client, err = res.hypervisor.ConnectVRes(res.uuid)
}
return err
default:
res.client, err = gconn.NewSSHClient(res.address, res.username)
return err
}
}
func (res *Resource) setAddress() error {
var ok bool
if res.isVirtual {
res.address, ok = res.Attributes["Host"]
if !ok {
return errors.Errorf("no address specified for smartos hypervisor")
}
} else {
res.address, ok = res.Attributes["Address"]
if !ok {
for _, cand := range []string{res.Name, res.ID} {
ips, err := net.LookupIP(cand)
if err == nil && len(ips) > 0 {
res.address = ips[0].String()
break
}
}
}
}
if res.address == "" {
return fmt.Errorf("Host address attribute not specified!")
}
return nil
}
func handleChild(parentID, path string, attrs Attributes, node *parser.AstNode) ([]*smPackage, error) {
pkgs := []*smPackage{}
switch node.Type {
case parser.AstAttributes:
newAttrs, err := newAttributes(node)
if err != nil {
return nil, err
}
if err := attrs.MergeInplace(newAttrs); err != nil {
return nil, err
}
case parser.AstPackage:
pkg, err := newPackage(parentID, path, attrs, node)
if err != nil {
return nil, err
}
pkgs = append(pkgs, pkg)
case parser.AstInclude:
newPkgs, err := newInclude(parentID, path, attrs, node)
if err != nil {
return nil, err
}
pkgs = append(pkgs, newPkgs...)
case parser.AstText:
// ignore
default:
return nil, errors.Errorf("unexpected node seen: %s", node.Type)
}
return pkgs, nil
}