Package VMBuilder :: Package plugins :: Package libvirt
[frames] | no frames]

Source Code for Package VMBuilder.plugins.libvirt

 1  # 
 2  #    Uncomplicated VM Builder 
 3  #    Copyright (C) 2007-2009 Canonical Ltd. 
 4  #     
 5  #    See AUTHORS for list of contributors 
 6  # 
 7  #    This program is free software: you can redistribute it and/or modify 
 8  #    it under the terms of the GNU General Public License version 3, as 
 9  #    published by the Free Software Foundation. 
10  # 
11  #    This program is distributed in the hope that it will be useful, 
12  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
13  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
14  #    GNU General Public License for more details. 
15  # 
16  #    You should have received a copy of the GNU General Public License 
17  #    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
18  # 
19  from   VMBuilder import register_plugin, Plugin, VMBuilderUserError 
20  import VMBuilder.util 
21   
22 -class Libvirt(Plugin):
23 name = 'libvirt integration' 24
25 - def register_options(self):
26 group = self.vm.setting_group('libvirt integration') 27 group.add_option('--libvirt', metavar='URI', help='Add VM to given URI') 28 group.add_option('--bridge', metavar="BRIDGE", help='Set up bridged network connected to BRIDGE.') 29 self.vm.register_setting_group(group)
30
31 - def all_domains(self):
32 # This does not seem to work when any domain is already running 33 return self.conn.listDefinedDomains() + [self.conn.lookupByID(id).name() for id in self.conn.listDomainsID()]
34
35 - def preflight_check(self):
36 if not self.vm.libvirt: 37 return True 38 39 import libvirt 40 41 self.conn = libvirt.open(self.vm.libvirt) 42 if self.vm.hostname in self.all_domains() and not self.vm.overwrite: 43 raise VMBuilderUserError('Domain %s already exists at %s' % (self.vm.hostname, self.vm.libvirt)) 44 45 if not self.vm.hypervisor.name == 'KVM': 46 raise VMBuilderUserError('The libvirt plugin is only equiped to work with KVM at the moment.') 47 48 if not self.vm.hypervisor.name == 'KVM': 49 raise VMBuilderUserError('The libvirt plugin is only equiped to work with KVM at the moment.')
50
51 - def deploy(self):
52 if not self.vm.libvirt: 53 # Not for us 54 return False 55 56 if self.vm.hypervisor.preferred_storage == VMBuilder.hypervisor.STORAGE_FS_IMAGE: 57 vmxml = VMBuilder.util.render_template('libvirt', self.vm, 'libvirtxml_fsimage') 58 else: 59 vmxml = VMBuilder.util.render_template('libvirt', self.vm, 'libvirtxml') 60 61 if self.vm.hostname in self.all_domains() and not self.vm.overwrite: 62 raise VMBuilderUserError('Domain %s already exists at %s' % (self.vm.hostname, self.vm.libvirt)) 63 else: 64 self.conn.defineXML(vmxml) 65 66 return True
67 68 register_plugin(Libvirt) 69