Class: Kytoon::Providers::Libvirt::ServerGroup
- Inherits:
-
Object
- Object
- Kytoon::Providers::Libvirt::ServerGroup
- Defined in:
- lib/kytoon/providers/libvirt/server_group.rb
Overview
2) Generate an ssh keypair to be injected into the image.
Constant Summary collapse
- KIB_PER_GIG =
1048576
- CONFIG_FILE =
KYTOON_PROJECT + File::SEPARATOR + "config" + File::SEPARATOR + "server_group.json"
- @@data_dir =
File.join(KYTOON_PROJECT, "tmp", "libvirt")
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
-
#use_sudo ⇒ Object
Returns the value of attribute use_sudo.
Class Method Summary collapse
- .cleanup_instances(group_id, inst_name, disk_path, sudo) ⇒ Object
- .create(sg) ⇒ Object
- .create_instance(group_id, inst_name, memory_gigs, original, original_xml, disk_path, create_cow, ssh_public_key, sudo) ⇒ Object
- .data_dir ⇒ Object
- .data_dir=(dir) ⇒ Object
- .default_ip_type ⇒ Object
-
.from_json(json) ⇒ Object
generate a Server Group XML from server_group.json.
- .get(options = {}) ⇒ Object
- .index(options = {}) ⇒ Object
- .most_recent ⇒ Object
- .ping_test(ip_addr) ⇒ Object
-
.source_disk_filename(original, original_xml) ⇒ Object
Determine the path of the source disk to be used.
Instance Method Summary collapse
- #cache_to_disk ⇒ Object
- #delete ⇒ Object
- #gateway_ip ⇒ Object
-
#initialize(options = {}) ⇒ ServerGroup
constructor
A new instance of ServerGroup.
- #pretty_print ⇒ Object
- #server(name) ⇒ Object
- #server_names ⇒ Object
- #servers ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ ServerGroup
Returns a new instance of ServerGroup.
39 40 41 42 43 44 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 39 def initialize(={}) @id = [:id] || Time.now.to_f @name = [:name] @use_sudo = [:use_sudo] @servers=[] end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
35 36 37 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 35 def id @id end |
#name ⇒ Object
Returns the value of attribute name.
36 37 38 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 36 def name @name end |
#use_sudo ⇒ Object
Returns the value of attribute use_sudo.
37 38 39 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 37 def use_sudo @use_sudo end |
Class Method Details
.cleanup_instances(group_id, inst_name, disk_path, sudo) ⇒ Object
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 390 def self.cleanup_instances(group_id, inst_name, disk_path, sudo) domain_name="#{group_id}_#{inst_name}" out = %x{ if [ -n "$DEBUG" ]; then set -x fi export VIRSH_DEFAULT_CONNECT_URI="qemu:///system" if #{sudo} virsh dumpxml #{domain_name} &> /dev/null; then #{sudo} virsh destroy "#{domain_name}" &> /dev/null #{sudo} virsh undefine "#{domain_name}" fi # If we used --preserve-data there will be no volume... ignore it #{sudo} virsh vol-delete --pool default "#{group_id}_#{inst_name}.img" &> /dev/null if [ -f "#{disk_path}" ]; then #{sudo} rm -f "#{disk_path}" fi } puts out retval=$? if not retval.success? puts out raise KytoonException, "Failed to cleanup instances." end end |
.create(sg) ⇒ Object
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 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 151 def self.create(sg) ssh_public_key = Kytoon::Util.load_public_key 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") sudo = sg.use_sudo =~ /(true|t|yes|y|1)$/i ? "sudo" : "" hosts_file_data = "127.0.0.1\tlocalhost localhost.localdomain\n" sg.servers.each do |server| image_dir=server['image_dir'] || '/var/lib/libvirt/images' disk_path=File.join(image_dir, "#{sg.id}_#{server['hostname']}.img") server['disk_path'] = disk_path instance_ip = create_instance(sg.id, server['hostname'], server['memory'], server['original'], server['original_xml'], disk_path, server['create_cow'], ssh_public_key, sudo) server['ip_address'] = instance_ip hosts_file_data += "#{instance_ip}\t#{server['hostname']}\n" sg.cache_to_disk 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']) 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, inst_name, memory_gigs, original, original_xml, disk_path, create_cow, ssh_public_key, sudo) ⇒ Object
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 297 def self.create_instance(group_id, inst_name, memory_gigs, original, original_xml, disk_path, create_cow, ssh_public_key, sudo) puts "Creating instance: #{inst_name}" instance_memory = (KIB_PER_GIG * memory_gigs.to_f).to_i original_disk_path = source_disk_filename(original, original_xml) #cow only domain_name="#{group_id}_#{inst_name}" out = %x{ if [ -n "$DEBUG" ]; then set -x fi export VIRSH_DEFAULT_CONNECT_URI="qemu:///system" if [ -n "#{original_xml}" ]; then ORIGIN="--original-xml #{original_xml}" elif [ -n "#{original}" ]; then ORIGIN="--original #{original}" else { echo "Please specify 'original' or 'original_xml'."; exit 1; } fi if [ -n "#{create_cow}" ]; then #{sudo} virt-clone --connect="$VIRSH_DEFAULT_CONNECT_URI" \ --name '#{domain_name}' \ --file '#{disk_path}' \ --force \ $ORIGIN \ --preserve-data \ || { echo "failed to virt-clone"; exit 1; } #{sudo} qemu-img create -f qcow2 -o backing_file=#{original_disk_path} "#{disk_path}" else #{sudo} virt-clone --connect="$VIRSH_DEFAULT_CONNECT_URI" \ --name '#{domain_name}' \ --file '#{disk_path}' \ --force \ $ORIGIN \ || { echo "failed to virt-clone"; exit 1; } fi LV_ROOT=$(#{sudo} virt-filesystems -a #{disk_path} --logical-volumes | grep root) # If using LVM we inject the ssh key this way if [ -n "$LV_ROOT" ]; then #{sudo} guestfish --selinux add #{disk_path} : \ run : \ mount $LV_ROOT / : \ sh "/bin/mkdir -p /root/.ssh" : \ write-append /root/.ssh/authorized_keys "#{ssh_public_key}\n" : \ sh "/bin/chmod -R 700 /root/.ssh" fi #{sudo} virsh setmaxmem #{domain_name} #{instance_memory} #{sudo} virsh start #{domain_name} #{sudo} virsh setmem #{domain_name} #{instance_memory} } retval=$? if not retval.success? puts out raise KytoonException, "Failed to create instance #{inst_name}." end # lookup server IP here... mac_addr = nil network_name = nil dom_xml = %x{#{sudo} virsh --connect=qemu:///system dumpxml #{domain_name}} dom = REXML::Document.new(dom_xml) REXML::XPath.each(dom, "//interface/mac") do |interface_xml| mac_addr = interface_xml.attributes['address'] end raise KytoonException, "Failed to lookup mac address for #{inst_name}" if mac_addr.nil? REXML::XPath.each(dom, "//interface/source") do |interface_xml| network_name = interface_xml.attributes['network'] end raise KytoonException, "Failed to lookup network name for #{inst_name}" if network_name.nil? instance_ip = %x{grep -i #{mac_addr} /var/lib/libvirt/dnsmasq/#{network_name}.leases | cut -d " " -f 3}.chomp count = 0 until not instance_ip.empty? do instance_ip = %x{grep -i #{mac_addr} /var/lib/libvirt/dnsmasq/#{network_name}.leases | cut -d " " -f 3}.chomp sleep 1 count += 1 if count >= 60 then raise KytoonException, "Failed to lookup ip address for #{inst_name}" end end return instance_ip end |
.data_dir ⇒ Object
25 26 27 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 25 def self.data_dir @@data_dir end |
.data_dir=(dir) ⇒ Object
29 30 31 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 29 def self.data_dir=(dir) @@data_dir=dir end |
.default_ip_type ⇒ Object
224 225 226 227 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 224 def self.default_ip_type() ip_type = Util.load_configs['libvirt_ip_type'] || 4 ip_type.to_i end |
.from_json(json) ⇒ Object
generate a Server Group XML from server_group.json
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 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 59 def self.from_json(json) json_hash=JSON.parse(json) configs = Util.load_configs use_sudo = ENV['LIBVIRT_USE_SUDO'] || configs['libvirt_use_sudo'].to_s sg=ServerGroup.new( :id => json_hash["id"], :name => json_hash["name"], :use_sudo => use_sudo ) json_hash["servers"].each do |server_hash| sg.servers << { 'hostname' => server_hash['hostname'], 'memory' => server_hash['memory'], 'original' => server_hash['original'], 'original_xml' => server_hash['original_xml'], 'create_cow' => server_hash['create_cow'], 'disk_path' => server_hash['disk_path'], 'ip_address' => server_hash['ip_address'], 'gateway' => server_hash['gateway'] || "false" } end return sg end |
.get(options = {}) ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 249 def self.get(={}) id = [: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 |
.index(options = {}) ⇒ Object
262 263 264 265 266 267 268 269 270 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 262 def self.index(={}) server_groups=[] Dir[File.join(ServerGroup.data_dir, '*.json')].each do |file| server_groups << ServerGroup.from_json(IO.read(file)) end server_groups end |
.most_recent ⇒ Object
272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 272 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
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 229 def self.ping_test(ip_addr) ping_timeout = (Util.load_configs['libvirt_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 |
.source_disk_filename(original, original_xml) ⇒ Object
Determine the path of the source disk to be used
285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 285 def self.source_disk_filename(original, original_xml) if original and not original.empty? then dom = REXML::Document.new(%x{virsh dumpxml nova1}) else dom = REXML::Document.new(IO.read(original_xml)) end REXML::XPath.each(dom, "//disk[1]/source") do |source_xml| return source_xml.attributes['file'] end raise KytoonException, "Unable to find disk path for instance." end |
Instance Method Details
#cache_to_disk ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 116 def cache_to_disk sg_hash = { 'id' => @id, 'name' => @name, 'servers' => [] } @servers.each do |server| sg_hash['servers'] << {'hostname' => server['hostname'], 'memory' => server['memory'], 'gateway' => server['gateway'], 'original' => server['original'], 'original_xml' => server['original_xml'], 'create_cow' => server['create_cow'], 'disk_path' => server['disk_path'], 'ip_address' => server['ip_address']} 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 |
#delete ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 134 def delete sudo = @use_sudo =~ /(true|t|yes|y|1)$/i ? "sudo" : "" servers.each do |server| ServerGroup.cleanup_instances(@id, server['hostname'], server['disk_path'], sudo) end out_file=File.join(@@data_dir, "#{@id}.json") File.delete(out_file) if File.exists?(out_file) #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 end |
#gateway_ip ⇒ Object
54 55 56 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 54 def gateway_ip @servers.select {|s| s['gateway'] == 'true' }[0]['ip_address'] if @servers.size > 0 end |
#pretty_print ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 87 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
46 47 48 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 46 def server(name) @servers.select {|s| s['hostname'] == name}[0] if @servers.size > 0 end |
#server_names ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 100 def server_names names=[] servers.each do |server| if block_given? then yield server['hostname'] else names << server['hostname'] end end names end |
#servers ⇒ Object
50 51 52 |
# File 'lib/kytoon/providers/libvirt/server_group.rb', line 50 def servers @servers end |