예제 #1
0
def create_n_check_tmp_files(delete_first=False, create_ramdisk=False):
    """ Create tmp dirs if not existing yet.

    Raises
    ------
    AlreadyRunning
    """
    if os.path.exists(PATH_TMP):
        if delete_first:
            try:
                shutil.rmtree(PATH_LOGS)
            except BaseException:
                pass

            # umount ramdisk
            try_umount_ramdisk()
        else:
            # Ticket #12
            raise AlreadyRunning("Another instance of %s might be running! Did you invoke the cleanup script '%s'?" % (PROJECT_NAME, PATH_CLEANUP_SCRIPT))

    if not os.path.exists(PATH_TMP):
        log.debug("Creating directory '%s'!", PATH_TMP)
        os.makedirs(PATH_TMP)

    if create_ramdisk:
        if not os.path.ismount(PATH_TMP):

            log.info("creating ramdisk at '%s'", PATH_TMP)
            # TODO: better way for ramdisk creation?
            run_shell("sudo mount -t ramfs ramfs {}".format(PATH_TMP))
        else:
            log.info("ramdisk still exists at '%s' ... ", PATH_TMP)
예제 #2
0
        def get_ipdb(self):
            global ipdb
            if self.ipdb is None:
                from pyroute2 import IPDB
                # ipdb = IPDB()
                # https://github.com/svinota/pyroute2/issues/304#issuecomment-259275184
                import pyroute2.netlink.rtnl as rtnl
                GROUPS = rtnl.RTNLGRP_LINK | rtnl.RTNLGRP_NEIGH | rtnl.RTNLGRP_IPV4_IFADDR | rtnl.RTNLGRP_IPV6_IFADDR | rtnl.RTNLGRP_IPV4_ROUTE | rtnl.RTNLGRP_IPV6_ROUTE | rtnl.RTNLGRP_IPV4_MROUTE
                GROUPS = \
                    rtnl.RTNLGRP_LINK | \
                    rtnl.RTNLGRP_NEIGH | \
                    rtnl.RTNLGRP_IPV4_IFADDR | \
                    rtnl.RTNLGRP_IPV4_ROUTE | \
                    rtnl.RTNLGRP_IPV4_MROUTE | \
                    rtnl.RTNLGRP_IPV6_IFADDR | \
                    rtnl.RTNLGRP_IPV6_ROUTE | \
                    rtnl.RTNLGRP_MPLS_ROUTE

                # rtnl.RTNLGRP_NONE |\
                # rtnl.RTNLGRP_NOTIFY |\
                # rtnl.RTNLGRP_IPV4_RULE |\
                # rtnl.RTNLGRP_DECnet_IFADDR |\
                # rtnl.RTNLGRP_DECnet_ROUTE |\
                # rtnl.RTNLGRP_DECnet_RULE |\
                # rtnl.RTNLGRP_IPV6_MROUTE |\

                # rtnl.RTNLGRP_IPV6_IFINFO |\
                # rtnl.RTNLGRP_IPV6_PREFIX |\
                # rtnl.RTNLGRP_IPV6_RULE | \
                # rtnl.RTNLGRP_NOP2 |\
                # rtnl.RTNLGRP_NOP4 |\
                # rtnl.RTNLGRP_TC |\

                # .*_ROUTE
                # AttributeError: 'IPDB' object has no attribute 'routes'
                # .*_IFADDR
                # AttributeError: 'IPDB' object has no attribute 'by_name'
                self.ipdb = IPDB(nl_async="process", nl_bind_groups=GROUPS)
                # import pyroute2
                # ipr = pyroute2.IPRoute()
                # ipb = pyroute2.IPBatch()
                # ipb.link("add", index=550, kind="dummy", ifname="test2")
                # ipr.sendto(ipb.batch, (0, 0))
                # ipdb = IPDB(nl_async="process")
                # run_shell("renice -n {} {}".format(-20, self.ipdb.mnl.async_cache.pid))
                run_shell("chrt -f -p {} {}".format(
                    1, self.ipdb.mnl.async_cache.pid))

            return self.ipdb
예제 #3
0
def find_empty_group(start=1, end=10000):
    """
    Returns the next free iproute2 group.

    Parameters
    ----------
    start : int
    end : int

    Returns
    -------
    int
    """
    for idx in range(start, end + 1):
        # no links for this group
        if not run_shell("ip link list group {}".format(idx)):
            return idx
예제 #4
0
def umount_ramdisk():
    run_shell("init", "umount {}".format(PATH_TMP))
예제 #5
0
def get_nic_models():
    output = run_shell("kvm -device ?")
    output = output.split("Network devices:")[1:]
    return re.findall('name\s+"([^"]+)', output, re.MULTILINE)