Skip to content

Commit 22854bb

Browse files
committed
refactor: update environment and automation library methods
1 parent f9fdda2 commit 22854bb

File tree

4 files changed

+51
-28
lines changed

4 files changed

+51
-28
lines changed

examples/run_command.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import asyncio
55
from gitpod import Gitpod
66
import gitpod.lib as util
7-
from gitpod.types.environment_create_params import SpecContentInitializerSpecContextURL
87

98
# Example: ./examples/run_command.py https://github.com/gitpod-io/empty 'echo "Hello World!"'
109
async def main() -> None:
@@ -25,7 +24,11 @@ async def main() -> None:
2524
spec={
2625
"desired_phase": "ENVIRONMENT_PHASE_RUNNING",
2726
"content": {
28-
"initializer": {"specs": [SpecContentInitializerSpecContextURL(context_url={"url": context_url})]}
27+
"initializer": {"specs": [{
28+
"contextUrl": {
29+
"url": context_url
30+
}
31+
}]}
2932
},
3033
"machine": {"class": env_class.id},
3134
}

examples/run_service.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import asyncio
55
from gitpod import Gitpod
66
import gitpod.lib as util
7-
from gitpod.types.environment_create_params import SpecContentInitializerSpecContextURL
87

98
# Example: ./examples/run_service.py https://github.com/gitpod-io/empty
109
async def main() -> None:
@@ -26,7 +25,11 @@ async def main() -> None:
2625
spec={
2726
"desired_phase": "ENVIRONMENT_PHASE_RUNNING",
2827
"content": {
29-
"initializer": {"specs": [SpecContentInitializerSpecContextURL(context_url={"url": context_url})]}
28+
"initializer": {"specs": [{
29+
"contextUrl": {
30+
"url": context_url
31+
}
32+
}]}
3033
},
3134
"machine": {"class": env_class.id},
3235
"ports": [

src/gitpod/lib/automation.py

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import httpx
23
from typing import AsyncIterator
34

@@ -8,8 +9,8 @@
89
def run_service(
910
client: Gitpod,
1011
environment_id: str,
11-
metadata: service_create_params.Metadata,
12-
spec: service_create_params.Spec
12+
metadata: service_create_params.ServiceMetadataParam,
13+
spec: service_create_params.ServiceSpecParam
1314
) -> AsyncIterator[str]:
1415
reference = metadata["reference"]
1516
if not reference:
@@ -36,7 +37,7 @@ def run_service(
3637
log_url = wait_for_service_log_url(client, environment_id, service_id)
3738
return stream_logs(client, environment_id, log_url)
3839

39-
async def run_command(client: Gitpod, environment_id: str, command: str) -> AsyncIterator[str]:
40+
def run_command(client: Gitpod, environment_id: str, command: str) -> AsyncIterator[str]:
4041
tasks = client.environments.automations.tasks.list(
4142
filter={
4243
"references": [TASK_REFERENCE],
@@ -70,20 +71,6 @@ async def run_command(client: Gitpod, environment_id: str, command: str) -> Asyn
7071
log_url = wait_for_task_log_url(client, environment_id, task_execution_id)
7172
return stream_logs(client, environment_id, log_url)
7273

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-
8774
def wait_for_task_log_url(client: Gitpod, environment_id: str, task_execution_id: str) -> str:
8875
def get_log_url():
8976
execution = client.environments.automations.tasks.executions.retrieve(id=task_execution_id).task_execution
@@ -118,3 +105,35 @@ def wait_for_log_url(client: Gitpod, environment_id: str, resource_id: str, get_
118105
return log_url
119106
finally:
120107
event_stream.http_response.close()
108+
109+
async def stream_logs(client: Gitpod, environment_id: str, log_url: str) -> AsyncIterator[str]:
110+
logs_access_token = client.environments.create_logs_token(environment_id=environment_id).access_token
111+
async with httpx.AsyncClient() as http_client:
112+
retries = 3
113+
while retries > 0:
114+
try:
115+
async with http_client.stream("GET", log_url, headers={"Authorization": f"Bearer {logs_access_token}"}, timeout=None) as response:
116+
if response.status_code == 502: # Bad Gateway
117+
retries -= 1
118+
if retries == 0:
119+
raise Exception("Failed to stream logs after 3 retries")
120+
await asyncio.sleep(1) # Wait before retrying
121+
continue
122+
123+
buffer = ""
124+
async for chunk in response.aiter_text():
125+
buffer += chunk
126+
while "\n" in buffer:
127+
line, buffer = buffer.split("\n", 1)
128+
if line:
129+
yield line
130+
if buffer:
131+
yield buffer
132+
break # Success - exit retry loop
133+
134+
except httpx.HTTPError as e:
135+
if retries > 0 and (isinstance(e, httpx.HTTPStatusError) and e.response.status_code == 502):
136+
retries -= 1
137+
await asyncio.sleep(1) # Wait before retrying
138+
continue
139+
raise # Re-raise if not a 502 or out of retries

src/gitpod/lib/environment.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
from typing import Optional
22

33
from gitpod import Gitpod
4-
from gitpod.types.environments.class_list_response import ClassListResponse
4+
from gitpod.types.shared import EnvironmentClass
55

6-
def find_most_used_environment_class(client: Gitpod) -> Optional[ClassListResponse]:
6+
def find_most_used_environment_class(client: Gitpod) -> Optional[EnvironmentClass]:
77
"""
88
Find the most used environment class.
99
"""
10-
user = client.users.get_authenticated_user(body={}).user
11-
1210
class_usage = {}
13-
page = client.environments.list(organization_id=user.organization_id)
11+
page = client.environments.list()
1412
while page:
1513
for env in page.environments:
1614
env_class = env.spec.machine.class_
1715
if env_class not in class_usage:
1816
class_usage[env_class] = 0
1917
class_usage[env_class] += 1
2018
if page.pagination and page.pagination.next_token:
21-
page = client.environments.list(token=page.pagination.next_token, organization_id=user.organization_id)
19+
page = client.environments.list(token=page.pagination.next_token)
2220
else:
2321
break
2422

@@ -27,7 +25,7 @@ def find_most_used_environment_class(client: Gitpod) -> Optional[ClassListRespon
2725
if not environment_class_id:
2826
return None
2927

30-
page = client.environments.classes.list(filter={"enabled": True})
28+
page = client.environments.classes.list(filter={"can_create_environments": True})
3129
while page:
3230
for cls in page.environment_classes:
3331
if cls.id == environment_class_id:

0 commit comments

Comments
 (0)