|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from gitpod import Gitpod |
| 4 | +from utils import get_context_url, get_authenticated_user, get_environment_class_id, get_environment_class, create_environment |
| 5 | +import time |
| 6 | +import requests |
| 7 | + |
| 8 | +def check_environment_status(client, environment_id): |
| 9 | + environment = client.environments.retrieve(environment_id=environment_id).environment |
| 10 | + print(f"Environment status: {environment.status.phase}") |
| 11 | + if environment.status.phase in ["ENVIRONMENT_PHASE_STOPPING", "ENVIRONMENT_PHASE_STOPPED", "ENVIRONMENT_PHASE_DELETING", "ENVIRONMENT_PHASE_DELETED"]: |
| 12 | + raise RuntimeError(f"Environment {environment_id} is in an unexpected phase: {environment.status.phase}") |
| 13 | + elif environment.status.failure_message and len(environment.status.failure_message) > 0: |
| 14 | + raise RuntimeError(f"Environment {environment_id} failed: {'; '.join(environment.status.failure_message)}") |
| 15 | + |
| 16 | +def main() -> None: |
| 17 | + client: Gitpod = Gitpod() |
| 18 | + context_url: str = get_context_url() |
| 19 | + user = get_authenticated_user(client) |
| 20 | + environment_class_id: str = get_environment_class_id(client, user) |
| 21 | + |
| 22 | + if not environment_class_id: |
| 23 | + raise ValueError("no environment class found") |
| 24 | + |
| 25 | + environment_class = get_environment_class(client, environment_class_id) |
| 26 | + |
| 27 | + if not environment_class: |
| 28 | + raise ValueError("no environment class found") |
| 29 | + |
| 30 | + print(f"Using repository: {context_url}") |
| 31 | + print(f"Using environment class: {environment_class.display_name} (ID: {environment_class.id})") |
| 32 | + |
| 33 | + environment_id: str = create_environment(client, context_url, environment_class) |
| 34 | + print(f"Created environment: {environment_id}") |
| 35 | + |
| 36 | + try: |
| 37 | + reference = "hello-world" |
| 38 | + task = client.environments.automations.tasks.create( |
| 39 | + spec={ |
| 40 | + "command": "echo 'Hello, World!'" |
| 41 | + }, |
| 42 | + environment_id=environment_id, |
| 43 | + metadata={ |
| 44 | + "name": "Hello, World!", |
| 45 | + "description": "Prints 'Hello, World!' to the console.", |
| 46 | + "reference": reference, |
| 47 | + } |
| 48 | + ).task |
| 49 | + print(f"Created task: {task.id}") |
| 50 | + |
| 51 | + execution = client.environments.automations.tasks.start( |
| 52 | + id=task.id |
| 53 | + ).task_execution |
| 54 | + print(f"Started task execution: {execution.id}") |
| 55 | + |
| 56 | + logs_access_token = client.environments.create_logs_token(environment_id=environment_id).access_token |
| 57 | + print(f"Waiting for task logs to be available...") |
| 58 | + |
| 59 | + event_stream = client.events.watch( |
| 60 | + environment_id=environment_id |
| 61 | + ) |
| 62 | + |
| 63 | + log_url = None |
| 64 | + for event in event_stream: |
| 65 | + print(f"Received event: {event}") |
| 66 | + if event.resource_type == "RESOURCE_TYPE_ENVIRONMENT" and event.resource_id == environment_id: |
| 67 | + check_environment_status(client, environment_id) |
| 68 | + elif event.resource_type == "RESOURCE_TYPE_TASK_EXECUTION" and event.resource_id == execution.id: |
| 69 | + execution = client.environments.automations.tasks.executions.retrieve(id=execution.id).task_execution |
| 70 | + print(f"Task execution status: {execution.status.phase}") |
| 71 | + if execution.status.log_url: |
| 72 | + log_url = execution.status.log_url |
| 73 | + break |
| 74 | + |
| 75 | + if not log_url: |
| 76 | + raise RuntimeError("Task logs are not available.") |
| 77 | + |
| 78 | + print(f"Task logs are available at: {log_url}") |
| 79 | + |
| 80 | + # Stream the log URL and print the logs |
| 81 | + response = requests.get(log_url, stream=True, headers={"Authorization": f"Bearer {logs_access_token}"}) |
| 82 | + for line in response.iter_lines(): |
| 83 | + if line: |
| 84 | + print(line.decode('utf-8')) |
| 85 | + |
| 86 | + finally: |
| 87 | + client.environments.delete(environment_id=environment_id) |
| 88 | + print(f"Deleted environment: {environment_id}") |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + main() |
0 commit comments