forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_bundle.go
131 lines (104 loc) · 3.43 KB
/
file_bundle.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
package bundlecollection
import (
"os"
"path/filepath"
bosherr "github.com/cloudfoundry/bosh-agent/internal/github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-agent/internal/github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-agent/internal/github.com/cloudfoundry/bosh-utils/system"
)
const (
fileBundleLogTag = "FileBundle"
installDirsPerms = os.FileMode(0755)
enableDirPerms = os.FileMode(0755)
)
type FileBundle struct {
installPath string
enablePath string
fs boshsys.FileSystem
logger boshlog.Logger
}
func NewFileBundle(
installPath, enablePath string,
fs boshsys.FileSystem,
logger boshlog.Logger,
) FileBundle {
return FileBundle{
installPath: installPath,
enablePath: enablePath,
fs: fs,
logger: logger,
}
}
func (b FileBundle) Install(sourcePath string) (boshsys.FileSystem, string, error) {
b.logger.Debug(fileBundleLogTag, "Installing %v", b)
err := b.fs.Chmod(sourcePath, installDirsPerms)
if err != nil {
return nil, "", bosherr.WrapError(err, "Setting permissions on source directory")
}
err = b.fs.MkdirAll(filepath.Dir(b.installPath), installDirsPerms)
if err != nil {
return nil, "", bosherr.WrapError(err, "Creating parent installation directory")
}
// Rename MUST be the last possibly-failing operation
// because IsInstalled() relies on installPath presence.
err = b.fs.Rename(sourcePath, b.installPath)
if err != nil {
return nil, "", bosherr.WrapError(err, "Moving to installation directory")
}
return b.fs, b.installPath, nil
}
func (b FileBundle) InstallWithoutContents() (boshsys.FileSystem, string, error) {
b.logger.Debug(fileBundleLogTag, "Installing without contents %v", b)
// MkdirAll MUST be the last possibly-failing operation
// because IsInstalled() relies on installPath presence.
err := b.fs.MkdirAll(b.installPath, installDirsPerms)
if err != nil {
return nil, "", bosherr.WrapError(err, "Creating installation directory")
}
return b.fs, b.installPath, nil
}
func (b FileBundle) GetInstallPath() (boshsys.FileSystem, string, error) {
path := b.installPath
if !b.fs.FileExists(path) {
return nil, "", bosherr.Error("install dir does not exist")
}
return b.fs, path, nil
}
func (b FileBundle) IsInstalled() (bool, error) {
return b.fs.FileExists(b.installPath), nil
}
func (b FileBundle) Enable() (boshsys.FileSystem, string, error) {
b.logger.Debug(fileBundleLogTag, "Enabling %v", b)
if !b.fs.FileExists(b.installPath) {
return nil, "", bosherr.Error("bundle must be installed")
}
err := b.fs.MkdirAll(filepath.Dir(b.enablePath), enableDirPerms)
if err != nil {
return nil, "", bosherr.WrapError(err, "failed to create enable dir")
}
err = b.fs.Symlink(b.installPath, b.enablePath)
if err != nil {
return nil, "", bosherr.WrapError(err, "failed to enable")
}
return b.fs, b.enablePath, nil
}
func (b FileBundle) Disable() error {
b.logger.Debug(fileBundleLogTag, "Disabling %v", b)
target, err := b.fs.ReadLink(b.enablePath)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return bosherr.WrapError(err, "Reading symlink")
}
if target == b.installPath {
return b.fs.RemoveAll(b.enablePath)
}
return nil
}
func (b FileBundle) Uninstall() error {
b.logger.Debug(fileBundleLogTag, "Uninstalling %v", b)
// RemoveAll MUST be the last possibly-failing operation
// because IsInstalled() relies on installPath presence.
return b.fs.RemoveAll(b.installPath)
}