Skip to content

Commit

Permalink
Add aws login command do CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
pziemkowski committed Sep 6, 2023
1 parent a7aa840 commit f169674
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 62 deletions.
28 changes: 0 additions & 28 deletions Makefile.base.mk
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,7 @@ export HOST_UID
# As such, default shell of the user has to be get the other way.
USER_SHELL=$(shell env | grep '^SHELL=' | cut -d '=' -f 2)

up:
pnpm nx run --output-style=stream core:docker-compose:up

down:
pnpm nx run --output-style=stream core:docker-compose:down

serve:
pnpm nx run --output-style=stream core:serve

clean:
# remove created images
@docker-compose -p down --remove-orphans --rmi all 2>/dev/null \
&& echo 'Image(s) removed.' \
|| echo 'Image(s) already removed.'

prune:
# clean all that is not actively used
docker system prune -af


set-env:
ifeq ($(ENV_STAGE), local)
$(error Please set ENV_STAGE to other value than "local")
endif
ifeq (, $(shell which aws-vault))
$(USER_SHELL)
else
aws-vault exec $(AWS_VAULT_PROFILE) -- $(USER_SHELL)
endif

aws-login:
aws-vault login $(AWS_VAULT_PROFILE)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Command } from '@oclif/core';

import { getConfigStorage, getEnvStageKey } from '../config/storage';
import { initConfig } from '../config/init';
import { initConfig } from '../../config/init';

export default class GetEnv extends Command {
static description = 'Get currently selected ENV stage';

static examples = [`$ saas get-env`];
static examples = [`$ saas aws get-env`];

async run(): Promise<void> {
const { envStage } = await initConfig(this, {});
Expand Down
18 changes: 18 additions & 0 deletions packages/internal/cli/src/commands/aws/login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Command } from '@oclif/core';

import { initConfig } from '../../config/init';
import { runCommand } from '../../lib/runCommand';
import { assertAwsVaultInstalled } from '../../lib/awsVault';

export default class GetEnv extends Command {
static description = 'Get currently selected ENV stage';

static examples = [`$ saas aws login`];

async run(): Promise<void> {
await initConfig(this, { requireAws: true });
await assertAwsVaultInstalled();

await runCommand('aws-vault', ['login']);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Args, Command } from '@oclif/core';

import { setEnvStage } from '../config/storage';
import { initConfig } from '../config/init';
import { setEnvStage } from '../../config/storage';
import { initConfig } from '../../config/init';

export default class SetEnv extends Command {
static description = 'Select ENV stage';

static examples = [`$ saas set-env qa`];
static examples = [`$ saas aws set-env qa`];

static args = {
envStage: Args.string({
Expand Down
18 changes: 18 additions & 0 deletions packages/internal/cli/src/commands/backend/down.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Command } from '@oclif/core';

import { initConfig } from '../../config/init';
import { runCommand } from '../../lib/runCommand';
import { assertDockerIsRunning } from '../../lib/docker';

export default class BackendUp extends Command {
static description = 'Stops all backend services';

static examples = [`$ saas backend down`];

async run(): Promise<void> {
await initConfig(this, { requireLocalEnvStage: true });
await assertDockerIsRunning();

await runCommand('pnpm', ['nx', 'run', 'core:docker-compose:down']);
}
}
12 changes: 4 additions & 8 deletions packages/internal/cli/src/config/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { lookpath } from 'lookpath';

import { validateStageEnv } from './env';
import { color } from '@oclif/color';
import { isAwsVaultInstalled } from '../lib/awsVault';
import { assertChamberInstalled, isChamberInstalled } from '../lib/chamber';

const exec = promisify(childProcess.exec);

Expand All @@ -21,12 +23,7 @@ async function loadStageEnv(
envStage: string,
shouldValidate = true
) {
const chamberExists = await lookpath('chamber');
if (!chamberExists) {
context.error(
'chamber executable missing. Make sure it is installed in the system and re-run the command.'
);
}
await assertChamberInstalled();

let chamberOutput;
try {
Expand Down Expand Up @@ -56,8 +53,7 @@ export const initAWS = async (
context: Command,
options: LoadAWSCredentialsOptions
) => {
const awsVaultExists = await lookpath('aws-vault');
if (awsVaultExists) {
if (await isAwsVaultInstalled()) {
const awsVaultProfile = process.env.AWS_VAULT_PROFILE;

const { stdout } = await exec(`aws-vault export ${awsVaultProfile}`);
Expand Down
4 changes: 2 additions & 2 deletions packages/internal/cli/src/config/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const initConfig = async (
if (requireLocalEnvStage && envStage !== ENV_STAGE_LOCAL) {
context.error(
`This command should only be run on a local environment stage.
Please call \`saas set-env local\` first or open a new terminal.`
Please call \`saas aws set-env local\` first or open a new terminal.`
);
}

Expand All @@ -56,7 +56,7 @@ Please call \`saas set-env local\` first or open a new terminal.`
if (envStage === ENV_STAGE_LOCAL) {
context.error(
`Remote environment stage required.\nPlease call \`${color.green(
'saas set-env [stage-name]'
'saas aws set-env [stage-name]'
)}\` first. Do not use \`local\` value.`
);
}
Expand Down
18 changes: 18 additions & 0 deletions packages/internal/cli/src/lib/awsVault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { CLIError } from '@oclif/errors';
import * as childProcess from 'child_process';
import { promisify } from 'util';
import { lookpath } from 'lookpath';

const exec = promisify(childProcess.exec);

export const isAwsVaultInstalled = async () => {
return await lookpath('aws-vault');
};

export const assertAwsVaultInstalled = async () => {
const isInstalled = await isAwsVaultInstalled();
if (!isInstalled) {
throw new CLIError(`aws-vault executable not found. Make sure it is installed in the system and re-run the
command. Go to https://github.com/99designs/aws-vault and learn how to install and configure it.`);
}
};
18 changes: 18 additions & 0 deletions packages/internal/cli/src/lib/chamber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { CLIError } from '@oclif/errors';
import * as childProcess from 'child_process';
import { promisify } from 'util';
import { lookpath } from 'lookpath';

const exec = promisify(childProcess.exec);

export const isChamberInstalled = async () => {
return await lookpath('chamber');
};

export const assertChamberInstalled = async () => {
const isInstalled = await isChamberInstalled();
if (!isInstalled) {
throw new CLIError(`chamber executable not found. Make sure it is installed in the system and re-run the
command. Go to https://github.com/segmentio/chamber and learn how to install and configure it.`);
}
};
17 changes: 0 additions & 17 deletions packages/internal/core/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,12 @@
"commands": ["docker-compose down"]
}
},
"serve": {
"executor": "nx:run-commands",
"options": {
"color": true,
"commands": ["nx run core:docker-compose:up", "nx run webapp:start"],
"parallel": false
}
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["packages/internal/core/**/*.ts"]
}
},
"set-env": {
"executor": "nx:run-commands",
"options": {
"color": true,
"commands": ["node ./scripts/set-env.js {args.env}"],
"cwd": "packages/internal/core",
"parallel": false
}
}
},
"tags": []
Expand Down

0 comments on commit f169674

Please sign in to comment.