From a46f23fcb81572508396bb9dccc276c8369be60e Mon Sep 17 00:00:00 2001 From: Travis Longoria Date: Thu, 28 May 2015 16:41:56 +0200 Subject: [PATCH] Allow users to specify alternate private networks. https://github.com/chef/knife-openstack/issues/146 --- README.md | 2 +- lib/chef/knife/openstack_server_create.rb | 6 ++++-- spec/unit/openstack_server_create_spec.rb | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4e40ecf1..6a608d7e 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ This plugin provides the following Knife subcommands. Specific command options c knife openstack server create ----------------------------- -Provisions a new server in an OpenStack Compute cloud and then perform a Chef bootstrap (using the SSH protocol). The goal of the bootstrap is to get Chef installed on the target system so it can run Chef Client with a Chef Server. The main assumption is a baseline OS installation exists (provided by the provisioning). It is primarily intended for Chef Client systems that talk to a Chef server. By default the server is bootstrapped using the [chef-full](https://github.com/opscode/chef/blob/master/chef/lib/chef/knife/bootstrap/chef-full.erb) template (default since the 10.10 release). This may be overridden using the `-d` or `--template-file` command options. If you do not have public IP addresses, use the `--private-network` option to use the private IP address for bootstrapping or `--bootstrap-network NAME` to specify an alternate network. Please see `knife openstack server create --help` for all of the supported options. +Provisions a new server in an OpenStack Compute cloud and then perform a Chef bootstrap (using the SSH protocol). The goal of the bootstrap is to get Chef installed on the target system so it can run Chef Client with a Chef Server. The main assumption is a baseline OS installation exists (provided by the provisioning). It is primarily intended for Chef Client systems that talk to a Chef server. By default the server is bootstrapped using the [chef-full](https://github.com/opscode/chef/blob/master/chef/lib/chef/knife/bootstrap/chef-full.erb) template (default since the 10.10 release). This may be overridden using the `-d` or `--template-file` command options. If you do not have public IP addresses, use the `--openstack-private-network` option to use the private IP address for bootstrapping. In addition, you can use the `--bootstrap-network NAME` option to specify an alternate network for either a private or public network. If a network name isn't specified, the default name will be `'public'` for a public network and `'private'` for a private network e.g. when the `--openstack-private-network` option is specified. Please see `knife openstack server create --help` for all of the supported options. knife openstack server delete ----------------------------- diff --git a/lib/chef/knife/openstack_server_create.rb b/lib/chef/knife/openstack_server_create.rb index 48f89ef4..3b6085a2 100644 --- a/lib/chef/knife/openstack_server_create.rb +++ b/lib/chef/knife/openstack_server_create.rb @@ -129,8 +129,10 @@ def before_bootstrap # Use SSH password either specified from command line or from openstack server instance config[:ssh_password] = locate_config_value(:ssh_password) || server.password unless config[:openstack_ssh_key_id] - # private_network means bootstrap_network = 'private' - config[:bootstrap_network] = 'private' if config[:private_network] + # The bootstrap network is always initialised to 'public' when a network name isn't specified. Therefore, + # only set the bootstrap network to 'private' if still initialised to public and nothing was specified for + # the private network name. + config[:bootstrap_network] = 'private' if (config[:private_network] && config[:bootstrap_network] == 'public') # Which IP address to bootstrap unless config[:network] # --no-network diff --git a/spec/unit/openstack_server_create_spec.rb b/spec/unit/openstack_server_create_spec.rb index 627ec27b..e44c9c01 100644 --- a/spec/unit/openstack_server_create_spec.rb +++ b/spec/unit/openstack_server_create_spec.rb @@ -300,6 +300,21 @@ @instance.before_bootstrap expect(@instance.config[:ssh_password]).to be == server_password end + + it "configures the default private bootstrap network to use 'private'" do + allow(@instance.server).to receive(:addresses).and_return({"private"=>[{"version"=>4, "addr"=>"127.0.0.1"}]}) + @instance.config[:private_network] = true + @instance.before_bootstrap + expect(@instance.config[:bootstrap_network]).to be == 'private' + end + + it "configures the bootstrap to use alternate private network" do + allow(@instance.server).to receive(:addresses).and_return({"secure"=>[{"version"=>4, "addr"=>"127.0.0.1"}]}) + @instance.config[:bootstrap_network] = 'secure' + @instance.config[:private_network] = true + @instance.before_bootstrap + expect(@instance.config[:bootstrap_network]).to be == 'secure' + end end describe "#post_connection_validations" do