Module virt
[hide private]
[frames] | no frames]

Source Code for Module virt

  1  # 
  2  # Copyright (c) 2011 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU Lesser General Public 
  5  # License as published by the Free Software Foundation; either version 
  6  # 2 of the License (LGPLv2) or (at your option) any later version. 
  7  # There is NO WARRANTY for this software, express or implied, 
  8  # including the implied warranties of MERCHANTABILITY, 
  9  # NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should 
 10  # have received a copy of LGPLv2 along with this software; if not, see 
 11  # http://www.gnu.org/licenses/old-licenses/lgpl-2.0.txt. 
 12  # 
 13  # Jeff Ortel <jortel@redhat.com> 
 14  # 
 15   
 16  import libvirt 
 17  from gofer.decorators import * 
 18  from gofer.agent.plugin import Plugin 
 19  from logging import getLogger 
 20   
 21  log = getLogger(__name__) 
 22  plugin = Plugin.find(__name__) 
23 24 25 -class Virt:
26 27 @classmethod
28 - def open(cls):
29 cfg = plugin.cfg() 30 con = libvirt.open(cfg.virt.uri) 31 return con
32 33 @remote
34 - def getDomainID(self, name):
35 """ 36 Resolve a domain name to a domain ID. 37 @param name: A domain name. 38 @type name: str 39 @return: A domain ID. 40 @rtype: int 41 """ 42 con = self.open() 43 try: 44 domain = con.lookupByName(name) 45 return domain.ID() 46 finally: 47 con.close()
48 49 @remote
50 - def listDomains(self):
51 """ 52 Get a list of domains. 53 @return: List of dict: {id, name, active} 54 @rtype: list 55 """ 56 con = self.open() 57 try: 58 domains = [] 59 for id in con.listDomainsID(): 60 domain = con.lookupByID(id) 61 d = dict(id=id, 62 name=domain.name(), 63 active=domain.isActive()) 64 domains.append(d) 65 return domains 66 finally: 67 con.close()
68 69 @remote
70 - def isAlive(self, id):
71 """ 72 Get whether a domain is alive (running). 73 @param id: A domain ID. 74 @type id: int 75 @return: True if alive. 76 @rtype: bool 77 """ 78 con = self.open() 79 try: 80 domain = con.lookupByID(id) 81 return domain.isAlive() 82 finally: 83 con.close()
84 85 @remote 86 @pam(user='root')
87 - def start(self, id):
88 """ 89 Start (create) a domain. 90 @param id: A domain ID. 91 @type id: int 92 """ 93 con = self.open() 94 try: 95 domain = con.lookupByID(id) 96 domain.create() 97 finally: 98 con.close()
99 100 @remote 101 @pam(user='root')
102 - def shutdown(self, id):
103 """ 104 Shutdown a domain. 105 @param id: A domain ID. 106 @type id: int 107 """ 108 con = self.open() 109 try: 110 domain = con.lookupByID(id) 111 domain.shutdown() 112 finally: 113 con.close()
114