@agilecustoms/envctl 0.24.0 → 0.26.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.
@@ -16,17 +16,17 @@ export class CliHelper {
16
16
  }]);
17
17
  return answer;
18
18
  }
19
- ensureEnv(env) {
20
- if (env) {
21
- return env;
19
+ ensureEnv(key) {
20
+ if (key) {
21
+ return key;
22
22
  }
23
23
  const owner = this.configService.getOwner();
24
24
  if (!owner) {
25
- throw new KnownException('--env argument is not provided\n'
25
+ throw new KnownException('--key argument is not provided\n'
26
26
  + 'default to owner from local configuration, but it is not configured\n'
27
- + 'please call with --env argument or run \'envctl configure\' to configure owner');
27
+ + 'please call with --key argument or run \'envctl configure\' to configure owner');
28
28
  }
29
- console.log(`Env name not provided, default to owner name ${owner}`);
29
+ console.log(`Key is not provided, default to owner name ${owner}`);
30
30
  return owner;
31
31
  }
32
32
  ensureOwner(owner) {
@@ -22,54 +22,10 @@ export class TerraformAdapter {
22
22
  });
23
23
  console.log(`\nTime EST: ${est}, UTC: ${utc}\n`);
24
24
  }
25
- async init(key, cwd, upgrade = false) {
26
- let accAlias = '';
27
- const accAliasScanner = (line) => {
28
- accAlias = line;
29
- };
30
- await this.processRunner.runScript('get-acc-alias.sh', [], cwd, accAliasScanner);
31
- console.log('Run terraform init to download providers, this doesn\'t create any resources in AWS even S3 object');
32
- let emptyDir = false;
33
- const scanner = (line) => {
34
- if (line.includes('Terraform initialized in an empty directory!')) {
35
- emptyDir = true;
36
- }
37
- };
38
- const bucket = `agilecustoms-${accAlias}-tf-state`;
39
- const args = [
40
- 'init',
41
- '-reconfigure',
42
- `-backend-config=bucket=${bucket}`,
43
- `-backend-config=key=${key}`
44
- ];
45
- if (upgrade) {
46
- args.push('-upgrade');
47
- }
48
- this.printTime();
49
- try {
50
- await this.processRunner.run('terraform', args, cwd, scanner);
51
- this.printTime();
52
- }
53
- catch (error) {
54
- if (error instanceof ProcessException) {
55
- if (!upgrade && error.message.includes('Failed to query available provider packages')) {
56
- args.push('-upgrade');
57
- await this.processRunner.run('terraform', args, cwd);
58
- this.printTime();
59
- return;
60
- }
61
- throw new KnownException(`terraform init failed with code ${error.code}:\n${error.message}`, { cause: error });
62
- }
63
- throw error;
64
- }
65
- if (emptyDir) {
66
- throw new KnownException('Can not find terraform files. Command needs to be run in a directory with terraform files');
67
- }
68
- }
69
25
  tfArgs(env, cwd) {
70
26
  const varDefs = this.getVarDefs(cwd);
71
27
  const argVars = this.getArgVars(env.args);
72
- const specialFields = { ...env, env: env.envName };
28
+ const specialFields = { ...env, env: env.key };
73
29
  const extraArgs = [];
74
30
  for (const name of ['env', 'owner', 'ephemeral']) {
75
31
  if (varDefs[name] && !argVars[name]) {
@@ -1,7 +1,7 @@
1
1
  export const _keys = {
2
2
  CWD: 'Working directory (default: current directory)',
3
- ENV: 'Environment name (can be git hash). = key used to store env state (s3 key in case of AWS)',
4
3
  FORCE: 'Force deletion without confirmation',
4
+ KEY: 'Environment name/identifier - taken from remote stake key, must be unique for a given customer',
5
5
  KIND: 'Environment kind: complete project (default) or some slice such as tt-core + tt-web',
6
6
  OWNER: 'Environment owner (GH username)'
7
7
  };
@@ -6,12 +6,12 @@ export function createEphemeral(program) {
6
6
  program
7
7
  .command('create-ephemeral')
8
8
  .description('Create bare env w/o resources. Used in CI as first step just to pre-generate token for extension')
9
- .requiredOption('--env <env>', _keys.ENV)
9
+ .requiredOption('--key <key>', _keys.KEY)
10
10
  .option('--owner <owner>', _keys.OWNER)
11
11
  .action(wrap(handler));
12
12
  }
13
13
  async function handler(options) {
14
- const { env, owner } = options;
15
- const token = await envCtl.createEphemeral(env, owner);
14
+ const { key, owner } = options;
15
+ const token = await envCtl.createEphemeral(key, owner);
16
16
  console.log(token);
17
17
  }
@@ -6,13 +6,13 @@ export function deleteIt(program) {
6
6
  program
7
7
  .command('delete')
8
8
  .description('Delete a development environment')
9
- .option('--env <env>', _keys.ENV)
9
+ .option('--key <key>', _keys.KEY)
10
10
  .option('--force', _keys.FORCE)
11
11
  .option('--cwd <cwd>', _keys.CWD)
12
12
  .action(wrap(handler));
13
13
  }
14
14
  async function handler(options) {
15
- const { env, force, cwd } = options;
16
- const envName = cliHelper.ensureEnv(env);
17
- await envCtl.delete(envName, Boolean(force), cwd);
15
+ const { key, force, cwd } = options;
16
+ const theKey = cliHelper.ensureEnv(key);
17
+ await envCtl.delete(theKey, Boolean(force), cwd);
18
18
  }
@@ -6,7 +6,7 @@ export function deploy(program) {
6
6
  program
7
7
  .command('deploy')
8
8
  .description('Create new or update existing dev environment')
9
- .option('--env <env>', _keys.ENV)
9
+ .option('--key <key>', _keys.KEY)
10
10
  .option('--owner <owner>', _keys.OWNER)
11
11
  .option('--kind <kind>', _keys.KIND)
12
12
  .option('--cwd <cwd>', _keys.CWD)
@@ -16,7 +16,6 @@ export function deploy(program) {
16
16
  }
17
17
  async function handler(tfArgs, options) {
18
18
  await awsCredsHelper.ensureCredentials();
19
- const { cwd, env, ...envDto } = options;
20
- envDto.envName = env;
19
+ const { cwd, ...envDto } = options;
21
20
  await envCtl.deploy(envDto, tfArgs, cwd);
22
21
  }
@@ -8,7 +8,7 @@ export function destroy(program) {
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
10
  + ' Main use case - test deletion process, basically that you have enough permissions to delete resources')
11
- .option('--env <env>', _keys.ENV)
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,7 @@ export function destroy(program) {
16
16
  .action(wrap(handler));
17
17
  }
18
18
  async function handler(tfArgs, options) {
19
- const { env, force, cwd } = options;
20
- const envName = cliHelper.ensureEnv(env);
21
- await envCtl.destroy(envName, tfArgs, Boolean(force), cwd);
19
+ const { key, force, cwd } = options;
20
+ const theKey = cliHelper.ensureEnv(key);
21
+ await envCtl.destroy(theKey, tfArgs, Boolean(force), cwd);
22
22
  }
@@ -3,6 +3,5 @@ export { createEphemeral } from './createEphemeral.js';
3
3
  export { deleteIt } from './delete.js';
4
4
  export { deploy } from './deploy.js';
5
5
  export { destroy } from './destroy.js';
6
- export { init } from './init.js';
7
6
  export { plan } from './plan.js';
8
7
  export { status } from './status.js';
@@ -7,7 +7,7 @@ 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('--env <env>', _keys.ENV)
10
+ .option('--key <key>', _keys.KEY)
11
11
  .option('--owner <owner>', _keys.OWNER)
12
12
  .option('--cwd <cwd>', _keys.CWD)
13
13
  .allowUnknownOption(true)
@@ -16,7 +16,6 @@ export function plan(program) {
16
16
  }
17
17
  async function handler(tfArgs, options) {
18
18
  await awsCredsHelper.ensureCredentials();
19
- const { cwd, env, ...envDto } = options;
20
- envDto.envName = env;
19
+ const { cwd, ...envDto } = options;
21
20
  await envCtl.plan(envDto, tfArgs, cwd);
22
21
  }
@@ -6,12 +6,12 @@ export function status(program) {
6
6
  program
7
7
  .command('status')
8
8
  .description('Get env status')
9
- .option('--env <env>', _keys.ENV)
9
+ .option('--key <key>', _keys.KEY)
10
10
  .option('--cwd <cwd>', _keys.CWD)
11
11
  .action(wrap(handler));
12
12
  }
13
13
  async function handler(options) {
14
- const { env, cwd } = options;
15
- const envName = cliHelper.ensureEnv(env);
16
- await envCtl.status(envName, cwd);
14
+ const { key, cwd } = options;
15
+ const theKey = cliHelper.ensureEnv(key);
16
+ await envCtl.status(theKey, cwd);
17
17
  }
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
  import { createRequire } from 'module';
3
3
  import { Command } from 'commander';
4
4
  import updateNotifier from 'update-notifier';
5
- import { configure, createEphemeral, deleteIt, deploy, destroy, init, plan, status } from './commands/index.js';
5
+ import { configure, createEphemeral, deleteIt, deploy, destroy, plan, status } from './commands/index.js';
6
6
  const require = createRequire(import.meta.url);
7
7
  const pkg = require('../package.json');
8
8
  updateNotifier({ pkg, updateCheckInterval: 0 }).notify();
@@ -16,7 +16,6 @@ createEphemeral(program);
16
16
  deleteIt(program);
17
17
  deploy(program);
18
18
  destroy(program);
19
- init(program);
20
19
  plan(program);
21
20
  status(program);
22
21
  program.parse(process.argv);
@@ -9,11 +9,7 @@ export class EnvCtl {
9
9
  this.envApi = envApi;
10
10
  this.terraformAdapter = terraformAdapter;
11
11
  }
12
- key(env) {
13
- return env;
14
- }
15
- async status(envName, cwd) {
16
- const key = this.key(envName);
12
+ async status(key, cwd) {
17
13
  console.log(`Retrieve env ${key}`);
18
14
  const env = await this.envApi.get(key);
19
15
  if (env === null) {
@@ -27,14 +23,13 @@ export class EnvCtl {
27
23
  }
28
24
  }
29
25
  async tryGetEnv(input) {
30
- let envName = input.envName;
26
+ let key = input.key;
31
27
  let owner = input.owner;
32
- if (!envName) {
28
+ if (!key) {
33
29
  owner = this.cliHelper.ensureOwner(owner);
34
30
  console.log(`Env name not provided, default to owner name ${owner}`);
35
- envName = owner;
31
+ key = owner;
36
32
  }
37
- const key = this.key(envName);
38
33
  console.log(`Check if env ${key} already exists`);
39
34
  const env = await this.envApi.get(key);
40
35
  if (env) {
@@ -43,7 +38,7 @@ export class EnvCtl {
43
38
  else {
44
39
  console.log(`Env ${key} does not exist`);
45
40
  }
46
- return { envName, anOwner: owner, key, env };
41
+ return { anOwner: owner, key, env };
47
42
  }
48
43
  checkInput(env, input, cwd) {
49
44
  if (input.owner && env.owner !== input.owner) {
@@ -63,15 +58,11 @@ export class EnvCtl {
63
58
  }
64
59
  throw new KnownException(`Env ${env.key} status is ${env.status}, can not run this command`);
65
60
  }
66
- async init(envName, cwd) {
67
- const key = this.key(envName);
68
- await this.terraformAdapter.init(key, cwd, true);
69
- }
70
61
  async plan(input, tfArgs, cwd) {
71
- const { envName, anOwner, key, env } = await this.tryGetEnv(input);
62
+ const { anOwner, key, env } = await this.tryGetEnv(input);
72
63
  if (env == null) {
73
64
  const owner = this.cliHelper.ensureOwner(anOwner);
74
- await this.runPlan(key, envName, owner, tfArgs, undefined, cwd);
65
+ await this.runPlan(key, owner, tfArgs, undefined, cwd);
75
66
  return;
76
67
  }
77
68
  this.checkInput(env, input, cwd);
@@ -81,15 +72,13 @@ export class EnvCtl {
81
72
  if (env.status !== EnvStatus.Active) {
82
73
  throw new KnownException(`Env ${env.key} status is ${env.status}, can not run plan`);
83
74
  }
84
- await this.runPlan(key, envName, owner, tfArgs, env.vars, cwd);
75
+ await this.runPlan(key, owner, tfArgs, env.vars, cwd);
85
76
  }
86
- async runPlan(key, envName, owner, args, vars, cwd) {
87
- const envTerraform = { envName, ephemeral: false, owner, args, vars };
88
- await this.terraformAdapter.init(key, cwd);
77
+ async runPlan(key, owner, args, vars, cwd) {
78
+ const envTerraform = { key, ephemeral: false, owner, args, vars };
89
79
  await this.terraformAdapter.plan(envTerraform, cwd);
90
80
  }
91
- async createEphemeral(envName, owner) {
92
- const key = this.key(envName);
81
+ async createEphemeral(key, owner) {
93
82
  const env = await this.envApi.get(key);
94
83
  if (env !== null) {
95
84
  throw new KnownException(`Env ${key} already exists`);
@@ -98,14 +87,14 @@ export class EnvCtl {
98
87
  return await this.envApi.createEphemeral(createEnv);
99
88
  }
100
89
  async deploy(input, tfArgs, cwd) {
101
- const { envName, anOwner, key, env } = await this.tryGetEnv(input);
90
+ const { anOwner, key, env } = await this.tryGetEnv(input);
102
91
  if (env === null) {
103
92
  const owner = this.cliHelper.ensureOwner(anOwner);
104
93
  const kind = this.cliHelper.ensureKind(input.kind, cwd);
105
94
  console.log('Creating env tracking record in DynamoDB');
106
95
  const createEnv = { key, owner, kind };
107
96
  const newEnv = await this.envApi.create(createEnv);
108
- return await this.runDeploy(newEnv, envName, tfArgs, cwd);
97
+ return await this.runDeploy(newEnv, key, tfArgs, cwd);
109
98
  }
110
99
  this.checkInput(env, input, cwd);
111
100
  this.checkStatus(env);
@@ -124,13 +113,12 @@ export class EnvCtl {
124
113
  console.log('Env status is ACTIVE\nWill lock for update and run terraform apply (to update resources)');
125
114
  await this.envApi.lockForUpdate(env);
126
115
  }
127
- await this.runDeploy(env, envName, tfArgs, cwd);
116
+ await this.runDeploy(env, key, tfArgs, cwd);
128
117
  }
129
- async runDeploy(env, envName, tfArgs, cwd) {
130
- await this.terraformAdapter.init(env.key, cwd);
118
+ async runDeploy(env, key, tfArgs, cwd) {
131
119
  console.log('Deploying resources');
132
120
  const { ephemeral, vars } = env;
133
- const envTerraform = { envName, ephemeral, owner: env.owner || 'system', args: tfArgs, vars };
121
+ const envTerraform = { key, ephemeral, owner: env.owner || 'system', args: tfArgs, vars };
134
122
  const onDemandVars = {};
135
123
  try {
136
124
  await this.terraformAdapter.apply(envTerraform, onDemandVars, cwd);
@@ -146,8 +134,8 @@ export class EnvCtl {
146
134
  console.log('Activating env (to finish creation)');
147
135
  await this.envApi.activate(env);
148
136
  }
149
- async delete(envName, force, cwd) {
150
- const env = await this.get(envName);
137
+ async delete(key, force, cwd) {
138
+ const env = await this.get(key);
151
139
  if (!force) {
152
140
  this.checkStatus(env);
153
141
  if (env.status === EnvStatus.Init) {
@@ -174,8 +162,8 @@ export class EnvCtl {
174
162
  const message = await this.envApi.delete(env);
175
163
  console.log(message);
176
164
  }
177
- async destroy(envName, tfArgs, force, cwd) {
178
- const env = await this.get(envName);
165
+ async destroy(key, tfArgs, force, cwd) {
166
+ const env = await this.get(key);
179
167
  this.checkStatus(env);
180
168
  if (env.status === EnvStatus.Init) {
181
169
  console.log(`Env ${env.key} has no resources, nothing to destroy, call 'delete' command instead`);
@@ -196,9 +184,8 @@ export class EnvCtl {
196
184
  await this.envApi.lockForUpdate(env);
197
185
  }
198
186
  const { ephemeral, owner, vars } = env;
199
- const envTerraform = { envName, ephemeral, owner: owner || 'system', args: tfArgs, vars };
187
+ const envTerraform = { key, ephemeral, owner: owner || 'system', args: tfArgs, vars };
200
188
  console.log('Destroying env resources');
201
- await this.terraformAdapter.init(env.key, cwd);
202
189
  await this.terraformAdapter.destroy(envTerraform, force, cwd);
203
190
  console.log('Unlock env');
204
191
  await this.envApi.activate(env);
@@ -206,8 +193,7 @@ export class EnvCtl {
206
193
  await this.envApi.delete(env);
207
194
  console.log('Please wait for ~15 sec before you can create env with same name');
208
195
  }
209
- async get(envName) {
210
- const key = this.key(envName);
196
+ async get(key) {
211
197
  console.log(`Retrieve env ${key}`);
212
198
  const env = await this.envApi.get(key);
213
199
  if (!env) {
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.24.0",
4
+ "version": "0.26.0",
5
5
  "author": "Alex Chekulaev",
6
6
  "type": "module",
7
7
  "bin": {
@@ -29,16 +29,16 @@
29
29
  "run-version": " tsc --sourceMap true && npm run run -- --version",
30
30
  "run-configure": "tsc --sourceMap true && npm run run -- configure",
31
31
  "~": "",
32
+ "run-init": "cd ../tt-core && terraform init -backend-config=key=laxa1986 -upgrade",
32
33
  "run-status": " tsc --sourceMap true && npm run run -- status --cwd ../tt-core",
33
- "run-init": " tsc --sourceMap true && npm run run -- init --cwd ../tt-core",
34
34
  "run-plan": " tsc --sourceMap true && npm run run -- plan --cwd ../tt-core",
35
35
  "run-deploy": " tsc --sourceMap true && npm run run -- deploy --cwd ../tt-core -var=\"env_size=min\"",
36
36
  "run-delete": " tsc --sourceMap true && npm run run -- delete --cwd ../tt-core",
37
37
  "run-destroy": "tsc --sourceMap true && npm run run -- destroy --cwd ../tt-core",
38
38
  "-": "",
39
+ "run-ephemeral-init": "cd ../tt-gitops && terraform init -backend-config=key=test",
39
40
  "run-ephemeral-create": " tsc --sourceMap true && npm run run -- create-ephemeral --env test",
40
41
  "run-ephemeral-status": " tsc --sourceMap true && npm run run -- status --env test",
41
- "run-ephemeral-init": " tsc --sourceMap true && npm run run -- init --env test --cwd ../tt-gitops",
42
42
  "run-ephemeral-deploy": " tsc --sourceMap true && npm run run -- deploy --env test --cwd ../tt-gitops -var-file=versions.tfvars",
43
43
  "run-ephemeral-delete": " tsc --sourceMap true && npm run run -- delete --env test --cwd ../tt-gitops --force",
44
44
  "run-ephemeral-destroy": "tsc --sourceMap true && npm run run -- destroy --env test --cwd ../tt-gitops --force -var-file=versions.tfvars"
@@ -1,17 +0,0 @@
1
- import { Command } from 'commander';
2
- import { cliHelper, envCtl } from '../container.js';
3
- import { _keys } from './_keys.js';
4
- import { wrap } from './_utils.js';
5
- export function init(program) {
6
- program
7
- .command('init')
8
- .description('Terraform init wrapper')
9
- .option('--env <env>', _keys.ENV)
10
- .option('--cwd <cwd>', _keys.CWD)
11
- .action(wrap(handler));
12
- }
13
- async function handler(options) {
14
- const { env, cwd } = options;
15
- const envName = cliHelper.ensureEnv(env);
16
- await envCtl.init(envName, cwd);
17
- }