Skip to content

Commit

Permalink
copy image env to workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
lyang2821 committed Oct 14, 2024
1 parent 84f29cf commit 4c3d678
Show file tree
Hide file tree
Showing 8 changed files with 1,069 additions and 109 deletions.
24 changes: 15 additions & 9 deletions lapdev-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,18 @@ pub struct RepoContent {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum RepoBuildOutput {
Compose(Vec<RepoComposeService>),
Image(String),
Image {
image: String,
info: ContainerImageInfo,
},
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RepoComposeService {
pub name: String,
pub image: String,
pub env: Vec<(String, String)>,
pub env: Vec<String>,
pub info: ContainerImageInfo,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand Down Expand Up @@ -351,7 +355,7 @@ pub struct CreateWorkspaceRequest {
pub image: String,
pub ssh_public_key: String,
pub repo_name: String,
pub env: Vec<(String, String)>,
pub env: Vec<String>,
pub cpus: CpuCore,
pub memory: usize,
pub disk: usize,
Expand Down Expand Up @@ -503,15 +507,15 @@ pub struct Container {
pub id: String,
}

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ContainerPortBinding {
#[serde(rename = "HostIp")]
pub host_ip: String,
#[serde(rename = "HostPort")]
pub host_port: String,
}

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ContainerConfig {
#[serde(rename = "Hostname")]
pub hostname: String,
Expand All @@ -521,7 +525,7 @@ pub struct ContainerConfig {
pub env: Option<Vec<String>>,
}

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct ContainerImageConfig {
#[serde(rename = "Hostname")]
pub hostname: String,
Expand All @@ -531,27 +535,29 @@ pub struct ContainerImageConfig {
pub entrypoint: Option<Vec<String>>,
#[serde(rename = "Cmd")]
pub cmd: Option<Vec<String>>,
#[serde(rename = "Env")]
pub env: Option<Vec<String>>,
#[serde(rename = "ExposedPorts")]
pub exposed_ports: Option<HashMap<String, HashMap<String, String>>>,
}

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ContainerHostConfig {
#[serde(rename = "PortBindings")]
pub port_bindings: HashMap<String, Vec<ContainerPortBinding>>,
#[serde(rename = "Binds")]
pub binds: Vec<String>,
}

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ContainerInfo {
#[serde(rename = "Config")]
pub config: ContainerConfig,
#[serde(rename = "HostConfig")]
pub host_config: ContainerHostConfig,
}

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct ContainerImageInfo {
#[serde(rename = "Id")]
pub id: String,
Expand Down
47 changes: 38 additions & 9 deletions lapdev-conductor/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1682,21 +1682,40 @@ impl Conductor {
ws_client: &WorkspaceServiceClient,
machine_type: &entities::machine_type::Model,
) -> Result<(), ApiError> {
let flatten_env = env
.iter()
.map(|(k, v)| format!("{k}={v}"))
.collect::<Vec<_>>();
let (is_compose, images) = match &output {
RepoBuildOutput::Compose(services) => (
true,
services
.iter()
.map(|s| (Some(s.name.clone()), s.image.clone(), s.env.clone()))
.map(|s| {
(
Some(s.name.clone()),
s.image.clone(),
s.info.config.env.clone().unwrap_or_default(),
s.env.clone(),
)
})
.collect(),
),
RepoBuildOutput::Image(tag) => (false, vec![(None, tag.clone(), Vec::new())]),
RepoBuildOutput::Image { image, info } => (
false,
vec![(
None,
image.clone(),
info.config.env.clone().unwrap_or_default(),
vec![],
)],
),
};

let build_output = serde_json::to_string(&output)?;

let cores: CpuCore = serde_json::from_str(&ws.cores)?;
for (i, (service, tag, image_env)) in images.into_iter().enumerate() {
for (i, (service, tag, image_env, service_env)) in images.into_iter().enumerate() {
let workspace_name = if let Some(service) = service.clone() {
if i > 0 {
format!("{}-{service}", ws.name)
Expand All @@ -1711,8 +1730,18 @@ impl Conductor {
} else {
self.generate_key_pair()?
};
let mut env = env.clone();
env.extend_from_slice(&image_env);
let mut ws_env = service_env.clone();
ws_env.extend_from_slice(&flatten_env);

let mut all_env = image_env
.iter()
.chain(service_env.iter())
.filter_map(|env| {
let (key, value) = env.split_once('=')?;
Some((key.to_string(), value.to_string()))
})
.collect::<Vec<_>>();
all_env.extend_from_slice(&env);
ws_client
.create_workspace(
long_running_context(),
Expand All @@ -1727,7 +1756,7 @@ impl Conductor {
image: tag,
ssh_public_key: ssh_public_key.clone(),
repo_name: ws.repo_name.clone(),
env: env.clone(),
env: ws_env.clone(),
cpus: cores.clone(),
memory: machine_type.memory as usize,
disk: machine_type.disk as usize,
Expand Down Expand Up @@ -1796,7 +1825,7 @@ impl Conductor {
prebuild_id: ActiveValue::Set(prebuild_id),
build_output: ActiveValue::Set(Some(build_output.clone())),
is_compose: ActiveValue::Set(is_compose),
env: ActiveValue::Set(serde_json::to_string(&env).ok()),
env: ActiveValue::Set(serde_json::to_string(&all_env).ok()),
usage_id: ActiveValue::Set(Some(usage.id)),
..Default::default()
}
Expand Down Expand Up @@ -1831,7 +1860,7 @@ impl Conductor {
last_inactivity: ActiveValue::Set(None),
auto_stop: ActiveValue::Set(ws.auto_stop),
auto_start: ActiveValue::Set(ws.auto_start),
env: ActiveValue::Set(serde_json::to_string(&env).ok()),
env: ActiveValue::Set(serde_json::to_string(&all_env).ok()),
build_output: ActiveValue::Set(Some(build_output.clone())),
is_compose: ActiveValue::Set(is_compose),
compose_parent: ActiveValue::Set(Some(ws.id)),
Expand Down Expand Up @@ -2167,7 +2196,7 @@ impl Conductor {
RepoBuildOutput::Compose(services) => {
services.into_iter().map(|s| s.image).collect()
}
RepoBuildOutput::Image(tag) => vec![tag],
RepoBuildOutput::Image { image, .. } => vec![image],
}
} else {
Vec::new()
Expand Down
Loading

0 comments on commit 4c3d678

Please sign in to comment.