|
| 1 | +import httpx |
| 2 | +from typing import AsyncIterator |
| 3 | + |
| 4 | +from gitpod import Gitpod |
| 5 | +from gitpod.types.environments.automations import service_create_params |
| 6 | +TASK_REFERENCE = "gitpod-python-sdk" |
| 7 | + |
| 8 | +def run_service( |
| 9 | + client: Gitpod, |
| 10 | + environment_id: str, |
| 11 | + metadata: service_create_params.Metadata, |
| 12 | + spec: service_create_params.Spec |
| 13 | +) -> AsyncIterator[str]: |
| 14 | + reference = metadata["reference"] |
| 15 | + if not reference: |
| 16 | + raise ValueError("metadata.reference is required") |
| 17 | + |
| 18 | + services = client.environments.automations.services.list( |
| 19 | + filter={ |
| 20 | + "references": [reference], |
| 21 | + "environment_ids": [environment_id] |
| 22 | + } |
| 23 | + ).services |
| 24 | + |
| 25 | + service_id = "" |
| 26 | + if not services: |
| 27 | + service_id = client.environments.automations.services.create( |
| 28 | + environment_id=environment_id, |
| 29 | + spec=spec, |
| 30 | + metadata=metadata |
| 31 | + ).service.id |
| 32 | + else: |
| 33 | + service_id = services[0].id |
| 34 | + |
| 35 | + client.environments.automations.services.start(id=service_id) |
| 36 | + log_url = wait_for_service_log_url(client, environment_id, service_id) |
| 37 | + return stream_logs(client, environment_id, log_url) |
| 38 | + |
| 39 | +async def run_command(client: Gitpod, environment_id: str, command: str) -> AsyncIterator[str]: |
| 40 | + tasks = client.environments.automations.tasks.list( |
| 41 | + filter={ |
| 42 | + "references": [TASK_REFERENCE], |
| 43 | + "environment_ids": [environment_id] |
| 44 | + } |
| 45 | + ).tasks |
| 46 | + |
| 47 | + task_id = "" |
| 48 | + if not tasks: |
| 49 | + task_id = client.environments.automations.tasks.create( |
| 50 | + spec={ |
| 51 | + "command": command, |
| 52 | + }, |
| 53 | + environment_id=environment_id, |
| 54 | + metadata={ |
| 55 | + "name": "Gitpod Python SDK Task", |
| 56 | + "description": "Gitpod Python SDK Task", |
| 57 | + "reference": TASK_REFERENCE, |
| 58 | + }, |
| 59 | + ).task.id |
| 60 | + else: |
| 61 | + task_id = tasks[0].id |
| 62 | + client.environments.automations.tasks.update( |
| 63 | + id=task_id, |
| 64 | + spec={ |
| 65 | + "command": command, |
| 66 | + }, |
| 67 | + ) |
| 68 | + |
| 69 | + task_execution_id = client.environments.automations.tasks.start(id=task_id).task_execution.id |
| 70 | + log_url = wait_for_task_log_url(client, environment_id, task_execution_id) |
| 71 | + return stream_logs(client, environment_id, log_url) |
| 72 | + |
| 73 | +async def stream_logs(client: Gitpod, environment_id: str, log_url: str) -> AsyncIterator[str]: |
| 74 | + logs_access_token = client.environments.create_logs_token(environment_id=environment_id).access_token |
| 75 | + async with httpx.AsyncClient() as http_client: |
| 76 | + async with http_client.stream("GET", log_url, headers={"Authorization": f"Bearer {logs_access_token}"}, timeout=None) as response: |
| 77 | + buffer = "" |
| 78 | + async for chunk in response.aiter_text(): |
| 79 | + buffer += chunk |
| 80 | + while "\n" in buffer: |
| 81 | + line, buffer = buffer.split("\n", 1) |
| 82 | + if line: |
| 83 | + yield line |
| 84 | + if buffer: |
| 85 | + yield buffer |
| 86 | + |
| 87 | +def wait_for_task_log_url(client: Gitpod, environment_id: str, task_execution_id: str) -> str: |
| 88 | + def get_log_url(): |
| 89 | + execution = client.environments.automations.tasks.executions.retrieve(id=task_execution_id).task_execution |
| 90 | + return execution.status.log_url |
| 91 | + |
| 92 | + return wait_for_log_url(client, environment_id, task_execution_id, get_log_url, "RESOURCE_TYPE_TASK_EXECUTION") |
| 93 | + |
| 94 | +def wait_for_service_log_url(client: Gitpod, environment_id: str, service_id: str) -> str: |
| 95 | + def get_log_url(): |
| 96 | + service = client.environments.automations.services.retrieve(id=service_id).service |
| 97 | + if service.status.phase != "SERVICE_PHASE_RUNNING": |
| 98 | + return None |
| 99 | + return service.status.log_url |
| 100 | + |
| 101 | + return wait_for_log_url(client, environment_id, service_id, get_log_url, "RESOURCE_TYPE_SERVICE") |
| 102 | + |
| 103 | +def wait_for_log_url(client: Gitpod, environment_id: str, resource_id: str, get_log_url_fn, resource_type: str) -> str: |
| 104 | + log_url = get_log_url_fn() |
| 105 | + if log_url: |
| 106 | + return log_url |
| 107 | + |
| 108 | + event_stream = client.events.watch(environment_id=environment_id, timeout=None) |
| 109 | + try: |
| 110 | + log_url = get_log_url_fn() |
| 111 | + if log_url: |
| 112 | + return log_url |
| 113 | + |
| 114 | + for event in event_stream: |
| 115 | + if event.resource_type == resource_type and event.resource_id == resource_id: |
| 116 | + log_url = get_log_url_fn() |
| 117 | + if log_url is not None: |
| 118 | + return log_url |
| 119 | + finally: |
| 120 | + event_stream.http_response.close() |
0 commit comments