Skip to content

Commit b47b8cd

Browse files
committed
Merge pull request #105 from ninefold/fix-stack-create-actions
Fix stack create actions
2 parents 27c247f + 3bf747d commit b47b8cd

6 files changed

Lines changed: 41 additions & 51 deletions

File tree

README.rdoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ Displays all the volumes that are currently available within the cloudstack envi
311311
Creates a "stack" of servers based on a JSON definition file. Simple orchestration can be performed by
312312
specifying one or more actions to be executed after a server (or group of servers) is created.
313313

314+
<tt>--skip-existing</tt> Skip erroring on any servers already created in the stack (default is false)
315+
314316
==== Example Stack Definition File:
315317

316318
{

lib/chef/knife/cs_baselist.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def self.included(includer)
4949

5050
def output_format(json)
5151
if locate_config_value(:format) =~ /^j/i
52-
json_hash = {};
52+
json_hash = {};
5353
json.each { |k| json_hash.merge!( k['id'] => k) }
54-
puts JSON.pretty_generate(json_hash)
54+
puts JSON.pretty_generate(json_hash)
5555
exit 0
5656
end
5757
end
@@ -85,16 +85,16 @@ def list_object(columns, object)
8585
object_list = []
8686
if locate_config_value(:fields)
8787
locate_config_value(:fields).split(',').each { |n| object_list << ui.color(("#{n}").strip, :bold) }
88-
else
88+
else
8989
columns.each do |column|
90-
n = (column.split(':').first).strip
90+
n = (column.split(':').first).strip
9191
object_list << (ui.color("#{n}", :bold) || 'N/A')
9292
end
9393
end
9494

9595
n_columns = object_list.count
9696
object_list = [] if locate_config_value(:noheader)
97-
97+
9898
object.each do |r|
9999
if locate_config_value(:fields)
100100
locate_config_value(:fields).downcase.split(',').each { |n| object_list << ((r[("#{n}").strip]).to_s || 'N/A') }

lib/chef/knife/cs_server_create.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,9 @@ class CsServerCreate < Chef::Knife
206206
:default => []
207207

208208
option :aag,
209-
:long => "--anti-affinity-groups GROUP_NAME",
210-
:description => "Comma separated list of anti-affinity group names",
211-
:default => false
209+
:long => "--anti-affinity-groups GROUP_NAME",
210+
:description => "Comma separated list of anti-affinity group names",
211+
:default => false
212212

213213
option :fw_rules,
214214
:short => "-f PORT_RULES",

lib/chef/knife/cs_server_delete.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ def run
6363
ui.msg("Deleted server #{hostname}")
6464

6565
# delete chef client and node
66-
node_name = connection.get_server_fqdn server
67-
delete_chef = confirm_action("Do you want to delete the chef node and client '#{node_name}")
66+
node_name = hostname # connection.get_server_fqdn server ## server create doesn't add fqdn!
67+
delete_chef = confirm_action("Do you want to delete the chef node and client '#{node_name}'")
6868
if delete_chef
6969
delete_node node_name
7070
delete_client node_name
@@ -76,7 +76,7 @@ def run
7676

7777
def show_object_details(s, connection, rules)
7878
return if locate_config_value(:yes)
79-
79+
8080
object_fields = []
8181
object_fields << ui.color("Name:", :cyan)
8282
object_fields << s['name'].to_s

lib/chef/knife/cs_stack_create.rb

Lines changed: 19 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] = 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|

lib/knife-cloudstack/connection.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def get_project_id(name)
7272
end
7373
project['id']
7474
end
75-
75+
7676
##
7777
# Finds the server with the specified name.
7878

@@ -103,7 +103,7 @@ def get_server_public_ip(server, cached_rules=nil, cached_nat=nil)
103103
return ssh_rule['ipaddress'] if ssh_rule
104104

105105
winrm_rule = get_winrm_port_forwarding_rule(server, cached_rules)
106-
return winrm_rule['ipaddress'] if winrm_rule
106+
return winrm_rule['ipaddress'] if winrm_rule
107107

108108
#check for static NAT
109109
if cached_nat
@@ -679,7 +679,7 @@ def get_public_ip_address(ip_address)
679679
end
680680

681681
def list_public_ip_addresses(listall=false)
682-
params = { 'command' => 'listPublicIpAddresses' }
682+
params = { 'command' => 'listPublicIpAddresses' }
683683
params['listall'] = listall
684684

685685
json = send_request(params)
@@ -793,8 +793,8 @@ def get_ssh_port_forwarding_rule(server, cached_rules=nil)
793793
r['publicport'] == '22'
794794
}.first
795795
end
796-
797-
##
796+
797+
##
798798
# Gets the WINRM port forwarding rule for the specified server.
799799

800800
def get_winrm_port_forwarding_rule(server, cached_rules=nil)
@@ -875,16 +875,16 @@ def send_request(params)
875875
uri = URI.parse(url)
876876

877877
http = http_client_builder.new(uri.host, uri.port)
878-
878+
879879
if uri.scheme == "https"
880880
http.use_ssl = true
881881
# Still need to do some testing on SSL, so will fix this later
882882
if @no_ssl_verify
883-
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
883+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
884884
else
885-
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
885+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
886886
end
887-
end
887+
end
888888
request = Net::HTTP::Get.new(uri.request_uri)
889889
response = http.request(request)
890890

0 commit comments

Comments
 (0)