Exemplo n.º 1
0
def rbd_image_info(ceph_monitor, rbd_pool_name, rbd_image_name):
    """
    Get information of a rbd image
    :params ceph_monitor: The specified monitor to connect to
    :params rbd_pool_name: The name of rbd pool
    :params rbd_image_name: The name of rbd image
    """
    cmd = "rbd info %s/%s -m %s" % (rbd_pool_name, rbd_image_name,
                                    ceph_monitor)
    output = utils.system(cmd)
    info_pattern = "rbd image \'%s\':.*?$" % rbd_image_name

    rbd_image_info_str = re.findall(info_pattern, output, re.S)[0]

    rbd_image_info = {}
    for rbd_image_line in rbd_image_info_str.splitlines():
        if ":" not in rbd_image_line:
            if "size" in rbd_image_line:
                size_str = re.findall("size\s+(\d+\s+\w+)\s+",
                                      rbd_image_line)[0]
                size = utils_misc.normalize_data_size(size_str)
                rbd_image_info['size'] = size
            if "order" in rbd_image_line:
                rbd_image_info['order'] = int(re.findall("order\s+(\d+)",
                                                         rbd_image_line))
        else:
            tmp_str = rbd_image_line.strip().split(":")
            rbd_image_info[tmp_str[0]] = tmp_str[1]
    return rbd_image_info
Exemplo n.º 2
0
def rbd_image_create(ceph_monitor, rbd_pool_name, rbd_image_name,
                     rbd_image_size, force_create=False):
    """
    Create a rbd image.
    :params ceph_monitor: The specified monitor to connect to
    :params rbd_pool_name: The name of rbd pool
    :params rbd_image_name: The name of rbd image
    :params rbd_image_size: The size of rbd image
    :params force_create: Force create the image or not
    """
    if rbd_image_exist(ceph_monitor, rbd_pool_name, rbd_image_name):
        create_image = False
        image_info = rbd_image_info(ceph_monitor, rbd_pool_name,
                                    rbd_image_name)
        try:
            int(rbd_image_size)
            compare_str = rbd_image_size
        except ValueError:
            compare_str = utils_misc.normalize_data_size(rbd_image_size)
        if image_info['size'] != compare_str or force_create:
            rbd_image_rm(ceph_monitor, rbd_pool_name, rbd_image_name)
            create_image = True
    if create_image:
        cmd = "rbd create %s/%s -m %s" % (rbd_pool_name, rbd_image_name,
                                          ceph_monitor)
        utils.system(cmd, verbose=True)
    else:
        logging.debug("Image already exist skip the create.")
Exemplo n.º 3
0
def rbd_image_info(ceph_monitor, rbd_pool_name, rbd_image_name):
    """
    Get information of a rbd image
    :params ceph_monitor: The specified monitor to connect to
    :params rbd_pool_name: The name of rbd pool
    :params rbd_image_name: The name of rbd image
    """
    cmd = "rbd info %s/%s -m %s" % (rbd_pool_name, rbd_image_name,
                                    ceph_monitor)
    output = utils.system(cmd)
    info_pattern = "rbd image \'%s\':.*?$" % rbd_image_name

    rbd_image_info_str = re.findall(info_pattern, output, re.S)[0]

    rbd_image_info = {}
    for rbd_image_line in rbd_image_info_str.splitlines():
        if ":" not in rbd_image_line:
            if "size" in rbd_image_line:
                size_str = re.findall("size\s+(\d+\s+\w+)\s+",
                                      rbd_image_line)[0]
                size = utils_misc.normalize_data_size(size_str)
                rbd_image_info['size'] = size
            if "order" in rbd_image_line:
                rbd_image_info['order'] = int(
                    re.findall("order\s+(\d+)", rbd_image_line))
        else:
            tmp_str = rbd_image_line.strip().split(":")
            rbd_image_info[tmp_str[0]] = tmp_str[1]
    return rbd_image_info
Exemplo n.º 4
0
def rbd_image_create(ceph_monitor,
                     rbd_pool_name,
                     rbd_image_name,
                     rbd_image_size,
                     force_create=False):
    """
    Create a rbd image.
    :params ceph_monitor: The specified monitor to connect to
    :params rbd_pool_name: The name of rbd pool
    :params rbd_image_name: The name of rbd image
    :params rbd_image_size: The size of rbd image
    :params force_create: Force create the image or not
    """
    if rbd_image_exist(ceph_monitor, rbd_pool_name, rbd_image_name):
        create_image = False
        image_info = rbd_image_info(ceph_monitor, rbd_pool_name,
                                    rbd_image_name)
        try:
            int(rbd_image_size)
            compare_str = rbd_image_size
        except ValueError:
            compare_str = utils_misc.normalize_data_size(rbd_image_size)
        if image_info['size'] != compare_str or force_create:
            rbd_image_rm(ceph_monitor, rbd_pool_name, rbd_image_name)
            create_image = True
    if create_image:
        cmd = "rbd create %s/%s -m %s" % (rbd_pool_name, rbd_image_name,
                                          ceph_monitor)
        utils.system(cmd, verbose=True)
    else:
        logging.debug("Image already exist skip the create.")
Exemplo n.º 5
0
 def test_normalize_data_size(self):
     n1 = utils_misc.normalize_data_size("12M")
     n2 = utils_misc.normalize_data_size("1024M", "G")
     n3 = utils_misc.normalize_data_size("1024M", "T")
     n4 = utils_misc.normalize_data_size("1000M", "G", 1000)
     n5 = utils_misc.normalize_data_size("1T", "G", 1000)
     n6 = utils_misc.normalize_data_size("1T", "M")
     self.assertEqual(n1, "12.0")
     self.assertEqual(n2, "1.0")
     self.assertEqual(n3, "0.0009765625")
     self.assertEqual(n4, "1.0")
     self.assertEqual(n5, "1000.0")
     self.assertEqual(n6, "1048576.0")
Exemplo n.º 6
0
 def test_normalize_data_size(self):
     n1 = utils_misc.normalize_data_size("12M")
     n2 = utils_misc.normalize_data_size("1024M", "G")
     n3 = utils_misc.normalize_data_size("1024M", "T")
     n4 = utils_misc.normalize_data_size("1000M", "G", 1000)
     n5 = utils_misc.normalize_data_size("1T", "G", 1000)
     n6 = utils_misc.normalize_data_size("1T", "M")
     self.assertEqual(n1, "12.0")
     self.assertEqual(n2, "1.0")
     self.assertEqual(n3, "0.0009765625")
     self.assertEqual(n4, "1.0")
     self.assertEqual(n5, "1000.0")
     self.assertEqual(n6, "1048576.0")
Exemplo n.º 7
0
def normalize_data_size(size):
    if re.match(".*\d$", str(size)):
        size = "%s%s" % (size, UNIT)
    size = float(utils_misc.normalize_data_size(size, UNIT, 1024))
    return int(math.ceil(size))
Exemplo n.º 8
0
Arquivo: lvm.py Projeto: ypu/virt-test
def normalize_data_size(size):
    if re.match(".*\d$", str(size)):
        size = "%s%s" % (size, UNIT)
    size = float(utils_misc.normalize_data_size(size, UNIT, 1024))
    return int(math.ceil(size))