-
Notifications
You must be signed in to change notification settings - Fork 566
add new field related_integrations
to the post build
#2060
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 62 commits
2811249
1381e11
9d43b5b
21b1a8e
0f4f88e
9982dc2
62c8aa2
6ae5318
2dbff89
f4e39f6
7d25987
fa8fc84
f53f9d7
d818c57
6dfde72
0170361
3ccc408
9685bbe
ddcbbde
aff4d08
5dbab29
50c8664
acc408c
e031f2c
40c4eb9
76cb759
866e577
8457a3c
7d68f51
5fdba7e
07c331b
357662b
c981d9f
03fcef5
b244b18
3b2961e
db5ee18
30d0c82
71ffa74
47f67cf
92e3c1c
561987e
15fc949
a88f784
1f76bbd
f5e243e
553e47b
165f383
d57f5dc
684292d
ef58a09
1e6efa1
ca3a082
ee8ac61
986f19b
1dcbff8
fd26283
e6230cb
481d233
0de0c45
825132a
565b45b
abeccd0
2537d07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
# or more contributor license agreements. Licensed under the Elastic License | ||
# 2.0; you may not use this file except in compliance with the Elastic License | ||
# 2.0. | ||
|
||
"""Functions to support and interact with Kibana integrations.""" | ||
import gzip | ||
import json | ||
import os | ||
import re | ||
from pathlib import Path | ||
|
||
import yaml | ||
from marshmallow import EXCLUDE, Schema, fields, post_load | ||
|
||
from .ghwrap import GithubClient | ||
from .semver import Version | ||
from .utils import INTEGRATION_RULE_DIR, cached, get_etc_path, read_gzip | ||
|
||
MANIFEST_FILE_PATH = Path(get_etc_path('integration-manifests.json.gz')) | ||
|
||
|
||
@cached | ||
def load_integrations_manifests() -> dict: | ||
"""Load the consolidated integrations manifest.""" | ||
return json.loads(read_gzip(get_etc_path('integration-manifests.json.gz'))) | ||
|
||
|
||
class IntegrationManifestSchema(Schema): | ||
name = fields.Str(required=True) | ||
version = fields.Str(required=True) | ||
release = fields.Str(required=True) | ||
description = fields.Str(required=True) | ||
conditions = fields.Dict(required=True) | ||
policy_templates = fields.List(fields.Dict, required=True) | ||
owner = fields.Dict(required=True) | ||
|
||
@post_load | ||
def transform_policy_template(self, data, **kwargs): | ||
data["policy_templates"] = [policy["name"] for policy in data["policy_templates"]] | ||
return data | ||
|
||
|
||
def build_integrations_manifest(token: str, overwrite: bool) -> None: | ||
"""Builds a new local copy of manifest.yaml from integrations Github.""" | ||
if overwrite: | ||
if os.path.exists(MANIFEST_FILE_PATH): | ||
os.remove(MANIFEST_FILE_PATH) | ||
rule_integrations = [d.name for d in Path(INTEGRATION_RULE_DIR).glob('*')] | ||
if "endpoint" in rule_integrations: | ||
rule_integrations.remove("endpoint") | ||
|
||
final_integration_manifests = {integration: {} for integration in rule_integrations} | ||
|
||
# initialize github client and point to package-storage prod | ||
github = GithubClient(token) | ||
client = github.authenticated_client | ||
organization = client.get_organization("elastic") | ||
repository = organization.get_repo("package-storage") | ||
pkg_storage_prod_branch = repository.get_branch("production") | ||
pkg_storage_branch_sha = pkg_storage_prod_branch.commit.sha | ||
|
||
for integration in rule_integrations: | ||
integration_manifests = get_integration_manifests(repository, pkg_storage_branch_sha, | ||
pkg_path=f"packages/{integration}") | ||
for manifest in integration_manifests: | ||
validated_manifest = IntegrationManifestSchema(unknown=EXCLUDE).load(manifest) | ||
package_version = validated_manifest.pop("version") | ||
final_integration_manifests[integration][package_version] = validated_manifest | ||
|
||
manifest_file = gzip.open(MANIFEST_FILE_PATH, "w+") | ||
manifest_file_bytes = json.dumps(final_integration_manifests).encode("utf-8") | ||
manifest_file.write(manifest_file_bytes) | ||
|
||
|
||
def find_least_compatible_version(package: str, integration: str, | ||
current_stack_version: str, packages_manifest: dict) -> str: | ||
"""Finds least compatible version for specified integration based on stack version supplied.""" | ||
integration_manifests = {k: v for k, v in sorted(packages_manifest[package].items(), key=Version)} | ||
|
||
def compare_versions(int_ver: str, pkg_ver: str) -> bool: | ||
"""Compares integration and package version""" | ||
pkg_major, pkg_minor = Version(pkg_ver) | ||
integration_major, integration_minor = Version(int_ver)[:2] | ||
|
||
if int(integration_major) < int(pkg_major) or int(pkg_major) > int(integration_major): | ||
return False | ||
|
||
compatible = Version(int_ver) <= Version(pkg_ver) | ||
return compatible | ||
|
||
for version, manifest in integration_manifests.items(): | ||
for kibana_compat_vers in re.sub(r"\>|\<|\=|\^", "", manifest["conditions"]["kibana.version"]).split(" || "): | ||
if compare_versions(kibana_compat_vers, current_stack_version): | ||
return version | ||
raise Exception(f"no compatible version for integration {package}:{integration}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. going forward, we may just want to return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adjusted in recent commit. Method expects |
||
|
||
|
||
def get_integration_manifests(repository, sha: str, pkg_path: str) -> list: | ||
terrancedejesus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Iterates over specified integrations from package-storage and combines manifests per version.""" | ||
integration = pkg_path.split("/")[-1] | ||
versioned_packages = repository.get_dir_contents(pkg_path, ref=sha) | ||
versions = [p.path.split("/")[-1] for p in versioned_packages] | ||
|
||
manifests = [] | ||
for version in versions: | ||
contents = repository.get_dir_contents(f"{pkg_path}/{version}", ref=sha) | ||
print(f"Processing {integration} - Version: {version}") | ||
|
||
processing_version = contents[0].path.split("/")[2] | ||
manifest_content = [c for c in contents if "manifest" in c.path] | ||
|
||
if len(manifest_content) < 1: | ||
raise Exception(f"manifest file does not exist for {integration}:{processing_version}") | ||
|
||
path = manifest_content[0].path | ||
manifest_content = yaml.safe_load(repository.get_contents(path, ref=sha).decoded_content.decode()) | ||
manifests.append(manifest_content) | ||
|
||
return manifests |
Uh oh!
There was an error while loading. Please reload this page.