forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blobstore.go
105 lines (87 loc) · 2.87 KB
/
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
package blobstore
import (
"io"
"os"
boshdavcli "github.com/cloudfoundry/bosh-davcli/client"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
)
type Blobstore interface {
Get(blobID string) (LocalBlob, error)
Add(sourcePath string) (blobID string, err error)
}
type Config struct {
Endpoint string
Username string
Password string
}
type blobstore struct {
davClient boshdavcli.Client
uuidGenerator boshuuid.Generator
fs boshsys.FileSystem
logger boshlog.Logger
logTag string
}
func NewBlobstore(davClient boshdavcli.Client, uuidGenerator boshuuid.Generator, fs boshsys.FileSystem, logger boshlog.Logger) Blobstore {
return &blobstore{
davClient: davClient,
uuidGenerator: uuidGenerator,
fs: fs,
logger: logger,
logTag: "blobstore",
}
}
func (b *blobstore) Get(blobID string) (LocalBlob, error) {
file, err := b.fs.TempFile("bosh-init-local-blob")
destinationPath := file.Name()
err = file.Close()
if err != nil {
return nil, bosherr.WrapErrorf(err, "Closing new temp file '%s'", destinationPath)
}
b.logger.Debug(b.logTag, "Downloading blob %s to %s", blobID, destinationPath)
readCloser, err := b.davClient.Get(blobID)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Getting blob %s from blobstore", blobID)
}
defer func() {
if err = readCloser.Close(); err != nil {
b.logger.Warn(b.logTag, "Couldn't close davClient.Get reader: %s", err.Error())
}
}()
targetFile, err := b.fs.OpenFile(destinationPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Opening file for blob at %s", destinationPath)
}
_, err = io.Copy(targetFile, readCloser)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Saving blob to %s", destinationPath)
}
return NewLocalBlob(destinationPath, b.fs, b.logger), nil
}
func (b *blobstore) Add(sourcePath string) (string, error) {
blobID, err := b.uuidGenerator.Generate()
if err != nil {
return "", bosherr.WrapError(err, "Generating Blob ID")
}
b.logger.Debug(b.logTag, "Uploading blob %s from %s", blobID, sourcePath)
file, err := b.fs.OpenFile(sourcePath, os.O_RDONLY, 0)
if err != nil {
return "", bosherr.WrapErrorf(err, "Opening file for reading %s", sourcePath)
}
defer func() {
if err := file.Close(); err != nil {
b.logger.Warn(b.logTag, "Couldn't close source file: %s", err.Error())
}
}()
fileInfo, err := file.Stat()
if err != nil {
return "", bosherr.WrapErrorf(err, "Getting fileInfo from %s", sourcePath)
}
err = b.davClient.Put(blobID, file, fileInfo.Size())
if err != nil {
return "", bosherr.WrapErrorf(err, "Putting file '%s' into blobstore (via DAVClient) as blobID '%s'", sourcePath, blobID)
}
return blobID, nil
}