Exemplo n.º 1
0
    def vol_create(self,
                   name,
                   capacity=None,
                   base=None,
                   pool_name='default',
                   backing_store=False,
                   base_plus=0):
        xml = xmlbuilder.XMLBuilder('volume')
        xml.name(name)
        xml.allocation('0', unit='MiB')
        if base:
            xml.capacity(
                str(int(get_qcow_size(base)) + int(base_plus) * 1048576))
        else:
            xml.capacity(capacity, unit='MiB')

        with xml.target:
            xml.format(type='qcow2')

        pool = self.conn.storagePoolLookupByName(pool_name)
        if base and backing_store:
            with xml.backingStore:
                xml.path(base)
                xml.format(type='qcow2')

        vol = pool.createXML(str(xml), flags=0)

        if base and not backing_store:
            self.volume_upload(vol.key(), base)

        return vol.key()
Exemplo n.º 2
0
def render_svg(literals, size=20):
    import xmlbuilder

    answer_set = extractExtensions(literals)
    maxx = max([x[0] for x in answer_set['row']])
    maxy = max([x[1] for x in answer_set['column']])

    svg = xmlbuilder.XMLBuilder(
        'svg',
        viewBox="0 0 %d %d" % (10 * (maxx + 1), 10 * (maxy + 1)),
        xmlns="http://www.w3.org/2000/svg",
        **{'xmlns:xlink': "http://www.w3.org/1999/xlink"})

    #with svg.linearGradient(id="grad"):
    #    svg.stop(offset="0", **{'stop-color': "#f00"})
    #    svg.stop(offset="1", **{'stop-color': "#ff0"})

    #with svg.g():
    #    for (x,y) in room.values():
    #        svg.circle(cx=str(5+10*x),cy=str(5+10*y),r="2")

    with svg.g():
        for x in answer_set['row']:
            for y in answer_set['column']:
                x = int(x)
                y = int(y)
                svg.rect(x=str(10 * x - 5),
                         y=str(10 * y - 5),
                         width=str(10),
                         height=str(10),
                         style="stroke: black; stroke-width: 1px; fill:white;")

    #return IPython.display.SVG(data=str(svg))
    with file("out.svg", "w+") as f:
        f.write(str(svg))
Exemplo n.º 3
0
    def start_net(self, name):
        logger.info("Start network " + name)

        if name in self.networks:
            conn = libvirt.open(self.urls[self.networks[name].htype])
        else:
            conn = libvirt.open(self.def_connection)

        try:
            net = conn.networkLookupByName(name)
            if not net.isActive():
                logger.debug("Network registered in libvirt - start it")
                net.create()
            else:
                logger.debug("Network already active")

        except libvirt.libvirtError:
            try:
                logger.debug("No such network in libvirt")
                net = self.networks[name]
            except KeyError:
                msg = "Can't found network {0!r}".format(name)
                logger.error(msg)
                raise CloudError(msg)

            xml = xmlbuilder.XMLBuilder('network')
            xml.name(name)
            xml.bridge(name=net.bridge)
            with xml.ip(address=net.ip, netmask=net.netmask):
                xml.dhcp.range(start=net.ip1, end=net.ip2)

            logger.debug("Create network")
            conn.networkCreateXML(str(xml))
Exemplo n.º 4
0
    def _generate_xml(self, exc, trace):
        message = "{0}: {1}".format(exc.message, str(exc))

        xml = xmlbuilder.XMLBuilder()
        with xml.notice(version=2.0):
            xml << ('api-key', self.api_key)
            with xml.notifier:
                xml << ('name', app_name)
                xml << ('version', version)
                xml << ('url', source_url)
            with xml('server-environment'):
                xml << ('environment-name', self.environment)
            with xml.request:
                xml << ("url", "")
                xml << ("component", self.component_name)
                with xml("cgi-data"):
                    with xml("var", key="nodeName"):
                        xml << self.node_name
                    with xml("var", key="componentName"):
                        xml << self.component_name
            with xml.error:
                xml << ('class', '' if exc is None else exc.__class__.__name__)
                xml << ('message', message)
                if trace:
                    with xml.backtrace:
                        [xml << ('line', {'file': filename, 'number': line_number, 'method': "{0}: {1}".format(function_name, text)})\
                         for filename, line_number, function_name, text in traceback.extract_tb(trace)]
        return str(xml)
Exemplo n.º 5
0
 def pool_define(self, name, path):
     xml = xmlbuilder.XMLBuilder('pool', type='dir')
     xml.name(name)
     with xml.target:
         xml.path(path)
     if not os.path.isdir(path):
         os.makedirs(path, 0o755)
     return self.conn.storagePoolCreateXML(str(xml)).UUIDString()
Exemplo n.º 6
0
    def net_define(self,
                   name,
                   uuid=None,
                   bridge_name=None,
                   forward_mode=None,
                   virtualport_type=None,
                   ip_address=None,
                   ip_netmask=None,
                   dhcp=None,
                   tftp_root=None):

        xml = xmlbuilder.XMLBuilder('network')
        xml.name(name)

        if uuid:
            xml.uuid(uuid)

        if bridge_name:
            xml.bridge(name=bridge_name)

        if forward_mode:
            xml.forward(mode=forward_mode)

        if virtualport_type:
            xml.virtualport(type=virtualport_type)

        if ip_address:
            with xml.ip(address=ip_address,
                        netmask=(ip_netmask or '255.255.255.0')):

                if tftp_root:
                    xml.tftp(root=tftp_root)

                if dhcp:
                    with xml.dhcp:
                        xml.range(start=dhcp['start'], end=dhcp['end'])
                        if dhcp.get('hosts'):
                            for host in dhcp['hosts']:
                                kwargs = {'mac': host['mac'], 'ip': host['ip']}
                                if host.get('name'):
                                    kwargs.update({'name': host['name']})
                                xml.host(**kwargs)
                        if dhcp.get('bootp'):
                            if dhcp['bootp'].get('server'):
                                xml.bootp(file=dhcp['bootp']['file'],
                                          server=dhcp['bootp']['server'])
                            else:
                                xml.bootp(file=dhcp['bootp']['file'])

        net = self.conn.networkDefineXML(str(xml))
        return net.UUIDString()
Exemplo n.º 7
0
    def _generate_xml(self, record):
        exn = None
        trace = None
        if record.exc_info:
            _, exn, trace = record.exc_info

        message = record.getMessage()

        if exn:
            message = "{0}: {1}".format(message, str(exn))

        xml = xmlbuilder.XMLBuilder()
        with xml.notice(version=2.0):
            xml << ('api-key', self.api_key)
            with xml.notifier:
                xml << ('name', __app_name__)
                xml << ('version', __version__)
                xml << ('url', __source_url__)
            with xml('server-environment'):
                xml << ('environment-name', self.environment)
            with xml.request:
                xml << ("url", "")
                xml << ("component", self.component_name)
                with xml("cgi-data"):
                    with xml("var", key="nodeName"):
                        xml << self.node_name
                    with xml("var", key="componentName"):
                        xml << self.component_name
            with xml.error:
                xml << ('class', '' if exn is None else exn.__class__.__name__)
                xml << ('message', message)
                with xml.backtrace:
                    if trace is None:
                        [
                            xml << ('line', {
                                'file': record.pathname,
                                'number': record.lineno,
                                'method': record.funcName
                            })
                        ]
                    else:
                        [xml << ('line', {'file': filename, 'number': line_number, 'method': "{0}: {1}".format(function_name, text)})\
                         for filename, line_number, function_name, text in traceback.extract_tb(trace)]
        return str(xml)
Exemplo n.º 8
0
def generate_xml(api_key, notifier, exc_info, request=None, environment=None):
    request = request or {}

    xml = xmlbuilder.XMLBuilder('notice', version=SCHEMA_VERSION)
    getattr(xml, 'api-key')(api_key)

    with xml.notifier:
        xml.name(notifier.get('name', ''))
        xml.version(notifier.get('version'))
        xml.url(notifier.get('url'))

    if exc_info:
        with xml.error:
            add_xml_error(xml, exc_info)

    if request:
        with xml.request:
            add_xml_request(xml, request)

    with getattr(xml, 'server-environment'):
        add_xml_server_environment(xml, environment or {})

    return str(xml)
Exemplo n.º 9
0
def render_svg(literals, size=20):
    import xmlbuilder

    answer_set = extractExtensions(literals)
    maxx = max(map(lambda x: x[0], answer_set['cell']))
    maxy = max(map(lambda x: x[1], answer_set['cell']))

    svg = xmlbuilder.XMLBuilder(
        'svg',
        viewBox="0 0 %d %d" % (10 * (maxx + 1), 10 * (maxy + 1)),
        xmlns="http://www.w3.org/2000/svg",
        **{'xmlns:xlink': "http://www.w3.org/1999/xlink"})

    #with svg.linearGradient(id="grad"):
    #    svg.stop(offset="0", **{'stop-color': "#f00"})
    #    svg.stop(offset="1", **{'stop-color': "#ff0"})

    #with svg.g():
    #    for (x,y) in room.values():
    #        svg.circle(cx=str(5+10*x),cy=str(5+10*y),r="2")

    with svg.g():
        for (x, y) in answer_set['cell']:
            x = int(x)
            y = int(y)
            svg.rect(x=str(10 * x - 5),
                     y=str(10 * y - 5),
                     width=str(10),
                     height=str(10),
                     style="stroke: black; stroke-width: 1px; fill:white;")
        for (x, y) in answer_set['live']:
            x = int(x)
            y = int(y)
            svg.rect(x=str(10 * x - 5),
                     y=str(10 * y - 5),
                     width=str(10),
                     height=str(10),
                     style="stroke: black; stroke-width: 1px; fill:black;")
        for (x, y) in answer_set['exit']:
            x = int(x)
            y = int(y)
            svg.circle(cx=str(10 * x),
                       cy=str(10 * y),
                       r=str(3),
                       style="stroke: red; fill:red; ")
        for (x, y) in answer_set['mark']:
            x = int(x)
            y = int(y)
            svg.circle(cx=str(10 * x),
                       cy=str(10 * y),
                       r=str(2),
                       style="stroke: blue; fill:blue; ")
        for (x, y, text) in answer_set['text']:
            x = int(x)
            y = int(y)
            text = str(text)
            print("SVG %d %d %s" % (x, y, text))
            svg.text(text,
                     x=str(10 * x - 3),
                     y=str(10 * y + 3),
                     style="stroke: green; font-size: 9px; ")

    #return IPython.display.SVG(data=str(svg))
    with file("out.svg", "w+") as f:
        f.write(str(svg))
Exemplo n.º 10
0
    def define(self,
               name,
               uuid=None,
               type='kvm',
               memory='2048',
               vcpu='1',
               arch='x86_64',
               boot=None,
               disks=None,
               interfaces=None):
        xml = xmlbuilder.XMLBuilder('domain', type=type)
        xml.name(name)
        if uuid:
            xml.uuid(uuid)

        xml.memory(memory, unit='MiB')
        xml.vcpu(vcpu)

        with xml.os:
            xml.type('hvm', arch=arch, machine='pc-1.0')
            if boot:
                if isinstance(boot, (list, tuple)):
                    for dev in boot:
                        xml.boot(dev=dev)
                elif isinstance(boot, (str, unicode)):
                    xml.boot(dev=boot)
            xml.bootmenu(enable='no')

        with xml.features:
            xml.acpi
            xml.apic
            xml.pae
        xml.clock(offset='utc')
        xml.on_poweroff('destroy')
        xml.on_reboot('restart')
        xml.on_crash('restart')

        with xml.devices:
            if os.path.exists('/usr/bin/kvm'):  # Debian
                xml.emulator('/usr/bin/kvm')
            elif os.path.exists('/usr/bin/qemu-kvm'):  # Redhat
                xml.emulator('/usr/bin/qemu-kvm')

            xml.input(type='mouse', bus='ps2')
            xml.graphics(type='vnc', port='-1', autoport='yes')
            with xml.video:
                xml.model(type='cirrus', vram='9216', heads='1')
                xml.address(type='pci',
                            domain='0x0000',
                            bus='0x00',
                            slot='0x02',
                            function='0x0')
            with xml.memballoon(model='virtio'):
                xml.address(type='pci',
                            domain='0x0000',
                            bus='0x00',
                            slot='0x07',
                            function='0x0')

            if disks:
                if isinstance(disks, (list, )):
                    for disk in disks:
                        self._add_disk(xml, disk)
                else:
                    self._add_disk(xml, disks)

            if interfaces:
                if isinstance(interfaces, (list, )):
                    for interface in interfaces:
                        self._add_interface(xml, interface)
                else:
                    self._add_interface(xml, interfaces)

        dom = self.conn.defineXML(str(xml))
        return dom.UUIDString()
Exemplo n.º 11
0
    def start_vm(self, vmname, users, prepare_image=False):
        logger.info("Start vm/network {0} with credentials {1}".format(
            vmname, users))

        vms = [
            vm for vm in self.vms.values()
            if vm.name == vmname or vm.name.startswith(vmname +
                                                       self.DOM_SEPARATOR)
        ]
        vm_names = " ".join(vm.name for vm in vms)
        logger.debug(
            "Found next vm's, which match name glob {0}".format(vm_names))

        for vm in vms:
            logger.debug("Prepare vm {0}".format(vm.name))

            path = os.path.join(self.root, self.templates[vm.htype])
            vm_xml_templ = open(path).read()
            logger.info("Use template '{0}'".format(path))

            vm_xm = fromstring(vm_xml_templ)

            el = Element('vcpu')
            el.text = str(vm.vcpu)
            vm_xm.append(el)

            el = Element('name')
            el.text = vm.name
            vm_xm.append(el)

            el = Element('memory')
            el.text = str(vm.mem * 1024)
            vm_xm.append(el)

            devs = vm_xm.find('devices')

            disk_emulator = self.defaults.get('disk_emulator', 'qemu')

            if 'virtio' in vm.opts:
                bus = 'virtio'
            else:
                bus = 'ide'

            if 'ide' == bus:
                dev_name_templ = 'hd'
            elif 'scsi' == bus:
                dev_name_templ = 'sd'
            elif 'virtio' == bus:
                dev_name_templ = 'vd'

            letters = [
                chr(ord('a') + pos) for pos in range(ord('z') - ord('a'))
            ]

            for hdd_pos, image in enumerate(vm.images):

                if hdd_pos > len(letters):
                    raise CloudError("To many HHD devices {0}".format(
                        len(vm.images)))

                rimage = image

                dev_st = os.stat(rimage)
                while stat.S_ISLNK(dev_st.st_mode):
                    rimage = os.readlink(rimage)
                    dev_st = os.stat(rimage)

                dev = dev_name_templ + letters[hdd_pos]

                if stat.S_ISDIR(dev_st.st_mode):
                    hdd = xmlbuilder.XMLBuilder('filesystem', type='mount')
                    hdd.source(dir=image)
                    hdd.target(dir='/')
                else:
                    res = subprocess.check_output(['qemu-img', 'info', image])
                    hdr = "file format: "
                    tp = None
                    for line in res.split('\n'):
                        if line.startswith(hdr):
                            tp = line[len(hdr):].strip()
                    assert tp is not None

                    if stat.S_ISBLK(dev_st.st_mode):
                        hdd = xmlbuilder.XMLBuilder('disk',
                                                    device='disk',
                                                    type='block')
                        hdd.driver(name=disk_emulator, type=tp)
                        hdd.source(dev=image)
                        hdd.target(bus=bus, dev=dev)
                    elif stat.S_ISREG(dev_st.st_mode):
                        hdd = xmlbuilder.XMLBuilder('disk',
                                                    device='disk',
                                                    type='file')
                        hdd.driver(name=disk_emulator, type=tp)
                        hdd.source(file=image)
                        hdd.target(bus=bus, dev=dev)
                    else:
                        raise CloudError(
                            "Can't connect hdd device {0!r}".format(image))

                devs.append(~hdd)

            eths = {}

            conn = self.get_vm_conn(vm.name)

            for eth in vm.eths():
                edev = xmlbuilder.XMLBuilder('interface', type='network')
                edev.source(network=eth['network'])
                edev.mac(address=eth['mac'])
                devs.append(~edev)

                if 'ip' not in eth:
                    eths[eth['name']] = (eth['mac'], 'dhcp', None, None)
                else:
                    brdev = get_network_bridge(conn, eth['network'])
                    addr = ifconfig.getAddr(brdev)
                    mask = ifconfig.getMask(brdev)
                    eths[eth['name']] = (eth['mac'], eth['ip'],
                                         netmask2netsz(mask), addr)

            if users is None:
                users = {vm.user: vm.passwd}

            try:
                if vm.htype == 'lxc':
                    prepare_guest(vm.images[0],
                                  vm.name,
                                  users,
                                  eths,
                                  format='lxc')
                elif prepare_image:
                    prepare_guest(vm.images[0], vm.name, users, eths)
            except CloudError as x:
                print "Can't update vm image -", x

            logger.debug("Image ready - start vm {0}".format(vm.name))

            conn.createXML(tostring(vm_xm), 0)
            logger.debug("VM {0} started ok".format(vm.name))
            conn.close()