zerus
用于创建项目专用和/或通用离线 crates.io 镜像的轻量级工具
构建 zerus
可以从 crates.io 上发布的源代码进行构建。
$ cargo install zerus --locked
或者从 github releases 下载。
用法
下载 crates
使用 zerus mirror 下载项目依赖项的 .crate 文件。
$ zerus mirror new-mirror ../deku/Cargo.toml ../adsb_deku/Cargo.toml
按名称镜像单个 crate(解析为最新版本)或固定到特定版本。当使用 --crate 时,隐含 --get-feature-gated。
$ zerus mirror new-mirror --crate reqwest --crate serde@1.0.210
使用 --get-feature-gated 递归展开并下载所有传递依赖项,无论当前启用了哪些功能。这在构建更完整的镜像而非针对特定项目当前功能集定制的镜像时非常有用——确保即使功能稍后发生变化,crate 也可用。
[!NOTE] 该扩展(由
--get-feature-gated执行并由--crate隐含)包含其访问的每个 crate 的开发和构建依赖项,并忽略 feature gating,因此结果远大于cargo metadata所解析的内容——即使是像cfg-if这样的小 crate 也会扩展为数千个 crate。若要仅镜像项目的已解析依赖项,请传入其Cargo.toml而不是使用--crate。
$ zerus mirror new-mirror --get-feature-gated ../deku/Cargo.toml
添加 rust-playground 使用的排名前 100 的 rust crate 很简单:
$ git clone https://github.com/rust-lang/rust-playground
$ zerus mirror new-mirror rust-playground/top-crates/Cargo.toml
传输到离线网络
将镜像目录复制到您的代理或离线网络。
重复传输
为避免携带已经过之前传输的 crate,使用
generate-manifest 记录每次传输,并使用 cull 在下一次传输前移除已传输的 crate。
$ zerus mirror new-mirror Cargo.toml # download everything
$ zerus cull new-mirror transfers/*.txt # remove crates from previous transfers
$ zerus generate-manifest new-mirror -o transfers/$(date +%F).txt # record this transfer
# copy new-mirror to the offline network, keep transfers/ locally
cull 接受任意数量的 manifest 文件并移除其条目的并集;传入
--dry-run 以预览将被删除的内容。在将新 crate 合并到现有镜像后,在离线网络上运行 update-index。
生成索引
在离线网络上,使用 update-index 从 .crate 文件生成注册表索引。
$ zerus update-index new-mirror --dl-url http://[IP]
提供镜像服务
使用 zerus serve 托管带有稀疏索引、crate 下载和搜索 API 的注册表。
$ zerus serve new-mirror --bind 0.0.0.0:8080
启用请求日志记录:
$ RUST_LOG=tower_http=debug zerus serve new-mirror
使用镜像构建
将以下内容添加到 .cargo/config.toml(用您的服务器地址替换 [IP])。
[source.zerus]
registry = "sparse+http://[IP]/crates.io-index/"
[source.crates-io]
replace-with = "zerus"
[registries.zerus]
index = "sparse+http://[IP]/crates.io-index/"
使用 registries 条目,您可以搜索镜像:
$ cargo search --registry zerus serde