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 pathrun.go
88 lines (75 loc) · 2.28 KB
/
run.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
package cmd
import (
"fmt"
"log"
"os"
"text/tabwriter"
"github.com/spf13/cobra"
"gitlab.com/qm64/backpack/conn"
"gitlab.com/qm64/backpack/templating"
)
// runCmd represents the run command
var runCmd = &cobra.Command{
Use: "run [path]",
Aliases: []string{"start", "install"},
Args: cobra.ExactArgs(1),
Short: "Starts the jobs of a pack",
Long: `It allows to run different jobs specified in the pack.
It accepts one argument that is the path or URL of the file, but if the option
--unpacked (or -u is) passed it consider the first argument as the path of an
unpacked directory that will be used instead of a file.
`,
Run: runRun,
}
func init() {
rootCmd.AddCommand(runCmd)
runCmd.Flags().StringP("values", "v", "", "specifies the file to use for values and ensure to populate the Go Templates")
runCmd.Flags().BoolP("unpacked", "u", false, "instead of reading from a file, read from a directory")
runCmd.Flags().Bool("debug", false, "prints the jobs on stdout instead of sending them to nomad")
}
// This is the actual command..
func runRun(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)
}
values := getValuesFromCLIInput(cmd)
// Populate the template into job files 💪
bts, err := templating.BuildHCL(&b, values)
if err != nil {
log.Fatalf("Error building the HCL files: %s", err)
}
debugFlag, _ := cmd.Flags().GetBool("debug")
if debugFlag {
for name, hcl := range bts {
log.Printf("File: %s\n", name)
fmt.Println(string(hcl))
}
return
}
// For each job file run it! 🚀
// then store the job ID in the backpack to show it afterwards.
jIDs := map[string]string{}
for name, hcl := range bts {
job, err := client.GetJobFromCode(string(hcl))
if err != nil {
log.Fatalf("Error obtaining job %s: %s", name, err)
}
// Run = Register the job
jr, err := client.Run(job)
if err != nil {
log.Fatalf("Error running %s: %s", name, err)
}
jIDs[name] = jr.EvalID
}
b.JobsEvalIDs = jIDs
// Prepare a table for the output
w := tabwriter.NewWriter(os.Stdout, 3, 0, 2, ' ', 0)
fmt.Fprintln(w, "File Name\tJob ID\t")
for n, jID := range b.JobsEvalIDs {
fmt.Fprintf(w, "%s\t%s\t\n", n, jID)
}
w.Flush()
}