Ejemplo n.º 1
0
    def extract_nulecule_data(self, image, source, dest, update=False):
        """
        Extract the Nulecule contents from a container into a destination
        directory.

        Args:
            image (str): Docker image name
            source (str): Source directory in Docker image to copy from
            dest (str): Path to destination directory on host
            update (bool): Update destination directory if it exists when
                           True

        Returns:
            None
        """
        logger.info(
            'Extracting Nulecule data from image %s to %s' % (image, dest))
        if self.dryrun:
            return

        # Create a temporary directory for extraction
        tmpdir = '/tmp/nulecule-{}'.format(uuid.uuid1())

        self.extract_files(image, source=source, dest=tmpdir)

        # If the application already exists locally then need to
        # make sure the local app id is the same as the one requested
        # on the command line.
        mainfile = os.path.join(dest, MAIN_FILE)
        tmpmainfile = os.path.join(tmpdir, MAIN_FILE)
        if os.path.exists(mainfile):
            existing_id = Utils.getAppId(mainfile)
            new_id = Utils.getAppId(tmpmainfile)
            cockpit_logger.info("Loading app_id %s" % new_id)
            if existing_id != new_id:
                raise NuleculeException(
                    "Existing app (%s) and requested app (%s) differ" %
                    (existing_id, new_id))
            # If app exists and no update requested then move on
            if update:
                logger.info("App exists locally. Performing update...")
            else:
                logger.info("App exists locally and no update requested")
                return

        # Copy files from tmpdir into place
        logger.debug('Copying nulecule data from %s to %s' % (tmpdir, dest))
        Utils.copy_dir(tmpdir, dest, update)

        # Clean up tmpdir
        logger.debug('Removing tmp dir: %s' % tmpdir)
        Utils.rm_dir(tmpdir)

        # Set the proper permissions on the extracted folder
        Utils.setFileOwnerGroup(dest)
Ejemplo n.º 2
0
    def extract_nulecule_data(self, image, source, dest, update=False):
        """
        Extract the Nulecule contents from a container into a destination
        directory.

        Args:
            image (str): Docker image name
            source (str): Source directory in Docker image to copy from
            dest (str): Path to destination directory on host
            update (bool): Update destination directory if it exists when
                           True

        Returns:
            None
        """
        logger.info("Extracting Nulecule data from image %s to %s" % (image, dest))
        if self.dryrun:
            return

        # Create a temporary directory for extraction
        tmpdir = "/tmp/nulecule-{}".format(uuid.uuid1())

        self.extract_files(image, source=source, dest=tmpdir)

        # If the application already exists locally then need to
        # make sure the local app id is the same as the one requested
        # on the command line.
        mainfile = os.path.join(dest, MAIN_FILE)
        tmpmainfile = os.path.join(tmpdir, MAIN_FILE)
        if os.path.exists(mainfile):
            existing_id = Utils.getAppId(mainfile)
            new_id = Utils.getAppId(tmpmainfile)
            cockpit_logger.info("Loading app_id %s" % new_id)
            if existing_id != new_id:
                raise NuleculeException("Existing app (%s) and requested app (%s) differ" % (existing_id, new_id))
            # If app exists and no update requested then move on
            if update:
                logger.info("App exists locally. Performing update...")
            else:
                logger.info("App exists locally and no update requested")
                return

        # Copy files from tmpdir into place
        logger.debug("Copying nulecule data from %s to %s" % (tmpdir, dest))
        Utils.copy_dir(tmpdir, dest, update)

        # Clean up tmpdir
        logger.debug("Removing tmp dir: %s" % tmpdir)
        Utils.rm_dir(tmpdir)

        # Set the proper permissions on the extracted folder
        Utils.setFileOwnerGroup(dest)
Ejemplo n.º 3
0
    def extract(self, image, source, dest, update=False):
        """
        Extracts content from a directory in a Docker image to specified
        destination.

        Args:
            image (str): Docker image name
            source (str): Source directory in Docker image to copy from
            dest (str): Path to destination directory on host
            update (bool): Update destination directory if it exists when
                           True

        Returns:
            None
        """
        logger.info('Extracting nulecule data from image: %s to %s' %
                    (image, dest))
        if self.dryrun:
            return

        # Create dummy container
        run_cmd = [
            self.docker_cli, 'create', '--entrypoint', '/bin/true', image
        ]
        logger.debug('Creating docker container: %s' % ' '.join(run_cmd))
        container_id = subprocess.check_output(run_cmd).strip()

        # Copy files out of dummy container to tmpdir
        tmpdir = '/tmp/nulecule-{}'.format(uuid.uuid1())
        cp_cmd = [
            self.docker_cli, 'cp',
            '%s:/%s' % (container_id, source), tmpdir
        ]
        logger.debug('Copying data from Docker container: %s' %
                     ' '.join(cp_cmd))
        subprocess.call(cp_cmd)

        # There has been some inconsistent behavior where docker cp
        # will either copy out the entire dir /APP_ENT_PATH/*files* or
        # it will copy out just /*files* without APP_ENT_PATH. Detect
        # that here and adjust accordingly.
        src = os.path.join(tmpdir, APP_ENT_PATH)
        if not os.path.exists(src):
            src = tmpdir

        # If the application already exists locally then need to
        # make sure the local app id is the same as the one requested
        # on the command line.
        mainfile = os.path.join(dest, MAIN_FILE)
        tmpmainfile = os.path.join(src, MAIN_FILE)
        if os.path.exists(mainfile):
            existing_id = Utils.getAppId(mainfile)
            new_id = Utils.getAppId(tmpmainfile)
            cockpit_logger.info("Loading app_id %s ." % new_id)
            if existing_id != new_id:
                raise NuleculeException(
                    "Existing app (%s) and requested app (%s) differ" %
                    (existing_id, new_id))
            # If app exists and no update requested then move on
            if update:
                logger.info("App exists locally. Performing update...")
            else:
                logger.info("App exists locally and no update requested")
                return

        # Copy files
        logger.debug('Copying nulecule data from %s to %s' % (src, dest))
        Utils.copy_dir(src, dest, update)
        logger.debug('Removing tmp dir: %s' % tmpdir)
        Utils.rm_dir(tmpdir)

        # Clean up dummy container
        rm_cmd = [self.docker_cli, 'rm', '-f', container_id]
        logger.debug('Removing Docker container: %s' % ' '.join(rm_cmd))
        subprocess.call(rm_cmd)
Ejemplo n.º 4
0
    def extract(self, image, source, dest, update=False):
        """
        Extracts content from a directory in a Docker image to specified
        destination.

        Args:
            image (str): Docker image name
            source (str): Source directory in Docker image to copy from
            dest (str): Path to destination directory on host
            update (bool): Update destination directory if it exists when
                           True

        Returns:
            None
        """
        logger.info("Extracting Nulecule data from image %s to %s" % (image, dest))
        if self.dryrun:
            return

        # Create dummy container
        run_cmd = [self.docker_cli, "create", "--entrypoint", "/bin/true", image]
        logger.debug("Creating docker container: %s" % " ".join(run_cmd))
        container_id = subprocess.check_output(run_cmd).strip()

        # Copy files out of dummy container to tmpdir
        tmpdir = "/tmp/nulecule-{}".format(uuid.uuid1())
        cp_cmd = [self.docker_cli, "cp", "%s:/%s" % (container_id, source), tmpdir]
        logger.debug("Copying data from docker container: %s" % " ".join(cp_cmd))
        subprocess.check_output(cp_cmd)

        # There has been some inconsistent behavior where docker cp
        # will either copy out the entire dir /APP_ENT_PATH/*files* or
        # it will copy out just /*files* without APP_ENT_PATH. Detect
        # that here and adjust accordingly.
        src = os.path.join(tmpdir, APP_ENT_PATH)
        if not os.path.exists(src):
            src = tmpdir

        # If the application already exists locally then need to
        # make sure the local app id is the same as the one requested
        # on the command line.
        mainfile = os.path.join(dest, MAIN_FILE)
        tmpmainfile = os.path.join(src, MAIN_FILE)
        if os.path.exists(mainfile):
            existing_id = Utils.getAppId(mainfile)
            new_id = Utils.getAppId(tmpmainfile)
            cockpit_logger.info("Loading app_id %s" % new_id)
            if existing_id != new_id:
                raise NuleculeException("Existing app (%s) and requested app (%s) differ" % (existing_id, new_id))
            # If app exists and no update requested then move on
            if update:
                logger.info("App exists locally. Performing update...")
            else:
                logger.info("App exists locally and no update requested")
                return

        # Copy files
        logger.debug("Copying nulecule data from %s to %s" % (src, dest))
        Utils.copy_dir(src, dest, update)
        logger.debug("Removing tmp dir: %s" % tmpdir)
        Utils.rm_dir(tmpdir)

        # Clean up dummy container
        rm_cmd = [self.docker_cli, "rm", "-f", container_id]
        logger.debug("Removing docker container: %s" % " ".join(rm_cmd))
        subprocess.check_output(rm_cmd)