|
3 | 3 |
|
4 | 4 | from codesphere import CodesphereSDK |
5 | 5 | from codesphere.resources.workspace import WorkspaceCreate |
6 | | -from codesphere.resources.workspace.landscape import ProfileBuilder, ProfileConfig |
| 6 | +from codesphere.resources.workspace.landscape import ( |
| 7 | + PipelineStage, |
| 8 | + PipelineState, |
| 9 | + ProfileBuilder, |
| 10 | +) |
| 11 | +from codesphere.resources.workspace.logs import LogStage |
7 | 12 |
|
8 | | -TEAM_ID = 123 # Replace with your actual team ID |
| 13 | +TEAM_ID = 123 |
9 | 14 |
|
10 | 15 |
|
11 | | -async def get_plan_id(sdk: CodesphereSDK, plan_name: str = "Micro") -> int: |
12 | | - plans = await sdk.metadata.list_plans() |
13 | | - plan = next((p for p in plans if p.title == plan_name and not p.deprecated), None) |
14 | | - if not plan: |
15 | | - raise ValueError(f"Plan '{plan_name}' not found") |
16 | | - return plan.id |
| 16 | +async def main(): |
| 17 | + async with CodesphereSDK() as sdk: |
| 18 | + plans = await sdk.metadata.list_plans() |
| 19 | + plan = next((p for p in plans if p.title == "Micro" and not p.deprecated), None) |
| 20 | + if not plan: |
| 21 | + raise ValueError("Micro plan not found") |
| 22 | + |
| 23 | + workspace_name = f"pipeline-demo-{int(time.time())}" |
| 24 | + |
| 25 | + print(f"Creating workspace '{workspace_name}'...") |
| 26 | + workspace = await sdk.workspaces.create( |
| 27 | + WorkspaceCreate(plan_id=plan.id, team_id=TEAM_ID, name=workspace_name) |
| 28 | + ) |
| 29 | + print(f"✓ Workspace created (ID: {workspace.id})") |
| 30 | + |
| 31 | + print("Waiting for workspace to start...") |
| 32 | + await workspace.wait_until_running(timeout=300.0, poll_interval=5.0) |
| 33 | + print("✓ Workspace is running\n") |
17 | 34 |
|
| 35 | + profile = ( |
| 36 | + ProfileBuilder() |
| 37 | + .prepare() |
| 38 | + .add_step("echo 'Installing dependencies...' && sleep 2") |
| 39 | + .add_step("echo 'Setup complete!' && sleep 1") |
| 40 | + .done() |
| 41 | + .add_reactive_service("web") |
| 42 | + .plan(plan.id) |
| 43 | + .add_step( |
| 44 | + 'for i in $(seq 1 20); do echo "[$i] Processing request..."; sleep 1; done' |
| 45 | + ) |
| 46 | + .add_port(3000, public=True) |
| 47 | + .add_path("/", port=3000) |
| 48 | + .replicas(1) |
| 49 | + .done() |
| 50 | + .build() |
| 51 | + ) |
18 | 52 |
|
19 | | -def build_web_profile(plan_id: int) -> ProfileConfig: |
20 | | - """Build a simple web service landscape profile.""" |
21 | | - return ( |
22 | | - ProfileBuilder() |
23 | | - .prepare() |
24 | | - .add_step("npm install", name="Install dependencies") |
25 | | - .done() |
26 | | - .add_reactive_service("web") |
27 | | - .plan(plan_id) |
28 | | - .add_step("npm start") |
29 | | - .add_port(3000, public=True) |
30 | | - .add_path("/", port=3000) |
31 | | - .replicas(1) |
32 | | - .env("NODE_ENV", "production") |
33 | | - .build() |
34 | | - ) |
| 53 | + print("Deploying landscape profile...") |
| 54 | + await workspace.landscape.save_profile("production", profile) |
| 55 | + await workspace.landscape.deploy(profile="production") |
| 56 | + print("✓ Profile deployed\n") |
35 | 57 |
|
| 58 | + print("--- Prepare Stage ---") |
| 59 | + await workspace.landscape.start_stage( |
| 60 | + PipelineStage.PREPARE, profile="production" |
| 61 | + ) |
| 62 | + prepare_status = await workspace.landscape.wait_for_stage( |
| 63 | + PipelineStage.PREPARE, timeout=60.0 |
| 64 | + ) |
36 | 65 |
|
37 | | -async def create_workspace(sdk: CodesphereSDK, plan_id: int, name: str): |
38 | | - workspace = await sdk.workspaces.create( |
39 | | - WorkspaceCreate(plan_id=plan_id, team_id=TEAM_ID, name=name) |
40 | | - ) |
41 | | - await workspace.wait_until_running(timeout=300.0, poll_interval=5.0) |
42 | | - return workspace |
| 66 | + for status in prepare_status: |
| 67 | + icon = "✓" if status.state == PipelineState.SUCCESS else "✗" |
| 68 | + print(f"{icon} {status.server}: {status.state.value}") |
43 | 69 |
|
| 70 | + print("\nPrepare logs:") |
| 71 | + for step in range(len(prepare_status[0].steps)): |
| 72 | + logs = await workspace.logs.collect( |
| 73 | + stage=LogStage.PREPARE, step=step, timeout=5.0 |
| 74 | + ) |
| 75 | + for entry in logs: |
| 76 | + if entry.get_text(): |
| 77 | + print(f" {entry.get_text().strip()}") |
44 | 78 |
|
45 | | -async def deploy_landscape(workspace, profile: dict, profile_name: str = "production"): |
46 | | - await workspace.landscape.save_profile(profile_name, profile) |
47 | | - await workspace.landscape.deploy(profile=profile_name) |
48 | | - print("Deployment started!") |
| 79 | + print("\n--- Run Stage ---") |
| 80 | + await workspace.landscape.start_stage(PipelineStage.RUN, profile="production") |
| 81 | + print("Started run stage\n") |
49 | 82 |
|
| 83 | + print("Streaming logs from 'web' service (using context manager):") |
| 84 | + count = 0 |
| 85 | + async with workspace.logs.open_server_stream(step=0, server="web") as stream: |
| 86 | + async for entry in stream: |
| 87 | + if entry.get_text(): |
| 88 | + print(f" {entry.get_text().strip()}") |
| 89 | + count += 1 |
50 | 90 |
|
51 | | -async def main(): |
52 | | - async with CodesphereSDK() as sdk: |
53 | | - plan_id = await get_plan_id(sdk) |
54 | | - workspace = await create_workspace( |
55 | | - sdk, plan_id, f"landscape-demo-{int(time.time())}" |
56 | | - ) |
57 | | - profile = build_web_profile(plan_id) |
58 | | - await deploy_landscape(workspace, profile) |
| 91 | + print(f"\n✓ Stream ended ({count} log entries)") |
59 | 92 |
|
60 | 93 |
|
61 | 94 | if __name__ == "__main__": |
|
0 commit comments