Class: Kytoon::Providers::Openstack::ServerGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/kytoon/providers/openstack/server_group.rb

Overview

Openstack server group provider.

Constant Summary collapse

CONFIG_FILE =
KYTOON_PROJECT + File::SEPARATOR + "config" + File::SEPARATOR + "server_group.json"
@@connection =
nil
@@data_dir =
File.join(KYTOON_PROJECT, "tmp", "openstack")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ServerGroup

Returns a new instance of ServerGroup.



31
32
33
34
35
36
# File 'lib/kytoon/providers/openstack/server_group.rb', line 31

def initialize(options={})
@id = options[:id] || Time.now.to_f
@name = options[:name]
@use_security_groups = options[:use_security_groups]
@servers=[]
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



27
28
29
# File 'lib/kytoon/providers/openstack/server_group.rb', line 27

def id
  @id
end

#nameObject

Returns the value of attribute name.



28
29
30
# File 'lib/kytoon/providers/openstack/server_group.rb', line 28

def name
  @name
end

#use_security_groupsObject

Returns the value of attribute use_security_groups.



29
30
31
# File 'lib/kytoon/providers/openstack/server_group.rb', line 29

def use_security_groups
  @use_security_groups
end

Class Method Details

.assign_floating_ip(server_id) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/kytoon/providers/openstack/server_group.rb', line 319

def self.assign_floating_ip(server_id)

  conn = self.init_connection

  data = conn.allocate_address.body
  address_id = data['floating_ip']['id']
  address_ip = data['floating_ip']['ip']

  configs = Util.load_configs
  network_name = configs['openstack_network_name'] || 'public'

  # wait for instance to obtain fixed ip
  1.upto(60) do
    server = conn.servers.get(server_id)
    if server.addresses and server.addresses[network_name] and server.addresses[network_name].detect {|a| a['version'] == self.default_ip_type} then
      break
    end
  end

  conn.associate_address(server_id, address_ip).body
  [address_id, address_ip]

end

.create(sg) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/kytoon/providers/openstack/server_group.rb', line 146

def self.create(sg)

  hosts_file_data = "127.0.0.1\tlocalhost localhost.localdomain\n"

  build_timeout = (Util.load_configs['openstack_build_timeout'] || 60).to_i

  base_key_name=File.join(@@data_dir, "#{sg.id}_id_rsa")
  Kytoon::Util.generate_ssh_keypair(base_key_name)
  private_ssh_key=IO.read(base_key_name)
  public_ssh_key=IO.read(base_key_name + ".pub")

  sg.servers.each do |server|
    server_id = create_instance(sg.id, server['hostname'], server['image_ref'], server['flavor_ref'], server['keypair_name']).id
    server['id'] = server_id

    if server['assign_floating_ip'] == 'true' then
      floating_data = assign_floating_ip(server_id)
      server['floating_ip_id'] = floating_data[0]
      server['floating_ip'] = floating_data[1]
    end

    sg.cache_to_disk
  end

  begin
    Timeout::timeout(build_timeout) do
      ips = get_server_ips
      sg.servers.each do |server|
        server_ip = ips[server['id']]
        if server['assign_floating_ip'] == 'true' then
          server['ip_address'] = server['floating_ip']
        else
          server['ip_address'] = server_ip
        end
        sg.cache_to_disk
        hosts_file_data += "#{server_ip}\t#{server['hostname']}\n"
      end
    end
  rescue Timeout::Error => te
    raise KytoonException, "Timeout building server group."
  end

  puts "Copying hosts files..."

gateway_ssh_config = %{
mkdir -p .ssh
cat > .ssh/id_rsa <<-EOF_CAT
#{private_ssh_key}
EOF_CAT
chmod 600 .ssh/id_rsa
cat > .ssh/id_rsa.pub <<-EOF_CAT
#{public_ssh_key}
EOF_CAT
chmod 644 .ssh/id_rsa.pub
cat > .ssh/config <<-EOF_CAT
StrictHostKeyChecking no
EOF_CAT
chmod 600 .ssh/config
}

node_ssh_config= %{
mkdir -p .ssh
cat >> .ssh/authorized_keys <<-EOF_CAT
#{public_ssh_key}\n
EOF_CAT
chmod 600 .ssh/authorized_keys
}

  # now that we have IP info copy hosts files into the servers
  sg.servers.each do |server|
    ping_test(server['ip_address'])
    Kytoon::Util.remote_exec(%{
cat > /etc/hosts <<-EOF_CAT
#{hosts_file_data}
EOF_CAT
hostname "#{server['hostname']}"
if [ -f /etc/sysconfig/network ]; then
sed -e "s|^HOSTNAME.*|HOSTNAME=#{server['hostname']}|" -i /etc/sysconfig/network
fi
#{server['gateway'] == 'true' ? gateway_ssh_config : ""}
#{node_ssh_config}
    }, server['ip_address'], 3) do |ok, out|
      if not ok
        puts out
        raise KytoonException, "Failed to copy host file to instance #{server['hostname']}."
      end
    end
  end

  sg

end

.create_instance(group_id, hostname, image_ref, flavor_ref, keypair_name) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/kytoon/providers/openstack/server_group.rb', line 291

def self.create_instance(group_id, hostname, image_ref, flavor_ref, keypair_name)

  configs = Util.load_configs
  conn = self.init_connection

  options = {
    :name => "#{group_id}_#{hostname}",
    :image_ref => image_ref,
    :flavor_ref => flavor_ref}

  keypair_name = configs['openstack_keypair_name'] if keypair_name.nil?
  security_groups = configs['openstack_security_groups']
  if not security_groups.nil? and not security_groups.empty? then
    options.store(:security_groups, security_groups)
  end
  if not keypair_name.nil? and not keypair_name.empty? then
    options.store(:key_name, keypair_name)
  else
    options.store(:personality, [
      {'path' => "/root/.ssh/authorized_keys",
       'contents' => IO.read(Util.public_key_path)}])
  end

  server = conn.servers.create(options)
  server

end

.data_dirObject



17
18
19
# File 'lib/kytoon/providers/openstack/server_group.rb', line 17

def self.data_dir
  @@data_dir
end

.data_dir=(dir) ⇒ Object



21
22
23
# File 'lib/kytoon/providers/openstack/server_group.rb', line 21

def self.data_dir=(dir)
  @@data_dir=dir
end

.default_ip_typeObject



366
367
368
369
# File 'lib/kytoon/providers/openstack/server_group.rb', line 366

def self.default_ip_type()
  ip_type = Util.load_configs['openstack_ip_type'] || 4
  ip_type.to_i
end

.destroy_instance(uuid) ⇒ Object



419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/kytoon/providers/openstack/server_group.rb', line 419

def self.destroy_instance(uuid)
  begin
    conn = self.init_connection
    server = conn.servers.get(uuid)
    if server then
      server.destroy
    else
      puts "Server #{uuid} no longer exists."
    end
  rescue Exception => e
    puts "Error deleting server: #{e.message}"
  end
end

.from_json(json) ⇒ Object

generate a Server Group XML from server_group.json



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/kytoon/providers/openstack/server_group.rb', line 51

def self.from_json(json)

  json_hash=JSON.parse(json)

  sg=ServerGroup.new(
    :id => json_hash["id"],
    :name => json_hash["name"],
    :use_security_groups => json_hash["use_security_groups"]
  )
  json_hash["servers"].each do |server_hash|

    sg.servers << {
      'id' => server_hash['id'],
      'hostname' => server_hash['hostname'],
      'image_ref' => server_hash['image_ref'],
      'flavor_ref' => server_hash['flavor_ref'],
      'keypair_name' => server_hash['keypair_name'],
      'floating_ip' => server_hash['floating_ip'],
      'gateway' => server_hash['gateway'] || "false",
      'assign_floating_ip' => server_hash['assign_floating_ip'] || "false",
      'floating_ip' => server_hash['floating_ip'] || nil,
      'floating_ip_id' => server_hash['floating_ip_id'] || nil,
      'ip_address' => server_hash['ip_address']
    }
  end
  return sg
end

.get(options = {}) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/kytoon/providers/openstack/server_group.rb', line 239

def self.get(options={})
  id = options[:id]
  if id.nil? then
    group=ServerGroup.most_recent
    raise NoServerGroupExists, "No server group files exist." if group.nil?
    id=group.id
  end

  out_file=File.join(@@data_dir, "#{id}.json")
  raise NoServerGroupExists, "No server group files exist." if not File.exists?(out_file)
  ServerGroup.from_json(IO.read(out_file))
end

.get_server_ipsObject



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/kytoon/providers/openstack/server_group.rb', line 371

def self.get_server_ips()

  ips = {}
  configs = Util.load_configs

  network_name = configs['openstack_network_name'] || 'public'

  conn = self.init_connection
  all_active = false
  until all_active do
    all_active = true
    conn.servers.each do |server|
      if ips[server.id].nil? then
        server = conn.servers.get(server.id)
        if server.state == 'ACTIVE' then
          addresses = server.addresses[network_name].select {|a| a['version'] == self.default_ip_type}
          ips[server.id] = addresses[0]['addr']
        else
          all_active = false
        end
      end
    end
  end

  ips

end

.index(options = {}) ⇒ Object



252
253
254
255
256
257
258
259
260
# File 'lib/kytoon/providers/openstack/server_group.rb', line 252

def self.index(options={})

  server_groups=[]
  Dir[File.join(ServerGroup.data_dir, '*.json')].each do  |file|
    server_groups << ServerGroup.from_json(IO.read(file))
  end
  server_groups

end

.init_connectionObject



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/kytoon/providers/openstack/server_group.rb', line 274

def self.init_connection
  configs = Util.load_configs
  if @@connection.nil? then
    @connection = Fog::Compute.new(
      :provider           => :openstack,
      :openstack_auth_url  => configs['openstack_url'],
      :openstack_username => configs['openstack_username'],
      :openstack_api_key => configs['openstack_password'],
      :openstack_service_name => configs['openstack_service_name'],
      :openstack_service_type => configs['openstack_service_type'],
      :openstack_region => configs['openstack_region']
    )
  else
    @@connection
  end
end

.most_recentObject



262
263
264
265
266
267
268
269
270
271
272
# File 'lib/kytoon/providers/openstack/server_group.rb', line 262

def self.most_recent
  server_groups=[]
  Dir[File.join(@@data_dir, "*.json")].each do  |file|
    server_groups << ServerGroup.from_json(IO.read(file))
  end
  if server_groups.size > 0 then
    server_groups.sort { |a,b| b.id <=> a.id }[0]
  else
    nil
  end
end

.ping_test(ip_addr) ⇒ Object



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/kytoon/providers/openstack/server_group.rb', line 399

def self.ping_test(ip_addr)

  ping_timeout = (Util.load_configs['openstack_ping_timeout'] || 60).to_i

  begin
    ping = self.default_ip_type == 6 ? 'ping6' : 'ping'
    ping_command = "#{ping} -c 1 #{ip_addr} > /dev/null 2>&1"
    Timeout::timeout(ping_timeout) do
      while(1) do
        return true if system(ping_command)
      end
    end
  rescue Timeout::Error => te
    raise KytoonException, "Timeout pinging server: #{ping_command}"
  end

  return false

end

.release_floating_ip(server) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/kytoon/providers/openstack/server_group.rb', line 343

def self.release_floating_ip(server)

  conn = self.init_connection

  address_ip = server['floating_ip']
  address_id = server['floating_ip_id']

  conn.disassociate_address(server['id'], address_ip)

  # wait for address to disassociate (instance_id should be nil)
  1.upto(30) do
    floating_ips = conn.list_all_addresses.body['floating_ips']
    break if floating_ips.detect {|f| f['id'] == address_id and f['instance_id' == nil]}
  end

  begin
    conn.release_address(address_id)
  rescue Fog::Compute::OpenStack::NotFound
    puts "Unable to release IP address #{address_ip}: Not Found."
  end

end

Instance Method Details

#cache_to_diskObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/kytoon/providers/openstack/server_group.rb', line 108

def cache_to_disk

  sg_hash = {
      'id' => @id,
      'name' => @name,
      'use_security_groups' => @use_security_groups,
      'servers' => []
  }
  @servers.each do |server|
      sg_hash['servers'] << {'id' => server['id'], 'hostname' => server['hostname'], 'image_ref' => server['image_ref'], 'gateway' => server['gateway'], 'flavor_ref' => server['flavor_ref'], 'ip_address' => server['ip_address'], 'floating_ip' => server['floating_ip'], 'floating_ip_id' => server['floating_ip_id'], 'assign_floating_ip' => server['assign_floating_ip']}
  end

  FileUtils.mkdir_p(@@data_dir)
  File.open(File.join(@@data_dir, "#{@id}.json"), 'w') do |f|
    f.chmod(0600)
    f.write(sg_hash.to_json)
  end
end

#deleteObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/kytoon/providers/openstack/server_group.rb', line 127

def delete
  servers.each do |server|
    if server['assign_floating_ip'] == 'true' then
      ServerGroup.release_floating_ip(server)
    end
    ServerGroup.destroy_instance(server['id'])
  end

  #cleanup ssh keys
  private_ssh_key = File.join(@@data_dir, "#{@id}_id_rsa")
  public_ssh_key = File.join(@@data_dir, "#{@id}_id_rsa.pub")
  [private_ssh_key, public_ssh_key].each do |file|
    File.delete(file) if File.exists?(file)
  end

  out_file=File.join(@@data_dir, "#{@id}.json")
  File.delete(out_file) if File.exists?(out_file)
end

#gateway_ipObject



46
47
48
# File 'lib/kytoon/providers/openstack/server_group.rb', line 46

def gateway_ip
  @servers.select {|s| s['gateway'] == 'true' }[0]['ip_address'] if @servers.size > 0
end

#pretty_printObject



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/kytoon/providers/openstack/server_group.rb', line 79

def pretty_print

  puts "Group ID: #{@id}"
  puts "name: #{@name}"
  puts "gateway IP: #{self.gateway_ip}"
  puts "Servers:"
  servers.each do |server|
    puts "\tname: #{server['hostname']}"
    puts "\t--"
  end

end

#server(name) ⇒ Object



38
39
40
# File 'lib/kytoon/providers/openstack/server_group.rb', line 38

def server(name)
  @servers.select {|s| s['hostname'] == name}[0] if @servers.size > 0
end

#server_namesObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/kytoon/providers/openstack/server_group.rb', line 92

def server_names

  names=[]

  servers.each do |server|
    if block_given? then
      yield server['hostname']
    else
      names << server['hostname']
    end  
  end

  names
  
end

#serversObject



42
43
44
# File 'lib/kytoon/providers/openstack/server_group.rb', line 42

def servers
  @servers
end