Skip to content

Commit

Permalink
add workspace port label
Browse files Browse the repository at this point in the history
  • Loading branch information
lyang2821 committed Oct 25, 2024
1 parent a8a9223 commit 474c033
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 61 deletions.
1 change: 1 addition & 0 deletions lapdev-api/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ pub async fn workspace_ports(
port: p.port as u16,
shared: p.shared,
public: p.public,
label: p.label,
})
.collect::<Vec<_>>(),
)
Expand Down
11 changes: 9 additions & 2 deletions lapdev-common/src/devcontainer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use serde::Deserialize;
use serde::{Deserialize, Serialize};

pub type DevContainerCwd = std::path::PathBuf;

Expand All @@ -18,7 +18,8 @@ pub struct DevContainerConfig {
#[serde(default)]
pub run_args: Vec<String>,
pub docker_compose_file: Option<String>,

#[serde(default)]
pub ports_attributes: HashMap<String, PortAttribute>,
pub service: Option<String>,
}

Expand All @@ -45,3 +46,9 @@ pub struct BuildConfig {
#[serde(default)]
pub args: HashMap<String, String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct PortAttribute {
pub label: Option<String>,
}
8 changes: 7 additions & 1 deletion lapdev-common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;

use chrono::{DateTime, FixedOffset};
use devcontainer::PortAttribute;
use serde::{Deserialize, Serialize};
use strum_macros::EnumString;
use uuid::Uuid;
Expand Down Expand Up @@ -143,10 +144,14 @@ pub struct RepoContent {

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum RepoBuildOutput {
Compose(Vec<RepoComposeService>),
Compose {
services: Vec<RepoComposeService>,
ports_attributes: HashMap<String, PortAttribute>,
},
Image {
image: String,
info: ContainerImageInfo,
ports_attributes: HashMap<String, PortAttribute>,
},
}

Expand Down Expand Up @@ -321,6 +326,7 @@ pub struct WorkspacePort {
pub port: u16,
pub shared: bool,
pub public: bool,
pub label: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Hash, Eq, PartialEq)]
Expand Down
1 change: 1 addition & 0 deletions lapdev-conductor/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ mod tests {
compose_parent: ActiveValue::Set(None),
auto_stop: ActiveValue::Set(None),
build_output: ActiveValue::Set(None),
updated_at: ActiveValue::Set(None),
deleted_at: ActiveValue::Set(None),
env: ActiveValue::Set(None),
last_inactivity: ActiveValue::Set(None),
Expand Down
30 changes: 26 additions & 4 deletions lapdev-conductor/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,7 @@ impl Conductor {
let ws = entities::workspace::ActiveModel {
id: ActiveValue::Set(workspace_id),
deleted_at: ActiveValue::Set(None),
updated_at: ActiveValue::Set(None),
name: ActiveValue::Set(name.clone()),
created_at: ActiveValue::Set(now.into()),
status: ActiveValue::Set(WorkspaceStatus::New.to_string()),
Expand Down Expand Up @@ -1671,8 +1672,11 @@ impl Conductor {
.iter()
.map(|(k, v)| format!("{k}={v}"))
.collect::<Vec<_>>();
let (is_compose, images) = match &output {
RepoBuildOutput::Compose(services) => (
let (is_compose, images, ports_attributes) = match &output {
RepoBuildOutput::Compose {
services,
ports_attributes,
} => (
true,
services
.iter()
Expand All @@ -1685,15 +1689,21 @@ impl Conductor {
)
})
.collect(),
ports_attributes,
),
RepoBuildOutput::Image { image, info } => (
RepoBuildOutput::Image {
image,
info,
ports_attributes,
} => (
false,
vec![(
None,
image.clone(),
info.config.env.clone().unwrap_or_default(),
vec![],
)],
ports_attributes,
),
};

Expand Down Expand Up @@ -1822,6 +1832,7 @@ impl Conductor {
id: ActiveValue::Set(Uuid::new_v4()),
name: ActiveValue::Set(workspace_name),
created_at: ActiveValue::Set(Utc::now().into()),
updated_at: ActiveValue::Set(None),
deleted_at: ActiveValue::Set(None),
status: ActiveValue::Set(WorkspaceStatus::Running.to_string()),
repo_url: ActiveValue::Set(ws.repo_url.clone()),
Expand Down Expand Up @@ -1863,6 +1874,11 @@ impl Conductor {
host_port: ActiveValue::Set(host_port as i32),
shared: ActiveValue::Set(false),
public: ActiveValue::Set(false),
label: ActiveValue::Set(
ports_attributes
.get(&port.to_string())
.and_then(|a| a.label.clone()),
),
}
.insert(&self.db.conn)
.await?;
Expand Down Expand Up @@ -2192,7 +2208,7 @@ impl Conductor {

if let Some(output) = output {
match output {
RepoBuildOutput::Compose(services) => {
RepoBuildOutput::Compose { services, .. } => {
services.into_iter().map(|s| s.image).collect()
}
RepoBuildOutput::Image { image, .. } => vec![image],
Expand Down Expand Up @@ -2335,6 +2351,7 @@ impl Conductor {
.await?;
let update_ws = entities::workspace::ActiveModel {
id: ActiveValue::Set(workspace.id),
updated_at: ActiveValue::Set(Some(now.into())),
status: ActiveValue::Set(WorkspaceStatus::Starting.to_string()),
..Default::default()
};
Expand Down Expand Up @@ -2410,6 +2427,7 @@ impl Conductor {
let update_ws = entities::workspace::ActiveModel {
id: ActiveValue::Set(workspace.id),
status: ActiveValue::Set(WorkspaceStatus::Stopping.to_string()),
updated_at: ActiveValue::Set(Some(now.into())),
..Default::default()
};
let ws = update_ws.update(&txn).await?;
Expand Down Expand Up @@ -2496,6 +2514,7 @@ impl Conductor {
status: ActiveValue::Set(status.to_string()),
usage_id: ActiveValue::Set(usage.map(|u| u.id)),
last_inactivity: ActiveValue::Set(None),
updated_at: ActiveValue::Set(Some(now.into())),
..Default::default()
}
.update(&txn)
Expand All @@ -2513,6 +2532,7 @@ impl Conductor {
let status = WorkspaceStatus::Failed;
entities::workspace::ActiveModel {
id: ActiveValue::Set(ws.id),
updated_at: ActiveValue::Set(Some(now.into())),
status: ActiveValue::Set(WorkspaceStatus::Failed.to_string()),
..Default::default()
}
Expand Down Expand Up @@ -2553,6 +2573,7 @@ impl Conductor {
entities::workspace::ActiveModel {
id: ActiveValue::Set(ws.id),
status: ActiveValue::Set(status.to_string()),
updated_at: ActiveValue::Set(Some(now.into())),
usage_id: ActiveValue::Set(None),
..Default::default()
}
Expand All @@ -2571,6 +2592,7 @@ impl Conductor {
entities::workspace::ActiveModel {
id: ActiveValue::Set(ws.id),
status: ActiveValue::Set(status.to_string()),
updated_at: ActiveValue::Set(Some(now.into())),
..Default::default()
}
.update(&self.db.conn)
Expand Down
8 changes: 7 additions & 1 deletion lapdev-dashboard/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1441,7 +1441,13 @@ fn WorkspacePortView(

view! {
<div class="flex flex-row items-center w-full px-4 py-2 border-b">
<span class="w-1/3 truncate pr-2">{p.port}</span>
<span class="w-1/3 truncate pr-2">{
if let Some(label) = p.label.as_ref() {
format!("{label} ({})", p.port)
} else {
p.port.to_string()
}
}</span>
<div class="w-1/3 truncate flex flex-row items-center">
<div class="w-1/2 flex items-center justify-center">
<input
Expand Down
1 change: 1 addition & 0 deletions lapdev-db/src/entities/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub created_at: DateTimeWithTimeZone,
pub updated_at: Option<DateTimeWithTimeZone>,
pub deleted_at: Option<DateTimeWithTimeZone>,
pub organization_id: Uuid,
pub user_id: Uuid,
Expand Down
1 change: 1 addition & 0 deletions lapdev-db/src/entities/workspace_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct Model {
pub host_port: i32,
pub shared: bool,
pub public: bool,
pub label: Option<String>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl MigrationTrait for Migration {
.timestamp_with_time_zone()
.not_null(),
)
.col(ColumnDef::new(Workspace::UpdatedAt).timestamp_with_time_zone())
.col(ColumnDef::new(Workspace::DeletedAt).timestamp_with_time_zone())
.col(ColumnDef::new(Workspace::OrganizationId).uuid().not_null())
.col(ColumnDef::new(Workspace::UserId).uuid().not_null())
Expand Down Expand Up @@ -141,6 +142,7 @@ pub enum Workspace {
Table,
Id,
CreatedAt,
UpdatedAt,
DeletedAt,
OrganizationId,
UserId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ impl MigrationTrait for Migration {
.col(ColumnDef::new(WorkspacePort::Port).integer().not_null())
.col(ColumnDef::new(WorkspacePort::HostPort).integer().not_null())
.col(ColumnDef::new(WorkspacePort::Shared).boolean().not_null())
.col(ColumnDef::new(WorkspacePort::Public).boolean().not_null())
.col(ColumnDef::new(WorkspacePort::Label).string())
.foreign_key(
ForeignKey::create()
.from_tbl(WorkspacePort::Table)
Expand All @@ -41,7 +43,6 @@ impl MigrationTrait for Migration {
.table(WorkspacePort::Table)
.col(WorkspacePort::WorkspaceId)
.col(WorkspacePort::Port)
.col(WorkspacePort::Public)
.to_owned(),
)
.await?;
Expand All @@ -59,4 +60,5 @@ enum WorkspacePort {
HostPort,
Shared,
Public,
Label,
}
2 changes: 2 additions & 0 deletions lapdev-enterprise/src/auto_start_stop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ pub mod tests {

entities::workspace::ActiveModel {
id: ActiveValue::Set(Uuid::new_v4()),
updated_at: ActiveValue::Set(None),
deleted_at: ActiveValue::Set(None),
name: ActiveValue::Set(utils::rand_string(10)),
created_at: ActiveValue::Set(Utc::now().into()),
Expand Down Expand Up @@ -261,6 +262,7 @@ pub mod tests {

let ws = entities::workspace::ActiveModel {
id: ActiveValue::Set(Uuid::new_v4()),
updated_at: ActiveValue::Set(None),
deleted_at: ActiveValue::Set(None),
name: ActiveValue::Set(utils::rand_string(10)),
created_at: ActiveValue::Set(Utc::now().into()),
Expand Down
1 change: 1 addition & 0 deletions lapdev-enterprise/src/quota.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ pub mod tests {
) -> Result<entities::workspace::Model> {
let ws = entities::workspace::ActiveModel {
id: ActiveValue::Set(Uuid::new_v4()),
updated_at: ActiveValue::Set(None),
deleted_at: ActiveValue::Set(None),
name: ActiveValue::Set(utils::rand_string(10)),
created_at: ActiveValue::Set(Utc::now().into()),
Expand Down
6 changes: 3 additions & 3 deletions lapdev-guest-agent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn run_sshd() -> Result<(), LapdevGuestAgentError> {
.arg("-D")
.arg("-o")
.arg("AcceptEnv=*")
.status()?;
.output()?;
Ok(())
}

Expand All @@ -82,7 +82,7 @@ fn run_cmd(cmd: Vec<String>) -> Result<(), LapdevGuestAgentError> {
return Err(LapdevGuestAgentError::Cmds("empty cmd".to_string()));
}
thread::spawn(move || {
if let Err(e) = Command::new("sh").arg("-c").arg(cmd.join(" ")).status() {
if let Err(e) = Command::new("sh").arg("-c").arg(cmd.join(" ")).output() {
eprintln!("run cmd error: {e:?}");
}
});
Expand All @@ -97,6 +97,6 @@ fn run_ide_cmds() -> Result<(), LapdevGuestAgentError> {
if cmds.is_empty() {
return Err(LapdevGuestAgentError::Cmds("empty cmd".to_string()));
}
Command::new(&cmds[0]).args(&cmds[1..]).status()?;
Command::new(&cmds[0]).args(&cmds[1..]).output()?;
Ok(())
}
Loading

0 comments on commit 474c033

Please sign in to comment.