github.com/smithx10/nomad@v0.9.1-rc1/e2e/cli/command/provision.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  
     8  	getter "github.com/hashicorp/go-getter"
     9  	hclog "github.com/hashicorp/go-hclog"
    10  	"github.com/mitchellh/cli"
    11  )
    12  
    13  const (
    14  	DefaultEnvironmentsPath = "./environments/"
    15  )
    16  
    17  func init() {
    18  	getter.Getters["file"].(*getter.FileGetter).Copy = true
    19  }
    20  
    21  func ProvisionCommandFactory(meta Meta) cli.CommandFactory {
    22  	return func() (cli.Command, error) {
    23  		return &Provision{Meta: meta}, nil
    24  	}
    25  }
    26  
    27  type Provision struct {
    28  	Meta
    29  }
    30  
    31  func (c *Provision) Help() string {
    32  	helpText := `
    33  Usage: nomad-e2e provision <provider> <environment>
    34  
    35    Uses terraform to provision a target test environment to use
    36    for end-to-end testing.
    37  
    38    The output is a list of environment variables used to configure
    39    various api clients such as Nomad, Consul and Vault.
    40  
    41  General Options:
    42  
    43  ` + generalOptionsUsage() + `
    44  
    45  Provision Options:
    46  
    47    -env-path
    48      Sets the path for where to search for test environment configuration.
    49      This defaults to './environments/'.
    50  
    51    -nomad-binary
    52      Sets the target nomad-binary to use when provisioning a nomad cluster.
    53  	The binary is retrieved by go-getter and can therefore be a local file
    54  	path, remote http url, or other support go-getter uri.
    55  
    56    -destroy
    57      If set, will destroy the target environment.
    58  
    59    -tf-path
    60      Sets the path for which terraform state files are stored. Defaults to
    61  	the current working directory.
    62  `
    63  	return strings.TrimSpace(helpText)
    64  }
    65  
    66  func (c *Provision) Synopsis() string {
    67  	return "Provisions the target testing environment"
    68  }
    69  
    70  func (c *Provision) Run(args []string) int {
    71  	var envPath string
    72  	var nomadBinary string
    73  	var destroy bool
    74  	var tfPath string
    75  	cmdFlags := c.FlagSet("provision")
    76  	cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
    77  	cmdFlags.StringVar(&envPath, "env-path", DefaultEnvironmentsPath, "Path to e2e environment terraform configs")
    78  	cmdFlags.StringVar(&nomadBinary, "nomad-binary", "", "")
    79  	cmdFlags.BoolVar(&destroy, "destroy", false, "")
    80  	cmdFlags.StringVar(&tfPath, "tf-path", "", "")
    81  
    82  	if err := cmdFlags.Parse(args); err != nil {
    83  		c.logger.Error("failed to parse flags:", "error", err)
    84  		return 1
    85  	}
    86  	if c.verbose {
    87  		c.logger.SetLevel(hclog.Debug)
    88  	}
    89  
    90  	args = cmdFlags.Args()
    91  	if len(args) != 2 {
    92  		c.logger.Error("expected 2 args (provider and environment)", "args", args)
    93  		return 0
    94  	}
    95  
    96  	env, err := newEnv(envPath, args[0], args[1], tfPath, c.logger)
    97  	if err != nil {
    98  		c.logger.Error("failed to build environment", "error", err)
    99  		return 1
   100  	}
   101  
   102  	if destroy {
   103  		if err := env.destroy(); err != nil {
   104  			c.logger.Error("failed to destroy environment", "error", err)
   105  			return 1
   106  		}
   107  		c.logger.Debug("environment successfully destroyed")
   108  		return 0
   109  	}
   110  
   111  	// Use go-getter to fetch the nomad binary
   112  	nomadPath, err := fetchBinary(nomadBinary)
   113  	defer os.RemoveAll(nomadPath)
   114  	if err != nil {
   115  		c.logger.Error("failed to fetch nomad binary", "error", err)
   116  		return 1
   117  	}
   118  
   119  	results, err := env.provision(nomadPath)
   120  	if err != nil {
   121  		c.logger.Error("", "error", err)
   122  		return 1
   123  	}
   124  
   125  	c.Ui.Output(strings.TrimSpace(fmt.Sprintf(`
   126  NOMAD_ADDR=%s
   127  	`, results.nomadAddr)))
   128  
   129  	return 0
   130  }