From d3c2239b9e356ac098260c68f13c2f21d5df0e35 Mon Sep 17 00:00:00 2001 From: Rickard von Essen Date: Wed, 14 Dec 2016 21:50:26 +0100 Subject: [PATCH] builder/amazon: Change shutdown_behaviour to shutdown_behavior --- builder/amazon/common/run_config.go | 4 +- builder/amazon/ebs/builder_test.go | 8 +-- builder/amazon/ebsvolume/builder_test.go | 8 +-- fix/fixer.go | 22 ++++--- fix/fixer_amazon_shutdown_behavior.go | 52 ++++++++++++++++ fix/fixer_amazon_shutdown_behavior_test.go | 62 +++++++++++++++++++ .../docs/builders/amazon-ebs-volume.html.md | 2 +- .../source/docs/builders/amazon-ebs.html.md | 6 +- .../source/docs/builders/openstack.html.md | 2 +- .../source/docs/provisioners/ansible.html.md | 4 +- 10 files changed, 141 insertions(+), 29 deletions(-) create mode 100644 fix/fixer_amazon_shutdown_behavior.go create mode 100644 fix/fixer_amazon_shutdown_behavior_test.go diff --git a/builder/amazon/common/run_config.go b/builder/amazon/common/run_config.go index 3ede3ebc1..52289ba50 100644 --- a/builder/amazon/common/run_config.go +++ b/builder/amazon/common/run_config.go @@ -46,7 +46,7 @@ type RunConfig struct { UserDataFile string `mapstructure:"user_data_file"` WindowsPasswordTimeout time.Duration `mapstructure:"windows_password_timeout"` VpcId string `mapstructure:"vpc_id"` - InstanceInitiatedShutdownBehavior string `mapstructure:"shutdown_behaviour"` + InstanceInitiatedShutdownBehavior string `mapstructure:"shutdown_behavior"` // Communicator settings Comm communicator.Config `mapstructure:",squash"` @@ -106,7 +106,7 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error { if c.InstanceInitiatedShutdownBehavior == "" { c.InstanceInitiatedShutdownBehavior = "stop" } else if !reShutdownBehavior.MatchString(c.InstanceInitiatedShutdownBehavior) { - errs = append(errs, fmt.Errorf("shutdown_behaviour only accepts 'stop' or 'terminate' values.")) + errs = append(errs, fmt.Errorf("shutdown_behavior only accepts 'stop' or 'terminate' values.")) } return errs diff --git a/builder/amazon/ebs/builder_test.go b/builder/amazon/ebs/builder_test.go index e63667d4b..945afbd95 100644 --- a/builder/amazon/ebs/builder_test.go +++ b/builder/amazon/ebs/builder_test.go @@ -93,12 +93,12 @@ func TestBuilderPrepare_InvalidKey(t *testing.T) { } } -func TestBuilderPrepare_InvalidShutdownBehaviour(t *testing.T) { +func TestBuilderPrepare_InvalidShutdownBehavior(t *testing.T) { var b Builder config := testConfig() // Test good - config["shutdown_behaviour"] = "terminate" + config["shutdown_behavior"] = "terminate" warnings, err := b.Prepare(config) if len(warnings) > 0 { t.Fatalf("bad: %#v", warnings) @@ -108,7 +108,7 @@ func TestBuilderPrepare_InvalidShutdownBehaviour(t *testing.T) { } // Test good - config["shutdown_behaviour"] = "stop" + config["shutdown_behavior"] = "stop" warnings, err = b.Prepare(config) if len(warnings) > 0 { t.Fatalf("bad: %#v", warnings) @@ -118,7 +118,7 @@ func TestBuilderPrepare_InvalidShutdownBehaviour(t *testing.T) { } // Test bad - config["shutdown_behaviour"] = "foobar" + config["shutdown_behavior"] = "foobar" warnings, err = b.Prepare(config) if len(warnings) > 0 { t.Fatalf("bad: %#v", warnings) diff --git a/builder/amazon/ebsvolume/builder_test.go b/builder/amazon/ebsvolume/builder_test.go index 9b1040f01..61a8f2ff1 100644 --- a/builder/amazon/ebsvolume/builder_test.go +++ b/builder/amazon/ebsvolume/builder_test.go @@ -55,12 +55,12 @@ func TestBuilderPrepare_InvalidKey(t *testing.T) { } } -func TestBuilderPrepare_InvalidShutdownBehaviour(t *testing.T) { +func TestBuilderPrepare_InvalidShutdownBehavior(t *testing.T) { var b Builder config := testConfig() // Test good - config["shutdown_behaviour"] = "terminate" + config["shutdown_behavior"] = "terminate" warnings, err := b.Prepare(config) if len(warnings) > 0 { t.Fatalf("bad: %#v", warnings) @@ -70,7 +70,7 @@ func TestBuilderPrepare_InvalidShutdownBehaviour(t *testing.T) { } // Test good - config["shutdown_behaviour"] = "stop" + config["shutdown_behavior"] = "stop" warnings, err = b.Prepare(config) if len(warnings) > 0 { t.Fatalf("bad: %#v", warnings) @@ -80,7 +80,7 @@ func TestBuilderPrepare_InvalidShutdownBehaviour(t *testing.T) { } // Test bad - config["shutdown_behaviour"] = "foobar" + config["shutdown_behavior"] = "foobar" warnings, err = b.Prepare(config) if len(warnings) > 0 { t.Fatalf("bad: %#v", warnings) diff --git a/fix/fixer.go b/fix/fixer.go index 382cd25b8..3c285c4be 100644 --- a/fix/fixer.go +++ b/fix/fixer.go @@ -20,16 +20,17 @@ var FixerOrder []string func init() { Fixers = map[string]Fixer{ - "iso-md5": new(FixerISOMD5), - "createtime": new(FixerCreateTime), - "pp-vagrant-override": new(FixerVagrantPPOverride), - "virtualbox-gaattach": new(FixerVirtualBoxGAAttach), - "virtualbox-rename": new(FixerVirtualBoxRename), - "vmware-rename": new(FixerVMwareRename), - "parallels-headless": new(FixerParallelsHeadless), - "parallels-deprecations": new(FixerParallelsDeprecations), - "sshkeypath": new(FixerSSHKeyPath), - "manifest-filename": new(FixerManifestFilename), + "iso-md5": new(FixerISOMD5), + "createtime": new(FixerCreateTime), + "pp-vagrant-override": new(FixerVagrantPPOverride), + "virtualbox-gaattach": new(FixerVirtualBoxGAAttach), + "virtualbox-rename": new(FixerVirtualBoxRename), + "vmware-rename": new(FixerVMwareRename), + "parallels-headless": new(FixerParallelsHeadless), + "parallels-deprecations": new(FixerParallelsDeprecations), + "sshkeypath": new(FixerSSHKeyPath), + "manifest-filename": new(FixerManifestFilename), + "amazon-shutdown_behavior": new(FixerAmazonShutdownBehavior), } FixerOrder = []string{ @@ -43,5 +44,6 @@ func init() { "parallels-deprecations", "sshkeypath", "manifest-filename", + "amazon-shutdown_behavior", } } diff --git a/fix/fixer_amazon_shutdown_behavior.go b/fix/fixer_amazon_shutdown_behavior.go new file mode 100644 index 000000000..2e47c5592 --- /dev/null +++ b/fix/fixer_amazon_shutdown_behavior.go @@ -0,0 +1,52 @@ +package fix + +import ( + "github.com/mitchellh/mapstructure" +) + +// FixerAmazonShutdownBehavior fix the spelling of "shutdown_behavior" +// template in a Amazon builder +type FixerAmazonShutdownBehavior struct{} + +func (FixerAmazonShutdownBehavior) Fix(input map[string]interface{}) (map[string]interface{}, error) { + // The type we'll decode into; we only care about builders + type template struct { + Builders []map[string]interface{} + } + + // Decode the input into our structure, if we can + var tpl template + if err := mapstructure.Decode(input, &tpl); err != nil { + return nil, err + } + + for _, builder := range tpl.Builders { + builderTypeRaw, ok := builder["type"] + if !ok { + continue + } + + builderType, ok := builderTypeRaw.(string) + if !ok { + continue + } + + if builderType != "amazon-ebs" && builderType != "amazon-ebsvolume" && builderType != "amazon-instance" && builderType != "amazon-chroot" { + continue + } + + shutdownBehavior, ok := builder["shutdown_behaviour"] + + if ok { + builder["shutdown_behavior"] = shutdownBehavior + delete(builder, "shutdown_behaviour") + } + } + + input["builders"] = tpl.Builders + return input, nil +} + +func (FixerAmazonShutdownBehavior) Synopsis() string { + return `Changes "shutdown_behaviour" to "shutdown_behavior" in Amazon builders.` +} diff --git a/fix/fixer_amazon_shutdown_behavior_test.go b/fix/fixer_amazon_shutdown_behavior_test.go new file mode 100644 index 000000000..50718372f --- /dev/null +++ b/fix/fixer_amazon_shutdown_behavior_test.go @@ -0,0 +1,62 @@ +package fix + +import ( + "reflect" + "testing" +) + +func TestFixerAmazonShutdownBehavior(t *testing.T) { + var _ Fixer = new(FixerAmazonShutdownBehavior) +} + +func TestFixerAmazonShutdownBehavior_Fix_shutdown_behaviour(t *testing.T) { + cases := []struct { + Input map[string]interface{} + Expected map[string]interface{} + }{ + // No shutdown_behaviour field + { + Input: map[string]interface{}{ + "type": "amazon-ebs", + }, + + Expected: map[string]interface{}{ + "type": "amazon-ebs", + }, + }, + + // shutdown_behaviour field + { + Input: map[string]interface{}{ + "type": "amazon-ebs", + "shutdown_behaviour": "stop", + }, + + Expected: map[string]interface{}{ + "type": "amazon-ebs", + "shutdown_behavior": "stop", + }, + }, + } + + for _, tc := range cases { + var f FixerAmazonShutdownBehavior + + input := map[string]interface{}{ + "builders": []map[string]interface{}{tc.Input}, + } + + expected := map[string]interface{}{ + "builders": []map[string]interface{}{tc.Expected}, + } + + output, err := f.Fix(input) + if err != nil { + t.Fatalf("err: %s", err) + } + + if !reflect.DeepEqual(output, expected) { + t.Fatalf("unexpected: %#v\nexpected: %#v\n", output, expected) + } + } +} diff --git a/website/source/docs/builders/amazon-ebs-volume.html.md b/website/source/docs/builders/amazon-ebs-volume.html.md index 11e13dd50..8cd97c09c 100644 --- a/website/source/docs/builders/amazon-ebs-volume.html.md +++ b/website/source/docs/builders/amazon-ebs-volume.html.md @@ -114,7 +114,7 @@ builder. described above. Note that if this is specified, you must omit the `security_group_id`. -- `shutdown_behaviour` (string) - Automatically terminate instances on shutdown +- `shutdown_behavior` (string) - Automatically terminate instances on shutdown incase packer exits ungracefully. Possible values are `stop` and `terminate`. Defaults to `stop`. diff --git a/website/source/docs/builders/amazon-ebs.html.md b/website/source/docs/builders/amazon-ebs.html.md index 070d1959a..0352d8327 100644 --- a/website/source/docs/builders/amazon-ebs.html.md +++ b/website/source/docs/builders/amazon-ebs.html.md @@ -192,7 +192,7 @@ builder. described above. Note that if this is specified, you must omit the `security_group_id`. -- `shutdown_behaviour` (string) - Automatically terminate instances on shutdown +- `shutdown_behavior` (string) - Automatically terminate instances on shutdown incase packer exits ungracefully. Possible values are "stop" and "terminate", default is `stop`. @@ -301,10 +301,6 @@ builder. - `windows_password_timeout` (string) - The timeout for waiting for a Windows password for Windows instances. Defaults to 20 minutes. Example value: `10m` -- `shutdown_behaviour` (string) - Automatically terminate instances on shutdown - incase packer exits ungracefully. Possible values are `stop` and `terminate`, - - ## Basic Example Here is a basic example. You will need to provide access keys, and may need to change the AMI IDs according to what images exist at the time the template is run: diff --git a/website/source/docs/builders/openstack.html.md b/website/source/docs/builders/openstack.html.md index 09382101d..1f499d189 100644 --- a/website/source/docs/builders/openstack.html.md +++ b/website/source/docs/builders/openstack.html.md @@ -122,7 +122,7 @@ builder. - `ssh_ip_version` (string) - The IP version to use for SSH connections, valid values are `4` and `6`. Useful on dual stacked instances where the default - behaviour is to connect via whichever IP address is returned first from the + behavior is to connect via whichever IP address is returned first from the OpenStack API. - `ssh_keypair_name` (string) - If specified, this is the key that will be diff --git a/website/source/docs/provisioners/ansible.html.md b/website/source/docs/provisioners/ansible.html.md index b51a832ab..89b8a702c 100644 --- a/website/source/docs/provisioners/ansible.html.md +++ b/website/source/docs/provisioners/ansible.html.md @@ -63,12 +63,12 @@ Optional Parameters: - `ssh_host_key_file` (string) - The SSH key that will be used to run the SSH server on the host machine to forward commands to the target machine. Ansible connects to this server and will validate the identity of the server using - the system known_hosts. The default behaviour is to generate and use a + the system known_hosts. The default behavior is to generate and use a onetime key. Host key checking is disabled via the `ANSIBLE_HOST_KEY_CHECKING` environment variable if the key is generated. - `ssh_authorized_key_file` (string) - The SSH public key of the Ansible - `ssh_user`. The default behaviour is to generate and use a onetime key. If + `ssh_user`. The default behavior is to generate and use a onetime key. If this key is generated, the corresponding private key is passed to `ansible-playbook` with the `--private-key` option.