Skip to content

Commit

Permalink
compile lapdev-guest-agent with musl
Browse files Browse the repository at this point in the history
  • Loading branch information
lyang2821 committed Nov 5, 2024
1 parent 30a07c3 commit 538e8dd
Showing 16 changed files with 116 additions and 47 deletions.
5 changes: 5 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"dockerComposeFile": "docker-compose.yml",
"service": "lapdev",
"portsAttributes": {
"8080": {
"label": "dashboard",
}
}
}
2 changes: 2 additions & 0 deletions lapdev-common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -357,6 +357,8 @@ pub struct WorkspaceService {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct HostWorkspace {
pub id: Uuid,
pub name: String,
pub osuser: String,
pub ssh_port: Option<i32>,
pub ide_port: Option<i32>,
pub last_inactivity: Option<DateTime<FixedOffset>>,
2 changes: 2 additions & 0 deletions lapdev-conductor/src/rpc.rs
Original file line number Diff line number Diff line change
@@ -83,6 +83,8 @@ impl ConductorService for ConductorRpc {
.into_iter()
.map(|ws| HostWorkspace {
id: ws.id,
name: ws.name,
osuser: ws.osuser,
ssh_port: ws.ssh_port,
ide_port: ws.ide_port,
last_inactivity: ws.last_inactivity,
1 change: 1 addition & 0 deletions lapdev-conductor/src/server.rs
Original file line number Diff line number Diff line change
@@ -481,6 +481,7 @@ impl Conductor {

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

Ok(())
2 changes: 1 addition & 1 deletion lapdev-dashboard/src/workspace.rs
Original file line number Diff line number Diff line change
@@ -410,7 +410,7 @@ fn WorkspaceControl(
href="#"
class="block px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white"
on:click=stop_workspace
class:hidden=move || workspace_status != WorkspaceStatus::Running && workspace_status != WorkspaceStatus::Stopping
class:hidden=move || workspace_status == WorkspaceStatus::Stopped
>
Stop
</a>
83 changes: 80 additions & 3 deletions lapdev-ws/src/server.rs
Original file line number Diff line number Diff line change
@@ -20,8 +20,9 @@ use lapdev_common::{
devcontainer::{
DevContainerCmd, DevContainerConfig, DevContainerCwd, DevContainerLifeCycleCmd,
},
utils, BuildTarget, ContainerImageInfo, CpuCore, GitBranch, HostWorkspace, ProjectRequest,
RepoBuildInfo, RepoBuildOutput, RepoBuildResult, RepoComposeService, RepobuildError,
utils, BuildTarget, ContainerImageInfo, ContainerInfo, CpuCore, GitBranch, HostWorkspace,
ProjectRequest, RepoBuildInfo, RepoBuildOutput, RepoBuildResult, RepoComposeService,
RepobuildError, StartWorkspaceRequest,
};
use lapdev_rpc::{
error::ApiError, spawn_twoway, ConductorServiceClient, InterWorkspaceService, WorkspaceService,
@@ -48,7 +49,8 @@ use crate::{

pub const LAPDEV_WS_VERSION: &str = env!("CARGO_PKG_VERSION");
const INSTALL_SCRIPT: &[u8] = include_bytes!("../scripts/install_guest_agent.sh");
const LAPDEV_GUEST_AGENT: &[u8] = include_bytes!("../../target/release/lapdev-guest-agent");
const LAPDEV_GUEST_AGENT: &[u8] =
include_bytes!("../../target/x86_64-unknown-linux-musl/release/lapdev-guest-agent");

#[derive(Clone, Deserialize, Default)]
#[serde(rename_all = "kebab-case")]
@@ -175,6 +177,7 @@ impl WorkspaceServer {
});
}

let mut startup_task_run = false;
while let Some(conn) = listener.next().await {
if let Ok(conn) = conn {
let peer_addr = conn.peer_addr();
@@ -203,6 +206,16 @@ impl WorkspaceServer {
tracing::info!("incoming conductor connection {peer_addr:?} stopped");
rpcs.write().await.retain(|rpc| rpc.id != id);
});

if !startup_task_run {
startup_task_run = true;
let server = self.clone();
tokio::spawn(async move {
if let Err(e) = server.run_startup_task().await {
tracing::error!("run start up task error: {e:?}");
}
});
}
}
}

@@ -233,6 +246,29 @@ impl WorkspaceServer {
}
}

async fn run_startup_task(&self) -> Result<(), ApiError> {
tracing::info!("run startup task");
let rpc = { self.rpcs.read().await.first().cloned() };
let rpc = rpc.ok_or_else(|| anyhow!("don't have any conductor connections"))?;
let workspaces = rpc
.conductor_client
.running_workspaces_on_host(current())
.await??;
for ws in workspaces {
if let Err(e) = self
.start_workspace(StartWorkspaceRequest {
osuser: ws.osuser,
workspace_name: ws.name.clone(),
})
.await
{
tracing::error!("on startup task, start workspace {} error: {e:?}", ws.name);
}
}

Ok(())
}

async fn run_auto_stop_delete_task(&self) -> Result<(), ApiError> {
let rpc = { self.rpcs.read().await.first().cloned() };
let rpc = rpc.ok_or_else(|| anyhow!("don't have any conductor connections"))?;
@@ -1458,6 +1494,47 @@ driver = "overlay"

Ok(branches)
}

pub async fn start_workspace(
&self,
ws_req: StartWorkspaceRequest,
) -> Result<ContainerInfo, ApiError> {
let socket = self.get_podman_socket(&ws_req.osuser).await?;
let client = unix_client();
let url = Uri::new(
&socket,
&format!("/containers/{}/start", ws_req.workspace_name),
);
let req = hyper::Request::builder()
.method(hyper::Method::POST)
.uri(url)
.body(Full::<Bytes>::new(Bytes::new()))?;
let resp = client.request(req).await?;
let status = resp.status();
let body = resp.collect().await?.to_bytes();
if status != 204 && status != 304 {
let err = String::from_utf8(body.to_vec())?;
return Err(anyhow!("start container error: {err}").into());
}

let url = Uri::new(
&socket,
&format!("/containers/{}/json", ws_req.workspace_name),
);
let req = hyper::Request::builder()
.method(hyper::Method::GET)
.uri(url)
.body(Full::<Bytes>::new(Bytes::new()))?;
let resp = client.request(req).await?;
let status = resp.status();
let body = resp.collect().await?.to_bytes();
if status != 200 {
let err = String::from_utf8(body.to_vec())?;
return Err(anyhow!("get container info error: {err}").into());
}
let info: ContainerInfo = serde_json::from_slice(&body)?;
Ok(info)
}
}

async fn spawn(fut: impl futures::Future<Output = ()> + Send + 'static) {
36 changes: 1 addition & 35 deletions lapdev-ws/src/service.rs
Original file line number Diff line number Diff line change
@@ -265,41 +265,7 @@ impl WorkspaceService for WorkspaceRpcService {
_context: context::Context,
ws_req: StartWorkspaceRequest,
) -> Result<ContainerInfo, ApiError> {
let socket = self.server.get_podman_socket(&ws_req.osuser).await?;
let client = unix_client();
let url = Uri::new(
&socket,
&format!("/containers/{}/start", ws_req.workspace_name),
);
let req = hyper::Request::builder()
.method(hyper::Method::POST)
.uri(url)
.body(Full::<Bytes>::new(Bytes::new()))?;
let resp = client.request(req).await?;
let status = resp.status();
let body = resp.collect().await?.to_bytes();
if status != 204 {
let err = String::from_utf8(body.to_vec())?;
return Err(anyhow!("start container error: {err}").into());
}

let url = Uri::new(
&socket,
&format!("/containers/{}/json", ws_req.workspace_name),
);
let req = hyper::Request::builder()
.method(hyper::Method::GET)
.uri(url)
.body(Full::<Bytes>::new(Bytes::new()))?;
let resp = client.request(req).await?;
let status = resp.status();
let body = resp.collect().await?.to_bytes();
if status != 200 {
let err = String::from_utf8(body.to_vec())?;
return Err(anyhow!("get container info error: {err}").into());
}
let info: ContainerInfo = serde_json::from_slice(&body)?;
Ok(info)
self.server.start_workspace(ws_req).await
}

async fn delete_workspace(
2 changes: 1 addition & 1 deletion pkg/image/fluxbox/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM debian:12

USER root
COPY ../../../target/release/lapdev-guest-agent /lapdev-guest-agent
COPY ../../../target/x86_64-unknown-linux-musl/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
2 changes: 1 addition & 1 deletion pkg/image/gnome/Dockerfile
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ RUN apt-get update && \
/cleanup

USER root
COPY ../../../target/release/lapdev-guest-agent /lapdev-guest-agent
COPY ../../../target/x86_64-unknown-linux-musl/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
2 changes: 1 addition & 1 deletion pkg/image/go/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM mcr.microsoft.com/devcontainers/go:latest

USER root
COPY ../../../target/release/lapdev-guest-agent /lapdev-guest-agent
COPY ../../../target/x86_64-unknown-linux-musl/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
2 changes: 1 addition & 1 deletion pkg/image/javascript/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM mcr.microsoft.com/devcontainers/javascript-node:latest

USER root
COPY ../../../target/release/lapdev-guest-agent /lapdev-guest-agent
COPY ../../../target/x86_64-unknown-linux-musl/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
16 changes: 16 additions & 0 deletions pkg/image/lapdev/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM debian:12

RUN apt update
RUN apt install -y curl build-essential git vim podman pkg-config libssl-dev libz-dev fuse-overlayfs s3cmd

RUN curl https://sh.rustup.rs -sSf | bash -s -- -y
RUN $HOME/.cargo/bin/rustup target add wasm32-unknown-unknown
RUN $HOME/.cargo/bin/rustup target add x86_64-unknown-linux-musl
RUN $HOME/.cargo/bin/cargo install trunk && rm -rf $HOME/.cargo/registry/

COPY ../../../target/x86_64-unknown-linux-musl/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 && rm /install_guest_agent.sh

EXPOSE 8080
2 changes: 1 addition & 1 deletion pkg/image/python/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM mcr.microsoft.com/devcontainers/python:latest

USER root
COPY ../../../target/release/lapdev-guest-agent /lapdev-guest-agent
COPY ../../../target/x86_64-unknown-linux-musl/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
2 changes: 1 addition & 1 deletion pkg/image/rust/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM mcr.microsoft.com/devcontainers/rust:latest

USER root
COPY ../../../target/release/lapdev-guest-agent /lapdev-guest-agent
COPY ../../../target/x86_64-unknown-linux-musl/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
2 changes: 1 addition & 1 deletion pkg/image/typescript/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM mcr.microsoft.com/devcontainers/typescript-node:latest

USER root
COPY ../../../target/release/lapdev-guest-agent /lapdev-guest-agent
COPY ../../../target/x86_64-unknown-linux-musl/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
2 changes: 1 addition & 1 deletion pkg/image/universal/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM mcr.microsoft.com/devcontainers/universal:latest

USER root
COPY ../../../target/release/lapdev-guest-agent /lapdev-guest-agent
COPY ../../../target/x86_64-unknown-linux-musl/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

0 comments on commit 538e8dd

Please sign in to comment.