def import_template(template, vm_type, storage_pool=None):
    """Import external template into ON storage pool"""
    config = get_config()

    if not storage_pool:
        storage_pool = config.getstring('general', 'default-storage-pool')

    if not os.path.exists(template):
        raise RuntimeError("Template not found: %s" % template)

    if not template.endswith('tar') or template.endswith('ova'):
        raise RuntimeError("Expecting a file ending with .tar or .ova for a template")

    tmpl_name = os.path.basename(template)
    target_file = os.path.join(storage.get_pool_path(storage_pool), vm_type, tmpl_name)

    log.info("Copying template to the storage pool... %s -> %s" % (template, target_file))

    unfinished_local = "%s.unfinished" % tmpl_name
    shutil.copyfile(template, unfinished_local)
    calculate_hash(unfinished_local)

    log.info("Unpacking template %s..." % tmpl_name)
    extension = 'ova' if template.endswith('ova') else 'tar'
    unpack_template(storage_pool, vm_type, tmpl_name.rstrip('.%s' % extension))

    os.rename(unfinished_local, target_file)
    os.rename(unfinished_local + '.pfff', '%s.pfff' % (target_file, extension))
Exemple #2
0
def _package_files(vm_type, ovf_file, template_name, new_name=None):
    """ Package files to template and calculate hash.
    @param template_name: name of the existing template
    @param new_name: name for new template. Optional
    """
    if new_name is None:
        new_name = template_name
    unpacked_base = _get_unpacked_base(vm_type)

    ## XXX: hack to cleanup templates no matter how they're named
    try:
        os.unlink(os.path.join(unpacked_base, '..', template_name + '.ova'))
        os.unlink(os.path.join(unpacked_base, '..', template_name + '.ova.pfff'))
    except OSError:
        pass

    try:
        os.unlink(os.path.join(unpacked_base, '..', template_name + '.tar'))
        os.unlink(os.path.join(unpacked_base, '..', template_name + '.tar.pfff'))
    except OSError:
        pass

    tmpl_file = os.path.join(unpacked_base, '..', new_name + '.ova')
    filelist = generate_ovf_archive_filelist(vm_type, ovf_file, new_name)
    save_to_tar(tmpl_file, filelist)
    calculate_hash(tmpl_file)
Exemple #3
0
def _package_files(vm_type, ovf_file, template_name, new_name=None):
    """ Package files to template and calculate hash.
    @param template_name: name of the existing template
    @param new_name: name for new template. Optional
    """
    if new_name is None:
        new_name = template_name
    unpacked_base = _get_unpacked_base(vm_type)

    ## XXX: hack to cleanup templates no matter how they're named
    try:
        os.unlink(os.path.join(unpacked_base, '..', template_name + '.ova'))
        os.unlink(
            os.path.join(unpacked_base, '..', template_name + '.ova.pfff'))
    except OSError:
        pass

    try:
        os.unlink(os.path.join(unpacked_base, '..', template_name + '.tar'))
        os.unlink(
            os.path.join(unpacked_base, '..', template_name + '.tar.pfff'))
    except OSError:
        pass

    tmpl_file = os.path.join(unpacked_base, '..', new_name + '.ova')
    filelist = generate_ovf_archive_filelist(vm_type, ovf_file, new_name)
    save_to_tar(tmpl_file, filelist)
    calculate_hash(tmpl_file)
Exemple #4
0
def save_as_ovf(vm_settings, storage_pool):
    """
    Creates ovf template archive for the specified container.
    Steps:
        - archive container directory
        - generate ovf configuration file
        - pack ovf and container archive into tar.gz file
    """
    dest_dir = path.join(get_config().getstring('general', 'storage-endpoint'),
                         storage_pool, "openvz")
    unpacked_dir = path.join(dest_dir, "unpacked")
    ct_archive_fnm = path.join(unpacked_dir,
                               "%s.tar.gz" % vm_settings["template_name"])
    ct_source_dir = path.join("/vz/private", vm_settings["vm_name"])

    # Pack vm container catalog
    log = get_logger()
    msg = "Archiving VM container catalog %s. This may take a while..." % ct_source_dir
    log.info(msg)
    with closing(tarfile.open(ct_archive_fnm, "w:gz")) as tar:
        for f in os.listdir(ct_source_dir):
            tar.add(path.join(ct_source_dir, f), arcname=f)

    # Archive action scripts if they are present
    msg = "Adding action scripts..."
    log.info(msg)
    ct_scripts_fnm = path.join(
        unpacked_dir, "%s.scripts.tar.gz" % vm_settings["template_name"])
    with closing(tarfile.open(ct_scripts_fnm, "w:gz")) as tar:
        for script_type in [
                'premount', 'mount', 'start', 'stop', 'umount', 'postumount'
        ]:
            script_fnm = "/etc/vz/conf/%s.%s" % (vm_settings["vm_name"],
                                                 script_type)
            if os.path.exists(script_fnm):
                tar.add(script_fnm, arcname=script_type)

    # generate and save ovf configuration file
    msg = "Generating ovf file..."
    log.info(msg)
    ovf = _generate_ovf_file(vm_settings, ct_archive_fnm)
    ovf_fnm = path.join(unpacked_dir, "%s.ovf" % vm_settings["template_name"])
    with open(ovf_fnm, 'w') as f:
        ovf.writeFile(f, pretty=True, encoding='UTF-8')

    # pack container archive and ovf file
    msg = "Archiving..."
    log.info(msg)
    ovf_archive_fnm = path.join(dest_dir,
                                "%s.ova" % vm_settings["template_name"])
    with closing(tarfile.open(ovf_archive_fnm, "w")) as tar:
        tar.add(ct_archive_fnm, arcname=path.basename(ct_archive_fnm))
        tar.add(ovf_fnm, arcname=path.basename(ovf_fnm))
        tar.add(ct_scripts_fnm, arcname=path.basename(ct_scripts_fnm))

    calculate_hash(ovf_archive_fnm)
    log.info("Done! Saved template at %s" % ovf_archive_fnm)
Exemple #5
0
def save_as_ovf(vm_settings, storage_pool):
    """
    Creates ovf template archive for the specified container.
    Steps:
        - archive container directory
        - generate ovf configuration file
        - pack ovf and container archive into tar.gz file
    """
    dest_dir = path.join(get_config().getstring('general', 'storage-endpoint'), storage_pool, "openvz")
    unpacked_dir = path.join(dest_dir, "unpacked")
    ct_archive_fnm = path.join(unpacked_dir, "%s.tar.gz" % vm_settings["template_name"])
    ct_source_dir = path.join("/vz/private", vm_settings["vm_name"])

    # Pack vm container catalog
    log = get_logger()
    msg = "Archiving VM container catalog %s. This may take a while..." % ct_source_dir
    log.info(msg)
    with closing(tarfile.open(ct_archive_fnm, "w:gz")) as tar:
        for f in os.listdir(ct_source_dir):
            tar.add(path.join(ct_source_dir, f), arcname=f)

    # Archive action scripts if they are present
    msg = "Adding action scripts..."
    log.info(msg)
    ct_scripts_fnm = path.join(unpacked_dir, "%s.scripts.tar.gz" % vm_settings["template_name"])
    with closing(tarfile.open(ct_scripts_fnm, "w:gz")) as tar:
        for script_type in ['premount', 'mount', 'start', 'stop', 'umount', 'postumount']:
            script_fnm = "/etc/vz/conf/%s.%s" % (vm_settings["vm_name"], script_type)
            if os.path.exists(script_fnm):
                tar.add(script_fnm, arcname=script_type)

    # generate and save ovf configuration file
    msg = "Generating ovf file..."
    log.info(msg)
    ovf = _generate_ovf_file(vm_settings, ct_archive_fnm)
    ovf_fnm = path.join(unpacked_dir, "%s.ovf" % vm_settings["template_name"])
    with open(ovf_fnm, 'w') as f:
        ovf.writeFile(f, pretty=True, encoding='UTF-8')

    # pack container archive and ovf file
    msg = "Archiving..."
    log.info(msg)
    ovf_archive_fnm = path.join(dest_dir, "%s.ova" % vm_settings["template_name"])
    with closing(tarfile.open(ovf_archive_fnm, "w")) as tar:
        tar.add(ct_archive_fnm, arcname=path.basename(ct_archive_fnm))
        tar.add(ovf_fnm, arcname=path.basename(ovf_fnm))
        tar.add(ct_scripts_fnm, arcname=path.basename(ct_scripts_fnm))

    calculate_hash(ovf_archive_fnm)
    log.info("Done! Saved template at %s" % ovf_archive_fnm)
Exemple #6
0
def save_as_ovf(vm_settings, storage_pool, unpack=True):
    """
    Creates ovf template archive for the specified VM.
    Steps:
        - relocate kvm disk files
        - generate ovf configuration file
        - pack ovf and disk files into tar.gz file
        - (if unpack) leave generated files as unpacked
    """

    config = get_config()
    log = get_logger()
    target_dir = path.join(config.getstring('general', 'storage-endpoint'),
                           storage_pool, "kvm")
    if unpack:
        target_dir = path.join(target_dir, 'unpacked')
    # prepare file system
    msg = "Preparing disks... (This may take a while)"
    log.info(msg)
    vm_settings["disks"] = _prepare_disks(vm_settings, target_dir)

    # generate and save ovf configuration file
    msg = "Generating ovf file..."
    log.info(msg)
    ovf = _generate_ovf_file(vm_settings)
    ovf_fnm = path.join(target_dir, "%s.ovf" % vm_settings["template_name"])
    with open(ovf_fnm, 'w') as f:
        ovf.writeFile(f, pretty=True, encoding='UTF-8')

    # pack container archive and ovf file
    msg = "Archiving..."
    log.info(msg)
    arch_location = path.join(config.getstring('general', 'storage-endpoint'),
                              storage_pool, "kvm")
    ovf_archive_fnm = path.join(arch_location,
                                "%s.ova" % vm_settings["template_name"])
    with closing(tarfile.open(ovf_archive_fnm, "w")) as tar:
        tar.add(ovf_fnm, arcname=path.basename(ovf_fnm))
        for disk in vm_settings["disks"]:
            tar.add(disk["new_path"], arcname=path.basename(disk["new_path"]))

    # remove generated files
    if not unpack:
        os.remove(ovf_fnm)
        for disk in vm_settings["disks"]:
            os.remove(disk["new_path"])

    calculate_hash(ovf_archive_fnm)
    msg = "Done! Template saved at %s" % ovf_archive_fnm
    log.info(msg)
Exemple #7
0
def import_template(template, vm_type, storage_pool = c('general', 'default-storage-pool')):
    """Import external template into ON storage pool"""
    if not os.path.exists(template):
        raise RuntimeError("Template not found: " % template)
    if not template.endswith('tar'):
        raise RuntimeError("Expecting a file ending with .tar for a template")
    storage_endpoint = c('general', 'storage-endpoint')
    tmpl_name = os.path.basename(template)
    target_file = os.path.join(storage_endpoint, storage_pool, vm_type, tmpl_name)
    print "Copying template to the storage pool..."
    print template, target_file
    shutil.copyfile(template, target_file)
    calculate_hash(target_file)
    print "Unpacking..."
    unpack_template(storage_pool, vm_type, tmpl_name)
Exemple #8
0
def save_as_ovf(vm_settings, storage_pool, unpack=True):
    """
    Creates ovf template archive for the specified VM.
    Steps:
        - relocate kvm disk files
        - generate ovf configuration file
        - pack ovf and disk files into tar.gz file
        - (if unpack) leave generated files as unpacked
    """

    config = get_config()
    log = get_logger()
    target_dir = path.join(config.getstring('general', 'storage-endpoint'), storage_pool, "kvm")
    if unpack:
        target_dir = path.join(target_dir, 'unpacked')
    # prepare file system
    msg = "Preparing disks... (This may take a while)"
    log.info(msg)
    vm_settings["disks"] = _prepare_disks(vm_settings, target_dir)

    # generate and save ovf configuration file
    msg = "Generating ovf file..."
    log.info(msg)
    ovf = _generate_ovf_file(vm_settings)
    ovf_fnm = path.join(target_dir, "%s.ovf" % vm_settings["template_name"])
    with open(ovf_fnm, 'w') as f:
        ovf.writeFile(f, pretty=True, encoding='UTF-8')

    # pack container archive and ovf file
    msg = "Archiving..."
    log.info(msg)
    arch_location = path.join(config.getstring('general', 'storage-endpoint'), storage_pool, "kvm")
    ovf_archive_fnm = path.join(arch_location, "%s.ova" % vm_settings["template_name"])
    with closing(tarfile.open(ovf_archive_fnm, "w")) as tar:
        tar.add(ovf_fnm, arcname=path.basename(ovf_fnm))
        for disk in vm_settings["disks"]:
            tar.add(disk["new_path"], arcname=path.basename(disk["new_path"]))

    # remove generated files
    if not unpack:
        os.remove(ovf_fnm)
        for disk in vm_settings["disks"]:
            os.remove(disk["new_path"])

    calculate_hash(ovf_archive_fnm)
    msg = "Done! Template saved at %s" % ovf_archive_fnm
    log.info(msg)
Exemple #9
0
def import_template(template, vm_type, storage_pool=None):
    """Import external template into ON storage pool"""
    config = get_config()

    if not storage_pool:
        storage_pool = config.getstring('general', 'default-storage-pool')

    if not os.path.exists(template):
        raise RuntimeError("Template not found: %s" % template)

    if not (template.endswith('tar') or template.endswith('ova')):
        raise RuntimeError("Expecting a file ending with .tar or .ova for a template, %s" % template)

    tmpl_name = os.path.basename(template)
    target_file = os.path.join(storage.get_pool_path(storage_pool), vm_type, tmpl_name)

    log.info("Copying template to the storage pool... %s -> %s" % (template, target_file))
    shutil.copyfile(template, target_file)
    calculate_hash(target_file)

    log.info("Unpacking template %s..." % target_file)
    extension = 'ova' if template.endswith('ova') else 'tar'
    unpack_template(storage_pool, vm_type, tmpl_name.rstrip('.%s' % extension))
Exemple #10
0
def import_template(template, vm_type, storage_pool=None):
    """Import external template into ON storage pool"""
    config = get_config()

    if not storage_pool:
        storage_pool = config.getstring("general", "default-storage-pool")

    if not os.path.exists(template):
        raise RuntimeError("Template not found: %s" % template)

    if not (template.endswith("tar") or template.endswith("ova")):
        raise RuntimeError("Expecting a file ending with .tar or .ova for a template, %s" % template)

    tmpl_name = os.path.basename(template)
    target_file = os.path.join(storage.get_pool_path(storage_pool), vm_type, tmpl_name)

    log.info("Copying template to the storage pool... %s -> %s" % (template, target_file))
    shutil.copyfile(template, target_file)
    calculate_hash(target_file)

    log.info("Unpacking template %s..." % target_file)
    extension = "ova" if template.endswith("ova") else "tar"
    unpack_template(storage_pool, vm_type, tmpl_name.rstrip(".%s" % extension))