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
-
#create(name, options = nil) ⇒ Object
subject.create(name: ‘test01’).
- #create_volume(pool_name, volume_doc) ⇒ Object
- #define_domain(domain) ⇒ Object
- #define_volumes(document, domain) ⇒ Object
- #destroy(name) ⇒ Object
- #domain_exists?(name) ⇒ Boolean
- #generate_nics(ifaces, domain) ⇒ Object
- #get_domain_list ⇒ 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
Methods included from Util
#check_ip_available, #get_gateway, #knife_bootstrap, #knife_remove, #ping_available?, #recognize_env, #ssh_available?, #symbolize_keys, #update_vault, #wait_until_available
Constructor Details
#initialize(conf, maas, logger) ⇒ GogetLibvirt
Returns a new instance of GogetLibvirt.
13 14 15 16 17 18 |
# File 'lib/providers/libvirt.rb', line 13 def initialize(conf, maas, logger) @config = conf @conn = Libvirt::open(config[:libvirt][:url]) @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
327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/providers/libvirt.rb', line 327 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 |
#create(name, options = nil) ⇒ Object
subject.create(name: ‘test01’)
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 |
# File 'lib/providers/libvirt.rb', line 89 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] if ['ipaddresses'] ifaces = check_ip_available(['ipaddresses'], maas, logger) domain = generate_nics(ifaces, domain) elsif [:vlans] #check_vlan_available(options[:vlans]) else domain[:nic] = [ { network: config[:default][:native_bridge], portgroup: config[:default][:native_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') # To configure interfaces if ['ipaddresses'] # 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 elsif [:vlans] #check_vlan_available(options[:vlans]) else end logger.info("Calling to deploy...") maas.conn.request(:post, ['machines', system_id], {'op' => 'deploy'}) maas.wait_until_state(system_id, 'Deployed') fqdn = name + '.' + maas.get_domain wait_until_available(fqdn, logger) logger.info("#{domain[:name]} has been created.") true end |
#create_volume(pool_name, volume_doc) ⇒ Object
320 321 322 323 324 325 |
# File 'lib/providers/libvirt.rb', line 320 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
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/providers/libvirt.rb', line 220 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
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 314 315 316 317 318 |
# File 'lib/providers/libvirt.rb', line 271 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 |
#destroy(name) ⇒ Object
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 |
# File 'lib/providers/libvirt.rb', line 185 def destroy(name) logger.info("Calling <#{__method__.to_s}>") system_id = maas.get_system_id(name) 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) 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 maas.refresh_pods logger.info("#{name} has been destroyed.") true end |
#domain_exists?(name) ⇒ Boolean
29 30 31 32 33 34 35 |
# File 'lib/providers/libvirt.rb', line 29 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
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 |
# File 'lib/providers/libvirt.rb', line 45 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][:native_bridge], portgroup: config[:default][:native_bridge] } elsif iface['vlan']['name'] != 'untagged' nic = { network: config[:default][:native_bridge], portgroup: config[:default][:native_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][:native_bridge], portgroup: config[:default][:native_bridge] + '-' + iface['vlan']['vid'].to_s } domain[:nic].push(nic) end end end return domain end |
#get_domain_list ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/providers/libvirt.rb', line 20 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_mac_addr(domain_name) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/providers/libvirt.rb', line 37 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
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/providers/libvirt.rb', line 254 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
246 247 248 249 250 251 252 |
# File 'lib/providers/libvirt.rb', line 246 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 |