@gravito/launchpad 1.0.0-beta.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # @gravito/launchpad
2
+
3
+ ## 1.0.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies
8
+ - @gravito/core@1.0.0
9
+ - @gravito/enterprise@1.0.0
10
+ - @gravito/ripple@1.0.0
11
+ - @gravito/stasis@1.0.0
package/debug-launch.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { getRuntimeAdapter } from '@gravito/core'
1
2
  import { DockerAdapter } from './src/Infrastructure/Docker/DockerAdapter'
2
3
  import { bootstrapLaunchpad } from './src/index'
3
4
 
@@ -6,23 +7,25 @@ async function run() {
6
7
 
7
8
  // 1. Cleanup
8
9
  const _docker = new DockerAdapter()
10
+ const runtime = getRuntimeAdapter()
9
11
  try {
10
- const containers = await Bun.spawn([
12
+ const listProc = runtime.spawn([
11
13
  'docker',
12
14
  'ps',
13
15
  '-aq',
14
16
  '--filter',
15
17
  'label=gravito-origin=launchpad',
16
- ]).text()
18
+ ])
19
+ const containers = await new Response(listProc.stdout ?? null).text()
17
20
  if (containers.trim()) {
18
21
  console.log('🧹 Cleaning up old containers...')
19
- await Bun.spawn(['docker', 'rm', '-f', ...containers.trim().split('\n')]).exited
22
+ await runtime.spawn(['docker', 'rm', '-f', ...containers.trim().split('\n')]).exited
20
23
  }
21
24
  } catch (_e) {}
22
25
 
23
26
  // 2. Start Server
24
27
  const config = await bootstrapLaunchpad()
25
- const server = Bun.serve(config)
28
+ const server = runtime.serve(config)
26
29
  console.log(`🚀 Server started at http://localhost:${config.port}`)
27
30
 
28
31
  // 3. Launch Mission
@@ -0,0 +1,188 @@
1
+ import * as _gravito_ripple from '@gravito/ripple';
2
+ import { OrbitRipple } from '@gravito/ripple';
3
+ import { GravitoOrbit, PlanetCore } from '@gravito/core';
4
+ import { ValueObject, AggregateRoot } from '@gravito/enterprise';
5
+
6
+ interface MissionProps {
7
+ id: string;
8
+ repoUrl: string;
9
+ branch: string;
10
+ commitSha: string;
11
+ }
12
+ declare class Mission extends ValueObject<MissionProps> {
13
+ get id(): string;
14
+ get repoUrl(): string;
15
+ get branch(): string;
16
+ get commitSha(): string;
17
+ static create(props: MissionProps): Mission;
18
+ }
19
+
20
+ /**
21
+ * 火箭生命週期狀態
22
+ */
23
+ declare enum RocketStatus {
24
+ IDLE = "IDLE",// 待命:容器運行中,無任務
25
+ PREPARING = "PREPARING",// 裝填:正在注入代碼 (Locked)
26
+ ORBITING = "ORBITING",// 運行:應用服務中
27
+ REFURBISHING = "REFURBISHING",// 翻新:正在清理環境
28
+ DECOMMISSIONED = "DECOMMISSIONED"
29
+ }
30
+
31
+ declare class Rocket extends AggregateRoot<string> {
32
+ private _status;
33
+ private _currentMission;
34
+ private _containerId;
35
+ private _assignedDomain;
36
+ constructor(id: string, containerId: string);
37
+ get status(): RocketStatus;
38
+ get currentMission(): Mission | null;
39
+ get containerId(): string;
40
+ get assignedDomain(): string | null;
41
+ /**
42
+ * 分配域名
43
+ */
44
+ assignDomain(domain: string): void;
45
+ /**
46
+ * 分配任務 (指派任務給 IDLE 的火箭)
47
+ */
48
+ assignMission(mission: Mission): void;
49
+ /**
50
+ * 點火啟動 (應用程式啟動成功)
51
+ */
52
+ ignite(): void;
53
+ /**
54
+ * 任務降落 (PR 合併或關閉,暫停服務)
55
+ */
56
+ splashDown(): void;
57
+ /**
58
+ * 翻新完成 (清理完畢,回歸池中)
59
+ */
60
+ finishRefurbishment(): void;
61
+ /**
62
+ * 火箭除役 (移除容器)
63
+ */
64
+ decommission(): void;
65
+ toJSON(): {
66
+ id: string;
67
+ containerId: string;
68
+ status: RocketStatus;
69
+ currentMission: Mission | null;
70
+ assignedDomain: string | null;
71
+ };
72
+ static fromJSON(data: any): Rocket;
73
+ }
74
+
75
+ /**
76
+ * 負責與底層容器引擎(如 Docker)溝通的轉接器介面
77
+ */
78
+ interface IDockerAdapter {
79
+ createBaseContainer(): Promise<string>;
80
+ copyFiles(containerId: string, sourcePath: string, targetPath: string): Promise<void>;
81
+ executeCommand(containerId: string, command: string[]): Promise<{
82
+ stdout: string;
83
+ stderr: string;
84
+ exitCode: number;
85
+ }>;
86
+ /**
87
+ * 獲取容器映射到宿主機的真實端口
88
+ */
89
+ getExposedPort(containerId: string, containerPort?: number): Promise<number>;
90
+ removeContainer(containerId: string): Promise<void>;
91
+ /**
92
+ * 根據 Label 批量移除容器
93
+ */
94
+ removeContainerByLabel(label: string): Promise<void>;
95
+ streamLogs(containerId: string, onData: (data: string) => void): void;
96
+ getStats(containerId: string): Promise<{
97
+ cpu: string;
98
+ memory: string;
99
+ }>;
100
+ }
101
+ /**
102
+ * 負責代碼獲取
103
+ */
104
+ interface IGitAdapter {
105
+ clone(repoUrl: string, branch: string): Promise<string>;
106
+ }
107
+ /**
108
+ * 負責動態反向代理與域名路由的轉接器
109
+ */
110
+ interface IRouterAdapter {
111
+ register(domain: string, targetUrl: string): void;
112
+ unregister(domain: string): void;
113
+ start(port: number): void;
114
+ }
115
+ /**
116
+ * 負責持久化火箭狀態的儲存庫介面
117
+ */
118
+ interface IRocketRepository {
119
+ save(rocket: Rocket): Promise<void>;
120
+ findById(id: string): Promise<Rocket | null>;
121
+ findIdle(): Promise<Rocket | null>;
122
+ findAll(): Promise<Rocket[]>;
123
+ delete(id: string): Promise<void>;
124
+ }
125
+
126
+ declare class PayloadInjector {
127
+ private docker;
128
+ private git;
129
+ constructor(docker: IDockerAdapter, git: IGitAdapter);
130
+ deploy(rocket: Rocket): Promise<void>;
131
+ }
132
+
133
+ declare class RefurbishUnit {
134
+ private docker;
135
+ constructor(docker: IDockerAdapter);
136
+ /**
137
+ * 執行火箭翻新邏輯
138
+ */
139
+ refurbish(rocket: Rocket): Promise<void>;
140
+ }
141
+
142
+ declare class PoolManager {
143
+ private dockerAdapter;
144
+ private rocketRepository;
145
+ private refurbishUnit?;
146
+ private router?;
147
+ constructor(dockerAdapter: IDockerAdapter, rocketRepository: IRocketRepository, refurbishUnit?: RefurbishUnit | undefined, router?: IRouterAdapter | undefined);
148
+ /**
149
+ * 初始化發射場:預先準備指定數量的火箭
150
+ */
151
+ warmup(count: number): Promise<void>;
152
+ /**
153
+ * 獲取一架可用的火箭並分配任務
154
+ */
155
+ assignMission(mission: Mission): Promise<Rocket>;
156
+ /**
157
+ * 回收指定任務的火箭
158
+ */
159
+ recycle(missionId: string): Promise<void>;
160
+ }
161
+
162
+ declare class MissionControl {
163
+ private poolManager;
164
+ private injector;
165
+ private docker;
166
+ private router?;
167
+ constructor(poolManager: PoolManager, injector: PayloadInjector, docker: IDockerAdapter, router?: IRouterAdapter | undefined);
168
+ launch(mission: Mission, onTelemetry: (type: string, data: any) => void): Promise<string>;
169
+ }
170
+
171
+ /**
172
+ * Gravito Launchpad Orbit
173
+ */
174
+ declare class LaunchpadOrbit implements GravitoOrbit {
175
+ private ripple;
176
+ constructor(ripple: OrbitRipple);
177
+ install(core: PlanetCore): Promise<void>;
178
+ }
179
+ /**
180
+ * 一鍵啟動 Launchpad 應用程式
181
+ */
182
+ declare function bootstrapLaunchpad(): Promise<{
183
+ port: number;
184
+ websocket: _gravito_ripple.WebSocketHandlerConfig;
185
+ fetch: (req: Request, server: any) => Response | Promise<Response> | undefined;
186
+ }>;
187
+
188
+ export { LaunchpadOrbit, Mission, MissionControl, PayloadInjector, PoolManager, RefurbishUnit, Rocket, RocketStatus, bootstrapLaunchpad };
@@ -0,0 +1,188 @@
1
+ import * as _gravito_ripple from '@gravito/ripple';
2
+ import { OrbitRipple } from '@gravito/ripple';
3
+ import { GravitoOrbit, PlanetCore } from '@gravito/core';
4
+ import { ValueObject, AggregateRoot } from '@gravito/enterprise';
5
+
6
+ interface MissionProps {
7
+ id: string;
8
+ repoUrl: string;
9
+ branch: string;
10
+ commitSha: string;
11
+ }
12
+ declare class Mission extends ValueObject<MissionProps> {
13
+ get id(): string;
14
+ get repoUrl(): string;
15
+ get branch(): string;
16
+ get commitSha(): string;
17
+ static create(props: MissionProps): Mission;
18
+ }
19
+
20
+ /**
21
+ * 火箭生命週期狀態
22
+ */
23
+ declare enum RocketStatus {
24
+ IDLE = "IDLE",// 待命:容器運行中,無任務
25
+ PREPARING = "PREPARING",// 裝填:正在注入代碼 (Locked)
26
+ ORBITING = "ORBITING",// 運行:應用服務中
27
+ REFURBISHING = "REFURBISHING",// 翻新:正在清理環境
28
+ DECOMMISSIONED = "DECOMMISSIONED"
29
+ }
30
+
31
+ declare class Rocket extends AggregateRoot<string> {
32
+ private _status;
33
+ private _currentMission;
34
+ private _containerId;
35
+ private _assignedDomain;
36
+ constructor(id: string, containerId: string);
37
+ get status(): RocketStatus;
38
+ get currentMission(): Mission | null;
39
+ get containerId(): string;
40
+ get assignedDomain(): string | null;
41
+ /**
42
+ * 分配域名
43
+ */
44
+ assignDomain(domain: string): void;
45
+ /**
46
+ * 分配任務 (指派任務給 IDLE 的火箭)
47
+ */
48
+ assignMission(mission: Mission): void;
49
+ /**
50
+ * 點火啟動 (應用程式啟動成功)
51
+ */
52
+ ignite(): void;
53
+ /**
54
+ * 任務降落 (PR 合併或關閉,暫停服務)
55
+ */
56
+ splashDown(): void;
57
+ /**
58
+ * 翻新完成 (清理完畢,回歸池中)
59
+ */
60
+ finishRefurbishment(): void;
61
+ /**
62
+ * 火箭除役 (移除容器)
63
+ */
64
+ decommission(): void;
65
+ toJSON(): {
66
+ id: string;
67
+ containerId: string;
68
+ status: RocketStatus;
69
+ currentMission: Mission | null;
70
+ assignedDomain: string | null;
71
+ };
72
+ static fromJSON(data: any): Rocket;
73
+ }
74
+
75
+ /**
76
+ * 負責與底層容器引擎(如 Docker)溝通的轉接器介面
77
+ */
78
+ interface IDockerAdapter {
79
+ createBaseContainer(): Promise<string>;
80
+ copyFiles(containerId: string, sourcePath: string, targetPath: string): Promise<void>;
81
+ executeCommand(containerId: string, command: string[]): Promise<{
82
+ stdout: string;
83
+ stderr: string;
84
+ exitCode: number;
85
+ }>;
86
+ /**
87
+ * 獲取容器映射到宿主機的真實端口
88
+ */
89
+ getExposedPort(containerId: string, containerPort?: number): Promise<number>;
90
+ removeContainer(containerId: string): Promise<void>;
91
+ /**
92
+ * 根據 Label 批量移除容器
93
+ */
94
+ removeContainerByLabel(label: string): Promise<void>;
95
+ streamLogs(containerId: string, onData: (data: string) => void): void;
96
+ getStats(containerId: string): Promise<{
97
+ cpu: string;
98
+ memory: string;
99
+ }>;
100
+ }
101
+ /**
102
+ * 負責代碼獲取
103
+ */
104
+ interface IGitAdapter {
105
+ clone(repoUrl: string, branch: string): Promise<string>;
106
+ }
107
+ /**
108
+ * 負責動態反向代理與域名路由的轉接器
109
+ */
110
+ interface IRouterAdapter {
111
+ register(domain: string, targetUrl: string): void;
112
+ unregister(domain: string): void;
113
+ start(port: number): void;
114
+ }
115
+ /**
116
+ * 負責持久化火箭狀態的儲存庫介面
117
+ */
118
+ interface IRocketRepository {
119
+ save(rocket: Rocket): Promise<void>;
120
+ findById(id: string): Promise<Rocket | null>;
121
+ findIdle(): Promise<Rocket | null>;
122
+ findAll(): Promise<Rocket[]>;
123
+ delete(id: string): Promise<void>;
124
+ }
125
+
126
+ declare class PayloadInjector {
127
+ private docker;
128
+ private git;
129
+ constructor(docker: IDockerAdapter, git: IGitAdapter);
130
+ deploy(rocket: Rocket): Promise<void>;
131
+ }
132
+
133
+ declare class RefurbishUnit {
134
+ private docker;
135
+ constructor(docker: IDockerAdapter);
136
+ /**
137
+ * 執行火箭翻新邏輯
138
+ */
139
+ refurbish(rocket: Rocket): Promise<void>;
140
+ }
141
+
142
+ declare class PoolManager {
143
+ private dockerAdapter;
144
+ private rocketRepository;
145
+ private refurbishUnit?;
146
+ private router?;
147
+ constructor(dockerAdapter: IDockerAdapter, rocketRepository: IRocketRepository, refurbishUnit?: RefurbishUnit | undefined, router?: IRouterAdapter | undefined);
148
+ /**
149
+ * 初始化發射場:預先準備指定數量的火箭
150
+ */
151
+ warmup(count: number): Promise<void>;
152
+ /**
153
+ * 獲取一架可用的火箭並分配任務
154
+ */
155
+ assignMission(mission: Mission): Promise<Rocket>;
156
+ /**
157
+ * 回收指定任務的火箭
158
+ */
159
+ recycle(missionId: string): Promise<void>;
160
+ }
161
+
162
+ declare class MissionControl {
163
+ private poolManager;
164
+ private injector;
165
+ private docker;
166
+ private router?;
167
+ constructor(poolManager: PoolManager, injector: PayloadInjector, docker: IDockerAdapter, router?: IRouterAdapter | undefined);
168
+ launch(mission: Mission, onTelemetry: (type: string, data: any) => void): Promise<string>;
169
+ }
170
+
171
+ /**
172
+ * Gravito Launchpad Orbit
173
+ */
174
+ declare class LaunchpadOrbit implements GravitoOrbit {
175
+ private ripple;
176
+ constructor(ripple: OrbitRipple);
177
+ install(core: PlanetCore): Promise<void>;
178
+ }
179
+ /**
180
+ * 一鍵啟動 Launchpad 應用程式
181
+ */
182
+ declare function bootstrapLaunchpad(): Promise<{
183
+ port: number;
184
+ websocket: _gravito_ripple.WebSocketHandlerConfig;
185
+ fetch: (req: Request, server: any) => Response | Promise<Response> | undefined;
186
+ }>;
187
+
188
+ export { LaunchpadOrbit, Mission, MissionControl, PayloadInjector, PoolManager, RefurbishUnit, Rocket, RocketStatus, bootstrapLaunchpad };