Skip to content

Commit

Permalink
ruff pre-commit format
Browse files Browse the repository at this point in the history
Signed-off-by: Cagri Yonca <cagri@ibm.com>
  • Loading branch information
CagriYonca committed Oct 10, 2024
1 parent ed15e4d commit 9fe367b
Show file tree
Hide file tree
Showing 128 changed files with 2,850 additions and 1,829 deletions.
36 changes: 24 additions & 12 deletions .tekton/.currency/scripts/generate_report.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Standard Libraries
import re
import json
import re

import pandas as pd

# Third Party
import requests
import pandas as pd
from bs4 import BeautifulSoup
from kubernetes import client, config

Expand Down Expand Up @@ -117,7 +118,9 @@ def process_taskrun_logs(
match = re.search("Successfully installed .* (starlette-[^\s]+)", logs)
tekton_ci_output += f"{match[1]}\n"
elif task_name == "python-tracer-unittest-googlecloud-task":
match = re.search("Successfully installed .* (google-cloud-storage-[^\s]+)", logs)
match = re.search(
"Successfully installed .* (google-cloud-storage-[^\s]+)", logs
)
tekton_ci_output += f"{match[1]}\n"
elif task_name == "python-tracer-unittest-default-task":
for line in logs.splitlines():
Expand All @@ -140,29 +143,38 @@ def get_tekton_ci_output():
core_v1_client = client.CoreV1Api()

task_name = "python-tracer-unittest-gevent-starlette-task"
taskrun_filter = lambda tr: tr["status"]["conditions"][0]["type"] == "Succeeded"

def taskrun_filter(tr):
return tr["status"]["conditions"][0]["type"] == "Succeeded"

starlette_taskruns = get_taskruns(namespace, task_name, taskrun_filter)

tekton_ci_output = process_taskrun_logs(
starlette_taskruns, core_v1_client, namespace, task_name, ""
)

task_name = "python-tracer-unittest-googlecloud-task"
taskrun_filter = (
lambda tr: tr["metadata"]["name"].endswith("unittest-googlecloud-0")
and tr["status"]["conditions"][0]["type"] == "Succeeded"
)

def taskrun_filter(tr):
return (
tr["metadata"]["name"].endswith("unittest-googlecloud-0")
and tr["status"]["conditions"][0]["type"] == "Succeeded"
)

googlecloud_taskruns = get_taskruns(namespace, task_name, taskrun_filter)

tekton_ci_output = process_taskrun_logs(
googlecloud_taskruns, core_v1_client, namespace, task_name, tekton_ci_output
)

task_name = "python-tracer-unittest-default-task"
taskrun_filter = (
lambda tr: tr["metadata"]["name"].endswith("unittest-default-3")
and tr["status"]["conditions"][0]["type"] == "Succeeded"
)

def taskrun_filter(tr):
return (
tr["metadata"]["name"].endswith("unittest-default-3")
and tr["status"]["conditions"][0]["type"] == "Succeeded"
)

default_taskruns = get_taskruns(namespace, task_name, taskrun_filter)

tekton_ci_output = process_taskrun_logs(
Expand Down
30 changes: 19 additions & 11 deletions bin/announce_release_on_slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@


def ensure_environment_variables_are_present():
required_env_vars = ('GITHUB_RELEASE_TAG', 'GITHUB_TOKEN',
'SLACK_BOT_TOKEN', 'SLACK_CHANNEL_ID_RELEASES')
required_env_vars = (
"GITHUB_RELEASE_TAG",
"GITHUB_TOKEN",
"SLACK_BOT_TOKEN",
"SLACK_CHANNEL_ID_RELEASES",
)

for v in required_env_vars:
if not os.environ.get(v):
Expand All @@ -33,7 +37,8 @@ def get_gh_release_info_text_with_token(release_tag, access_token):
f"Tag: {release.tag_name}\n"
f"Created at: {release.created_at}\n"
f"Published at: {release.published_at}\n"
f"{release.body}\n")
f"{release.body}\n"
)

logging.info(msg)
return msg
Expand All @@ -42,8 +47,10 @@ def get_gh_release_info_text_with_token(release_tag, access_token):
def post_on_slack_channel(slack_token, slack_channel_id, message_text):
api_url = "https://slack.com/api/chat.postMessage"

headers = {"Authorization": f"Bearer {slack_token}",
"Content-Type": "application/json"}
headers = {
"Authorization": f"Bearer {slack_token}",
"Content-Type": "application/json",
}
body = {"channel": slack_channel_id, "text": message_text}

response = requests.post(api_url, headers=headers, data=json.dumps(body))
Expand All @@ -52,7 +59,7 @@ def post_on_slack_channel(slack_token, slack_channel_id, message_text):
if response_data["ok"]:
logging.info("Message sent successfully!")
else:
logging.fatal("Error sending message: %s", response_data['error'])
logging.fatal("Error sending message: %s", response_data["error"])


def main():
Expand All @@ -61,12 +68,13 @@ def main():
logging.basicConfig(level=logging.INFO)
ensure_environment_variables_are_present()

msg = get_gh_release_info_text_with_token(os.environ['GITHUB_RELEASE_TAG'],
os.environ['GITHUB_TOKEN'])
msg = get_gh_release_info_text_with_token(
os.environ["GITHUB_RELEASE_TAG"], os.environ["GITHUB_TOKEN"]
)

post_on_slack_channel(os.environ['SLACK_BOT_TOKEN'],
os.environ['SLACK_CHANNEL_ID_RELEASES'],
msg)
post_on_slack_channel(
os.environ["SLACK_BOT_TOKEN"], os.environ["SLACK_CHANNEL_ID_RELEASES"], msg
)


if __name__ == "__main__":
Expand Down
196 changes: 129 additions & 67 deletions bin/aws-lambda/build_and_publish_lambda_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@
import distutils.spawn
from subprocess import call, check_call, check_output, CalledProcessError, DEVNULL

for profile in ('china', 'non-china'):
for profile in ("china", "non-china"):
try:
check_call(['aws', 'configure', 'list', '--profile', profile], stdout=DEVNULL)
check_call(["aws", "configure", "list", "--profile", profile], stdout=DEVNULL)
except CalledProcessError:
raise ValueError(
f"Please ensure, that your aws configuration includes a profile called '{profile}'"
"and has the 'access_key' and 'secret_key' configured for the respective regions")
"and has the 'access_key' and 'secret_key' configured for the respective regions"
)

# Either -dev or -prod must be specified (and nothing else)
if len(sys.argv) != 2 or (('-dev' not in sys.argv) and ('-prod' not in sys.argv)):
raise ValueError('Please specify -dev or -prod to indicate which type of layer to build.')
if len(sys.argv) != 2 or (("-dev" not in sys.argv) and ("-prod" not in sys.argv)):
raise ValueError(
"Please specify -dev or -prod to indicate which type of layer to build."
)

dev_mode = '-dev' in sys.argv
dev_mode = "-dev" in sys.argv

# Disable aws CLI pagination
os.environ["AWS_PAGER"] = ""
Expand All @@ -48,7 +51,7 @@
if "PYTHONPATH" not in os.environ:
local_env["PYTHONPATH"] = os.getcwd()

build_directory = os.getcwd() + '/build/lambda/python'
build_directory = os.getcwd() + "/build/lambda/python"

if os.path.isdir(build_directory):
print("===> Cleaning build pre-existing directory: %s" % build_directory)
Expand All @@ -58,7 +61,18 @@
os.makedirs(build_directory, exist_ok=True)

print("===> Installing Instana and dependencies into build directory")
call(["pip", "install", "-q", "-U", "-t", os.getcwd() + '/build/lambda/python', "instana"], env=local_env)
call(
[
"pip",
"install",
"-q",
"-U",
"-t",
os.getcwd() + "/build/lambda/python",
"instana",
],
env=local_env,
)

print("===> Manually copying in local dev code")
shutil.rmtree(build_directory + "/instana")
Expand All @@ -69,86 +83,134 @@
zip_filename = "instana-py-layer-%s.zip" % timestamp

os.chdir(os.getcwd() + "/build/lambda/")
call(["zip", "-q", "-r", zip_filename, "./python", "-x", "*.pyc", "./python/pip*", "./python/setuptools*", "./python/wheel*"])

fq_zip_filename = os.getcwd() + '/%s' % zip_filename
call(
[
"zip",
"-q",
"-r",
zip_filename,
"./python",
"-x",
"*.pyc",
"./python/pip*",
"./python/setuptools*",
"./python/wheel*",
]
)

fq_zip_filename = os.getcwd() + "/%s" % zip_filename
aws_zip_filename = "fileb://%s" % fq_zip_filename
print("Zipfile should be at: ", fq_zip_filename)

cn_regions = [
'cn-north-1',
'cn-northwest-1',
]
"cn-north-1",
"cn-northwest-1",
]

if dev_mode:
target_regions = ['us-west-1']
target_regions = ["us-west-1"]
LAYER_NAME = "instana-py-dev"
else:

target_regions = [
'af-south-1',
'ap-east-1',
'ap-northeast-1',
'ap-northeast-2',
'ap-northeast-3',
'ap-south-1',
'ap-south-2',
'ap-southeast-1',
'ap-southeast-2',
'ap-southeast-3',
'ap-southeast-4',
'ca-central-1',
'ca-west-1',
'cn-north-1',
'cn-northwest-1',
'eu-central-1',
'eu-central-2',
'eu-north-1',
'eu-south-1',
'eu-south-2',
'eu-west-1',
'eu-west-2',
'eu-west-3',
'il-central-1',
'me-central-1',
'me-south-1',
'sa-east-1',
'us-east-1',
'us-east-2',
'us-west-1',
'us-west-2'
]
"af-south-1",
"ap-east-1",
"ap-northeast-1",
"ap-northeast-2",
"ap-northeast-3",
"ap-south-1",
"ap-south-2",
"ap-southeast-1",
"ap-southeast-2",
"ap-southeast-3",
"ap-southeast-4",
"ca-central-1",
"ca-west-1",
"cn-north-1",
"cn-northwest-1",
"eu-central-1",
"eu-central-2",
"eu-north-1",
"eu-south-1",
"eu-south-2",
"eu-west-1",
"eu-west-2",
"eu-west-3",
"il-central-1",
"me-central-1",
"me-south-1",
"sa-east-1",
"us-east-1",
"us-east-2",
"us-west-1",
"us-west-2",
]
LAYER_NAME = "instana-python"

published = dict()

for region in target_regions:
print("===> Uploading layer to AWS %s " % region)
profile = 'china' if region in cn_regions else 'non-china'

response = check_output(["aws", "--region", region, "lambda", "publish-layer-version",
"--description",
"Provides Instana tracing and monitoring of AWS Lambda functions built with Python",
"--license-info", "MIT", "--output", "json",
"--layer-name", LAYER_NAME, "--zip-file", aws_zip_filename,
"--compatible-runtimes", "python3.8", "python3.9", "python3.10", "python3.11", "python3.12",
"--profile", profile])
profile = "china" if region in cn_regions else "non-china"

response = check_output(
[
"aws",
"--region",
region,
"lambda",
"publish-layer-version",
"--description",
"Provides Instana tracing and monitoring of AWS Lambda functions built with Python",
"--license-info",
"MIT",
"--output",
"json",
"--layer-name",
LAYER_NAME,
"--zip-file",
aws_zip_filename,
"--compatible-runtimes",
"python3.8",
"python3.9",
"python3.10",
"python3.11",
"python3.12",
"--profile",
profile,
]
)

json_data = json.loads(response)
version = json_data['Version']
version = json_data["Version"]
print("===> Uploaded version is %s" % version)

if dev_mode is False:
print("===> Making layer public...")
response = check_output(["aws", "--region", region, "lambda", "add-layer-version-permission",
"--layer-name", LAYER_NAME, "--version-number", str(version),
"--statement-id", "public-permission-all-accounts",
"--principal", "*",
"--action", "lambda:GetLayerVersion",
"--output", "text",
"--profile", profile])

published[region] = json_data['LayerVersionArn']
response = check_output(
[
"aws",
"--region",
region,
"lambda",
"add-layer-version-permission",
"--layer-name",
LAYER_NAME,
"--version-number",
str(version),
"--statement-id",
"public-permission-all-accounts",
"--principal",
"*",
"--action",
"lambda:GetLayerVersion",
"--output",
"text",
"--profile",
profile,
]
)

published[region] = json_data["LayerVersionArn"]


print("===> Published list:")
Expand Down
Loading

0 comments on commit 9fe367b

Please sign in to comment.