Move the stuff related to runners to lambda_shared

This commit is contained in:
Mikhail f. Shiryaev 2023-05-23 12:55:23 +02:00
parent 7f08f218d9
commit 0fa6a84161
No known key found for this signature in database
GPG Key ID: 4B02ED204C7D93F4
7 changed files with 76 additions and 120 deletions

View File

@ -10,7 +10,6 @@ import argparse
import sys import sys
import json import json
import time import time
from collections import namedtuple
from datetime import datetime from datetime import datetime
from typing import Dict, List, Tuple from typing import Dict, List, Tuple
@ -19,21 +18,14 @@ import requests # type: ignore
import boto3 # type: ignore import boto3 # type: ignore
from botocore.exceptions import ClientError # type: ignore from botocore.exceptions import ClientError # type: ignore
UNIVERSAL_LABEL = "universal" from lambda_shared import (
RUNNER_TYPE_LABELS = [ RUNNER_TYPE_LABELS,
"builder", RunnerDescription,
"func-tester", RunnerDescriptions,
"func-tester-aarch64", list_runners,
"fuzzer-unit-tester",
"stress-tester",
"style-checker",
"style-checker-aarch64",
]
RunnerDescription = namedtuple(
"RunnerDescription", ["id", "name", "tags", "offline", "busy"]
) )
RunnerDescriptions = List[RunnerDescription]
UNIVERSAL_LABEL = "universal"
def get_dead_runners_in_ec2(runners: RunnerDescriptions) -> RunnerDescriptions: def get_dead_runners_in_ec2(runners: RunnerDescriptions) -> RunnerDescriptions:
@ -193,52 +185,6 @@ def get_access_token(jwt_token: str, installation_id: int) -> str:
return data["token"] # type: ignore return data["token"] # type: ignore
def list_runners(access_token: str) -> RunnerDescriptions:
headers = {
"Authorization": f"token {access_token}",
"Accept": "application/vnd.github.v3+json",
}
per_page = 100
response = requests.get(
f"https://api.github.com/orgs/ClickHouse/actions/runners?per_page={per_page}",
headers=headers,
)
response.raise_for_status()
data = response.json()
total_runners = data["total_count"]
print("Expected total runners", total_runners)
runners = data["runners"]
# round to 0 for 0, 1 for 1..100, but to 2 for 101..200
total_pages = (total_runners - 1) // per_page + 1
print("Total pages", total_pages)
for i in range(2, total_pages + 1):
response = requests.get(
"https://api.github.com/orgs/ClickHouse/actions/runners"
f"?page={i}&per_page={per_page}",
headers=headers,
)
response.raise_for_status()
data = response.json()
runners += data["runners"]
print("Total runners", len(runners))
result = []
for runner in runners:
tags = [tag["name"] for tag in runner["labels"]]
desc = RunnerDescription(
id=runner["id"],
name=runner["name"],
tags=tags,
offline=runner["status"] == "offline",
busy=runner["busy"],
)
result.append(desc)
return result
def group_runners_by_tag( def group_runners_by_tag(
listed_runners: RunnerDescriptions, listed_runners: RunnerDescriptions,
) -> Dict[str, RunnerDescriptions]: ) -> Dict[str, RunnerDescriptions]:

View File

@ -0,0 +1 @@
../lambda_shared_package/lambda_shared

View File

@ -1,3 +1,3 @@
requests<2.30 ../lambda_shared_package
PyJWT PyJWT
cryptography<38 cryptography<38

View File

@ -4,7 +4,8 @@ It exists as __init__.py and lambda_shared/__init__.py to work both in local and
import json import json
import logging import logging
import time import time
from typing import List, Optional from collections import namedtuple
from typing import Any, List, Optional
import boto3 # type: ignore import boto3 # type: ignore
import requests # type: ignore import requests # type: ignore
@ -21,10 +22,14 @@ RUNNER_TYPE_LABELS = [
### VENDORING ### VENDORING
def get_parameter_from_ssm(name, decrypt=True, client=None): def get_parameter_from_ssm(
name: str, decrypt: bool = True, client: Optional[Any] = None
) -> str:
if not client: if not client:
client = boto3.client("ssm", region_name="us-east-1") client = boto3.client("ssm", region_name="us-east-1")
return client.get_parameter(Name=name, WithDecryption=decrypt)["Parameter"]["Value"] return client.get_parameter(Name=name, WithDecryption=decrypt)[ # type: ignore
"Parameter"
]["Value"]
class CHException(Exception): class CHException(Exception):
@ -65,10 +70,64 @@ class ClickHouseHelper:
raise CHException("Cannot fetch data from clickhouse") raise CHException("Cannot fetch data from clickhouse")
def select_json_each_row(self, db: str, query: str) -> List[dict]: # type: ignore def select_json_each_row(self, db: str, query: str) -> List[dict]:
text = self._select_and_get_json_each_row(db, query) text = self._select_and_get_json_each_row(db, query)
result = [] result = []
for line in text.split("\n"): for line in text.split("\n"):
if line: if line:
result.append(json.loads(line)) result.append(json.loads(line))
return result return result
### Runners
RunnerDescription = namedtuple(
"RunnerDescription", ["id", "name", "tags", "offline", "busy"]
)
RunnerDescriptions = List[RunnerDescription]
def list_runners(access_token: str) -> RunnerDescriptions:
headers = {
"Authorization": f"token {access_token}",
"Accept": "application/vnd.github.v3+json",
}
per_page = 100
response = requests.get(
f"https://api.github.com/orgs/ClickHouse/actions/runners?per_page={per_page}",
headers=headers,
)
response.raise_for_status()
data = response.json()
total_runners = data["total_count"]
print("Expected total runners", total_runners)
runners = data["runners"]
# round to 0 for 0, 1 for 1..100, but to 2 for 101..200
total_pages = (total_runners - 1) // per_page + 1
print("Total pages", total_pages)
for i in range(2, total_pages + 1):
response = requests.get(
"https://api.github.com/orgs/ClickHouse/actions/runners"
f"?page={i}&per_page={per_page}",
headers=headers,
)
response.raise_for_status()
data = response.json()
runners += data["runners"]
print("Total runners", len(runners))
result = []
for runner in runners:
tags = [tag["name"] for tag in runner["labels"]]
desc = RunnerDescription(
id=runner["id"],
name=runner["name"],
tags=tags,
offline=runner["status"] == "offline",
busy=runner["busy"],
)
result.append(desc)
return result

View File

@ -4,7 +4,6 @@ import argparse
import json import json
import sys import sys
import time import time
from collections import namedtuple
from dataclasses import dataclass from dataclasses import dataclass
from typing import Any, Dict, List, Tuple from typing import Any, Dict, List, Tuple
@ -12,6 +11,8 @@ import boto3 # type: ignore
import requests # type: ignore import requests # type: ignore
import jwt import jwt
from lambda_shared import RunnerDescriptions, list_runners
def get_key_and_app_from_aws() -> Tuple[str, int]: def get_key_and_app_from_aws() -> Tuple[str, int]:
secret_name = "clickhouse_github_secret_key" secret_name = "clickhouse_github_secret_key"
@ -118,58 +119,6 @@ def get_cached_instances() -> dict:
return cached_instances.value return cached_instances.value
RunnerDescription = namedtuple(
"RunnerDescription", ["id", "name", "tags", "offline", "busy"]
)
RunnerDescriptions = List[RunnerDescription]
def list_runners(access_token: str) -> RunnerDescriptions:
headers = {
"Authorization": f"token {access_token}",
"Accept": "application/vnd.github.v3+json",
}
per_page = 100
response = requests.get(
f"https://api.github.com/orgs/ClickHouse/actions/runners?per_page={per_page}",
headers=headers,
)
response.raise_for_status()
data = response.json()
total_runners = data["total_count"]
print("Expected total runners", total_runners)
runners = data["runners"]
# round to 0 for 0, 1 for 1..100, but to 2 for 101..200
total_pages = (total_runners - 1) // per_page + 1
print("Total pages", total_pages)
for i in range(2, total_pages + 1):
response = requests.get(
"https://api.github.com/orgs/ClickHouse/actions/runners"
f"?page={i}&per_page={per_page}",
headers=headers,
)
response.raise_for_status()
data = response.json()
runners += data["runners"]
print("Total runners", len(runners))
result = []
for runner in runners:
tags = [tag["name"] for tag in runner["labels"]]
desc = RunnerDescription(
id=runner["id"],
name=runner["name"],
tags=tags,
offline=runner["status"] == "offline",
busy=runner["busy"],
)
result.append(desc)
return result
def how_many_instances_to_kill(event_data: dict) -> Dict[str, int]: def how_many_instances_to_kill(event_data: dict) -> Dict[str, int]:
data_array = event_data["CapacityToTerminate"] data_array = event_data["CapacityToTerminate"]
to_kill_by_zone = {} # type: Dict[str, int] to_kill_by_zone = {} # type: Dict[str, int]

View File

@ -0,0 +1 @@
../lambda_shared_package/lambda_shared

View File

@ -1,3 +1,3 @@
requests<2.30 ../lambda_shared_package
PyJWT PyJWT
cryptography<38 cryptography<38