Beispiel #1
0
def build_and_verify(source_dir=None,
                     yamls=None,
                     ui_validate_io=False,
                     validation_output=None,
                     repository=None,
                     output=None):
    """Build and verify constructs an operator bundle from
    a set of files and then verifies it for usefulness and accuracy.

    It returns the bundle as a string.

    :param source_dir: Path to local directory of yaml files to be read.
    :param yamls: List of yaml strings to create bundle with
    :param ui_validate_io: Optional flag to test operatorhub.io specific validation
    :param validation_output: Path to optional output file for validation logs
    :param repository: Repository name for the application

    :raises TypeError: When called with both source_dir and yamls specified

    :raises OpCourierBadYaml: When an invalid yaml file is encountered
    :raises OpCourierBadArtifact: When a file is not any of {CSV, CRD, Package}
    :raises OpCourierBadBundle: When the resulting bundle fails validation
    """

    if source_dir is not None and yamls is not None:
        logger.error("Both source_dir and yamls cannot be defined.")
        raise TypeError(
            "Both source_dir and yamls cannot be specified on function call.")

    yaml_files = []

    if source_dir is not None:
        for filename in os.listdir(source_dir):
            if filename.endswith(".yaml") or filename.endswith(".yml"):
                with open(source_dir + "/" + filename) as f:
                    yaml_files.append(f.read())
    elif yamls is not None:
        yaml_files = yamls

    bundle = BuildCmd().build_bundle(yaml_files)

    valid, validation_results_dict = ValidateCmd(ui_validate_io).validate(
        bundle, repository)

    if validation_output is not None:
        with open(validation_output, 'w') as f:
            f.write(json.dumps(validation_results_dict) + "\n")

    if valid:
        bundle = format_bundle(bundle)
        if output is not None:
            with open(output, 'w') as f:
                yaml.dump(bundle, f, default_flow_style=False)
        return bundle
    else:
        logger.error("Bundle failed validation.")
        raise OpCourierBadBundle(
            "Resulting bundle is invalid, input yaml is improperly defined.",
            validation_info=validation_results_dict)
 def bundle(self):
     if self.nested:
         raise AttributeError(
             'VerifiedManifest does not have the bundle property '
             'in nested cases.')
     return format_bundle(self.bundle_dict)