Skip to content

Commit

Permalink
fix workspace host inactive toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
lyang2821 committed Oct 4, 2024
1 parent bef1b63 commit a872deb
Show file tree
Hide file tree
Showing 20 changed files with 190 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lapdev-api/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ pub async fn get_cluster_info(
machine_types,
has_enterprise,
hostnames: state.conductor.hostnames.read().await.clone(),
ssh_proxy_port: state.ssh_proxy_port,
ssh_proxy_port: state.ssh_proxy_display_port,
}))
}

Expand Down
18 changes: 15 additions & 3 deletions lapdev-api/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,11 @@ async fn handle_catch_all(
.and_then(|h| h.to_str().ok())
.map(|s| s.contains("gzip"))
.unwrap_or(false);
if let Some(file) = STATIC_DIR.get_file(f) {
if let Some(file) = if let Some(d) = state.static_dir.as_ref() {
d.get_file(f)
} else {
STATIC_DIR.get_file(f)
} {
let content_type = if f.ends_with(".css") {
Some("text/css")
} else if f.ends_with(".js") {
Expand All @@ -311,7 +315,11 @@ async fn handle_catch_all(
};
if let Some(content_type) = content_type {
if accept_gzip {
if let Some(file) = STATIC_DIR.get_file(format!("{f}.gz")) {
if let Some(file) = if let Some(d) = state.static_dir.as_ref() {
d.get_file(format!("{f}.gz"))
} else {
STATIC_DIR.get_file(format!("{f}.gz"))
} {
return Ok((
[
(axum::http::header::CONTENT_TYPE, content_type),
Expand Down Expand Up @@ -341,7 +349,11 @@ async fn handle_catch_all(
}
}

if let Some(file) = STATIC_DIR.get_file("index.html") {
if let Some(file) = if let Some(d) = state.static_dir.as_ref() {
d.get_file("index.html")
} else {
STATIC_DIR.get_file("index.html")
} {
return Ok(axum::response::Html::from(file.contents()).into_response());
}

Expand Down
18 changes: 15 additions & 3 deletions lapdev-api/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct LapdevConfig {
http_port: Option<u16>,
https_port: Option<u16>,
ssh_proxy_port: Option<u16>,
ssh_proxy_display_port: Option<u16>,
}

#[derive(Parser)]
Expand All @@ -49,7 +50,10 @@ struct Cli {
no_migration: bool,
}

pub async fn start(additional_router: Option<Router<CoreState>>) {
pub async fn start(
additional_router: Option<Router<CoreState>>,
static_dir: Option<include_dir::Dir<'static>>,
) {
let cli = Cli::parse();
let data_folder = cli
.data_folder
Expand All @@ -58,7 +62,7 @@ pub async fn start(additional_router: Option<Router<CoreState>>) {

let _result = setup_log(&cli, &data_folder).await;

if let Err(e) = run(&cli, additional_router, data_folder).await {
if let Err(e) = run(&cli, additional_router, data_folder, static_dir).await {
tracing::error!("lapdev api start server error: {e:#}");
}
}
Expand All @@ -67,6 +71,7 @@ async fn run(
cli: &Cli,
additional_router: Option<Router<CoreState>>,
data_folder: PathBuf,
static_dir: Option<include_dir::Dir<'static>>,
) -> Result<()> {
let config_file = cli
.config_file
Expand All @@ -85,6 +90,7 @@ async fn run(
let conductor = Conductor::new(LAPDEV_VERSION, db.clone(), data_folder).await?;

let ssh_proxy_port = config.ssh_proxy_port.unwrap_or(2222);
let ssh_proxy_display_port = config.ssh_proxy_display_port.unwrap_or(2222);
{
let conductor = conductor.clone();
let bind = config.bind.clone();
Expand All @@ -101,7 +107,13 @@ async fn run(
});
}

let state = CoreState::new(conductor, ssh_proxy_port).await;
let state = CoreState::new(
conductor,
ssh_proxy_port,
ssh_proxy_display_port,
static_dir,
)
.await;
let app = router::build_router(state.clone(), additional_router).await;
let certs = state.certs.clone();

Expand Down
13 changes: 12 additions & 1 deletion lapdev-api/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,20 @@ pub struct CoreState {
pub auth: Arc<Auth>,
pub auth_token_key: Arc<SymmetricKey<V4>>,
pub certs: CertStore,
// actuall ssh proxy port
pub ssh_proxy_port: u16,
// ssh proxy port to display in front end
pub ssh_proxy_display_port: u16,
pub static_dir: Arc<Option<include_dir::Dir<'static>>>,
}

impl CoreState {
pub async fn new(conductor: Conductor, ssh_proxy_port: u16) -> Self {
pub async fn new(
conductor: Conductor,
ssh_proxy_port: u16,
ssh_proxy_display_port: u16,
static_dir: Option<include_dir::Dir<'static>>,
) -> Self {
let github_client = GithubClient::new();
let key = conductor.db.load_api_auth_token_key().await;
let auth = Auth::new(&conductor.db).await;
Expand All @@ -95,7 +104,9 @@ impl CoreState {
auth_token_key: Arc::new(key),
certs: Arc::new(std::sync::RwLock::new(Arc::new(certs))),
ssh_proxy_port,
ssh_proxy_display_port,
hyper_client: Arc::new(hyper_client),
static_dir: Arc::new(static_dir),
};

{
Expand Down
2 changes: 2 additions & 0 deletions lapdev-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,8 @@ pub struct NewContainerHostConfig {
pub security_opt: Option<Vec<String>>,
#[serde(rename = "StorageOpt")]
pub storage_opt: Option<HashMap<String, String>>,
#[serde(rename = "Privileged")]
pub privileged: bool,
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down
20 changes: 11 additions & 9 deletions lapdev-conductor/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,17 @@ impl Conductor {
}
}

let _ = self.connect_workspace_host_once(id, &host, port).await;
if let Err(e) = self.connect_workspace_host_once(id, &host, port).await {
tracing::error!("connect workspace host {host}:{port} failed: {e:?}");
}

let _ = entities::workspace_host::ActiveModel {
id: ActiveValue::Set(id),
status: ActiveValue::Set(WorkspaceHostStatus::Inactive.to_string()),
..Default::default()
}
.update(&self.db.conn)
.await;

{
self.rpcs.lock().await.remove(&id);
Expand Down Expand Up @@ -471,14 +481,6 @@ impl Conductor {

let _ = rpc_future.await;
tracing::debug!("workspace host connection ended");

entities::workspace_host::ActiveModel {
id: ActiveValue::Set(id),
status: ActiveValue::Set(WorkspaceHostStatus::Inactive.to_string()),
..Default::default()
}
.update(&self.db.conn)
.await?;
}

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions lapdev-dashboard/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ fn ProjectInfoView(
.into_iter()
.find(|m| m.id == info.machine_type)
)
.map(|machine_type| format!("{} - {} {}cores, {}GB memory, {}GB disk",machine_type.name, machine_type.cpu, if machine_type.shared { "shared "} else {""}, machine_type.memory, machine_type.disk))
.map(|machine_type| format!("{} - {} vCPUs, {}GB memory, {}GB disk",machine_type.name, machine_type.cpu, machine_type.memory, machine_type.disk))
}
</span>
<span class="mt-2 text-sm inline-flex items-center rounded me-2">
Expand Down Expand Up @@ -1247,7 +1247,7 @@ pub fn MachineTypeView(
data-uuid=move || machine_type.id.to_string()
selected=move || Some(machine_type.id) == preferred_machine_type.get()
>
{format!("{} - {} {}cores, {}GB memory, {}GB disk",machine_type.name, machine_type.cpu, if machine_type.shared { "shared "} else {""}, machine_type.memory, machine_type.disk)}
{format!("{} - {} vCPUs, {}GB memory, {}GB disk",machine_type.name, machine_type.cpu, machine_type.memory, machine_type.disk)}
</option>
}
}
Expand Down
40 changes: 36 additions & 4 deletions lapdev-dashboard/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,18 @@ fn OpenWorkspaceView(
let port = cluster_info
.with_untracked(|i| i.as_ref().map(|i| i.ssh_proxy_port).unwrap_or(2222));
let _ = window()
.open_with_url_and_target(&format!("vscode://vscode-remote/ssh-remote+{workspace_name}@{}:{port}/workspaces/{workspace_folder}", workspace_hostname.split(':').next().unwrap_or("")), "_blank");
.open_with_url_and_target(
&format!(
"vscode://vscode-remote/ssh-remote+{workspace_name}@{}{}/workspaces/{workspace_folder}",
workspace_hostname.split(':').next().unwrap_or(""),
if port == 22 {
"".to_string()
} else {
format!(":{port}")
}
),
"_blank"
);
}
};

Expand Down Expand Up @@ -1076,7 +1087,17 @@ pub fn WorkspaceDetails() -> impl IntoView {
{
let workspace_name = workspace_name.clone();
let workspace_hostname = workspace_hostname.clone();
move || format!("ssh {workspace_name}@{} -p {}", workspace_hostname.split(':').next().unwrap_or(""), cluster_info.with(|i| i.as_ref().map(|i| i.ssh_proxy_port).unwrap_or(2222)))
move || {
let ssh_port = cluster_info.with(|i| i.as_ref().map(|i| i.ssh_proxy_port).unwrap_or(2222));
format!("ssh {workspace_name}@{}{}",
workspace_hostname.split(':').next().unwrap_or(""),
if ssh_port == 22 {
"".to_string()
} else {
format!(" -p {ssh_port}")
}
)
}
}
</span>
<span class="mt-2 text-sm flex flex-row items-center rounded me-2">
Expand All @@ -1092,7 +1113,7 @@ pub fn WorkspaceDetails() -> impl IntoView {
m.into_iter()
.find(|m| m.id == info.machine_type)
)
.map(|machine_type| format!("{} - {} {}cores, {}GB memory, {}GB disk",machine_type.name, machine_type.cpu, if machine_type.shared { "shared "} else {""}, machine_type.memory, machine_type.disk))
.map(|machine_type| format!("{} - {} vCPUs, {}GB memory, {}GB disk",machine_type.name, machine_type.cpu, machine_type.memory, machine_type.disk))
}
</span>
<span class="mt-2 text-sm flex flex-row items-center rounded me-2">
Expand Down Expand Up @@ -1141,7 +1162,18 @@ pub fn WorkspaceDetails() -> impl IntoView {
{
let ws_service_name = ws_service.name.clone();
let workspace_hostname = workspace_hostname.clone();
move || format!("ssh {ws_service_name}@{} -p {}", workspace_hostname.split(':').next().unwrap_or(""), cluster_info.with(|i| i.as_ref().map(|i| i.ssh_proxy_port).unwrap_or(2222)))
move || {
let ssh_proxy_port = cluster_info.with(|i| i.as_ref().map(|i| i.ssh_proxy_port).unwrap_or(2222));
format!(
"ssh {ws_service_name}@{}{}",
workspace_hostname.split(':').next().unwrap_or(""),
if ssh_proxy_port == 22 {
"".to_string()
} else {
format!(" -p {ssh_proxy_port}")
},
)
}
}
</span>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ impl MigrationTrait for Migration {
.big_integer()
.not_null(),
)
.col(
ColumnDef::new(Organization::RunningWorkspaceLimit)
.integer()
.not_null(),
)
.to_owned(),
)
.await?;
Expand Down Expand Up @@ -68,4 +73,5 @@ enum Organization {
AllowWorkspaceChangeAutoStop,
LastAutoStopCheck,
UsageLimit,
RunningWorkspaceLimit,
}
2 changes: 2 additions & 0 deletions lapdev-guest-agent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ fn run_sshd() -> Result<(), LapdevGuestAgentError> {
fs::create_dir_all("/root/.ssh/")?;
fs::write("/root/.ssh/authorized_keys", public_key)?;
Command::new("/usr/sbin/sshd")
.arg("-p")
.arg("22")
.arg("-D")
.arg("-o")
.arg("AcceptEnv=*")
Expand Down
1 change: 1 addition & 0 deletions lapdev-proxy-ssh/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub async fn run(conductor: Conductor, bind: &str, port: u16) -> Result<()> {

let config = russh::server::Config {
inactivity_timeout: Some(std::time::Duration::from_secs(3600)),
keepalive_interval: Some(std::time::Duration::from_secs(10)),
auth_rejection_time: std::time::Duration::from_secs(3),
auth_rejection_time_initial: Some(std::time::Duration::from_secs(0)),
methods: MethodSet::NONE | MethodSet::PUBLICKEY,
Expand Down
37 changes: 34 additions & 3 deletions lapdev-ws/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,10 @@ impl WorkspaceServer {
.arg(osuser)
.arg("-c")
.arg("podman system service --time=0")
.status()
.output()
.await;
println!("podman system service finished");
});
tokio::time::sleep(Duration::from_secs(1)).await;
}
}

Expand Down Expand Up @@ -379,12 +379,43 @@ impl WorkspaceServer {
.wait()
.await?;

let containers_config_folder = format!("/home/{username}/.config/containers");
Command::new("su")
.arg("-")
.arg(username)
.arg("-c")
.arg(format!("mkdir -p {containers_config_folder}"))
.spawn()?
.wait()
.await?;
tokio::fs::write(
format!("{containers_config_folder}/registries.conf"),
r#"
unqualified-search-registries = ["docker.io"]
"#,
)
.await?;
tokio::fs::write(
format!("{containers_config_folder}/storage.conf"),
r#"
[storage]
driver = "overlay"
"#,
)
.await?;
Command::new("chown")
.arg("-R")
.arg(format!("{username}:{username}"))
.arg(&containers_config_folder)
.output()
.await?;

let uid = self._os_user_uid(username).await?;

Command::new("loginctl")
.arg("enable-linger")
.arg(&uid)
.status()
.output()
.await?;

Ok(uid)
Expand Down
4 changes: 4 additions & 0 deletions lapdev-ws/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ impl WorkspaceService for WorkspaceRpcService {
"none",
"--bind-addr",
"0.0.0.0:30000",
"--app-name",
"Lapdev",
"--disable-workspace-trust",
&format!("/workspaces/{}", ws_req.repo_name),
])?;

Expand Down Expand Up @@ -194,6 +197,7 @@ impl WorkspaceService for WorkspaceRpcService {
cap_add: vec!["NET_RAW".to_string()],
security_opt: Some(vec!["label=disable".to_string()]),
storage_opt: None,
privileged: true,
},
networking_config: NewContainerNetworkingConfig {
endpoints_config: if let Some(service) = ws_req.service.as_ref() {
Expand Down
8 changes: 8 additions & 0 deletions pkg/image/go/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM mcr.microsoft.com/devcontainers/go:latest

USER root
COPY ../../../target/release/lapdev-guest-agent /lapdev-guest-agent
RUN chmod +x /lapdev-guest-agent
COPY ../../../lapdev-ws/scripts/install_guest_agent.sh /install_guest_agent.sh
RUN sh /install_guest_agent.sh
RUN rm /install_guest_agent.sh
8 changes: 8 additions & 0 deletions pkg/image/javascript/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM mcr.microsoft.com/devcontainers/javascript-node:latest

USER root
COPY ../../../target/release/lapdev-guest-agent /lapdev-guest-agent
RUN chmod +x /lapdev-guest-agent
COPY ../../../lapdev-ws/scripts/install_guest_agent.sh /install_guest_agent.sh
RUN sh /install_guest_agent.sh
RUN rm /install_guest_agent.sh
Loading

0 comments on commit a872deb

Please sign in to comment.