|
| 1 | +import {mkdirp} from "fs-extra"; |
| 2 | +import assert from "node:assert"; |
| 3 | +import {spawnSync} from "node:child_process"; |
| 4 | +import {cp, readFile, writeFile} from "node:fs/promises"; |
| 5 | +import {tmpdir} from "node:os"; |
| 6 | +import path from "node:path"; |
| 7 | +import yargs from "yargs"; |
| 8 | +import type { |
| 9 | + TerraformMetadata, |
| 10 | + TerraformMetadataFromCli, |
| 11 | +} from "../src/utils/terraformUtils"; |
| 12 | + |
| 13 | +async function main() { |
| 14 | + const argv = await yargs |
| 15 | + .option("cli", { |
| 16 | + description: "Path to the Databricks CLI", |
| 17 | + type: "string", |
| 18 | + requiresArg: true, |
| 19 | + }) |
| 20 | + .option("binDir", { |
| 21 | + description: "Path to the bin directory", |
| 22 | + type: "string", |
| 23 | + requiresArg: true, |
| 24 | + }) |
| 25 | + .option("arch", { |
| 26 | + description: "Architecture of databricks cli.", |
| 27 | + type: "string", |
| 28 | + requiresArg: true, |
| 29 | + }) |
| 30 | + .option("package", { |
| 31 | + description: "path/to/package.json", |
| 32 | + type: "string", |
| 33 | + requiresArg: true, |
| 34 | + }).argv; |
| 35 | + |
| 36 | + const res = spawn(argv.cli!, [ |
| 37 | + "bundle", |
| 38 | + "debug", |
| 39 | + "terraform", |
| 40 | + "--output", |
| 41 | + "json", |
| 42 | + ]); |
| 43 | + const dependencies = JSON.parse(res.stdout.toString()); |
| 44 | + const terraform = dependencies.terraform as TerraformMetadataFromCli; |
| 45 | + assert(terraform, "cli must return terraform dependencies"); |
| 46 | + assert(terraform.version, "cli must return terraform version"); |
| 47 | + assert(terraform.providerHost, "cli must return provider host"); |
| 48 | + assert(terraform.providerSource, "cli must return provider source"); |
| 49 | + assert(terraform.providerVersion, "cli must return provider version"); |
| 50 | + |
| 51 | + const tempDir = path.join(tmpdir(), `terraform_${Date.now()}`); |
| 52 | + const depsDir = path.join(argv.binDir!, "dependencies"); |
| 53 | + await mkdirp(tempDir); |
| 54 | + await mkdirp(depsDir); |
| 55 | + |
| 56 | + // Download terraform bin for the selected arch |
| 57 | + const arch = argv.arch!; |
| 58 | + const terraformZip = `terraform_${terraform.version}_${arch}.zip`; |
| 59 | + const terraformUrl = `https://releases.hashicorp.com/terraform/${terraform.version}/${terraformZip}`; |
| 60 | + spawn("curl", ["-sLO", terraformUrl], {cwd: tempDir}); |
| 61 | + // Check sha of the archive |
| 62 | + const shasumsFile = `terraform_${terraform.version}_SHA256SUMS`; |
| 63 | + const shasumsUrl = `https://releases.hashicorp.com/terraform/${terraform.version}/${shasumsFile}`; |
| 64 | + spawn("curl", ["-sLO", shasumsUrl], {cwd: tempDir}); |
| 65 | + const shasumRes = spawn( |
| 66 | + "shasum", |
| 67 | + ["--algorithm", "256", "--check", shasumsFile], |
| 68 | + {cwd: tempDir} |
| 69 | + ); |
| 70 | + assert( |
| 71 | + shasumRes.output.toString().includes(`${terraformZip}: OK`), |
| 72 | + "sha256sum check failed" |
| 73 | + ); |
| 74 | + spawn("unzip", ["-q", terraformZip], {cwd: tempDir}); |
| 75 | + const fileExt = arch.includes("windows") ? ".exe" : ""; |
| 76 | + const terraformBinRelPath = path.join(depsDir, `terraform${fileExt}`); |
| 77 | + await cp(`${tempDir}/terraform${fileExt}`, terraformBinRelPath); |
| 78 | + // Set the path to the terraform bin, the extension will use it to setup the environment variables |
| 79 | + const execRelPath = terraformBinRelPath; |
| 80 | + |
| 81 | + // Download databricks provider archive for the selected arch |
| 82 | + const providerZip = `terraform-provider-databricks_${terraform.providerVersion}_${arch}.zip`; |
| 83 | + spawn( |
| 84 | + "gh", |
| 85 | + [ |
| 86 | + "release", |
| 87 | + "download", |
| 88 | + `v${terraform.providerVersion}`, |
| 89 | + "--pattern", |
| 90 | + providerZip, |
| 91 | + "--repo", |
| 92 | + "databricks/terraform-provider-databricks", |
| 93 | + ], |
| 94 | + {cwd: tempDir} |
| 95 | + ); |
| 96 | + const providersMirrorRelPath = path.join(depsDir, "providers"); |
| 97 | + const databricksProviderDir = path.join( |
| 98 | + providersMirrorRelPath, |
| 99 | + terraform.providerHost, |
| 100 | + terraform.providerSource |
| 101 | + ); |
| 102 | + await mkdirp(databricksProviderDir); |
| 103 | + await cp( |
| 104 | + path.join(tempDir, providerZip), |
| 105 | + path.join(databricksProviderDir, providerZip) |
| 106 | + ); |
| 107 | + // Set the path to the providers mirror dir, the extension will use it |
| 108 | + // to create the terraform CLI config at runtime. |
| 109 | + const terraformCliConfigRelPath = path.join(depsDir, "config.tfrc"); |
| 110 | + |
| 111 | + // Save the info about all dependencies to the package.json |
| 112 | + const terraformMetadata: TerraformMetadata = { |
| 113 | + ...terraform, |
| 114 | + execRelPath, |
| 115 | + providersMirrorRelPath, |
| 116 | + terraformCliConfigRelPath, |
| 117 | + }; |
| 118 | + const rawData = await readFile(argv.package!, {encoding: "utf-8"}); |
| 119 | + const jsonData = JSON.parse(rawData); |
| 120 | + jsonData["terraformMetadata"] = terraformMetadata; |
| 121 | + await writeFile(argv.package!, JSON.stringify(jsonData, null, 4), { |
| 122 | + encoding: "utf-8", |
| 123 | + }); |
| 124 | +} |
| 125 | + |
| 126 | +function spawn(command: string, args: string[], options: any = {}) { |
| 127 | + const child = spawnSync(command, args, options); |
| 128 | + if (child.error) { |
| 129 | + throw child.error; |
| 130 | + } else { |
| 131 | + return child; |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +main(); |
0 commit comments