|
| 1 | +import os |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +from gitpod import Gitpod |
| 5 | +from gitpod.types.environment_create_params import SpecContentInitializerSpecContextURL |
| 6 | +from gitpod.types.environments.class_list_response import ClassListResponse |
| 7 | +from gitpod.types.user_get_authenticated_user_response import User |
| 8 | + |
| 9 | + |
| 10 | +def get_environment_class_id(client: Gitpod, user: User) -> Optional[str]: |
| 11 | + """ |
| 12 | + Get the most used environment class ID for the given user. |
| 13 | +
|
| 14 | + Args: |
| 15 | + client (Gitpod): The Gitpod client instance. |
| 16 | + user (User): The authenticated user. |
| 17 | +
|
| 18 | + Returns: |
| 19 | + Optional[str]: The environment class ID or None if not found. |
| 20 | + """ |
| 21 | + environment_class_id = os.environ.get("ENVIRONMENT_CLASS_ID") |
| 22 | + if environment_class_id: |
| 23 | + return environment_class_id |
| 24 | + |
| 25 | + class_usage = {} |
| 26 | + page = client.environments.list(organization_id=user.organization_id) |
| 27 | + while page: |
| 28 | + for env in page.environments: |
| 29 | + env_class = env.spec.machine.class_ |
| 30 | + if env_class not in class_usage: |
| 31 | + class_usage[env_class] = 0 |
| 32 | + class_usage[env_class] += 1 |
| 33 | + if page.pagination and page.pagination.next_token: |
| 34 | + page = client.environments.list(token=page.pagination.next_token, organization_id=user.organization_id) |
| 35 | + else: |
| 36 | + break |
| 37 | + |
| 38 | + sorted_classes = sorted(class_usage.items(), key=lambda item: -item[1]) |
| 39 | + return sorted_classes[0][0] if sorted_classes else None |
| 40 | + |
| 41 | + |
| 42 | +def get_environment_class(client: Gitpod, environment_class_id: str) -> Optional[ClassListResponse]: |
| 43 | + """ |
| 44 | + Get the environment class details for the given environment class ID. |
| 45 | +
|
| 46 | + Args: |
| 47 | + client (Gitpod): The Gitpod client instance. |
| 48 | + environment_class_id (str): The environment class ID. |
| 49 | +
|
| 50 | + Returns: |
| 51 | + Optional[ClassListResponse]: The environment class details or None if not found. |
| 52 | + """ |
| 53 | + page = client.environments.classes.list(filter={"enabled": True}) |
| 54 | + while page: |
| 55 | + for cls in page.environment_classes: |
| 56 | + if cls.id == environment_class_id: |
| 57 | + return cls |
| 58 | + if page.pagination and page.pagination.next_token: |
| 59 | + page = client.environments.classes.list(token=page.pagination.next_token) |
| 60 | + else: |
| 61 | + break |
| 62 | + return None |
| 63 | + |
| 64 | + |
| 65 | +def create_environment(client: Gitpod, context_url: str, environment_class: ClassListResponse) -> str: |
| 66 | + """ |
| 67 | + Create a new environment with the given context URL and environment class. |
| 68 | +
|
| 69 | + Args: |
| 70 | + client (Gitpod): The Gitpod client instance. |
| 71 | + context_url (str): The context URL. |
| 72 | + environment_class (ClassListResponse): The environment class details. |
| 73 | +
|
| 74 | + Returns: |
| 75 | + str: The created environment ID. |
| 76 | + """ |
| 77 | + resp = client.environments.create( |
| 78 | + spec={ |
| 79 | + "desired_phase": "ENVIRONMENT_PHASE_RUNNING", |
| 80 | + "content": { |
| 81 | + "initializer": {"specs": [SpecContentInitializerSpecContextURL(context_url={"url": context_url})]} |
| 82 | + }, |
| 83 | + "machine": {"class": environment_class.id}, |
| 84 | + } |
| 85 | + ) |
| 86 | + return resp.environment.id |
| 87 | + |
| 88 | + |
| 89 | +def ensure_environment_healthy(client: Gitpod, environment_id: str) -> None: |
| 90 | + """ |
| 91 | + Ensure the environment is running or will be running and raise an error if it is in an unexpected phase. |
| 92 | +
|
| 93 | + Args: |
| 94 | + client (Gitpod): The Gitpod client instance. |
| 95 | + environment_id (str): The environment ID. |
| 96 | +
|
| 97 | + Raises: |
| 98 | + RuntimeError: If the environment is in an unexpected phase or has a failure message. |
| 99 | + """ |
| 100 | + environment = client.environments.retrieve(environment_id=environment_id).environment |
| 101 | + print(f"Environment status: {environment.status.phase}") |
| 102 | + if environment.status.phase in [ |
| 103 | + "ENVIRONMENT_PHASE_STOPPING", |
| 104 | + "ENVIRONMENT_PHASE_STOPPED", |
| 105 | + "ENVIRONMENT_PHASE_DELETING", |
| 106 | + "ENVIRONMENT_PHASE_DELETED", |
| 107 | + ]: |
| 108 | + raise RuntimeError(f"Environment {environment_id} is in an unexpected phase: {environment.status.phase}") |
| 109 | + elif environment.status.failure_message and len(environment.status.failure_message) > 0: |
| 110 | + raise RuntimeError(f"Environment {environment_id} failed: {'; '.join(environment.status.failure_message)}") |
0 commit comments