Skip to content

Commit 75b4e5f

Browse files
committed
Add disable endpoint from agent
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent cb43df2 commit 75b4e5f

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

cmd/disable.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"os"
7+
"strings"
8+
9+
"github.com/self-actuated/actuated-cli/pkg"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
func makeDisable() *cobra.Command {
14+
cmd := &cobra.Command{
15+
Use: "disable",
16+
Short: "Disable the actuated service remotely.",
17+
Example: ` # Disable the actuated systemd service from restarting
18+
19+
actuated-cli disable --owner ORG HOST
20+
`,
21+
}
22+
23+
cmd.RunE = runDisableE
24+
25+
cmd.Flags().StringP("owner", "o", "", "Owner")
26+
27+
return cmd
28+
}
29+
30+
func runDisableE(cmd *cobra.Command, args []string) error {
31+
if len(args) < 1 {
32+
return fmt.Errorf("specify the host as an argument")
33+
}
34+
host := strings.TrimSpace(args[0])
35+
36+
pat, err := getPat(cmd)
37+
if err != nil {
38+
return err
39+
}
40+
41+
staff, err := cmd.Flags().GetBool("staff")
42+
if err != nil {
43+
return err
44+
}
45+
46+
owner, err := cmd.Flags().GetString("owner")
47+
if err != nil {
48+
return err
49+
}
50+
51+
if len(owner) == 0 {
52+
return fmt.Errorf("owner is required")
53+
}
54+
55+
if len(pat) == 0 {
56+
return fmt.Errorf("pat is required")
57+
}
58+
59+
c := pkg.NewClient(http.DefaultClient, os.Getenv("ACTUATED_URL"))
60+
61+
res, status, err := c.DisableAgent(pat, owner, host, staff)
62+
if err != nil {
63+
return err
64+
}
65+
66+
if status != http.StatusOK && status != http.StatusAccepted &&
67+
status != http.StatusNoContent && status != http.StatusCreated {
68+
return fmt.Errorf("unexpected status code: %d, error: %s", status, res)
69+
}
70+
71+
fmt.Printf("Disable requested for %s, status: %d\n", owner, status)
72+
if strings.TrimSpace(res) != "" {
73+
fmt.Printf("Response: %s\n", res)
74+
}
75+
76+
return nil
77+
}

pkg/client.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,3 +500,52 @@ func (c *Client) RestartAgent(patStr, owner, host string, reboot bool, staff boo
500500

501501
return string(body), res.StatusCode, nil
502502
}
503+
504+
func (c *Client) DisableAgent(patStr, owner, host string, staff bool) (string, int, error) {
505+
506+
u, _ := url.Parse(c.baseURL)
507+
u.Path = "/api/v1/disable"
508+
509+
q := u.Query()
510+
q.Set("owner", owner)
511+
q.Set("host", host)
512+
513+
if staff {
514+
q.Set("staff", "1")
515+
}
516+
517+
u.RawQuery = q.Encode()
518+
519+
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
520+
if err != nil {
521+
return "", http.StatusBadRequest, err
522+
}
523+
524+
req.Header.Set("Authorization", "Bearer "+patStr)
525+
526+
if os.Getenv("DEBUG") == "1" {
527+
sanitised := http.Header{}
528+
for k, v := range req.Header {
529+
530+
if k == "Authorization" {
531+
v = []string{"redacted"}
532+
}
533+
sanitised[k] = v
534+
}
535+
536+
fmt.Printf("URL %s\nHeaders: %v\n", u.String(), sanitised)
537+
}
538+
539+
res, err := c.httpClient.Do(req)
540+
if err != nil {
541+
return "", http.StatusBadRequest, err
542+
}
543+
544+
var body []byte
545+
if res.Body != nil {
546+
defer res.Body.Close()
547+
body, _ = io.ReadAll(res.Body)
548+
}
549+
550+
return string(body), res.StatusCode, nil
551+
}

0 commit comments

Comments
 (0)