2018-07-12 19:06:14 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
getter "github.com/hashicorp/go-getter"
|
2018-07-27 03:11:58 +00:00
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
2018-07-12 19:06:14 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2018-07-31 15:41:20 +00:00
|
|
|
const (
|
|
|
|
DefaultEnvironmentsPath = "./environments/"
|
|
|
|
)
|
|
|
|
|
2018-07-12 19:06:14 +00:00
|
|
|
func init() {
|
|
|
|
getter.Getters["file"].(*getter.FileGetter).Copy = true
|
|
|
|
}
|
|
|
|
|
2018-07-31 15:41:20 +00:00
|
|
|
func ProvisionCommandFactory(meta Meta) cli.CommandFactory {
|
2018-07-27 03:11:58 +00:00
|
|
|
return func() (cli.Command, error) {
|
|
|
|
return &Provision{Meta: meta}, nil
|
|
|
|
}
|
2018-07-12 19:06:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Provision struct {
|
2018-07-27 03:11:58 +00:00
|
|
|
Meta
|
2018-07-12 19:06:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Provision) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad-e2e provision <provider> <environment>
|
|
|
|
|
|
|
|
Uses terraform to provision a target test environment to use
|
|
|
|
for end-to-end testing.
|
|
|
|
|
|
|
|
The output is a list of environment variables used to configure
|
|
|
|
various api clients such as Nomad, Consul and Vault.
|
|
|
|
|
2018-07-31 16:01:54 +00:00
|
|
|
General Options:
|
|
|
|
|
|
|
|
` + generalOptionsUsage() + `
|
|
|
|
|
2018-07-12 19:06:14 +00:00
|
|
|
Provision Options:
|
|
|
|
|
|
|
|
-env-path
|
|
|
|
Sets the path for where to search for test environment configuration.
|
2018-07-31 15:41:20 +00:00
|
|
|
This defaults to './environments/'.
|
2018-07-12 19:06:14 +00:00
|
|
|
|
|
|
|
-nomad-binary
|
|
|
|
Sets the target nomad-binary to use when provisioning a nomad cluster.
|
|
|
|
The binary is retrieved by go-getter and can therefore be a local file
|
|
|
|
path, remote http url, or other support go-getter uri.
|
|
|
|
|
|
|
|
-destroy
|
|
|
|
If set, will destroy the target environment.
|
|
|
|
|
|
|
|
-tf-path
|
|
|
|
Sets the path for which terraform state files are stored. Defaults to
|
|
|
|
the current working directory.
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Provision) Synopsis() string {
|
|
|
|
return "Provisions the target testing environment"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Provision) Run(args []string) int {
|
|
|
|
var envPath string
|
|
|
|
var nomadBinary string
|
|
|
|
var destroy bool
|
|
|
|
var tfPath string
|
2018-07-27 03:11:58 +00:00
|
|
|
cmdFlags := c.FlagSet("provision")
|
|
|
|
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
|
2018-07-31 15:41:20 +00:00
|
|
|
cmdFlags.StringVar(&envPath, "env-path", DefaultEnvironmentsPath, "Path to e2e environment terraform configs")
|
2018-07-12 19:06:14 +00:00
|
|
|
cmdFlags.StringVar(&nomadBinary, "nomad-binary", "", "")
|
|
|
|
cmdFlags.BoolVar(&destroy, "destroy", false, "")
|
|
|
|
cmdFlags.StringVar(&tfPath, "tf-path", "", "")
|
|
|
|
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
2018-07-27 03:11:58 +00:00
|
|
|
c.logger.Error("failed to parse flags:", "error", err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if c.verbose {
|
|
|
|
c.logger.SetLevel(hclog.Debug)
|
2018-07-12 19:06:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
args = cmdFlags.Args()
|
|
|
|
if len(args) != 2 {
|
2018-07-27 03:11:58 +00:00
|
|
|
c.logger.Error("expected 2 args (provider and environment)", "args", args)
|
2018-07-12 19:06:14 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 03:11:58 +00:00
|
|
|
env, err := newEnv(envPath, args[0], args[1], tfPath, c.logger)
|
2018-07-12 19:06:14 +00:00
|
|
|
if err != nil {
|
2018-07-27 03:11:58 +00:00
|
|
|
c.logger.Error("failed to build environment", "error", err)
|
|
|
|
return 1
|
2018-07-12 19:06:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if destroy {
|
|
|
|
if err := env.destroy(); err != nil {
|
2018-07-27 03:11:58 +00:00
|
|
|
c.logger.Error("failed to destroy environment", "error", err)
|
2018-07-12 19:06:14 +00:00
|
|
|
return 1
|
|
|
|
}
|
2018-07-27 03:11:58 +00:00
|
|
|
c.logger.Debug("environment successfully destroyed")
|
2018-07-12 19:06:14 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use go-getter to fetch the nomad binary
|
2018-07-26 04:03:23 +00:00
|
|
|
nomadPath, err := fetchBinary(nomadBinary)
|
2018-07-12 19:06:14 +00:00
|
|
|
defer os.RemoveAll(nomadPath)
|
2018-07-31 15:41:20 +00:00
|
|
|
if err != nil {
|
|
|
|
c.logger.Error("failed to fetch nomad binary", "error", err)
|
|
|
|
return 1
|
|
|
|
}
|
2018-07-12 19:06:14 +00:00
|
|
|
|
|
|
|
results, err := env.provision(nomadPath)
|
|
|
|
if err != nil {
|
2018-07-27 03:11:58 +00:00
|
|
|
c.logger.Error("", "error", err)
|
|
|
|
return 1
|
2018-07-12 19:06:14 +00:00
|
|
|
}
|
|
|
|
|
2018-07-31 16:01:54 +00:00
|
|
|
c.Ui.Output(strings.TrimSpace(fmt.Sprintf(`
|
2018-07-12 19:06:14 +00:00
|
|
|
NOMAD_ADDR=%s
|
2018-07-31 16:01:54 +00:00
|
|
|
`, results.nomadAddr)))
|
2018-07-12 19:06:14 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|