def publish(self, units): """ Publish the specified units. Writes the units.json file and symlinks each of the files associated to the unit.storage_path. Publishing is staged in a temporary directory and must use commit() to make the publishing permanent. :param units: A list of units to publish. :type units: iterable :return: The absolute path to the manifest. :rtype: str """ pathlib.mkdir(self.publish_dir) self.tmp_dir = mkdtemp(dir=self.publish_dir) units_path = pathlib.join(self.tmp_dir, UNITS_FILE_NAME) manifest_path = pathlib.join(self.tmp_dir, MANIFEST_FILE_NAME) with UnitWriter(units_path) as writer: for unit in units: self.publish_unit(unit) writer.add(unit) manifest_id = str(uuid4()) manifest = Manifest(manifest_id) manifest.set_units(writer) manifest_path = manifest.write(manifest_path) self.staged = True return manifest_path
def publish(self, units): """ Publish the specified units. Writes the units.json file and symlinks each of the files associated to the unit.storage_path. Publishing is staged in a temporary directory and must use commit() to make the publishing permanent. :param units: A list of units to publish. :type units: iterable :return: The absolute path to the manifest. :rtype: str """ # make the parent dir and a temp dir within it parent_path = os.path.normpath(os.path.join(self.publish_dir, '../')) pathlib.mkdir(parent_path) self.tmp_dir = mkdtemp(dir=parent_path) with UnitWriter(self.tmp_dir) as writer: for unit in units: self.publish_unit(unit) writer.add(unit) manifest_id = str(uuid4()) manifest = Manifest(self.tmp_dir, manifest_id) manifest.units_published(writer) manifest.write() self.staged = True return manifest.path
def download_started(self, report): """ A specific download (request) has started. 1. Check to see if the node sync request has been cancelled and cancel the downloader as needed. 2. Create the destination directory structure as needed. :param report: A nectar download report. :type report: nectar.report.DownloadReport. """ super(self.__class__, self).download_started(report) if not self.request.cancelled(): dir_path = os.path.dirname(report.destination) pathlib.mkdir(dir_path) else: self.request.downloader.cancel()
def publish_unit(self, unit): """ Publish the file associated with the unit into the publish directory. :param unit: A content unit. :type unit: dict """ storage_path = unit.get(constants.STORAGE_PATH) if not storage_path: # not all units have associated files. return unit, None relative_path = unit[constants.RELATIVE_PATH] published_path = pathlib.join(self.tmp_dir, relative_path) pathlib.mkdir(os.path.dirname(published_path)) unit[constants.FILE_SIZE] = os.path.getsize(storage_path) if os.path.isdir(storage_path): tar_dir(storage_path, tar_path(published_path)) unit[constants.TARBALL_PATH] = tar_path(relative_path) else: os.symlink(storage_path, published_path)
def publish_unit(self, unit): """ Publish the file associated with the unit into the publish directory. :param unit: A content unit. :type unit: dict """ storage_path = unit.get('storage_path') if not storage_path: # not all units have associated files. return unit, None relative_path = unit['relative_path'] published_path = pathlib.join(self.tmp_dir, relative_path) pathlib.mkdir(os.path.dirname(published_path)) if os.path.isdir(storage_path): self.tar_dir(storage_path, published_path) unit[constants.PUBLISHED_AS_TARBALL] = True else: os.symlink(storage_path, published_path) unit[constants.PUBLISHED_AS_FILE] = True
def create_request(url, destination, unit, unit_ref): """ Create a content container download request that is compatible with the listener. The destination directory is created as needed. :param url: The download URL. :type url: str :param destination: The absolute path to where the file is to be downloaded. :type destination: str :param unit: A published content unit. :type unit: dict :param unit_ref: A reference to the unit association. :type unit_ref: pulp_node.manifest.UnitRef. :return: A download request. :rtype: Request """ data = {STORAGE_PATH: unit[constants.STORAGE_PATH], UNIT_REF: unit_ref} dir_path = os.path.dirname(destination) pathlib.mkdir(dir_path) request = Request(unit[constants.TYPE_ID], unit[constants.UNIT_KEY], url, destination) request.data = data return request
def create_request(unit_URL, destination, unit, unit_ref): """ Create a nectar download request compatible with the listener. The destination directory is created as needed. :param unit_URL: The download URL. :type unit_URL: str :param destination: The absolute path to where the file is to be downloaded. :type destination: str :param unit: A published content unit. :type unit: dict :param unit_ref: A reference to the unit association. :type unit_ref: pulp_node.manifest.UnitRef. :return: A nectar download request. :rtype: DownloadRequest """ data = { STORAGE_PATH: unit[constants.STORAGE_PATH], UNIT_REF: unit_ref } dir_path = os.path.dirname(destination) pathlib.mkdir(dir_path) return DownloadRequest(unit_URL, destination, data=data)