Browse Source

Virtng module virtual function

tags/0.4
Ales Komarek 8 years ago
parent
commit
f81883307b
1 changed files with 48 additions and 48 deletions
  1. +48
    -48
      _modules/virtng.py

+ 48
- 48
_modules/virtng.py View File

def __virtual__(): def __virtual__():
if not HAS_ALL_IMPORTS: if not HAS_ALL_IMPORTS:
return False return False
return 'virt'
return 'virtng'




def __get_conn(): def __get_conn():


.. code-block:: bash .. code-block:: bash


salt '*' virt.list_vms
salt '*' virtng.list_vms
''' '''
vms = [] vms = []
vms.extend(list_active_vms()) vms.extend(list_active_vms())


.. code-block:: bash .. code-block:: bash


salt '*' virt.list_active_vms
salt '*' virtng.list_active_vms
''' '''
conn = __get_conn() conn = __get_conn()
vms = [] vms = []


.. code-block:: bash .. code-block:: bash


salt '*' virt.list_inactive_vms
salt '*' virtng.list_inactive_vms
''' '''
conn = __get_conn() conn = __get_conn()
vms = [] vms = []


.. code-block:: bash .. code-block:: bash


salt '*' virt.vm_info
salt '*' virtng.vm_info
''' '''
def _info(vm_): def _info(vm_):
dom = _get_dom(vm_) dom = _get_dom(vm_)


.. code-block:: bash .. code-block:: bash


salt '*' virt.vm_state <vm name>
salt '*' virtng.vm_state <vm name>
''' '''
def _info(vm_): def _info(vm_):
state = '' state = ''


.. code-block:: bash .. code-block:: bash


salt '*' virt.node_info
salt '*' virtng.node_info
''' '''
conn = __get_conn() conn = __get_conn()
raw = conn.getInfo() raw = conn.getInfo()


.. code-block:: bash .. code-block:: bash


salt '*' virt.get_nics <vm name>
salt '*' virtng.get_nics <vm name>
''' '''
nics = {} nics = {}
doc = minidom.parse(_StringIO(get_xml(vm_))) doc = minidom.parse(_StringIO(get_xml(vm_)))


.. code-block:: bash .. code-block:: bash


salt '*' virt.get_macs <vm name>
salt '*' virtng.get_macs <vm name>
''' '''
macs = [] macs = []
doc = minidom.parse(_StringIO(get_xml(vm_))) doc = minidom.parse(_StringIO(get_xml(vm_)))


.. code-block:: bash .. code-block:: bash


salt '*' virt.get_graphics <vm name>
salt '*' virtng.get_graphics <vm name>
''' '''
out = {'autoport': 'None', out = {'autoport': 'None',
'keymap': 'None', 'keymap': 'None',


.. code-block:: bash .. code-block:: bash


salt '*' virt.get_disks <vm name>
salt '*' virtng.get_disks <vm name>
''' '''
disks = {} disks = {}
doc = minidom.parse(_StringIO(get_xml(vm_))) doc = minidom.parse(_StringIO(get_xml(vm_)))


.. code-block:: bash .. code-block:: bash


salt '*' virt.setmem myvm 768
salt '*' virtng.setmem myvm 768
''' '''
if vm_state(vm_) != 'shutdown': if vm_state(vm_) != 'shutdown':
return False return False


.. code-block:: bash .. code-block:: bash


salt '*' virt.setvcpus myvm 2
salt '*' virtng.setvcpus myvm 2
''' '''
if vm_state(vm_) != 'shutdown': if vm_state(vm_) != 'shutdown':
return False return False


.. code-block:: bash .. code-block:: bash


salt '*' virt.freemem
salt '*' virtng.freemem
''' '''
conn = __get_conn() conn = __get_conn()
mem = conn.getInfo()[1] mem = conn.getInfo()[1]


.. code-block:: bash .. code-block:: bash


salt '*' virt.freecpu
salt '*' virtng.freecpu
''' '''
conn = __get_conn() conn = __get_conn()
cpus = conn.getInfo()[2] cpus = conn.getInfo()[2]


.. code-block:: bash .. code-block:: bash


salt '*' virt.full_info
salt '*' virtng.full_info
''' '''
return {'freecpu': freecpu(), return {'freecpu': freecpu(),
'freemem': freemem(), 'freemem': freemem(),


.. code-block:: bash .. code-block:: bash


salt '*' virt.get_xml <vm name>
salt '*' virtng.get_xml <vm name>
''' '''
dom = _get_dom(vm_) dom = _get_dom(vm_)
return dom.XMLDesc(0) return dom.XMLDesc(0)


.. code-block:: bash .. code-block:: bash


salt '*' virt.get_profiles
salt '*' virt.get_profiles hypervisor=esxi
salt '*' virtng.get_profiles
salt '*' virtng.get_profiles hypervisor=esxi
''' '''
ret = {} ret = {}
if hypervisor: if hypervisor:


.. code-block:: bash .. code-block:: bash


salt '*' virt.shutdown <vm name>
salt '*' virtng.shutdown <vm name>
''' '''
dom = _get_dom(vm_) dom = _get_dom(vm_)
return dom.shutdown() == 0 return dom.shutdown() == 0


.. code-block:: bash .. code-block:: bash


salt '*' virt.pause <vm name>
salt '*' virtng.pause <vm name>
''' '''
dom = _get_dom(vm_) dom = _get_dom(vm_)
return dom.suspend() == 0 return dom.suspend() == 0


.. code-block:: bash .. code-block:: bash


salt '*' virt.resume <vm name>
salt '*' virtng.resume <vm name>
''' '''
dom = _get_dom(vm_) dom = _get_dom(vm_)
return dom.resume() == 0 return dom.resume() == 0


.. code-block:: bash .. code-block:: bash


salt '*' virt.create <vm name>
salt '*' virtng.create <vm name>
''' '''
dom = _get_dom(vm_) dom = _get_dom(vm_)
return dom.create() == 0 return dom.create() == 0


.. code-block:: bash .. code-block:: bash


salt '*' virt.start <vm name>
salt '*' virtng.start <vm name>
''' '''
return create(vm_) return create(vm_)




.. code-block:: bash .. code-block:: bash


salt '*' virt.stop <vm name>
salt '*' virtng.stop <vm name>
''' '''
return destroy(vm_) return destroy(vm_)




.. code-block:: bash .. code-block:: bash


salt '*' virt.reboot <vm name>
salt '*' virtng.reboot <vm name>
''' '''
dom = _get_dom(vm_) dom = _get_dom(vm_)




.. code-block:: bash .. code-block:: bash


salt '*' virt.reset <vm name>
salt '*' virtng.reset <vm name>
''' '''
dom = _get_dom(vm_) dom = _get_dom(vm_)




.. code-block:: bash .. code-block:: bash


salt '*' virt.ctrl_alt_del <vm name>
salt '*' virtng.ctrl_alt_del <vm name>
''' '''
dom = _get_dom(vm_) dom = _get_dom(vm_)
return dom.sendKey(0, 0, [29, 56, 111], 3, 0) == 0 return dom.sendKey(0, 0, [29, 56, 111], 3, 0) == 0


.. code-block:: bash .. code-block:: bash


salt '*' virt.create_xml_str <XML in string format>
salt '*' virtng.create_xml_str <XML in string format>
''' '''
conn = __get_conn() conn = __get_conn()
return conn.createXML(xml, 0) is not None return conn.createXML(xml, 0) is not None


.. code-block:: bash .. code-block:: bash


salt '*' virt.create_xml_path <path to XML file on the node>
salt '*' virtng.create_xml_path <path to XML file on the node>
''' '''
if not os.path.isfile(path): if not os.path.isfile(path):
return False return False


.. code-block:: bash .. code-block:: bash


salt '*' virt.define_xml_str <XML in string format>
salt '*' virtng.define_xml_str <XML in string format>
''' '''
conn = __get_conn() conn = __get_conn()
return conn.defineXML(xml) is not None return conn.defineXML(xml) is not None


.. code-block:: bash .. code-block:: bash


salt '*' virt.define_xml_path <path to XML file on the node>
salt '*' virtng.define_xml_path <path to XML file on the node>


''' '''
if not os.path.isfile(path): if not os.path.isfile(path):


.. code-block:: bash .. code-block:: bash


salt '*' virt.define_vol_xml_str <XML in string format>
salt '*' virtng.define_vol_xml_str <XML in string format>
''' '''
poolname = __salt__['config.get']('libvirt:storagepool', 'default') poolname = __salt__['config.get']('libvirt:storagepool', 'default')
conn = __get_conn() conn = __get_conn()


.. code-block:: bash .. code-block:: bash


salt '*' virt.define_vol_xml_path <path to XML file on the node>
salt '*' virtng.define_vol_xml_path <path to XML file on the node>


''' '''
if not os.path.isfile(path): if not os.path.isfile(path):


.. code-block:: bash .. code-block:: bash


salt '*' virt.migrate_non_shared <vm name> <target hypervisor>
salt '*' virtng.migrate_non_shared <vm name> <target hypervisor>
''' '''
cmd = _get_migrate_command() + ' --copy-storage-all ' + vm_\ cmd = _get_migrate_command() + ' --copy-storage-all ' + vm_\
+ _get_target(target, ssh) + _get_target(target, ssh)


.. code-block:: bash .. code-block:: bash


salt '*' virt.migrate_non_shared_inc <vm name> <target hypervisor>
salt '*' virtng.migrate_non_shared_inc <vm name> <target hypervisor>
''' '''
cmd = _get_migrate_command() + ' --copy-storage-inc ' + vm_\ cmd = _get_migrate_command() + ' --copy-storage-inc ' + vm_\
+ _get_target(target, ssh) + _get_target(target, ssh)


.. code-block:: bash .. code-block:: bash


salt '*' virt.migrate <vm name> <target hypervisor>
salt '*' virtng.migrate <vm name> <target hypervisor>
''' '''
cmd = _get_migrate_command() + ' ' + vm_\ cmd = _get_migrate_command() + ' ' + vm_\
+ _get_target(target, ssh) + _get_target(target, ssh)


.. code-block:: bash .. code-block:: bash


salt '*' virt.seed_non_shared_migrate <disks>
salt '*' virtng.seed_non_shared_migrate <disks>
''' '''
for _, data in disks.items(): for _, data in disks.items():
fn_ = data['file'] fn_ = data['file']


.. code-block:: bash .. code-block:: bash


salt '*' virt.destroy <vm name>
salt '*' virtng.destroy <vm name>
''' '''
dom = _get_dom(vm_) dom = _get_dom(vm_)
return dom.destroy() == 0 return dom.destroy() == 0


.. code-block:: bash .. code-block:: bash


salt '*' virt.undefine <vm name>
salt '*' virtng.undefine <vm name>
''' '''
dom = _get_dom(vm_) dom = _get_dom(vm_)
return dom.undefine() == 0 return dom.undefine() == 0


.. code-block:: bash .. code-block:: bash


salt '*' virt.purge <vm name>
salt '*' virtng.purge <vm name>
''' '''
disks = get_disks(vm_) disks = get_disks(vm_)
try: try:


.. code-block:: bash .. code-block:: bash


salt '*' virt.virt_type
salt '*' virtng.virt_type
''' '''
return __grains__['virtual'] return __grains__['virtual']




.. code-block:: bash .. code-block:: bash


salt '*' virt.is_kvm_hyper
salt '*' virtng.is_kvm_hyper
''' '''
try: try:
if 'kvm_' not in salt.utils.fopen('/proc/modules').read(): if 'kvm_' not in salt.utils.fopen('/proc/modules').read():


.. code-block:: bash .. code-block:: bash


salt '*' virt.is_xen_hyper
salt '*' virtng.is_xen_hyper
''' '''
try: try:
if __grains__['virtual_subtype'] != 'Xen Dom0': if __grains__['virtual_subtype'] != 'Xen Dom0':


.. code-block:: bash .. code-block:: bash


salt '*' virt.is_hyper
salt '*' virtng.is_hyper
''' '''
try: try:
import libvirt # pylint: disable=import-error import libvirt # pylint: disable=import-error


.. code-block:: bash .. code-block:: bash


salt '*' virt.vm_cputime
salt '*' virtng.vm_cputime
''' '''
host_cpus = __get_conn().getInfo()[2] host_cpus = __get_conn().getInfo()[2]




.. code-block:: bash .. code-block:: bash


salt '*' virt.vm_netstats
salt '*' virtng.vm_netstats
''' '''
def _info(vm_): def _info(vm_):
dom = _get_dom(vm_) dom = _get_dom(vm_)


.. code-block:: bash .. code-block:: bash


salt '*' virt.vm_blockstats
salt '*' virtng.vm_blockstats
''' '''
def get_disk_devs(vm_): def get_disk_devs(vm_):
doc = minidom.parse(_StringIO(get_xml(vm_))) doc = minidom.parse(_StringIO(get_xml(vm_)))

Loading…
Cancel
Save