Skip to content

Commit

Permalink
Add --all to upgrade flag
Browse files Browse the repository at this point in the history
This makes it easier for staff to upgrade a number of hosts
at once. Tested with openfaas runners.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed May 26, 2023
1 parent 40b8965 commit 77225fb
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 87 deletions.
87 changes: 0 additions & 87 deletions cmd/update.go

This file was deleted.

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

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

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

func makeUpgrade() *cobra.Command {
cmd := &cobra.Command{
Use: "upgrade",
Short: "Upgrade an agent's kernel and root filesystem",
Example: ` # Upgrade the agent if a newer one is available
actuated-cli upgrade --owner ORG --host HOST
# Force an upgrade, even if on the latest version of the agent
actuated-cli upgrade --owner ORG --host HOST --host
`,
}

cmd.RunE = runUpgradeE

cmd.Flags().StringP("owner", "o", "", "Owner")
cmd.Flags().BoolP("staff", "s", false, "Staff flag")
cmd.Flags().BoolP("force", "f", false, "Force upgrade")
cmd.Flags().String("host", "", "Host to upgrade")
cmd.Flags().BoolP("all", "a", false, "Upgrade all hosts instead of giving --host")

return cmd
}

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

force, err := cmd.Flags().GetBool("force")
if err != nil {
return err
}

host, err := cmd.Flags().GetString("host")
if err != nil {
return err
}

allHosts, err := cmd.Flags().GetBool("all")
if err != nil {
return err
}

if !allHosts && len(host) == 0 {
return fmt.Errorf("--all or --host is required")
}

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"))

var upgradeHosts []Host
if allHosts {
hosts, httpStatus, err := c.ListRunners(pat, owner, staff, true)
if err != nil {
return err
}
if httpStatus != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", httpStatus)
}

var hostsList []Host

if err := json.Unmarshal([]byte(hosts), &hostsList); err != nil {
return err
}
reachableHosts := []Host{}

for _, h := range hostsList {
if h.Reachable {
reachableHosts = append(reachableHosts, h)
}
}

if len(reachableHosts) == 0 {
return fmt.Errorf("no reachable hosts found")
}
upgradeHosts = reachableHosts

} else {
upgradeHosts = []Host{
{
Name: host,
Customer: owner,
},
}
}

for _, h := range upgradeHosts {

fmt.Printf("Upgrading: %s for: %s\n", h.Name, h.Customer)
res, status, err := c.UpgradeAgent(pat, h.Customer, h.Name, force, 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("Upgrade requested for %s, status: %d\n", owner, status)
if strings.TrimSpace(res) != "" {
fmt.Printf("Response: %s\n", res)
}
}

return nil
}

type Host struct {
Name string `json:"name"`
Customer string `json:"customer"`
Reachable bool `json:"reachable"`
}

0 comments on commit 77225fb

Please sign in to comment.