Skip to content

Commit f024957

Browse files
author
Warren Bain
committed
Improvements to 'knife cs stack create'
* Add option to skip servers that are already created * Remove unused 'destroy_all' method * Allow ssh_port to come from command line or config file and default to 22 if neither is provided * Whitespace cleanup
1 parent 28a9cdc commit f024957

1 file changed

Lines changed: 18 additions & 31 deletions

File tree

lib/chef/knife/cs_stack_create.rb

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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("\nServer #{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] = config[:ssh_port] || Chef::Config[:knife][: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]
@@ -183,7 +192,6 @@ def search_nodes(query, attribute=nil)
183192
end
184193

185194
def knife_ssh(host_list, command)
186-
187195
ssh = Chef::Knife::Ssh.new
188196
ssh.name_args = [host_list, command]
189197
ssh.config[:ssh_user] = config[:ssh_user]
@@ -203,7 +211,6 @@ def knife_ssh_with_password_auth(host_list, command)
203211
end
204212

205213
def knife_ssh_action(query, command)
206-
207214
public_ips = find_public_ips(query)
208215
return if public_ips.nil? || public_ips.empty?
209216
host_list = public_ips.join(' ')
@@ -218,7 +225,6 @@ def knife_ssh_action(query, command)
218225
ssh.run
219226
end
220227
end
221-
222228
end
223229

224230
def http_request(url)
@@ -229,7 +235,6 @@ def http_request(url)
229235
url = url.sub(/\$\{#{server_name}\}/, ip)
230236
end
231237

232-
233238
puts "HTTP Request: #{url}"
234239
puts `curl -s -m 5 #{url}`
235240
end
@@ -272,24 +277,6 @@ def get_environment
272277
current_stack[:environment]
273278
end
274279

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-
293280
def print_local_hosts
294281
hosts = []
295282
current_stack[:servers].each do |server|

0 commit comments

Comments
 (0)