Skip to content

Commit

Permalink
feat: show instance states and launch dates, only show pending/runnin…
Browse files Browse the repository at this point in the history
…g instances
  • Loading branch information
jedwards1211 committed Mar 22, 2022
1 parent d0c17f7 commit 731de41
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
35 changes: 30 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ async function getAmiName(ec2, ImageId) {
if (!name) throw new Error(`failed to get name for image: ${ImageId}`)
return name
}

function getUser(ami) {
if (/ubuntu|nanostack/i.test(ami)) return 'ubuntu'
if (/debian/i.test(ami)) return 'admin'
Expand Down Expand Up @@ -56,15 +55,41 @@ function signalCode(signal) {
return 0
}

async function ec2ssh({ ec2 = new AWS.EC2() } = {}) {
const instance = await selectEC2Instance({ ec2 })
async function ec2ssh({ ec2 = new AWS.EC2(), ssm = new AWS.SSM() } = {}) {
const instance = await selectEC2Instance({
ec2,
Filters: [
{
Name: 'instance-state-name',
Values: ['pending', 'running'],
},
],
})

const { ImageId, KeyName, PrivateDnsName, PublicDnsName } = instance
const {
InstanceId,
ImageId,
KeyName,
PrivateDnsName,
PublicDnsName,
} = instance
let host = PrivateDnsName || PublicDnsName
if (!host)
throw new Error(`instance doesn't have a PrivateDnsName or PublicDnsName`)

const user = ImageId ? getUser(await getAmiName(ec2, ImageId)) : null
let user = ImageId ? getUser(await getAmiName(ec2, ImageId)) : null

if (InstanceId) {
const { InstanceInformationList: [{ PlatformName } = {}] = [] } = await ssm
.describeInstanceInformation({
Filters: [{ Key: 'InstanceIds', Values: [InstanceId] }],
})
.promise()
if (PlatformName) {
const userFromPlatform = getUser(PlatformName)
if (userFromPlatform !== 'ec2-user') user = userFromPlatform
}
}

if (user) host = `${user}@${host}`

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
"semantic-release": "^17.1.2"
},
"dependencies": {
"@jcoreio/aws-select-cli-prompts": "^1.0.1",
"@jcoreio/aws-select-cli-prompts": "^1.1.0",
"aws-sdk": "^2.829.0"
}
}
9 changes: 5 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,14 @@
update-notifier "^2.2.0"
yargs "^8.0.2"

"@jcoreio/aws-select-cli-prompts@^1.0.1":
version "1.0.1"
resolved "https://registry.npmjs.org/@jcoreio/aws-select-cli-prompts/-/aws-select-cli-prompts-1.0.1.tgz#a5b92055e59ba74758b01968c7e17605cf67ff9b"
integrity sha512-ycean9jyEOP6awi0Ay4XmOmp9QNjkUpsu7Cbg/iXI9t6JE+wPA7+9ze+ZyyCN6ic1y5iFLefbKWCwqGi/IAHGg==
"@jcoreio/aws-select-cli-prompts@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@jcoreio/aws-select-cli-prompts/-/aws-select-cli-prompts-1.1.0.tgz#43a70afd021335977275ea2c1d60a0bf5f0f6deb"
integrity sha512-iSxhElIPfUTFK0QS72HQlgeN+OeHW5eTrFCwt8/0YNsjj5lFv+gPGKbUbyNa5XGj0dh3cVOGp+tAlG8WEpRMeA==
dependencies:
"@babel/runtime" "^7.1.5"
async-autocomplete-cli "^1.0.0"
chalk "^4.0.0"
fs-extra "^9.1.0"

"@jedwards1211/commitlint-config@^1.0.1":
Expand Down

0 comments on commit 731de41

Please sign in to comment.