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

Commit

Permalink
Merge branch '15-as-a-user-i-want-to-be-able-to-stop-packs-entire-job…
Browse files Browse the repository at this point in the history
…s' into 'master'

Resolve "As a User I want to be able to stop Packs (entire jobs)"

Closes #15

See merge request Qm64/backpack!13
  • Loading branch information
koalalorenzo committed Nov 15, 2020
2 parents 108a434 + 2bbeb0e commit c8c5a74
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 24 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- It is possible to plan changes and check the differences before applying them via `backpack plan` ([mr11](https://gitlab.com/Qm64/backpack/-/merge_requests/11), [#12](https://gitlab.com/Qm64/backpack/-/issues/12))
- It is possible to stop jobs of a pack using `backpack stop` ([mr13](https://gitlab.com/Qm64/backpack/-/merge_requests/13), [#15](https://gitlab.com/Qm64/backpack/-/issues/15))
- CLI Aliases for Helm users (ex: `backpack uninstall` is `backpack stop`)([mr13](https://gitlab.com/Qm64/backpack/-/merge_requests/13))

### Removed
### Changed
- Optimize the Connection struct and avoid repeating tasks ([mr11](https://gitlab.com/Qm64/backpack/-/merge_requests/11))
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ the differences between k8s and nomad
To learn more about the motivation behind the development of this project
check [the blog post on Qm64 website](https://qm64.tech/posts/202011-hashicorp-nomad-backpack/).

If you need some help or you want to stay updated with the latest news,
[join Qm64's chatroom on Matrix](https://matrix.to/#/#qm64:matrix.org?via=matrix.org)

Backpack is currently tested against Nomad version 0.12.8

## TL;DR: Install
Expand Down
5 changes: 3 additions & 2 deletions cmd/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (

// packCmd represents the pack command
var packCmd = &cobra.Command{
Use: "pack [path]",
Short: "Build a Backpack file (pack) from a directory/template",
Use: "pack [path]",
Aliases: []string{"package"},
Short: "Build a Backpack file (pack) from a directory/template",
Long: `Generate a Backpack file (pack) from a directory containing the various
jobs, metadata and documentation.
Expand Down
9 changes: 5 additions & 4 deletions cmd/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import (

// planCmd represents the run command
var planCmd = &cobra.Command{
Use: "plan [path]",
Args: cobra.ExactArgs(1),
Short: "Plan and check the changes before running a pack",
Use: "plan [path]",
Aliases: []string{"diff", "dry-run"},
Args: cobra.ExactArgs(1),
Short: "Plan and check the changes before running a pack",
Long: `It allows you to plan ahead before running/registering jobs.
It is useful when combined with existing jobs to validate changes. By default
the output shows you a brief summary of changes, but if you want to see the
Expand Down Expand Up @@ -81,7 +82,7 @@ func planRun(cmd *cobra.Command, args []string) {
}
// For each job file perform the plan! 🚀
for name, hcl := range bts {
job, err := client.GetJob(string(hcl))
job, err := client.GetJobFromCode(string(hcl))
if err != nil {
log.Fatalf("Error obtaining job %s: %s", name, err)
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (

// runCmd represents the run command
var runCmd = &cobra.Command{
Use: "run [path]",
Args: cobra.ExactArgs(1),
Short: "Starts the jobs of a pack",
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
Expand Down Expand Up @@ -62,7 +63,7 @@ func runRun(cmd *cobra.Command, args []string) {
// then store the job ID in the backpack to show it afterwards.
jIDs := map[string]string{}
for name, hcl := range bts {
job, err := client.GetJob(string(hcl))
job, err := client.GetJobFromCode(string(hcl))
if err != nil {
log.Fatalf("Error obtaining job %s: %s", name, err)
}
Expand Down
100 changes: 100 additions & 0 deletions cmd/stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package cmd

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

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

// stopCmd represents the run command
var stopCmd = &cobra.Command{
Use: "stop [path]",
Aliases: []string{"uninstall", "delete"},
Args: cobra.ExactArgs(1),
Short: "Stop all the jobs in a pack",
Long: `
`,
Run: stopRun,
}

func init() {
rootCmd.AddCommand(stopCmd)
stopCmd.Flags().BoolP("purge", "p", false, "Delete the jobs, without waiting for the GC to fully delete them")
stopCmd.Flags().BoolP("unpacked", "u", false, "instead of reading from a file, read from a directory")
}

// This is the actual command..
func stopRun(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)
}

purgeJob, err := cmd.Flags().GetBool("purge")
if err != nil {
log.Fatalf("Error parsing CLI flags (purge): %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, "Recap of stopping jobs in \"%s\" backpack:\n", b.Name)
fmt.Fprintln(w, "File Name\tJob ID\tStatus\tError if any")
// For each job file go and stop 🚀
for name, hcl := range bts {
job, err := client.GetJobFromCode(string(hcl))
if err != nil {
log.Fatalf("Error obtaining job %s: %s", name, err)
}

// Stop the job
_, err = client.Stop(*job.ID, purgeJob)
if err != nil {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t\n", name, *job.ID, *job.Status, err)
continue
}

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

fmt.Fprintf(w, "%s\t%s\t%s\t%s\t\n", name, *jobAfter.ID, *jobAfter.Status, "")
} else {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t\n", name, *job.ID, "purged", "")
}

}
// 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)
}
9 changes: 5 additions & 4 deletions cmd/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (

// unpackCmd represents the unpack command
var unpackCmd = &cobra.Command{
Use: "unpack [file.backpack]",
Args: cobra.ExactArgs(1),
Run: unpackRun,
Short: "Opens a pack file to explore content",
Use: "unpack [file.backpack]",
Aliases: []string{"pull"},
Args: cobra.ExactArgs(1),
Run: unpackRun,
Short: "Opens a pack file to explore content",
Long: `Explodes/Open the pack inside a directory. This is useful to edit a
pack, inspecting it or seeing default values... or if you are looking for
something inside it and you know it is at the bottom of the backpack!
Expand Down
2 changes: 1 addition & 1 deletion conn/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewClient() (co *Client, err error) {

// IsValid that is the question!
func (co *Client) IsValid(code string) bool {
job, err := co.GetJob(code)
job, err := co.GetJobFromCode(code)
if err != nil {
return false
}
Expand Down
11 changes: 8 additions & 3 deletions conn/client_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import (
"github.com/hashicorp/nomad/api"
)

// Run is parsing the HCL code and registering the Job into Nomad
func (co *Client) GetJob(code string) (*api.Job, error) {
return co.c.Jobs().ParseHCL(code, true)
// GetJobFromCode is parsing the HCL code and providing a api.Job{} pointer
func (co *Client) GetJobFromCode(code string) (*api.Job, error) {
return co.jobs.ParseHCL(code, true)
}

func (co *Client) GetJobStatus(jobId string) (j *api.Job, err error) {
j, _, err = co.jobs.Info(jobId, nil)
return
}
11 changes: 11 additions & 0 deletions conn/client_stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package conn

// Stop stops a specific Job
func (co *Client) Stop(jobID string, purge bool) (evalID string, err error) {
evalID, _, err = co.jobs.Deregister(jobID, purge, nil)
if err != nil {
return "", err
}

return evalID, nil
}
9 changes: 6 additions & 3 deletions docs/examples/unpacked/fabiolb/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# How to deploy FabioLB

This backpack allows to deploy [FabioLB](http://fabiolb.net/).
Please check the values inline documentation for more information
This pack allows to deploy [FabioLB](http://fabiolb.net/).
Please check the values inline documentation for more information. If you
have unpacked the documentation only, unpack the whole package to check
the YAML and HCL files! 😉

If you see something missing, feel free to open an Issue or to submit a
pull request / merge request on [our repository](http://gitlab.com/Qm64/backpack)
pull request / merge request on [our repository](http://gitlab.com/Qm64/backpack)
or [join Qm64's chatroom on Matrix](https://matrix.to/#/#qm64:matrix.org?via=matrix.org)
7 changes: 4 additions & 3 deletions docs/examples/unpacked/redis/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# How to deploy Redis

This backpack allows to deploy [Redis](http://redis.io/).
This pack allows to deploy [Redis](http://redis.io/).
Please check the values inline documentation for more information

The version of this backpack is following the major version of Redis
The version of this pack is following the major version of Redis

If you see something missing, feel free to open an Issue or to submit a
pull request / merge request on [our repository](http://gitlab.com/Qm64/backpack)
pull request / merge request on [our repository](http://gitlab.com/Qm64/backpack)
or [join Qm64's chatroom on Matrix](https://matrix.to/#/#qm64:matrix.org?via=matrix.org)

0 comments on commit c8c5a74

Please sign in to comment.