Skip to content

Commit 314897f

Browse files
committed
feat: initial commit
1 parent 45c851e commit 314897f

File tree

5 files changed

+261
-33
lines changed

5 files changed

+261
-33
lines changed

.eslintrc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
22
"extends": ["@jedwards1211/eslint-config", "eslint-config-prettier"],
3+
"parserOptions": {
4+
"ecmaVersion": 2018
5+
},
36
"env": {
4-
"commonjs": true
7+
"node": true,
8+
"es6": true
59
}
610
}

README.md

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,42 @@
1-
# untranspiled-js-library-skeleton
1+
# @jcoreio/ec2-ssh
22

3-
[![CircleCI](https://circleci.com/gh/jedwards1211/untranspiled-js-library-skeleton.svg?style=svg)](https://circleci.com/gh/jedwards1211/untranspiled-js-library-skeleton)
4-
[![Coverage Status](https://codecov.io/gh/jedwards1211/untranspiled-js-library-skeleton/branch/master/graph/badge.svg)](https://codecov.io/gh/jedwards1211/untranspiled-js-library-skeleton)
3+
[![CircleCI](https://circleci.com/gh/jcoreio/ec2-ssh.svg?style=svg)](https://circleci.com/gh/jcoreio/ec2-ssh)
54
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
65
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
7-
[![npm version](https://badge.fury.io/js/untranspiled-js-library-skeleton.svg)](https://badge.fury.io/js/untranspiled-js-library-skeleton)
6+
[![npm version](https://badge.fury.io/js/%40jcoreio%2Fec2-ssh.svg)](https://badge.fury.io/js/%40jcoreio%2Fec2-ssh)
87

9-
This is my personal skeleton for creating an untranspiled JS library npm package. You are welcome to use it.
8+
```
9+
npm i -g ec2-ssh
10+
# Or
11+
npx ec2-ssh
12+
```
13+
14+
Use at your own risk. Prompts you to select an EC2 instance, then SSHes into that instance, attempting to select the correct username automatically, and using the internal DNS hostname, and the identity file at `~/.ssh/<KeyName>.pem` if it exists, where `KeyName` is the property of the EC2 instance in the `aws-sdk` response.
15+
16+
If you're not storing your identity files in that manner, you can add the following to your `~/.ssh/config`:
1017

11-
## Quick start
18+
```
19+
Host *.compute.internal
20+
User ec2-user
21+
IdentityFile ~/.ssh/your-identity-file.pem
22+
```
23+
24+
It will set the `AWS_SDK_LOAD_CONFIG` environment variable to load the default region from your `~/.aws/config`.
25+
See https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-region.html for other ways
26+
of setting the default region.
1227

13-
```sh
14-
npx 0-60 clone https://github.com/jedwards1211/untranspiled-js-library-skeleton.git
1528
```
29+
> ec2-ssh
30+
? Select an EC2 Instance (region: us-west-2) ›
31+
i-00000000000000000 foo (recent)
32+
i-00000000000000001 bar
33+
❯ i-00000000000000002 baz
34+
i-00000000000000003 qux
35+
ssh -t ubuntu@ip-192-168-0-1.us-west-2.compute.internal
36+
Welcome to Ubuntu 18.04.1 LTS (GNU/Linux 4.15.0-1021-aws x86_64)
1637
17-
## Tools used
18-
19-
- mocha
20-
- chai
21-
- istanbul
22-
- nyc
23-
- eslint
24-
- eslint-watch
25-
- flow
26-
- flow-watch
27-
- husky
28-
- semantic-release
29-
- Travis CI
30-
- Coveralls
38+
* Documentation: https://help.ubuntu.com
39+
* Management: https://landscape.canonical.com
40+
* Support: https://ubuntu.com/advantage
41+
...
42+
```

index.js

100644100755
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env node
2+
3+
'use strict'
4+
5+
if (process.env.AWS_SDK_LOAD_CONFIG == null)
6+
process.env.AWS_SDK_LOAD_CONFIG = '1'
7+
8+
const AWS = require('aws-sdk')
9+
const { selectEC2Instance } = require('@jcoreio/aws-select-cli-prompts')
10+
const { spawn } = require('child_process')
11+
const os = require('os')
12+
const fs = require('fs')
13+
const path = require('path')
14+
const { promisify } = require('util')
15+
16+
async function getAmiName(ec2, ImageId) {
17+
const { Images } = await ec2.describeImages({ ImageIds: [ImageId] }).promise()
18+
const name = Images && Images[0] && Images[0].Name
19+
if (!name) throw new Error(`failed to get name for image: ${ImageId}`)
20+
return name
21+
}
22+
23+
function getUser(ami) {
24+
if (/ubuntu|nanostack/i.test(ami)) return 'ubuntu'
25+
if (/debian/i.test(ami)) return 'admin'
26+
if (/fedora/i.test(ami)) return 'fedora'
27+
if (/centos/i.test(ami)) return 'centos'
28+
if (/bitnami/i.test(ami)) return 'bitnami'
29+
if (/turnkey|omni/i.test(ami)) return 'root'
30+
return 'ec2-user'
31+
}
32+
33+
function signalCode(signal) {
34+
switch (signal) {
35+
case 'SIGABRT':
36+
return 6
37+
case 'SIGHUP':
38+
return 1
39+
case 'SIGILL':
40+
return 4
41+
case 'SIGINT':
42+
return 2
43+
case 'SIGKILL':
44+
return 9
45+
case 'SIGPIPE':
46+
return 13
47+
case 'SIGQUIT':
48+
return 3
49+
case 'SIGSEGV':
50+
return 11
51+
case 'SIGTERM':
52+
return 15
53+
case 'SIGTRAP':
54+
return 5
55+
}
56+
return 0
57+
}
58+
59+
async function ec2ssh({ ec2 = new AWS.EC2() } = {}) {
60+
const instance = await selectEC2Instance({ ec2 })
61+
62+
const { ImageId, KeyName, PrivateDnsName, PublicDnsName } = instance
63+
let host = PrivateDnsName || PublicDnsName
64+
if (!host)
65+
throw new Error(`instance doesn't have a PrivateDnsName or PublicDnsName`)
66+
67+
const user = ImageId ? getUser(await getAmiName(ec2, ImageId)) : null
68+
69+
if (user) host = `${user}@${host}`
70+
71+
const args = ['-t']
72+
if (KeyName) {
73+
const identityFile = path.join(os.homedir(), '.ssh', `${KeyName}.pem`)
74+
try {
75+
await promisify(fs.stat)(identityFile)
76+
args.push('-i', identityFile)
77+
} catch (error) {
78+
// ignore
79+
}
80+
}
81+
82+
args.push(host, ...process.argv.slice(2))
83+
84+
// eslint-disable-next-line no-console
85+
console.log('ssh', ...args)
86+
87+
const child = spawn('ssh', args, { stdio: 'inherit' })
88+
89+
return await new Promise((resolve, reject) => {
90+
child.once('error', reject)
91+
child.once('close', (code, signal) => {
92+
resolve({ code, signal })
93+
if (typeof code === 'number') process.exit(code)
94+
process.exit(signalCode(signal) + 127)
95+
})
96+
})
97+
}
98+
99+
if (require.main === module) {
100+
ec2ssh().then(
101+
({ code, signal }) => {
102+
if (typeof code === 'number') process.exit(code)
103+
process.exit(signalCode(signal) + 127)
104+
},
105+
error => {
106+
// eslint-disable-next-line no-console
107+
console.error(error.message)
108+
process.exit(1)
109+
}
110+
)
111+
}

package.json

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
{
2-
"name": "untranspiled-js-library-skeleton",
2+
"name": "@jcoreio/ec2-ssh",
33
"version": "0.0.0-development",
4-
"description": "my skeleton for transpiler-free JS packages",
4+
"description": "select an EC2 instance in your account from a prompt, then SSH into it",
55
"main": "index.js",
66
"author": "Andy Edwards",
77
"license": "MIT",
8+
"engines": {
9+
"node": ">=12"
10+
},
11+
"bin": {
12+
"ec2-ssh": "./index.js"
13+
},
814
"scripts": {
915
"lint": "eslint *.js test --cache",
1016
"lint:fix": "eslint --fix *.js test --cache",
@@ -14,7 +20,7 @@
1420
"test:watch": "cross-env NODE_ENV=test mocha --watch $npm_package_config_mocha",
1521
"test:debug": "cross-env NODE_ENV=test mocha --inspect-brk $npm_package_config_mocha",
1622
"codecov": "nyc report --reporter=text-lcov > coverage.lcov; codecov",
17-
"prepublishOnly": "npm run lint && npm test",
23+
"prepublishOnly": "npm run lint",
1824
"open:coverage": "open coverage/lcov-report/index.html",
1925
"semantic-release": "semantic-release"
2026
},
@@ -56,15 +62,18 @@
5662
},
5763
"repository": {
5864
"type": "git",
59-
"url": "https://github.com/jedwards1211/untranspiled-js-library-skeleton.git"
65+
"url": "https://github.com/jcoreio/ec2-ssh.git"
6066
},
6167
"keywords": [
62-
"skeleton"
68+
"aws",
69+
"ec2",
70+
"ssh",
71+
"cli"
6372
],
6473
"bugs": {
65-
"url": "https://github.com/jedwards1211/untranspiled-js-library-skeleton/issues"
74+
"url": "https://github.com/jcoreio/ec2-ssh/issues"
6675
},
67-
"homepage": "https://github.com/jedwards1211/untranspiled-js-library-skeleton#readme",
76+
"homepage": "https://github.com/jcoreio/ec2-ssh#readme",
6877
"devDependencies": {
6978
"@commitlint/cli": "^11.0.0",
7079
"@commitlint/config-conventional": "^11.0.0",
@@ -84,5 +93,7 @@
8493
"prettier-eslint": "^8.8.2",
8594
"semantic-release": "^17.1.2"
8695
},
87-
"dependencies": {}
96+
"dependencies": {
97+
"aws-sdk": "^2.829.0"
98+
}
8899
}

0 commit comments

Comments
 (0)