本文主要是介绍Cargo 私有仓库部署,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Step 1:服务端创建Git仓库
私有仓库的crates目录使用Git进行版本管理:
$ mkdir mycrates-io
$ cd mycrates-io
$ git init
$ cd ..
$ git clone --bare mycrates-io mycrates-io.git
Step 2:服务端部署Alexandrie服务
Alexandrie是Rust语言编写的私有仓库管理服务器,当前版本0.1.0。官方提供了份花里胡哨的安装脚本,事实上也就执行了以下几步。
Step 2.1:构建Alexandrie
$ cd alexandrie
$ cargo build -p alexandrie
编译器:rustc 1.46.0
编到migrations_macros
模块可能会报/usr/bin/ld: cannot find -lsqlite3
,解决方法:
$ sudo apt install sqlite3
$ sudo apt install libsqlite3-dev
Step 2.2:创建仓库目录
在alexandrie目录中创建存放crates的文件夹:
$ crate-storage
Step 2.3:配置仓库目录
在alexandrie目录中拉一份mycrates-io.git:
$ git clone path/to/mycrates-io.git crate-index
在crate-index中添加配置文件config.json:
{"dl": "http://<Alexandrie_IP>:3000/api/v1/crates/{crate}/{version}/download","api": "http://$(hostname):3000","allowed-registries": ["https://github.com/rust-lang/crates.io-index"]
}
注意:官方脚本中这里的
<Alexandrie_IP>
字段用的是$(hostname)
,实践证明可能会导致error: [6] Couldn't resolve host name
,查错查了好久。
提交:
$ git add config.json;
$ git commit -m 'Added `config.json`';
$ git push -u origin master;
Step 2.4:运行Alexandrie
在alexandrie目录中运行:
$ ./target/debug/alexandrie -c alexandrie.toml
Sep 12 16:48:21.043 INFO running database migrations, version: 0.1.0
Sep 12 16:48:21.048 INFO setting up request logger middleware, version: 0.1.0
Sep 12 16:48:21.050 INFO setting up cookie middleware, version: 0.1.0
Sep 12 16:48:21.052 INFO setting up authentication middleware, version: 0.1.0
Sep 12 16:48:21.055 INFO mounting '/', version: 0.1.0
Sep 12 16:48:21.059 INFO mounting '/me', version: 0.1.0
Sep 12 16:48:21.061 INFO mounting '/search', version: 0.1.0
Sep 12 16:48:21.064 INFO mounting '/most-downloaded', version: 0.1.0
Sep 12 16:48:21.065 INFO mounting '/last-updated', version: 0.1.0
Sep 12 16:48:21.067 INFO mounting '/crates/:crate', version: 0.1.0
Sep 12 16:48:21.069 INFO mounting '/account/login', version: 0.1.0
Sep 12 16:48:21.071 INFO mounting '/account/logout', version: 0.1.0
Sep 12 16:48:21.073 INFO mounting '/account/register', version: 0.1.0
Sep 12 16:48:21.075 INFO mounting '/account/manage', version: 0.1.0
Sep 12 16:48:21.081 INFO mounting '/account/manage/password', version: 0.1.0
Sep 12 16:48:21.083 INFO mounting '/account/manage/tokens', version: 0.1.0
Sep 12 16:48:21.084 INFO mounting '/account/manage/tokens/:token-id/revoke', version: 0.1.0
Sep 12 16:48:21.086 INFO mounting '/assets/*path', version: 0.1.0
Sep 12 16:48:21.088 INFO mounting '/api/v1/account/register', version: 0.1.0
Sep 12 16:48:21.089 INFO mounting '/api/v1/account/login', version: 0.1.0
Sep 12 16:48:21.091 INFO mounting '/api/v1/account/tokens', version: 0.1.0
Sep 12 16:48:21.093 INFO mounting '/api/v1/account/tokens/:name', version: 0.1.0
Sep 12 16:48:21.094 INFO mounting '/api/v1/categories', version: 0.1.0
Sep 12 16:48:21.101 INFO mounting '/api/v1/crates', version: 0.1.0
Sep 12 16:48:21.102 INFO mounting '/api/v1/crates/new', version: 0.1.0
Sep 12 16:48:21.104 INFO mounting '/api/v1/crates/suggest', version: 0.1.0
Sep 12 16:48:21.107 INFO mounting '/api/v1/crates/:name', version: 0.1.0
Sep 12 16:48:21.108 INFO mounting '/api/v1/crates/:name/owners', version: 0.1.0
Sep 12 16:48:21.110 INFO mounting '/api/v1/crates/:name/:version/yank', version: 0.1.0
Sep 12 16:48:21.112 INFO mounting '/api/v1/crates/:name/:version/unyank', version: 0.1.0
Sep 12 16:48:21.114 INFO mounting '/api/v1/crates/:name/:version/download', version: 0.1.0
Sep 12 16:48:21.119 INFO listening on '127.0.0.1:3000', version: 0.1.0
Sep 12 16:48:21.122 INFO Server listening on http://127.0.0.1:3000, version: 0.1.0
为了让局域网中的其他机器访问到,可能需要将alexandrie.toml中配置的回环地址改成固定IP。
输出信息显示Alexandrie在3000端口监听请求,但防火墙可能会将其禁用,使用以下步骤依次完成查看端口状态、开启端口、重启防火墙、确认端口状态:
$ sudo firewall-cmd --query-port=3000/tcp
no
$ sudo firewall-cmd --permanent --add-port=3000/tcp
success
$ sudo firewall-cmd --reload
success
$ sudo firewall-cmd --query-port=3000/tcp
yes
后面如需关闭端口:
$ sudo firewall-cmd --permanent --remove-port=3000/tcp
Step 3:客户端注册账户
上传crates之前需注册一个Alexandrie账户,使用浏览器访问http://<Alexandrie_IP>:3000
进行注册。有趣的是,密码似乎只能是十六进制数,因为alexandrie/src/frontend/account/register.rs
是这么处理的:
//? Decode hex-encoded password hash.let decoded_password = match hex::decode(form.password.as_bytes()) {Ok(passwd) => passwd,Err(_) => {let error_msg = String::from("password/salt decoding issue.");req.set_flash_message(FlashMessage::from_json(&error_msg)?);return Ok(utils::response::redirect("/account/register"));}};
注册之后进入http://<Alexandrie_IP>:3000/account/manage
页面,点击「Create token」获得token。
Step 4:客户端配置私有仓库
默认情况下,cargo访问的crates仓库目录是https://github.com/rust-lang/crates.io-index
,我们需要在~/.cargo/config
中将其替换为自己的私有仓库:
[source.crates-io]
replace-with = 'mycrates-io'
[source.mycrates-io]
registry = "ssh://<USER>:<PASSWORD>@<Alexandrie_IP>/path/to/mycrates-io.git"
注意:一个是把服务端密码放进去,二个是IP后面的
:
改成/
。
Step 5:客户端使用
登陆
$ cargo login <TOKEN>
cargo会自动将token保存至~/.cargo/credential
。
发布
$ cargo publish
由于Cargo仓库被设计成永久保存,发布上去的crate只能yank,不能删除,无法重新发布同版本的crate,这意味着一旦传错,只能在服务端删库重来:
$ rm alexandrie.db
参考资料
- Cargo Alternative Registries
- Alexandrie
- Alexandrie Installation script
- Rust crates私有化部署指南
2020年9月11日、14日 无锡
这篇关于Cargo 私有仓库部署的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!