33 Commits

Author SHA1 Message Date
663feba85d feat: can use ssh sock
Some checks failed
continuous-integration/drone/push Build is failing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-27 22:18:05 +01:00
e5b3e1b62a feat: with username
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-27 22:15:11 +01:00
0f7ed2b6f4 feat: with git name
Some checks failed
continuous-integration/drone/push Build is failing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-27 22:04:39 +01:00
c391482874 feat: with sync
Some checks failed
continuous-integration/drone/push Build encountered an error
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-27 21:36:46 +01:00
b879da4d2f feat: with update deployment
Some checks failed
continuous-integration/drone/push Build is failing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-27 21:34:23 +01:00
a900ebae54 feat: with registry
Some checks failed
continuous-integration/drone/push Build is failing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-27 21:24:45 +01:00
c893dc9005 feat: with before test
Some checks failed
continuous-integration/drone/push Build is failing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-27 20:31:29 +01:00
30587b2f97 feat: with impl into
Some checks failed
continuous-integration/drone/push Build is failing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-27 20:19:50 +01:00
bda242422d feat: with arc
Some checks failed
continuous-integration/drone/push Build is failing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-27 20:11:49 +01:00
999d81bb7a feat: with &mut service
Some checks failed
continuous-integration/drone/push Build encountered an error
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-27 19:20:41 +01:00
93e73cc66e feat: with mutex
Some checks failed
continuous-integration/drone/push Build is failing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-27 19:14:31 +01:00
52266599e2 feat: with rust service impl
Some checks failed
continuous-integration/drone/push Build is failing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-27 18:11:58 +01:00
85ad929d80 feat: with src
Some checks failed
continuous-integration/drone/push Build is failing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-27 14:15:43 +01:00
a0acb54896 feat: with sqlx
Some checks failed
continuous-integration/drone/push Build is failing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-27 13:52:11 +01:00
2a988c33a4 feat: forgot async_trait
Some checks failed
continuous-integration/drone/push Build is failing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-27 13:44:44 +01:00
015cb6b23a feat: with cargo clean
Some checks failed
continuous-integration/drone/push Build is failing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-27 13:42:13 +01:00
8869b75072 feat: with extensions
Some checks failed
continuous-integration/drone/push Build is failing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-27 13:25:31 +01:00
3c28b30f8f feat: extract arch
Some checks failed
continuous-integration/drone/push Build is failing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-26 22:46:34 +01:00
7a1ad63b57 feat: with full support for rust services
Some checks failed
continuous-integration/drone/push Build is failing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-26 22:19:34 +01:00
80782e70f9 chore: fmt
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-25 23:16:21 +01:00
3939940c01 chore: fmt
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-25 23:14:38 +01:00
82ccdefd93 feat: with middleware
Some checks failed
continuous-integration/drone/push Build is failing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-25 23:10:09 +01:00
3e9a840851 chore(deps): update rust crate async-scoped to 0.8.0
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
2023-11-25 21:26:21 +00:00
455660f1e0 chore(deps): update rust crate futures to 0.3.29
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
2023-11-25 21:01:05 +00:00
f5ba46186b feat: with logs
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-25 21:49:04 +01:00
8cf148726a feat: add cuddle ci draft
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-25 21:41:17 +01:00
e29615cb05 feat: with offline mode
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-25 14:00:17 +01:00
cdd13283e0 feat: with cargo clean
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-25 13:50:43 +01:00
a3d92cdde3 feat: without export
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-25 13:47:17 +01:00
e4fc1cc834 feat: with output
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-25 13:33:54 +01:00
d6af354776 feat: with nested mold
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-25 12:55:56 +01:00
0524b2e0bf feat: fix name
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-25 12:48:57 +01:00
5c69c3fa16 feat: with mold
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-11-25 12:47:43 +01:00
22 changed files with 1202 additions and 92 deletions

View File

@@ -102,6 +102,8 @@ steps:
CUDDLE_SECRETS_PROVIDER: 1password
CUDDLE_ONE_PASSWORD_DOT_ENV: ".env.ci"
CUDDLE_SSH_AGENT: "true"
GIT_PASSWORD:
from_secret: gitea_token
CI_PREFIX: "/mnt/ci/ci"
CUDDLE_PLEASE_TOKEN:
from_secret: cuddle_please_token
@@ -167,4 +169,4 @@ volumes:
- name: dockersock
temp: {}
- name: ci
temp: {}
temp: {}

13
Cargo.lock generated
View File

@@ -320,6 +320,19 @@ dependencies = [
"typenum",
]
[[package]]
name = "cuddle-ci"
version = "0.2.0"
dependencies = [
"async-trait",
"clap",
"dagger-rust",
"dagger-sdk",
"eyre",
"futures",
"tokio",
]
[[package]]
name = "cuddle-components"
version = "0.1.0"

View File

@@ -23,3 +23,7 @@ eyre = "0.6.9"
tokio = "1.34.0"
dotenv = "0.15.0"
async-trait = "0.1.74"
color-eyre = "*"
clap = {version = "4", features = ["derive"]}
futures = "0.3.29"
async-scoped = { version = "0.8.0", features = ["tokio", "use-tokio"] }

View File

@@ -1,5 +1,4 @@
use std::path::PathBuf;
use std::sync::Arc;
use clap::Args;
use clap::Parser;
@@ -62,10 +61,10 @@ async fn main() -> eyre::Result<()> {
let _ = dotenv::dotenv();
let _ = color_eyre::install();
let client = dagger_sdk::connect().await?;
let cli = Command::parse();
let client = dagger_sdk::connect().await?;
match &cli.commands {
Commands::Local { command } => match command {
LocalCommands::Test => {
@@ -107,7 +106,6 @@ async fn main() -> eyre::Result<()> {
}
mod please_release {
use std::sync::Arc;
use dagger_cuddle_please::{models::CuddlePleaseSrcArgs, DaggerCuddlePleaseAction};
@@ -136,7 +134,7 @@ mod please_release {
}
mod test {
use std::{path::PathBuf, sync::Arc};
use std::path::PathBuf;
use dagger_rust::build::RustVersion;

View File

@@ -0,0 +1,23 @@
[package]
name = "cuddle-ci"
version.workspace = true
edition.workspace = true
license.workspace = true
authors.workspace = true
readme.workspace = true
repository.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dagger-rust.workspace = true
dagger-sdk.workspace = true
eyre.workspace = true
clap.workspace = true
async-trait.workspace = true
futures.workspace = true
tokio.workspace = true
[dev-dependencies]
tokio.workspace = true

166
crates/cuddle-ci/src/cli.rs Normal file
View File

@@ -0,0 +1,166 @@
use std::sync::Arc;
use async_trait::async_trait;
use tokio::sync::Mutex;
pub struct CuddleCI {
pr_action: Arc<Mutex<dyn PullRequestAction + Send + Sync>>,
main_action: Arc<Mutex<dyn MainAction + Send + Sync>>,
release_action: Arc<Mutex<dyn ReleaseAction + Send + Sync>>,
}
impl CuddleCI {
pub fn new(
pr: Arc<Mutex<dyn PullRequestAction + Send + Sync>>,
main: Arc<Mutex<dyn MainAction + Send + Sync>>,
release: Arc<Mutex<dyn ReleaseAction + Send + Sync>>,
) -> Self {
Self {
pr_action: pr,
main_action: main,
release_action: release,
}
}
pub fn with_pull_request(
&mut self,
pr: Arc<Mutex<dyn PullRequestAction + Send + Sync>>,
) -> &mut Self {
self.pr_action = pr;
self
}
pub fn with_main(&mut self, main: Arc<Mutex<dyn MainAction + Send + Sync>>) -> &mut Self {
self.main_action = main;
self
}
pub fn with_release(
&mut self,
release: Arc<Mutex<dyn ReleaseAction + Send + Sync>>,
) -> &mut Self {
self.release_action = release;
self
}
pub async fn execute(
&mut self,
args: impl IntoIterator<Item = impl Into<String>>,
) -> eyre::Result<()> {
let matches = clap::Command::new("cuddle-ci")
.about("is a wrapper around common CI actions")
.subcommand(clap::Command::new("pr"))
.subcommand(clap::Command::new("main"))
.subcommand(clap::Command::new("release"))
.subcommand_required(true)
.try_get_matches_from(args.into_iter().map(|a| a.into()).collect::<Vec<String>>())?;
match matches.subcommand() {
Some((name, args)) => match (name, args) {
("pr", _args) => {
eprintln!("starting pr validate");
self.pr_action.lock().await.execute_pull_request().await?;
eprintln!("finished pr validate");
}
("main", _args) => {
eprintln!("starting main validate");
self.main_action.lock().await.execute_main().await?;
eprintln!("finished main validate");
}
("release", _args) => {
eprintln!("starting release validate");
self.release_action.lock().await.execute_release().await?;
eprintln!("finished release validate");
}
(command_name, _) => {
eyre::bail!("command is not recognized: {}", command_name)
}
},
None => eyre::bail!("command required a subcommand [pr, main, release] etc."),
}
Ok(())
}
}
impl Default for CuddleCI {
fn default() -> Self {
Self::new(
Arc::new(Mutex::new(DefaultPullRequestAction {})),
Arc::new(Mutex::new(DefaultMainAction {})),
Arc::new(Mutex::new(DefaultReleaseAction {})),
)
}
}
#[async_trait]
pub trait PullRequestAction {
async fn execute_pull_request(&self) -> eyre::Result<()> {
eprintln!("validate pull request: noop");
Ok(())
}
}
pub struct DefaultPullRequestAction {}
#[async_trait]
impl PullRequestAction for DefaultPullRequestAction {}
#[async_trait]
pub trait MainAction {
async fn execute_main(&self) -> eyre::Result<()> {
eprintln!("validate main: noop");
Ok(())
}
}
pub struct DefaultMainAction {}
#[async_trait]
impl MainAction for DefaultMainAction {}
#[async_trait]
pub trait ReleaseAction {
async fn execute_release(&self) -> eyre::Result<()> {
eprintln!("validate release: noop");
Ok(())
}
}
pub struct DefaultReleaseAction {}
#[async_trait]
impl ReleaseAction for DefaultReleaseAction {}
#[cfg(test)]
mod test {
use super::*;
#[tokio::test]
async fn test_can_call_default() -> eyre::Result<()> {
CuddleCI::default().execute(["cuddle-ci", "pr"]).await?;
Ok(())
}
#[tokio::test]
async fn test_fails_on_no_command() -> eyre::Result<()> {
let res = CuddleCI::default().execute(["cuddle-ci"]).await;
assert!(res.is_err());
Ok(())
}
#[tokio::test]
async fn test_fails_on_wrong_command() -> eyre::Result<()> {
let res = CuddleCI::default()
.execute(["cuddle-ci", "something"])
.await;
assert!(res.is_err());
Ok(())
}
}

View File

@@ -0,0 +1,54 @@
use async_trait::async_trait;
use dagger_sdk::Container;
use std::{future::Future, pin::Pin, sync::Arc};
pub type DynMiddleware = Arc<dyn DaggerMiddleware + Send + Sync>;
#[async_trait]
pub trait DaggerMiddleware {
async fn handle(
&self,
container: dagger_sdk::Container,
) -> eyre::Result<dagger_sdk::Container> {
Ok(container)
}
}
pub struct DaggerMiddlewareFn<F>
where
F: Fn(Container) -> Pin<Box<dyn Future<Output = eyre::Result<Container>> + Send>>,
{
pub func: F,
}
pub fn middleware<F>(func: F) -> Box<DaggerMiddlewareFn<F>>
where
F: Fn(Container) -> Pin<Box<dyn Future<Output = eyre::Result<Container>> + Send>>,
{
Box::new(DaggerMiddlewareFn { func })
}
#[async_trait]
impl<F> DaggerMiddleware for DaggerMiddlewareFn<F>
where
F: Fn(Container) -> Pin<Box<dyn Future<Output = eyre::Result<Container>> + Send>> + Send + Sync,
{
async fn handle(&self, container: Container) -> eyre::Result<Container> {
// Call the closure stored in the struct
(self.func)(container).await
}
}
#[cfg(test)]
mod test {
use futures::FutureExt;
use super::*;
#[tokio::test]
async fn can_add_middleware() -> eyre::Result<()> {
middleware(|c| async move { Ok(c) }.boxed());
Ok(())
}
}

View File

@@ -0,0 +1,5 @@
pub mod cli;
pub use cli::*;
pub mod dagger_middleware;
pub mod rust_service;

View File

@@ -0,0 +1,455 @@
use std::{path::PathBuf, sync::Arc};
use async_trait::async_trait;
use dagger_rust::source::RustSource;
use dagger_sdk::Container;
use futures::{stream, StreamExt};
use crate::{
dagger_middleware::{DaggerMiddleware, DynMiddleware},
MainAction, PullRequestAction,
};
use self::architecture::{Architecture, Os};
#[derive(Clone)]
pub enum RustServiceStage {
BeforeDeps(DynMiddleware),
AfterDeps(DynMiddleware),
BeforeBase(DynMiddleware),
AfterBase(DynMiddleware),
BeforeBuild(DynMiddleware),
AfterBuild(DynMiddleware),
BeforePackage(DynMiddleware),
AfterPackage(DynMiddleware),
BeforeRelease(DynMiddleware),
AfterRelease(DynMiddleware),
}
#[derive(Clone)]
pub struct RustService {
client: dagger_sdk::Query,
base_image: Option<dagger_sdk::Container>,
final_image: Option<dagger_sdk::Container>,
stages: Vec<RustServiceStage>,
source: Option<PathBuf>,
crates: Vec<String>,
bin_name: String,
arch: Option<Architecture>,
os: Option<Os>,
}
impl From<dagger_sdk::Query> for RustService {
fn from(value: dagger_sdk::Query) -> Self {
Self {
client: value,
base_image: None,
final_image: None,
stages: Vec::new(),
source: None,
crates: Vec::new(),
bin_name: String::new(),
arch: None,
os: None,
}
}
}
impl RustService {
pub async fn new() -> eyre::Result<Self> {
Ok(Self {
client: dagger_sdk::connect().await?,
base_image: None,
final_image: None,
stages: Vec::new(),
source: None,
crates: Vec::new(),
bin_name: String::new(),
arch: None,
os: None,
})
}
pub fn with_base_image(&mut self, base: dagger_sdk::Container) -> &mut Self {
self.base_image = Some(base);
self
}
pub fn with_stage(&mut self, stage: RustServiceStage) -> &mut Self {
self.stages.push(stage);
self
}
pub fn with_source(&mut self, path: impl Into<PathBuf>) -> &mut Self {
self.source = Some(path.into());
self
}
pub fn with_bin_name(&mut self, bin_name: impl Into<String>) -> &mut Self {
self.bin_name = bin_name.into();
self
}
pub fn with_crates(
&mut self,
crates: impl IntoIterator<Item = impl Into<String>>,
) -> &mut Self {
self.crates = crates.into_iter().map(|c| c.into()).collect();
self
}
pub fn with_arch(&mut self, arch: Architecture) -> &mut Self {
self.arch = Some(arch);
self
}
pub fn with_os(&mut self, os: Os) -> &mut Self {
self.os = Some(os);
self
}
fn get_src(&self) -> PathBuf {
self.source
.clone()
.unwrap_or(std::env::current_dir().unwrap())
}
fn get_arch(&self) -> Architecture {
self.arch
.clone()
.unwrap_or_else(|| match std::env::consts::ARCH {
"x86" | "x86_64" | "amd64" => Architecture::Amd64,
"arm" => Architecture::Arm64,
arch => panic!("unsupported architecture: {arch}"),
})
}
fn get_os(&self) -> Os {
self.os
.clone()
.unwrap_or_else(|| match std::env::consts::OS {
"linux" => Os::Linux,
"macos" => Os::MacOS,
os => panic!("unsupported os: {os}"),
})
}
async fn run_stage(
&self,
stages: impl IntoIterator<Item = &Arc<dyn DaggerMiddleware + Send + Sync>>,
container: Container,
) -> eyre::Result<Container> {
let before_deps_stream = stream::iter(stages.into_iter().map(Ok));
let res = StreamExt::fold(before_deps_stream, Ok(container), |base, m| async move {
match (base, m) {
(Ok(base), Ok(m)) => m.handle(base).await,
(_, Err(e)) | (Err(e), _) => eyre::bail!("failed with {e}"),
}
})
.await?;
Ok(res)
}
pub async fn build_base(&self) -> eyre::Result<Container> {
let rust_src = RustSource::new(self.client.clone());
let (src, dep_src) = rust_src
.get_rust_src(Some(&self.get_src()), self.crates.clone())
.await?;
let base_image = self
.base_image
.clone()
.unwrap_or(self.client.container().from("rustlang/rust:nightly"));
let before_deps = self
.stages
.iter()
.filter_map(|s| match s {
RustServiceStage::BeforeDeps(middleware) => Some(middleware),
_ => None,
})
.collect::<Vec<_>>();
let image = self.run_stage(before_deps, base_image).await?;
let after_deps = self
.stages
.iter()
.filter_map(|s| match s {
RustServiceStage::AfterDeps(m) => Some(m),
_ => None,
})
.collect::<Vec<_>>();
let image = self.run_stage(after_deps, image).await?;
let before_base = self
.stages
.iter()
.filter_map(|s| match s {
RustServiceStage::BeforeBase(m) => Some(m),
_ => None,
})
.collect::<Vec<_>>();
let image = self.run_stage(before_base, image).await?;
let cache = self.client.cache_volume("rust_target_cache");
let rust_prebuild = image
.with_workdir("/mnt/src")
.with_directory("/mnt/src", dep_src)
.with_exec(vec!["cargo", "build", "--release", "--bin", &self.bin_name])
.with_mounted_cache("/mnt/src/target/", cache);
let incremental_dir = rust_src
.get_rust_target_src(&self.get_src(), rust_prebuild.clone(), self.crates.clone())
.await?;
let rust_with_src = image
.with_workdir("/mnt/src")
.with_directory(
"/usr/local/cargo",
rust_prebuild.directory("/usr/local/cargo"),
)
.with_directory("/mnt/src/target", incremental_dir)
.with_directory("/mnt/src/", src);
let after_base = self
.stages
.iter()
.filter_map(|s| match s {
RustServiceStage::AfterBase(m) => Some(m),
_ => None,
})
.collect::<Vec<_>>();
let image = self.run_stage(after_base, rust_with_src).await?;
Ok(image)
}
pub async fn build_release(&self) -> eyre::Result<Container> {
let base = self.build_base().await?;
let before_build = self
.stages
.iter()
.filter_map(|s| match s {
RustServiceStage::BeforeBuild(m) => Some(m),
_ => None,
})
.collect::<Vec<_>>();
let base = self.run_stage(before_build, base).await?;
let binary_build =
base.with_exec(vec!["cargo", "build", "--release", "--bin", &self.bin_name]);
let after_build = self
.stages
.iter()
.filter_map(|s| match s {
RustServiceStage::AfterBuild(m) => Some(m),
_ => None,
})
.collect::<Vec<_>>();
let binary_build = self.run_stage(after_build, binary_build).await?;
let dest = self
.final_image
.clone()
.unwrap_or(self.client.container().from("debian:bullseye"));
let before_package = self
.stages
.iter()
.filter_map(|s| match s {
RustServiceStage::BeforePackage(m) => Some(m),
_ => None,
})
.collect::<Vec<_>>();
let dest = self.run_stage(before_package, dest).await?;
let final_image = dest.with_workdir("/mnt/app").with_file(
format!("/usr/local/bin/{}", self.bin_name),
binary_build.file(format!("/mnt/src/target/release/{}", self.bin_name)),
);
let after_package = self
.stages
.iter()
.filter_map(|s| match s {
RustServiceStage::AfterPackage(m) => Some(m),
_ => None,
})
.collect::<Vec<_>>();
let final_image = self.run_stage(after_package, final_image).await?;
Ok(final_image)
}
pub async fn build_test(&self) -> eyre::Result<()> {
let base = self.build_base().await?;
let before_build = self
.stages
.iter()
.filter_map(|s| match s {
RustServiceStage::BeforeBuild(m) => Some(m),
_ => None,
})
.collect::<Vec<_>>();
let base = self.run_stage(before_build, base).await?;
base.with_exec(vec!["cargo", "test", "--release"])
.sync()
.await?;
Ok(())
}
}
#[async_trait]
impl PullRequestAction for RustService {
async fn execute_pull_request(&self) -> eyre::Result<()> {
self.build_test().await?;
Ok(())
}
}
#[async_trait]
impl MainAction for RustService {
async fn execute_main(&self) -> eyre::Result<()> {
let container = self.build_release().await?;
let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs();
container
.publish(format!(
"docker.io/kasperhermansen/{}:main-{}",
self.bin_name, timestamp,
))
.await?;
let update_deployments_docker_image =
"docker.io/kasperhermansen/update-deployment:1690401410";
let dep = self
.client
.container()
.from(update_deployments_docker_image);
let dep = if let Some(sock) = std::env::var("SSH_AUTH_SOCK").ok() {
dep.with_unix_socket("/tmp/ssh_sock", self.client.host().unix_socket(sock))
.with_env_variable("SSH_AUTH_SOCK", "/tmp/ssh_sock")
} else {
dep.with_env_variable("GIT_USERNAME", "kjuulh")
.with_env_variable(
"GIT_PASSWORD",
std::env::var("GIT_PASSWORD").expect("GIT_PASSWORD to be set"),
)
};
dep.with_exec(vec![
"update-deployment",
"--repo",
&format!(
"https://git.front.kjuulh.io/kjuulh/{}-deployment.git",
self.bin_name
),
"--service",
&self.bin_name,
"--image",
&format!("kasperhermansen/{}:main-{}", self.bin_name, timestamp),
])
.sync()
.await?;
Ok(())
}
}
pub mod architecture {
#[derive(Debug, Clone)]
pub enum Architecture {
Amd64,
Arm64,
}
#[derive(Debug, Clone)]
pub enum Os {
Linux,
MacOS,
}
}
mod apt;
mod cargo_binstall;
mod cargo_clean;
mod clap_sanity_test;
mod mold;
mod sqlx;
pub mod extensions {
pub use super::apt::*;
pub use super::cargo_binstall::*;
pub use super::cargo_clean::*;
pub use super::clap_sanity_test::*;
pub use super::mold::*;
pub use super::sqlx::*;
}
#[cfg(test)]
mod test {
use futures::FutureExt;
use crate::{
dagger_middleware::middleware,
rust_service::{
apt::AptExt,
architecture::{Architecture, Os},
cargo_binstall::CargoBInstallExt,
clap_sanity_test::ClapSanityTestExt,
mold::MoldActionExt,
RustService, RustServiceStage,
},
};
#[tokio::test]
async fn test_can_build_rust() -> eyre::Result<()> {
let client = dagger_sdk::connect().await?;
let root_dir = std::path::PathBuf::from("../../").canonicalize()?;
let container = RustService::from(client.clone())
.with_arch(Architecture::Amd64)
.with_os(Os::Linux)
.with_source(root_dir)
.with_bin_name("ci")
.with_crates(["crates/*", "examples/*", "ci"])
.with_apt(&["git"])
.with_cargo_binstall("latest", ["sqlx-cli"])
.with_mold("2.3.3")
.with_stage(RustServiceStage::BeforeDeps(middleware(|c| {
async move {
// Noop
Ok(c)
}
.boxed()
})))
.with_clap_sanity_test()
.build_release()
.await?;
container.sync().await?;
Ok(())
}
}

View File

@@ -0,0 +1,56 @@
use std::sync::Arc;
use async_trait::async_trait;
use dagger_sdk::Container;
use crate::dagger_middleware::DaggerMiddleware;
use super::RustService;
pub struct Apt {
deps: Vec<String>,
}
impl Apt {
pub fn new() -> Self {
Self { deps: Vec::new() }
}
pub fn add(mut self, dep_name: impl Into<String>) -> Self {
self.deps.push(dep_name.into());
self
}
pub fn extend(mut self, deps: &[&str]) -> Self {
self.deps.extend(deps.iter().map(|s| s.to_string()));
self
}
}
#[async_trait]
impl DaggerMiddleware for Apt {
async fn handle(&self, container: Container) -> eyre::Result<Container> {
let mut deps = vec!["apt", "install", "-y"];
deps.extend(self.deps.iter().map(|s| s.as_str()));
let c = container.with_exec(vec!["apt", "update"]).with_exec(deps);
Ok(c)
}
}
pub trait AptExt {
fn with_apt(&mut self, deps: &[&str]) -> &mut Self {
self
}
}
impl AptExt for RustService {
fn with_apt(&mut self, deps: &[&str]) -> &mut Self {
self.with_stage(super::RustServiceStage::BeforeDeps(Arc::new(
Apt::new().extend(deps),
)))
}
}

View File

@@ -0,0 +1,106 @@
use std::sync::Arc;
use async_trait::async_trait;
use dagger_sdk::Container;
use crate::dagger_middleware::DaggerMiddleware;
use super::{
architecture::{Architecture, Os},
RustService,
};
pub struct CargoBInstall {
arch: Architecture,
os: Os,
version: String,
crates: Vec<String>,
}
impl CargoBInstall {
pub fn new(
arch: Architecture,
os: Os,
version: impl Into<String>,
crates: impl Into<Vec<String>>,
) -> Self {
Self {
arch,
os,
version: version.into(),
crates: crates.into(),
}
}
fn get_arch(&self) -> String {
match self.arch {
Architecture::Amd64 => "x86_64",
Architecture::Arm64 => "armv7",
}
.into()
}
fn get_os(&self) -> String {
match self.os {
Os::Linux => "linux",
Os::MacOS => "darwin",
}
.into()
}
pub fn get_download_url(&self) -> String {
format!("https://github.com/cargo-bins/cargo-binstall/releases/{}/download/cargo-binstall-{}-unknown-{}-musl.tgz", self.version, self.get_arch(), self.get_os())
}
pub fn get_archive(&self) -> String {
format!(
"cargo-binstall-{}-unknown-{}-musl.tgz",
self.get_arch(),
self.get_os()
)
}
}
#[async_trait]
impl DaggerMiddleware for CargoBInstall {
async fn handle(&self, container: Container) -> eyre::Result<Container> {
let c = container
.with_exec(vec!["wget", &self.get_download_url()])
.with_exec(vec!["tar", "-xvf", &self.get_archive()])
.with_exec(
"mv cargo-binstall /usr/local/cargo/bin"
.split_whitespace()
.collect(),
);
let c = self.crates.iter().cloned().fold(c, |acc, item| {
acc.with_exec(vec!["cargo", "binstall", &item, "-y"])
});
Ok(c)
}
}
pub trait CargoBInstallExt {
fn with_cargo_binstall(
&mut self,
version: impl Into<String>,
crates: impl IntoIterator<Item = impl Into<String>>,
) -> &mut Self {
self
}
}
impl CargoBInstallExt for RustService {
fn with_cargo_binstall(
&mut self,
version: impl Into<String>,
crates: impl IntoIterator<Item = impl Into<String>>,
) -> &mut Self {
let crates: Vec<String> = crates.into_iter().map(|s| s.into()).collect();
self.with_stage(super::RustServiceStage::BeforeDeps(Arc::new(
CargoBInstall::new(self.get_arch(), self.get_os(), version, crates),
)))
}
}

View File

@@ -0,0 +1,39 @@
use std::sync::Arc;
use async_trait::async_trait;
use dagger_sdk::Container;
use crate::dagger_middleware::DaggerMiddleware;
use super::RustService;
pub struct CargoClean;
impl CargoClean {
pub fn new() -> Self {
Self {}
}
}
#[async_trait]
impl DaggerMiddleware for CargoClean {
async fn handle(&self, container: Container) -> eyre::Result<Container> {
Ok(container.with_exec(vec!["cargo", "clean"]))
}
}
pub trait CargoCleanExt {
fn with_cargo_clean(&mut self) -> &mut Self {
self
}
}
impl CargoCleanExt for RustService {
fn with_cargo_clean(&mut self) -> &mut Self {
self.with_stage(super::RustServiceStage::BeforeBuild(Arc::new(
CargoClean::new(),
)));
self
}
}

View File

@@ -0,0 +1,43 @@
use std::sync::Arc;
use async_trait::async_trait;
use dagger_sdk::Container;
use crate::dagger_middleware::DaggerMiddleware;
use super::RustService;
pub struct ClapSanityTest {
bin_name: String,
}
impl ClapSanityTest {
pub fn new(bin_name: impl Into<String>) -> Self {
Self {
bin_name: bin_name.into(),
}
}
}
#[async_trait]
impl DaggerMiddleware for ClapSanityTest {
async fn handle(&self, container: Container) -> eyre::Result<Container> {
Ok(container.with_exec(vec![&self.bin_name, "--help"]))
}
}
pub trait ClapSanityTestExt {
fn with_clap_sanity_test(&mut self) -> &mut Self {
self
}
}
impl ClapSanityTestExt for RustService {
fn with_clap_sanity_test(&mut self) -> &mut Self {
self.with_stage(super::RustServiceStage::AfterPackage(Arc::new(
ClapSanityTest::new(&self.bin_name),
)));
self
}
}

View File

@@ -0,0 +1,104 @@
use std::sync::Arc;
use async_trait::async_trait;
use crate::dagger_middleware::DaggerMiddleware;
use super::{
architecture::{Architecture, Os},
RustService,
};
pub struct MoldInstall {
arch: Architecture,
os: Os,
version: String,
}
impl MoldInstall {
pub fn new(arch: Architecture, os: Os, version: impl Into<String>) -> Self {
Self {
arch,
os,
version: version.into(),
}
}
fn get_arch(&self) -> String {
match self.arch {
Architecture::Amd64 => "x86_64",
Architecture::Arm64 => "arm",
}
.into()
}
fn get_os(&self) -> String {
match &self.os {
Os::Linux => "linux",
o => todo!("os not implemented for mold: {:?}", o),
}
.into()
}
pub fn get_download_url(&self) -> String {
format!(
"https://github.com/rui314/mold/releases/download/v{}/mold-{}-{}-{}.tar.gz",
self.version,
self.version,
self.get_arch(),
self.get_os()
)
}
pub fn get_folder(&self) -> String {
format!(
"mold-{}-{}-{}",
self.version,
self.get_arch(),
self.get_os()
)
}
pub fn get_archive_name(&self) -> String {
format!(
"mold-{}-{}-{}.tar.gz",
self.version,
self.get_arch(),
self.get_os()
)
}
}
#[async_trait]
impl DaggerMiddleware for MoldInstall {
async fn handle(
&self,
container: dagger_sdk::Container,
) -> eyre::Result<dagger_sdk::Container> {
println!("installing mold");
let c = container
.with_exec(vec!["wget", &self.get_download_url()])
.with_exec(vec!["tar", "-xvf", &self.get_archive_name()])
.with_exec(vec![
"mv",
&format!("{}/bin/mold", self.get_folder()),
"/usr/bin/mold",
]);
Ok(c)
}
}
pub trait MoldActionExt {
fn with_mold(&mut self, version: impl Into<String>) -> &mut Self {
self
}
}
impl MoldActionExt for RustService {
fn with_mold(&mut self, version: impl Into<String>) -> &mut Self {
self.with_stage(super::RustServiceStage::AfterDeps(Arc::new(
MoldInstall::new(self.get_arch(), self.get_os(), version),
)))
}
}

View File

@@ -0,0 +1,45 @@
use std::sync::Arc;
use async_trait::async_trait;
use dagger_sdk::Container;
use crate::dagger_middleware::DaggerMiddleware;
use super::RustService;
pub struct Sqlx {
client: dagger_sdk::Query,
}
impl Sqlx {
pub fn new(client: dagger_sdk::Query) -> Self {
Self { client }
}
}
#[async_trait]
impl DaggerMiddleware for Sqlx {
async fn handle(&self, container: Container) -> eyre::Result<Container> {
let src = self.client.host().directory(".sqlx/");
Ok(container
.with_directory(".sqlx", src)
.with_env_variable("SQLX_OFFLINE", "true"))
}
}
pub trait SqlxExt {
fn with_sqlx(&mut self) -> &mut Self {
self
}
}
impl SqlxExt for RustService {
fn with_sqlx(&mut self) -> &mut Self {
self.with_stage(super::RustServiceStage::BeforeBuild(Arc::new(Sqlx::new(
self.client.clone(),
))));
self
}
}

View File

@@ -1,4 +1,4 @@
use std::sync::{Arc, Mutex};
use std::sync::{Arc};
use models::{CuddlePleaseArgs, CuddlePleaseSrcArgs};
use traits::CuddlePlease;

View File

@@ -44,6 +44,9 @@ impl RustBuild {
.from(rust_version.to_string())
.with_exec(vec!["rustup", "target", "add", &target.to_string()])
.with_exec(vec!["apt", "update"])
.with_exec(vec!["wget", "https://github.com/rui314/mold/releases/latest/download/mold-2.3.3-x86_64-linux.tar.gz"])
.with_exec("tar -xvf mold-2.3.3-x86_64-linux.tar.gz".split_whitespace().collect())
.with_exec("mv mold-2.3.3-x86_64-linux/bin/mold /usr/bin/mold".split_whitespace().collect())
.with_exec(deps);
let target_cache = self.client.cache_volume(format!(
@@ -94,92 +97,87 @@ impl RustBuild {
let mut containers = Vec::new();
for container_image in images {
let container = match &container_image {
SlimImage::Debian { image, deps, .. } => {
let target = BuildTarget::from_target(&container_image);
let container =
match &container_image {
SlimImage::Debian { image, deps, .. } => {
let target = BuildTarget::from_target(&container_image);
let build_container = self
.build(
source_path.clone(),
&rust_version,
let build_container = self
.build(
source_path.clone(),
&rust_version,
BuildTarget::from_target(&container_image),
BuildProfile::Release,
crates,
extra_deps,
)
.await?;
let bin = build_container
.with_env_variable("SQLX_OFFLINE", "true")
.with_exec(vec!["cargo", "clean"])
.with_exec(vec![
"cargo",
"build",
"--target",
&target.to_string(),
"--release",
"-p",
bin_name,
])
.file(format!("target/{}/release/{}", target.to_string(), bin_name));
self.build_debian_image(
bin,
image,
BuildTarget::from_target(&container_image),
BuildProfile::Release,
crates,
extra_deps,
)
.await?;
let bin = build_container
.with_exec(vec![
"cargo",
"build",
"--target",
&target.to_string(),
"--release",
"-p",
deps.iter()
.map(|d| d.as_str())
.collect::<Vec<&str>>()
.as_slice(),
bin_name,
])
.file(format!(
"target/{}/release/{}",
target.to_string(),
bin_name
));
)
.await?
}
SlimImage::Alpine { image, deps, .. } => {
let target = BuildTarget::from_target(&container_image);
self.build_debian_image(
bin,
image,
BuildTarget::from_target(&container_image),
deps.iter()
.map(|d| d.as_str())
.collect::<Vec<&str>>()
.as_slice(),
bin_name,
)
.await?
}
SlimImage::Alpine { image, deps, .. } => {
let target = BuildTarget::from_target(&container_image);
let build_container = self
.build(
source_path.clone(),
&rust_version,
BuildTarget::from_target(&container_image),
BuildProfile::Release,
crates,
extra_deps,
)
.await?;
let build_container = self
.build(
source_path.clone(),
&rust_version,
let bin = build_container
.with_exec(vec![
"cargo",
"build",
"--target",
&target.to_string(),
"--release",
"-p",
bin_name,
])
.file(format!("target/{}/release/{}", target.to_string(), bin_name));
self.build_alpine_image(
bin,
image,
BuildTarget::from_target(&container_image),
BuildProfile::Release,
crates,
extra_deps,
)
.await?;
let bin = build_container
.with_exec(vec![
"cargo",
"build",
"--target",
&target.to_string(),
"--release",
"-p",
deps.iter()
.map(|d| d.as_str())
.collect::<Vec<&str>>()
.as_slice(),
bin_name,
])
.file(format!(
"target/{}/release/{}",
target.to_string(),
bin_name
));
self.build_alpine_image(
bin,
image,
BuildTarget::from_target(&container_image),
deps.iter()
.map(|d| d.as_str())
.collect::<Vec<&str>>()
.as_slice(),
bin_name,
)
.await?
}
};
)
.await?
}
};
containers.push(container);
}

View File

@@ -110,7 +110,7 @@ impl HtmxBuild {
let container =
match &container_image {
SlimImage::Debian { image, deps, .. } => {
let target = BuildTarget::from_target(&container_image);
let _target = BuildTarget::from_target(&container_image);
let build_container = self
.build(

View File

@@ -55,7 +55,7 @@ impl LeptosBuild {
.client
.cache_volume(format!("rust_leptos_{}", profile.to_string()));
let mut build_options = vec!["cargo", "leptos", "build", "--release", "-vv"];
let build_options = vec!["cargo", "leptos", "build", "--release", "-vv"];
let rust_prebuild = rust_build_image
.with_workdir("/mnt/src")
@@ -95,7 +95,7 @@ impl LeptosBuild {
for container_image in images {
let container = match &container_image {
SlimImage::Debian { image, deps, .. } => {
let target = BuildTarget::from_target(&container_image);
let _target = BuildTarget::from_target(&container_image);
let build_container = self
.build(

View File

@@ -1,6 +1,5 @@
use std::{
path::{Path, PathBuf},
sync::Arc,
};
use eyre::Context;

View File

@@ -1,4 +1,4 @@
use std::{path::PathBuf, sync::Arc};
use std::{path::PathBuf};
use crate::{build::RustVersion, source::RustSource};

View File

@@ -1,4 +1,4 @@
use dagger_rust::build::{BuildProfile, RustVersion, SlimImage};
use dagger_rust::build::{RustVersion, SlimImage};
#[tokio::main]
pub async fn main() -> eyre::Result<()> {