Esempio n. 1
0
 def __init__(self, config_file):
     """Instantiate ConfigParser object. Parse configuration file."""
     if config_file is None:
         raise error.ConfigError('First call needs a config file')
     self.config_file = config_file
     ConfigParser.ConfigParser.__init__(self)
     if self.read(self.config_file) == []:
         raise error.ConfigError('Config file %s is missing or empty' %
                                 self.config_file)
Esempio n. 2
0
 def items(self, section):
     """Lists items in key value pairs from a section"""
     try:
         result = ConfigParser.ConfigParser.items(self, section)
     except ConfigParser.NoSectionError:
         msg = '%s missing required [%s] section' % \
                                                 (self.config_file, section)
         raise error.ConfigError(msg)
     except ConfigParser.NoOptionError:
         msg = '%s missing required \'<key> = <value>\' entry' % \
                                                 (self.config_file)
         raise error.ConfigError(msg)
     return result
Esempio n. 3
0
File: ip.py Progetto: mattmb/spoke
 def _populate_from_ldap(self):
     """Create a list of allocated IP addresses from LDAP"""
     if not self.ip_ldap_search_base:
         msg = 'LDAP enabled but no LDAP search base defined'
         raise error.ConfigError(msg)
     # Do LDAP search and populate allocated IP address store
     ldap = SpokeLDAP()
     dn = self.ip_ldap_search_base
     search_scope = 2  # ldap.SCOPE_SUBTREE
     # Filter requires a modified DHCP schema with substr match support
     filter = '%s=%s*' % (self.ip_ldap_attr, self.ip_ldap_key)
     attr = [self.ip_ldap_attr]
     result = ldap._get_object(dn, search_scope, filter, attr)
     if result['data'] == []:
         msg = 'No reserved IP addresses found in LDAP'
         self.log.debug(msg)
         return True
     # Loop through our result and build a set of allocated addresses
     for item in result['data']:
         # Fetch the value of the IP Address attribute
         entry = item[1][self.ip_ldap_attr][0]
         # Remove the string prefix
         ip = entry.split(' ')[1]
         # Check if ip address is in our subnet
         if self.subnet.has_key(ip):
             self.aloc_ips.add(ip)
     msg = 'Found %s IP addreses in LDAP' % len(self.aloc_ips)
     self.log.debug(msg)
Esempio n. 4
0
 def get(self, section, key, default=None):
     """Get method extended with with specific exceptions."""
     try:
         result = ConfigParser.ConfigParser.get(self, section, key)
     except ConfigParser.NoSectionError:
         if default == None:
             msg = '%s missing required [%s] section' % \
                                                 (self.config_file, section)
             raise error.ConfigError(msg)
         result = default
     except ConfigParser.NoOptionError:
         if default == None:
             msg = '%s missing required \'%s = <value>\' entry' % \
                                                 (self.config_file, key)
             raise error.ConfigError(msg)
         result = default
     return result
Esempio n. 5
0
    def create(self, vm_name, vm_uuid, vm_mem, vm_cpu, vm_family, vm_storage_layout,
               vm_network_layout, vm_install=False, vm_disks=None, vm_interfaces=None):
        """Define a new VM and add to hypervisor's store (does not start)."""
        try:
            vm_name = common.validate_hostname(vm_name)
            vm_cpu = common.validate_cpu(vm_cpu)
            vm_mem = common.validate_mem(vm_mem)
            #vm_type = common.validate_host_type(vm_type)
            vm_family = common.validate_host_family(vm_family)
            #vm_extra_opts = common.is_shell_safe(vm_extra_opts)
            vm_uuid = common.validate_uuid(vm_uuid)
        except error.InputError as e:
            self.log.error(e)
            raise e
        
        try:
            self.conn.lookupByName(vm_name)
        except libvirt.libvirtError:
            pass
        else:
            msg = "Domain %s already exists, cannot create." % vm_name
            raise error.AlreadyExists(msg)
        
        # Create a UUID in hypervisor format
        formatted_uuid = self._format_uuid(vm_uuid)
        
        #-1 means XEN will give the right XenID when it starts
        vm_id=-1
        
        #Initial xml structure
        doc = xml.createDoc("doc")
        domain = xml.createElement(doc, "domain", {"type": vm_family})
        #Variable config options
        #xml.createChild(doc, domain, "arch name", None, 'i686')
        xml.createChild(doc, domain, "name", None, vm_name)
        xml.createChild(doc, domain, "memory", None, vm_mem)
        xml.createChild(doc, domain, "currentMemory", None, vm_mem)
        xml.createChild(doc, domain, "vcpu", None, vm_cpu)
        xml.createChild(doc, domain, "uuid", None, formatted_uuid)
        
        #fixed(ish) config options
        os = xml.createChild(doc, domain, "os", None, None)
        #ks - the below is going to have to change for 64bit
        xml.createChild(doc, os, "type", {"arch": "i686"}, "hvm")
        xml.createChild(doc, domain, "clock", {"offset": "utc"}, None)
        xml.createChild(doc, domain, "on_poweroff", None, "destroy")
        xml.createChild(doc, domain, "on_reboot", None, "restart")
        xml.createChild(doc, domain, "on_crash", None, "restart")
        
        devices = xml.createChild(doc, domain, "devices", None, None)
        console = xml.createChild(doc, devices, "console", {"type": "pty"}, None)
        xml.createChild(doc, console, "target", {"type": "xen", "port": "0"}, None)
        #ks
        #xml.createChild(doc, devices, "input", {"type": "mouse", "bus": "xen"}, None)
        # TODO Change the port such that it is associated with the UUID and change listen to internal interface only
        xml.createChild(doc, devices, "graphics", {"type": "vnc", "port": "-1", "autoport": "yes", "listen": "0.0.0.0"}, None)
        xml.createChild(doc, devices, "emulator", None, "/usr/lib/xen/bin/qemu-dm")
        
#        #parse disk info
#        for item in vm_disks:
#            #local1 means hda and vg01
#            if item[0] == "local1":
#                disk = xml.createChild(doc, devices, "disk", {"type": "block", "device": "disk"}, None)
#                xml.createChild(doc, disk, "driver", {"name": "phy"}, None)
#                xml.createChild(doc, disk, "source", {"dev": "/dev/vg01/%s" % vm_name}, None)
#                xml.createChild(doc, disk, "target", {"dev": "hda", "bus": "ide"}, None)
#            #local2 means hdb and vg02
#            if item[0] == "local2":
#                disk = xml.createChild(doc, devices, "disk", {"type": "block", "device": "disk"}, None)
#                xml.createChild(doc, disk, "driver", {"name": "phy"}, None)
#                xml.createChild(doc, disk, "source", {"dev": "/dev/vg01/ko-test-02"}, None)
#                xml.createChild(doc, disk, "target", {"dev": "hdb", "bus": "ide"}, None)                                                      

        if vm_disks is not None:
            for item in vm_disks:
                if item[0] == "local1":
                    disk = xml.createChild(doc, devices, "disk", {"type": "block", "device": "disk"}, None)
                    xml.createChild(doc, disk, "driver", {"name": "phy"}, None)
                    xml.createChild(doc, disk, "source", {"dev": "/dev/vg01/%s" % vm_name}, None)
                    xml.createChild(doc, disk, "target", {"dev": "hda", "bus": "ide"}, None)
                #local2 means hdb and vg02
                if item[0] == "local2":
                    disk = xml.createChild(doc, devices, "disk", {"type": "block", "device": "disk"}, None)
                    xml.createChild(doc, disk, "driver", {"name": "phy"}, None)
                    xml.createChild(doc, disk, "source", {"dev": "/dev/vg02/%s" % vm_name}, None)
                    xml.createChild(doc, disk, "target", {"dev": "hdb", "bus": "ide"}, None)                                                      

        elif vm_storage_layout is not None:
            try:
                disks = self.dtypes[vm_storage_layout]
            except KeyError as e:
                msg = "The disk type %s is not present in config file." % e
                raise error.InputError, msg
            for item in disks:
                item = common.validate_disks_in_conf(self.dnames[item])
                hv_dev = item[0] + "/" + vm_name
                dom_dev = item[1]
                disk = xml.createChild(doc, devices, "disk", {"type": "block", "device": "disk"}, None)
                xml.createChild(doc, disk, "driver", {"name": "phy"}, None)
                xml.createChild(doc, disk, "source", {"dev": hv_dev}, None)
                xml.createChild(doc, disk, "target", {"dev": dom_dev, "bus": "ide"}, None)                                                      

        #parse interface info
        if vm_interfaces is not None:
            for interface in vm_interfaces:
                #get input from interface list
                bridge = int( interface[0].lstrip('breth') )
                mac = interface[1]
                source_interface = interface[2]
                
                interface = xml.createChild(doc, devices, "interface", {"type": "bridge"}, None)
                xml.createChild(doc, interface, "mac", {"address": mac}, None)
                xml.createChild(doc, interface, "source", {"bridge": source_interface}, None)
                xml.createChild(doc, interface, "script", {"path": "vif-bridge"}, None)
                xml.createChild(doc, interface, "target", {"dev": "vif%i.%i" % (vm_id, bridge)}, None)
        elif vm_network_layout is not None:
            try:
                interfaces = self.ifacedef[vm_network_layout]
            except KeyError:
                msg = "The interface type %s is not present in config file." % vm_network_layout
                raise error.InputError(msg)
            
            # Ensure that br0,x is done first as xen cares about order in xml.
            interfaces = sorted(interfaces, key=itemgetter(0))
            for interface in interfaces:
                interface = common.validate_interfaces_in_conf(interface)
                iface_number = int( interface[0].lstrip('breth') )
                if iface_number == 0:
                    boot_mac = common.mac_from_uuid(vm_uuid, iface_number)                   
                    boot_int = interface[1]
                mac = common.mac_from_uuid(vm_uuid, iface_number)
                                   
                source_interface = interface[1]
                # KS enumerate avail interfaces via facter, not remote socket op 
                #if not source_interface in self._all_interfaces():
                #    msg = "%s does not exist on this machine so we cant bridge to it!" % source_interface
                #    raise error.InsufficientResource, msg
                                    
                interface = xml.createChild(doc, devices, "interface", {"type": "bridge"}, None)
                xml.createChild(doc, interface, "mac", {"address": mac}, None)
                xml.createChild(doc, interface, "source", {"bridge": source_interface}, None)
                xml.createChild(doc, interface, "script", {"path": "vif-bridge"}, None)
                xml.createChild(doc, interface, "target", {"dev": "vif%i.%i" % (vm_id, iface_number)}, None)
        
        if vm_install: # Add network boot lines
            xml.createChild(doc, domain, "bootloader", None, "/usr/sbin/pypxeboot" )
            try:
                xml.createChild(doc, domain, "bootloader_args", None, "--udhcpc=/usr/local/pkg/udhcp/sbin/udhcpc --interface=%s mac=%s --label=install-aethernet" % (boot_int, boot_mac) )
            except UnboundLocalError:
                msg = "In config there must be an interface br0 as the provisioning interface!"
                raise error.ConfigError(msg)
        else:
            xml.createChild(doc, domain, "bootloader", None, "/usr/bin/pygrub" )

        try:
            out = self.conn.defineXML(xml.out(doc))
        except Exception, e:
            trace = traceback.format_exc()
            raise error.LibvirtError(e, trace)
Esempio n. 6
0
File: vm.py Progetto: mattmb/spoke
    raise

hv_uri = conf.get('VM', 'hv_uri')
try:
    vm_types = conf.get('VM', 'vm_types')
except:
    vm_types = 'test,xen,kvm,vmware'
vm_types_re = vm_types.replace(',','|')
    
family = re.compile('^%s' % vm_types_re)
vm_family = family.match(hv_uri)
try:
    vm_family = vm_family.group(0)
except AttributeError as e:
    msg = "not using valid family in hv_uri, must be one of: %s" % vm_types
    raise error.ConfigError(msg)

def store_create(vm_family, vm_name, vm_uuid, vm_mem, vm_cpu,
                vm_storage_layout, vm_network_layout,
                vm_install=False):
    from spoke.lib.vm_storage import SpokeVMStorageXen
    vms = SpokeVMStorageXen(hv_uri)
    try:
        result = vms.create(vm_name, vm_uuid, vm_mem, vm_cpu, vm_family,
                vm_storage_layout, vm_network_layout,
                vm_install)
    except error.SpokeError as e:
        result = common.handle_error(e)
    return result