This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstatus.go
115 lines (98 loc) · 3.29 KB
/
status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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. By default it shows the allocations that
are running, or the first allocation available. If you want to see all the
previous allocation you can use the option --all (or -a).
`,
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)
}