Example #1
0
    def unpack(self):
        # directory is encoded into string to prevent unicode errors
        directory = tempfile.mkdtemp(dir=self.upaas_config.paths.workdir,
                                     prefix="upaas_package_").encode("utf-8")

        storage = load_handler(self.upaas_config.storage.handler,
                               self.upaas_config.storage.settings)
        if not storage:
            log.error("Storage handler '%s' not "
                      "found" % self.upaas_config.storage.handler)

        workdir = os.path.join(directory, "system")
        pkg_path = os.path.join(directory, self.filename)

        if os.path.exists(self.package_path):
            log.error(_("Package directory already exists: {path}").format(
                path=self.package_path))
            raise UnpackError(_("Package directory already exists"))

        log.info("Fetching package '%s'" % self.filename)
        try:
            storage.get(self.filename, pkg_path)
        except StorageError:
            log.error(_("Storage error while fetching package {name}").format(
                name=self.filename))
            utils.rmdirs(directory)
            raise UnpackError(_("Storage error while fetching package "
                                "{name}").format(name=self.filename))

        log.info("Unpacking package")
        os.mkdir(workdir, 0o755)
        if not tar.unpack_tar(pkg_path, workdir):
            log.error(_("Error while unpacking package to '{workdir}'").format(
                workdir=workdir))
            utils.rmdirs(directory)
            raise UnpackError(_("Error during package unpack"))

        with open(os.path.join(workdir, self.ack_filename), 'w') as ack:
            ack.write(_('Unpacked: {now}').format(now=datetime.datetime.now()))

        for feature in self.application.feature_helper.load_enabled_features():
            feature.after_unpack(self.application, workdir)

        log.info(_("Package unpacked, moving into '{path}'").format(
            path=self.package_path))
        try:
            shutil.move(workdir, self.package_path)
        except shutil.Error as e:
            log.error(_("Error while moving unpacked package to final "
                        "destination: e").format(e=e))
            utils.rmdirs(directory, self.package_path)
            raise UnpackError(_("Can't move to final directory: "
                                "{path}").format(path=self.package_path))
        log.info(_("Package moved"))
        utils.rmdirs(directory)
Example #2
0
    def unpack_os(self, directory, workdir, system_filename=None):
        empty_os_image = False
        if not system_filename:
            system_filename = distro.distro_image_filename()
            empty_os_image = True

        os_image_path = os.path.join(directory, "os.image")
        log.info("Fetching OS image '%s'" % system_filename)
        try:
            self.storage.get(system_filename, os_image_path)
        except StorageError:
            log.error("Storage error while fetching OS image")
            return False
        else:
            log.info("Unpacking OS image")
            if not tar.unpack_tar(os_image_path, workdir):
                log.error("Error while unpacking OS image to '%s'" % workdir)
                return False
        # verify if os is working
        log.info("Checking if OS image is working (will execute /bin/true)")
        try:
            with Chroot(workdir):
                commands.execute('/bin/true',
                                 timeout=self.config.commands.timelimit,
                                 output_loglevel=logging.INFO)
        except Exception as e:
            log.error("Broken OS image! /bin/true failed: %s" % e)
            if empty_os_image:
                try:
                    self.storage.delete(system_filename)
                except StorageError as e:
                    log.error("Storage error while deleting OS image: %s" % e)
                else:
                    log.info("Deleted broken OS image from storage")
                    self.bootstrap_os()
                    return self.unpack_os(directory, workdir,
                                          system_filename=system_filename)
            return False
        else:
            return True