Merge pull request #42 from hashicorp/f-backup

command: Adding backup of state file
This commit is contained in:
Mitchell Hashimoto 2014-07-27 22:55:24 -07:00
commit 21370b8e23
11 changed files with 641 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package command
import (
"bytes"
"fmt"
"log"
"os"
"sort"
"strings"
@ -20,7 +21,7 @@ type ApplyCommand struct {
func (c *ApplyCommand) Run(args []string) int {
var refresh bool
var statePath, stateOutPath string
var statePath, stateOutPath, backupPath string
args = c.Meta.process(args)
@ -28,6 +29,7 @@ func (c *ApplyCommand) Run(args []string) int {
cmdFlags.BoolVar(&refresh, "refresh", true, "refresh")
cmdFlags.StringVar(&statePath, "state", DefaultStateFilename, "path")
cmdFlags.StringVar(&stateOutPath, "state-out", "", "path")
cmdFlags.StringVar(&backupPath, "backup", "", "path")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
return 1
@ -59,6 +61,12 @@ func (c *ApplyCommand) Run(args []string) int {
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
ctx, planned, err := c.Context(configPath, statePath)
if err != nil {
@ -69,6 +77,20 @@ func (c *ApplyCommand) Run(args []string) int {
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 {
defer f.Close()
err = terraform.WriteState(c.State, f)
}
if err != nil {
c.Ui.Error(fmt.Sprintf("Error writing backup state file: %s", err))
return 1
}
}
// Plan if we haven't already
if !planned {
if refresh {
@ -201,6 +223,10 @@ Usage: terraform apply [options] [dir]
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.
-refresh=true Update state prior to checking for differences. This

View File

@ -358,6 +358,22 @@ func TestApply_refresh(t *testing.T) {
if state == 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) {
@ -517,6 +533,25 @@ func TestApply_state(t *testing.T) {
if state == 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) {
@ -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 = `
foo = "bar"
`

View File

@ -10,6 +10,9 @@ import (
// DefaultStateFilename is the default filename used for the state file.
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 {
if ws, es := ctx.Validate(); len(ws) > 0 || len(es) > 0 {
ui.Output(

View File

@ -16,6 +16,7 @@ type Meta struct {
Color bool
ContextOpts *terraform.ContextOpts
Ui cli.Ui
State *terraform.State
// This can be set by the command itself to provide extra hooks.
extraHooks []terraform.Hook
@ -77,6 +78,9 @@ func (m *Meta) Context(path, statePath string) (*terraform.Context, bool, error)
}
}
// Store the loaded state
m.State = state
config, err := config.LoadDir(path)
if err != nil {
return nil, false, fmt.Errorf("Error loading config: %s", err)

View File

@ -17,7 +17,7 @@ type PlanCommand struct {
func (c *PlanCommand) Run(args []string) int {
var destroy, refresh bool
var outPath, statePath string
var outPath, statePath, backupPath string
args = c.Meta.process(args)
@ -26,6 +26,7 @@ func (c *PlanCommand) Run(args []string) int {
cmdFlags.BoolVar(&refresh, "refresh", true, "refresh")
cmdFlags.StringVar(&outPath, "out", "", "path")
cmdFlags.StringVar(&statePath, "state", DefaultStateFilename, "path")
cmdFlags.StringVar(&backupPath, "backup", "", "path")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
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)
if err != nil {
c.Ui.Error(err.Error())
@ -68,6 +75,20 @@ func (c *PlanCommand) Run(args []string) int {
}
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 {
defer f.Close()
err = terraform.WriteState(c.State, f)
}
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")
if _, err := ctx.Refresh(); err != nil {
c.Ui.Error(fmt.Sprintf("Error refreshing state: %s", err))
@ -130,6 +151,10 @@ Usage: terraform plan [options] [dir]
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
managed by the given configuration and state.

View File

@ -78,6 +78,21 @@ func TestPlan_destroy(t *testing.T) {
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) {
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 = `
foo = "bar"
`

View File

@ -16,13 +16,14 @@ type RefreshCommand struct {
}
func (c *RefreshCommand) Run(args []string) int {
var statePath, stateOutPath string
var statePath, stateOutPath, backupPath string
args = c.Meta.process(args)
cmdFlags := c.Meta.flagSet("refresh")
cmdFlags.StringVar(&statePath, "state", DefaultStateFilename, "path")
cmdFlags.StringVar(&stateOutPath, "state-out", "", "path")
cmdFlags.StringVar(&backupPath, "backup", "", "path")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
return 1
@ -50,6 +51,12 @@ func (c *RefreshCommand) Run(args []string) int {
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
// will actually do this, but we want to provide a richer error message
// if possible.
@ -86,6 +93,20 @@ func (c *RefreshCommand) Run(args []string) int {
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 {
defer f.Close()
err = terraform.WriteState(c.State, f)
}
if err != nil {
c.Ui.Error(fmt.Sprintf("Error writing backup state file: %s", err))
return 1
}
}
state, err := ctx.Refresh()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error refreshing state: %s", err))
@ -119,6 +140,10 @@ Usage: terraform refresh [options] [dir]
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.
-state=path Path to read and save state (unless state-out

View File

@ -221,6 +221,23 @@ func TestRefresh_defaultState(t *testing.T) {
if !reflect.DeepEqual(actual, expected) {
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) {
@ -295,6 +312,21 @@ func TestRefresh_outPath(t *testing.T) {
if !reflect.DeepEqual(actual, expected) {
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) {
@ -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 = `
foo = "bar"
`

View File

@ -21,6 +21,9 @@ execute a pre-determined set of actions.
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.
* `-refresh=true` - Update the state for each resource prior to planning

View File

@ -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:
* `-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.
* `-no-color` - Disables output with coloring.

View File

@ -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:
* `-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
* `-state=path` - Path to read and write the state file to. Defaults to "terraform.tfstate".