terraform/command/push.go

545 lines
14 KiB
Go
Raw Normal View History

2015-03-05 05:42:26 +01:00
package command
import (
"fmt"
2015-03-05 23:55:15 +01:00
"io"
2015-03-05 05:42:26 +01:00
"os"
2015-03-05 21:37:13 +01:00
"path/filepath"
2015-06-29 22:41:07 +02:00
"sort"
2015-03-05 05:42:26 +01:00
"strings"
2015-03-05 21:37:13 +01:00
"github.com/hashicorp/atlas-go/archive"
"github.com/hashicorp/atlas-go/v1"
2017-01-19 05:50:45 +01:00
"github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/terraform"
2015-03-05 05:42:26 +01:00
)
type PushCommand struct {
Meta
2015-03-05 23:55:15 +01:00
// client is the client to use for the actual push operations.
// If this isn't set, then the Atlas client is used. This should
// really only be set for testing reasons (and is hence not exported).
client pushClient
2015-03-05 05:42:26 +01:00
}
func (c *PushCommand) Run(args []string) int {
2015-03-25 01:45:19 +01:00
var atlasAddress, atlasToken string
2015-03-25 01:03:59 +01:00
var archiveVCS, moduleUpload bool
var name string
2015-06-29 22:57:58 +02:00
var overwrite []string
2015-04-08 00:34:06 +02:00
args = c.Meta.process(args, true)
2015-03-06 23:49:22 +01:00
cmdFlags := c.Meta.flagSet("push")
2015-03-25 01:45:19 +01:00
cmdFlags.StringVar(&atlasAddress, "atlas-address", "", "")
2015-03-05 05:42:26 +01:00
cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
cmdFlags.StringVar(&atlasToken, "token", "", "")
2015-03-25 01:42:40 +01:00
cmdFlags.BoolVar(&moduleUpload, "upload-modules", true, "")
cmdFlags.StringVar(&name, "name", "", "")
2015-03-25 01:03:59 +01:00
cmdFlags.BoolVar(&archiveVCS, "vcs", true, "")
2015-06-29 22:57:58 +02:00
cmdFlags.Var((*FlagStringSlice)(&overwrite), "overwrite", "")
2015-03-05 05:42:26 +01:00
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
return 1
}
// Make a map of the set values
2015-06-29 22:57:58 +02:00
overwriteMap := make(map[string]struct{}, len(overwrite))
for _, v := range overwrite {
overwriteMap[v] = struct{}{}
}
// This is a map of variables specifically from the CLI that we want to overwrite.
// We need this because there is a chance that the user is trying to modify
// a variable we don't see in our context, but which exists in this Terraform
// Enterprise workspace.
cliVars := make(map[string]string)
for k, v := range c.variables {
if _, ok := overwriteMap[k]; ok {
if val, ok := v.(string); ok {
cliVars[k] = val
} else {
c.Ui.Error(fmt.Sprintf("Error reading value for variable: %s", k))
return 1
}
}
}
2017-01-19 05:50:45 +01:00
// Get the path to the configuration depending on the args.
configPath, err := ModulePath(cmdFlags.Args())
2015-03-05 05:42:26 +01:00
if err != nil {
2017-01-19 05:50:45 +01:00
c.Ui.Error(err.Error())
2015-03-05 05:42:26 +01:00
return 1
}
2017-01-19 05:50:45 +01:00
// Check if the path is a plan
plan, err := c.Plan(configPath)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
if plan != nil {
c.Ui.Error(
"A plan file cannot be given as the path to the configuration.\n" +
"A path to a module (directory with configuration) must be given.")
2015-03-05 05:42:26 +01:00
return 1
}
2017-01-19 05:50:45 +01:00
// Load the module
mod, err := c.Module(configPath)
2015-03-05 05:42:26 +01:00
if err != nil {
2017-01-19 05:50:45 +01:00
c.Ui.Error(fmt.Sprintf("Failed to load root config module: %s", err))
2015-03-05 05:42:26 +01:00
return 1
}
2017-01-19 05:50:45 +01:00
if mod == nil {
c.Ui.Error(fmt.Sprintf(
"No configuration files found in the directory: %s\n\n"+
"This command requires configuration to run.",
configPath))
2015-03-05 05:42:26 +01:00
return 1
}
var conf *config.Config
if mod != nil {
conf = mod.Config()
}
2017-01-19 05:50:45 +01:00
// Load the backend
b, err := c.Backend(&BackendOpts{
Config: conf,
2015-03-05 05:42:26 +01:00
})
if err != nil {
2017-01-19 05:50:45 +01:00
c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
2015-03-05 05:42:26 +01:00
return 1
}
2017-01-19 05:50:45 +01:00
// We require a non-local backend
if c.IsLocalBackend(b) {
c.Ui.Error(
"A remote backend is not enabled. For Atlas to run Terraform\n" +
"for you, remote state must be used and configured. Remote \n" +
"state via any backend is accepted, not just Atlas. To configure\n" +
"a backend, please see the documentation at the URL below:\n\n" +
"https://www.terraform.io/docs/state/remote.html")
return 1
}
2017-01-19 05:50:45 +01:00
// We require a local backend
local, ok := b.(backend.Local)
if !ok {
c.Ui.Error(ErrUnsupportedLocalOp)
return 1
}
// Build the operation
opReq := c.Operation()
opReq.Module = mod
opReq.Plan = plan
// Get the context
ctx, _, err := local.Context(opReq)
if err != nil {
c.Ui.Error(err.Error())
2015-03-05 05:42:26 +01:00
return 1
}
// Get the configuration
config := ctx.Module().Config()
if name == "" {
if config.Atlas == nil || config.Atlas.Name == "" {
c.Ui.Error(
"The name of this Terraform configuration in Atlas must be\n" +
"specified within your configuration or the command-line. To\n" +
"set it on the command-line, use the `-name` parameter.")
return 1
}
name = config.Atlas.Name
}
// Initialize the client if it isn't given.
if c.client == nil {
// Make sure to nil out our client so our token isn't sitting around
defer func() { c.client = nil }()
// Initialize it to the default client, we set custom settings later
client := atlas.DefaultClient()
2015-03-25 01:45:19 +01:00
if atlasAddress != "" {
client, err = atlas.NewClient(atlasAddress)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing Atlas client: %s", err))
return 1
}
}
client.DefaultHeader.Set(terraform.VersionHeader, terraform.Version)
if atlasToken != "" {
client.Token = atlasToken
}
c.client = &atlasPushClient{Client: client}
}
// Get the variables we already have in atlas
2015-06-29 22:41:07 +02:00
atlasVars, err := c.client.Get(name)
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error looking up previously pushed configuration: %s", err))
return 1
}
// Set remote variables in the context if we don't have a value here. These
// don't have to be correct, it just prevents the Input walk from prompting
// the user for input.
ctxVars := ctx.Variables()
atlasVarSentry := "ATLAS_78AC153CA649EAA44815DAD6CBD4816D"
for k, _ := range atlasVars {
if _, ok := ctxVars[k]; !ok {
ctx.SetVariable(k, atlasVarSentry)
}
}
2015-03-05 23:55:15 +01:00
// Ask for input
if err := ctx.Input(c.InputMode()); err != nil {
c.Ui.Error(fmt.Sprintf(
"Error while asking for variable input:\n\n%s", err))
return 1
}
// Now that we've gone through the input walk, we can be sure we have all
// the variables we're going to get.
// We are going to keep these separate from the atlas variables until
// upload, so we can notify the user which local variables we're sending.
serializedVars, err := tfVars(ctx.Variables())
if err != nil {
c.Ui.Error(fmt.Sprintf(
"An error has occurred while serializing the variables for uploading:\n"+
"%s", err))
return 1
}
// Get the absolute path for our data directory, since the Extra field
// value below needs to be absolute.
dataDirAbs, err := filepath.Abs(c.DataDir())
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error while expanding the data directory %q: %s", c.DataDir(), err))
return 1
}
2015-03-05 21:37:13 +01:00
// Build the archiving options, which includes everything it can
// by default according to VCS rules but forcing the data directory.
archiveOpts := &archive.ArchiveOpts{
VCS: archiveVCS,
Extra: map[string]string{
DefaultDataDir: archive.ExtraEntryDir,
},
2015-03-05 21:37:13 +01:00
}
// Always store the state file in here so we can find state
statePathKey := fmt.Sprintf("%s/%s", DefaultDataDir, DefaultStateFilename)
archiveOpts.Extra[statePathKey] = filepath.Join(dataDirAbs, DefaultStateFilename)
if moduleUpload {
// If we're uploading modules, explicitly add that directory if exists.
moduleKey := fmt.Sprintf("%s/%s", DefaultDataDir, "modules")
moduleDir := filepath.Join(dataDirAbs, "modules")
_, err := os.Stat(moduleDir)
if err == nil {
archiveOpts.Extra[moduleKey] = filepath.Join(dataDirAbs, "modules")
}
if err != nil && !os.IsNotExist(err) {
c.Ui.Error(fmt.Sprintf(
"Error checking for module dir %q: %s", moduleDir, err))
return 1
}
} else {
// If we're not uploading modules, explicitly exclude add that
2015-03-05 21:37:13 +01:00
archiveOpts.Exclude = append(
archiveOpts.Exclude,
filepath.Join(c.DataDir(), "modules"))
}
2015-03-05 23:55:15 +01:00
archiveR, err := archive.CreateArchive(configPath, archiveOpts)
2015-03-05 21:37:13 +01:00
if err != nil {
c.Ui.Error(fmt.Sprintf(
"An error has occurred while archiving the module for uploading:\n"+
"%s", err))
return 1
}
// List of the vars we're uploading to display to the user.
// We always upload all vars from atlas, but only report them if they are overwritten.
2015-06-29 22:41:07 +02:00
var setVars []string
// variables to upload
var uploadVars []atlas.TFVar
// first add all the variables we want to send which have been serialized
// from the local context.
for _, sv := range serializedVars {
_, inOverwrite := overwriteMap[sv.Key]
_, inAtlas := atlasVars[sv.Key]
// We have a variable that's not in atlas, so always send it.
if !inAtlas {
uploadVars = append(uploadVars, sv)
setVars = append(setVars, sv.Key)
}
// We're overwriting an atlas variable.
// We also want to check that we
// don't send the dummy sentry value back to atlas. This could happen
// if it's specified as an overwrite on the cli, but we didn't set a
// new value.
if inAtlas && inOverwrite && sv.Value != atlasVarSentry {
uploadVars = append(uploadVars, sv)
setVars = append(setVars, sv.Key)
// remove this value from the atlas vars, because we're going to
// send back the remainder regardless.
delete(atlasVars, sv.Key)
2015-06-29 22:41:07 +02:00
}
}
2015-06-29 22:41:07 +02:00
// now send back all the existing atlas vars, inserting any overwrites from the cli.
for k, av := range atlasVars {
if v, ok := cliVars[k]; ok {
av.Value = v
setVars = append(setVars, k)
}
uploadVars = append(uploadVars, av)
2015-06-29 22:41:07 +02:00
}
2015-06-29 22:41:07 +02:00
sort.Strings(setVars)
if len(setVars) > 0 {
c.Ui.Output(
2015-06-29 22:58:54 +02:00
"The following variables will be set or overwritten within Atlas from\n" +
2015-06-29 22:41:07 +02:00
"their local values. All other variables are already set within Atlas.\n" +
"If you want to modify the value of a variable, use the Atlas web\n" +
"interface or set it locally and use the -overwrite flag.\n\n")
2015-06-29 22:41:07 +02:00
for _, v := range setVars {
c.Ui.Output(fmt.Sprintf(" * %s", v))
}
// Newline
c.Ui.Output("")
}
2015-03-05 23:55:15 +01:00
// Upsert!
opts := &pushUpsertOptions{
Name: name,
Archive: archiveR,
Variables: ctx.Variables(),
TFVars: uploadVars,
}
2015-06-29 22:41:07 +02:00
c.Ui.Output("Uploading Terraform configuration...")
vsn, err := c.client.Upsert(opts)
if err != nil {
2015-03-05 23:55:15 +01:00
c.Ui.Error(fmt.Sprintf(
"An error occurred while uploading the module:\n\n%s", err))
return 1
}
c.Ui.Output(c.Colorize().Color(fmt.Sprintf(
2015-03-25 01:41:26 +01:00
"[reset][bold][green]Configuration %q uploaded! (v%d)",
name, vsn)))
2015-03-05 05:42:26 +01:00
return 0
}
func (c *PushCommand) Help() string {
helpText := `
Usage: terraform push [options] [DIR]
Upload this Terraform module to an Atlas server for remote
infrastructure management.
Options:
2015-03-25 01:45:19 +01:00
-atlas-address=<url> An alternate address to an Atlas instance. Defaults
to https://atlas.hashicorp.com
2015-03-25 01:42:40 +01:00
-upload-modules=true If true (default), then the modules are locked at
2015-03-05 05:42:26 +01:00
their current checkout and uploaded completely. This
prevents Atlas from running "terraform get".
-name=<name> Name of the configuration in Atlas. This can also
be set in the configuration itself. Format is
typically: "username/name".
2015-03-25 01:41:26 +01:00
-token=<token> Access token to use to upload. If blank or unspecified,
the ATLAS_TOKEN environmental variable will be used.
2015-03-05 05:42:26 +01:00
-overwrite=foo Variable keys that should overwrite values in Atlas.
Otherwise, variables already set in Atlas will overwrite
local values. This flag can be repeated.
-var 'foo=bar' Set a variable in the Terraform configuration. This
flag can be set multiple times.
-var-file=foo Set variables in the Terraform configuration from
a file. If "terraform.tfvars" is present, it will be
automatically loaded if this flag is not specified.
2015-03-25 01:03:59 +01:00
-vcs=true If true (default), push will upload only files
2015-09-11 20:56:20 +02:00
committed to your VCS, if detected.
2015-03-25 01:03:59 +01:00
2015-06-22 14:14:01 +02:00
-no-color If specified, output won't contain any color.
2015-03-05 05:42:26 +01:00
`
return strings.TrimSpace(helpText)
}
func sortedKeys(m map[string]interface{}) []string {
var keys []string
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
// build the set of TFVars for push
func tfVars(vars map[string]interface{}) ([]atlas.TFVar, error) {
var tfVars []atlas.TFVar
var err error
RANGE:
for _, k := range sortedKeys(vars) {
v := vars[k]
var hcl []byte
tfv := atlas.TFVar{Key: k}
switch v := v.(type) {
case string:
tfv.Value = v
default:
// everything that's not a string is now HCL encoded
hcl, err = encodeHCL(v)
if err != nil {
break RANGE
}
tfv.Value = string(hcl)
tfv.IsHCL = true
}
tfVars = append(tfVars, tfv)
}
return tfVars, err
}
2015-03-05 05:42:26 +01:00
func (c *PushCommand) Synopsis() string {
return "Upload this Terraform module to Atlas to run"
}
2015-03-05 23:55:15 +01:00
// pushClient is implemented internally to control where pushes go. This is
// either to Atlas or a mock for testing. We still return a map to make it
// easier to check for variable existence when filtering the overrides.
2015-03-05 23:55:15 +01:00
type pushClient interface {
Get(string) (map[string]atlas.TFVar, error)
Upsert(*pushUpsertOptions) (int, error)
}
type pushUpsertOptions struct {
Name string
Archive *archive.Archive
Variables map[string]interface{}
TFVars []atlas.TFVar
2015-03-05 23:55:15 +01:00
}
type atlasPushClient struct {
Client *atlas.Client
}
func (c *atlasPushClient) Get(name string) (map[string]atlas.TFVar, error) {
user, name, err := atlas.ParseSlug(name)
if err != nil {
return nil, err
}
version, err := c.Client.TerraformConfigLatest(user, name)
if err != nil {
return nil, err
}
variables := make(map[string]atlas.TFVar)
if version == nil {
return variables, nil
}
// Variables is superseded by TFVars
if version.TFVars == nil {
for k, v := range version.Variables {
variables[k] = atlas.TFVar{Key: k, Value: v}
}
} else {
for _, v := range version.TFVars {
variables[v.Key] = v
}
}
return variables, nil
}
func (c *atlasPushClient) Upsert(opts *pushUpsertOptions) (int, error) {
user, name, err := atlas.ParseSlug(opts.Name)
if err != nil {
return 0, err
}
data := &atlas.TerraformConfigVersion{
TFVars: opts.TFVars,
}
version, err := c.Client.CreateTerraformConfigVersion(
user, name, data, opts.Archive, opts.Archive.Size)
if err != nil {
return 0, err
}
return version, nil
}
2015-03-05 23:55:15 +01:00
type mockPushClient struct {
File string
GetCalled bool
GetName string
GetResult map[string]atlas.TFVar
GetError error
UpsertCalled bool
UpsertOptions *pushUpsertOptions
UpsertVersion int
UpsertError error
2015-03-05 23:55:15 +01:00
}
func (c *mockPushClient) Get(name string) (map[string]atlas.TFVar, error) {
c.GetCalled = true
c.GetName = name
return c.GetResult, c.GetError
}
func (c *mockPushClient) Upsert(opts *pushUpsertOptions) (int, error) {
2015-03-05 23:55:15 +01:00
f, err := os.Create(c.File)
if err != nil {
return 0, err
2015-03-05 23:55:15 +01:00
}
defer f.Close()
data := opts.Archive
size := opts.Archive.Size
2015-03-05 23:55:15 +01:00
if _, err := io.CopyN(f, data, size); err != nil {
return 0, err
2015-03-05 23:55:15 +01:00
}
c.UpsertCalled = true
c.UpsertOptions = opts
return c.UpsertVersion, c.UpsertError
2015-03-05 23:55:15 +01:00
}