Skip to content

Commit

Permalink
Migrate crates.io-index sync script to docker (#80)
Browse files Browse the repository at this point in the history
* crates-io-index: add

* crates-io-index: use is_empty func

* crates-io-index: add user.name and user.email

* crates-io-index: fix typo

* crates-io-index: fix typo

* crates-io-index: add missing env names in comments
  • Loading branch information
taoky authored Mar 18, 2022
1 parent 707f443 commit e7ed5bb
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- [aptsync](#aptsync)
- [apt-sync](#apt-sync)
- [archvsync](#archvsync)
- [crates-io-index](#crates-io-index)
- [debian-cd](#debian-cd)
- [docker-ce](#docker-ce)
- [fedora](#fedora)
Expand Down Expand Up @@ -130,6 +131,20 @@ A.K.A. [ftpsync](https://anonscm.debian.org/cgit/mirror/archvsync.git/)
| ------------- | ---------------------------------------------- |
| `IGNORE_LOCK` | Purge lockfiles at first. Defaults to `false`. |

### crates-io-index

[![crates-io-index](https://img.shields.io/docker/image-size/ustcmirror/crates-io-index/latest)](https://hub.docker.com/r/ustcmirror/crates-io-index "crates-io-index")
[![crates-io-index](https://img.shields.io/docker/pulls/ustcmirror/crates-io-index)](https://hub.docker.com/r/ustcmirror/crates-io-index "crates-io-index")

A dedicated script to sync <https://github.com/rust-lang/crates.io-index>.

| Parameter | Description |
| ---------------- | ---------------------------------------------------------------------------------------------------------- |
| `CRATES_PROXY` | The URL that crates will be redirected to. Defaults to `https://crates-io.proxy.ustclug.org/api/v1/crates` |
| `CRATES_GITMSG` | The commit message of `config.json`. Defaults to `Redirect to USTC Mirrors` |
| `CRATES_GITMAIL` | `user.email` when committing `config.json`. Defaults to `lug AT ustc.edu.cn` |
| `CRATES_GITNAME` | `user.name` when committing `config.json`. Defaults to `mirror` |

### debian-cd

[![debian-cd](https://img.shields.io/docker/image-size/ustcmirror/debian-cd/latest)](https://hub.docker.com/r/ustcmirror/debian-cd "debian-cd")
Expand Down
4 changes: 4 additions & 0 deletions crates-io-index/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM ustcmirror/base:alpine
LABEL maintainer "Keyu Tao <taoky AT lug.ustc.edu.cn>"
RUN apk add --no-cache git
ADD sync.sh /
56 changes: 56 additions & 0 deletions crates-io-index/sync.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash

# Migrated to yuki (docker) by @taoky
# Created by @ksqsf
# Based on the original version written by @knight42

# ENVS:
# CRATES_PROXY=
# CRATES_GITMSG=
# CRATES_GITMAIL=
# CRATES_GITNAME=

is_empty() {
[[ -z $(ls -A "$1" 2>/dev/null) ]]
}

set -e
[[ -n $DEBUG ]] && set -x

CRATES_PROXY="${CRATES_PROXY:-https://crates-io.proxy.ustclug.org/api/v1/crates}"
CRATES_GITMSG="${CRATES_GITMSG:-Redirect to USTC Mirrors}"
CRATES_GITMAIL="${CRATES_GITMAIL:-lug AT ustc.edu.cn}"
CRATES_GITNAME="${CRATES_GITNAME:-mirror}"

ensure_redirect() {
pushd "$TO"
if grep -F -q "$CRATES_PROXY" 'config.json'; then
return
else
cat <<EOF > 'config.json'
{
"dl": "$CRATES_PROXY",
"api": "https://crates.io/"
}
EOF
git add config.json
git -c user.name="$CRATES_GITNAME" -c user.email="$CRATES_GITMAIL" commit -qm "$CRATES_GITMSG"
fi
popd
}

# crates.io-index has a custom ensure_redirect logic
# so now we don't use gitsync here.

if ! is_empty "$TO"; then
cd "$TO"
git fetch origin
git reset --hard origin/master
ensure_redirect
git repack -adb
git gc --auto
git update-server-info
else
git clone 'https://github.com/rust-lang/crates.io-index.git' "$TO"
ensure_redirect
fi

0 comments on commit e7ed5bb

Please sign in to comment.