|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import random as rd |
| 4 | +import sys |
| 5 | +from time import sleep |
| 6 | +from typing import Any, Dict |
| 7 | + |
| 8 | +import requests as req |
| 9 | + |
| 10 | +logging.basicConfig(level=logging.INFO) |
| 11 | + |
| 12 | +CLIENT_ID = os.environ["AURA_API_CLIENT_ID"] |
| 13 | +CLIENT_SECRET = os.environ["AURA_API_CLIENT_SECRET"] |
| 14 | + |
| 15 | + |
| 16 | +def get_access_token() -> str: |
| 17 | + data = { |
| 18 | + "grant_type": "client_credentials", |
| 19 | + } |
| 20 | + |
| 21 | + # getting a token like {'access_token':'X','expires_in':3600,'token_type':'bearer'} |
| 22 | + response = req.post("https://api-staging.neo4j.io/oauth/token", data=data, auth=(CLIENT_ID, CLIENT_SECRET)) |
| 23 | + |
| 24 | + response.raise_for_status() |
| 25 | + |
| 26 | + return response.json()["access_token"] # type: ignore |
| 27 | + |
| 28 | + |
| 29 | +def create_instance(access_token: str) -> Dict[str, Any]: |
| 30 | + CREATE_OK_MAX_WAIT_TIME = 10 |
| 31 | + MAX_INT = 2**31 |
| 32 | + |
| 33 | + data = { |
| 34 | + "name": "ci-instance-" + str(rd.randint(0, MAX_INT)), |
| 35 | + "memory": "8GB", |
| 36 | + "version": "5", |
| 37 | + "region": "europe-west1", |
| 38 | + "type": "gds", |
| 39 | + "cloud_provider": "gcp", |
| 40 | + "tenant_id": get_tenant_id(access_token), |
| 41 | + } |
| 42 | + |
| 43 | + should_retry = True |
| 44 | + wait_time = 1 |
| 45 | + |
| 46 | + while should_retry: |
| 47 | + sleep(wait_time) |
| 48 | + wait_time *= 2 |
| 49 | + |
| 50 | + response = req.post( |
| 51 | + "https://api-staging.neo4j.io/v1/instances", |
| 52 | + json=data, |
| 53 | + headers={"Authorization": f"Bearer {access_token}"}, |
| 54 | + ) |
| 55 | + should_retry = response.status_code in [500, 502, 503, 504, 405] and CREATE_OK_MAX_WAIT_TIME > wait_time |
| 56 | + |
| 57 | + if should_retry: |
| 58 | + logging.debug(f"Error code: {response.status_code} - Retrying in {wait_time} s") |
| 59 | + |
| 60 | + response.raise_for_status() |
| 61 | + |
| 62 | + return response.json()["data"] # type: ignore |
| 63 | + |
| 64 | + |
| 65 | +def check_running(access_token: str, db_id: str) -> None: |
| 66 | + RUNNING_MAX_WAIT_TIME = 60 * 5 |
| 67 | + |
| 68 | + should_retry = True |
| 69 | + wait_time = 1 |
| 70 | + |
| 71 | + while should_retry: |
| 72 | + sleep(wait_time) |
| 73 | + wait_time *= 2 |
| 74 | + |
| 75 | + response = req.get( |
| 76 | + f"https://api-staging.neo4j.io/v1/instances/{db_id}", |
| 77 | + headers={"Authorization": f"Bearer {access_token}"}, |
| 78 | + ) |
| 79 | + |
| 80 | + instance_status = "?" |
| 81 | + if response.status_code == 200: |
| 82 | + instance_status = response.json()["data"]["status"] |
| 83 | + |
| 84 | + should_retry = ( |
| 85 | + response.status_code in [500, 502, 503, 504] or instance_status == "creating" |
| 86 | + ) and RUNNING_MAX_WAIT_TIME > wait_time |
| 87 | + |
| 88 | + if should_retry: |
| 89 | + logging.debug(f"Status code: {response.status_code}, Status: {instance_status} - Retrying in {wait_time} s") |
| 90 | + |
| 91 | + response.raise_for_status() |
| 92 | + |
| 93 | + |
| 94 | +def get_tenant_id(access_token: str) -> str: |
| 95 | + response = req.get( |
| 96 | + "https://api-staging.neo4j.io/v1/tenants", |
| 97 | + headers={"Authorization": f"Bearer {access_token}"}, |
| 98 | + ) |
| 99 | + response.raise_for_status() |
| 100 | + |
| 101 | + raw_data = response.json()["data"] |
| 102 | + assert len(raw_data) == 1 |
| 103 | + |
| 104 | + return raw_data[0]["id"] # type: ignore |
| 105 | + |
| 106 | + |
| 107 | +def run_tests(uri: str, username: str, password: str) -> None: |
| 108 | + cmd = ( |
| 109 | + f"NEO4J_URI={uri} NEO4J_USER={username} NEO4J_PASSWORD={password}" |
| 110 | + "tox -e $(tox -l | grep aura | grep main | paste -sd ',' -)" |
| 111 | + ) |
| 112 | + |
| 113 | + if os.system(cmd) != 0: |
| 114 | + raise Exception("Failed to run tests") |
| 115 | + |
| 116 | + |
| 117 | +def run_notebooks(uri: str, username: str, password: str) -> None: |
| 118 | + cmd = f"NEO4J_URI={uri} NEO4J_USER={username} NEO4J_PASSWORD={password} tox -e jupyter-notebook-ci" |
| 119 | + |
| 120 | + if os.system(cmd) != 0: |
| 121 | + raise Exception("Failed to run notebooks") |
| 122 | + |
| 123 | + |
| 124 | +def teardown_instance(access_token: str, db_id: str) -> None: |
| 125 | + TEARDOWN_MAX_WAIT_TIME = 10 |
| 126 | + |
| 127 | + should_retry = True |
| 128 | + wait_time = 1 |
| 129 | + |
| 130 | + while should_retry: |
| 131 | + sleep(wait_time) |
| 132 | + wait_time *= 2 |
| 133 | + |
| 134 | + response = req.delete( |
| 135 | + f"https://api-staging.neo4j.io/v1/instances/{db_id}", |
| 136 | + headers={"Authorization": f"Bearer {access_token}"}, |
| 137 | + ) |
| 138 | + |
| 139 | + if response.status_code == 202: |
| 140 | + should_retry = False |
| 141 | + |
| 142 | + should_retry = (response.status_code in [500, 502, 503, 504]) and TEARDOWN_MAX_WAIT_TIME > wait_time |
| 143 | + |
| 144 | + if should_retry: |
| 145 | + logging.debug(f"Status code: {response.status_code} - Retrying in {wait_time} s") |
| 146 | + |
| 147 | + response.raise_for_status() |
| 148 | + |
| 149 | + |
| 150 | +def main() -> None: |
| 151 | + access_token = get_access_token() |
| 152 | + logging.info("Access token for creation acquired") |
| 153 | + |
| 154 | + create_result = create_instance(access_token) |
| 155 | + instance_id = create_result["id"] |
| 156 | + logging.info("Creation of database accepted") |
| 157 | + |
| 158 | + try: |
| 159 | + check_running(access_token, instance_id) |
| 160 | + logging.info("Database %s up and running", instance_id) |
| 161 | + |
| 162 | + if sys.argv[1] == "tests": |
| 163 | + run_tests( |
| 164 | + create_result["connection_url"], |
| 165 | + create_result["username"], |
| 166 | + create_result["password"], |
| 167 | + ) |
| 168 | + logging.info("Tests ran successfully") |
| 169 | + elif sys.argv[1] == "notebooks": |
| 170 | + run_notebooks( |
| 171 | + create_result["connection_url"], |
| 172 | + create_result["username"], |
| 173 | + create_result["password"], |
| 174 | + ) |
| 175 | + logging.info("Notebooks ran successfully") |
| 176 | + else: |
| 177 | + logging.error(f"Invalid target: {sys.argv[1]}") |
| 178 | + finally: |
| 179 | + access_token = get_access_token() |
| 180 | + logging.info("Access token for teardown acquired") |
| 181 | + |
| 182 | + teardown_instance(access_token, instance_id) |
| 183 | + logging.info("Teardown of instance %s successful", instance_id) |
| 184 | + |
| 185 | + |
| 186 | +if __name__ == "__main__": |
| 187 | + main() |
0 commit comments