forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_logs_with_signed_url.go
106 lines (86 loc) · 2.8 KB
/
fetch_logs_with_signed_url.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
package action
import (
"errors"
blobdelegator "github.com/cloudfoundry/bosh-agent/agent/httpblobprovider/blobstore_delegator"
boshdirs "github.com/cloudfoundry/bosh-agent/settings/directories"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshcmd "github.com/cloudfoundry/bosh-utils/fileutil"
)
type FetchLogsWithSignedURLRequest struct {
SignedURL string `json:"signed_url"`
LogType string `json:"log_type"`
Filters []string `json:"filters"`
Headers map[string]string `json:"headers"`
}
type FetchLogsWithSignedURLResponse struct {
SHA1Digest string `json:"sha1"`
}
type FetchLogsWithSignedURLAction struct {
compressor boshcmd.Compressor
copier boshcmd.Copier
settingsDir boshdirs.Provider
blobDelegator blobdelegator.BlobstoreDelegator
}
func NewFetchLogsWithSignedURLAction(
compressor boshcmd.Compressor,
copier boshcmd.Copier,
settingsDir boshdirs.Provider,
blobDelegator blobdelegator.BlobstoreDelegator) (action FetchLogsWithSignedURLAction) {
action.compressor = compressor
action.copier = copier
action.settingsDir = settingsDir
action.blobDelegator = blobDelegator
return
}
func (a FetchLogsWithSignedURLAction) IsAsynchronous(_ ProtocolVersion) bool {
return true
}
func (a FetchLogsWithSignedURLAction) IsPersistent() bool {
return false
}
func (a FetchLogsWithSignedURLAction) IsLoggable() bool {
return true
}
func (a FetchLogsWithSignedURLAction) Run(request FetchLogsWithSignedURLRequest) (FetchLogsWithSignedURLResponse, error) {
var logsDir string
filters := request.Filters
switch request.LogType {
case "job":
if len(request.Filters) == 0 {
filters = []string{"**/*"}
}
logsDir = a.settingsDir.LogsDir()
case "agent":
if len(request.Filters) == 0 {
filters = []string{"**/*"}
}
logsDir = a.settingsDir.AgentLogsDir()
default:
return FetchLogsWithSignedURLResponse{}, bosherr.Error("Invalid log type")
}
tmpDir, err := a.copier.FilteredCopyToTemp(logsDir, filters)
if err != nil {
return FetchLogsWithSignedURLResponse{}, bosherr.WrapError(err, "Copying filtered files to temp directory")
}
defer a.copier.CleanUp(tmpDir)
tarball, err := a.compressor.CompressFilesInDir(tmpDir)
if err != nil {
return FetchLogsWithSignedURLResponse{}, bosherr.WrapError(err, "Making logs tarball")
}
defer func() {
_ = a.compressor.CleanUp(tarball)
}()
_, digest, err := a.blobDelegator.Write(request.SignedURL, tarball, request.Headers)
if err != nil {
return FetchLogsWithSignedURLResponse{}, bosherr.WrapError(err, "Create file on blobstore")
}
return FetchLogsWithSignedURLResponse{
SHA1Digest: digest.String(),
}, nil
}
func (a FetchLogsWithSignedURLAction) Resume() (interface{}, error) {
return nil, errors.New("not supported")
}
func (a FetchLogsWithSignedURLAction) Cancel() error {
return errors.New("not supported")
}