forked from cloudfoundry/bosh-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
external_blobstore.go
119 lines (97 loc) · 2.75 KB
/
external_blobstore.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
package blobstore
import (
"encoding/json"
"fmt"
"path/filepath"
"errors"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
)
type externalBlobstore struct {
fs boshsys.FileSystem
runner boshsys.CmdRunner
uuidGen boshuuid.Generator
configFilePath string
provider string
options map[string]interface{}
}
func NewExternalBlobstore(
provider string,
options map[string]interface{},
fs boshsys.FileSystem,
runner boshsys.CmdRunner,
uuidGen boshuuid.Generator,
configFilePath string,
) Blobstore {
return externalBlobstore{
provider: provider,
fs: fs,
runner: runner,
uuidGen: uuidGen,
configFilePath: configFilePath,
options: options,
}
}
func (b externalBlobstore) Get(blobID string) (string, error) {
file, err := b.fs.TempFile("bosh-blobstore-externalBlobstore-Get")
if err != nil {
return "", bosherr.WrapError(err, "Creating temporary file")
}
defer file.Close()
fileName := file.Name()
err = b.run("get", blobID, fileName)
if err != nil {
b.fs.RemoveAll(fileName)
return "", err
}
return fileName, nil
}
func (b externalBlobstore) CleanUp(fileName string) error {
return b.fs.RemoveAll(fileName)
}
func (b externalBlobstore) Delete(blobId string) error {
return errors.New("externalBlobstore doesn't implement Delete")
}
func (b externalBlobstore) Create(fileName string) (string, error) {
filePath, err := filepath.Abs(fileName)
if err != nil {
return "", bosherr.WrapError(err, "Getting absolute file path")
}
blobID, err := b.uuidGen.Generate()
if err != nil {
return "", bosherr.WrapError(err, "Generating UUID")
}
err = b.run("put", filePath, blobID)
if err != nil {
return "", bosherr.WrapError(err, "Making put command")
}
return blobID, nil
}
func (b externalBlobstore) Validate() error {
if !b.runner.CommandExists(b.executable()) {
return bosherr.Errorf("executable %s not found in PATH", b.executable())
}
return b.writeConfigFile()
}
func (b externalBlobstore) writeConfigFile() error {
configJSON, err := json.Marshal(b.options)
if err != nil {
return bosherr.WrapError(err, "Marshalling JSON")
}
err = b.fs.WriteFile(b.configFilePath, configJSON)
if err != nil {
return bosherr.WrapError(err, "Writing config file")
}
return nil
}
func (b externalBlobstore) run(method, src, dst string) (err error) {
_, _, _, err = b.runner.RunCommand(b.executable(), "-c", b.configFilePath, method, src, dst)
if err != nil {
return bosherr.WrapErrorf(err, "Shelling out to %s cli", b.executable())
}
return nil
}
func (b externalBlobstore) executable() string {
return fmt.Sprintf("bosh-blobstore-%s", b.provider)
}