Skip to content

Commit 2c8ac7b

Browse files
author
Sandesh Grangdan
committed
fix: added SSM support also
1 parent 5ef1a52 commit 2c8ac7b

7 files changed

Lines changed: 84 additions & 32 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "e2s"
3-
version = "0.1.2"
3+
version = "0.1.3"
44
edition = "2021"
55

66
# Github Repo

src/app.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clap::Parser;
33
use rand::Rng;
44

55
use crate::app::{
6-
aws::ec2::Ec2Client,
6+
aws::ec2::{ConnectMode, Ec2Client},
77
input::{ssh_keys::SshKeys, ssh_user::SshUsers},
88
};
99
use input::user_input::{
@@ -49,7 +49,8 @@ pub struct App {
4949
pub ec2_client: Ec2Client,
5050
pub ssh_keys: SshKeys,
5151
pub ssh_user: SshUsers,
52-
pub private: bool,
52+
pub connect_mode: ConnectMode,
53+
5354
}
5455

5556
// ANCHOR: application_impl
@@ -70,7 +71,7 @@ impl App {
7071
ec2_client: Ec2Client::None,
7172
ssh_keys: SshKeys::load(),
7273
ssh_user: SshUsers::load(),
73-
private: true,
74+
connect_mode: ConnectMode::Public,
7475
}
7576
}
7677

src/app/aws/ec2.rs

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,31 @@ impl Data {
4545
}
4646
}
4747

48+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
49+
pub enum ConnectMode {
50+
Public,
51+
Private,
52+
Ssm,
53+
}
54+
55+
impl ConnectMode {
56+
pub fn next(self) -> Self {
57+
match self {
58+
ConnectMode::Public => ConnectMode::Private,
59+
ConnectMode::Private => ConnectMode::Ssm,
60+
ConnectMode::Ssm => ConnectMode::Public,
61+
}
62+
}
63+
64+
pub fn toggle(&mut self) {
65+
*self = match *self {
66+
ConnectMode::Public => ConnectMode::Private,
67+
ConnectMode::Private => ConnectMode::Ssm,
68+
ConnectMode::Ssm => ConnectMode::Public,
69+
};
70+
}
71+
}
72+
4873
fn generate_instance_id() -> String {
4974
let hex: String = (0..17)
5075
.map(|_| format!("{:x}", rand::random::<u8>() % 16))
@@ -253,30 +278,52 @@ impl App {
253278
pub async fn ssh(&mut self) -> io::Result<()> {
254279
if let Some(selected) = self.state.selected() {
255280
if let Some(item) = self.display_items.get(selected) {
256-
let key_path = match &self.ssh_keys.selected_key {
257-
Some(key) => key,
258-
None => {
259-
println!("No SSH key selected.");
260-
return Ok(());
281+
let mut cmd = match self.connect_mode {
282+
ConnectMode::Public | ConnectMode::Private => {
283+
let key_path = match &self.ssh_keys.selected_key {
284+
Some(key) => key,
285+
None => {
286+
eprintln!("No SSH key selected.");
287+
return Ok(());
288+
}
289+
};
290+
let user = self
291+
.ssh_user
292+
.selected_user
293+
.as_deref()
294+
.unwrap_or("ec2-user");
295+
let ip = match self.connect_mode {
296+
ConnectMode::Public => &item.public_ipv4,
297+
ConnectMode::Private => &item.private_ipv4,
298+
_ => unreachable!(),
299+
};
300+
let mut ssh_cmd = Command::new("ssh");
301+
ssh_cmd.args(["-i", key_path]);
302+
ssh_cmd.arg(format!("{}@{}", user, ip));
303+
ssh_cmd
261304
}
262-
};
305+
ConnectMode::Ssm => {
306+
let mut ssm_cmd = Command::new("aws");
307+
ssm_cmd.args([
308+
"ssm",
309+
"start-session",
310+
"--target",
311+
&item.instance_id,
312+
]);
263313

264-
let user = self.ssh_user.selected_user.as_deref().unwrap_or("ec2-user");
265-
let ip = if self.private {
266-
&item.private_ipv4
267-
} else {
268-
&item.public_ipv4
314+
if self.args.region != *"None" {
315+
ssm_cmd.args(["--region", &self.args.region]);
316+
}
317+
318+
if self.args.profile != *"None" {
319+
ssm_cmd.args(["--profile", &self.args.profile]);
320+
}
321+
ssm_cmd
322+
}
269323
};
270324

271-
let status = Command::new("ssh")
272-
.arg("-i")
273-
.arg(key_path)
274-
.arg(format!("{}@{}", user, ip))
275-
.stdin(std::process::Stdio::inherit())
276-
.stdout(std::process::Stdio::inherit())
277-
.stderr(std::process::Stdio::inherit())
278-
.status()?;
279-
325+
let status = cmd.status()?;
326+
280327
if !status.success() {
281328
eprintln!("Failed to launch SSH session.");
282329
}

src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ use update::update;
2626
// ANCHOR: main
2727
#[tokio::main]
2828
async fn main() -> Result<()> {
29-
use std::time::Instant;
29+
// use std::time::Instant;
3030
// Create an application.
3131
let mut app = App::new(app::Args::parse());
3232
app.set_ec2_client().await;
3333

34-
let start = Instant::now();
34+
// let start = Instant::now();
3535
app.fetch_ec2_data().await;
36-
let duration = start.elapsed();
37-
println!("fetch_ec2_data took: {:?}", duration);
36+
// let duration = start.elapsed();
37+
// println!("fetch_ec2_data took: {:?}", duration);
3838

3939
// Initialize the terminal user interface.
4040
let terminal = ratatui::init();

src/ui/ui_block/header.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ratatui::{
44
widgets::{Block, BorderType, Borders, Padding, Paragraph, Wrap},
55
};
66

7-
use crate::app::App;
7+
use crate::app::{App, aws::ec2::ConnectMode};
88

99
pub fn render(app: &mut App, f: &mut Frame, layout: Rect) {
1010
let title_block = Block::default()
@@ -13,7 +13,11 @@ pub fn render(app: &mut App, f: &mut Frame, layout: Rect) {
1313
.padding(Padding::new(2, 1, 0, 0))
1414
.border_type(BorderType::Plain);
1515

16-
let ssh_from_private = if app.private { "Private" } else { "Public" };
16+
let ssh_from_private = match app.connect_mode {
17+
ConnectMode::Private => "Private",
18+
ConnectMode::Public => "Public",
19+
ConnectMode::Ssm => "SSM",
20+
};
1721

1822
let selected_ssh_key = if Some(app.ssh_keys.selected_key.clone().unwrap_or_default()) != None {
1923
app.ssh_keys.selected_key.clone().unwrap_or_default()

src/update.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub async fn update(app: &mut App, key_event: KeyEvent, tui: &mut Tui) {
4747
app.ssh_user.previous();
4848
}
4949
KeyCode::Char('p') => {
50-
app.private = !app.private;
50+
app.connect_mode.toggle();
5151
}
5252
KeyCode::Char('?') => {
5353
app.show_help = !app.show_help;

0 commit comments

Comments
 (0)