Skip to content

Commit 75d1d0e

Browse files
Merge pull request #11 from cxcloud/preset-env-secrets
Run with preset AWS environment secrets
2 parents 25388d4 + f457ffe commit 75d1d0e

File tree

5 files changed

+34
-21
lines changed

5 files changed

+34
-21
lines changed

lib/utils/aws.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
const execa = require('execa');
21
const path = require('path');
32
const awsRegions = require('aws-regions');
4-
const { getBinPath } = require('./env');
3+
const { getBinPath, runCommandWithAWSCredentials } = require('./env');
54
const { logOperation } = require('./');
65

76
function runAWSCommand(cmd, args) {
8-
return execa(getBinPath('awsudo'), [
9-
'-u',
10-
process.env.AWS_PROFILE,
11-
getBinPath('aws'),
7+
return runCommandWithAWSCredentials('aws', [
128
cmd,
139
...args
1410
]);

lib/utils/env.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ const chalk = require('chalk');
66
const { runCommand, logOperation, showError } = require('./');
77
const { deploymentManifestSchema } = require('./schemas');
88
const { checkForUpdates } = require('./update');
9+
const execa = require('execa');
910

1011
const getBinPath = bin => path.join(__dirname, '../../.env/bin', bin);
1112

13+
const envAWSKeysExist = function() {
14+
const { AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY } = process.env;
15+
return AWS_ACCESS_KEY_ID && AWS_SECRET_ACCESS_KEY;
16+
}
17+
1218
const installEnvironment = async () => {
1319
if (await fs.pathExists(path.join(__dirname, '../../.env'))) {
1420
logOperation('Environment already exists, skipping installation.');
@@ -23,6 +29,10 @@ const installEnvironment = async () => {
2329
};
2430

2531
const installRequirements = async () => {
32+
if (envAWSKeysExist()) {
33+
logOperation('AWS API key environment variables is set, `awsudo` not needed.');
34+
return;
35+
}
2636
if (await fs.pathExists(getBinPath('awsudo'))) {
2737
logOperation('`awsudo` exists, skipping installation.');
2838
return;
@@ -37,9 +47,9 @@ const installRequirements = async () => {
3747
};
3848

3949
exports.checkEnvironment = async () => {
40-
if (!process.env.AWS_PROFILE) {
50+
if (!process.env.AWS_PROFILE && !envAWSKeysExist()) {
4151
return showError(
42-
'You have to set `AWS_PROFILE` environment variable before continuing. Visit https://docs.cxcloud.com for help.'
52+
'Some environment variables has to be set before running cxcloud. Visit https://docs.cxcloud.com for help.'
4353
);
4454
}
4555

@@ -48,6 +58,21 @@ exports.checkEnvironment = async () => {
4858
await installRequirements();
4959
};
5060

61+
exports.runCommandWithAWSCredentials = (cmd, args) => {
62+
if (envAWSKeysExist()) {
63+
return execa(getBinPath(cmd), [
64+
...args
65+
]);
66+
} else {
67+
return execa(getBinPath('awsudo'), [
68+
'-u',
69+
process.env.AWS_PROFILE,
70+
getBinPath(cmd),
71+
...args
72+
]);
73+
}
74+
}
75+
5176
exports.getBinPath = getBinPath;
5277

5378
exports.ensureDeployEnvironment = async () => {

lib/utils/kops.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
const execa = require('execa');
21
const ora = require('ora');
32
const fs = require('fs-extra');
43
const path = require('path');
5-
const { getBinPath } = require('./env');
4+
const { getBinPath, runCommandWithAWSCredentials } = require('./env');
65
const { logOperation, sleep } = require('./');
76

87
function runKopsCommand(cmd, args) {
9-
return execa(getBinPath('awsudo'), [
10-
'-u',
11-
process.env.AWS_PROFILE,
12-
'kops',
8+
return runCommandWithAWSCredentials('kops', [
139
cmd,
1410
...args
1511
]);

lib/utils/terraform.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
const path = require('path');
2-
const execa = require('execa');
3-
const { getBinPath } = require('./env');
2+
const { getBinPath, runCommandWithAWSCredentials } = require('./env');
43
const { copyTpl } = require('./fs');
54
const { logOperation } = require('./');
65

76
function runTerraformCommand(cmd, args = []) {
8-
const child = execa(getBinPath('awsudo'), [
9-
'-u',
10-
process.env.AWS_PROFILE,
11-
'terraform',
7+
child = runCommandWithAWSCredentials('terraform', [
128
cmd,
139
...args
1410
]);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cxcloud",
3-
"version": "0.9.0",
3+
"version": "0.10.0",
44
"description": "CXCloud command line tools",
55
"license": "MIT",
66
"repository": "cxcloud/cxcloud-cli",

0 commit comments

Comments
 (0)