Example #1
0
 def run(self, context):
     swift = self.get_object_client(context)
     # regex from tempfile's _RandomNameSequence characters
     _regex = '^/tmp/file-mistral-action[A-Za-z0-9_]{6}$'
     if (not isinstance(self.path, six.string_types) or
             not re.match(_regex, self.path)):
         msg = "Path does not match %s" % _regex
         return actions.Result(error={"msg": msg})
     try:
         swiftutils.download_container(swift, self.container, self.path)
         filenames = os.listdir(self.path)
         if len(filenames) == 0:
             pass
         elif len(filenames) == 1:
             tarball.extract_tarball(self.path, filenames[0], remove=True)
         else:
             msg = "%d objects found in container: %s" \
                   % (len(filenames), self.container)
             msg += " but one object was expected."
             return actions.Result(error={"msg": six.text_type(msg)})
     except swiftexceptions.ClientException as err:
         msg = "Error attempting an operation on container: %s" % err
         return actions.Result(error={"msg": six.text_type(msg)})
     except (OSError, IOError) as err:
         msg = "Error while writing file: %s" % err
         return actions.Result(error={"msg": six.text_type(msg)})
     except processutils.ProcessExecutionError as err:
         msg = "Error while creating a tarball: %s" % err
         return actions.Result(error={"msg": six.text_type(msg)})
     except Exception as err:
         msg = "Error exporting logs: %s" % err
         return actions.Result(error={"msg": six.text_type(msg)})
     msg = "Swift container: %s has been extracted to directory: %s" % (
         self.container, self.path)
     return actions.Result(data={"msg": msg})
Example #2
0
    def run(self, context):
        swift = self.get_object_client(context)
        swift_service = self.get_object_service(context)

        tmp_dir = tempfile.mkdtemp()
        tarball_name = '%s.tar.gz' % self.plan

        try:
            swiftutils.download_container(swift, self.plan, tmp_dir)
            swiftutils.create_and_upload_tarball(
                swift_service, tmp_dir, self.exports_container, tarball_name,
                delete_after=self.delete_after)
        except swiftexceptions.ClientException as err:
            msg = "Error attempting an operation on container: %s" % err
            return actions.Result(error=msg)
        except (OSError, IOError) as err:
            msg = "Error while writing file: %s" % err
            return actions.Result(error=msg)
        except processutils.ProcessExecutionError as err:
            msg = "Error while creating a tarball: %s" % err
            return actions.Result(error=msg)
        except Exception as err:
            msg = "Error exporting plan: %s" % err
            return actions.Result(error=msg)
        finally:
            shutil.rmtree(tmp_dir)
def download_validation(swift, plan, validation):
    """Downloads validations from Swift to a temporary location"""
    dst_dir = '/tmp/{}-validations'.format(plan)

    # Download the whole default validations container
    swift_utils.download_container(
        swift,
        constants.VALIDATIONS_CONTAINER_NAME,
        dst_dir,
        overwrite_only_newer=True
    )

    filename = '{}.yaml'.format(validation)
    swift_path = os.path.join(constants.CUSTOM_VALIDATIONS_FOLDER, filename)
    dst_path = os.path.join(dst_dir, filename)

    # If a custom validation with that name exists, get it from the plan
    # container and override. Otherwise, the default one will be used.
    try:
        contents = swift_utils.get_object_string(swift, plan, swift_path)
    except swiftexceptions.ClientException:
        pass
    else:
        with open(dst_path, 'w') as f:
            f.write(contents)

    return dst_path
Example #4
0
    def run(self, context):
        swift = self.get_object_client(context)
        swift_service = self.get_object_service(context)

        tmp_dir = tempfile.mkdtemp()
        tarball_name = '%s.tar.gz' % self.plan

        try:
            swiftutils.download_container(swift, self.plan, tmp_dir)
            swiftutils.create_and_upload_tarball(
                swift_service, tmp_dir, self.exports_container, tarball_name,
                delete_after=self.delete_after)
        except swiftexceptions.ClientException as err:
            msg = "Error attempting an operation on container: %s" % err
            return actions.Result(error=msg)
        except (OSError, IOError) as err:
            msg = "Error while writing file: %s" % err
            return actions.Result(error=msg)
        except processutils.ProcessExecutionError as err:
            msg = "Error while creating a tarball: %s" % err
            return actions.Result(error=msg)
        except Exception as err:
            msg = "Error exporting plan: %s" % err
            return actions.Result(error=msg)
        finally:
            shutil.rmtree(tmp_dir)
Example #5
0
 def run(self, context):
     swift = self.get_object_client(context)
     # regex from tempfile's _RandomNameSequence characters
     _regex = '^/tmp/file-mistral-action[A-Za-z0-9_]{6}$'
     if (not isinstance(self.path, six.string_types)
             or not re.match(_regex, self.path)):
         msg = "Path does not match %s" % _regex
         return actions.Result(error={"msg": msg})
     try:
         swiftutils.download_container(swift, self.container, self.path)
         filenames = os.listdir(self.path)
         if len(filenames) == 0:
             pass
         elif len(filenames) == 1:
             tarball.extract_tarball(self.path, filenames[0], remove=True)
         else:
             msg = "%d objects found in container: %s" \
                   % (len(filenames), self.container)
             msg += " but one object was expected."
             return actions.Result(error={"msg": six.text_type(msg)})
     except swiftexceptions.ClientException as err:
         msg = "Error attempting an operation on container: %s" % err
         return actions.Result(error={"msg": six.text_type(msg)})
     except (OSError, IOError) as err:
         msg = "Error while writing file: %s" % err
         return actions.Result(error={"msg": six.text_type(msg)})
     except processutils.ProcessExecutionError as err:
         msg = "Error while creating a tarball: %s" % err
         return actions.Result(error={"msg": six.text_type(msg)})
     except Exception as err:
         msg = "Error exporting logs: %s" % err
         return actions.Result(error={"msg": six.text_type(msg)})
     msg = "Swift container: %s has been extracted to directory: %s" % (
         self.container, self.path)
     return actions.Result(data={"msg": msg})
Example #6
0
def download_validation(swift, plan, validation):
    """Downloads validations from Swift to a temporary location"""
    dst_dir = '/tmp/{}-validations'.format(plan)

    # Download the whole default validations container
    swift_utils.download_container(
        swift,
        constants.VALIDATIONS_CONTAINER_NAME,
        dst_dir,
        overwrite_only_newer=True
    )

    filename = '{}.yaml'.format(validation)
    swift_path = os.path.join(constants.CUSTOM_VALIDATIONS_FOLDER, filename)
    dst_path = os.path.join(dst_dir, filename)

    # If a custom validation with that name exists, get it from the plan
    # container and override. Otherwise, the default one will be used.
    try:
        contents = swift_utils.get_object_string(swift, plan, swift_path)
    except swiftexceptions.ClientException:
        pass
    else:
        with open(dst_path, 'w') as f:
            f.write(contents)

    return dst_path
Example #7
0
 def run(self, context):
     swift = self.get_object_client(context)
     swiftutils.download_container(swift, self.container_config,
                                   self.work_dir)
     symlink_path = os.path.join(os.path.dirname(self.work_dir),
                                 'config-download-latest')
     if os.path.exists(symlink_path):
         os.unlink(symlink_path)
     os.symlink(self.work_dir, symlink_path)
     return self.work_dir
Example #8
0
 def run(self, context):
     swift = self.get_object_client(context)
     swiftutils.download_container(swift, self.container_config,
                                   self.work_dir)
     symlink_path = os.path.join(
         os.path.dirname(self.work_dir), 'config-download-latest')
     if os.path.exists(symlink_path):
         os.unlink(symlink_path)
     os.symlink(self.work_dir, symlink_path)
     return self.work_dir
Example #9
0
    def run(self, context):
        heat = self.get_orchestration_client(context)
        swift = self.get_object_client(context)

        # Since the config-download directory is now a git repo, first download
        # the existing config container if it exists so we can reuse the
        # existing git repo.
        try:
            swiftutils.download_container(swift, self.container_config,
                                          self.config_dir)
            # Delete the existing container before we re-upload, otherwise
            # files may not be fully overwritten.
            swiftutils.delete_container(swift, self.container_config)
        except swiftexceptions.ClientException as err:
            if err.http_status != 404:
                raise

        # Delete downloaded tarball as it will be recreated later and we don't
        # want to include the old tarball in the new tarball.
        old_tarball_path = os.path.join(self.config_dir,
                                        '%s.tar.gz' % self.container_config)
        if os.path.exists(old_tarball_path):
            os.unlink(old_tarball_path)

        config = ooo_config.Config(heat)
        message = ('Automatic commit by Mistral GetOvercloudConfig action.\n\n'
                   'User: {user}\n'
                   'Project: {project}'.format(user=context.user_name,
                                               project=context.project_name))
        config_path = config.download_config(self.container,
                                             self.config_dir,
                                             self.config_type,
                                             preserve_config_dir=True,
                                             commit_message=message)

        with tempfile.NamedTemporaryFile() as tmp_tarball:
            tarball.create_tarball(config_path,
                                   tmp_tarball.name,
                                   excludes=['.tox', '*.pyc', '*.pyo'])
            tarball.tarball_extract_to_swift_container(
                self.get_object_client(context), tmp_tarball.name,
                self.container_config)
            # Also upload the tarball to the container for use by export later
            with open(tmp_tarball.name, 'rb') as t:
                swift.put_object(self.container_config,
                                 '%s.tar.gz' % self.container_config, t)
        if os.path.exists(config_path):
            shutil.rmtree(config_path)
Example #10
0
    def run(self, context):
        heat = self.get_orchestration_client(context)
        swift = self.get_object_client(context)

        # Since the config-download directory is now a git repo, first download
        # the existing config container if it exists so we can reuse the
        # existing git repo.
        try:
            swiftutils.download_container(swift, self.container_config,
                                          self.config_dir)
            # Delete the existing container before we re-upload, otherwise
            # files may not be fully overwritten.
            swiftutils.delete_container(swift, self.container_config)
        except swiftexceptions.ClientException as err:
            if err.http_status != 404:
                raise

        # Delete downloaded tarball as it will be recreated later and we don't
        # want to include the old tarball in the new tarball.
        old_tarball_path = os.path.join(
            self.config_dir, '%s.tar.gz' % self.container_config)
        if os.path.exists(old_tarball_path):
            os.unlink(old_tarball_path)

        config = ooo_config.Config(heat)
        message = ('Automatic commit by Mistral GetOvercloudConfig action.\n\n'
                   'User: {user}\n'
                   'Project: {project}'.format(user=context.user_name,
                                               project=context.project_name))
        config_path = config.download_config(self.container, self.config_dir,
                                             self.config_type,
                                             preserve_config_dir=True,
                                             commit_message=message)

        with tempfile.NamedTemporaryFile() as tmp_tarball:
            tarball.create_tarball(config_path, tmp_tarball.name,
                                   excludes=['.tox', '*.pyc', '*.pyo'])
            tarball.tarball_extract_to_swift_container(
                self.get_object_client(context),
                tmp_tarball.name,
                self.container_config)
            # Also upload the tarball to the container for use by export later
            with open(tmp_tarball.name, 'rb') as t:
                swift.put_object(self.container_config,
                                 '%s.tar.gz' % self.container_config, t)
        if os.path.exists(config_path):
            shutil.rmtree(config_path)
Example #11
0
    def run(self, context):
        heat = self.get_orchestration_client(context)
        swift = self.get_object_client(context)

        # Since the config-download directory is now a git repo, first download
        # the existing config container if it exists so we can reuse the
        # existing git repo.
        try:
            swiftutils.download_container(swift, self.container_config,
                                          self.config_dir)
            # Delete the existing container before we re-upload, otherwise
            # files may not be fully overwritten.
            swiftutils.delete_container(swift, self.container_config)
        except swiftexceptions.ClientException as err:
            if err.http_status != 404:
                raise

        config = ooo_config.Config(heat)
        message = ('Automatic commit by Mistral GetOvercloudConfig action.\n\n'
                   'User: {user}\n'
                   'Project: {project}'.format(user=context.user_name,
                                               project=context.project_name))
        config_path = config.download_config(self.container,
                                             self.config_dir,
                                             preserve_config_dir=True,
                                             commit_message=message)

        with tempfile.NamedTemporaryFile() as tmp_tarball:
            tarball.create_tarball(config_path,
                                   tmp_tarball.name,
                                   excludes=['.tox', '*.pyc', '*.pyo'])
            tarball.tarball_extract_to_swift_container(
                self.get_object_client(context), tmp_tarball.name,
                self.container_config)
        if os.path.exists(config_path):
            shutil.rmtree(config_path)