Skip to content

Commit

Permalink
Add disable endpoint from agent
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 Jun 10, 2024
1 parent cb43df2 commit 75b4e5f
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
77 changes: 77 additions & 0 deletions cmd/disable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package cmd

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

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

func makeDisable() *cobra.Command {
cmd := &cobra.Command{
Use: "disable",
Short: "Disable the actuated service remotely.",
Example: ` # Disable the actuated systemd service from restarting
actuated-cli disable --owner ORG HOST
`,
}

cmd.RunE = runDisableE

cmd.Flags().StringP("owner", "o", "", "Owner")

return cmd
}

func runDisableE(cmd *cobra.Command, args []string) error {
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
}

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

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

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

res, status, err := c.DisableAgent(pat, owner, host, staff)
if err != nil {
return err
}

if status != http.StatusOK && status != http.StatusAccepted &&
status != http.StatusNoContent && status != http.StatusCreated {
return fmt.Errorf("unexpected status code: %d, error: %s", status, res)
}

fmt.Printf("Disable requested for %s, status: %d\n", owner, status)
if strings.TrimSpace(res) != "" {
fmt.Printf("Response: %s\n", res)
}

return nil
}
49 changes: 49 additions & 0 deletions pkg/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,3 +500,52 @@ func (c *Client) RestartAgent(patStr, owner, host string, reboot bool, staff boo

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

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

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

q := u.Query()
q.Set("owner", owner)
q.Set("host", host)

if staff {
q.Set("staff", "1")
}

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
}

0 comments on commit 75b4e5f

Please sign in to comment.