@goldstack/utils-terraform 0.3.71 → 0.4.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.
@@ -1,17 +1,17 @@
1
- import { CloudProvider } from './cloudProvider';
2
- import { TerraformVersion } from './types/utilsTerraformConfig';
3
- export declare type Variables = [string, string][];
4
- interface TerraformOptions {
5
- dir?: string;
6
- provider: CloudProvider;
7
- variables?: Variables;
8
- backendConfig?: Variables;
9
- version: TerraformVersion;
10
- options?: string[];
11
- silent?: boolean;
12
- workspace?: string;
13
- }
14
- export declare const assertTerraform: () => void;
15
- export declare const tf: (cmd: string, options: TerraformOptions) => string;
16
- export {};
1
+ import { CloudProvider } from './cloudProvider';
2
+ import { TerraformVersion } from './types/utilsTerraformConfig';
3
+ export declare type Variables = [string, string][];
4
+ interface TerraformOptions {
5
+ dir?: string;
6
+ provider: CloudProvider;
7
+ variables?: Variables;
8
+ backendConfig?: Variables;
9
+ version: TerraformVersion;
10
+ options?: string[];
11
+ silent?: boolean;
12
+ workspace?: string;
13
+ }
14
+ export declare const assertTerraform: () => void;
15
+ export declare const tf: (cmd: string, options: TerraformOptions) => string;
16
+ export {};
17
17
  //# sourceMappingURL=terraformCli.d.ts.map
@@ -1,87 +1,87 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.tf = exports.assertTerraform = void 0;
4
- const utils_sh_1 = require("@goldstack/utils-sh");
5
- const utils_docker_1 = require("@goldstack/utils-docker");
6
- const utils_log_1 = require("@goldstack/utils-log");
7
- const renderVariables = (variables) => {
8
- if (variables.length === 0) {
9
- return '';
10
- }
11
- return variables
12
- .map(([key, value]) => {
13
- const isWin = process.platform === 'win32';
14
- let valueFixed = value.replace(/"/g, '\\"');
15
- // on anything that is not Windows, ensure '$' is escaped
16
- // so it is not replaced with a variable
17
- if (!isWin) {
18
- valueFixed = valueFixed.replace(/\$/g, '\\$');
19
- }
20
- return `-var \"${key}=${valueFixed}\" `;
21
- })
22
- .join('');
23
- };
24
- const renderBackendConfig = (variables) => {
25
- if (variables.length === 0) {
26
- return '';
27
- }
28
- return variables
29
- .map(([key, value]) => `-backend-config=\"${key}=${value}\" `)
30
- .join('');
31
- };
32
- const execWithDocker = (cmd, options) => {
33
- var _a;
34
- if (!options.dir) {
35
- options.dir = (0, utils_sh_1.pwd)();
36
- }
37
- (0, utils_docker_1.assertDocker)();
38
- let workspaceEnvVariable = '';
39
- if (options.workspace) {
40
- workspaceEnvVariable = `-e TF_WORKSPACE=${options.workspace}`;
41
- }
42
- const cmd3 = `docker run --rm -v "${options.dir}":/app ` +
43
- ` ${options.provider.generateEnvVariableString()} ${workspaceEnvVariable} ` +
44
- '-w /app ' +
45
- `${(0, utils_docker_1.imageTerraform)(options.version)} ${cmd} ` +
46
- ` ${renderBackendConfig(options.backendConfig || [])} ` +
47
- ` ${renderVariables(options.variables || [])} ` +
48
- ` ${((_a = options.options) === null || _a === void 0 ? void 0 : _a.join(' ')) || ''} `;
49
- return (0, utils_sh_1.exec)(cmd3, { silent: options.silent });
50
- };
51
- const assertTerraform = () => {
52
- if (!(0, utils_sh_1.commandExists)('terraform')) {
53
- (0, utils_log_1.fatal)('Terraform is not installed.\n\n' +
54
- 'Install terraform CLI or Docker (preferred).');
55
- throw new Error();
56
- }
57
- };
58
- exports.assertTerraform = assertTerraform;
59
- const execWithCli = (cmd, options) => {
60
- var _a;
61
- if (!options.dir) {
62
- options.dir = (0, utils_sh_1.pwd)();
63
- }
64
- (0, exports.assertTerraform)();
65
- const version = (0, utils_sh_1.exec)('terraform version');
66
- if (version.indexOf(options.version) === -1) {
67
- throw new Error(`Invalid local Terraform version detected: [${version.split('\n')[0]}], expected version compatible with [${options.version}]. Please install this version locally or install Docker for Goldstack to run the correct Terraform version required for this deployment using Docker.`);
68
- }
69
- options.provider.setEnvVariables();
70
- if (options.workspace) {
71
- process.env.TF_WORKSPACE = options.workspace;
72
- }
73
- const execCmd = `terraform ${cmd} ` +
74
- ` ${renderBackendConfig(options.backendConfig || [])} ` +
75
- ` ${renderVariables(options.variables || [])} ` +
76
- ` ${((_a = options.options) === null || _a === void 0 ? void 0 : _a.join(' ')) || ''} `;
77
- return (0, utils_sh_1.exec)(execCmd, { silent: options.silent });
78
- };
79
- const tf = (cmd, options) => {
80
- // always prefer running with Docker
81
- if ((0, utils_docker_1.hasDocker)()) {
82
- return execWithDocker(cmd, options);
83
- }
84
- return execWithCli(cmd, options);
85
- };
86
- exports.tf = tf;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.tf = exports.assertTerraform = void 0;
4
+ const utils_sh_1 = require("@goldstack/utils-sh");
5
+ const utils_docker_1 = require("@goldstack/utils-docker");
6
+ const utils_log_1 = require("@goldstack/utils-log");
7
+ const renderVariables = (variables) => {
8
+ if (variables.length === 0) {
9
+ return '';
10
+ }
11
+ return variables
12
+ .map(([key, value]) => {
13
+ const isWin = process.platform === 'win32';
14
+ let valueFixed = value.replace(/"/g, '\\"');
15
+ // on anything that is not Windows, ensure '$' is escaped
16
+ // so it is not replaced with a variable
17
+ if (!isWin) {
18
+ valueFixed = valueFixed.replace(/\$/g, '\\$');
19
+ }
20
+ return `-var \"${key}=${valueFixed}\" `;
21
+ })
22
+ .join('');
23
+ };
24
+ const renderBackendConfig = (variables) => {
25
+ if (variables.length === 0) {
26
+ return '';
27
+ }
28
+ return variables
29
+ .map(([key, value]) => `-backend-config=\"${key}=${value}\" `)
30
+ .join('');
31
+ };
32
+ const execWithDocker = (cmd, options) => {
33
+ var _a;
34
+ if (!options.dir) {
35
+ options.dir = (0, utils_sh_1.pwd)();
36
+ }
37
+ (0, utils_docker_1.assertDocker)();
38
+ let workspaceEnvVariable = '';
39
+ if (options.workspace) {
40
+ workspaceEnvVariable = `-e TF_WORKSPACE=${options.workspace}`;
41
+ }
42
+ const cmd3 = `docker run --rm -v "${options.dir}":/app ` +
43
+ ` ${options.provider.generateEnvVariableString()} ${workspaceEnvVariable} ` +
44
+ '-w /app ' +
45
+ `${(0, utils_docker_1.imageTerraform)(options.version)} ${cmd} ` +
46
+ ` ${renderBackendConfig(options.backendConfig || [])} ` +
47
+ ` ${renderVariables(options.variables || [])} ` +
48
+ ` ${((_a = options.options) === null || _a === void 0 ? void 0 : _a.join(' ')) || ''} `;
49
+ return (0, utils_sh_1.exec)(cmd3, { silent: options.silent });
50
+ };
51
+ const assertTerraform = () => {
52
+ if (!(0, utils_sh_1.commandExists)('terraform')) {
53
+ (0, utils_log_1.fatal)('Terraform is not installed.\n\n' +
54
+ 'Install terraform CLI or Docker (preferred).');
55
+ throw new Error();
56
+ }
57
+ };
58
+ exports.assertTerraform = assertTerraform;
59
+ const execWithCli = (cmd, options) => {
60
+ var _a;
61
+ if (!options.dir) {
62
+ options.dir = (0, utils_sh_1.pwd)();
63
+ }
64
+ (0, exports.assertTerraform)();
65
+ const version = (0, utils_sh_1.exec)('terraform version');
66
+ if (version.indexOf(options.version) === -1) {
67
+ throw new Error(`Invalid local Terraform version detected: [${version.split('\n')[0]}], expected version compatible with [${options.version}]. Please install this version locally or install Docker for Goldstack to run the correct Terraform version required for this deployment using Docker.`);
68
+ }
69
+ options.provider.setEnvVariables();
70
+ if (options.workspace) {
71
+ process.env.TF_WORKSPACE = options.workspace;
72
+ }
73
+ const execCmd = `terraform ${cmd} ` +
74
+ ` ${renderBackendConfig(options.backendConfig || [])} ` +
75
+ ` ${renderVariables(options.variables || [])} ` +
76
+ ` ${((_a = options.options) === null || _a === void 0 ? void 0 : _a.join(' ')) || ''} `;
77
+ return (0, utils_sh_1.exec)(execCmd, { silent: options.silent });
78
+ };
79
+ const tf = (cmd, options) => {
80
+ // always prefer running with Docker
81
+ if ((0, utils_docker_1.hasDocker)()) {
82
+ return execWithDocker(cmd, options);
83
+ }
84
+ return execWithCli(cmd, options);
85
+ };
86
+ exports.tf = tf;
87
87
  //# sourceMappingURL=terraformCli.js.map
@@ -1,36 +1,36 @@
1
- import { Deployment } from '@goldstack/infra';
2
- /**
3
- * Name of the property that should be converted into a Terraform variable.
4
- *
5
- * @title Terraform Variable
6
- *
7
- */
8
- export declare type TerraformVariable = string;
9
- /**
10
- * Key used for Terraform state persisted in Terraform state bucket.
11
- *
12
- * Will be auto-generated upon first deployment if not provided.
13
- *
14
- * @title Terraform State Key
15
- */
16
- export declare type TerraformStateKey = string;
17
- /**
18
- * Version of Terraform that the remote state for this deployment was created with.
19
- *
20
- * Go to the next version using `yarn infra upgrade [deploymentName] [targetVersion]`. Note that Terraform versions should only be increased one at a time, so for instance you can go from v0.12 to v0.13 but not from v0.12 to v0.14.
21
- *
22
- * @default '0.12'
23
- */
24
- export declare type TerraformVersion = '0.12' | '0.13' | '0.14' | '0.15' | '1.0' | '1.1';
25
- /**
26
- * Define which of the deployment variables will be made available for terraform.
27
- *
28
- * @title Terraform Variables
29
- */
30
- export declare type TerraformVariables = TerraformVariable[];
31
- export interface TerraformDeployment extends Deployment {
32
- terraformVariables?: TerraformVariables;
33
- tfStateKey?: TerraformStateKey;
34
- tfVersion?: TerraformVersion;
35
- }
1
+ import { Deployment } from '@goldstack/infra';
2
+ /**
3
+ * Name of the property that should be converted into a Terraform variable.
4
+ *
5
+ * @title Terraform Variable
6
+ *
7
+ */
8
+ export declare type TerraformVariable = string;
9
+ /**
10
+ * Key used for Terraform state persisted in Terraform state bucket.
11
+ *
12
+ * Will be auto-generated upon first deployment if not provided.
13
+ *
14
+ * @title Terraform State Key
15
+ */
16
+ export declare type TerraformStateKey = string;
17
+ /**
18
+ * Version of Terraform that the remote state for this deployment was created with.
19
+ *
20
+ * Go to the next version using `yarn infra upgrade [deploymentName] [targetVersion]`. Note that Terraform versions should only be increased one at a time, so for instance you can go from v0.12 to v0.13 but not from v0.12 to v0.14.
21
+ *
22
+ * @default '0.12'
23
+ */
24
+ export declare type TerraformVersion = '0.12' | '0.13' | '0.14' | '0.15' | '1.0' | '1.1';
25
+ /**
26
+ * Define which of the deployment variables will be made available for terraform.
27
+ *
28
+ * @title Terraform Variables
29
+ */
30
+ export declare type TerraformVariables = TerraformVariable[];
31
+ export interface TerraformDeployment extends Deployment {
32
+ terraformVariables?: TerraformVariables;
33
+ tfStateKey?: TerraformStateKey;
34
+ tfVersion?: TerraformVersion;
35
+ }
36
36
  //# sourceMappingURL=utilsTerraformConfig.d.ts.map
@@ -1,3 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  //# sourceMappingURL=utilsTerraformConfig.js.map
@@ -1,12 +1,12 @@
1
- export type { TerraformDeployment, TerraformVariable, TerraformVariables, } from './types/utilsTerraformConfig';
2
- import { CloudProvider } from './cloudProvider';
3
- export type { CloudProvider } from './cloudProvider';
4
- export { tf } from './terraformCli';
5
- export { getVariablesFromProperties } from './terraformBuild';
6
- export declare const infraCommands: () => any;
7
- export interface TerraformOptions {
8
- parallelism?: number;
9
- provider?: CloudProvider;
10
- }
11
- export declare const terraformCli: (args: string[], options: TerraformOptions) => void;
1
+ export type { TerraformDeployment, TerraformVariable, TerraformVariables, } from './types/utilsTerraformConfig';
2
+ import { CloudProvider } from './cloudProvider';
3
+ export type { CloudProvider } from './cloudProvider';
4
+ export { tf } from './terraformCli';
5
+ export { getVariablesFromProperties } from './terraformBuild';
6
+ export declare const infraCommands: () => any;
7
+ export interface TerraformOptions {
8
+ parallelism?: number;
9
+ provider?: CloudProvider;
10
+ }
11
+ export declare const terraformCli: (args: string[], options: TerraformOptions) => void;
12
12
  //# sourceMappingURL=utilsTerraform.d.ts.map
@@ -1,96 +1,96 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.terraformCli = exports.infraCommands = exports.getVariablesFromProperties = exports.tf = void 0;
4
- const terraformBuild_1 = require("./terraformBuild");
5
- var terraformCli_1 = require("./terraformCli");
6
- Object.defineProperty(exports, "tf", { enumerable: true, get: function () { return terraformCli_1.tf; } });
7
- var terraformBuild_2 = require("./terraformBuild");
8
- Object.defineProperty(exports, "getVariablesFromProperties", { enumerable: true, get: function () { return terraformBuild_2.getVariablesFromProperties; } });
9
- const infraCommands = () => {
10
- const deploymentPositional = (yargs) => {
11
- return yargs.positional('deployment', {
12
- type: 'string',
13
- describe: 'Name of the deployment this command should be applied to',
14
- demandOption: true,
15
- });
16
- };
17
- return (yargs) => {
18
- return yargs
19
- .command('up <deployment>', 'Stands up infrastructure for the specified deployment', deploymentPositional)
20
- .command('init <deployment>', 'Initialises Terraform working directory for deployment', deploymentPositional)
21
- .command('plan <deployment>', 'Creates a Terraform execution plan for deployment', deploymentPositional)
22
- .command('apply <deployment>', 'Applies the last Terraform execution plan calculated using `infra plan`', deploymentPositional)
23
- .command('destroy <deployment>', 'DANGER: Destroys all deployed infrastructure for the deployment', (yargs) => {
24
- return deploymentPositional(yargs).option('yes', {
25
- alias: 'y',
26
- description: 'DANGER: If provided, confirmation for deleting infrastructure resources will be skipped.',
27
- default: false,
28
- demandOption: false,
29
- type: 'boolean',
30
- });
31
- })
32
- .command('upgrade <deployment> <targetVersion>', 'Upgrades Terraform version to a new target version (experimental)', (yargs) => {
33
- return deploymentPositional(yargs).positional('targetVersion', {
34
- type: 'string',
35
- description: 'Provides the target Terraform version that should be migrated to.',
36
- demandOption: true,
37
- });
38
- })
39
- .command('terraform <deployment> [command..]', 'Runs an arbitrary Terraform CLI command', (yargs) => {
40
- return deploymentPositional(yargs)
41
- .option('inject-variables', {
42
- description: 'Injects variables into the Terraform CLI command.',
43
- default: false,
44
- type: 'boolean',
45
- demandOption: false,
46
- })
47
- .option('inject-backend-config', {
48
- description: 'Injects backend config into the Terraform CLI command.',
49
- default: false,
50
- type: 'boolean',
51
- demandOption: false,
52
- });
53
- });
54
- };
55
- };
56
- exports.infraCommands = infraCommands;
57
- const terraformCli = (args, options) => {
58
- if (!options.provider) {
59
- throw new Error('Cloud provider not defined');
60
- }
61
- const [operation, ...opArgs] = args;
62
- const build = new terraformBuild_1.TerraformBuild(options.provider);
63
- if (operation === 'up') {
64
- build.init(opArgs);
65
- build.plan(opArgs);
66
- build.apply(opArgs, options);
67
- return;
68
- }
69
- if (operation === 'init') {
70
- build.init(opArgs);
71
- return;
72
- }
73
- if (operation === 'plan') {
74
- build.plan(opArgs);
75
- return;
76
- }
77
- if (operation === 'apply') {
78
- build.apply(opArgs);
79
- return;
80
- }
81
- if (operation === 'destroy') {
82
- build.destroy(opArgs);
83
- return;
84
- }
85
- if (operation === 'upgrade') {
86
- build.upgrade(opArgs);
87
- return;
88
- }
89
- if (operation === 'terraform') {
90
- build.terraform(opArgs);
91
- return;
92
- }
93
- throw new Error('Unknown infrastructure operation: ' + operation);
94
- };
95
- exports.terraformCli = terraformCli;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.terraformCli = exports.infraCommands = exports.getVariablesFromProperties = exports.tf = void 0;
4
+ const terraformBuild_1 = require("./terraformBuild");
5
+ var terraformCli_1 = require("./terraformCli");
6
+ Object.defineProperty(exports, "tf", { enumerable: true, get: function () { return terraformCli_1.tf; } });
7
+ var terraformBuild_2 = require("./terraformBuild");
8
+ Object.defineProperty(exports, "getVariablesFromProperties", { enumerable: true, get: function () { return terraformBuild_2.getVariablesFromProperties; } });
9
+ const infraCommands = () => {
10
+ const deploymentPositional = (yargs) => {
11
+ return yargs.positional('deployment', {
12
+ type: 'string',
13
+ describe: 'Name of the deployment this command should be applied to',
14
+ demandOption: true,
15
+ });
16
+ };
17
+ return (yargs) => {
18
+ return yargs
19
+ .command('up <deployment>', 'Stands up infrastructure for the specified deployment', deploymentPositional)
20
+ .command('init <deployment>', 'Initialises Terraform working directory for deployment', deploymentPositional)
21
+ .command('plan <deployment>', 'Creates a Terraform execution plan for deployment', deploymentPositional)
22
+ .command('apply <deployment>', 'Applies the last Terraform execution plan calculated using `infra plan`', deploymentPositional)
23
+ .command('destroy <deployment>', 'DANGER: Destroys all deployed infrastructure for the deployment', (yargs) => {
24
+ return deploymentPositional(yargs).option('yes', {
25
+ alias: 'y',
26
+ description: 'DANGER: If provided, confirmation for deleting infrastructure resources will be skipped.',
27
+ default: false,
28
+ demandOption: false,
29
+ type: 'boolean',
30
+ });
31
+ })
32
+ .command('upgrade <deployment> <targetVersion>', 'Upgrades Terraform version to a new target version (experimental)', (yargs) => {
33
+ return deploymentPositional(yargs).positional('targetVersion', {
34
+ type: 'string',
35
+ description: 'Provides the target Terraform version that should be migrated to.',
36
+ demandOption: true,
37
+ });
38
+ })
39
+ .command('terraform <deployment> [command..]', 'Runs an arbitrary Terraform CLI command', (yargs) => {
40
+ return deploymentPositional(yargs)
41
+ .option('inject-variables', {
42
+ description: 'Injects variables into the Terraform CLI command.',
43
+ default: false,
44
+ type: 'boolean',
45
+ demandOption: false,
46
+ })
47
+ .option('inject-backend-config', {
48
+ description: 'Injects backend config into the Terraform CLI command.',
49
+ default: false,
50
+ type: 'boolean',
51
+ demandOption: false,
52
+ });
53
+ });
54
+ };
55
+ };
56
+ exports.infraCommands = infraCommands;
57
+ const terraformCli = (args, options) => {
58
+ if (!options.provider) {
59
+ throw new Error('Cloud provider not defined');
60
+ }
61
+ const [operation, ...opArgs] = args;
62
+ const build = new terraformBuild_1.TerraformBuild(options.provider);
63
+ if (operation === 'up') {
64
+ build.init(opArgs);
65
+ build.plan(opArgs);
66
+ build.apply(opArgs, options);
67
+ return;
68
+ }
69
+ if (operation === 'init') {
70
+ build.init(opArgs);
71
+ return;
72
+ }
73
+ if (operation === 'plan') {
74
+ build.plan(opArgs);
75
+ return;
76
+ }
77
+ if (operation === 'apply') {
78
+ build.apply(opArgs);
79
+ return;
80
+ }
81
+ if (operation === 'destroy') {
82
+ build.destroy(opArgs);
83
+ return;
84
+ }
85
+ if (operation === 'upgrade') {
86
+ build.upgrade(opArgs);
87
+ return;
88
+ }
89
+ if (operation === 'terraform') {
90
+ build.terraform(opArgs);
91
+ return;
92
+ }
93
+ throw new Error('Unknown infrastructure operation: ' + operation);
94
+ };
95
+ exports.terraformCli = terraformCli;
96
96
  //# sourceMappingURL=utilsTerraform.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goldstack/utils-terraform",
3
- "version": "0.3.71",
3
+ "version": "0.4.0",
4
4
  "description": "Run Terraform commands easily as npm package scripts",
5
5
  "keywords": [
6
6
  "goldstack",
@@ -37,22 +37,22 @@
37
37
  "version:apply:force": "yarn version $@ && yarn version apply"
38
38
  },
39
39
  "dependencies": {
40
- "@goldstack/infra": "0.3.38",
41
- "@goldstack/infra-aws": "0.3.47",
42
- "@goldstack/utils-config": "0.3.35",
43
- "@goldstack/utils-docker": "0.3.40",
44
- "@goldstack/utils-log": "0.2.16",
45
- "@goldstack/utils-package": "0.3.41",
46
- "@goldstack/utils-sh": "0.4.33",
40
+ "@goldstack/infra": "0.4.0",
41
+ "@goldstack/infra-aws": "0.4.0",
42
+ "@goldstack/utils-config": "0.4.0",
43
+ "@goldstack/utils-docker": "0.4.0",
44
+ "@goldstack/utils-log": "0.3.0",
45
+ "@goldstack/utils-package": "0.4.0",
46
+ "@goldstack/utils-sh": "0.5.0",
47
47
  "json-stable-stringify": "^1.0.1",
48
48
  "prompt-sync": "^4.2.0",
49
49
  "yargs": "^17.5.1"
50
50
  },
51
51
  "devDependencies": {
52
- "@goldstack/utils-git": "0.1.36",
53
- "@types/jest": "^27.5.1",
52
+ "@goldstack/utils-git": "0.2.0",
53
+ "@types/jest": "^28.1.8",
54
54
  "@types/json-stable-stringify": "^1",
55
- "@types/node": "^17.0.33",
55
+ "@types/node": "^18.7.13",
56
56
  "@types/prompt-sync": "^4.1.0",
57
57
  "@types/yargs": "^17.0.10",
58
58
  "jest": "^28.1.0",