parent
79033f240f
commit
82e7d58250
|
@ -19,13 +19,11 @@ type ApplyCommand struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ApplyCommand) Run(args []string) int {
|
func (c *ApplyCommand) Run(args []string) int {
|
||||||
var init bool
|
|
||||||
var statePath, stateOutPath string
|
var statePath, stateOutPath string
|
||||||
|
|
||||||
args = c.Meta.process(args)
|
args = c.Meta.process(args)
|
||||||
|
|
||||||
cmdFlags := c.Meta.flagSet("apply")
|
cmdFlags := c.Meta.flagSet("apply")
|
||||||
cmdFlags.BoolVar(&init, "init", false, "init")
|
|
||||||
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.Usage = func() { c.Ui.Error(c.Help()) }
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
||||||
|
@ -59,15 +57,8 @@ func (c *ApplyCommand) Run(args []string) int {
|
||||||
stateOutPath = statePath
|
stateOutPath = statePath
|
||||||
}
|
}
|
||||||
|
|
||||||
// The state path to use to generate a plan. If we're initializing
|
|
||||||
// a new infrastructure, then we don't use a state path.
|
|
||||||
planStatePath := statePath
|
|
||||||
if init {
|
|
||||||
planStatePath = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build the context based on the arguments given
|
// Build the context based on the arguments given
|
||||||
ctx, err := c.Context(configPath, planStatePath, true)
|
ctx, err := c.Context(configPath, statePath, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Ui.Error(err.Error())
|
c.Ui.Error(err.Error())
|
||||||
return 1
|
return 1
|
||||||
|
@ -192,11 +183,6 @@ Usage: terraform apply [options] [dir]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
|
|
||||||
-init If specified, new infrastructure can be built (no
|
|
||||||
previous state). This is just a safety switch
|
|
||||||
to prevent accidentally spinning up a new
|
|
||||||
infrastructure.
|
|
||||||
|
|
||||||
-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
|
||||||
|
|
|
@ -28,7 +28,6 @@ func TestApply(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"-init",
|
|
||||||
"-state", statePath,
|
"-state", statePath,
|
||||||
testFixturePath("apply"),
|
testFixturePath("apply"),
|
||||||
}
|
}
|
||||||
|
@ -66,7 +65,6 @@ func TestApply_configInvalid(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"-init",
|
|
||||||
"-state", testTempFile(t),
|
"-state", testTempFile(t),
|
||||||
testFixturePath("apply-config-invalid"),
|
testFixturePath("apply-config-invalid"),
|
||||||
}
|
}
|
||||||
|
@ -102,7 +100,6 @@ func TestApply_defaultState(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"-init",
|
|
||||||
testFixturePath("apply"),
|
testFixturePath("apply"),
|
||||||
}
|
}
|
||||||
if code := c.Run(args); code != 0 {
|
if code := c.Run(args); code != 0 {
|
||||||
|
@ -168,7 +165,6 @@ func TestApply_error(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"-init",
|
|
||||||
"-state", statePath,
|
"-state", statePath,
|
||||||
testFixturePath("apply-error"),
|
testFixturePath("apply-error"),
|
||||||
}
|
}
|
||||||
|
@ -220,7 +216,6 @@ func TestApply_noArgs(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"-init",
|
|
||||||
"-state", statePath,
|
"-state", statePath,
|
||||||
}
|
}
|
||||||
if code := c.Run(args); code != 0 {
|
if code := c.Run(args); code != 0 {
|
||||||
|
@ -373,7 +368,6 @@ func TestApply_shutdown(t *testing.T) {
|
||||||
}()
|
}()
|
||||||
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"-init",
|
|
||||||
"-state", statePath,
|
"-state", statePath,
|
||||||
testFixturePath("apply-shutdown"),
|
testFixturePath("apply-shutdown"),
|
||||||
}
|
}
|
||||||
|
@ -516,7 +510,6 @@ func TestApply_vars(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"-init",
|
|
||||||
"-var", "foo=bar",
|
"-var", "foo=bar",
|
||||||
"-state", statePath,
|
"-state", statePath,
|
||||||
testFixturePath("apply-vars"),
|
testFixturePath("apply-vars"),
|
||||||
|
@ -559,7 +552,6 @@ func TestApply_varFile(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"-init",
|
|
||||||
"-var-file", varFilePath,
|
"-var-file", varFilePath,
|
||||||
"-state", statePath,
|
"-state", statePath,
|
||||||
testFixturePath("apply-vars"),
|
testFixturePath("apply-vars"),
|
||||||
|
|
|
@ -59,26 +59,15 @@ func (m *Meta) Context(path, statePath string, doPlan bool) (*terraform.Context,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if statePath != "" {
|
|
||||||
if _, err := os.Stat(statePath); err != nil {
|
|
||||||
return nil, fmt.Errorf(
|
|
||||||
"There was an error reading the state file. The path\n"+
|
|
||||||
"and error are shown below. If you're trying to build a\n"+
|
|
||||||
"brand new infrastructure, explicitly pass the '-init'\n"+
|
|
||||||
"flag to Terraform to tell it it is okay to build new\n"+
|
|
||||||
"state.\n\n"+
|
|
||||||
"Path: %s\n"+
|
|
||||||
"Error: %s",
|
|
||||||
statePath,
|
|
||||||
err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load up the state
|
// Load up the state
|
||||||
var state *terraform.State
|
var state *terraform.State
|
||||||
if statePath != "" {
|
if statePath != "" {
|
||||||
f, err := os.Open(statePath)
|
f, err := os.Open(statePath)
|
||||||
if err == nil {
|
if err != nil && os.IsNotExist(err) {
|
||||||
|
// If the state file doesn't exist, it is okay, since it
|
||||||
|
// is probably a new infrastructure.
|
||||||
|
err = nil
|
||||||
|
} else if err == nil {
|
||||||
state, err = terraform.ReadState(f)
|
state, err = terraform.ReadState(f)
|
||||||
f.Close()
|
f.Close()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue