From 9e0955e530982402e2b28b483880e16532df96aa Mon Sep 17 00:00:00 2001 From: Samuka007 Date: Tue, 25 Feb 2025 15:39:05 +0800 Subject: [PATCH] hakari to speedup building, change structure of the workspace --- .config/hakari.toml | 27 ++++++++ Cargo.lock | 21 ++++++ Cargo.toml | 9 ++- {app => crates/app}/Cargo.toml | 3 +- {app => crates/app}/src/handlers.rs | 0 {app => crates/app}/src/main.rs | 0 {app => crates/app}/src/types.rs | 0 crates/my-workspace-hack/.gitattributes | 4 ++ crates/my-workspace-hack/Cargo.toml | 45 ++++++++++++ crates/my-workspace-hack/build.rs | 2 + crates/my-workspace-hack/src/lib.rs | 1 + {service => crates/service}/Cargo.toml | 3 +- .../service/container_spec.json | 0 {service => crates/service}/src/lib.rs | 10 +-- flake.nix | 68 +++++++++++++++++-- 15 files changed, 180 insertions(+), 13 deletions(-) create mode 100644 .config/hakari.toml rename {app => crates/app}/Cargo.toml (71%) rename {app => crates/app}/src/handlers.rs (100%) rename {app => crates/app}/src/main.rs (100%) rename {app => crates/app}/src/types.rs (100%) create mode 100644 crates/my-workspace-hack/.gitattributes create mode 100644 crates/my-workspace-hack/Cargo.toml create mode 100644 crates/my-workspace-hack/build.rs create mode 100644 crates/my-workspace-hack/src/lib.rs rename {service => crates/service}/Cargo.toml (74%) rename container_spec.json => crates/service/container_spec.json (100%) rename {service => crates/service}/src/lib.rs (96%) diff --git a/.config/hakari.toml b/.config/hakari.toml new file mode 100644 index 0000000..f43ae8f --- /dev/null +++ b/.config/hakari.toml @@ -0,0 +1,27 @@ +# This file contains settings for `cargo hakari`. +# See https://docs.rs/cargo-hakari/latest/cargo_hakari/config for a full list of options. + +hakari-package = "my-workspace-hack" + +# Format version for hakari's output. Version 4 requires cargo-hakari 0.9.22 or above. +dep-format-version = "4" + +# Setting workspace.resolver = "2" or higher in the root Cargo.toml is HIGHLY recommended. +# Hakari works much better with the v2 resolver. (The v2 and v3 resolvers are identical from +# hakari's perspective, so you're welcome to set either.) +# +# For more about the new feature resolver, see: +# https://blog.rust-lang.org/2021/03/25/Rust-1.51.0.html#cargos-new-feature-resolver +resolver = "2" + +# Add triples corresponding to platforms commonly used by developers here. +# https://doc.rust-lang.org/rustc/platform-support.html +platforms = [ + "x86_64-unknown-linux-gnu", + # "x86_64-apple-darwin", + # "aarch64-apple-darwin", + # "x86_64-pc-windows-msvc", +] + +# Write out exact versions rather than a semver range. (Defaults to false.) +# exact-versions = true diff --git a/Cargo.lock b/Cargo.lock index d32afd5..db6850c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -583,6 +583,7 @@ name = "faas-rs" version = "0.1.0" dependencies = [ "actix-web", + "my-workspace-hack", "serde", "serde_json", "service", @@ -1172,6 +1173,25 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "defc4c55412d89136f966bbb339008b474350e5e6e78d2714439c386b3137a03" +[[package]] +name = "my-workspace-hack" +version = "0.1.0" +dependencies = [ + "actix-router", + "bytes", + "log", + "memchr", + "mio", + "prost", + "regex", + "regex-automata", + "regex-syntax", + "serde", + "syn", + "tracing", + "tracing-core", +] + [[package]] name = "num-conv" version = "0.1.0" @@ -1548,6 +1568,7 @@ dependencies = [ "containerd-client", "env_logger", "log", + "my-workspace-hack", "prost-types", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index c19cfab..c8e3103 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,14 @@ [workspace] -members = ["app", "service"] +members = ["crates/*"] resolver = "3" +[workspace.package] +version = "0.0.1" +authors = ["sparkzky", "Samuka007"] + +[workspace.metadata.crane] +name = "faas-rs" + # API 模块:基于 Actix Web 提供 RESTful 接口,调用 service 模块的功能。 # 测试:编写单元测试和集成测试,确保服务的可靠性。 # 镜像管理:实现缓存逻辑,并支持镜像解包和检查功能 diff --git a/app/Cargo.toml b/crates/app/Cargo.toml similarity index 71% rename from app/Cargo.toml rename to crates/app/Cargo.toml index d9b1e81..f5e8b9c 100644 --- a/app/Cargo.toml +++ b/crates/app/Cargo.toml @@ -8,4 +8,5 @@ actix-web = "4.0" tokio = { version = "1", features = ["full"] } service = { path = "../service" } serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" \ No newline at end of file +serde_json = "1.0" +my-workspace-hack = { version = "0.1", path = "../my-workspace-hack" } diff --git a/app/src/handlers.rs b/crates/app/src/handlers.rs similarity index 100% rename from app/src/handlers.rs rename to crates/app/src/handlers.rs diff --git a/app/src/main.rs b/crates/app/src/main.rs similarity index 100% rename from app/src/main.rs rename to crates/app/src/main.rs diff --git a/app/src/types.rs b/crates/app/src/types.rs similarity index 100% rename from app/src/types.rs rename to crates/app/src/types.rs diff --git a/crates/my-workspace-hack/.gitattributes b/crates/my-workspace-hack/.gitattributes new file mode 100644 index 0000000..3e9dba4 --- /dev/null +++ b/crates/my-workspace-hack/.gitattributes @@ -0,0 +1,4 @@ +# Avoid putting conflict markers in the generated Cargo.toml file, since their presence breaks +# Cargo. +# Also do not check out the file as CRLF on Windows, as that's what hakari needs. +Cargo.toml merge=binary -crlf diff --git a/crates/my-workspace-hack/Cargo.toml b/crates/my-workspace-hack/Cargo.toml new file mode 100644 index 0000000..2ed3ba4 --- /dev/null +++ b/crates/my-workspace-hack/Cargo.toml @@ -0,0 +1,45 @@ +# This file is generated by `cargo hakari`. +# To regenerate, run: +# cargo hakari generate + +[package] +name = "my-workspace-hack" +version = "0.1.0" +edition = "2021" +description = "workspace-hack package, managed by hakari" +# You can choose to publish this crate: see https://docs.rs/cargo-hakari/latest/cargo_hakari/publishing. +publish = false + +# The parts of the file between the BEGIN HAKARI SECTION and END HAKARI SECTION comments +# are managed by hakari. + +### BEGIN HAKARI SECTION +[dependencies] +actix-router = { version = "0.5", default-features = false, features = ["http", "unicode"] } +bytes = { version = "1" } +log = { version = "0.4", default-features = false, features = ["std"] } +memchr = { version = "2" } +mio = { version = "1", features = ["net", "os-ext"] } +prost = { version = "0.13", features = ["prost-derive"] } +regex = { version = "1" } +regex-automata = { version = "0.4", default-features = false, features = ["dfa-onepass", "hybrid", "meta", "nfa-backtrack", "perf-inline", "perf-literal", "unicode"] } +regex-syntax = { version = "0.8" } +serde = { version = "1", features = ["derive"] } +tracing = { version = "0.1", features = ["log"] } +tracing-core = { version = "0.1", default-features = false, features = ["std"] } + +[build-dependencies] +actix-router = { version = "0.5", default-features = false, features = ["http", "unicode"] } +bytes = { version = "1" } +log = { version = "0.4", default-features = false, features = ["std"] } +memchr = { version = "2" } +prost = { version = "0.13", features = ["prost-derive"] } +regex = { version = "1" } +regex-automata = { version = "0.4", default-features = false, features = ["dfa-onepass", "hybrid", "meta", "nfa-backtrack", "perf-inline", "perf-literal", "unicode"] } +regex-syntax = { version = "0.8" } +serde = { version = "1", features = ["derive"] } +syn = { version = "2", features = ["extra-traits", "fold", "full", "visit", "visit-mut"] } +tracing = { version = "0.1", features = ["log"] } +tracing-core = { version = "0.1", default-features = false, features = ["std"] } + +### END HAKARI SECTION diff --git a/crates/my-workspace-hack/build.rs b/crates/my-workspace-hack/build.rs new file mode 100644 index 0000000..92518ef --- /dev/null +++ b/crates/my-workspace-hack/build.rs @@ -0,0 +1,2 @@ +// A build script is required for cargo to consider build dependencies. +fn main() {} diff --git a/crates/my-workspace-hack/src/lib.rs b/crates/my-workspace-hack/src/lib.rs new file mode 100644 index 0000000..22489f6 --- /dev/null +++ b/crates/my-workspace-hack/src/lib.rs @@ -0,0 +1 @@ +// This is a stub lib.rs. diff --git a/service/Cargo.toml b/crates/service/Cargo.toml similarity index 74% rename from service/Cargo.toml rename to crates/service/Cargo.toml index aa78ebc..f607d27 100644 --- a/service/Cargo.toml +++ b/crates/service/Cargo.toml @@ -11,4 +11,5 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" log = "0.4" env_logger = "0.10" -prost-types = "0.13.4" \ No newline at end of file +prost-types = "0.13.4" +my-workspace-hack = { version = "0.1", path = "../my-workspace-hack" } diff --git a/container_spec.json b/crates/service/container_spec.json similarity index 100% rename from container_spec.json rename to crates/service/container_spec.json diff --git a/service/src/lib.rs b/crates/service/src/lib.rs similarity index 96% rename from service/src/lib.rs rename to crates/service/src/lib.rs index 38688d4..733f9d7 100644 --- a/service/src/lib.rs +++ b/crates/service/src/lib.rs @@ -1,15 +1,14 @@ use containerd_client::{ services::v1::{ container::Runtime, Container, CreateContainerRequest, CreateTaskRequest, - DeleteContainerRequest, DeleteTaskRequest, GetImageRequest, KillRequest, + DeleteContainerRequest, DeleteTaskRequest, KillRequest, ListContainersRequest, ListTasksRequest, StartRequest, WaitRequest, }, tonic::Request, with_namespace, Client, }; -use prost_types::Any; + use std::{ - collections::HashMap, fs::{self, File}, sync::{Arc, Mutex}, time::Duration, @@ -17,7 +16,8 @@ use std::{ use tokio::time::timeout; // config.json,dockerhub密钥 -const DOCKER_CONFIG_DIR: &str = "/var/lib/faasd/.docker/"; +// const DOCKER_CONFIG_DIR: &str = "/var/lib/faasd/.docker/"; + // 命名空间(容器的) const NAMESPACE: &str = "default"; @@ -36,7 +36,7 @@ impl Service { } pub async fn create_container(&self, image: String, cid: String) { - // let spec = include_str!("../../container_spec.json").to_string(); + // let spec = include_str!("../container_spec.json").to_string(); // let spec = Any { // type_url: "types.containerd.io/opencontainers/runtime-spec/1/Spec".to_string(), // value: spec.into_bytes(), diff --git a/flake.nix b/flake.nix index 019ad67..de09beb 100644 --- a/flake.nix +++ b/flake.nix @@ -53,24 +53,82 @@ fileset = lib.fileset.unions [ ./Cargo.toml ./Cargo.lock - (craneLib.fileset.commonCargoSources ./app) - (craneLib.fileset.commonCargoSources ./service) + (craneLib.fileset.commonCargoSources ./crates/app) + (craneLib.fileset.commonCargoSources ./crates/service) + (craneLib.fileset.commonCargoSources ./crates/my-workspace-hack) (craneLib.fileset.commonCargoSources crate) ]; }; faas-rs-crate = craneLib.buildPackage ( individualCrateArgs // { pname = "faas-rs"; - cargoExtraArgs = "--bin faas-rs"; - src = fileSetForCrate ./app; + cargoExtraArgs = "-p faas-rs"; + src = fileSetForCrate ./crates/app; }); in with pkgs; { - checks = { inherit faas-rs-crate; }; + checks = { + inherit faas-rs-crate; + + # Run clippy (and deny all warnings) on the workspace source, + # again, reusing the dependency artifacts from above. + # + # Note that this is done as a separate derivation so that + # we can block the CI if there are issues here, but not + # prevent downstream consumers from building our crate by itself. + clippy = craneLib.cargoClippy (commonArgs // { + inherit cargoArtifacts; + cargoClippyExtraArgs = "--all-targets -- --deny warnings -Z unstable-options"; + }); + + doc = craneLib.cargoDoc (commonArgs // { + inherit cargoArtifacts; + }); + + # Check formatting + fmt = craneLib.cargoFmt { + inherit src; + }; + + # Run tests with cargo-nextest + # Consider setting `doCheck = false` on other crate derivations + # if you do not want the tests to run twice + nextest = craneLib.cargoNextest (commonArgs // { + inherit cargoArtifacts; + partitions = 1; + partitionType = "count"; + cargoNextestPartitionsExtraArgs = "--no-tests=pass"; + }); + + # Ensure that cargo-hakari is up to date + hakari = craneLib.mkCargoDerivation { + inherit src; + pname = "my-workspace-hack"; + cargoArtifacts = null; + doInstallCargoArtifacts = false; + + buildPhaseCargoCommand = '' + cargo hakari generate --diff # workspace-hack Cargo.toml is up-to-date + cargo hakari manage-deps --dry-run # all workspace crates depend on workspace-hack + cargo hakari verify + ''; + + nativeBuildInputs = [ + pkgs.cargo-hakari + ]; + }; + }; packages.default = faas-rs-crate; + apps = { + faas-rs = flake-utils.lib.mkApp { + drv = faas-rs-crate; + desc = "FaaS Rust Project"; + }; + }; + devShells.default = craneLib.devShell { checks = self.checks.${system};