Class: KnifeCloudstack::CsStackCreate
Instance Attribute Summary collapse
Instance Method Summary
collapse
included, #list_object, #list_object_fields, #output_format
included
Instance Attribute Details
#current_stack ⇒ Object
Returns the value of attribute current_stack.
25
26
27
|
# File 'lib/chef/knife/cs_stack_create.rb', line 25
def current_stack
@current_stack
end
|
Instance Method Details
#create_server(server) ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/chef/knife/cs_stack_create.rb', line 99
def create_server(server)
cmd = KnifeCloudstack::CsServerCreate.new([server[:name]])
cmd.config[:cloudstack_url] = config[:cloudstack_url]
cmd.config[:cloudstack_api_key] = config[:cloudstack_api_key]
cmd.config[:cloudstack_secret_key] = config[:cloudstack_secret_key]
cmd.config[:cloudstack_proxy] = config[:cloudstack_proxy]
cmd.config[:cloudstack_no_ssl_verify] = config[:cloudstack_no_ssl_verify]
cmd.config[:cloudstack_project] = config[:cloudstack_project]
cmd.config[:ssh_user] = config[:ssh_user]
cmd.config[:ssh_password] = config[:ssh_password]
cmd.config[:ssh_port] = config[:ssh_port] || "22" cmd.config[:identity_file] = config[:identity_file]
cmd.config[:keypair] = server[:keypair]
cmd.config[:cloudstack_template] = server[:template] if server[:template]
cmd.config[:cloudstack_service] = server[:service] if server[:service]
cmd.config[:cloudstack_zone] = server[:zone] if server[:zone]
server.has_key?(:public_ip) ? cmd.config[:public_ip] = server[:public_ip] : cmd.config[:no_public_ip] = true
cmd.config[:ik_private_ip] = server[:private_ip] if server[:private_ip]
cmd.config[:bootstrap] = server[:bootstrap] if server.has_key?(:bootstrap)
cmd.config[:bootstrap_protocol] = server[:bootstrap_protocol] || "ssh"
cmd.config[:distro] = server[:distro] || "chef-full"
cmd.config[:template_file] = server[:template_file] if server.has_key?(:template_file)
cmd.config[:no_host_key_verify] = server[:no_host_key_verify] if server.has_key?(:no_host_key_verify)
cmd.config[:cloudstack_networks] = server[:networks].split(/[\s,]+/) if server[:networks]
cmd.config[:run_list] = server[:run_list].split(/[\s,]+/) if server[:run_list]
cmd.config[:port_rules] = server[:port_rules].split(/[\s,]+/) if server[:port_rules]
if current_stack[:environment]
cmd.config[:environment] = current_stack[:environment]
Chef::Config[:environment] = current_stack[:environment]
end
cmd.run_with_pretty_exceptions
end
|
#create_stack(stack) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/chef/knife/cs_stack_create.rb', line 78
def create_stack(stack)
@current_stack = Mash.new(stack)
current_stack[:servers].each do |server|
if server[:name]
names = server[:name].split(/[\s,]+/)
names.each do |n|
s = Mash.new(server)
s[:name] = n
create_server(s)
end
end
run_actions server[:actions]
end
print_local_hosts
end
|
#destroy_all(domain, excludes = []) ⇒ Object
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
# File 'lib/chef/knife/cs_stack_create.rb', line 275
def destroy_all(domain, excludes=[])
servers = connection.list_servers || []
servers.each do |s|
excluded = false
excludes.each { |val|
if s['name'] =~ /#{val}/ then
excluded = true
next
end
}
next if excluded
nodename = "#{s['name']}.#{domain}"
system "knife cs server delete #{s['name']} -y"
system "knife client delete #{nodename} -y"
system "knife node delete #{nodename} -y"
end
end
|
#find_public_ips(query) ⇒ Object
247
248
249
250
251
252
253
254
|
# File 'lib/chef/knife/cs_stack_create.rb', line 247
def find_public_ips(query)
hostnames = search_nodes(query, 'hostname')
puts "Found hostnames: #{hostnames.inspect}"
ips = hostnames.map { |h|
public_ip_for_host h
}
ips.compact.uniq
end
|
#get_environment ⇒ Object
271
272
273
|
# File 'lib/chef/knife/cs_stack_create.rb', line 271
def get_environment
current_stack[:environment]
end
|
#http_request(url) ⇒ Object
224
225
226
227
228
229
230
231
232
233
234
235
|
# File 'lib/chef/knife/cs_stack_create.rb', line 224
def http_request(url)
match_data = /\$\{([a-zA-Z0-9-]+)\}/.match url
if match_data
server_name = match_data[1]
ip = public_ip_for_host(server_name)
url = url.sub(/\$\{#{server_name}\}/, ip)
end
puts "HTTP Request: #{url}"
puts `curl -s -m 5 #{url}`
end
|
#knife_ssh(host_list, command) ⇒ Object
185
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/chef/knife/cs_stack_create.rb', line 185
def knife_ssh(host_list, command)
ssh = Chef::Knife::Ssh.new
ssh.name_args = [host_list, command]
ssh.config[:ssh_user] = config[:ssh_user]
ssh.config[:ssh_password] = config[:ssh_password]
ssh.config[:ssh_port] = Chef::Config[:knife][:ssh_port] || config[:ssh_port]
ssh.config[:identity_file] = config[:identity_file]
ssh.config[:manual] = true
ssh.config[:no_host_key_verify] = config[:no_host_key_verify]
ssh
end
|
#knife_ssh_action(query, command) ⇒ Object
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
# File 'lib/chef/knife/cs_stack_create.rb', line 205
def knife_ssh_action(query, command)
public_ips = find_public_ips(query)
return if public_ips.nil? || public_ips.empty?
host_list = public_ips.join(' ')
ssh = knife_ssh(host_list, command)
begin
ssh.run
rescue Net::SSH::AuthenticationFailed
unless config[:ssh_password]
puts "Failed to authenticate #{config[:ssh_user]} - trying password auth"
ssh = knife_ssh_with_password_auth(host_list, command)
ssh.run
end
end
end
|
#knife_ssh_with_password_auth(host_list, command) ⇒ Object
198
199
200
201
202
203
|
# File 'lib/chef/knife/cs_stack_create.rb', line 198
def knife_ssh_with_password_auth(host_list, command)
ssh = knife_ssh(host_list, command)
ssh.config[:identity_file] = nil
ssh.config[:ssh_password] = ssh.get_password
ssh
end
|
#print_local_hosts ⇒ Object
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
# File 'lib/chef/knife/cs_stack_create.rb', line 293
def print_local_hosts
hosts = []
current_stack[:servers].each do |server|
next unless server[:local_hosts]
name = server[:name].split(' ').first
ip = public_ip_for_host(name)
server[:local_hosts].each { |host|
hostname = host.sub(/\$\{environment\}/, get_environment)
hosts << "#{ip} #{hostname}"
}
end
unless hosts.empty?
puts "\nAdd this to your /etc/hosts file:"
puts hosts.join("\n")
end
end
|
#public_ip_for_host(name) ⇒ Object
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
# File 'lib/chef/knife/cs_stack_create.rb', line 256
def public_ip_for_host(name)
return nil unless name
@public_ip_cache ||= {}
if !@public_ip_cache[name] then
server = connection.get_server(name)
return nil unless server
ip = connection.get_server_public_ip(server)
@public_ip_cache[name] = ip if ip
end
@public_ip_cache[name]
end
|
#run ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/chef/knife/cs_stack_create.rb', line 61
def run
validate_base_options
if @name_args.first.nil?
ui.error "Please specify json file eg: knife cs stack create JSON_FILE"
exit 1
end
file_path = File.expand_path(@name_args.first)
unless File.exist?(file_path) then
ui.error "Stack file '#{file_path}' not found. Please check the path."
exit 1
end
data = File.read file_path
stack = Chef::JSONCompat.from_json data
create_stack stack
end
|
#run_actions(actions) ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/chef/knife/cs_stack_create.rb', line 136
def run_actions(actions)
puts "\n"
ui.msg("Processing actions...")
sleep 1 actions ||= []
actions.each do |cmd|
cmd ||= {}
cmd.each do |name, args|
case name
when 'knife_ssh'
knife_ssh_action(*args)
when 'http_request'
http_request(args)
when 'run_list_remove'
run_list_remove(*args)
when 'sleep'
dur = args || 5
sleep dur
end
end
end
end
|
#run_list_remove(query, entry) ⇒ Object
237
238
239
240
241
242
243
244
245
|
# File 'lib/chef/knife/cs_stack_create.rb', line 237
def run_list_remove(query, entry)
nodes = search_nodes(query)
return unless nodes
nodes.each do |n|
cmd = Chef::Knife::NodeRunListRemove.new([n.name, entry])
cmd.run_with_pretty_exceptions
end
end
|
#search_nodes(query, attribute = nil) ⇒ Object
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/chef/knife/cs_stack_create.rb', line 160
def search_nodes(query, attribute=nil)
if get_environment
query = "(#{query})" + " AND chef_environment:#{get_environment}"
end
Chef::Log.debug("Searching for nodes: #{query}")
q = Chef::Search::Query.new
nodes = Array(q.search(:node, query))
if nodes.length > 1
nodes = nodes.first || []
end
if attribute
nodes.map do |node|
node[attribute.to_s]
end
else
nodes
end
end
|