Merge branch 'master' of github.com:hashicorp/terraform
This commit is contained in:
commit
9249d91c5d
|
@ -3,6 +3,7 @@ package command
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -20,7 +21,7 @@ type ApplyCommand struct {
|
||||||
|
|
||||||
func (c *ApplyCommand) Run(args []string) int {
|
func (c *ApplyCommand) Run(args []string) int {
|
||||||
var refresh bool
|
var refresh bool
|
||||||
var statePath, stateOutPath string
|
var statePath, stateOutPath, backupPath string
|
||||||
|
|
||||||
args = c.Meta.process(args)
|
args = c.Meta.process(args)
|
||||||
|
|
||||||
|
@ -28,6 +29,7 @@ func (c *ApplyCommand) Run(args []string) int {
|
||||||
cmdFlags.BoolVar(&refresh, "refresh", true, "refresh")
|
cmdFlags.BoolVar(&refresh, "refresh", true, "refresh")
|
||||||
cmdFlags.StringVar(&statePath, "state", DefaultStateFilename, "path")
|
cmdFlags.StringVar(&statePath, "state", DefaultStateFilename, "path")
|
||||||
cmdFlags.StringVar(&stateOutPath, "state-out", "", "path")
|
cmdFlags.StringVar(&stateOutPath, "state-out", "", "path")
|
||||||
|
cmdFlags.StringVar(&backupPath, "backup", "", "path")
|
||||||
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
||||||
if err := cmdFlags.Parse(args); err != nil {
|
if err := cmdFlags.Parse(args); err != nil {
|
||||||
return 1
|
return 1
|
||||||
|
@ -59,6 +61,12 @@ func (c *ApplyCommand) Run(args []string) int {
|
||||||
stateOutPath = statePath
|
stateOutPath = statePath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we don't specify a backup path, default to state out with
|
||||||
|
// the extention
|
||||||
|
if backupPath == "" {
|
||||||
|
backupPath = stateOutPath + DefaultBackupExtention
|
||||||
|
}
|
||||||
|
|
||||||
// Build the context based on the arguments given
|
// Build the context based on the arguments given
|
||||||
ctx, planned, err := c.Context(configPath, statePath)
|
ctx, planned, err := c.Context(configPath, statePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -69,6 +77,20 @@ func (c *ApplyCommand) Run(args []string) int {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a backup of the state before updating
|
||||||
|
if backupPath != "-" && c.state != nil {
|
||||||
|
log.Printf("[INFO] Writing backup state to: %s", backupPath)
|
||||||
|
f, err := os.Create(backupPath)
|
||||||
|
if err == nil {
|
||||||
|
err = terraform.WriteState(c.state, f)
|
||||||
|
f.Close()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
c.Ui.Error(fmt.Sprintf("Error writing backup state file: %s", err))
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Plan if we haven't already
|
// Plan if we haven't already
|
||||||
if !planned {
|
if !planned {
|
||||||
if refresh {
|
if refresh {
|
||||||
|
@ -201,6 +223,10 @@ Usage: terraform apply [options] [dir]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
|
|
||||||
|
-backup=path Path to backup the existing state file before
|
||||||
|
modifying. Defaults to the "-state-out" path with
|
||||||
|
".backup" extention. Set to "-" to disable backup.
|
||||||
|
|
||||||
-no-color If specified, output won't contain any color.
|
-no-color If specified, output won't contain any color.
|
||||||
|
|
||||||
-refresh=true Update state prior to checking for differences. This
|
-refresh=true Update state prior to checking for differences. This
|
||||||
|
|
|
@ -358,6 +358,22 @@ func TestApply_refresh(t *testing.T) {
|
||||||
if state == nil {
|
if state == nil {
|
||||||
t.Fatal("state should not be nil")
|
t.Fatal("state should not be nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Should have a backup file
|
||||||
|
f, err = os.Open(statePath + DefaultBackupExtention)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
backupState, err := terraform.ReadState(f)
|
||||||
|
f.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(backupState, originalState) {
|
||||||
|
t.Fatalf("bad: %#v", backupState)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestApply_shutdown(t *testing.T) {
|
func TestApply_shutdown(t *testing.T) {
|
||||||
|
@ -517,6 +533,25 @@ func TestApply_state(t *testing.T) {
|
||||||
if state == nil {
|
if state == nil {
|
||||||
t.Fatal("state should not be nil")
|
t.Fatal("state should not be nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Should have a backup file
|
||||||
|
f, err = os.Open(statePath + DefaultBackupExtention)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
backupState, err := terraform.ReadState(f)
|
||||||
|
f.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// nil out the ConnInfo since that should not be restored
|
||||||
|
originalState.Resources["test_instance.foo"].ConnInfo = nil
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(backupState, originalState) {
|
||||||
|
t.Fatalf("bad: %#v", backupState)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestApply_stateNoExist(t *testing.T) {
|
func TestApply_stateNoExist(t *testing.T) {
|
||||||
|
@ -617,6 +652,160 @@ func TestApply_varFile(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestApply_backup(t *testing.T) {
|
||||||
|
originalState := &terraform.State{
|
||||||
|
Resources: map[string]*terraform.ResourceState{
|
||||||
|
"test_instance.foo": &terraform.ResourceState{
|
||||||
|
ID: "bar",
|
||||||
|
Type: "test_instance",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
statePath := testStateFile(t, originalState)
|
||||||
|
backupPath := testTempFile(t)
|
||||||
|
|
||||||
|
p := testProvider()
|
||||||
|
p.DiffReturn = &terraform.ResourceDiff{
|
||||||
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
||||||
|
"ami": &terraform.ResourceAttrDiff{
|
||||||
|
New: "bar",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
ui := new(cli.MockUi)
|
||||||
|
c := &ApplyCommand{
|
||||||
|
Meta: Meta{
|
||||||
|
ContextOpts: testCtxConfig(p),
|
||||||
|
Ui: ui,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run the apply command pointing to our existing state
|
||||||
|
args := []string{
|
||||||
|
"-state", statePath,
|
||||||
|
"-backup", backupPath,
|
||||||
|
testFixturePath("apply"),
|
||||||
|
}
|
||||||
|
if code := c.Run(args); code != 0 {
|
||||||
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify a new state exists
|
||||||
|
if _, err := os.Stat(statePath); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.Open(statePath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
state, err := terraform.ReadState(f)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
if state == nil {
|
||||||
|
t.Fatal("state should not be nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should have a backup file
|
||||||
|
f, err = os.Open(backupPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
backupState, err := terraform.ReadState(f)
|
||||||
|
f.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual := backupState.Resources["test_instance.foo"]
|
||||||
|
expected := originalState.Resources["test_instance.foo"]
|
||||||
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
|
t.Fatalf("bad: %#v %#v", actual, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestApply_disableBackup(t *testing.T) {
|
||||||
|
originalState := &terraform.State{
|
||||||
|
Resources: map[string]*terraform.ResourceState{
|
||||||
|
"test_instance.foo": &terraform.ResourceState{
|
||||||
|
ID: "bar",
|
||||||
|
Type: "test_instance",
|
||||||
|
ConnInfo: make(map[string]string),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
statePath := testStateFile(t, originalState)
|
||||||
|
|
||||||
|
p := testProvider()
|
||||||
|
p.DiffReturn = &terraform.ResourceDiff{
|
||||||
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
||||||
|
"ami": &terraform.ResourceAttrDiff{
|
||||||
|
New: "bar",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
ui := new(cli.MockUi)
|
||||||
|
c := &ApplyCommand{
|
||||||
|
Meta: Meta{
|
||||||
|
ContextOpts: testCtxConfig(p),
|
||||||
|
Ui: ui,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run the apply command pointing to our existing state
|
||||||
|
args := []string{
|
||||||
|
"-state", statePath,
|
||||||
|
"-backup", "-",
|
||||||
|
testFixturePath("apply"),
|
||||||
|
}
|
||||||
|
if code := c.Run(args); code != 0 {
|
||||||
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify that the provider was called with the existing state
|
||||||
|
expectedState := originalState.Resources["test_instance.foo"]
|
||||||
|
if !reflect.DeepEqual(p.DiffState, expectedState) {
|
||||||
|
t.Fatalf("bad: %#v", p.DiffState)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(p.ApplyState, expectedState) {
|
||||||
|
t.Fatalf("bad: %#v", p.ApplyState)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify a new state exists
|
||||||
|
if _, err := os.Stat(statePath); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.Open(statePath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
state, err := terraform.ReadState(f)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
if state == nil {
|
||||||
|
t.Fatal("state should not be nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure there is no backup
|
||||||
|
_, err = os.Stat(statePath + DefaultBackupExtention)
|
||||||
|
if err == nil || !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("backup should not exist")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const applyVarFile = `
|
const applyVarFile = `
|
||||||
foo = "bar"
|
foo = "bar"
|
||||||
`
|
`
|
||||||
|
|
|
@ -10,6 +10,9 @@ import (
|
||||||
// DefaultStateFilename is the default filename used for the state file.
|
// DefaultStateFilename is the default filename used for the state file.
|
||||||
const DefaultStateFilename = "terraform.tfstate"
|
const DefaultStateFilename = "terraform.tfstate"
|
||||||
|
|
||||||
|
// DefaultBackupExtention is added to the state file to form the path
|
||||||
|
const DefaultBackupExtention = ".backup"
|
||||||
|
|
||||||
func validateContext(ctx *terraform.Context, ui cli.Ui) bool {
|
func validateContext(ctx *terraform.Context, ui cli.Ui) bool {
|
||||||
if ws, es := ctx.Validate(); len(ws) > 0 || len(es) > 0 {
|
if ws, es := ctx.Validate(); len(ws) > 0 || len(es) > 0 {
|
||||||
ui.Output(
|
ui.Output(
|
||||||
|
|
|
@ -17,6 +17,10 @@ type Meta struct {
|
||||||
ContextOpts *terraform.ContextOpts
|
ContextOpts *terraform.ContextOpts
|
||||||
Ui cli.Ui
|
Ui cli.Ui
|
||||||
|
|
||||||
|
// State read when calling `Context`. This is available after calling
|
||||||
|
// `Context`.
|
||||||
|
state *terraform.State
|
||||||
|
|
||||||
// This can be set by the command itself to provide extra hooks.
|
// This can be set by the command itself to provide extra hooks.
|
||||||
extraHooks []terraform.Hook
|
extraHooks []terraform.Hook
|
||||||
|
|
||||||
|
@ -77,6 +81,9 @@ func (m *Meta) Context(path, statePath string) (*terraform.Context, bool, error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Store the loaded state
|
||||||
|
m.state = state
|
||||||
|
|
||||||
config, err := config.LoadDir(path)
|
config, err := config.LoadDir(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, fmt.Errorf("Error loading config: %s", err)
|
return nil, false, fmt.Errorf("Error loading config: %s", err)
|
||||||
|
@ -89,7 +96,6 @@ func (m *Meta) Context(path, statePath string) (*terraform.Context, bool, error)
|
||||||
opts.State = state
|
opts.State = state
|
||||||
ctx := terraform.NewContext(opts)
|
ctx := terraform.NewContext(opts)
|
||||||
return ctx, false, nil
|
return ctx, false, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// contextOpts returns the options to use to initialize a Terraform
|
// contextOpts returns the options to use to initialize a Terraform
|
||||||
|
|
|
@ -17,7 +17,7 @@ type PlanCommand struct {
|
||||||
|
|
||||||
func (c *PlanCommand) Run(args []string) int {
|
func (c *PlanCommand) Run(args []string) int {
|
||||||
var destroy, refresh bool
|
var destroy, refresh bool
|
||||||
var outPath, statePath string
|
var outPath, statePath, backupPath string
|
||||||
|
|
||||||
args = c.Meta.process(args)
|
args = c.Meta.process(args)
|
||||||
|
|
||||||
|
@ -26,6 +26,7 @@ func (c *PlanCommand) Run(args []string) int {
|
||||||
cmdFlags.BoolVar(&refresh, "refresh", true, "refresh")
|
cmdFlags.BoolVar(&refresh, "refresh", true, "refresh")
|
||||||
cmdFlags.StringVar(&outPath, "out", "", "path")
|
cmdFlags.StringVar(&outPath, "out", "", "path")
|
||||||
cmdFlags.StringVar(&statePath, "state", DefaultStateFilename, "path")
|
cmdFlags.StringVar(&statePath, "state", DefaultStateFilename, "path")
|
||||||
|
cmdFlags.StringVar(&backupPath, "backup", "", "path")
|
||||||
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
||||||
if err := cmdFlags.Parse(args); err != nil {
|
if err := cmdFlags.Parse(args); err != nil {
|
||||||
return 1
|
return 1
|
||||||
|
@ -58,6 +59,12 @@ func (c *PlanCommand) Run(args []string) int {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we don't specify a backup path, default to state out with
|
||||||
|
// the extention
|
||||||
|
if backupPath == "" {
|
||||||
|
backupPath = statePath + DefaultBackupExtention
|
||||||
|
}
|
||||||
|
|
||||||
ctx, _, err := c.Context(path, statePath)
|
ctx, _, err := c.Context(path, statePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Ui.Error(err.Error())
|
c.Ui.Error(err.Error())
|
||||||
|
@ -68,6 +75,20 @@ func (c *PlanCommand) Run(args []string) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
if refresh {
|
if refresh {
|
||||||
|
// Create a backup of the state before updating
|
||||||
|
if backupPath != "-" && c.state != nil {
|
||||||
|
log.Printf("[INFO] Writing backup state to: %s", backupPath)
|
||||||
|
f, err := os.Create(backupPath)
|
||||||
|
if err == nil {
|
||||||
|
err = terraform.WriteState(c.state, f)
|
||||||
|
f.Close()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
c.Ui.Error(fmt.Sprintf("Error writing backup state file: %s", err))
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
c.Ui.Output("Refreshing Terraform state prior to plan...\n")
|
c.Ui.Output("Refreshing Terraform state prior to plan...\n")
|
||||||
if _, err := ctx.Refresh(); err != nil {
|
if _, err := ctx.Refresh(); err != nil {
|
||||||
c.Ui.Error(fmt.Sprintf("Error refreshing state: %s", err))
|
c.Ui.Error(fmt.Sprintf("Error refreshing state: %s", err))
|
||||||
|
@ -130,6 +151,10 @@ Usage: terraform plan [options] [dir]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
|
|
||||||
|
-backup=path Path to backup the existing state file before
|
||||||
|
modifying. Defaults to the "-state-out" path with
|
||||||
|
".backup" extention. Set to "-" to disable backup.
|
||||||
|
|
||||||
-destroy If set, a plan will be generated to destroy all resources
|
-destroy If set, a plan will be generated to destroy all resources
|
||||||
managed by the given configuration and state.
|
managed by the given configuration and state.
|
||||||
|
|
||||||
|
|
|
@ -78,6 +78,21 @@ func TestPlan_destroy(t *testing.T) {
|
||||||
t.Fatalf("bad: %#v", r)
|
t.Fatalf("bad: %#v", r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
f, err := os.Open(statePath + DefaultBackupExtention)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
backupState, err := terraform.ReadState(f)
|
||||||
|
f.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(backupState, originalState) {
|
||||||
|
t.Fatalf("bad: %#v", backupState)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func TestPlan_noState(t *testing.T) {
|
func TestPlan_noState(t *testing.T) {
|
||||||
p := testProvider()
|
p := testProvider()
|
||||||
|
@ -355,6 +370,136 @@ func TestPlan_varFile(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPlan_backup(t *testing.T) {
|
||||||
|
// Write out some prior state
|
||||||
|
tf, err := ioutil.TempFile("", "tf")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
statePath := tf.Name()
|
||||||
|
defer os.Remove(tf.Name())
|
||||||
|
|
||||||
|
// Write out some prior state
|
||||||
|
backupf, err := ioutil.TempFile("", "tf")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
backupPath := backupf.Name()
|
||||||
|
backupf.Close()
|
||||||
|
os.Remove(backupPath)
|
||||||
|
defer os.Remove(backupPath)
|
||||||
|
|
||||||
|
originalState := &terraform.State{
|
||||||
|
Resources: map[string]*terraform.ResourceState{
|
||||||
|
"test_instance.foo": &terraform.ResourceState{
|
||||||
|
ID: "bar",
|
||||||
|
Type: "test_instance",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err = terraform.WriteState(originalState, tf)
|
||||||
|
tf.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
p := testProvider()
|
||||||
|
ui := new(cli.MockUi)
|
||||||
|
c := &PlanCommand{
|
||||||
|
Meta: Meta{
|
||||||
|
ContextOpts: testCtxConfig(p),
|
||||||
|
Ui: ui,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
args := []string{
|
||||||
|
"-state", statePath,
|
||||||
|
"-backup", backupPath,
|
||||||
|
testFixturePath("plan"),
|
||||||
|
}
|
||||||
|
if code := c.Run(args); code != 0 {
|
||||||
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify that the provider was called with the existing state
|
||||||
|
expectedState := originalState.Resources["test_instance.foo"]
|
||||||
|
if !reflect.DeepEqual(p.DiffState, expectedState) {
|
||||||
|
t.Fatalf("bad: %#v", p.DiffState)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify the backup exist
|
||||||
|
f, err := os.Open(backupPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
backupState, err := terraform.ReadState(f)
|
||||||
|
f.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(backupState, originalState) {
|
||||||
|
t.Fatalf("bad: %#v", backupState)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPlan_disableBackup(t *testing.T) {
|
||||||
|
// Write out some prior state
|
||||||
|
tf, err := ioutil.TempFile("", "tf")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
statePath := tf.Name()
|
||||||
|
defer os.Remove(tf.Name())
|
||||||
|
|
||||||
|
originalState := &terraform.State{
|
||||||
|
Resources: map[string]*terraform.ResourceState{
|
||||||
|
"test_instance.foo": &terraform.ResourceState{
|
||||||
|
ID: "bar",
|
||||||
|
Type: "test_instance",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err = terraform.WriteState(originalState, tf)
|
||||||
|
tf.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
p := testProvider()
|
||||||
|
ui := new(cli.MockUi)
|
||||||
|
c := &PlanCommand{
|
||||||
|
Meta: Meta{
|
||||||
|
ContextOpts: testCtxConfig(p),
|
||||||
|
Ui: ui,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
args := []string{
|
||||||
|
"-state", statePath,
|
||||||
|
"-backup", "-",
|
||||||
|
testFixturePath("plan"),
|
||||||
|
}
|
||||||
|
if code := c.Run(args); code != 0 {
|
||||||
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify that the provider was called with the existing state
|
||||||
|
expectedState := originalState.Resources["test_instance.foo"]
|
||||||
|
if !reflect.DeepEqual(p.DiffState, expectedState) {
|
||||||
|
t.Fatalf("bad: %#v", p.DiffState)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure there is no backup
|
||||||
|
_, err = os.Stat(statePath + DefaultBackupExtention)
|
||||||
|
if err == nil || !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("backup should not exist")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const planVarFile = `
|
const planVarFile = `
|
||||||
foo = "bar"
|
foo = "bar"
|
||||||
`
|
`
|
||||||
|
|
|
@ -16,13 +16,14 @@ type RefreshCommand struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *RefreshCommand) Run(args []string) int {
|
func (c *RefreshCommand) Run(args []string) int {
|
||||||
var statePath, stateOutPath string
|
var statePath, stateOutPath, backupPath string
|
||||||
|
|
||||||
args = c.Meta.process(args)
|
args = c.Meta.process(args)
|
||||||
|
|
||||||
cmdFlags := c.Meta.flagSet("refresh")
|
cmdFlags := c.Meta.flagSet("refresh")
|
||||||
cmdFlags.StringVar(&statePath, "state", DefaultStateFilename, "path")
|
cmdFlags.StringVar(&statePath, "state", DefaultStateFilename, "path")
|
||||||
cmdFlags.StringVar(&stateOutPath, "state-out", "", "path")
|
cmdFlags.StringVar(&stateOutPath, "state-out", "", "path")
|
||||||
|
cmdFlags.StringVar(&backupPath, "backup", "", "path")
|
||||||
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
||||||
if err := cmdFlags.Parse(args); err != nil {
|
if err := cmdFlags.Parse(args); err != nil {
|
||||||
return 1
|
return 1
|
||||||
|
@ -50,6 +51,12 @@ func (c *RefreshCommand) Run(args []string) int {
|
||||||
stateOutPath = statePath
|
stateOutPath = statePath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we don't specify a backup path, default to state out with
|
||||||
|
// the extention
|
||||||
|
if backupPath == "" {
|
||||||
|
backupPath = stateOutPath + DefaultBackupExtention
|
||||||
|
}
|
||||||
|
|
||||||
// Verify that the state path exists. The "ContextArg" function below
|
// Verify that the state path exists. The "ContextArg" function below
|
||||||
// will actually do this, but we want to provide a richer error message
|
// will actually do this, but we want to provide a richer error message
|
||||||
// if possible.
|
// if possible.
|
||||||
|
@ -86,6 +93,20 @@ func (c *RefreshCommand) Run(args []string) int {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a backup of the state before updating
|
||||||
|
if backupPath != "-" && c.state != nil {
|
||||||
|
log.Printf("[INFO] Writing backup state to: %s", backupPath)
|
||||||
|
f, err := os.Create(backupPath)
|
||||||
|
if err == nil {
|
||||||
|
err = terraform.WriteState(c.state, f)
|
||||||
|
f.Close()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
c.Ui.Error(fmt.Sprintf("Error writing backup state file: %s", err))
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
state, err := ctx.Refresh()
|
state, err := ctx.Refresh()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Ui.Error(fmt.Sprintf("Error refreshing state: %s", err))
|
c.Ui.Error(fmt.Sprintf("Error refreshing state: %s", err))
|
||||||
|
@ -119,6 +140,10 @@ Usage: terraform refresh [options] [dir]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
|
|
||||||
|
-backup=path Path to backup the existing state file before
|
||||||
|
modifying. Defaults to the "-state-out" path with
|
||||||
|
".backup" extention. Set to "-" to disable backup.
|
||||||
|
|
||||||
-no-color If specified, output won't contain any color.
|
-no-color If specified, output won't contain any color.
|
||||||
|
|
||||||
-state=path Path to read and save state (unless state-out
|
-state=path Path to read and save state (unless state-out
|
||||||
|
|
|
@ -221,6 +221,23 @@ func TestRefresh_defaultState(t *testing.T) {
|
||||||
if !reflect.DeepEqual(actual, expected) {
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
t.Fatalf("bad: %#v", actual)
|
t.Fatalf("bad: %#v", actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
f, err = os.Open(statePath + DefaultBackupExtention)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
backupState, err := terraform.ReadState(f)
|
||||||
|
f.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual = backupState.Resources["test_instance.foo"]
|
||||||
|
expected = originalState.Resources["test_instance.foo"]
|
||||||
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
|
t.Fatalf("bad: %#v", actual)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRefresh_outPath(t *testing.T) {
|
func TestRefresh_outPath(t *testing.T) {
|
||||||
|
@ -295,6 +312,21 @@ func TestRefresh_outPath(t *testing.T) {
|
||||||
if !reflect.DeepEqual(actual, expected) {
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
t.Fatalf("bad: %#v", actual)
|
t.Fatalf("bad: %#v", actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
f, err = os.Open(outPath + DefaultBackupExtention)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
backupState, err := terraform.ReadState(f)
|
||||||
|
f.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(backupState, state) {
|
||||||
|
t.Fatalf("bad: %#v", backupState)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRefresh_var(t *testing.T) {
|
func TestRefresh_var(t *testing.T) {
|
||||||
|
@ -376,6 +408,186 @@ func TestRefresh_varFile(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRefresh_backup(t *testing.T) {
|
||||||
|
state := &terraform.State{
|
||||||
|
Resources: map[string]*terraform.ResourceState{
|
||||||
|
"test_instance.foo": &terraform.ResourceState{
|
||||||
|
ID: "bar",
|
||||||
|
Type: "test_instance",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
statePath := testStateFile(t, state)
|
||||||
|
|
||||||
|
// Output path
|
||||||
|
outf, err := ioutil.TempFile("", "tf")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
outPath := outf.Name()
|
||||||
|
outf.Close()
|
||||||
|
os.Remove(outPath)
|
||||||
|
|
||||||
|
// Backup path
|
||||||
|
backupf, err := ioutil.TempFile("", "tf")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
backupPath := backupf.Name()
|
||||||
|
backupf.Close()
|
||||||
|
os.Remove(backupPath)
|
||||||
|
|
||||||
|
p := testProvider()
|
||||||
|
ui := new(cli.MockUi)
|
||||||
|
c := &RefreshCommand{
|
||||||
|
Meta: Meta{
|
||||||
|
ContextOpts: testCtxConfig(p),
|
||||||
|
Ui: ui,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
p.RefreshFn = nil
|
||||||
|
p.RefreshReturn = &terraform.ResourceState{ID: "yes"}
|
||||||
|
|
||||||
|
args := []string{
|
||||||
|
"-state", statePath,
|
||||||
|
"-state-out", outPath,
|
||||||
|
"-backup", backupPath,
|
||||||
|
testFixturePath("refresh"),
|
||||||
|
}
|
||||||
|
if code := c.Run(args); code != 0 {
|
||||||
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.Open(statePath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
newState, err := terraform.ReadState(f)
|
||||||
|
f.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(newState, state) {
|
||||||
|
t.Fatalf("bad: %#v", newState)
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err = os.Open(outPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
newState, err = terraform.ReadState(f)
|
||||||
|
f.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual := newState.Resources["test_instance.foo"]
|
||||||
|
expected := p.RefreshReturn
|
||||||
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
|
t.Fatalf("bad: %#v", actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err = os.Open(backupPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
backupState, err := terraform.ReadState(f)
|
||||||
|
f.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(backupState, state) {
|
||||||
|
t.Fatalf("bad: %#v", backupState)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRefresh_disableBackup(t *testing.T) {
|
||||||
|
state := &terraform.State{
|
||||||
|
Resources: map[string]*terraform.ResourceState{
|
||||||
|
"test_instance.foo": &terraform.ResourceState{
|
||||||
|
ID: "bar",
|
||||||
|
Type: "test_instance",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
statePath := testStateFile(t, state)
|
||||||
|
|
||||||
|
// Output path
|
||||||
|
outf, err := ioutil.TempFile("", "tf")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
outPath := outf.Name()
|
||||||
|
outf.Close()
|
||||||
|
os.Remove(outPath)
|
||||||
|
|
||||||
|
p := testProvider()
|
||||||
|
ui := new(cli.MockUi)
|
||||||
|
c := &RefreshCommand{
|
||||||
|
Meta: Meta{
|
||||||
|
ContextOpts: testCtxConfig(p),
|
||||||
|
Ui: ui,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
p.RefreshFn = nil
|
||||||
|
p.RefreshReturn = &terraform.ResourceState{ID: "yes"}
|
||||||
|
|
||||||
|
args := []string{
|
||||||
|
"-state", statePath,
|
||||||
|
"-state-out", outPath,
|
||||||
|
"-backup", "-",
|
||||||
|
testFixturePath("refresh"),
|
||||||
|
}
|
||||||
|
if code := c.Run(args); code != 0 {
|
||||||
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.Open(statePath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
newState, err := terraform.ReadState(f)
|
||||||
|
f.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(newState, state) {
|
||||||
|
t.Fatalf("bad: %#v", newState)
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err = os.Open(outPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
newState, err = terraform.ReadState(f)
|
||||||
|
f.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual := newState.Resources["test_instance.foo"]
|
||||||
|
expected := p.RefreshReturn
|
||||||
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
|
t.Fatalf("bad: %#v", actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure there is no backup
|
||||||
|
_, err = os.Stat(outPath + DefaultBackupExtention)
|
||||||
|
if err == nil || !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("backup should not exist")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const refreshVarFile = `
|
const refreshVarFile = `
|
||||||
foo = "bar"
|
foo = "bar"
|
||||||
`
|
`
|
||||||
|
|
|
@ -21,6 +21,9 @@ execute a pre-determined set of actions.
|
||||||
|
|
||||||
The command-line flags are all optional. The list of available flags are:
|
The command-line flags are all optional. The list of available flags are:
|
||||||
|
|
||||||
|
* `-backup=path` - Path to the backup file. Defaults to `-state-out` with
|
||||||
|
the ".backup" extention. Disabled by setting to "-".
|
||||||
|
|
||||||
* `-no-color` - Disables output with coloring.
|
* `-no-color` - Disables output with coloring.
|
||||||
|
|
||||||
* `-refresh=true` - Update the state for each resource prior to planning
|
* `-refresh=true` - Update the state for each resource prior to planning
|
||||||
|
|
|
@ -21,6 +21,9 @@ for the configuration and state file to refresh.
|
||||||
|
|
||||||
The command-line flags are all optional. The list of available flags are:
|
The command-line flags are all optional. The list of available flags are:
|
||||||
|
|
||||||
|
* `-backup=path` - Path to the backup file. Defaults to `-state-out` with
|
||||||
|
the ".backup" extention. Disabled by setting to "-".
|
||||||
|
|
||||||
* `-destroy` - If set, generates a plan to destroy all the known resources.
|
* `-destroy` - If set, generates a plan to destroy all the known resources.
|
||||||
|
|
||||||
* `-no-color` - Disables output with coloring.
|
* `-no-color` - Disables output with coloring.
|
||||||
|
|
|
@ -24,6 +24,9 @@ for the configuration and state file to refresh.
|
||||||
|
|
||||||
The command-line flags are all optional. The list of available flags are:
|
The command-line flags are all optional. The list of available flags are:
|
||||||
|
|
||||||
|
* `-backup=path` - Path to the backup file. Defaults to `-state-out` with
|
||||||
|
the ".backup" extention. Disabled by setting to "-".
|
||||||
|
|
||||||
* `-no-color` - Disables output with coloring
|
* `-no-color` - Disables output with coloring
|
||||||
|
|
||||||
* `-state=path` - Path to read and write the state file to. Defaults to "terraform.tfstate".
|
* `-state=path` - Path to read and write the state file to. Defaults to "terraform.tfstate".
|
||||||
|
|
|
@ -6,6 +6,7 @@ Engine.Typewriter = function(element){
|
||||||
this.element = element;
|
this.element = element;
|
||||||
this.content = this.element.textContent.split('');
|
this.content = this.element.textContent.split('');
|
||||||
this.element.innerHTML = '';
|
this.element.innerHTML = '';
|
||||||
|
this.element.style.visibility = 'visible';
|
||||||
};
|
};
|
||||||
|
|
||||||
Engine.Typewriter.prototype = {
|
Engine.Typewriter.prototype = {
|
||||||
|
|
|
@ -93,12 +93,9 @@ Engine = Base.extend({
|
||||||
.then(function(){
|
.then(function(){
|
||||||
this.showShapes = true;
|
this.showShapes = true;
|
||||||
}, this)
|
}, this)
|
||||||
.wait(800)
|
.wait(1000)
|
||||||
.then(function(){
|
.then(function(){
|
||||||
this.logo.startBreathing();
|
this.logo.startBreathing();
|
||||||
}, this)
|
|
||||||
.wait(200)
|
|
||||||
.then(function(){
|
|
||||||
this.showGrid = true;
|
this.showGrid = true;
|
||||||
}, this)
|
}, this)
|
||||||
.wait(1000)
|
.wait(1000)
|
||||||
|
|
|
@ -66,10 +66,9 @@ var Init = {
|
||||||
|
|
||||||
Pages: {
|
Pages: {
|
||||||
'page-home': function(){
|
'page-home': function(){
|
||||||
var jumbotron;
|
|
||||||
if (isIE) {
|
if (isIE) {
|
||||||
jumbotron = document.getElementById('jumbotron');
|
document.getElementById('jumbotron').className += ' static';
|
||||||
jumbotron.className += ' static';
|
document.getElementById('tag-line').style.visibility = 'visible';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -121,6 +121,7 @@
|
||||||
top:540px;
|
top:540px;
|
||||||
color:#fff;
|
color:#fff;
|
||||||
text-align:center;
|
text-align:center;
|
||||||
|
visibility:hidden;
|
||||||
|
|
||||||
font-family: 'Lato', "Helvetica Neue", Helvetica, Arial, sans-serif;
|
font-family: 'Lato', "Helvetica Neue", Helvetica, Arial, sans-serif;
|
||||||
font-weight: 200;
|
font-weight: 200;
|
||||||
|
|
|
@ -686,6 +686,7 @@ body.page-home #footer {
|
||||||
top: 540px;
|
top: 540px;
|
||||||
color: #fff;
|
color: #fff;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
|
visibility: hidden;
|
||||||
font-family: 'Lato', "Helvetica Neue", Helvetica, Arial, sans-serif;
|
font-family: 'Lato', "Helvetica Neue", Helvetica, Arial, sans-serif;
|
||||||
font-weight: 200;
|
font-weight: 200;
|
||||||
-webkit-transform: translate3d(0, 0, 0);
|
-webkit-transform: translate3d(0, 0, 0);
|
||||||
|
|
Loading…
Reference in New Issue