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的文件夹:

$ mkdir 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.toml中配置的回环地址改成固定IP:

[general]
bind_address = "<Alexandrie_IP>:3000"

在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在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:客户端发布

Alexandrie就绪后,可在任意客户端发布crate,但需要注册Alexandrie账户并使用cargo登陆。

Step 3.1 注册账户

使用浏览器访问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 3.2 登陆

$ cargo login <TOKEN>

<TOKEN>就是上一步获得的token,cargo会自动将其保存至~/.cargo/credential

Step 3.3 发布

cargo publish --no-verify --manifest-path path/to/Cargo.toml --index=ssh://<USER>:<PASSWORD>@<Alexandrie_IP>/path/to/mycrates-io.git

笔者使用rustc源码自带的vendor进行实验,发现cargo在打包前会检查依赖,如果找不到就无法打包。一番研究,cargo似乎没有禁止依赖检查的选项,最后祭出绝招——改源码(1.34.2):把src/tools/cargo/src/cargo/core/resolver/mod.rs resolve()函数中对activate_deps_loop()的调用注释掉。

由于Cargo仓库被设计成永久保存,发布上去的crate只能yank,不能删除,无法重新发布同版本的crate,这意味着一旦传错,只能在服务端删库重来:

$ rm alexandrie.db
$ rm crate-storage/*
$ cd crate-index
$ git reset --hard <HEAD>
$ git push -f origin master

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后面的:改成/

参考资料


2020年9月11日、14日、16日 无锡

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,192评论 4 369
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,186评论 1 303
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,844评论 0 252
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,471评论 0 217
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,876评论 3 294
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,891评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,068评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,791评论 0 205
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,539评论 1 249
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,772评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,250评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,577评论 3 260
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,244评论 3 241
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,146评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,949评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,995评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,812评论 2 276