Skip to content

Commit

Permalink
Implament config struct for Levant to track config during run.
Browse files Browse the repository at this point in the history
This commit adds a config struct to Levant which is used during
every Levant invoction. This allows for easier tracking of info
and also easier future extensibility.

Closes hashicorp#95
  • Loading branch information
jrasell committed Feb 2, 2018
1 parent 4790a94 commit efc80ec
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 106 deletions.
49 changes: 19 additions & 30 deletions command/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/jrasell/levant/helper"
"github.com/jrasell/levant/levant"
"github.com/jrasell/levant/levant/structs"
"github.com/jrasell/levant/logging"
)

Expand Down Expand Up @@ -40,17 +41,17 @@ General Options:
The time in seconds, after which Levant will auto-promote a canary job
if all canaries within the deployment are healthy.
-force-count
Use the taskgroup count from the Nomad jobfile instead of the count that
is currently set in a running job.
-log-level=<level>
Specify the verbosity level of Levant's logs. Valid values include DEBUG,
INFO, and WARN, in decreasing order of verbosity. The default is INFO.
-var-file=<file>
Used in conjunction with the -job-file will deploy a templated job to your
Nomad cluster. [default: levant.(yaml|yml|tf)]
-force-count
Use the taskgroup count from the Nomad jobfile instead of the count that
is currently set in a running job.
`
return strings.TrimSpace(helpText)
}
Expand All @@ -63,33 +64,30 @@ func (c *DeployCommand) Synopsis() string {
// Run triggers a run of the Levant template and deploy functions.
func (c *DeployCommand) Run(args []string) int {

var variables, addr, log, templateFile string
var err error
var job *nomad.Job
var canary int
var forceCount bool
config := &structs.Config{}

flags := c.Meta.FlagSet("deploy", FlagSetVars)
flags.Usage = func() { c.UI.Output(c.Help()) }

flags.StringVar(&addr, "address", "", "")
flags.IntVar(&canary, "canary-auto-promote", 0, "")
flags.StringVar(&log, "log-level", "INFO", "")
flags.StringVar(&variables, "var-file", "", "")
flags.BoolVar(&forceCount, "force-count", false, "")
flags.StringVar(&config.Addr, "address", "", "")
flags.IntVar(&config.Canary, "canary-auto-promote", 0, "")
flags.BoolVar(&config.ForceCount, "force-count", false, "")
flags.StringVar(&config.LogLevel, "log-level", "INFO", "")
flags.StringVar(&config.VaiableFile, "var-file", "", "")

if err = flags.Parse(args); err != nil {
return 1
}

args = flags.Args()

logging.SetLevel(log)
logging.SetLevel(config.LogLevel)

if len(args) == 1 {
templateFile = args[0]
config.TemplateFile = args[0]
} else if len(args) == 0 {
if templateFile = helper.GetDefaultTmplFile(); templateFile == "" {
if config.TemplateFile = helper.GetDefaultTmplFile(); config.TemplateFile == "" {
c.UI.Error(c.Help())
c.UI.Error("\nERROR: Template arg missing and no default template found")
return 1
Expand All @@ -99,35 +97,26 @@ func (c *DeployCommand) Run(args []string) int {
return 1
}

job, err = levant.RenderJob(templateFile, variables, &c.Meta.flagVars)
config.Job, err = levant.RenderJob(config.TemplateFile, config.VaiableFile, &c.Meta.flagVars)
if err != nil {
c.UI.Error(fmt.Sprintf("[ERROR] levant/command: %v", err))
return 1
}

if canary > 0 {
if err = c.checkCanaryAutoPromote(job, canary); err != nil {
if config.Canary > 0 {
if err = c.checkCanaryAutoPromote(config.Job, config.Canary); err != nil {
c.UI.Error(fmt.Sprintf("[ERROR] levant/command: %v", err))
return 1
}

c.UI.Info(fmt.Sprintf("[INFO] levant/command: running canary-auto-update of %vs", canary))
c.UI.Info(fmt.Sprintf("[INFO] levant/command: running canary-auto-update of %vs", config.Canary))
}

client, err := levant.NewNomadClient(addr)
if err != nil {
c.UI.Error(fmt.Sprintf("[ERROR] levant/command: %v", err))
return 1
}

success := client.Deploy(job, canary, forceCount)
success := levant.TriggerDeployment(config)
if !success {
c.UI.Error(fmt.Sprintf("[ERROR] levant/command: deployment of job %s failed", *job.Name))
return 1
}

c.UI.Info(fmt.Sprintf("[INFO] levant/command: deployment of job %s successful", *job.Name))

return 0
}

Expand Down
2 changes: 1 addition & 1 deletion command/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
)

// Meta contains the meta-options and functionality that nearly every
// Packer command inherits.
// Levant command inherits.
type Meta struct {
UI cli.Ui

Expand Down
12 changes: 6 additions & 6 deletions levant/auto_revert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ import (
"github.com/jrasell/levant/logging"
)

func (c *nomadClient) autoRevert(jobID *string) {
func (l *levantDeployment) autoRevert(jobID *string) {

dep, _, err := c.nomad.Jobs().LatestDeployment(*jobID, nil)
dep, _, err := l.nomad.Jobs().LatestDeployment(*jobID, nil)
if err != nil {
logging.Error("levant/auto_revert: unable to query latest deployment of job %s", *jobID)
return
}

logging.Info("levant/auto_revert: beginning deployment watcher for job %s", *jobID)
success := c.deploymentWatcher(dep.ID, 0)
success := l.deploymentWatcher(dep.ID)

if success {
logging.Info("levant/auto_revert: auto-revert of job %s was successful", *jobID)
} else {
logging.Error("levant/auto_revert: auto-revert of job %s failed; POTENTIAL OUTAGE SITUATION", *jobID)
c.checkFailedDeployment(&dep.ID)
l.checkFailedDeployment(&dep.ID)
}
}

// checkAutoRevert inspects a Nomad deployment to determine if any TashGroups
// have been auto-reverted.
func (c *nomadClient) checkAutoRevert(dep *nomad.Deployment) {
func (l *levantDeployment) checkAutoRevert(dep *nomad.Deployment) {

var revert bool

Expand All @@ -44,7 +44,7 @@ func (c *nomadClient) checkAutoRevert(dep *nomad.Deployment) {
dep.JobID)

// Run the levant autoRevert function.
c.autoRevert(&dep.JobID)
l.autoRevert(&dep.JobID)
} else {
logging.Info("levant/auto_revert: job %v is not in auto-revert; POTENTIAL OUTAGE SITUATION", dep.JobID)
}
Expand Down
Loading

0 comments on commit efc80ec

Please sign in to comment.