-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathagent_logs.go
More file actions
100 lines (76 loc) · 1.92 KB
/
agent_logs.go
File metadata and controls
100 lines (76 loc) · 1.92 KB
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
package cmd
import (
"fmt"
"net/http"
"strings"
"time"
"github.com/self-actuated/actuated-cli/pkg"
"github.com/spf13/cobra"
)
func makeAgentLogs() *cobra.Command {
cmd := &cobra.Command{
Use: "agent-logs",
Short: "Fetch logs from the agent's systemd service",
Long: `Fetch the service logs from a remote server to if you need
to confirm the rollout of updates, agent version, and to troubleshoot
VM launches.`,
Example: ` # Latest logs for a given OWNER and HOST:
actuated agent-logs --owner OWNER HOST
# Latest logs for a given time-range
actuated agent-logs --owner OWNER --age 1h HOST
`,
Aliases: []string{"service-logs", "al"},
}
cmd.RunE = runAgentLogsE
cmd.Flags().StringP("owner", "o", "", "Owner for the logs")
cmd.Flags().DurationP("age", "a", time.Minute*15, "Age of logs to fetch")
return cmd
}
func runAgentLogsE(cmd *cobra.Command, args []string) error {
if err := checkGitHubOnly("agent-logs"); err != nil {
return err
}
if len(args) < 1 {
return fmt.Errorf("specify the host as an argument")
}
host := strings.TrimSpace(args[0])
pat, err := getPat(cmd)
if err != nil {
return err
}
staff, err := cmd.Flags().GetBool("staff")
if err != nil {
return err
}
owner, err := cmd.Flags().GetString("owner")
if err != nil {
return err
}
age, err := cmd.Flags().GetDuration("age")
if err != nil {
return err
}
if len(host) == 0 {
return fmt.Errorf("host is required")
}
if len(owner) == 0 {
return fmt.Errorf("owner is required")
}
if len(pat) == 0 {
return fmt.Errorf("pat is required")
}
controllerURL, err := getControllerURL()
if err != nil {
return err
}
c := pkg.NewClient(http.DefaultClient, controllerURL)
res, status, err := c.GetAgentLogs(pat, owner, host, age, staff)
if err != nil {
return err
}
if status != http.StatusAccepted {
return fmt.Errorf("unexpected status code: %d, body: %s", status, res)
}
fmt.Println(res)
return nil
}