Support terminals with ssh in remote projects (#11913)

Release Notes:

- Added a way to create terminal tabs in remote projects, if an ssh
connection string is specified
This commit is contained in:
Kirill Bulatov 2024-05-17 17:48:07 +03:00 committed by GitHub
parent 70888cf3d6
commit 8631280baa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 239 additions and 68 deletions

1
Cargo.lock generated
View File

@ -7651,6 +7651,7 @@ dependencies = [
"client", "client",
"clock", "clock",
"collections", "collections",
"dev_server_projects",
"env_logger", "env_logger",
"fs", "fs",
"futures 0.3.28", "futures 0.3.28",

View File

@ -407,6 +407,7 @@ CREATE TABLE dev_servers (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL REFERENCES users(id), user_id INTEGER NOT NULL REFERENCES users(id),
name TEXT NOT NULL, name TEXT NOT NULL,
ssh_connection_string TEXT,
hashed_token TEXT NOT NULL hashed_token TEXT NOT NULL
); );

View File

@ -0,0 +1 @@
ALTER TABLE dev_servers ADD COLUMN ssh_connection_string TEXT;

View File

@ -73,6 +73,7 @@ impl Database {
pub async fn create_dev_server( pub async fn create_dev_server(
&self, &self,
name: &str, name: &str,
ssh_connection_string: Option<&str>,
hashed_access_token: &str, hashed_access_token: &str,
user_id: UserId, user_id: UserId,
) -> crate::Result<(dev_server::Model, proto::DevServerProjectsUpdate)> { ) -> crate::Result<(dev_server::Model, proto::DevServerProjectsUpdate)> {
@ -86,6 +87,9 @@ impl Database {
hashed_token: ActiveValue::Set(hashed_access_token.to_string()), hashed_token: ActiveValue::Set(hashed_access_token.to_string()),
name: ActiveValue::Set(name.trim().to_string()), name: ActiveValue::Set(name.trim().to_string()),
user_id: ActiveValue::Set(user_id), user_id: ActiveValue::Set(user_id),
ssh_connection_string: ActiveValue::Set(
ssh_connection_string.map(ToOwned::to_owned),
),
}) })
.exec_with_returning(&*tx) .exec_with_returning(&*tx)
.await?; .await?;

View File

@ -10,6 +10,7 @@ pub struct Model {
pub name: String, pub name: String,
pub user_id: UserId, pub user_id: UserId,
pub hashed_token: String, pub hashed_token: String,
pub ssh_connection_string: Option<String>,
} }
impl ActiveModelBehavior for ActiveModel {} impl ActiveModelBehavior for ActiveModel {}
@ -32,6 +33,7 @@ impl Model {
dev_server_id: self.id.to_proto(), dev_server_id: self.id.to_proto(),
name: self.name.clone(), name: self.name.clone(),
status: status as i32, status: status as i32,
ssh_connection_string: self.ssh_connection_string.clone(),
} }
} }
} }

View File

@ -2365,7 +2365,12 @@ async fn create_dev_server(
let (dev_server, status) = session let (dev_server, status) = session
.db() .db()
.await .await
.create_dev_server(&request.name, &hashed_access_token, session.user_id()) .create_dev_server(
&request.name,
request.ssh_connection_string.as_deref(),
&hashed_access_token,
session.user_id(),
)
.await?; .await?;
send_dev_server_projects_update(session.user_id(), status, &session).await; send_dev_server_projects_update(session.user_id(), status, &session).await;
@ -2373,7 +2378,7 @@ async fn create_dev_server(
response.send(proto::CreateDevServerResponse { response.send(proto::CreateDevServerResponse {
dev_server_id: dev_server.id.0 as u64, dev_server_id: dev_server.id.0 as u64,
access_token: auth::generate_dev_server_token(dev_server.id.0 as usize, access_token), access_token: auth::generate_dev_server_token(dev_server.id.0 as usize, access_token),
name: request.name.clone(), name: request.name,
})?; })?;
Ok(()) Ok(())
} }

View File

@ -20,7 +20,7 @@ async fn test_dev_server(cx: &mut gpui::TestAppContext, cx2: &mut gpui::TestAppC
let resp = store let resp = store
.update(cx, |store, cx| { .update(cx, |store, cx| {
store.create_dev_server("server-1".to_string(), cx) store.create_dev_server("server-1".to_string(), None, cx)
}) })
.await .await
.unwrap(); .unwrap();
@ -167,7 +167,7 @@ async fn create_dev_server_project(
let resp = store let resp = store
.update(cx, |store, cx| { .update(cx, |store, cx| {
store.create_dev_server("server-1".to_string(), cx) store.create_dev_server("server-1".to_string(), None, cx)
}) })
.await .await
.unwrap(); .unwrap();
@ -521,7 +521,7 @@ async fn test_create_dev_server_project_path_validation(
let resp = store let resp = store
.update(cx1, |store, cx| { .update(cx1, |store, cx| {
store.create_dev_server("server-2".to_string(), cx) store.create_dev_server("server-2".to_string(), None, cx)
}) })
.await .await
.unwrap(); .unwrap();

View File

@ -39,6 +39,7 @@ impl From<proto::DevServerProject> for DevServerProject {
pub struct DevServer { pub struct DevServer {
pub id: DevServerId, pub id: DevServerId,
pub name: SharedString, pub name: SharedString,
pub ssh_connection_string: Option<SharedString>,
pub status: DevServerStatus, pub status: DevServerStatus,
} }
@ -48,6 +49,7 @@ impl From<proto::DevServer> for DevServer {
id: DevServerId(dev_server.dev_server_id), id: DevServerId(dev_server.dev_server_id),
status: dev_server.status(), status: dev_server.status(),
name: dev_server.name.into(), name: dev_server.name.into(),
ssh_connection_string: dev_server.ssh_connection_string.map(|s| s.into()),
} }
} }
} }
@ -164,11 +166,17 @@ impl Store {
pub fn create_dev_server( pub fn create_dev_server(
&mut self, &mut self,
name: String, name: String,
ssh_connection_string: Option<String>,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> Task<Result<proto::CreateDevServerResponse>> { ) -> Task<Result<proto::CreateDevServerResponse>> {
let client = self.client.clone(); let client = self.client.clone();
cx.background_executor().spawn(async move { cx.background_executor().spawn(async move {
let result = client.request(proto::CreateDevServer { name }).await?; let result = client
.request(proto::CreateDevServer {
name,
ssh_connection_string,
})
.await?;
Ok(result) Ok(result)
}) })
} }

View File

@ -30,6 +30,7 @@ async-trait.workspace = true
client.workspace = true client.workspace = true
clock.workspace = true clock.workspace = true
collections.workspace = true collections.workspace = true
dev_server_projects.workspace = true
fs.workspace = true fs.workspace = true
futures.workspace = true futures.workspace = true
fuzzy.workspace = true fuzzy.workspace = true

View File

@ -1,6 +1,8 @@
use crate::Project; use crate::Project;
use collections::HashMap; use collections::HashMap;
use gpui::{AnyWindowHandle, Context, Entity, Model, ModelContext, WeakModel}; use gpui::{
AnyWindowHandle, AppContext, Context, Entity, Model, ModelContext, SharedString, WeakModel,
};
use settings::{Settings, SettingsLocation}; use settings::{Settings, SettingsLocation};
use smol::channel::bounded; use smol::channel::bounded;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -18,7 +20,38 @@ pub struct Terminals {
pub(crate) local_handles: Vec<WeakModel<terminal::Terminal>>, pub(crate) local_handles: Vec<WeakModel<terminal::Terminal>>,
} }
#[derive(Debug, Clone)]
pub struct ConnectRemoteTerminal {
pub ssh_connection_string: SharedString,
pub project_path: SharedString,
}
impl Project { impl Project {
pub fn remote_terminal_connection_data(
&self,
cx: &AppContext,
) -> Option<ConnectRemoteTerminal> {
self.dev_server_project_id()
.and_then(|dev_server_project_id| {
let projects_store = dev_server_projects::Store::global(cx).read(cx);
let project_path = projects_store
.dev_server_project(dev_server_project_id)?
.path
.clone();
let ssh_connection_string = projects_store
.dev_server_for_project(dev_server_project_id)?
.ssh_connection_string
.clone();
Some(project_path).zip(ssh_connection_string)
})
.map(
|(project_path, ssh_connection_string)| ConnectRemoteTerminal {
ssh_connection_string,
project_path,
},
)
}
pub fn create_terminal( pub fn create_terminal(
&mut self, &mut self,
working_directory: Option<PathBuf>, working_directory: Option<PathBuf>,
@ -26,10 +59,15 @@ impl Project {
window: AnyWindowHandle, window: AnyWindowHandle,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> anyhow::Result<Model<Terminal>> { ) -> anyhow::Result<Model<Terminal>> {
anyhow::ensure!( let remote_connection_data = if self.is_remote() {
!self.is_remote(), let remote_connection_data = self.remote_terminal_connection_data(cx);
"creating terminals as a guest is not supported yet" if remote_connection_data.is_none() {
); anyhow::bail!("Cannot create terminal for remote project without connection data")
}
remote_connection_data
} else {
None
};
// used only for TerminalSettings::get // used only for TerminalSettings::get
let worktree = { let worktree = {
@ -48,7 +86,7 @@ impl Project {
path, path,
}); });
let is_terminal = spawn_task.is_none(); let is_terminal = spawn_task.is_none() && remote_connection_data.is_none();
let settings = TerminalSettings::get(settings_location, cx); let settings = TerminalSettings::get(settings_location, cx);
let python_settings = settings.detect_venv.clone(); let python_settings = settings.detect_venv.clone();
let (completion_tx, completion_rx) = bounded(1); let (completion_tx, completion_rx) = bounded(1);
@ -61,7 +99,30 @@ impl Project {
.as_deref() .as_deref()
.unwrap_or_else(|| Path::new("")); .unwrap_or_else(|| Path::new(""));
let (spawn_task, shell) = if let Some(spawn_task) = spawn_task { let (spawn_task, shell) = if let Some(remote_connection_data) = remote_connection_data {
log::debug!("Connecting to a remote server: {remote_connection_data:?}");
// Alacritty sets its terminfo to `alacritty`, this requiring hosts to have it installed
// to properly display colors.
// We do not have the luxury of assuming the host has it installed,
// so we set it to a default that does not break the highlighting via ssh.
env.entry("TERM".to_string())
.or_insert_with(|| "xterm-256color".to_string());
(
None,
Shell::WithArguments {
program: "ssh".to_string(),
args: vec![
remote_connection_data.ssh_connection_string.to_string(),
"-t".to_string(),
format!(
"cd {} && exec $SHELL -l",
escape_path_for_shell(remote_connection_data.project_path.as_ref())
),
],
},
)
} else if let Some(spawn_task) = spawn_task {
log::debug!("Spawning task: {spawn_task:?}"); log::debug!("Spawning task: {spawn_task:?}");
env.extend(spawn_task.env); env.extend(spawn_task.env);
// Activate minimal Python virtual environment // Activate minimal Python virtual environment
@ -231,4 +292,38 @@ impl Project {
} }
} }
#[cfg(unix)]
fn escape_path_for_shell(input: &str) -> String {
input
.chars()
.fold(String::with_capacity(input.len()), |mut s, c| {
match c {
' ' | '"' | '\'' | '\\' | '(' | ')' | '{' | '}' | '[' | ']' | '|' | ';' | '&'
| '<' | '>' | '*' | '?' | '$' | '#' | '!' | '=' | '^' | '%' | ':' => {
s.push('\\');
s.push('\\');
s.push(c);
}
_ => s.push(c),
}
s
})
}
#[cfg(windows)]
fn escape_path_for_shell(input: &str) -> String {
input
.chars()
.fold(String::with_capacity(input.len()), |mut s, c| {
match c {
'^' | '&' | '|' | '<' | '>' | ' ' | '(' | ')' | '@' | '`' | '=' | ';' | '%' => {
s.push('^');
s.push(c);
}
_ => s.push(c),
}
s
})
}
// TODO: Add a few tests for adding and removing terminal tabs // TODO: Add a few tests for adding and removing terminal tabs

View File

@ -16,6 +16,7 @@ use rpc::{
}; };
use settings::Settings; use settings::Settings;
use theme::ThemeSettings; use theme::ThemeSettings;
use ui::CheckboxWithLabel;
use ui::{prelude::*, Indicator, List, ListHeader, ListItem, ModalContent, ModalHeader, Tooltip}; use ui::{prelude::*, Indicator, List, ListHeader, ListItem, ModalContent, ModalHeader, Tooltip};
use ui_text_field::{FieldLabelLayout, TextField}; use ui_text_field::{FieldLabelLayout, TextField};
use util::ResultExt; use util::ResultExt;
@ -30,14 +31,16 @@ pub struct DevServerProjects {
dev_server_store: Model<dev_server_projects::Store>, dev_server_store: Model<dev_server_projects::Store>,
project_path_input: View<Editor>, project_path_input: View<Editor>,
dev_server_name_input: View<TextField>, dev_server_name_input: View<TextField>,
use_server_name_in_ssh: Selection,
rename_dev_server_input: View<TextField>, rename_dev_server_input: View<TextField>,
_subscription: gpui::Subscription, _dev_server_subscription: Subscription,
} }
#[derive(Default, Clone)] #[derive(Default, Clone)]
struct CreateDevServer { struct CreateDevServer {
creating: bool, creating: bool,
dev_server: Option<CreateDevServerResponse>, dev_server: Option<CreateDevServerResponse>,
// ssh_connection_string: Option<String>,
} }
#[derive(Clone)] #[derive(Clone)]
@ -118,7 +121,8 @@ impl DevServerProjects {
project_path_input, project_path_input,
dev_server_name_input, dev_server_name_input,
rename_dev_server_input, rename_dev_server_input,
_subscription: subscription, use_server_name_in_ssh: Selection::Unselected,
_dev_server_subscription: subscription,
} }
} }
@ -232,22 +236,20 @@ impl DevServerProjects {
} }
pub fn create_dev_server(&mut self, cx: &mut ViewContext<Self>) { pub fn create_dev_server(&mut self, cx: &mut ViewContext<Self>) {
let name = self let name = get_text(&self.dev_server_name_input, cx);
.dev_server_name_input if name.is_empty() {
.read(cx)
.editor()
.read(cx)
.text(cx)
.trim()
.to_string();
if name == "" {
return; return;
} }
let dev_server = self let ssh_connection_string = if self.use_server_name_in_ssh == Selection::Selected {
.dev_server_store Some(name.clone())
.update(cx, |store, cx| store.create_dev_server(name.clone(), cx)); } else {
None
};
let dev_server = self.dev_server_store.update(cx, |store, cx| {
store.create_dev_server(name, ssh_connection_string, cx)
});
cx.spawn(|this, mut cx| async move { cx.spawn(|this, mut cx| async move {
let result = dev_server.await; let result = dev_server.await;
@ -277,14 +279,7 @@ impl DevServerProjects {
} }
fn rename_dev_server(&mut self, id: DevServerId, cx: &mut ViewContext<Self>) { fn rename_dev_server(&mut self, id: DevServerId, cx: &mut ViewContext<Self>) {
let name = self let name = get_text(&self.rename_dev_server_input, cx);
.rename_dev_server_input
.read(cx)
.editor()
.read(cx)
.text(cx)
.trim()
.to_string();
let Some(dev_server) = self.dev_server_store.read(cx).dev_server(id) else { let Some(dev_server) = self.dev_server_store.read(cx).dev_server(id) else {
return; return;
@ -682,6 +677,18 @@ impl DevServerProjects {
ModalContent::new().child( ModalContent::new().child(
v_flex() v_flex()
.w_full() .w_full()
.child(
v_flex()
.pb_2()
.w_full()
.px_2()
.child(
div()
.pl_2()
.max_w(rems(16.))
.child(self.dev_server_name_input.clone()),
)
)
.child( .child(
h_flex() h_flex()
.pb_2() .pb_2()
@ -690,33 +697,59 @@ impl DevServerProjects {
.px_2() .px_2()
.border_b_1() .border_b_1()
.border_color(cx.theme().colors().border) .border_color(cx.theme().colors().border)
.child(
div()
.pl_2()
.max_w(rems(16.))
.child(self.dev_server_name_input.clone()),
)
.child( .child(
div() div()
.pl_1() .pl_1()
.pb(px(3.)) .pb(px(3.))
.when(!creating && dev_server.is_none(), |div| { .when(!creating && dev_server.is_none(), |div| {
div.child(Button::new("create-dev-server", "Create").on_click( div
cx.listener(move |this, _, cx| { .child(
this.create_dev_server(cx); CheckboxWithLabel::new(
}), "use-server-name-in-ssh",
)) Label::new("Use name as ssh connection string"),
self.use_server_name_in_ssh,
cx.listener(move |this, &new_selection, _| {
this.use_server_name_in_ssh = new_selection;
})
)
)
.child(
Button::new("create-dev-server", "Create").on_click(
cx.listener(move |this, _, cx| {
this.create_dev_server(cx);
})
)
)
}) })
.when(creating && dev_server.is_none(), |div| { .when(creating && dev_server.is_none(), |div| {
div.child( div
Button::new("create-dev-server", "Creating...") .child(
.disabled(true), CheckboxWithLabel::new(
) "use-server-name-in-ssh",
Label::new("Use name as ssh connection string"),
self.use_server_name_in_ssh,
|&_, _| {}
)
)
.child(
Button::new("create-dev-server", "Creating...")
.disabled(true),
)
}), }),
) )
) )
.when(dev_server.is_none(), |div| { .when(dev_server.is_none(), |div| {
div.px_2().child(Label::new("Once you have created a dev server, you will be given a command to run on the server to register it.").color(Color::Muted)) let server_name = get_text(&self.dev_server_name_input, cx);
let server_name_trimmed = server_name.trim();
let ssh_host_name = if server_name_trimmed.is_empty() {
"user@host"
} else {
server_name_trimmed
};
div.px_2().child(Label::new(format!(
"Once you have created a dev server, you will be given a command to run on the server to register it.\n\n\
Ssh connection string enables remote terminals, which runs `ssh {ssh_host_name}` when creating terminal tabs."
)))
}) })
.when_some(dev_server.clone(), |div, dev_server| { .when_some(dev_server.clone(), |div, dev_server| {
let status = self let status = self
@ -973,15 +1006,14 @@ impl DevServerProjects {
.icon_position(IconPosition::Start) .icon_position(IconPosition::Start)
.tooltip(|cx| Tooltip::text("Register a new dev server", cx)) .tooltip(|cx| Tooltip::text("Register a new dev server", cx))
.on_click(cx.listener(|this, _, cx| { .on_click(cx.listener(|this, _, cx| {
this.mode = Mode::CreateDevServer(Default::default()); this.mode =
Mode::CreateDevServer(CreateDevServer::default());
this.dev_server_name_input.update(cx, |input, cx| { this.dev_server_name_input.update(cx, |text_field, cx| {
input.editor().update(cx, |editor, cx| { text_field.editor().update(cx, |editor, cx| {
editor.set_text("", cx); editor.set_text("", cx);
}); });
input.focus_handle(cx).focus(cx)
}); });
this.use_server_name_in_ssh = Selection::Unselected;
cx.notify(); cx.notify();
})), })),
), ),
@ -999,6 +1031,17 @@ impl DevServerProjects {
) )
} }
} }
fn get_text(element: &View<TextField>, cx: &mut WindowContext) -> String {
element
.read(cx)
.editor()
.read(cx)
.text(cx)
.trim()
.to_string()
}
impl ModalView for DevServerProjects {} impl ModalView for DevServerProjects {}
impl FocusableView for DevServerProjects { impl FocusableView for DevServerProjects {

View File

@ -490,6 +490,7 @@ message ValidateDevServerProjectRequest {
message CreateDevServer { message CreateDevServer {
reserved 1; reserved 1;
string name = 2; string name = 2;
optional string ssh_connection_string = 3;
} }
message RegenerateDevServerToken { message RegenerateDevServerToken {
@ -1251,6 +1252,7 @@ message DevServer {
uint64 dev_server_id = 2; uint64 dev_server_id = 2;
string name = 3; string name = 3;
DevServerStatus status = 4; DevServerStatus status = 4;
optional string ssh_connection_string = 5;
} }
enum DevServerStatus { enum DevServerStatus {

View File

@ -493,14 +493,6 @@ impl TerminalPanel {
cx.spawn(|terminal_panel, mut cx| async move { cx.spawn(|terminal_panel, mut cx| async move {
let pane = terminal_panel.update(&mut cx, |this, _| this.pane.clone())?; let pane = terminal_panel.update(&mut cx, |this, _| this.pane.clone())?;
workspace.update(&mut cx, |workspace, cx| { workspace.update(&mut cx, |workspace, cx| {
if workspace.project().read(cx).is_remote() {
workspace.show_error(
&anyhow::anyhow!("Cannot open terminals on remote projects (yet!)"),
cx,
);
return;
};
let working_directory = if let Some(working_directory) = working_directory { let working_directory = if let Some(working_directory) = working_directory {
Some(working_directory) Some(working_directory)
} else { } else {

View File

@ -214,8 +214,24 @@ pub fn initialize_workspace(app_state: Arc<AppState>, cx: &mut AppContext) {
workspace_handle.update(&mut cx, |workspace, cx| { workspace_handle.update(&mut cx, |workspace, cx| {
workspace.add_panel(assistant_panel, cx); workspace.add_panel(assistant_panel, cx);
workspace.add_panel(project_panel, cx); workspace.add_panel(project_panel, cx);
if !workspace.project().read(cx).is_remote() { {
workspace.add_panel(terminal_panel, cx); let project = workspace.project().read(cx);
if project.is_local()
|| project
.dev_server_project_id()
.and_then(|dev_server_project_id| {
Some(
dev_server_projects::Store::global(cx)
.read(cx)
.dev_server_for_project(dev_server_project_id)?
.ssh_connection_string
.is_some(),
)
})
.unwrap_or(false)
{
workspace.add_panel(terminal_panel, cx);
}
} }
workspace.add_panel(channels_panel, cx); workspace.add_panel(channels_panel, cx);
workspace.add_panel(chat_panel, cx); workspace.add_panel(chat_panel, cx);