Skip to content

Commit

Permalink
Get SSH config from yaml instead of env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
karlhorky committed Mar 13, 2018
1 parent 9afb8f1 commit b7e4e4d
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 195 deletions.
22 changes: 1 addition & 21 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,7 @@ TODO: Make examples

## Development - local nodejs

The `runCli.sh` script injects the necessary environment variables such as `process.env.API_URL`.

```sh
../runCli.sh -- <commands>
```

For example:

```sh
../runCli.sh -- init --overwrite false --project my_project
```
The `execute <cli command>`, `sshlogin` and `sshlogout` yarn scripts can be used to run CLI commands during development.

## Development - inside docker

Expand All @@ -165,13 +155,3 @@ There is already a docker container prepared that has the cli running. Run a new
```sh
docker-compose run --rm cli bash
```

### Old development instructions

The instructions below were how we previously built (before `runCli.sh`), but they will not inject the necessary environment variables (for example, `process.env.API_URL`).

```sh
npm install # Install dependencies
npm run build # Build files to the `dist` folder
node . # Run the CLI
```
4 changes: 4 additions & 0 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
"build": "babel src -d dist",
"watch": "babel --watch=src -d dist",
"execute": "node -r babel-core/register src/cli.js",
"sshlogin":
"node -r babel-core/register src/cli.js login --identity ../local-dev/cli_id_rsa",
"sshlogout":
"node -r babel-core/register src/cli.js logout --identity ../local-dev/cli_id_rsa",
"lint-staged": "lint-staged",
"flow": "glow check"
},
Expand Down
34 changes: 5 additions & 29 deletions cli/src/commands/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,8 @@
import os from 'os';
import path from 'path';
import { green } from 'chalk';
import { utils } from 'ssh2-streams';
import untildify from 'untildify';
import {
getPrivateKeyPath,
getPrivateKeyPassphrase,
sshConnect,
sshExec,
} from '../util/ssh';
import { fileExists, readFile, writeFile } from '../util/fs';
import { sshConnect, sshExec } from '../util/ssh';
import { fileExists, writeFile } from '../util/fs';
import { printErrors } from '../printErrors';

import typeof Yargs from 'yargs';
Expand Down Expand Up @@ -41,36 +34,19 @@ export async function handler({ clog, cerr, argv }: Args): Promise<number> {
return printErrors(cerr, 'File does not exist at identity option path!');
}

const homeDir = os.homedir();
const defaultPrivateKeyPath = path.join(homeDir, '.ssh', 'id_rsa');
const fileExistsAtDefaultPath = await fileExists(defaultPrivateKeyPath);

const privateKeyPath = await getPrivateKeyPath({
fileExistsAtDefaultPath,
defaultPrivateKeyPath,
identity: argv.identity,
cerr,
});

const privateKey = await readFile(untildify(privateKeyPath));
const passphrase = await getPrivateKeyPassphrase(utils.parseKey(privateKey).encryption);

let connection;

try {
connection = await sshConnect({
host: process.env.SSH_HOST || 'auth.amazee.io',
port: Number(process.env.SSH_PORT) || 2020,
username: process.env.SSH_USER || 'lagoon',
privateKey,
passphrase,
identity: argv.identity,
});
} catch (err) {
return printErrors(cerr, err);
}

const output = await sshExec(connection, 'token');
const token = output.toString().replace(/(\r\n|\n|\r)/gm, '');
const tokenFilePath = path.join(homeDir, '.lagoon-token');
const tokenFilePath = path.join(os.homedir(), '.lagoon-token');
await writeFile(tokenFilePath, token);

clog(green('Logged in successfully.'));
Expand Down
41 changes: 11 additions & 30 deletions cli/src/commands/logout.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,8 @@
import os from 'os';
import path from 'path';
import { green } from 'chalk';
import { utils } from 'ssh2-streams';
import untildify from 'untildify';
import {
getPrivateKeyPath,
getPrivateKeyPassphrase,
sshConnect,
sshExec,
} from '../util/ssh';
import { fileExists, readFile, unlink } from '../util/fs';
import { sshConnect, sshExec } from '../util/ssh';
import { fileExists, unlink } from '../util/fs';
import { printErrors } from '../printErrors';

import typeof Yargs from 'yargs';
Expand Down Expand Up @@ -44,30 +37,18 @@ export async function handler({ clog, cerr, argv }: Args): Promise<number> {
return printErrors(cerr, 'File does not exist at identity option path!');
}

const homeDir = os.homedir();
const defaultPrivateKeyPath = path.join(homeDir, '.ssh', 'id_rsa');
const fileExistsAtDefaultPath = await fileExists(defaultPrivateKeyPath);
let connection;

const privateKeyPath = await getPrivateKeyPath({
fileExistsAtDefaultPath,
defaultPrivateKeyPath,
identity: argv.identity,
cerr,
});

const privateKey = await readFile(untildify(privateKeyPath));
const passphrase = await getPrivateKeyPassphrase(utils.parseKey(privateKey).encryption);

const connection = await sshConnect({
host: process.env.SSH_HOST || 'auth.amazee.io',
port: Number(process.env.SSH_PORT) || 2020,
username: process.env.SSH_USER || 'api',
privateKey,
passphrase,
});
try {
connection = await sshConnect({
identity: argv.identity,
});
} catch (err) {
return printErrors(cerr, err);
}

await sshExec(connection, 'logout');
const tokenFilePath = path.join(homeDir, '.lagoon-token');
const tokenFilePath = path.join(os.homedir(), '.lagoon-token');
if (await fileExists(tokenFilePath)) {
await unlink(tokenFilePath);
}
Expand Down
8 changes: 6 additions & 2 deletions cli/src/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ QLQueryArgs): Object {

const {
hostname, path: pathname, port: urlPort, protocol,
} = url.parse(apiUrl);
} = url.parse(
apiUrl,
);

if (hostname == null) {
throw new Error('API URL configured under the "api" key in .lagoon.yml doesn\'t contain a valid hostname.');
throw new Error(
'API URL configured under the "api" key in .lagoon.yml doesn\'t contain a valid hostname.',
);
}

const body = JSON.stringify(
Expand Down
Loading

0 comments on commit b7e4e4d

Please sign in to comment.