Class: Gogetit::GogetLibvirt
- Inherits:
-
Object
- Object
- Gogetit::GogetLibvirt
- Includes:
- Util
- Defined in:
- lib/providers/libvirt.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#maas ⇒ Object
readonly
Returns the value of attribute maas.
Instance Method Summary collapse
- #add_nic(document, nic_conf) ⇒ Object
- #configure_interfaces(ifaces, system_id) ⇒ Object
- #create(name, options = nil) ⇒ Object
- #create_volume(pool_name, volume_doc) ⇒ Object
- #define_domain(domain) ⇒ Object
- #define_volumes(document, domain) ⇒ Object
- #deploy(name, options = nil) ⇒ Object
- #destroy(name) ⇒ Object
- #domain_exists?(name) ⇒ Boolean
- #generate_nics(ifaces, domain) ⇒ Object
- #get_domain_list ⇒ Object
- #get_domain_xml(domain_name) ⇒ Object
- #get_mac_addr(domain_name) ⇒ Object
- #get_pool_path(pool) ⇒ Object
-
#initialize(conf, maas, logger) ⇒ GogetLibvirt
constructor
A new instance of GogetLibvirt.
- #print_xml(doc) ⇒ Object
- #release(name) ⇒ Object
Methods included from Util
#check_ip_available, #get_gateway, #get_http_content, #get_provider_of, #is_port_open?, #knife_bootstrap_chef, #knife_bootstrap_zero, #knife_remove, #ping_available?, #run_command, #run_through_ssh, #ssh_available?, #update_databags, #wait_until_available
Constructor Details
#initialize(conf, maas, logger) ⇒ GogetLibvirt
Returns a new instance of GogetLibvirt.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/providers/libvirt.rb', line 13 def initialize(conf, maas, logger) @config = conf begin @conn = Libvirt::open(config[:libvirt][:nodes][0][:url]) rescue Exception => e puts e abort("Unable to establish connection with"\ " #{config[:libvirt][:nodes][0][:url]}") end @maas = maas @logger = logger end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
11 12 13 |
# File 'lib/providers/libvirt.rb', line 11 def config @config end |
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
11 12 13 |
# File 'lib/providers/libvirt.rb', line 11 def conn @conn end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
11 12 13 |
# File 'lib/providers/libvirt.rb', line 11 def logger @logger end |
#maas ⇒ Object (readonly)
Returns the value of attribute maas.
11 12 13 |
# File 'lib/providers/libvirt.rb', line 11 def maas @maas end |
Instance Method Details
#add_nic(document, nic_conf) ⇒ Object
446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
# File 'lib/providers/libvirt.rb', line 446 def add_nic(document, nic_conf) logger.info("Calling <#{__method__.to_s}>") template = File.read(config[:lib_dir] + "/template/nic.xml") doc = Oga.parse_xml(template) nic_conf.each do |nic| doc = Oga.parse_xml(template) doc.at_xpath('interface/source').attribute('network').value = nic[:network] doc.at_xpath('interface/source').attribute('portgroup').value = nic[:portgroup] document.at_xpath('domain/devices').children << doc.at_xpath('interface') end document end |
#configure_interfaces(ifaces, system_id) ⇒ Object
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 |
# File 'lib/providers/libvirt.rb', line 104 def configure_interfaces(ifaces, system_id) # It assumes you only have a physical interfaces. interfaces = maas.interfaces([system_id]) maas.interfaces( [system_id, interfaces[0]['id']], { 'op' => 'unlink_subnet', 'id' => interfaces[0]['links'][0]['id'] } ) # VLAN configuration ifaces.each_with_index do |iface,index| if index == 0 params = { 'op' => 'link_subnet', 'mode' => 'STATIC', 'subnet' => ifaces[0]['id'], 'ip_address' => ifaces[0]['ip'], 'default_gateway' => 'True', 'force' => 'False' } maas.interfaces([system_id, interfaces[0]['id']], params) elsif index > 0 params = { 'op' => 'create_vlan', 'vlan' => iface['vlan']['id'], 'parent' => interfaces[0]['id'] } maas.interfaces([system_id], params) interfaces = maas.interfaces([system_id]) params = { 'op' => 'link_subnet', 'mode' => 'STATIC', 'subnet' => ifaces[index]['id'], 'ip_address' => ifaces[index]['ip'], 'default_gateway' => 'False', 'force' => 'False' } maas.interfaces([system_id, interfaces[index]['id']], params) end end end |
#create(name, options = nil) ⇒ Object
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 |
# File 'lib/providers/libvirt.rb', line 154 def create(name, = nil) logger.info("Calling <#{__method__.to_s}>") abort("Domain #{name} already exists!"\ " Please check both on MAAS and libvirt.") \ if maas.domain_name_exists?(name) or domain_exists?(name) domain = config[:libvirt][:specs][:default] ifaces = nil if ['ipaddresses'] ifaces = check_ip_available(['ipaddresses'], maas) domain = generate_nics(ifaces, domain) elsif ['vlans'] #check_vlan_available(options['vlans']) else domain[:nic] = [ { network: config[:default][:root_bridge], portgroup: config[:default][:root_bridge] } ] end domain[:name] = name domain[:uuid] = SecureRandom.uuid dom = conn.define_domain_xml(define_domain(domain)) maas.refresh_pods system_id = maas.get_system_id(domain[:name]) maas.wait_until_state(system_id, 'Ready') if ['ipaddresses'] configure_interfaces(ifaces, system_id) elsif ['vlans'] #check_vlan_available(options['vlans']) else end logger.info("Calling to deploy...") distro = nil if ['distro'].nil? or ['distro'].empty? distro = 'xenial' else distro = ['distro'] end maas.conn.request(:post, ['machines', system_id], \ {'op' => 'deploy', 'distro_series' => distro}) maas.wait_until_state(system_id, 'Deployed') fqdn = name + '.' + maas.get_domain distro_name = maas.get_distro_name(system_id) wait_until_available(fqdn, distro_name) # To enable serial console to use 'virsh console' if distro_name == 'ubuntu' commands = [ 'sudo systemctl enable [email protected]', 'sudo systemctl start [email protected]' ] run_through_ssh(fqdn, distro_name, commands) end logger.info("#{domain[:name]} has been created.") puts "ssh #{distro_name}@#{name}" domain[:default_user] = distro_name { result: true, info: domain } end |
#create_volume(pool_name, volume_doc) ⇒ Object
439 440 441 442 443 444 |
# File 'lib/providers/libvirt.rb', line 439 def create_volume(pool_name, volume_doc) logger.info("Calling <#{__method__.to_s}> to create volume in #{pool_name} pool.") pool = conn.lookup_storage_pool_by_name(pool_name) pool.create_volume_xml(volume_doc) pool.refresh end |
#define_domain(domain) ⇒ Object
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 |
# File 'lib/providers/libvirt.rb', line 337 def define_domain(domain) logger.info("Calling <#{__method__.to_s}>") template = File.read(config[:lib_dir] + '/template/domain.xml') doc = Oga.parse_xml(template) name = domain[:name] doc.at_xpath('domain/name').inner_text = name uuid = domain[:uuid] doc.at_xpath('domain/uuid').inner_text = uuid vcpu = domain[:vcpu].to_s doc.at_xpath('domain/vcpu').inner_text = vcpu memory = domain[:memory].to_s doc.at_xpath('domain/memory').inner_text = memory doc.at_xpath('domain/currentMemory').inner_text = memory doc = define_volumes(doc, domain) doc = add_nic(doc, domain[:nic]) # print_xml(doc) # volumes.each do |v| # print_xml(v) # end return Oga::XML::Generator.new(doc).to_xml end |
#define_volumes(document, domain) ⇒ Object
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
# File 'lib/providers/libvirt.rb', line 388 def define_volumes(document, domain) logger.info("Calling <#{__method__.to_s}>") disk_template = File.read(config[:lib_dir] + '/template/disk.xml') disk_doc = Oga.parse_xml(disk_template) volume_template = File.read(config[:lib_dir] + '/template/volume.xml') volume_doc = Oga.parse_xml(volume_template) defined_volumes = [] # For root volume pool_path = get_pool_path(domain[:disk][:root][:pool]) volume_name = "#{domain[:name]}_root_sda.qcow2" volume_file = pool_path + "/" + volume_name disk_doc.at_xpath('disk/source').attribute('file').value = volume_file document.at_xpath('domain/devices').children << disk_doc.at_xpath('disk') volume_doc.at_xpath('volume/name').inner_text = volume_name volume_doc.at_xpath('volume/target/path').inner_text = volume_file volume_doc.at_xpath('volume/capacity').inner_text = \ domain[:disk][:root][:capacity].to_s create_volume(domain[:disk][:root][:pool], \ Oga::XML::Generator.new(volume_doc).to_xml) defined_volumes << volume_doc # For data(secondary) volumes if domain[:disk][:data] != [] and domain[:disk][:data] != nil disk_index = 98 domain[:disk][:data].each do |v| pool_path = get_pool_path(v[:pool]) volume_index = "sd" + disk_index.chr volume_name = "#{domain[:name]}_data_#{volume_index}.qcow2" volume_file = pool_path + "/" + volume_name disk_doc = Oga.parse_xml(disk_template) disk_doc.at_xpath('disk/source').attribute('file').value = volume_file disk_doc.at_xpath('disk/target').attribute('dev').value = volume_index document.at_xpath('domain/devices').children << disk_doc.at_xpath('disk') volume_doc = Oga.parse_xml(volume_template) volume_doc.at_xpath('volume/name').inner_text = volume_name volume_doc.at_xpath('volume/target/path').inner_text = volume_file volume_doc.at_xpath('volume/capacity').inner_text = v[:capacity].to_s create_volume(v[:pool], Oga::XML::Generator.new(volume_doc).to_xml) defined_volumes << volume_doc disk_index += 1 end end return document end |
#deploy(name, options = nil) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/providers/libvirt.rb', line 271 def deploy(name, = nil) logger.info("Calling <#{__method__.to_s}>") abort("The machine, '#{name}', doesn't exist.") \ unless maas.machine_exists?(name) system_id = maas.get_system_id(name) maas.wait_until_state(system_id, 'Ready') logger.info("Calling to deploy...") distro = nil if ['distro'].nil? or ['distro'].empty? distro = 'xenial' else distro = ['distro'] end maas.conn.request(:post, ['machines', system_id], \ {'op' => 'deploy', 'distro_series' => distro}) maas.wait_until_state(system_id, 'Deployed') fqdn = name + '.' + maas.get_domain distro_name = maas.get_distro_name(system_id) wait_until_available(fqdn, distro_name) # To enable serial console to use 'virsh console' if distro_name == 'ubuntu' commands = [ 'sudo systemctl enable [email protected]', 'sudo systemctl start [email protected]' ] run_through_ssh(fqdn, distro_name, commands) end logger.info("#{name} has been created.") puts "ssh #{distro_name}@#{name}" info = {} info[:distro] = distro info[:default_user] = distro_name { result: true, info: info } end |
#destroy(name) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/providers/libvirt.rb', line 227 def destroy(name) logger.info("Calling <#{__method__.to_s}>") system_id = maas.get_system_id(name) info = {} info[:machine] = \ maas.conn.request(:get, ['machines', system_id]) if maas.machine_exists?(name) if maas.get_machine_state(system_id) == 'Deployed' logger.info("Calling to release...") maas.conn.request(:post, ['machines', system_id], {'op' => 'release'}) maas.wait_until_state(system_id, 'Ready') end maas.conn.request(:delete, ['machines', system_id]) end pools = [] conn.list_storage_pools.each do |name| pools << self.conn.lookup_storage_pool_by_name(name) end dom = conn.lookup_domain_by_name(name) info[:domain_xml] = dom.xml_desc dom.destroy if dom.active? Oga.parse_xml(dom.xml_desc).xpath('domain/devices/disk/source').each do |d| pool_path = d.attribute('file').value.split('/')[0..2].join('/') pools.each do |p| if Oga.parse_xml(p.xml_desc).at_xpath('pool/target/path')\ .inner_text == pool_path logger.info("Deleting volume in #{p.name} pool.") p.lookup_volume_by_name(d.attribute('file').value.split('/')[3]).delete end end end dom.undefine logger.info("#{name} has been destroyed.") { result: true, info: info } end |
#domain_exists?(name) ⇒ Boolean
37 38 39 40 41 42 43 |
# File 'lib/providers/libvirt.rb', line 37 def domain_exists?(name) logger.info("Calling <#{__method__.to_s}>") get_domain_list.each do |d| return true if d == name end false end |
#generate_nics(ifaces, domain) ⇒ Object
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 |
# File 'lib/providers/libvirt.rb', line 58 def generate_nics(ifaces, domain) abort("There is no dns server specified for the gateway network.") \ unless ifaces[0]['dns_servers'][0] abort("There is no gateway specified for the gateway network.") \ unless ifaces[0]['gateway_ip'] # It seems the first IP has to belong to the untagged VLAN in the Fabric. abort("The first IP you entered does not belong to the untagged"\ " VLAN in the Fabric.") \ unless ifaces[0]['vlan']['name'] == 'untagged' domain[:ifaces] = ifaces domain[:nic] = [] ifaces.each_with_index do |iface,index| if index == 0 if iface['vlan']['name'] == 'untagged' nic = { network: config[:default][:root_bridge], portgroup: config[:default][:root_bridge] } elsif iface['vlan']['name'] != 'untagged' nic = { network: config[:default][:root_bridge], portgroup: config[:default][:root_bridge] + '-' + \ iface['vlan']['vid'].to_s } end domain[:nic].push(nic) elsif index > 0 # Only if the fisrt interface has untagged VLAN, # it will be configured with VLANs. # This will not be hit as of now and might be deprecated. if ifaces[0]['vlan']['name'] != 'untagged' nic = { network: config[:default][:root_bridge], portgroup: config[:default][:root_bridge] + '-' + \ iface['vlan']['vid'].to_s } domain[:nic].push(nic) end end end return domain end |
#get_domain_list ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/providers/libvirt.rb', line 28 def get_domain_list logger.info("Calling <#{__method__.to_s}>") domains = [] conn.list_all_domains.each do |d| domains << d.name end domains end |
#get_domain_xml(domain_name) ⇒ Object
53 54 55 56 |
# File 'lib/providers/libvirt.rb', line 53 def get_domain_xml(domain_name) logger.info("Calling <#{__method__.to_s}>") Oga.parse_xml(conn.lookup_domain_by_name(domain_name).xml_desc) end |
#get_mac_addr(domain_name) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/providers/libvirt.rb', line 45 def get_mac_addr(domain_name) logger.info("Calling <#{__method__.to_s}>") Oga.parse_xml(conn.lookup_domain_by_name(domain_name).xml_desc) .at_xpath('domain/devices/interface[1]/mac') .attribute('address') .value end |
#get_pool_path(pool) ⇒ Object
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/providers/libvirt.rb', line 371 def get_pool_path(pool) logger.info("Calling <#{__method__.to_s}>") path = nil conn.list_all_storage_pools.each do |p| if p.name == pool pool_doc = Oga.parse_xml(p.xml_desc) path = pool_doc.at_xpath('pool/target/path').inner_text end end if path return path else raise 'No such pool found.' end end |
#print_xml(doc) ⇒ Object
363 364 365 366 367 368 369 |
# File 'lib/providers/libvirt.rb', line 363 def print_xml(doc) logger.info("Calling <#{__method__.to_s}>") output = REXML::Document.new(Oga::XML::Generator.new(doc).to_xml) formatter = REXML::Formatters::Pretty.new formatter.compact = true formatter.write(output, $stdout) end |
#release(name) ⇒ Object
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/providers/libvirt.rb', line 315 def release(name) logger.info("Calling <#{__method__.to_s}>") system_id = maas.get_system_id(name) info = {} info[:machine] = \ maas.conn.request(:get, ['machines', system_id]) if maas.machine_exists?(name) if maas.get_machine_state(system_id) == 'Deployed' logger.info("Calling to release...") maas.conn.request(:post, ['machines', system_id], {'op' => 'release'}) maas.wait_until_state(system_id, 'Ready') end end logger.info("#{name} has been released.") { result: true, info: info } end |