技术控

    今日:123| 主题:49331
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Bare Minimum Crates.io Mirror Plus One

[复制链接]
为爱放自己 发表于 2016-10-9 06:26:26
230 4

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
I’ve been interested for a long time in making it easier to set up a mirror of crates.io . Making our vibrant ecosystem of libraries highly available around the world, on the public internet and within corporate firewalls, will help drive adoption and increase trust in our new community.
   I enjoyed and learned a lot from Gary Josack’s Dissecting Crates.io: Bare Minimum Mirror post and I’ve gotten to the point where I’ve built one more step on top of that post! His mirror is all local on his computer; mine runs in the cloud.
  TL;DR

  I now have:
  
       
  • A download-API mirror of crates.io running at cratesio-mirror.herokuapp.com   
  • A registry index mirror at gitlab.com/integer32llc/crates.io-index that is scheduled to pull from crates.io’s index every 10 min  
  And, because of some changes that were just added to Cargo that went out with the Rust 1.12.0 release, I’m able to easily tell Cargo to use these instead of crates.io to be able to download crates.
  Note that the crates’ files themselves are still coming from crates.io’s S3 bucket, and both my app and crates.io are hosted on Heroku, so this would only gain the community resiliency if:
  [code](
  GitHub was inaccessible, since my registry index is on GitLab
  OR (
    crates.io's app was inaccessible for some reason
    AND
    Heroku in general stayed accessible
  )
)
AND
crates.io's S3 bucket stayed accessible[/code]  So this isn’t total, separate redundancy, but it’s a little bit more than we have now.
  There are some other disadvantages/annoyances to using my mirror:
  
       
  • There’s no crate information available in the web UI, so you couldn’t search for crates if crates.io was inaccessible.   
  • You can’t publish to my mirror, so you couldn’t share new crates if crates.io was inaccessible.  
  I’d like to share what I learned through this process by talking about how Cargo works with the registry index and crates.io to download crates for you, then talk about how to set up your own API server and registry index, and finally how you tell Cargo that you want to use a mirror instead of crates.io. I’ll wrap up with what I see as logical next steps for continuing this work that I plan to do and would love to collaborate with people on!
  How Cargo downloads crates

   My first step in making a working mirror was understanding what happens when you have dependencies specified in your Cargo.toml , you don’t have those crate files on your computer yet, and you run cargo build . Where do those files come from, and how does Cargo know that?
   Based on a comment Alex Crichton made on my issue about running a mirror , I knew that the pieces were cargo running on my computer, the registry index on GitHub , and crates.io , but I wasn’t sure how these interacted.
   By doing git grep crates.io in Cargo’s codebase , I found  the constant CRATES_IO that’s hardcoded to https://github.com/rust-lang/crates.io-index  for Cargo’s default registry source, so that’s how Cargo knows where to find the index. I did not find anywhere the code knew about https://crates.io , but I followed what Cargo does with the index: it does a git fetch , then in addition to all the files containing metadata on crates, in the index repo is a config.json file that contains crates.io URLs:
  [code]{
  "dl": "https://crates.io/api/v1/crates",
  "api": "https://crates.io/"
}
[/code]   When Cargo goes to download a crate , it takes the value for dl in the config.json and concatenates the crate name, the version, and the string “download” separated by slashes and then requests that URL. For example, if we wanted the crate zopfli version 0.3.3, Cargo would create the url https://crates.io/api/v1/crates/zopfli/0.3.3/download .
   If we then switch to crates.io’s codebase and look at what the download route does, we can see that it redirects to a different URL created in this manner:
  [code]let redirect_url = format!("https://{}/crates/{}/{}-{}.crate",
                            req.app().bucket.host(),
                            crate_name, crate_name, version);[/code]   crate_name and version came from the download URL, and in our example are “zopfli” and “0.3.3”, respectively. But what is req.app().bucket.host() set to for the production crates.io instance? The value ultimately comes from an environment variable , and we don’t have access to crates.io’s environment variables directly. But we do have access to the redirect URL; if we didn’t, we wouldn’t be able to download any crates! It was easiest for me to see by requesting a download URL using curl :
  [code]$ curl -v https://crates.io/api/v1/crates/zopfli/0.3.3/download
...
< HTTP/1.1 302 Found
...
< Location: https://crates-io.s3-us-west-1.amazonaws.com/crates/zopfli/zopfli-0.3.3.crate
...[/code]   A-HA! So the crate files are actually coming from an S3 bucket at crates-io.s3-us-west-1.amazonaws.com .
  Now we now how all these parts fit together:
   
Bare Minimum Crates.io Mirror Plus One-1 (interested,available,corporate,scheduled,internet)

   What Gary’s post did was make all of these different parts come from your computer by getting a local checkout of the registry index, pointing the config.json at a Python SimpleHTTPServer running locally with already-downloaded crate files in a file structure that matched the API paths, and configuring Cargo to use the local registry instead (differences shown in orange):

Bare Minimum Crates.io Mirror Plus One-2 (interested,available,corporate,scheduled,internet)

  There’s no need for a redirect with this setup since the files are already exactly where Cargo is looking for them!
  What I set out to do was to have the registry index be on GitLab, the download API be a different Heroku app, but for now still get the crate files from crates.io’s S3 bucket:
12下一页
友荐云推荐




上一篇:HTML Kong
下一篇:江湖外卖O2O系统同城配送解决方案任意场景由您决定!
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

shuyinejiao 发表于 2016-10-11 04:58:44
天灵灵地灵灵,这次一定是沙发!
回复 支持 反对

使用道具 举报

94507 发表于 2016-10-11 05:36:59
发发呆,回回帖,工作结束~
回复 支持 反对

使用道具 举报

願做妳唯一的召喚獸 发表于 2016-10-12 05:07:16
楼上的能详细介绍一下么?
回复 支持 反对

使用道具 举报

暗梅幽闻花 发表于 2016-10-21 07:01:23
我是个哑巴,平时说话都是伪装的。
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表