@agilecustoms/envctl 0.26.0 → 0.27.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.
package/README.md CHANGED
@@ -7,8 +7,8 @@
7
7
 
8
8
  ```shell
9
9
  terraform init -backend-config=key=laxa1986
10
- # TODO: remove --env in next minor release
11
- envctl deploy --env laxa1986 -var-file=versions.tfvars -var="log_level=debug"
10
+ envctl deploy -var-file=versions.tfvars -var="log_level=debug"
11
+ envctl delete
12
12
  ```
13
13
 
14
14
  ## setup/update
@@ -1,12 +1,7 @@
1
1
  import path from 'path';
2
2
  import inquirer from 'inquirer';
3
- import { KnownException } from '../exceptions.js';
4
- import { ConfigService } from '../service/index.js';
5
3
  export class CliHelper {
6
- configService;
7
- constructor(configService) {
8
- this.configService = configService;
9
- }
4
+ constructor() { }
10
5
  async promptYesNo(message, defaultValue = false) {
11
6
  const { answer } = await inquirer.prompt([{
12
7
  type: 'confirm',
@@ -16,34 +11,8 @@ export class CliHelper {
16
11
  }]);
17
12
  return answer;
18
13
  }
19
- ensureEnv(key) {
20
- if (key) {
21
- return key;
22
- }
23
- const owner = this.configService.getOwner();
24
- if (!owner) {
25
- throw new KnownException('--key argument is not provided\n'
26
- + 'default to owner from local configuration, but it is not configured\n'
27
- + 'please call with --key argument or run \'envctl configure\' to configure owner');
28
- }
29
- console.log(`Key is not provided, default to owner name ${owner}`);
30
- return owner;
31
- }
32
- ensureOwner(owner) {
33
- if (!owner) {
34
- owner = this.configService.getOwner();
35
- if (!owner) {
36
- throw new KnownException('when called without --owner option, first call \'envctl configure\'');
37
- }
38
- console.log(`Owner not provided, default to configured owner ${owner}`);
39
- }
40
- return owner;
41
- }
42
- ensureKind(kind, cwd) {
43
- if (kind) {
44
- return kind;
45
- }
46
- kind = getDirName(cwd);
14
+ getKind(cwd) {
15
+ const kind = getDirName(cwd);
47
16
  console.log(`Inferred kind from directory name: ${kind}`);
48
17
  return kind;
49
18
  }
@@ -1,14 +1,7 @@
1
1
  import { spawn } from 'child_process';
2
- import path from 'path';
3
2
  import * as readline from 'readline';
4
- import { fileURLToPath } from 'url';
5
3
  import { ProcessException } from '../exceptions.js';
6
4
  export class ProcessRunner {
7
- async runScript(script, args = [], cwd, scanner) {
8
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
9
- const scriptPath = path.join(__dirname, `../../scripts/${script}`);
10
- await this.run(scriptPath, args, cwd, scanner);
11
- }
12
5
  async run(command, args, cwd, out_scanner, in_scanner) {
13
6
  const spawnOptions = {
14
7
  stdio: ['pipe', 'pipe', 'pipe'],
@@ -14,6 +14,38 @@ export class TerraformAdapter {
14
14
  this.processRunner = processRunner;
15
15
  this.cliHelper = cliHelper;
16
16
  }
17
+ getKey(cwd) {
18
+ const dir = cwd ?? process.cwd();
19
+ const statePath = path.join(dir, '.terraform', 'terraform.tfstate');
20
+ if (!fs.existsSync(statePath)) {
21
+ throw new KnownException(`Terraform state file not found at: ${statePath}`);
22
+ }
23
+ let content;
24
+ try {
25
+ content = fs.readFileSync(statePath, 'utf8');
26
+ }
27
+ catch (err) {
28
+ throw new KnownException(`Failed to read terraform state file: ${statePath}`, { cause: err });
29
+ }
30
+ let tfstate;
31
+ try {
32
+ tfstate = JSON.parse(content);
33
+ }
34
+ catch (err) {
35
+ throw new KnownException(`Failed to parse terraform state file: ${statePath}`, { cause: err });
36
+ }
37
+ const backend = tfstate.backend;
38
+ const type = backend?.type;
39
+ if (type !== 's3') {
40
+ throw new KnownException(`Unsupported terraform backend type: ${type}. Only 's3' is supported for key inference`);
41
+ }
42
+ const config = backend?.config;
43
+ const key = config?.key;
44
+ if (!key) {
45
+ throw new KnownException(`Terraform backend config does not contain 'key' attribute`);
46
+ }
47
+ return key;
48
+ }
17
49
  printTime() {
18
50
  const now = new Date();
19
51
  const utc = now.toISOString();
@@ -1,5 +1,5 @@
1
1
  import { Command } from 'commander';
2
- import { cliHelper, envCtl } from '../container.js';
2
+ import { envCtl } from '../container.js';
3
3
  import { _keys } from './_keys.js';
4
4
  import { wrap } from './_utils.js';
5
5
  export function deleteIt(program) {
@@ -13,6 +13,5 @@ export function deleteIt(program) {
13
13
  }
14
14
  async function handler(options) {
15
15
  const { key, force, cwd } = options;
16
- const theKey = cliHelper.ensureEnv(key);
17
- await envCtl.delete(theKey, Boolean(force), cwd);
16
+ await envCtl.delete(Boolean(force), key, cwd);
18
17
  }
@@ -6,9 +6,6 @@ export function deploy(program) {
6
6
  program
7
7
  .command('deploy')
8
8
  .description('Create new or update existing dev environment')
9
- .option('--key <key>', _keys.KEY)
10
- .option('--owner <owner>', _keys.OWNER)
11
- .option('--kind <kind>', _keys.KIND)
12
9
  .option('--cwd <cwd>', _keys.CWD)
13
10
  .allowUnknownOption(true)
14
11
  .argument('[args...]')
@@ -16,6 +13,6 @@ export function deploy(program) {
16
13
  }
17
14
  async function handler(tfArgs, options) {
18
15
  await awsCredsHelper.ensureCredentials();
19
- const { cwd, ...envDto } = options;
20
- await envCtl.deploy(envDto, tfArgs, cwd);
16
+ const { cwd } = options;
17
+ await envCtl.deploy(tfArgs, cwd);
21
18
  }
@@ -1,5 +1,5 @@
1
1
  import { Command } from 'commander';
2
- import { cliHelper, envCtl } from '../container.js';
2
+ import { envCtl } from '../container.js';
3
3
  import { _keys } from './_keys.js';
4
4
  import { wrap } from './_utils.js';
5
5
  export function destroy(program) {
@@ -7,8 +7,8 @@ export function destroy(program) {
7
7
  .command('destroy')
8
8
  .description('Destroy environment resources. This is thin wrapper for terraform destroy.'
9
9
  + ' Unlike "delete" command (which just schedule deletion) this command deletes resources synchronously.'
10
+ + ' Unlike "delete" command, this command doesn\'t have --key option, it can only delete env after key you provided during terraform init.'
10
11
  + ' Main use case - test deletion process, basically that you have enough permissions to delete resources')
11
- .option('--key <key>', _keys.KEY)
12
12
  .option('--force', _keys.FORCE)
13
13
  .option('--cwd <cwd>', _keys.CWD)
14
14
  .allowUnknownOption(true)
@@ -16,7 +16,6 @@ export function destroy(program) {
16
16
  .action(wrap(handler));
17
17
  }
18
18
  async function handler(tfArgs, options) {
19
- const { key, force, cwd } = options;
20
- const theKey = cliHelper.ensureEnv(key);
21
- await envCtl.destroy(theKey, tfArgs, Boolean(force), cwd);
19
+ const { force, cwd } = options;
20
+ await envCtl.destroy(tfArgs, Boolean(force), cwd);
22
21
  }
@@ -7,8 +7,6 @@ export function plan(program) {
7
7
  .command('plan')
8
8
  .description('High level wrapper for terraform plan. Compliments deploy command: if you plan to deploy env with envctl deploy,'
9
9
  + ' then it is recommended to do plan with envctl plan to guarantee consistent behavior')
10
- .option('--key <key>', _keys.KEY)
11
- .option('--owner <owner>', _keys.OWNER)
12
10
  .option('--cwd <cwd>', _keys.CWD)
13
11
  .allowUnknownOption(true)
14
12
  .argument('[args...]')
@@ -16,6 +14,6 @@ export function plan(program) {
16
14
  }
17
15
  async function handler(tfArgs, options) {
18
16
  await awsCredsHelper.ensureCredentials();
19
- const { cwd, ...envDto } = options;
20
- await envCtl.plan(envDto, tfArgs, cwd);
17
+ const { cwd } = options;
18
+ await envCtl.plan(tfArgs, cwd);
21
19
  }
@@ -1,5 +1,5 @@
1
1
  import { Command } from 'commander';
2
- import { cliHelper, envCtl } from '../container.js';
2
+ import { envCtl } from '../container.js';
3
3
  import { _keys } from './_keys.js';
4
4
  import { wrap } from './_utils.js';
5
5
  export function status(program) {
@@ -12,6 +12,5 @@ export function status(program) {
12
12
  }
13
13
  async function handler(options) {
14
14
  const { key, cwd } = options;
15
- const theKey = cliHelper.ensureEnv(key);
16
- await envCtl.status(theKey, cwd);
15
+ await envCtl.status(key, cwd);
17
16
  }
package/dist/container.js CHANGED
@@ -2,12 +2,12 @@ import { AwsCredsHelper } from './client/AwsCredsHelper.js';
2
2
  import { CliHelper, EnvApiClient, HttpClient, TerraformAdapter } from './client/index.js';
3
3
  import { ProcessRunner } from './client/ProcessRunner.js';
4
4
  import { ConfigService, EnvCtl } from './service/index.js';
5
- const configService = new ConfigService();
6
- const cliHelper = new CliHelper(configService);
5
+ const cliHelper = new CliHelper();
7
6
  const awsCredsHelper = new AwsCredsHelper();
8
7
  const httpClient = new HttpClient(awsCredsHelper);
9
8
  const envApiClient = new EnvApiClient(httpClient);
10
9
  const processRunner = new ProcessRunner();
11
10
  const terraformAdapter = new TerraformAdapter(processRunner, cliHelper);
12
- const envCtl = new EnvCtl(cliHelper, envApiClient, terraformAdapter);
11
+ const configService = new ConfigService();
12
+ const envCtl = new EnvCtl(cliHelper, envApiClient, terraformAdapter, configService);
13
13
  export { awsCredsHelper, cliHelper, configService, envCtl };
@@ -1,6 +1,7 @@
1
1
  import * as fs from 'node:fs';
2
2
  import * as os from 'node:os';
3
3
  import path from 'path';
4
+ import { KnownException } from '../exceptions.js';
4
5
  const CONFIG_PATH = path.join(os.homedir(), '.envctl', 'config.json');
5
6
  export class ConfigService {
6
7
  config;
@@ -23,6 +24,10 @@ export class ConfigService {
23
24
  return this.config;
24
25
  }
25
26
  getOwner() {
26
- return this.loadConfig()?.owner;
27
+ const owner = this.loadConfig()?.owner;
28
+ if (!owner) {
29
+ throw new KnownException('please first set owner via calling \'envctl configure\'');
30
+ }
31
+ return owner;
27
32
  }
28
33
  }
@@ -4,32 +4,37 @@ export class EnvCtl {
4
4
  cliHelper;
5
5
  envApi;
6
6
  terraformAdapter;
7
- constructor(cliHelper, envApi, terraformAdapter) {
7
+ configService;
8
+ constructor(cliHelper, envApi, terraformAdapter, configService) {
8
9
  this.cliHelper = cliHelper;
9
10
  this.envApi = envApi;
10
11
  this.terraformAdapter = terraformAdapter;
12
+ this.configService = configService;
13
+ }
14
+ getKey(key, cwd) {
15
+ if (!key) {
16
+ console.log('Key is not provided, inferring from .terraform/terraform.tfstate file');
17
+ key = this.terraformAdapter.getKey(cwd);
18
+ }
19
+ return key;
11
20
  }
12
21
  async status(key, cwd) {
22
+ key = this.getKey(key, cwd);
13
23
  console.log(`Retrieve env ${key}`);
14
24
  const env = await this.envApi.get(key);
15
25
  if (env === null) {
16
26
  console.log(`Env ${key} does not exist`);
17
27
  return;
18
28
  }
19
- const kind = env.kind ? this.cliHelper.ensureKind(undefined, cwd) : undefined;
20
29
  console.log(`Env ${key} status: ${env.status}`);
21
- if (env.kind && env.kind !== kind) {
22
- console.warn(`Env ${key} kind (dir-name): ${env.kind} - looks like this env was deployed from a different directory`);
30
+ if (env.kind) {
31
+ const kind = this.cliHelper.getKind(cwd);
32
+ if (env.kind !== kind) {
33
+ console.warn(`Env ${key} kind (dir-name): ${env.kind} - looks like this env was deployed from a different directory`);
34
+ }
23
35
  }
24
36
  }
25
- async tryGetEnv(input) {
26
- let key = input.key;
27
- let owner = input.owner;
28
- if (!key) {
29
- owner = this.cliHelper.ensureOwner(owner);
30
- console.log(`Env name not provided, default to owner name ${owner}`);
31
- key = owner;
32
- }
37
+ async tryGetEnv(key) {
33
38
  console.log(`Check if env ${key} already exists`);
34
39
  const env = await this.envApi.get(key);
35
40
  if (env) {
@@ -38,15 +43,17 @@ export class EnvCtl {
38
43
  else {
39
44
  console.log(`Env ${key} does not exist`);
40
45
  }
41
- return { anOwner: owner, key, env };
46
+ return env;
42
47
  }
43
- checkInput(env, input, cwd) {
44
- if (input.owner && env.owner !== input.owner) {
45
- throw new KnownException(`Can not change env owner ${env.owner} -> ${input.owner}`);
46
- }
47
- const kind = this.cliHelper.ensureKind(input.kind, cwd);
48
- if ((input.kind || env.kind) && kind !== env.kind) {
49
- throw new KnownException(`Can not change env kind ${env.kind} -> ${kind}`);
48
+ checkInput(env, owner, cwd) {
49
+ if (env.owner !== owner) {
50
+ throw new KnownException(`Can not change env owner ${env.owner} -> ${owner}`);
51
+ }
52
+ if (env.kind) {
53
+ const kind = this.cliHelper.getKind(cwd);
54
+ if (kind !== env.kind) {
55
+ throw new KnownException(`Can not change env kind ${env.kind} -> ${kind}`);
56
+ }
50
57
  }
51
58
  }
52
59
  checkStatus(env, availableStatuses = [EnvStatus.Init, EnvStatus.Deploying, EnvStatus.Active, EnvStatus.Updating]) {
@@ -58,15 +65,16 @@ export class EnvCtl {
58
65
  }
59
66
  throw new KnownException(`Env ${env.key} status is ${env.status}, can not run this command`);
60
67
  }
61
- async plan(input, tfArgs, cwd) {
62
- const { anOwner, key, env } = await this.tryGetEnv(input);
68
+ async plan(tfArgs, cwd) {
69
+ const key = this.terraformAdapter.getKey(cwd);
70
+ const owner = this.configService.getOwner();
71
+ const env = await this.tryGetEnv(key);
63
72
  if (env == null) {
64
- const owner = this.cliHelper.ensureOwner(anOwner);
65
- await this.runPlan(key, owner, tfArgs, undefined, cwd);
73
+ const vars = undefined;
74
+ await this.runPlan(key, owner, tfArgs, vars, cwd);
66
75
  return;
67
76
  }
68
- this.checkInput(env, input, cwd);
69
- const owner = env.owner;
77
+ this.checkInput(env, owner, cwd);
70
78
  this.checkStatus(env);
71
79
  await this.promptUnlock(env);
72
80
  if (env.status !== EnvStatus.Active) {
@@ -86,17 +94,18 @@ export class EnvCtl {
86
94
  const createEnv = { key, owner };
87
95
  return await this.envApi.createEphemeral(createEnv);
88
96
  }
89
- async deploy(input, tfArgs, cwd) {
90
- const { anOwner, key, env } = await this.tryGetEnv(input);
97
+ async deploy(tfArgs, cwd) {
98
+ const key = this.terraformAdapter.getKey(cwd);
99
+ const owner = this.configService.getOwner();
100
+ const env = await this.tryGetEnv(key);
91
101
  if (env === null) {
92
- const owner = this.cliHelper.ensureOwner(anOwner);
93
- const kind = this.cliHelper.ensureKind(input.kind, cwd);
94
- console.log('Creating env tracking record in DynamoDB');
102
+ const kind = this.cliHelper.getKind(cwd);
103
+ console.log('Creating env metadata');
95
104
  const createEnv = { key, owner, kind };
96
105
  const newEnv = await this.envApi.create(createEnv);
97
106
  return await this.runDeploy(newEnv, key, tfArgs, cwd);
98
107
  }
99
- this.checkInput(env, input, cwd);
108
+ this.checkInput(env, owner, cwd);
100
109
  this.checkStatus(env);
101
110
  if (env.status === EnvStatus.Init) {
102
111
  await this.envApi.lockForUpdate(env);
@@ -134,7 +143,8 @@ export class EnvCtl {
134
143
  console.log('Activating env (to finish creation)');
135
144
  await this.envApi.activate(env);
136
145
  }
137
- async delete(key, force, cwd) {
146
+ async delete(force, key, cwd) {
147
+ key = this.getKey(key, cwd);
138
148
  const env = await this.get(key);
139
149
  if (!force) {
140
150
  this.checkStatus(env);
@@ -143,7 +153,7 @@ export class EnvCtl {
143
153
  console.log(`Env ${env.key} is deleted`);
144
154
  return;
145
155
  }
146
- const kind = this.cliHelper.ensureKind(undefined, cwd);
156
+ const kind = this.cliHelper.getKind(cwd);
147
157
  if (env.kind && env.kind !== kind) {
148
158
  const answerYes = await this.cliHelper.promptYesNo(`Env ${env.key} kind (dir-name): ${env.kind}\n`
149
159
  + 'You\'re deleting env deployed from different dir\n'
@@ -162,7 +172,8 @@ export class EnvCtl {
162
172
  const message = await this.envApi.delete(env);
163
173
  console.log(message);
164
174
  }
165
- async destroy(key, tfArgs, force, cwd) {
175
+ async destroy(tfArgs, force, cwd) {
176
+ const key = this.terraformAdapter.getKey(cwd);
166
177
  const env = await this.get(key);
167
178
  this.checkStatus(env);
168
179
  if (env.status === EnvStatus.Init) {
@@ -170,7 +181,7 @@ export class EnvCtl {
170
181
  return;
171
182
  }
172
183
  if (!force) {
173
- const kind = this.cliHelper.ensureKind(undefined, cwd);
184
+ const kind = this.cliHelper.getKind(cwd);
174
185
  if (env.kind && env.kind !== kind) {
175
186
  throw new KnownException(`Env ${env.key} kind (dir-name): ${env.kind} - make sure you run destroy from the same directory`);
176
187
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@agilecustoms/envctl",
3
3
  "description": "node.js CLI client for manage environments",
4
- "version": "0.26.0",
4
+ "version": "0.27.0",
5
5
  "author": "Alex Chekulaev",
6
6
  "type": "module",
7
7
  "bin": {
@@ -37,11 +37,11 @@
37
37
  "run-destroy": "tsc --sourceMap true && npm run run -- destroy --cwd ../tt-core",
38
38
  "-": "",
39
39
  "run-ephemeral-init": "cd ../tt-gitops && terraform init -backend-config=key=test",
40
- "run-ephemeral-create": " tsc --sourceMap true && npm run run -- create-ephemeral --env test",
41
- "run-ephemeral-status": " tsc --sourceMap true && npm run run -- status --env test",
42
- "run-ephemeral-deploy": " tsc --sourceMap true && npm run run -- deploy --env test --cwd ../tt-gitops -var-file=versions.tfvars",
43
- "run-ephemeral-delete": " tsc --sourceMap true && npm run run -- delete --env test --cwd ../tt-gitops --force",
44
- "run-ephemeral-destroy": "tsc --sourceMap true && npm run run -- destroy --env test --cwd ../tt-gitops --force -var-file=versions.tfvars"
40
+ "run-ephemeral-create": " tsc --sourceMap true && npm run run -- create-ephemeral",
41
+ "run-ephemeral-status": " tsc --sourceMap true && npm run run -- status --key test",
42
+ "run-ephemeral-deploy": " tsc --sourceMap true && npm run run -- deploy --cwd ../tt-gitops -var-file=versions.tfvars",
43
+ "run-ephemeral-delete": " tsc --sourceMap true && npm run run -- delete --cwd ../tt-gitops --force",
44
+ "run-ephemeral-destroy": "tsc --sourceMap true && npm run run -- destroy --cwd ../tt-gitops --force -var-file=versions.tfvars"
45
45
  },
46
46
  "dependencies": {
47
47
  "@aws-sdk/client-sts": "^3.716.0",
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
-
4
- # TF state is stored in S3 in format: {company}-{acc-alias}-tf-state/{key}
5
- # (for non ephemeral environments the {key} typically has form of {project-code}-{env-name} like tt-dev)
6
- # Retrieve AWS account information
7
- acc_id=$(aws sts get-caller-identity --query "Account" --output text)
8
- aws organizations list-tags-for-resource --resource-id "$acc_id" --query "Tags[?Key=='Alias'].Value" --output text