@goldstack/utils-terraform 0.3.46 → 0.3.47

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,362 +1,294 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.TerraformBuild = exports.getVariablesFromHCL = exports.parseVariables = exports.getVariablesFromProperties = exports.convertFromPythonVariable = exports.convertToPythonVariable = void 0;
7
- const prompt_sync_1 = __importDefault(require("prompt-sync"));
8
- const utils_log_1 = require("@goldstack/utils-log");
9
- const terraformCli_1 = require("./terraformCli");
10
- const utils_sh_1 = require("@goldstack/utils-sh");
11
- const utils_package_1 = require("@goldstack/utils-package");
12
- const assert_1 = __importDefault(require("assert"));
13
- const fs_1 = __importDefault(require("fs"));
14
- const crypto_1 = __importDefault(require("crypto"));
15
- const infra_1 = require("@goldstack/infra");
16
- const json_stable_stringify_1 = __importDefault(require("json-stable-stringify"));
17
- const path_1 = __importDefault(require("path"));
18
- const convertToPythonVariable = (variableName) => {
19
- let res = '';
20
- for (const char of variableName) {
21
- if (char.toLowerCase() === char) {
22
- res += char;
23
- }
24
- else {
25
- res += '_' + char.toLowerCase();
26
- }
27
- }
28
- return res;
29
- };
30
- exports.convertToPythonVariable = convertToPythonVariable;
31
- const convertFromPythonVariable = (variableName) => {
32
- let res = '';
33
- for (let i = 0; i < variableName.length; i++) {
34
- const char = variableName.charAt(i);
35
- if (i > 0 && variableName.charAt(i - 1) === '_') {
36
- res += variableName.charAt(i).toUpperCase();
37
- }
38
- else if (variableName.charAt(i) !== '_') {
39
- res += char;
40
- }
41
- }
42
- return res;
43
- };
44
- exports.convertFromPythonVariable = convertFromPythonVariable;
45
- const getVariablesFromProperties = (properties, terraformVariables) => {
46
- const vars = [];
47
- if (!terraformVariables) {
48
- return vars;
49
- }
50
- for (const key in properties) {
51
- if (terraformVariables.find((varName) => varName === key)) {
52
- const variableName = (0, exports.convertToPythonVariable)(key);
53
- const variableValue = properties[key];
54
- if (variableValue !== '') {
55
- vars.push([variableName, variableValue]);
56
- }
57
- else {
58
- vars.push([
59
- variableName,
60
- process.env[variableName.toLocaleUpperCase()] || '',
61
- ]);
62
- }
63
- }
64
- }
65
- return vars;
66
- };
67
- exports.getVariablesFromProperties = getVariablesFromProperties;
68
- const parseVariables = (hcl) => {
69
- const reg = /variable[^"]*"([^"]*)"[^{]*{/gm;
70
- let result;
71
- const variableNames = [];
72
- while ((result = reg.exec(hcl)) !== null) {
73
- variableNames.push(result[1]);
74
- }
75
- return variableNames;
76
- };
77
- exports.parseVariables = parseVariables;
78
- const getVariablesFromHCL = (properties) => {
79
- if (!fs_1.default.existsSync('./variables.tf')) {
80
- console.warn(`No variables.tf file exists in ${(0, utils_sh_1.pwd)()}. Goldstack only supports declaring variables in a variables.tf file.`);
81
- return [];
82
- }
83
- const variablesHCL = (0, utils_sh_1.read)('./variables.tf');
84
- const hclVariableNames = (0, exports.parseVariables)(variablesHCL);
85
- const jsVariableNames = hclVariableNames.map((hclVarName) => (0, exports.convertFromPythonVariable)(hclVarName));
86
- jsVariableNames.forEach((key) => {
87
- if (!properties.hasOwnProperty(key)) {
88
- console.warn(`Cannot find property "${key}" in Goldstack configuration. Therefore terraform variable ${(0, exports.convertToPythonVariable)(key)} will not be provided by Goldstack.`);
89
- }
90
- });
91
- const vars = [];
92
- for (const key in properties) {
93
- if (key.indexOf('_') !== -1) {
94
- console.warn('Property in Goldstack configuration contains "_". This is not recommended. Property: ' +
95
- key +
96
- ' Please use valid JavaScript variable names. For instance, use "myVar" instead of "my_var". ' +
97
- ' Goldstack will automatically convert these to Terraform variables ("myVar" -> "my_var") when required.');
98
- }
99
- if (jsVariableNames.find((varName) => varName === key)) {
100
- const variableName = (0, exports.convertToPythonVariable)(key);
101
- const variableValue = properties[key];
102
- if (variableValue !== '') {
103
- if (typeof variableValue === 'string') {
104
- vars.push([variableName, variableValue]);
105
- }
106
- else if (typeof variableValue === 'number') {
107
- vars.push([variableName, `${variableValue}`]);
108
- }
109
- else if (typeof variableValue === 'object') {
110
- vars.push([variableName, `${(0, json_stable_stringify_1.default)(variableValue)}`]);
111
- }
112
- else {
113
- throw new Error(`Not supported type for variable ${variableName}: ${typeof variableValue}`);
114
- }
115
- }
116
- else {
117
- const environmentVarialbeName = variableName.toLocaleUpperCase();
118
- if (process.env[environmentVarialbeName] ||
119
- process.env[environmentVarialbeName] === '') {
120
- console.log('Setting terraform variable from environment variable');
121
- vars.push([variableName, process.env[environmentVarialbeName] || '']);
122
- }
123
- else {
124
- console.log('Terraform variable will not be defined', variableName);
125
- }
126
- }
127
- }
128
- }
129
- return vars;
130
- };
131
- exports.getVariablesFromHCL = getVariablesFromHCL;
132
- const getDeployment = (args) => {
133
- if (args.length < 1) {
134
- throw new Error('Please specify the name of the deployment.');
135
- }
136
- const name = args[0];
137
- const packageConfig = (0, utils_package_1.readPackageConfig)();
138
- const deployment = packageConfig.deployments.find((deployment) => deployment.name === name);
139
- if (!deployment) {
140
- (0, utils_log_1.fatal)(`Cannot find configuration for deployment '${name}''`);
141
- throw new Error('Cannot parse configuration.');
142
- }
143
- return deployment;
144
- };
145
- class TerraformBuild {
146
- constructor(provider) {
147
- this.getTfStateVariables = (deployment) => {
148
- const packageConfig = (0, utils_package_1.readPackageConfig)();
149
- const deployments = packageConfig.deployments.filter((d) => d.name === deployment.name);
150
- if (deployments.length !== 1) {
151
- throw new Error(`Cannot find deployment ${deployment.name}`);
152
- }
153
- const deploymentConfig = deployments[0];
154
- // initialise id for key if required
155
- if (!deploymentConfig.tfStateKey) {
156
- const stateHash = crypto_1.default.randomBytes(10).toString('hex');
157
- const stateKey = `${packageConfig.name}-${deployment.name}-${stateHash}.tfstate`;
158
- console.log(`Intialising Terraform State key for ${deployment.name} to ${stateKey}`);
159
- deploymentConfig.tfStateKey = stateKey;
160
- (0, utils_package_1.writePackageConfig)(packageConfig);
161
- }
162
- return this.provider.getTfStateVariables(deploymentConfig);
163
- };
164
- this.getTfVersion = (args) => {
165
- const deployment = getDeployment(args);
166
- if (deployment.tfVersion) {
167
- return deployment.tfVersion;
168
- }
169
- if (fs_1.default.existsSync('./infra/tfConfig.json')) {
170
- try {
171
- const tsConfig = JSON.parse((0, utils_sh_1.read)('./infra/tfConfig.json'));
172
- return tsConfig.tfVersion;
173
- }
174
- catch (e) {
175
- throw new Error('Invalid Terraform configuration in ' +
176
- path_1.default.resolve('./infra/tfConfig.json'));
177
- }
178
- }
179
- // before Terraform versions were introduced, only version 0.12 was supported
180
- return '0.12';
181
- };
182
- this.init = (args) => {
183
- const deployment = getDeployment(args);
184
- const version = deployment.tfVersion || '0.12';
185
- const backendConfig = this.getTfStateVariables(deployment);
186
- (0, utils_sh_1.cd)('./infra/aws');
187
- const provider = this.provider;
188
- (0, terraformCli_1.tf)('init', {
189
- provider,
190
- version,
191
- backendConfig,
192
- options: ['-force-copy', '-reconfigure'],
193
- });
194
- const workspaces = (0, terraformCli_1.tf)('workspace list', { provider, version });
195
- const deploymentName = args[0];
196
- const workspaceExists = workspaces.split('\n').find((line) => {
197
- return line.indexOf(deploymentName) >= 0;
198
- });
199
- if (!workspaceExists) {
200
- (0, terraformCli_1.tf)(`workspace new ${deploymentName}`, {
201
- provider,
202
- version,
203
- });
204
- (0, terraformCli_1.tf)(`workspace select ${deploymentName}`, {
205
- provider,
206
- version,
207
- });
208
- }
209
- (0, utils_sh_1.cd)('../..');
210
- };
211
- this.plan = (args) => {
212
- const deployment = getDeployment(args);
213
- const version = deployment.tfVersion || '0.12';
214
- const backendConfig = this.getTfStateVariables(deployment);
215
- (0, utils_sh_1.cd)('./infra/aws');
216
- const provider = this.provider;
217
- const variables = [
218
- ...(0, exports.getVariablesFromHCL)({ ...deployment, ...deployment.configuration }),
219
- ];
220
- const currentWorkspace = (0, terraformCli_1.tf)('workspace show', { provider, version }).trim();
221
- if (currentWorkspace !== args[0]) {
222
- // init with reconfigure required here in case we are switching to a different
223
- // s3 bucket in a different environment for a different deployment
224
- (0, terraformCli_1.tf)('init', {
225
- provider,
226
- backendConfig,
227
- version,
228
- options: ['-reconfigure'],
229
- });
230
- (0, terraformCli_1.tf)(`workspace select ${args[0]}`, { provider, version });
231
- }
232
- (0, terraformCli_1.tf)('plan', {
233
- provider,
234
- variables,
235
- version,
236
- options: ['-input=false', '-out tfplan'],
237
- });
238
- (0, utils_sh_1.cd)('../..');
239
- };
240
- this.apply = (args) => {
241
- const deployment = getDeployment(args);
242
- const version = deployment.tfVersion || '0.12';
243
- const backendConfig = this.getTfStateVariables(deployment);
244
- (0, utils_sh_1.cd)('./infra/aws');
245
- const provider = this.provider;
246
- const deploymentName = args[0];
247
- const currentWorkspace = (0, terraformCli_1.tf)('workspace show', { provider, version }).trim();
248
- if (currentWorkspace !== deploymentName) {
249
- // init with reconfigure required here in case we are switching to a different
250
- // s3 bucket in a different environment for a different deployment
251
- (0, terraformCli_1.tf)('init', {
252
- provider,
253
- backendConfig,
254
- version,
255
- options: ['-reconfigure'],
256
- });
257
- (0, terraformCli_1.tf)(`workspace select ${deploymentName}`, { provider, version });
258
- }
259
- (0, terraformCli_1.tf)('apply', {
260
- provider,
261
- options: ['-input=false', 'tfplan'],
262
- version,
263
- });
264
- const res = (0, terraformCli_1.tf)('output', { provider, options: ['-json'], version }).trim();
265
- const deploymentState = (0, infra_1.readDeploymentState)('./../../', deploymentName, {
266
- createIfNotExist: true,
267
- });
268
- deploymentState.terraform = JSON.parse(res);
269
- (0, infra_1.writeDeploymentState)('./../../', deploymentState);
270
- (0, utils_sh_1.cd)('../..');
271
- };
272
- this.destroy = (args) => {
273
- const deployment = getDeployment(args);
274
- const version = deployment.tfVersion || '0.12';
275
- const backendConfig = this.getTfStateVariables(deployment);
276
- (0, utils_sh_1.cd)('./infra/aws');
277
- const ciConfirmed = args.find((str) => str === '-y');
278
- if (!ciConfirmed) {
279
- const value = (0, prompt_sync_1.default)()('Are you sure to destroy your deployed resources? If yes, please type `y` and enter.\n' +
280
- 'Otherwise, press enter.\nYour Input: ');
281
- if (value !== 'y') {
282
- (0, utils_log_1.fatal)('Prompt not confirmed with `y`');
283
- }
284
- }
285
- const provider = this.provider;
286
- const variables = [
287
- ...(0, exports.getVariablesFromHCL)({ ...deployment, ...deployment.configuration }),
288
- ];
289
- (0, terraformCli_1.tf)('init', { provider, backendConfig, options: ['-reconfigure'], version });
290
- (0, terraformCli_1.tf)(`workspace select ${args[0]}`, { provider, version });
291
- (0, terraformCli_1.tf)('plan', {
292
- provider,
293
- variables,
294
- version,
295
- options: ['-input=false', '-out tfplan'],
296
- });
297
- (0, terraformCli_1.tf)('destroy', {
298
- provider,
299
- variables,
300
- version,
301
- options: ['-input=false', '-auto-approve'],
302
- });
303
- (0, utils_sh_1.cd)('../..');
304
- };
305
- this.performUpgrade = (deploymentName, targetVersion) => {
306
- const provider = this.provider;
307
- if (targetVersion === '0.13') {
308
- (0, utils_sh_1.cd)('./infra/aws');
309
- const upgradeRes = (0, terraformCli_1.tf)(`${targetVersion}upgrade`, {
310
- version: targetVersion,
311
- provider,
312
- options: ['-yes'],
313
- });
314
- if (upgradeRes.indexOf('Upgrade complete!') === -1) {
315
- throw new Error('Upgrade of Terraform version not successful.');
316
- }
317
- (0, utils_sh_1.cd)('../..');
318
- }
319
- const packageConfig = (0, utils_package_1.readPackageConfig)();
320
- const deploymentInConfig = packageConfig.deployments.find((e) => e.name === deploymentName);
321
- (0, assert_1.default)(deploymentInConfig);
322
- deploymentInConfig.tfVersion = targetVersion;
323
- (0, utils_package_1.writePackageConfig)(packageConfig);
324
- this.init([deploymentName]);
325
- console.log(`Version upgraded to ${targetVersion}. Please run deployment to upgrade remote state before further upgrades.`);
326
- };
327
- this.upgrade = (args) => {
328
- const deployment = getDeployment(args);
329
- const version = deployment.tfVersion || '0.12';
330
- const newVersion = args[1];
331
- if (version === newVersion) {
332
- console.log('Already on version', newVersion);
333
- return;
334
- }
335
- if (version === '0.12' && newVersion === '0.13') {
336
- this.performUpgrade(args[0], '0.13');
337
- return;
338
- }
339
- if (version === '0.13' && newVersion === '0.14') {
340
- this.performUpgrade(args[0], '0.14');
341
- return;
342
- }
343
- if (version === '0.14' && newVersion === '0.15') {
344
- this.performUpgrade(args[0], '0.15');
345
- return;
346
- }
347
- if (version === '0.15' && newVersion === '1.0') {
348
- this.performUpgrade(args[0], '1.0');
349
- return;
350
- }
351
- if (version === '1.0' && newVersion === '1.1') {
352
- this.performUpgrade(args[0], '1.1');
353
- return;
354
- }
355
- throw new Error(`Version upgrade not supported: from [${version}] to [${newVersion}]. Currently only 0.12 -> 0.13 is supported.`);
356
- };
357
- (0, assert_1.default)(provider);
358
- this.provider = provider;
359
- }
360
- }
361
- exports.TerraformBuild = TerraformBuild;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.TerraformBuild = exports.getVariablesFromHCL = exports.parseVariables = exports.getVariablesFromProperties = exports.convertFromPythonVariable = exports.convertToPythonVariable = void 0;
7
+ const prompt_sync_1 = __importDefault(require("prompt-sync"));
8
+ const utils_log_1 = require("@goldstack/utils-log");
9
+ const terraformCli_1 = require("./terraformCli");
10
+ const utils_sh_1 = require("@goldstack/utils-sh");
11
+ const utils_package_1 = require("@goldstack/utils-package");
12
+ const assert_1 = __importDefault(require("assert"));
13
+ const fs_1 = __importDefault(require("fs"));
14
+ const crypto_1 = __importDefault(require("crypto"));
15
+ const infra_1 = require("@goldstack/infra");
16
+ const json_stable_stringify_1 = __importDefault(require("json-stable-stringify"));
17
+ const convertToPythonVariable = (variableName) => {
18
+ let res = '';
19
+ for (const char of variableName) {
20
+ if (char.toLowerCase() === char) {
21
+ res += char;
22
+ }
23
+ else {
24
+ res += '_' + char.toLowerCase();
25
+ }
26
+ }
27
+ return res;
28
+ };
29
+ exports.convertToPythonVariable = convertToPythonVariable;
30
+ const convertFromPythonVariable = (variableName) => {
31
+ let res = '';
32
+ for (let i = 0; i < variableName.length; i++) {
33
+ const char = variableName.charAt(i);
34
+ if (i > 0 && variableName.charAt(i - 1) === '_') {
35
+ res += variableName.charAt(i).toUpperCase();
36
+ }
37
+ else if (variableName.charAt(i) !== '_') {
38
+ res += char;
39
+ }
40
+ }
41
+ return res;
42
+ };
43
+ exports.convertFromPythonVariable = convertFromPythonVariable;
44
+ const getVariablesFromProperties = (properties, terraformVariables) => {
45
+ const vars = [];
46
+ if (!terraformVariables) {
47
+ return vars;
48
+ }
49
+ for (const key in properties) {
50
+ if (terraformVariables.find((varName) => varName === key)) {
51
+ const variableName = (0, exports.convertToPythonVariable)(key);
52
+ const variableValue = properties[key];
53
+ if (variableValue !== '') {
54
+ vars.push([variableName, variableValue]);
55
+ }
56
+ else {
57
+ vars.push([
58
+ variableName,
59
+ process.env[variableName.toLocaleUpperCase()] || '',
60
+ ]);
61
+ }
62
+ }
63
+ }
64
+ return vars;
65
+ };
66
+ exports.getVariablesFromProperties = getVariablesFromProperties;
67
+ const parseVariables = (hcl) => {
68
+ const reg = /variable[^"]*"([^"]*)"[^{]*{/gm;
69
+ let result;
70
+ const variableNames = [];
71
+ while ((result = reg.exec(hcl)) !== null) {
72
+ variableNames.push(result[1]);
73
+ }
74
+ return variableNames;
75
+ };
76
+ exports.parseVariables = parseVariables;
77
+ const getVariablesFromHCL = (properties) => {
78
+ if (!fs_1.default.existsSync('./variables.tf')) {
79
+ console.warn(`No variables.tf file exists in ${(0, utils_sh_1.pwd)()}. Goldstack only supports declaring variables in a variables.tf file.`);
80
+ return [];
81
+ }
82
+ const variablesHCL = (0, utils_sh_1.read)('./variables.tf');
83
+ const hclVariableNames = (0, exports.parseVariables)(variablesHCL);
84
+ const jsVariableNames = hclVariableNames.map((hclVarName) => (0, exports.convertFromPythonVariable)(hclVarName));
85
+ jsVariableNames.forEach((key) => {
86
+ if (!properties.hasOwnProperty(key)) {
87
+ console.warn(`Cannot find property "${key}" in Goldstack configuration. Therefore terraform variable ${(0, exports.convertToPythonVariable)(key)} will not be provided by Goldstack.`);
88
+ }
89
+ });
90
+ const vars = [];
91
+ for (const key in properties) {
92
+ if (key.indexOf('_') !== -1) {
93
+ console.warn('Property in Goldstack configuration contains "_". This is not recommended. Property: ' +
94
+ key +
95
+ ' Please use valid JavaScript variable names. For instance, use "myVar" instead of "my_var". ' +
96
+ ' Goldstack will automatically convert these to Terraform variables ("myVar" -> "my_var") when required.');
97
+ }
98
+ if (jsVariableNames.find((varName) => varName === key)) {
99
+ const variableName = (0, exports.convertToPythonVariable)(key);
100
+ const variableValue = properties[key];
101
+ if (variableValue !== '') {
102
+ if (typeof variableValue === 'string') {
103
+ vars.push([variableName, variableValue]);
104
+ }
105
+ else if (typeof variableValue === 'number') {
106
+ vars.push([variableName, `${variableValue}`]);
107
+ }
108
+ else if (typeof variableValue === 'object') {
109
+ vars.push([
110
+ variableName,
111
+ `${(0, json_stable_stringify_1.default)(variableValue).replace(/"/g, '\\"')}`,
112
+ ]);
113
+ }
114
+ else {
115
+ throw new Error(`Not supported type for variable ${variableName}: ${typeof variableValue}`);
116
+ }
117
+ }
118
+ else {
119
+ const environmentVarialbeName = variableName.toLocaleUpperCase();
120
+ if (process.env[environmentVarialbeName] ||
121
+ process.env[environmentVarialbeName] === '') {
122
+ console.log('Setting terraform variable from environment variable');
123
+ vars.push([variableName, process.env[environmentVarialbeName] || '']);
124
+ }
125
+ else {
126
+ console.log('Terraform variable will not be defined', variableName);
127
+ }
128
+ }
129
+ }
130
+ }
131
+ return vars;
132
+ };
133
+ exports.getVariablesFromHCL = getVariablesFromHCL;
134
+ const getDeployment = (args) => {
135
+ if (args.length < 1) {
136
+ throw new Error('Please specify the name of the deployment.');
137
+ }
138
+ const name = args[0];
139
+ const packageConfig = (0, utils_package_1.readPackageConfig)();
140
+ const deployment = packageConfig.deployments.find((deployment) => deployment.name === name);
141
+ if (!deployment) {
142
+ (0, utils_log_1.fatal)(`Cannot find configuration for deployment '${name}''`);
143
+ throw new Error('Cannot parse configuration.');
144
+ }
145
+ return deployment;
146
+ };
147
+ class TerraformBuild {
148
+ constructor(provider) {
149
+ this.getTfStateVariables = (deployment) => {
150
+ const packageConfig = (0, utils_package_1.readPackageConfig)();
151
+ const deployments = packageConfig.deployments.filter((d) => d.name === deployment.name);
152
+ if (deployments.length !== 1) {
153
+ throw new Error(`Cannot find deployment ${deployment.name}`);
154
+ }
155
+ const deploymentConfig = deployments[0];
156
+ // initialise id for key if required
157
+ if (!deploymentConfig.tfStateKey) {
158
+ const stateHash = crypto_1.default.randomBytes(10).toString('hex');
159
+ const stateKey = `${packageConfig.name}-${deployment.name}-${stateHash}.tfstate`;
160
+ console.log(`Intialising Terraform State key for ${deployment.name} to ${stateKey}`);
161
+ deploymentConfig.tfStateKey = stateKey;
162
+ (0, utils_package_1.writePackageConfig)(packageConfig);
163
+ }
164
+ return this.provider.getTfStateVariables(deploymentConfig);
165
+ };
166
+ this.init = (args) => {
167
+ const deployment = getDeployment(args);
168
+ const version = deployment.tfVersion || '0.12';
169
+ const backendConfig = this.getTfStateVariables(deployment);
170
+ (0, utils_sh_1.cd)('./infra/aws');
171
+ const provider = this.provider;
172
+ (0, terraformCli_1.tf)('init', {
173
+ provider,
174
+ version,
175
+ backendConfig,
176
+ options: ['-force-copy', '-reconfigure'],
177
+ });
178
+ const workspaces = (0, terraformCli_1.tf)('workspace list', { provider, version });
179
+ const deploymentName = args[0];
180
+ const workspaceExists = workspaces.split('\n').find((line) => {
181
+ return line.indexOf(deploymentName) >= 0;
182
+ });
183
+ if (!workspaceExists) {
184
+ (0, terraformCli_1.tf)(`workspace new ${deploymentName}`, {
185
+ provider,
186
+ version,
187
+ });
188
+ (0, terraformCli_1.tf)(`workspace select ${deploymentName}`, {
189
+ provider,
190
+ version,
191
+ });
192
+ }
193
+ (0, utils_sh_1.cd)('../..');
194
+ };
195
+ this.plan = (args) => {
196
+ const deployment = getDeployment(args);
197
+ const version = deployment.tfVersion || '0.12';
198
+ const backendConfig = this.getTfStateVariables(deployment);
199
+ (0, utils_sh_1.cd)('./infra/aws');
200
+ const provider = this.provider;
201
+ const variables = [
202
+ ...(0, exports.getVariablesFromHCL)({ ...deployment, ...deployment.configuration }),
203
+ ];
204
+ const currentWorkspace = (0, terraformCli_1.tf)('workspace show', { provider, version }).trim();
205
+ if (currentWorkspace !== args[0]) {
206
+ // init with reconfigure required here in case we are switching to a different
207
+ // s3 bucket in a different environment for a different deployment
208
+ (0, terraformCli_1.tf)('init', {
209
+ provider,
210
+ backendConfig,
211
+ version,
212
+ options: ['-reconfigure'],
213
+ });
214
+ (0, terraformCli_1.tf)(`workspace select ${args[0]}`, { provider, version });
215
+ }
216
+ (0, terraformCli_1.tf)('plan', {
217
+ provider,
218
+ variables,
219
+ version,
220
+ options: ['-input=false', '-out tfplan'],
221
+ });
222
+ (0, utils_sh_1.cd)('../..');
223
+ };
224
+ this.apply = (args) => {
225
+ const deployment = getDeployment(args);
226
+ const version = deployment.tfVersion || '0.12';
227
+ const backendConfig = this.getTfStateVariables(deployment);
228
+ (0, utils_sh_1.cd)('./infra/aws');
229
+ const provider = this.provider;
230
+ const deploymentName = args[0];
231
+ const currentWorkspace = (0, terraformCli_1.tf)('workspace show', { provider, version }).trim();
232
+ if (currentWorkspace !== deploymentName) {
233
+ // init with reconfigure required here in case we are switching to a different
234
+ // s3 bucket in a different environment for a different deployment
235
+ (0, terraformCli_1.tf)('init', {
236
+ provider,
237
+ backendConfig,
238
+ version,
239
+ options: ['-reconfigure'],
240
+ });
241
+ (0, terraformCli_1.tf)(`workspace select ${deploymentName}`, { provider, version });
242
+ }
243
+ (0, terraformCli_1.tf)('apply', {
244
+ provider,
245
+ options: ['-input=false', 'tfplan'],
246
+ version,
247
+ });
248
+ const res = (0, terraformCli_1.tf)('output', { provider, options: ['-json'], version }).trim();
249
+ const deploymentState = (0, infra_1.readDeploymentState)('./../../', deploymentName, {
250
+ createIfNotExist: true,
251
+ });
252
+ deploymentState.terraform = JSON.parse(res);
253
+ (0, infra_1.writeDeploymentState)('./../../', deploymentState);
254
+ (0, utils_sh_1.cd)('../..');
255
+ };
256
+ this.destroy = (args) => {
257
+ const deployment = getDeployment(args);
258
+ const version = deployment.tfVersion || '0.12';
259
+ const backendConfig = this.getTfStateVariables(deployment);
260
+ (0, utils_sh_1.cd)('./infra/aws');
261
+ const ciConfirmed = args.find((str) => str === '-y');
262
+ if (!ciConfirmed) {
263
+ const value = (0, prompt_sync_1.default)()('Are you sure to destroy your deployed resources? If yes, please type `y` and enter.\n' +
264
+ 'Otherwise, press enter.\nYour Input: ');
265
+ if (value !== 'y') {
266
+ (0, utils_log_1.fatal)('Prompt not confirmed with `y`');
267
+ }
268
+ }
269
+ const provider = this.provider;
270
+ const variables = [
271
+ ...(0, exports.getVariablesFromHCL)({ ...deployment, ...deployment.configuration }),
272
+ ];
273
+ (0, terraformCli_1.tf)('init', { provider, backendConfig, options: ['-reconfigure'], version });
274
+ (0, terraformCli_1.tf)(`workspace select ${args[0]}`, { provider, version });
275
+ (0, terraformCli_1.tf)('plan', {
276
+ provider,
277
+ variables,
278
+ version,
279
+ options: ['-input=false', '-out tfplan'],
280
+ });
281
+ (0, terraformCli_1.tf)('destroy', {
282
+ provider,
283
+ variables,
284
+ version,
285
+ options: ['-input=false', '-auto-approve'],
286
+ });
287
+ (0, utils_sh_1.cd)('../..');
288
+ };
289
+ (0, assert_1.default)(provider);
290
+ this.provider = provider;
291
+ }
292
+ }
293
+ exports.TerraformBuild = TerraformBuild;
362
294
  //# sourceMappingURL=terraformBuild.js.map