-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.service.ts
More file actions
234 lines (212 loc) · 6.68 KB
/
stack.service.ts
File metadata and controls
234 lines (212 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { Listr } from 'listr2';
import { createComposeCommandArgs } from '../lib/compose.js';
import type { GlobalCliOptions, UpCommandOptions } from '../types/cli.types.js';
import type { ProjectContext } from '../types/project.types.js';
import { assertPublishedPortsAvailable } from './host-preflight.service.js';
import { hasRunningComposeServices } from './compose-project.service.js';
import { assertNvidiaGpuRuntime } from './gpu-preflight.service.js';
import { printCommandHeader } from '../ui/banner.js';
import { formatTaskTitle, printInfo, printSuccess } from '../ui/logger.js';
import { runCommand } from '../utils/process.js';
import { createBootstrapTasks } from './bootstrap.service.js';
const LEGACY_IMAGES = [
'cli-node-lab-ollama-init:latest',
'cli-node-docker-atlas-lab-ollama-init:latest'
] as const;
const VERBOSE_TASK_RENDERER = 'verbose' as const;
/**
* Runs `docker compose up`, the bootstrap workflow, and the legacy image cleanup.
*/
export async function runUpCommand(
context: ProjectContext,
options: UpCommandOptions
): Promise<void> {
printCommandHeader({
title: 'Start Atlas Lab Stack',
summary: 'Bring Docker Compose up and reconcile runtime state',
projectRoot: context.projectRoot,
workingDirectory: context.workingDirectory
});
printInfo(`Enabled layers: ${describeEnabledLayers(options)}`, 'stack');
const stackWasRunning = await hasRunningComposeServices(context);
let composeStartedInThisRun = false;
const tasks = new Listr(
[
...(options.withAiLlm
? [
{
title: formatTaskTitle('host', 'Validate NVIDIA GPU runtime'),
task: async () => {
await assertNvidiaGpuRuntime();
}
}
]
: []),
{
title: formatTaskTitle('host', 'Validate published host ports'),
task: async () => {
await assertPublishedPortsAvailable(context, {
includeAiLlm: Boolean(options.withAiLlm),
includeWorkbench: Boolean(options.withWorkbench)
});
}
},
{
title: formatTaskTitle('stack', 'Start Docker Compose stack'),
task: async () => {
printInfo('Starting Docker Compose services in detached mode.', 'stack');
await runCommand('docker', createComposeUpArgs(context, options), {
cwd: context.projectRoot,
scope: 'compose'
});
printInfo('Docker Compose startup completed. Continuing with runtime bootstrap.', 'stack');
composeStartedInThisRun = true;
}
},
{
title: formatTaskTitle('stack', 'Run bootstrap workflow'),
task: () =>
new Listr(
createBootstrapTasks(context, {
skipGitea: false,
skipOllama: !options.withAiLlm,
withAiLlm: Boolean(options.withAiLlm)
}),
{
concurrent: false,
exitOnError: true,
renderer: VERBOSE_TASK_RENDERER
}
)
},
{
title: formatTaskTitle('stack', 'Remove legacy init images'),
task: async () => {
await cleanupLegacyImages(context.projectRoot);
}
}
],
{
concurrent: false,
exitOnError: true,
renderer: VERBOSE_TASK_RENDERER
}
);
try {
await tasks.run();
} catch (error) {
await rollbackPartialStartup(context, stackWasRunning, composeStartedInThisRun);
throw error;
}
printSuccess('Atlas Lab stack is ready.', 'stack');
}
/**
* Formats the selected Compose layers for the startup log header.
*/
function describeEnabledLayers(
options: Pick<UpCommandOptions, 'withAiLlm' | 'withWorkbench'>
): string {
const layers = ['core'];
if (options.withAiLlm) {
layers.push('ai-llm');
}
if (options.withWorkbench) {
layers.push('workbench');
}
return layers.join(', ');
}
/**
* Shows the current Compose status for the repository checkout.
*/
export async function runStatusCommand(
context: ProjectContext,
_options: GlobalCliOptions
): Promise<void> {
printCommandHeader({
title: 'Atlas Lab Status',
summary: 'Display Docker Compose services for this lab install',
projectRoot: context.projectRoot,
workingDirectory: context.workingDirectory
});
await runCommand('docker', createComposeCommandArgs(context, ['ps', '--all'], { includeAll: true }), {
cwd: context.projectRoot,
scope: 'compose'
});
}
/**
* Stops the lab stack and removes Compose orphans.
*/
export async function runDownCommand(
context: ProjectContext,
_options: GlobalCliOptions
): Promise<void> {
printCommandHeader({
title: 'Stop Atlas Lab Stack',
summary: 'Stop Docker Compose services and remove orphans',
projectRoot: context.projectRoot,
workingDirectory: context.workingDirectory
});
printInfo('Stopping the Atlas Lab stack...', 'stack');
await runCommand(
'docker',
createComposeCommandArgs(context, ['down', '--remove-orphans'], { includeAll: true }),
{
cwd: context.projectRoot,
scope: 'compose'
}
);
printSuccess('Atlas Lab stack stopped.', 'stack');
}
/**
* Builds the Docker Compose invocation for the `up` command.
*/
function createComposeUpArgs(context: ProjectContext, options: UpCommandOptions): string[] {
const composeArgs = ['up', '-d', '--remove-orphans'];
if (options.build) {
composeArgs.push('--build');
}
return createComposeCommandArgs(context, composeArgs, {
includeAiLlm: Boolean(options.withAiLlm),
includeWorkbench: Boolean(options.withWorkbench)
});
}
/**
* Removes the old one-shot init image if it is still present locally.
*/
async function cleanupLegacyImages(projectRoot: string): Promise<number> {
let removedImages = 0;
for (const image of LEGACY_IMAGES) {
const result = await runCommand('docker', ['image', 'rm', '-f', image], {
cwd: projectRoot,
captureOutput: true,
allowFailure: true,
scope: 'stack'
});
if (result.exitCode === 0) {
removedImages += 1;
}
}
return removedImages;
}
/**
* Stops a newly started stack after a later bootstrap failure so `up` does not leave stray runtime state behind.
*/
async function rollbackPartialStartup(
context: ProjectContext,
stackWasRunning: boolean,
composeStartedInThisRun: boolean
): Promise<void> {
if (stackWasRunning || !composeStartedInThisRun) {
return;
}
printInfo('Bootstrap failed after startup; stopping the partially started Atlas Lab stack.', 'stack');
await runCommand(
'docker',
createComposeCommandArgs(context, ['down', '--remove-orphans'], { includeAll: true }),
{
allowFailure: true,
cwd: context.projectRoot,
scope: 'compose'
}
);
}