forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_logs.go
97 lines (80 loc) · 2.21 KB
/
fetch_logs.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
package action
import (
"errors"
"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 FetchLogsAction struct {
compressor boshcmd.Compressor
copier boshcmd.Copier
blobstore blobstore_delegator.BlobstoreDelegator
settingsDir boshdirs.Provider
}
func NewFetchLogs(
compressor boshcmd.Compressor,
copier boshcmd.Copier,
blobstore blobstore_delegator.BlobstoreDelegator,
settingsDir boshdirs.Provider,
) (action FetchLogsAction) {
action.compressor = compressor
action.copier = copier
action.blobstore = blobstore
action.settingsDir = settingsDir
return
}
func (a FetchLogsAction) IsAsynchronous(_ ProtocolVersion) bool {
return true
}
func (a FetchLogsAction) IsPersistent() bool {
return false
}
func (a FetchLogsAction) IsLoggable() bool {
return true
}
func (a FetchLogsAction) Run(logType string, filters []string) (value map[string]string, err error) {
var logsDir string
switch logType {
case "job":
if len(filters) == 0 {
filters = []string{"**/*"}
}
logsDir = a.settingsDir.LogsDir()
case "agent":
if len(filters) == 0 {
filters = []string{"**/*"}
}
logsDir = a.settingsDir.AgentLogsDir()
default:
err = bosherr.Error("Invalid log type")
return
}
tmpDir, err := a.copier.FilteredCopyToTemp(logsDir, filters)
if err != nil {
err = bosherr.WrapError(err, "Copying filtered files to temp directory")
return
}
defer a.copier.CleanUp(tmpDir)
tarball, err := a.compressor.CompressFilesInDir(tmpDir)
if err != nil {
err = bosherr.WrapError(err, "Making logs tarball")
return
}
defer func() {
_ = a.compressor.CleanUp(tarball)
}()
blobID, multidigestSha, err := a.blobstore.Write("", tarball, nil)
if err != nil {
err = bosherr.WrapError(err, "Create file on blobstore")
return
}
value = map[string]string{"blobstore_id": blobID, "sha1": multidigestSha.String()}
return
}
func (a FetchLogsAction) Resume() (interface{}, error) {
return nil, errors.New("not supported")
}
func (a FetchLogsAction) Cancel() error {
return errors.New("not supported")
}