Skip to content

Commit

Permalink
Add command for staff for controller logs
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Nov 3, 2023
1 parent 19d1a3e commit 9f0b74b
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 1 deletion.
66 changes: 66 additions & 0 deletions cmd/controller-logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cmd

import (
"fmt"
"net/http"
"os"
"time"

"github.com/self-actuated/actuated-cli/pkg"
"github.com/spf13/cobra"
)

func makeControllerLogs() *cobra.Command {
cmd := &cobra.Command{
Use: "logs",
Short: "Fetch logs from the controller's systemd service",
Example: ` # Latest logs for a given host:
actuated controller logs --age 15m
`,
}

cmd.RunE = runControllerLogsE

cmd.Flags().DurationP("age", "a", time.Minute*15, "Age of logs to fetch")
cmd.Flags().StringP("output", "o", "cat", "Output format, use \"cat\" for brevity")

return cmd
}

func runControllerLogsE(cmd *cobra.Command, args []string) error {

pat, err := getPat(cmd)
if err != nil {
return err
}

outputFormat, err := cmd.Flags().GetString("output")
if err != nil {
return err
}
age, err := cmd.Flags().GetDuration("age")
if err != nil {
return err
}

if len(pat) == 0 {
return fmt.Errorf("pat is required")
}

c := pkg.NewClient(http.DefaultClient, os.Getenv("ACTUATED_URL"))

res, status, err := c.GetControllerLogs(pat, outputFormat, age)

if err != nil {
return err
}

if status != http.StatusOK {
return fmt.Errorf("unexpected status code: %d, body: %s", status, res)
}

fmt.Println(res)

return nil

}
20 changes: 20 additions & 0 deletions cmd/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"github.com/spf13/cobra"
)

func makeController() *cobra.Command {

controller := &cobra.Command{
Use: "controller",
Short: "Staff commands for the controller",
Hidden: true,
SilenceErrors: true,
SilenceUsage: true,
}

controller.AddCommand(makeControllerLogs())

return controller
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ https://github.com/self-actuated/actuated-cli
root.AddCommand(makeAuth())
root.AddCommand(MakeVersion())

root.AddCommand(makeController())

}

func Execute() error {
Expand Down
1 change: 0 additions & 1 deletion cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func runUpgradeE(cmd *cobra.Command, args []string) error {
}

if len(hostsList) == 0 {

return fmt.Errorf("no hosts found")
}
upgradeHosts = hostsList
Expand Down
51 changes: 51 additions & 0 deletions pkg/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,57 @@ func (c *Client) GetAgentLogs(patStr, owner, host string, age time.Duration, sta
return string(body), res.StatusCode, nil
}

func (c *Client) GetControllerLogs(patStr, outputFormat string, age time.Duration) (string, int, error) {

mins := int(age.Minutes())

u, _ := url.Parse(c.baseURL)
u.Path = "/api/v1/controller/logs"

q := u.Query()

q.Set("age", fmt.Sprintf("%dm", mins))

if len(outputFormat) > 0 {
q.Set("output", outputFormat)
}

u.RawQuery = q.Encode()

req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return "", http.StatusBadRequest, err
}

req.Header.Set("Authorization", "Bearer "+patStr)

if os.Getenv("DEBUG") == "1" {
sanitised := http.Header{}
for k, v := range req.Header {

if k == "Authorization" {
v = []string{"redacted"}
}
sanitised[k] = v
}

fmt.Printf("URL %s\nHeaders: %v\n", u.String(), sanitised)
}

res, err := c.httpClient.Do(req)
if err != nil {
return "", http.StatusBadRequest, err
}

var body []byte
if res.Body != nil {
defer res.Body.Close()
body, _ = io.ReadAll(res.Body)
}

return string(body), res.StatusCode, nil
}

func (c *Client) UpgradeAgent(patStr, owner, host string, force bool, staff bool) (string, int, error) {

u, _ := url.Parse(c.baseURL)
Expand Down

0 comments on commit 9f0b74b

Please sign in to comment.