forked from ansible/ansible-language-server
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
executionEnvironment.ts
370 lines (345 loc) · 11.9 KB
/
executionEnvironment.ts
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import * as child_process from "child_process";
import * as fs from "fs";
import * as path from "path";
import { URI } from "vscode-uri";
import { Connection } from "vscode-languageserver";
import { v4 as uuidv4 } from "uuid";
import { ImagePuller } from "../utils/imagePuller";
import { asyncExec } from "../utils/misc";
import { WorkspaceFolderContext } from "./workspaceManager";
import { IContainerEngine } from "../interfaces/extensionSettings";
export class ExecutionEnvironment {
private connection: Connection;
private context: WorkspaceFolderContext;
private useProgressTracker = false;
private successFileMarker = "SUCCESS";
private _container_engine: IContainerEngine;
private _container_image: string;
private _container_image_id: string;
constructor(connection: Connection, context: WorkspaceFolderContext) {
this.connection = connection;
this.context = context;
this.useProgressTracker =
!!context.clientCapabilities.window?.workDoneProgress;
}
public async initialize(): Promise<void> {
try {
const settings = await this.context.documentSettings.get(
this.context.workspaceFolder.uri
);
if (!settings.executionEnvironment.enabled) {
return;
}
this._container_image = settings.executionEnvironment.image;
this._container_engine = settings.executionEnvironment.containerEngine;
if (this._container_engine === "auto") {
for (const ce of ["podman", "docker"]) {
try {
child_process.execSync(`which ${ce}`, {
encoding: "utf-8",
});
} catch (error) {
this.connection.console.info(`Container engine '${ce}' not found`);
continue;
}
this._container_engine = <IContainerEngine>ce;
this.connection.console.log(`Container engine set to: '${ce}'`);
break;
}
} else {
try {
child_process.execSync(`which ${this._container_engine}`, {
encoding: "utf-8",
});
} catch (error) {
this.connection.window.showErrorMessage(
`Container engine '${this._container_engine}' not found. Failed with error '${error}'`
);
return;
}
}
if (!["podman", "docker"].includes(this._container_engine)) {
this.connection.window.showInformationMessage(
"No valid container engine found."
);
return;
}
const imagePuller = new ImagePuller(
this.connection,
this.context,
this._container_engine,
this._container_image,
settings.executionEnvironment.pullPolicy
);
const setupDone = await imagePuller.setupImage();
if (!setupDone) {
this.connection.window.showErrorMessage(
`Execution environment image '${this._container_image}' setup failed.
For more details check output console logs for ansible-language-server`
);
return;
}
await this.fetchPluginDocs();
} catch (error) {
if (error instanceof Error) {
this.connection.window.showErrorMessage(error.message);
} else {
this.connection.console.error(
`Exception in ExecutionEnvironment service: ${JSON.stringify(error)}`
);
}
}
}
async fetchPluginDocs(): Promise<void> {
const ansibleConfig = await this.context.ansibleConfig;
const containerName = `${this._container_image.replace(
/[^a-z0-9]/gi,
"_"
)}`;
let progressTracker;
try {
const containerImageIdCommand = `${this._container_engine} images ${this._container_image} --format="{{.ID}}" | head -n 1`;
this.connection.console.log(containerImageIdCommand);
this._container_image_id = child_process
.execSync(containerImageIdCommand, {
encoding: "utf-8",
})
.trim();
const hostCacheBasePath = path.resolve(
`${process.env.HOME}/.cache/ansible-language-server/${containerName}/${this._container_image_id}`
);
const isContainerRunning = this.runContainer(containerName);
if (!isContainerRunning) {
return;
}
if (this.isPluginDocCacheValid(hostCacheBasePath)) {
ansibleConfig.collections_paths = this.updateCachePaths(
ansibleConfig.collections_paths,
hostCacheBasePath
);
ansibleConfig.module_locations = this.updateCachePaths(
ansibleConfig.module_locations,
hostCacheBasePath
);
} else {
if (this.useProgressTracker) {
progressTracker =
await this.connection.window.createWorkDoneProgress();
}
if (progressTracker) {
progressTracker.begin(
"execution-environment",
undefined,
`Copy plugin docs from '${this._container_image} to host cache path`,
true
);
}
ansibleConfig.collections_paths = await this.copyPluginDocFiles(
hostCacheBasePath,
containerName,
ansibleConfig.collections_paths,
"ansible_collections"
);
const builtin_plugin_locations: string[] = [];
ansibleConfig.module_locations.forEach((modulePath) => {
const pluginsPathParts = modulePath.split(path.sep).slice(0, -1);
if (pluginsPathParts.includes("site-packages")) {
// ansible-config returns default builtin configured module path
// as ``<python-path>/site-packages/ansible/modules`` to copy other plugins
// to local cache strip the ``modules`` part from the path and append
// ``plugins`` folder.
pluginsPathParts.push("plugins");
}
builtin_plugin_locations.push(pluginsPathParts.join(path.sep));
});
// Copy builtin plugins
await this.copyPluginDocFiles(
hostCacheBasePath,
containerName,
builtin_plugin_locations,
"/"
);
// Copy builtin modules
ansibleConfig.module_locations = await this.copyPluginDocFiles(
hostCacheBasePath,
containerName,
ansibleConfig.module_locations,
"/"
);
}
// plugin cache successfully created
fs.closeSync(
fs.openSync(path.join(hostCacheBasePath, this.successFileMarker), "w")
);
} catch (error) {
this.connection.window.showErrorMessage(
`Exception in ExecutionEnvironment service while fetching docs: ${JSON.stringify(
error
)}`
);
} finally {
if (progressTracker) {
progressTracker.done();
}
this.cleanUpContainer(containerName);
}
}
public wrapContainerArgs(command: string, mountPaths?: Set<string>): string {
const workspaceFolderPath = URI.parse(
this.context.workspaceFolder.uri
).path;
const containerCommand: Array<string> = [this._container_engine];
containerCommand.push(...["run", "--rm"]);
containerCommand.push(...["--workdir", workspaceFolderPath]);
containerCommand.push(
...["-v", `${workspaceFolderPath}:${workspaceFolderPath}`]
);
for (const mountPath of mountPaths || []) {
const volumeMountPath = `${mountPath}:${mountPath}`;
if (containerCommand.includes(volumeMountPath)) {
continue;
}
containerCommand.push("-v", volumeMountPath);
}
if (this._container_engine === "podman") {
// container namespace stuff
containerCommand.push("--group-add=root");
containerCommand.push("--ipc=host");
// docker does not support this option
containerCommand.push("--quiet");
} else {
containerCommand.push(`--user=${process.getuid()}`);
}
containerCommand.push(`--name ansible_language_server_${uuidv4()}`);
containerCommand.push(this._container_image);
containerCommand.push(command);
const generatedCommand = containerCommand.join(" ");
this.connection.console.log(
`container engine invocation: ${generatedCommand}`
);
return generatedCommand;
}
public cleanUpContainer(containerName: string): void {
const cleanUpCommands = [
`${this._container_engine} stop ${containerName}`,
`${this._container_engine} rm ${containerName}`,
];
if (!this.doesContainerNameExist(containerName)) {
console.log(
`clean up container not required as container with name ${containerName} does not exist`
);
return;
}
for (const command of cleanUpCommands) {
try {
child_process.execSync(command, {
cwd: URI.parse(this.context.workspaceFolder.uri).path,
});
} catch (error) {
// container already stopped and/or removed
break;
}
}
}
public doesContainerNameExist(containerName: string): boolean {
let containerNameExist = false;
try {
child_process.execSync(
`${this._container_engine} inspect ${containerName}`
);
containerNameExist = true;
} catch (error) {
containerNameExist = false;
}
return containerNameExist;
}
private isPluginInPath(
containerName: string,
searchPath: string,
pluginFolderPath: string
): boolean {
const completeSearchPath = path.join(searchPath, pluginFolderPath);
const command = `${this._container_engine} exec ${containerName} ls ${completeSearchPath}`;
try {
this.connection.console.info(`Executing command ${command}`);
const result = child_process
.execSync(command, {
encoding: "utf-8",
})
.trim();
return result.trim() !== "";
} catch (error) {
this.connection.console.error(error);
return false;
}
}
private runContainer(containerName: string): boolean {
// ensure container is not running
this.cleanUpContainer(containerName);
try {
const command = `${this._container_engine} run --rm -it -d --name ${containerName} ${this._container_image} bash`;
this.connection.console.log(`run container with command '${command}'`);
child_process.execSync(command, {
encoding: "utf-8",
});
} catch (error) {
this.connection.window.showErrorMessage(
`Failed to initialize execution environment '${this._container_image}': ${error}`
);
return false;
}
return true;
}
private async copyPluginDocFiles(
hostPluginDocCacheBasePath: string,
containerName: string,
containerPluginPaths: string[],
searchKind: string
): Promise<string[]> {
const updatedHostDocPath: string[] = [];
containerPluginPaths.forEach((srcPath) => {
const destPath = path.join(hostPluginDocCacheBasePath, srcPath);
if (fs.existsSync(destPath)) {
updatedHostDocPath.push(destPath);
} else {
if (
srcPath === "" ||
!this.isPluginInPath(containerName, srcPath, searchKind)
) {
return;
}
const destPathFolder = destPath
.split(path.sep)
.slice(0, -1)
.join(path.sep);
fs.mkdirSync(destPath, { recursive: true });
const copyCommand = `${this._container_engine} cp ${containerName}:${srcPath} ${destPathFolder}`;
this.connection.console.log(
`Copying plugins from container to local cache path ${copyCommand}`
);
asyncExec(copyCommand, {
encoding: "utf-8",
});
updatedHostDocPath.push(destPath);
}
});
return updatedHostDocPath;
}
private updateCachePaths(
pluginPaths: string[],
cacheBasePath: string
): string[] {
const localCachePaths: string[] = [];
pluginPaths.forEach((srcPath) => {
const destPath = path.join(cacheBasePath, srcPath);
if (fs.existsSync(destPath)) {
localCachePaths.push(destPath);
}
});
return localCachePaths;
}
private isPluginDocCacheValid(hostCacheBasePath: string) {
const markerFilePath = path.join(hostCacheBasePath, this.successFileMarker);
return true ? fs.existsSync(markerFilePath) : false;
}
}