Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

Commit

Permalink
shows allocation status
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenzo Setale <[email protected]>
  • Loading branch information
koalalorenzo committed Nov 15, 2020
1 parent c8c5a74 commit 5c15dc7
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 4 deletions.
112 changes: 112 additions & 0 deletions cmd/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package cmd

import (
"fmt"
"io/ioutil"
"log"
"os"
"text/tabwriter"
"time"

"github.com/hashicorp/nomad/api"
"github.com/spf13/cobra"
"gitlab.com/qm64/backpack/conn"
"gitlab.com/qm64/backpack/pkg"
"gitlab.com/qm64/backpack/templating"
)

// statusCmd represents the status command
var statusCmd = &cobra.Command{
Use: "status [path]",
Aliases: []string{"state", "alloc"},
Args: cobra.ExactArgs(1),
Short: "Check the status of all the jobs in a pack",
Long: `Run this command to know the status of all the jobs in a pack. It will
check and provide useful information.
`,
Run: statusRun,
}

func init() {
rootCmd.AddCommand(statusCmd)
statusCmd.Flags().BoolP("all", "a", false, "show all allocations")
statusCmd.Flags().BoolP("unpacked", "u", false, "instead of reading from a file, read from a directory")
}

var showAllocationsWithStatus = map[string]bool{
api.AllocClientStatusRunning: true,
api.AllocClientStatusPending: true,
}

// This is the actual command..
func statusRun(cmd *cobra.Command, args []string) {
b := getPackFromCLIInput(cmd, args)
var err error

client, err := conn.NewClient()
if err != nil {
log.Fatalf("Error creating new Nomad Client: %s", err)
}

showAllAlloc, err := cmd.Flags().GetBool("all")
if err != nil {
log.Fatalf("Error parsing CLI flags (all): %s", err)
}

// Populate the template into job files 💪
bts, err := templating.BuildHCL(&b, pkg.ValuesType{})
if err != nil {
log.Fatalf("Error building the HCL files: %s", err)
}

// Prepare a table for the output in a buffer. This is done so that we can
// have a table after outputting the Plans for each job
rt, wt, err := os.Pipe()
if err != nil {
log.Fatal("Error preparing the output table:", err)
}

defer rt.Close()
w := tabwriter.NewWriter(wt, 3, 0, 4, ' ', 0)
fmt.Fprintf(w, "Status of the jobs' allocations from \"%s\" backpack:\n", b.Name)
fmt.Fprintln(w, "Job ID\tAlloc ID\tStatus/Desired\tModified At\tError")

for name, hcl := range bts {
job, err := client.GetJobFromCode(string(hcl))
if err != nil {
log.Fatalf("Error obtaining job %s: %s", name, err)
}

jobResult, err := client.GetJobStatus(*job.ID)
if err != nil {
fmt.Fprintf(w, "%s\t\t%s\t\t%s\n", *job.ID, *job.Status, err)
continue
}

allocations, err := client.GetJobAllocations(*job.ID)
if err != nil {
fmt.Fprintf(w, "%s\t\t%s\t\t%s\n", *job.ID, *jobResult.Status, err)
continue
}

// fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t\n", *jobResult.ID, "(check allocations)", *jobResult.Status, "", "")
for i, alloc := range allocations {
_, ok := showAllocationsWithStatus[alloc.ClientStatus]
if !showAllAlloc && len(allocations) > 1 && i != 0 && !ok {
continue
}
lt := time.Unix(0, alloc.ModifyTime).Format(time.RFC3339)
allocID := sanitizeUUIDPrefix(alloc.ID)
fmt.Fprintf(w, "%s\t%s\t%s/%s\t%s\t\n", *jobResult.ID, allocID, alloc.ClientStatus, alloc.DesiredStatus, lt)
}

}
// Flushes all the table output after all the plans output.
w.Flush()
wt.Close()
output, err := ioutil.ReadAll(rt)
if err != nil {
log.Fatal("Error reading the output table after operation completed:", err)
}
os.Stdout.Write(output)
}
5 changes: 4 additions & 1 deletion cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ var stopCmd = &cobra.Command{
Aliases: []string{"uninstall", "delete"},
Args: cobra.ExactArgs(1),
Short: "Stop all the jobs in a pack",
Long: `
Long: `This command will stop all the jobs available in a pack. By default it
mimics nomad CLI, that keeps the job as "dead" until the garbage collector
deletes the job. If you want to delete the jobs entirely and lose all the setup
you can pass the option --purge (or -p).
`,
Run: stopRun,
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/url"
"os"
"strings"

"github.com/spf13/cobra"
"gitlab.com/qm64/backpack/pkg"
Expand Down Expand Up @@ -118,3 +119,7 @@ func getValuesFromCLIInput(cmd *cobra.Command) pkg.ValuesType {
}
return values
}

func sanitizeUUIDPrefix(s string) string {
return strings.Split(s, "-")[0]
}
7 changes: 4 additions & 3 deletions conn/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
)

type Client struct {
c *api.Client
jobs *api.Jobs
c *api.Client
jobs *api.Jobs
alloc *api.Allocations
}

// NewClient returns a new client configured with the default values for Nomad
Expand All @@ -18,7 +19,7 @@ func NewClient() (co *Client, err error) {
return nil, err
}
co.jobs = co.c.Jobs()

co.alloc = co.c.Allocations()
return
}

Expand Down
17 changes: 17 additions & 0 deletions conn/client_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,20 @@ func (co *Client) GetJobStatus(jobId string) (j *api.Job, err error) {
j, _, err = co.jobs.Info(jobId, nil)
return
}

func (co *Client) GetJobAllocations(jobId string) (alls []*api.Allocation, err error) {
allList, _, err := co.jobs.Allocations(jobId, false, nil)
if err != nil {
return
}

alls = []*api.Allocation{}
for _, al := range allList {
alloc, _, err := co.alloc.Info(al.ID, nil)
if err != nil {
return []*api.Allocation{}, err
}
alls = append(alls, alloc)
}
return
}

0 comments on commit 5c15dc7

Please sign in to comment.