forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdutil.go
84 lines (69 loc) · 2.26 KB
/
cdutil.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
package cdrom
import (
"os"
"path/filepath"
"errors"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type CDUtil interface {
GetFilesContents(fileNames []string) (contents [][]byte, err error)
GetBlockDeviceSize() (size uint64, err error)
}
type cdUtil struct {
settingsMountPath string
fs boshsys.FileSystem
cdrom Cdrom
logger boshlog.Logger
logTag string
}
func NewCdUtil(settingsMountPath string, fs boshsys.FileSystem, cdrom Cdrom, logger boshlog.Logger) CDUtil {
return cdUtil{
settingsMountPath: settingsMountPath,
fs: fs,
cdrom: cdrom,
logger: logger,
logTag: "cdUtil",
}
}
func (util cdUtil) GetFilesContents(fileNames []string) ([][]byte, error) {
err := util.cdrom.WaitForMedia()
if err != nil {
return [][]byte{}, bosherr.WrapError(err, "Waiting for CDROM to be ready")
}
util.logger.Debug(util.logTag, "Mkdiring %s", util.settingsMountPath)
err = util.fs.MkdirAll(util.settingsMountPath, os.FileMode(0700))
if err != nil {
return [][]byte{}, bosherr.WrapError(err, "Creating CDROM mount point")
}
util.logger.Debug(util.logTag, "Mounting %s", util.settingsMountPath)
err = util.cdrom.Mount(util.settingsMountPath)
if err != nil {
return [][]byte{}, bosherr.WrapError(err, "Mounting CDROM")
}
contents := [][]byte{}
for _, fileName := range fileNames {
settingsPath := filepath.Join(util.settingsMountPath, fileName)
util.logger.Debug(util.logTag, "Reading %s", settingsPath)
stringContents, err := util.fs.ReadFile(settingsPath)
if err != nil {
return [][]byte{}, bosherr.WrapError(err, "Reading from CDROM")
}
contents = append(contents, []byte(stringContents))
}
util.logger.Debug(util.logTag, "Umounting CDROM")
err = util.cdrom.Unmount()
if err != nil {
return [][]byte{}, bosherr.WrapError(err, "Unmounting CDROM")
}
util.logger.Debug(util.logTag, "Ejecting CDROM")
err = util.cdrom.Eject()
if err != nil {
return [][]byte{}, bosherr.WrapError(err, "Ejecting CDROM")
}
return contents, nil
}
func (util cdUtil) GetBlockDeviceSize() (size uint64, err error) {
return 0, errors.New("not supported")
}