From 29b3310a49513e71e648d833783965a614720a48 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Tue, 30 Sep 2014 16:05:40 -0700 Subject: [PATCH] command/init: integrate remote state storage --- command/init.go | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/command/init.go b/command/init.go index 2a9c6f45d..b10d9b42d 100644 --- a/command/init.go +++ b/command/init.go @@ -8,6 +8,8 @@ import ( "github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/config/module" + "github.com/hashicorp/terraform/remote" + "github.com/hashicorp/terraform/terraform" ) // InitCommand is a Command implementation that takes a Terraform @@ -17,9 +19,12 @@ type InitCommand struct { } func (c *InitCommand) Run(args []string) int { + var remoteConf terraform.RemoteState args = c.Meta.process(args, false) - cmdFlags := flag.NewFlagSet("init", flag.ContinueOnError) + cmdFlags.StringVar(&remoteConf.Name, "remote", "", "") + cmdFlags.StringVar(&remoteConf.Server, "remote-server", "", "") + cmdFlags.StringVar(&remoteConf.AuthToken, "remote-auth", "", "") cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } if err := cmdFlags.Parse(args); err != nil { return 1 @@ -47,6 +52,14 @@ func (c *InitCommand) Run(args []string) int { } } + // Validate the remote configuration + if !remoteConf.Empty() { + if err := remote.ValidateConfig(&remoteConf); err != nil { + c.Ui.Error(fmt.Sprintf("%s", err)) + return 1 + } + } + source := args[0] // Get our pwd since we need it @@ -84,6 +97,23 @@ func (c *InitCommand) Run(args []string) int { return 1 } + // Handle remote state if configured + if !remoteConf.Empty() { + // Read the updated state file + remoteR, err := remote.ReadState(&remoteConf) + if err != nil { + c.Ui.Error(fmt.Sprintf( + "Failed to read remote state: %v", err)) + return 1 + } + + // Persist the remote state + if err := remote.Persist(remoteR); err != nil { + c.Ui.Error(fmt.Sprintf( + "Failed to persist state: %v", err)) + return 1 + } + } return 0 } @@ -99,6 +129,16 @@ Usage: terraform init [options] SOURCE [PATH] Git, it will not preserve the Git history, it will only copy the latest files. +Options: + + -remote=name Name of the state file in the state storage server. + Optional, default does not use remote storage. + + -remote-auth=token Authentication token for state storage server. + Optional, defaults to blank. + + -remote-server=url URL of the remote storage server. + ` return strings.TrimSpace(helpText) }