From 25386f8e8e9a24b6185209bbff73c168a9c7e107 Mon Sep 17 00:00:00 2001 From: cfortier2 Date: Wed, 21 May 2014 09:24:34 -0400 Subject: [PATCH 1/4] add support for chef_environment --- provisioner/chef-client/provisioner.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/provisioner/chef-client/provisioner.go b/provisioner/chef-client/provisioner.go index 1e2847f54..ba84673d4 100644 --- a/provisioner/chef-client/provisioner.go +++ b/provisioner/chef-client/provisioner.go @@ -20,6 +20,7 @@ import ( type Config struct { common.PackerConfig `mapstructure:",squash"` + ChefEnvironment string `mapstructure:"chef_environment"` ConfigTemplate string `mapstructure:"config_template"` ExecuteCommand string `mapstructure:"execute_command"` InstallCommand string `mapstructure:"install_command"` @@ -47,6 +48,7 @@ type ConfigTemplate struct { ServerUrl string ValidationKeyPath string ValidationClientName string + ChefEnvironment string } type ExecuteTemplate struct { @@ -94,10 +96,11 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { errs := common.CheckUnusedConfig(md) templates := map[string]*string{ - "config_template": &p.config.ConfigTemplate, - "node_name": &p.config.NodeName, - "staging_dir": &p.config.StagingDir, - "chef_server_url": &p.config.ServerUrl, + "config_template": &p.config.ConfigTemplate, + "node_name": &p.config.NodeName, + "staging_dir": &p.config.StagingDir, + "chef_server_url": &p.config.ServerUrl, + "chef_environment": &p.config.ChefEnvironment, } for n, ptr := range templates { @@ -202,7 +205,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { } configPath, err := p.createConfig( - ui, comm, nodeName, serverUrl, remoteValidationKeyPath, p.config.ValidationClientName) + ui, comm, nodeName, serverUrl, remoteValidationKeyPath, p.config.ValidationClientName, p.config.ChefEnvironment) if err != nil { return fmt.Errorf("Error creating Chef config file: %s", err) } @@ -256,7 +259,7 @@ func (p *Provisioner) uploadDirectory(ui packer.Ui, comm packer.Communicator, ds return comm.UploadDir(dst, src, nil) } -func (p *Provisioner) createConfig(ui packer.Ui, comm packer.Communicator, nodeName string, serverUrl string, remoteKeyPath string, validationClientName string) (string, error) { +func (p *Provisioner) createConfig(ui packer.Ui, comm packer.Communicator, nodeName string, serverUrl string, remoteKeyPath string, validationClientName string, chefEnvironment string) (string, error) { ui.Message("Creating configuration file 'client.rb'") // Read the template @@ -281,6 +284,7 @@ func (p *Provisioner) createConfig(ui packer.Ui, comm packer.Communicator, nodeN ServerUrl: serverUrl, ValidationKeyPath: remoteKeyPath, ValidationClientName: validationClientName, + ChefEnvironment: chefEnvironment, }) if err != nil { return "", err @@ -548,4 +552,7 @@ validation_key "{{.ValidationKeyPath}}" {{if ne .NodeName ""}} node_name "{{.NodeName}}" {{end}} +{{if ne .ChefEnvironment ""}} +environment "{{.ChefEnvironment}}" +{{end}} ` From e908cc1733d7981cca9f9d6464cbb54b6f4c0003 Mon Sep 17 00:00:00 2001 From: cfortier2 Date: Wed, 21 May 2014 09:30:18 -0400 Subject: [PATCH 2/4] add chef_environment test --- provisioner/chef-client/provisioner_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/provisioner/chef-client/provisioner_test.go b/provisioner/chef-client/provisioner_test.go index b9d3b9c43..b6a5a9d2e 100644 --- a/provisioner/chef-client/provisioner_test.go +++ b/provisioner/chef-client/provisioner_test.go @@ -22,6 +22,22 @@ func TestProvisioner_Impl(t *testing.T) { } } +func TestProvisionerPrepare_chefEnvironment(t *testing.T) { + var p Provisioner + + config := testConfig() + config["chef_environment"] = "some-env" + + err := p.Prepare(config) + if err != nil { + t.Fatalf("err: %s", err) + } + + if p.config.ChefEnvironment != "some-env" { + t.Fatalf("unexpected: %#v", p.config.ChefEnvironment) + } +} + func TestProvisionerPrepare_configTemplate(t *testing.T) { var err error var p Provisioner From fe743f96c7d2074b80422e42a4068d3d5b4aac5b Mon Sep 17 00:00:00 2001 From: cfortier2 Date: Wed, 21 May 2014 09:40:02 -0400 Subject: [PATCH 3/4] update website chef-client provisioner page --- website/source/docs/provisioners/chef-client.html.markdown | 3 +++ 1 file changed, 3 insertions(+) diff --git a/website/source/docs/provisioners/chef-client.html.markdown b/website/source/docs/provisioners/chef-client.html.markdown index 5c745a486..65b1c63c8 100644 --- a/website/source/docs/provisioners/chef-client.html.markdown +++ b/website/source/docs/provisioners/chef-client.html.markdown @@ -55,6 +55,9 @@ since it will allow the provisioner to clean up the node/client. Server. This is optional and by defalt is empty. If you don't set this, Packer can't clean up the node from the Chef Server using knife. +* `chef_environment` (string) - The name of the chef_environment sent to the + Chef server. By default this is empty and will not use an environment. + * `prevent_sudo` (boolean) - By default, the configured commands that are executed to install and run Chef are executed with `sudo`. If this is true, then the sudo will be omitted. From bf38fa9f4f070e3909257c6b557640c711d53b5f Mon Sep 17 00:00:00 2001 From: cfortier2 Date: Wed, 21 May 2014 13:34:12 -0400 Subject: [PATCH 4/4] reorder chef_environment alphabetically --- website/source/docs/provisioners/chef-client.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/website/source/docs/provisioners/chef-client.html.markdown b/website/source/docs/provisioners/chef-client.html.markdown index 65b1c63c8..bfa916fd0 100644 --- a/website/source/docs/provisioners/chef-client.html.markdown +++ b/website/source/docs/provisioners/chef-client.html.markdown @@ -33,6 +33,9 @@ The reference of available configuration options is listed below. No configuration is actually required, but `node_name` is recommended since it will allow the provisioner to clean up the node/client. +* `chef_environment` (string) - The name of the chef_environment sent to the + Chef server. By default this is empty and will not use an environment. + * `config_template` (string) - Path to a template that will be used for the Chef configuration file. By default Packer only sets configuration it needs to match the settings set in the provisioner configuration. If @@ -55,9 +58,6 @@ since it will allow the provisioner to clean up the node/client. Server. This is optional and by defalt is empty. If you don't set this, Packer can't clean up the node from the Chef Server using knife. -* `chef_environment` (string) - The name of the chef_environment sent to the - Chef server. By default this is empty and will not use an environment. - * `prevent_sudo` (boolean) - By default, the configured commands that are executed to install and run Chef are executed with `sudo`. If this is true, then the sudo will be omitted.