Class: Knifecosmic::CosmicStackCreate
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/cosmic_stack_create.rb', line 25
def current_stack
@current_stack
end
|
Instance Method Details
#create_server(server) ⇒ Object
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
135
136
137
138
139
140
141
142
143
|
# File 'lib/chef/knife/cosmic_stack_create.rb', line 108
def create_server(server)
cmd = Knifecosmic::CosmicServerCreate.new([server[:name]])
cmd.config[:cosmic_url] = config[:cosmic_url]
cmd.config[:cosmic_api_key] = config[:cosmic_api_key]
cmd.config[:cosmic_secret_key] = config[:cosmic_secret_key]
cmd.config[:cosmic_proxy] = config[:cosmic_proxy]
cmd.config[:cosmic_no_ssl_verify] = config[:cosmic_no_ssl_verify]
cmd.config[:cosmic_project] = config[:cosmic_project]
cmd.config[:ssh_user] = config[:ssh_user]
cmd.config[:ssh_password] = config[:ssh_password]
cmd.config[:ssh_port] = server[:ssh_port] || locate_config_value(:ssh_port) || "22"
cmd.config[:identity_file] = config[:identity_file]
cmd.config[:keypair] = server[:keypair]
cmd.config[:cosmic_template] = server[:template] if server[:template]
cmd.config[:cosmic_service] = server[:service] if server[:service]
cmd.config[:cosmic_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[:cosmic_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/chef/knife/cosmic_stack_create.rb', line 83
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|
if (config[:skip_existing] && connection.get_server(n))
ui.msg(ui.color("\nServer #{n} already exists; skipping create...", :yellow))
else
s = Mash.new(server)
s[:name] = n
create_server(s)
end
end
end
run_actions server[:actions]
end
print_local_hosts
end
|
#find_public_ips(query) ⇒ Object
253
254
255
256
257
258
259
260
|
# File 'lib/chef/knife/cosmic_stack_create.rb', line 253
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
277
278
279
|
# File 'lib/chef/knife/cosmic_stack_create.rb', line 277
def get_environment
current_stack[:environment]
end
|
#http_request(url) ⇒ Object
231
232
233
234
235
236
237
238
239
240
241
|
# File 'lib/chef/knife/cosmic_stack_create.rb', line 231
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
195
196
197
198
199
200
201
202
203
204
205
|
# File 'lib/chef/knife/cosmic_stack_create.rb', line 195
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
# File 'lib/chef/knife/cosmic_stack_create.rb', line 214
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
207
208
209
210
211
212
|
# File 'lib/chef/knife/cosmic_stack_create.rb', line 207
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
# File 'lib/chef/knife/cosmic_stack_create.rb', line 281
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
# File 'lib/chef/knife/cosmic_stack_create.rb', line 262
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/chef/knife/cosmic_stack_create.rb', line 66
def run
validate_base_options
if @name_args.first.nil?
ui.error "Please specify json file eg: knife cosmic 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/chef/knife/cosmic_stack_create.rb', line 145
def run_actions(actions)
return if actions.nil? || actions.empty?
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
243
244
245
246
247
248
249
250
251
|
# File 'lib/chef/knife/cosmic_stack_create.rb', line 243
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/chef/knife/cosmic_stack_create.rb', line 170
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
|