Example #1
0
def get_backend_tmpdir():
    """
    provide tmpdir which is scoped for the whole backend

    :return: str, path to the temporary directory
    """
    global _backend_tmpdir
    if _backend_tmpdir is None:
        _backend_tmpdir = mkdtemp()
    return _backend_tmpdir
Example #2
0
 def test_volumes(self):
     im = NspawnImage(repository=image_name,
                      pull_policy=ImagePullPolicy.IF_NOT_PRESENT,
                      location=url)
     dirname = mkdtemp()
     filename = "somefile"
     host_fn = os.path.join(dirname, filename)
     run_cmd(["touch", host_fn])
     cont = im.run_via_binary(volumes=["{}:/opt".format(dirname)])
     cont.execute(["ls", os.path.join("/opt", filename)])
     assert os.path.exists(host_fn)
     cont.execute(["rm", "-f", os.path.join("/opt", filename)])
     assert not os.path.exists(host_fn)
     os.rmdir(dirname)
Example #3
0
    def bootstrap(
            repositories, name, packages=None, additional_packages=None,
            tag="latest", prefix=constants.CONU_ARTIFACT_TAG, packager=None):
        """
        bootstrap Image from scratch. It creates new image based on giver dnf repositories and package setts

        :param repositories:list of repositories
        :param packages: list of base packages in case you don't want to use base packages defined in contants
        :param additional_packages: list of additonal packages
        :param tag: tag the output image
        :param prefix: use some prefix for newly cretaed image (internal usage)
        :param packager: use another packages in case you dont want to use defined in constants (dnf)
        :return: NspawnImage instance
        """
        additional_packages = additional_packages or []
        if packages is None:
            packages = constants.CONU_NSPAWN_BASEPACKAGES
        package_set = packages + additional_packages
        if packager is None:
            packager = constants.BOOTSTRAP_PACKAGER
        mounted_dir = mkdtemp()
        if not os.path.exists(mounted_dir):
            os.makedirs(mounted_dir)
        imdescriptor, tempimagefile = mkstemp()
        image_size = constants.BOOTSTRAP_IMAGE_SIZE_IN_MB
        # create no partitions when create own image
        run_cmd(["dd", "if=/dev/zero", "of={}".format(tempimagefile),
                 "bs=1M", "count=1", "seek={}".format(image_size)])
        run_cmd([constants.BOOTSTRAP_FS_UTIL, tempimagefile])
        # TODO: is there possible to use NspawnImageFS class instead of direct
        # mount/umount, image objects does not exist
        run_cmd(["mount", tempimagefile, mounted_dir])
        if os.path.exists(os.path.join(mounted_dir, "usr")):
            raise ConuException("Directory %s already in use" % mounted_dir)
        if not os.path.exists(mounted_dir):
            os.makedirs(mounted_dir)
        repo_params = []
        repo_file_content = ""
        for cnt in range(len(repositories)):
            repo_params += ["--repofrompath",
                            "{0}{1},{2}".format(prefix, cnt, repositories[cnt])]
            repo_file_content += """
[{NAME}{CNT}]
name={NAME}{CNT}
baseurl={REPO}
enabled=1
gpgcheck=0
""".format(NAME=prefix, CNT=cnt, REPO=repositories[cnt])
        packages += set(additional_packages)
        logger.debug("Install system to direcory: %s" % mounted_dir)
        logger.debug("Install packages: %s" % packages)
        logger.debug("Repositories: %s" % repositories)
        packager_addition = [
            "--installroot",
            mounted_dir,
            "--disablerepo",
            "*",
            "--enablerepo",
            prefix + "*"]
        final_command = packager + packager_addition + repo_params + package_set
        try:
            run_cmd(final_command)
        except Exception as e:
            raise ConuException("Unable to install packages via command: {} (original exception {})".format(final_command, e))
        insiderepopath = os.path.join(
            mounted_dir,
            "etc",
            "yum.repos.d",
            "{}.repo".format(prefix))
        if not os.path.exists(os.path.dirname(insiderepopath)):
            os.makedirs(os.path.dirname(insiderepopath))
        with open(insiderepopath, 'w') as f:
            f.write(repo_file_content)
        run_cmd(["umount", mounted_dir])
        # add sleep before umount, to ensure, that kernel finish ops
        time.sleep(constants.DEFAULT_SLEEP)
        nspawnimage = NspawnImage(repository=name, location=tempimagefile, tag=tag)
        os.remove(tempimagefile)
        os.rmdir(mounted_dir)
        return nspawnimage