forked from pkallberg/cx
/
tail.go
99 lines (80 loc) · 2.26 KB
/
tail.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
package main
import (
"fmt"
"os"
"runtime"
"github.com/cloud66/cloud66"
"github.com/cloud66/cli"
)
var cmdTail = &Command{
Name: "tail",
Build: buildBasicCommand,
Run: runTail,
NeedsStack: true,
Short: "shows and tails the logfile specified on the given server",
Long: `This will run a Linux tail command on the specified server and given logfile.
Logs are read from stack's log folder (current/log) and should be the full logfile name
including the extension.
Server names and roles are case insensitive and will work with the starting characters as well.
This command is only supported on Linux and OS X.
Examples:
$ cx tail -s mystack production.log
$ cx tail -s mystack 52.65.34.98 nginx_error.log
$ cx tail -s mystack web staging.log
`,
}
func runTail(c *cli.Context) {
if runtime.GOOS == "windows" {
printFatal("Not supported on Windows")
os.Exit(2)
}
stack := mustStack(c)
if len(c.Args()) != 2 {
//cmd.printUsage()
os.Exit(2)
}
// get the server
serverName := c.Args()[0]
logName := c.Args()[1]
servers, err := client.Servers(stack.Uid)
if err != nil {
printFatal(err.Error())
}
server, err := findServer(servers, serverName)
if err != nil {
printFatal(err.Error())
}
if server == nil {
printFatal("Server '" + serverName + "' not found")
}
fmt.Printf("Server: %s\n", server.Name)
err = tailLog(*stack, *server, logName)
if err != nil {
printFatal(err.Error())
}
}
func tailLog(stack cloud66.Stack, server cloud66.Server, logName string) error {
sshFile, err := prepareLocalSshKey(server)
must(err)
// open the firewall
var timeToOpen = 2
fmt.Printf("Opening access to %s...\n", server.Address)
genericRes, err := client.LeaseSync(server.StackUid, nil, &timeToOpen, nil, &server.Uid)
must(err)
if genericRes.Status != true {
printFatal("Unable to open server lease")
}
fmt.Printf("Connecting to %s (%s)...\n", server.Name, server.Address)
return startProgram("ssh", []string{
server.UserName + "@" + server.Address,
"-i", sshFile,
"-o", "UserKnownHostsFile=/dev/null",
"-o", "CheckHostIP=no",
"-o", "StrictHostKeyChecking=no",
"-o", "LogLevel=QUIET",
"-o", "IdentitiesOnly=yes",
"-A",
"-p", "22",
fmt.Sprintf("tail -f '%s/web_head/current/log/%s'", stack.DeployDir, logName),
})
}