-
Notifications
You must be signed in to change notification settings - Fork 0
/
openstack_handler.rb
186 lines (148 loc) · 5.33 KB
/
openstack_handler.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
135
136
137
138
139
140
141
142
143
144
145
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
require 'rubygems'
require 'openstack'
require 'retryable'
class OpenstackHandler
@@counter = 0
@@os = nil
@@allservers = []
def self.counter
return @@counter
end
def self.init_client
retryable(:tries => 5, :sleep => 2, :on => [OpenStack::Exception::Other, OpenStack::Exception::BadRequest]) do
@@os = OpenStack::Connection.create({:username => "cream",
:api_key=>"cream",
:auth_url => "http://192.168.124.81:5000/v1.1/",
:authtenant_name =>"scc-61",
:is_debug => ScalerConfig.debug_openstack})
end
if @@allservers == []
servers = []
retryable(:tries => 5, :sleep => 2, :on => [OpenStack::Exception::Other, OpenStack::Exception::BadRequest]) do
servers = @@os.servers
end
ids = []
servers.each do |server|
if server[:name] =~ /^vm-wn-.*/
ids << server[:id]
end
end
ids.each do |id|
retryable(:tries => 5, :sleep => 2, :on => [OpenStack::Exception::Other, OpenStack::Exception::BadRequest]) do
vm = @@os.get_server(id)
@@allservers << {:vm_ref => vm, :address => vm.addresses.first.address, :fqdn => vm.name + ".grid.auth.gr", :name => vm.name}
end
end
end
if ScalerConfig.debug
p "Current servers from init:"
p @@allservers
end
end
def self.create_vms(n)
# Create n new servers.
newservers = []
begin
n.times do |counter|
retryable(:tries => 3, :sleep => 2, :on => [OpenStack::Exception::Other, OpenStack::Exception::BadRequest]) do
newservers << @@os.create_server(:name => "vm-wn-#{@@counter}", :imageRef => ScalerConfig.image_id, :flavorRef => ScalerConfig.flavor_id, :security_groups => ['default', 'Torque-WN'], :key_name=>"test_key_set")
end
p "Counter = " + @@counter.to_s if ScalerConfig.debug
@@counter+=1
sleep(1)
end
rescue OpenStack::Exception::OverLimit
p "InstanceLimitExceeded: Instance quota exceeded. You cannot run any more instances of this type."
end
if ScalerConfig.debug
p "Printing new servers:"
p newservers
p newservers.collect {|n_s| n_s.name}
end
# Check if all servers are online and get IP addresses + name + fqdn in an array.
# e.g. [[10.0.0.1, vm-00.grid.auth.gr, vm-00], [10.0.0.2, vm-01.grid.auth.gr, vm-01] ...]
ip_name_fqdn_array = vms_ips(newservers)
if ScalerConfig.debug
p "ip_name_fqdn_array is :"
p ip_name_fqdn_array
end
ip_list, fqdn_list = [], []
ip_name_fqdn_array.each {|ip_name_fqdn| ip_list << ip_name_fqdn.first; fqdn_list << ip_name_fqdn[1]}
# Give some time to VMs to get up.
p "Give some time to VMs to get up." if ScalerConfig.debug
sleep(10)
# Check if yaim is finished to all vms.
VMHandler.yaim_terminated_in_each_host?(ip_list)
# Add new vms to cream files.
CreamHandler.write_to_hosts(ip_name_fqdn_array)
CreamHandler.add_wns_to_wn_list(fqdn_list)
# Restart cream services.
CreamHandler.restart_yaim!
if ScalerConfig.debug
p "Current servers:"
p @@allservers
end
end
def self.delete_vms(n)
return if n > @@allservers.count
p "We need to decrease our infrastructure!"
# Delete n servers.
deleted_servers = []
n.times do |counter|
retryable(:tries => 3, :sleep => 2, :on => OpenStack::Exception::Other) do
@@allservers.first[:vm_ref].refresh
# We do the shift after delete!, just in case delete! method fails due to network.
if @@allservers.first[:vm_ref].status == "ACTIVE"
@@allservers.first[:vm_ref].delete!
deleted_servers << @@allservers.shift
end
end
end
# Delete vms from cream files.
ip_list, fqdn_list = [], []
deleted_servers.each {|d_s| ip_list << d_s[:address]; fqdn_list << d_s[:fqdn] }
CreamHandler.delete_from_hosts(ip_list)
CreamHandler.delete_wns_from_wn_list(fqdn_list)
# Restart cream services.
CreamHandler.restart_yaim!
end
################## Private members ##################
private
def self.vms_ips(vms)
flag = true
ip_addresses = []
while flag
# Server refreshing
p "Server refreshing" if ScalerConfig.debug
vms.each do |server|
retryable(:tries => 3, :sleep => 2, :on => [OpenStack::Exception::Other, OpenStack::Exception::BadRequest]) do
server.refresh
end
end
# Check if all servers are active.
i = 0
vms.each do |vm|
if vm.status == "ACTIVE"
i+=1
end
end
if ScalerConfig.debug
p "Number of active vms:"
p i
p "Number of vms:"
p vms.count
end
if i == vms.count
flag = false
end
sleep(10)
end
# Get all ip addresses.
vms.each do |vm|
ip_addresses << [vm.addresses.first.address, vm.name + ".grid.auth.gr", vm.name]
# Save new servers.
@@allservers << {:vm_ref => vm, :address => vm.addresses.first.address, :fqdn => vm.name + ".grid.auth.gr", :name => vm.name}
end
return ip_addresses
end
end