forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.go
137 lines (111 loc) · 2.88 KB
/
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package cmd
import (
"fmt"
"strconv"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
boshdir "github.com/cloudfoundry/bosh-cli/director"
boshssh "github.com/cloudfoundry/bosh-cli/ssh"
)
type LogsCmd struct {
deployment boshdir.Deployment
downloader Downloader
uuidGen boshuuid.Generator
nonIntSSHRunner boshssh.Runner
}
func NewLogsCmd(
deployment boshdir.Deployment,
downloader Downloader,
uuidGen boshuuid.Generator,
nonIntSSHRunner boshssh.Runner,
) LogsCmd {
return LogsCmd{
deployment: deployment,
downloader: downloader,
uuidGen: uuidGen,
nonIntSSHRunner: nonIntSSHRunner,
}
}
func (c LogsCmd) Run(opts LogsOpts) error {
if opts.Follow || opts.Num > 0 {
return c.tail(opts)
}
return c.fetch(opts)
}
func (c LogsCmd) tail(opts LogsOpts) error {
sshOpts, privKey, err := boshdir.NewSSHOpts(c.uuidGen)
if err != nil {
return bosherr.WrapErrorf(err, "Generating SSH options")
}
connOpts := boshssh.ConnectionOpts{
PrivateKey: privKey,
GatewayDisable: opts.GatewayFlags.Disable,
GatewayUsername: opts.GatewayFlags.Username,
GatewayHost: opts.GatewayFlags.Host,
GatewayPrivateKeyPath: opts.GatewayFlags.PrivateKeyPath,
}
result, err := c.deployment.SetUpSSH(opts.Args.Slug, sshOpts)
if err != nil {
return err
}
defer func() {
_ = c.deployment.CleanUpSSH(opts.Args.Slug, sshOpts)
}()
err = c.nonIntSSHRunner.Run(connOpts, result, c.buildTailCmd(opts))
if err != nil {
return bosherr.WrapErrorf(err, "Running follow over non-interactive SSH")
}
return nil
}
func (c LogsCmd) buildTailCmd(opts LogsOpts) []string {
cmd := []string{"sudo", "tail"}
if opts.Follow {
// -F for continuing to follow after renames
cmd = append(cmd, "-F")
}
if opts.Num > 0 {
cmd = append(cmd, "-n", strconv.Itoa(opts.Num))
}
if opts.Quiet {
cmd = append(cmd, "-q")
}
var logsDir string
if opts.Agent {
logsDir = "/var/vcap/bosh/log"
} else {
logsDir = "/var/vcap/sys/log"
}
if len(opts.Jobs) > 0 {
for _, job := range opts.Jobs {
cmd = append(cmd, fmt.Sprintf("%s/%s/*.log", logsDir, job))
}
} else if len(opts.Filters) > 0 {
for _, filter := range opts.Filters {
cmd = append(cmd, fmt.Sprintf("%s/%s", logsDir, filter))
}
} else {
// includes only directory and its subdirectories
cmd = append(cmd, fmt.Sprintf("%s/{**/,}*.log", logsDir))
}
return cmd
}
func (c LogsCmd) fetch(opts LogsOpts) error {
slug, ok := opts.Args.Slug.InstanceSlug()
if !ok {
return bosherr.Errorf("Expected single instance for fetching logs")
}
result, err := c.deployment.FetchLogs(slug, opts.Filters, opts.Agent)
if err != nil {
return err
}
err = c.downloader.Download(
result.BlobstoreID,
result.SHA1,
opts.Args.Slug.Name(),
opts.Directory.Path,
)
if err != nil {
return bosherr.WrapError(err, "Downloading logs")
}
return nil
}