Skip to content

Commit

Permalink
Merge pull request #30 from opscode/muktaa-oc-9450-auto-generation-names
Browse files Browse the repository at this point in the history
OC 9450 - Auto generate chef node name when not input by user
  • Loading branch information
Matt Ray committed Aug 30, 2013
2 parents 4724634 + c6be7e7 commit 3c211f1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
7 changes: 0 additions & 7 deletions lib/chef/knife/cloud/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,6 @@ def validate!(*keys)
def validate_params!
end

#generate a random name if chef_node_name is empty
def get_node_name(chef_node_name)
return chef_node_name unless chef_node_name.nil?
#lazy uuids
chef_node_name = "os-"+rand.to_s.split('.')[1]
end

def pretty_key(key)
key.to_s.gsub(/_/, ' ').gsub(/\w+/){ |w| (w =~ /(ssh)|(aws)/i) ? w.upcase : w.capitalize }
end
Expand Down
11 changes: 11 additions & 0 deletions lib/chef/knife/cloud/server/create_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class ServerCreateCommand < Command
def validate_params!
# Some cloud provider like openstack does not provide way to identify image-os-type, So in such cases take image-os-type from user otherwise set it in code using set_image_os_type method.
set_image_os_type
# set param vm_name to a random value if the name is not set by the user (plugin)
config[:chef_node_name] = get_node_name(locate_config_value(:chef_node_name), locate_config_value(:chef_node_name_prefix))

# validate ssh_user, ssh_password, identity_file for ssh bootstrap protocol and winrm_password for winrm bootstrap protocol
errors = []

Expand Down Expand Up @@ -108,6 +111,14 @@ def after_bootstrap
def set_image_os_type
raise Chef::Exceptions::Override, "You must override set_image_os_type in #{self.to_s} to set image_os_type"
end

#generate a random name if chef_node_name is empty
def get_node_name(chef_node_name, prefix)
return chef_node_name unless chef_node_name.nil?
#lazy uuids
chef_node_name = "#{prefix}-"+rand.to_s.split('.')[1]
end

end # class ServerCreateCommand
end
end
Expand Down
6 changes: 6 additions & 0 deletions lib/chef/knife/cloud/server/create_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ def self.included(includer)
:default => false,
:description => "Destroy corresponding server in case of failure"

option :chef_node_name_prefix,
:long => "--chef-node-name-prefix PREFIX_FOR_NODE_NAME",
:description => "The prefix for chef node name",
:default => includer.snake_case_name.split('_').first,
:proc => Proc.new { |key| Chef::Config[:knife][:chef_node_name_prefix] = key }

end
end

Expand Down
30 changes: 29 additions & 1 deletion spec/support/shared_examples_for_servercreatecommand.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,33 @@
instance.should_receive(:after_bootstrap).ordered
instance.bootstrap
end
end
end

describe "#get_node_name" do
it "auto generates chef_node_name" do
instance.config[:bootstrap_protocol] = 'ssh'
instance.config[:ssh_password] = 'password'
instance.config[:image_os_type] = 'linux'
instance.config[:chef_node_name_prefix] = 'os'
instance.stub(:set_image_os_type)
instance.should_receive(:get_node_name).and_call_original
instance.validate_params!
instance.config[:chef_node_name].should =~ /os-*/
end

it "auto generates unique chef_node_name" do
node_names = []
instance.config[:bootstrap_protocol] = 'ssh'
instance.config[:ssh_password] = 'password'
instance.config[:image_os_type] = 'linux'
instance.config[:chef_node_name_prefix] = 'os'
instance.stub(:set_image_os_type)
5.times do
instance.config[:chef_node_name] = nil
instance.validate_params!
node_names.should_not include(instance.config[:chef_node_name])
node_names.push(instance.config[:chef_node_name])
end
end
end
end

0 comments on commit 3c211f1

Please sign in to comment.