dockerUtilities.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. import * as vscode from 'vscode';
  6. import * as cp from 'child_process';
  7. import * as path from 'path';
  8. import * as fs from 'fs';
  9. import { getWAMRExtensionVersion } from './lldbUtilities';
  10. import { downloadFile, unzipFile } from './directoryUtilities';
  11. import { SelectionOfPrompt, Status } from '../constants';
  12. const DOCKER_IMAGES_TEM_FOLDER_NAME = 'docker-resource';
  13. type SelectionStatus = SelectionOfPrompt | Status;
  14. const execShell = (cmd: string) =>
  15. new Promise<string>((resolve, reject) => {
  16. cp.exec(cmd, (error, result) => {
  17. if (error) {
  18. return reject(error);
  19. }
  20. return resolve(result);
  21. });
  22. });
  23. export async function promptSetupDockerImages(
  24. context: vscode.ExtensionContext
  25. ): Promise<SelectionStatus> {
  26. const extensionPath = context.extensionPath;
  27. const response = await vscode.window.showWarningMessage(
  28. 'Necessary docker images are not found. Setup now?',
  29. SelectionOfPrompt.setUp,
  30. SelectionOfPrompt.skip
  31. );
  32. if (response === SelectionOfPrompt.skip) {
  33. return response;
  34. }
  35. const downloadUrlArray = getDockerImagesDownloadUrl(context);
  36. const destinationFolder = path.resolve(
  37. extensionPath,
  38. 'resource',
  39. DOCKER_IMAGES_TEM_FOLDER_NAME
  40. );
  41. if (!fs.existsSync(destinationFolder)) {
  42. fs.mkdirSync(destinationFolder);
  43. }
  44. vscode.window.showInformationMessage(`Downloading Docker Images...`);
  45. for (const url of downloadUrlArray) {
  46. const imageZipName = path.basename(url);
  47. const imageStorePath = path.join(destinationFolder, imageZipName);
  48. await downloadFile(url, imageStorePath);
  49. /**
  50. * extract docker image tar package to
  51. * '${destinationFolder}'
  52. */
  53. const dockerImageFile = await unzipFile(imageStorePath, filename =>
  54. path.join(destinationFolder, filename)
  55. );
  56. /* give access before loading */
  57. dockerImageFile.forEach(file => fs.chmodSync(file, '0775'));
  58. /**NOTE - load docker image tar package to host
  59. * right now there are just one file
  60. * `docker-image-name.tar` inside so we can
  61. * directly use files[0] here, should be modified
  62. * if the package's files change
  63. */
  64. await execShell(`docker load -i ${dockerImageFile[0]}`);
  65. }
  66. /* remove the DOCKER_IMAGES_TEM_FOLDER */
  67. fs.rmSync(destinationFolder, { recursive: true, force: true });
  68. vscode.window.showInformationMessage(
  69. `Docker images are ready, please run '$docker images' to check.`
  70. );
  71. return Status.done;
  72. }
  73. export async function checkIfDockerStarted(): Promise<boolean> {
  74. try {
  75. await execShell('docker images');
  76. return true;
  77. } catch (e) {
  78. vscode.window.showWarningMessage((e as Error).message);
  79. return false;
  80. }
  81. }
  82. export async function checkIfDockerImagesExist(
  83. context: vscode.ExtensionContext
  84. ): Promise<boolean> {
  85. try {
  86. /* the tag of images is equal to extension's version */
  87. const imageTag = getWAMRExtensionVersion(context.extensionPath);
  88. await execShell(
  89. `docker image inspect wasm-debug-server:${imageTag} wasm-toolchain:${imageTag}`
  90. );
  91. return true;
  92. } catch (e) {
  93. return false;
  94. }
  95. }
  96. function getDockerImagesDownloadUrl(
  97. context: vscode.ExtensionContext
  98. ): string[] {
  99. const wamrVersion = getWAMRExtensionVersion(context.extensionPath);
  100. const wamrReleaseUrl = `https://github.com/bytecodealliance/wasm-micro-runtime/releases/download/WAMR`;
  101. return [
  102. `${wamrReleaseUrl}-${wamrVersion}/wasm-debug-server-${wamrVersion}.zip`,
  103. `${wamrReleaseUrl}-${wamrVersion}/wasm-toolchain-${wamrVersion}.zip`,
  104. ];
  105. }