Repository navigation
Expand file tree
/
Copy pathinstance.go
More file actions
313 lines (282 loc) · 7.8 KB
/
Copy pathinstance.go
File metadata and controls
313 lines (282 loc) · 7.8 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package lxops
import (
"fmt"
"os"
"path/filepath"
"strings"
"melato.org/lxops/cfg"
"melato.org/lxops/internal/util"
"melato.org/lxops/srv"
"melato.org/script"
"melato.org/table3"
)
type Instance struct {
globalProperties map[string]string
Config *cfg.Config
cliProperties map[string]string
Name string
profile string
containerSource *ContainerSource
deviceSource *DeviceSource
Properties cfg.PatternSubstitution
fspaths map[string]*InstanceFS
sourceConfig *cfg.Config
}
func (t *Instance) substitute(e *error, pattern cfg.Pattern, defaultPattern cfg.Pattern) string {
if pattern == "" {
pattern = defaultPattern
}
value, err := pattern.Substitute(t.Properties)
if err != nil {
*e = err
}
return value
}
func (instance *Instance) createBuiltins() map[string]string {
properties := make(map[string]string)
name := instance.Name
properties["instance"] = name
project := instance.Config.Project
var projectSlash, project_instance string
if project == "" || project == "default" {
project = "default"
projectSlash = ""
project_instance = name
} else {
projectSlash = project + "/"
project_instance = project + "_" + name
}
properties["project"] = project
properties["project/"] = projectSlash
properties["project_instance"] = project_instance
return properties
}
func mergeProperties(a, b map[string]string) {
for k, v := range b {
a[k] = v
}
}
func (instance *Instance) EffectiveProperties() map[string]string {
properties := make(map[string]string)
mergeProperties(properties, instance.globalProperties)
mergeProperties(properties, instance.Config.Properties)
mergeProperties(properties, instance.cliProperties)
return properties
}
func (instance *Instance) newProperties() cfg.PatternSubstitution {
cascade := &util.CascadingProperties{}
cascade.AddMap(instance.createBuiltins())
cascade.AddMap(instance.cliProperties)
cascade.AddMap(instance.Config.Properties)
cascade.AddMap(instance.globalProperties)
return cascade
}
func newInstance(cliProperties, globalProperties map[string]string, config *cfg.Config, name string, includeSource bool) (*Instance, error) {
t := &Instance{
globalProperties: globalProperties,
cliProperties: cliProperties,
Config: config,
Name: name}
t.Properties = t.newProperties()
var err error
t.profile = t.substitute(&err, config.Profile, "(instance).lxops")
if err != nil {
return nil, err
}
if includeSource {
t.containerSource, err = t.newContainerSource()
if err != nil {
return nil, err
}
t.deviceSource, err = t.newDeviceSource()
if err != nil {
return nil, err
}
} else {
t.containerSource = &ContainerSource{}
t.deviceSource = &DeviceSource{}
}
return t, nil
}
func NewInstance(cliProperties, globalProperties map[string]string, config *cfg.Config, name string) (*Instance, error) {
return newInstance(cliProperties, globalProperties, config, name, true)
}
func (t *Instance) NewInstance(name string) (*Instance, error) {
return NewInstance(nil, t.globalProperties, t.Config, name)
}
func (t *Instance) ContainerSource() *ContainerSource {
return t.containerSource
}
func (t *Instance) DeviceSource() *DeviceSource {
return t.deviceSource
}
func (t *Instance) ProfileName() string {
return t.profile
}
// Container is the same as Name
func (t *Instance) Container() string {
return t.Name
}
func (t *Instance) Filesystems() (map[string]*InstanceFS, error) {
if t.fspaths == nil {
fspaths := make(map[string]*InstanceFS)
for id, fs := range t.Config.Filesystems {
path, err := fs.Pattern.Substitute(t.Properties)
if err != nil {
return nil, err
}
fspaths[id] = &InstanceFS{Id: id, Path: path, Filesystem: fs}
}
t.fspaths = fspaths
}
return t.fspaths, nil
}
func (t *Instance) FilesystemList() ([]*InstanceFS, error) {
paths, err := t.Filesystems()
if err != nil {
return nil, err
}
var list []*InstanceFS
for _, path := range paths {
list = append(list, path)
}
InstanceFSList(list).Sort()
return list, nil
}
func (t *Instance) DeviceList() ([]InstanceDevice, error) {
var devices []InstanceDevice
for name, device := range t.Config.Devices {
d := InstanceDevice{Name: name, Device: device}
dir, err := t.DeviceDir(name, device)
if err != nil {
return nil, err
}
d.Source = dir
devices = append(devices, d)
}
InstanceDeviceList(devices).Sort()
return devices, nil
}
func (t *Instance) DeviceDir(deviceId string, device *cfg.Device) (string, error) {
dir, err := device.Dir.Substitute(t.Properties)
if err != nil {
return "", err
}
if strings.HasPrefix(dir, "/") {
return dir, nil
}
if device.Filesystem == "" {
return "", fmt.Errorf("device %s has no filesystem but relative dir: %s", deviceId, dir)
}
if dir == "" {
dir = deviceId
} else if device.Dir == "." {
dir = ""
}
fspaths, err := t.Filesystems()
if err != nil {
return "", err
}
fsPath, exists := fspaths[device.Filesystem]
if !exists {
return "", nil
}
if dir != "" {
return filepath.Join(fsPath.Dir(), dir), nil
} else {
return fsPath.Dir(), nil
}
}
// Snapshot creates a snapshot of all ZFS filesystems of the instance
func (instance *Instance) Snapshot(name string) error {
filesystems, err := instance.FilesystemList()
if err != nil {
return err
}
s := &script.Script{Trace: true}
for _, fs := range filesystems {
if fs.IsZfs() && !fs.Filesystem.Transient {
s.Run("sudo", "zfs", "snapshot", fs.Path+"@"+name)
}
}
return s.Error()
}
// Rollback calls zfs rollback -r on the non-transient ZFS filesystems of the instance
func (instance *Instance) Rollback(name string) error {
filesystems, err := instance.FilesystemList()
if err != nil {
return err
}
s := &script.Script{Trace: true}
for _, fs := range filesystems {
if fs.IsZfs() && !fs.Filesystem.Transient && fs.Filesystem.Destroy {
s.Run("sudo", "zfs", "rollback", "-r", fs.Path+"@"+name)
}
}
return s.Error()
}
func (t *Instance) getProperty(name string) (string, bool) {
value, exists := t.cliProperties[name]
if !exists {
value, exists = t.globalProperties[name]
}
return value, exists
}
// GetSourceConfig returns the parsed configuration specified by Config.SourceConfig
// If there is no Config.SourceConfig, it returns this instance's config
// It returns a non nil *Config or an error.
func (t *Instance) GetSourceConfig() (*cfg.Config, error) {
if t.Config.SourceConfig == "" {
return t.Config, nil
}
if t.sourceConfig == nil {
config, err := cfg.ReadConfig(string(t.Config.SourceConfig), t.getProperty)
if err != nil {
return nil, err
}
if config.Project == "" {
// fill-in missing project with our project
config.Project = t.Config.Project
}
t.sourceConfig = config
}
return t.sourceConfig, nil
}
func (t *Instance) GetOwner() (string, error) {
return t.Config.DeviceOwner.Substitute(t.Properties)
}
func (t *Instance) NewDeviceMap() (map[string]*srv.Device, error) {
devices := make(map[string]*srv.Device)
for deviceName, device := range t.Config.Devices {
dir, err := t.DeviceDir(deviceName, device)
if err != nil {
return nil, err
}
devices[deviceName] = &srv.Device{
Path: device.Path,
Source: dir,
Readonly: device.Readonly,
}
}
return devices, nil
}
func (instance *Instance) PrintDevices() error {
writer := &table.FixedWriter{Writer: os.Stdout}
devices, err := instance.DeviceList()
if err != nil {
return err
}
var d InstanceDevice
writer.Columns(
table.NewColumn("SOURCE", func() interface{} { return d.Source }),
table.NewColumn("PATH", func() interface{} { return d.Device.Path }),
table.NewColumn("NAME", func() interface{} { return d.Name }),
table.NewColumn("DIR", func() interface{} { return d.Device.Dir }),
table.NewColumn("FILESYSTEM", func() interface{} { return d.Device.Filesystem }),
)
for _, d = range devices {
writer.WriteRow()
}
writer.End()
return nil
}