@@ -58,6 +58,11 @@ class CsStackCreate < Chef::Knife
5858 :long => "--identity-file IDENTITY_FILE" ,
5959 :description => "The SSH identity file used for authentication"
6060
61+ option :skip_existing ,
62+ :long => "--skip-existing" ,
63+ :default => false ,
64+ :description => "Skip creating existing server(s)"
65+
6166 def run
6267 validate_base_options
6368 if @name_args . first . nil?
@@ -82,9 +87,13 @@ def create_stack(stack)
8287 # create server(s)
8388 names = server [ :name ] . split ( /[\s ,]+/ )
8489 names . each do |n |
85- s = Mash . new ( server )
86- s [ :name ] = n
87- create_server ( s )
90+ if ( config [ :skip_existing ] && connection . get_server ( n ) )
91+ ui . msg ( ui . color ( "\n Server #{ n } already exists; skipping create..." , :yellow ) )
92+ else
93+ s = Mash . new ( server )
94+ s [ :name ] = n
95+ create_server ( s )
96+ end
8897 end
8998
9099 end
@@ -109,19 +118,19 @@ def create_server(server)
109118 cmd . config [ :cloudstack_project ] = config [ :cloudstack_project ]
110119 cmd . config [ :ssh_user ] = config [ :ssh_user ]
111120 cmd . config [ :ssh_password ] = config [ :ssh_password ]
112- cmd . config [ :ssh_port ] = config [ :ssh_port ] || "22" # Chef::Config[:knife][:ssh_port]
121+ cmd . config [ :ssh_port ] = server [ :ssh_port ] || locate_config_value ( :ssh_port ) || "22"
113122 cmd . config [ :identity_file ] = config [ :identity_file ]
114123 cmd . config [ :keypair ] = server [ :keypair ]
115124 cmd . config [ :cloudstack_template ] = server [ :template ] if server [ :template ]
116125 cmd . config [ :cloudstack_service ] = server [ :service ] if server [ :service ]
117126 cmd . config [ :cloudstack_zone ] = server [ :zone ] if server [ :zone ]
118127 server . has_key? ( :public_ip ) ? cmd . config [ :public_ip ] = server [ :public_ip ] : cmd . config [ :no_public_ip ] = true
119128 cmd . config [ :ik_private_ip ] = server [ :private_ip ] if server [ :private_ip ]
120- cmd . config [ :bootstrap ] = server [ :bootstrap ] if server . has_key? ( :bootstrap )
121- cmd . config [ :bootstrap_protocol ] = server [ :bootstrap_protocol ] || "ssh"
122- cmd . config [ :distro ] = server [ :distro ] || "chef-full"
123- cmd . config [ :template_file ] = server [ :template_file ] if server . has_key? ( :template_file )
124- cmd . config [ :no_host_key_verify ] = server [ :no_host_key_verify ] if server . has_key? ( :no_host_key_verify )
129+ cmd . config [ :bootstrap ] = server [ :bootstrap ] if server . has_key? ( :bootstrap )
130+ cmd . config [ :bootstrap_protocol ] = server [ :bootstrap_protocol ] || "ssh"
131+ cmd . config [ :distro ] = server [ :distro ] || "chef-full"
132+ cmd . config [ :template_file ] = server [ :template_file ] if server . has_key? ( :template_file )
133+ cmd . config [ :no_host_key_verify ] = server [ :no_host_key_verify ] if server . has_key? ( :no_host_key_verify )
125134 cmd . config [ :cloudstack_networks ] = server [ :networks ] . split ( /[\s ,]+/ ) if server [ :networks ]
126135 cmd . config [ :run_list ] = server [ :run_list ] . split ( /[\s ,]+/ ) if server [ :run_list ]
127136 cmd . config [ :port_rules ] = server [ :port_rules ] . split ( /[\s ,]+/ ) if server [ :port_rules ]
@@ -134,6 +143,7 @@ def create_server(server)
134143 end
135144
136145 def run_actions ( actions )
146+ return if actions . nil? || actions . empty?
137147 puts "\n "
138148 ui . msg ( "Processing actions..." )
139149 sleep 1 # pause for e.g. chef solr indexing
@@ -183,7 +193,6 @@ def search_nodes(query, attribute=nil)
183193 end
184194
185195 def knife_ssh ( host_list , command )
186-
187196 ssh = Chef ::Knife ::Ssh . new
188197 ssh . name_args = [ host_list , command ]
189198 ssh . config [ :ssh_user ] = config [ :ssh_user ]
@@ -203,7 +212,6 @@ def knife_ssh_with_password_auth(host_list, command)
203212 end
204213
205214 def knife_ssh_action ( query , command )
206-
207215 public_ips = find_public_ips ( query )
208216 return if public_ips . nil? || public_ips . empty?
209217 host_list = public_ips . join ( ' ' )
@@ -218,7 +226,6 @@ def knife_ssh_action(query, command)
218226 ssh . run
219227 end
220228 end
221-
222229 end
223230
224231 def http_request ( url )
@@ -229,7 +236,6 @@ def http_request(url)
229236 url = url . sub ( /\$ \{ #{ server_name } \} / , ip )
230237 end
231238
232-
233239 puts "HTTP Request: #{ url } "
234240 puts `curl -s -m 5 #{ url } `
235241 end
@@ -272,24 +278,6 @@ def get_environment
272278 current_stack [ :environment ]
273279 end
274280
275- def destroy_all ( domain , excludes = [ ] )
276- servers = connection . list_servers || [ ]
277- servers . each do |s |
278- excluded = false
279- excludes . each { |val |
280- if s [ 'name' ] =~ /#{ val } / then
281- excluded = true
282- next
283- end
284- }
285- next if excluded
286- nodename = "#{ s [ 'name' ] } .#{ domain } "
287- system "knife cs server delete #{ s [ 'name' ] } -y"
288- system "knife client delete #{ nodename } -y"
289- system "knife node delete #{ nodename } -y"
290- end
291- end
292-
293281 def print_local_hosts
294282 hosts = [ ]
295283 current_stack [ :servers ] . each do |server |
0 commit comments