-
Notifications
You must be signed in to change notification settings - Fork 23
/
stemcell.go
70 lines (55 loc) · 1.56 KB
/
stemcell.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
package stemcell
import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
apiv1 "github.com/cppforlife/bosh-cpi-go/apiv1"
"github.com/cppforlife/bosh-virtualbox-cpi/driver"
)
type StemcellImpl struct {
cid apiv1.StemcellCID
path string
driver driver.Driver
runner driver.Runner
logger boshlog.Logger
}
func NewStemcellImpl(
cid apiv1.StemcellCID,
path string,
driver driver.Driver,
runner driver.Runner,
logger boshlog.Logger,
) StemcellImpl {
return StemcellImpl{cid, path, driver, runner, logger}
}
func (s StemcellImpl) ID() apiv1.StemcellCID { return s.cid }
func (s StemcellImpl) SnapshotName() string { return "prepared-clone" }
func (s StemcellImpl) Prepare() error {
_, err := s.driver.Execute("snapshot", s.cid.AsString(), "take", s.SnapshotName())
if err != nil {
return bosherr.WrapErrorf(err, "Preparing for future cloning")
}
return nil
}
func (s StemcellImpl) Exists() (bool, error) {
output, err := s.driver.Execute("showvminfo", s.cid.AsString(), "--machinereadable")
if err != nil {
if s.driver.IsMissingVMErr(output) {
return false, nil
}
return false, err
}
return true, nil
}
func (s StemcellImpl) Delete() error {
output, err := s.driver.Execute("unregistervm", s.cid.AsString(), "--delete")
if err != nil {
if !s.driver.IsMissingVMErr(output) {
return bosherr.WrapErrorf(err, "Unregistering stemcell VM")
}
}
_, _, err = s.runner.Execute("rm", "-rf", s.path)
if err != nil {
return bosherr.WrapErrorf(err, "Deleting stemcell '%s'", s.path)
}
return nil
}