#!/usr/bin/env python # coding: utf8 from lxml import etree from lxml import objectify from libvirt import * class StorageFormatter(): def formatBytes(self, num): size = float(num) SUFFIXES = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] multiple = 1024 for suffix in SUFFIXES: size /= multiple if size < multiple: return '{0:.2f} {1}'.format(size, suffix) class Pool(): def __init__(self, XMLSource=None): self.pool_type = None self.name = None self.uuid = None self.allocation = None self.capacity = None self.available = None if XMLSource: poolInfo = objectify.fromstring(XMLSource) self.pool_type = poolInfo.get('type') if hasattr( poolInfo, 'name'): self.name = poolInfo.name if hasattr( poolInfo, 'uuid'): self.uuid = poolInfo.uuid if hasattr( poolInfo, 'allocation'): self.allocation = poolInfo.allocation if hasattr( poolInfo, 'capacity'): self.capacity = poolInfo.capacity if hasattr( poolInfo, 'available'): self.available = poolInfo.available def xml( self, myElementMaker ): poolInfo = myElementMaker.pool() if self.pool_type: poolInfo.set('type', self.pool_type) if self.name: poolInfo.name = myElementMaker.name( self.name ) if self.uuid: poolInfo.uuid = myElementMaker.uuid( self.uuid ) return poolInfo def createXml(self): myElementMaker = objectify.ElementMaker(annotate=False,namespace=None, nsmap={}) return etree.tostring(self.xml(myElementMaker)) class DirectoryPool(Pool): def __init__(self, XMLSource=None): Pool.__init__(self, XMLSource) self.target_path = None if XMLSource: poolInfo = objectify.fromstring(XMLSource) if hasattr( poolInfo.target, 'path' ): self.target_path = poolInfo.target.path self.pool_type = 'dir' def xml( self, myElementMaker ): poolInfo = Pool.xml( self, myElementMaker ) poolInfo.target = myElementMaker.target() if self.target_path: poolInfo.target.path = myElementMaker.path( self.target_path ) return poolInfo class FsPool(DirectoryPool): def __init__(self, XMLSource=None): DirectoryPool.__init__(self, XMLSource) self.source_path = None self.target_format = None if XMLSource: poolInfo = objectify.fromstring(XMLSource) if hasattr( poolInfo.source, 'device' ): self.source_path = poolInfo.source.device.get('path') if hasattr( poolInfo.target, 'format'): self.target_format = poolInfo.target.format.get('type') self.pool_type = 'fs' def xml( self, myElementMaker ): poolInfo = DirectoryPool.xml(self, myElementMaker ) poolInfo.source = myElementMaker.source() if self.source_path: poolInfo.source.device = myElementMaker.device() poolInfo.source.device.set('path', self.source_path) if self.target_format: poolInfo.target.format = myElementMaker.format() poolInfo.target.format.set('type', self.target_format) return poolInfo def fsList(self): return ['auto','ext2','ext3','ext4','ufs','iso9660','udf','gfs','gfs2','vfat','hfs+','xfs' ] class NetFsPool(DirectoryPool): def __init__(self, XMLSource=None): DirectoryPool.__init__(self, XMLSource) self.source_path = None self.source_host = None self.source_format = 'auto' if XMLSource: poolInfo = objectify.fromstring(XMLSource) if hasattr( poolInfo.source, 'dir'): self.source_path = poolInfo.source.dir.get('path') if hasattr( poolInfo.source, 'host'): self.source_host = poolInfo.source.host.get('name') if hasattr( poolInfo.source, 'format'): self.source_format = poolInfo.source.format.get('type') self.pool_type = 'netfs' def xml( self, myElementMaker ): poolInfo = DirectoryPool.xml( self, myElementMaker ) poolInfo.source = myElementMaker.source() if self.source_path: poolInfo.source.dir = myElementMaker.dir() poolInfo.source.dir.set('path', self.source_path) if self.source_format: poolInfo.source.format = myElementMaker.format() poolInfo.source.format.set('type', self.source_format) if self.source_host: poolInfo.source.host = myElementMaker.host() poolInfo.source.host.set('name', self.source_host) return poolInfo def fsList(self): return ['auto','nfs'] class IscsiPool(FsPool): def __init__(self, XMLSource=None): FsPool.__init__(self, XMLSource) self.source_port = None self.source_host = None if XMLSource: poolInfo = objectify.fromstring(XMLSource) if hasattr( poolInfo.source, 'port'): self.source_port = poolInfo.source.port.get('name') if hasattr( poolInfo.source, 'host'): self.source_host = poolInfo.source.host.get('name') self.pool_type = 'iscsi' def xml( self, myElementMaker ): poolInfo = FsPool.xml( self, myElementMaker ) poolInfo.source = myElementMaker.source() if self.source_port: poolInfo.source.port = myElementMaker.device() poolInfo.source.port.set('path', self.source_port) if self.source_host: poolInfo.source.host = myElementMaker.host() poolInfo.source.host.set('name', self.source_host) return poolInfo def fsList(self): return [] class PoolFactory(): def getPool( self, XMLSource ): poolInfo = objectify.fromstring(XMLSource) if poolInfo.get('type') == 'dir': return DirectoryPool(XMLSource) elif poolInfo.get('type') == 'fs': return FsPool(XMLSource) elif poolInfo.get('type') == 'netfs': return NetFsPool(XMLSource) elif poolInfo.get('type') == 'iscsi': return IscsiPool(XMLSource) else: return None class Volume(): def __init__(self, XMLSource=None): self.name = None self.format = None self.path = None self.backing_store_path = None self.backing_store_format = None self.capacity = None self.allocation = None if XMLSource: volInfo = objectify.fromstring(XMLSource) if hasattr( volInfo, 'name'): self.name = volInfo.name if hasattr( volInfo, 'capacity'): self.capacity = volInfo.capacity if hasattr( volInfo, 'allocation'): self.allocation = volInfo.allocation if hasattr( volInfo, 'target'): if hasattr( volInfo.target, 'format'): self.format = volInfo.target.format.get('type') if hasattr( volInfo.target, 'path'): self.path = volInfo.target.path if hasattr( volInfo, 'backingStore'): if hasattr( volInfo.backingStore, 'path'): self.backing_store_path = volInfo.backingStore.path if hasattr( volInfo.backingStore, 'format'): self.backing_store_format = volInfo.backingStore.format.get('type') def xml( self ): myElementMaker = objectify.ElementMaker(annotate=False,namespace=None, nsmap={}) volInfo = myElementMaker.volume() if self.name: volInfo.name = myElementMaker.name( self.name ) if self.format: volInfo.target = myElementMaker.target() volInfo.target.format = myElementMaker.format() volInfo.target.format.set('type', self.format) if self.backing_store_path or self.backing_store_format: volInfo.backingStore = myElementMaker.backingStore() if self.backing_store_path: volInfo.backingStore.path = myElementMaker.path( self.backing_store_path ) if self.backing_store_format: volInfo.backingStore.format = myElementMaker.format() volInfo.backingStore.format.set('type', self.backing_store_format) if self.capacity: volInfo.capacity = myElementMaker.capacity( self.capacity ) if self.allocation: volInfo.allocation = myElementMaker.allocation( self.allocation ) return etree.tostring(volInfo)