Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: more flexible mirror URL by a command #1793

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ The build process may be configured through the following environment variables:
| `RUBY_BUILD_CURL_OPTS` | Additional options to pass to `curl` for downloading. |
| `RUBY_BUILD_WGET_OPTS` | Additional options to pass to `wget` for downloading. |
| `RUBY_BUILD_MIRROR_URL` | Custom mirror URL root. |
| `RUBY_BUILD_MIRROR_PACKAGE_URL` | Custom complete mirror URL (e.g. http://mirror.example.com/package-1.0.0.tar.gz). |
| `RUBY_BUILD_MIRROR_PACKAGE_URL` | Custom complete mirror URL (e.g. http://mirror.example.com/package-1.0.0.tar.gz). |
| `RUBY_BUILD_MIRROR_CMD` | Custom mirror command (see [below](#more-flexible-mirror-url) for the usage). |
| `RUBY_BUILD_SKIP_MIRROR` | Bypass the download mirror and fetch all package files from their original URLs. |
| `RUBY_BUILD_ROOT` | Custom build definition directory. (Default: `share/ruby-build`) |
| `RUBY_BUILD_DEFINITIONS` | Additional paths to search for build definitions. (Colon-separated list) |
Expand Down Expand Up @@ -142,6 +143,25 @@ complete URL by setting `RUBY_BUILD_MIRROR_PACKAGE_URL`. It behaves the same as
The default ruby-build download mirror is sponsored by
[Basecamp](https://basecamp.com/).

#### More flexible mirror URL

For more flexible mirror URL, you can provide a custom command to output the
desired mirror URL, as shown below:

```sh
# There are two arguments:
# 1st arg: the original URL without checksum
# 2nd arg: the checksum
$ cat <<'EOF' >./get_mirror_url && chmod +x ./get_mirror_url
#!/bin/sh
echo "$1" | sed "s/cache.ruby-lang.org/mirror.example.com/"
EOF

$ export RUBY_BUILD_MIRROR_CMD="$(pwd)/get_mirror_url"
```

After executing the above script in your shell, install a version as usual.

#### Keeping the build directory after installation

Both `ruby-build` and `rbenv install` accept the `-k` or `--keep` flag, which
Expand Down
6 changes: 4 additions & 2 deletions bin/ruby-build
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ fetch_tarball() {
fi
elif [ -n "$RUBY_BUILD_MIRROR_PACKAGE_URL" ]; then
mirror_url="$RUBY_BUILD_MIRROR_PACKAGE_URL"
elif [ -n "$RUBY_BUILD_MIRROR_CMD" ]; then
mirror_url="$(command "$RUBY_BUILD_MIRROR_CMD" "$package_url" "$checksum")"
fi
fi

Expand Down Expand Up @@ -1421,7 +1423,7 @@ else
unset RUBY_BUILD_CACHE_PATH
fi

if [ -z "$RUBY_BUILD_MIRROR_URL" -a -z "$RUBY_BUILD_MIRROR_PACKAGE_URL" ]; then
if [ -z "$RUBY_BUILD_MIRROR_URL" -a -z "$RUBY_BUILD_MIRROR_PACKAGE_URL" -a -z "$RUBY_BUILD_MIRROR_CMD" ]; then
RUBY_BUILD_MIRROR_URL="https://dqw8nmjcqpjn7.cloudfront.net"
RUBY_BUILD_DEFAULT_MIRROR=1
else
Expand All @@ -1430,7 +1432,7 @@ else
fi

if [ -n "$RUBY_BUILD_SKIP_MIRROR" ] || ! has_checksum_support compute_sha2; then
unset RUBY_BUILD_MIRROR_URL RUBY_BUILD_MIRROR_PACKAGE_URL
unset RUBY_BUILD_MIRROR_URL RUBY_BUILD_MIRROR_PACKAGE_URL RUBY_BUILD_MIRROR_CMD
fi

ARIA2_OPTS="${RUBY_BUILD_ARIA2_OPTS} ${IPV4+--disable-ipv6=true} ${IPV6+--disable-ipv6=false}"
Expand Down
24 changes: 24 additions & 0 deletions test/mirror.bats
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,27 @@ DEF
unstub curl
unstub shasum
}


@test "package is fetched from the URL returned by a commond" {
export RUBY_BUILD_MIRROR_URL=
export RUBY_BUILD_MIRROR_CMD=get_mirror_url

local checksum="ba988b1bb4250dee0b9dd3d4d722f9c64b2bacfc805d1b6eba7426bda72dd3c5"

stub get_mirror_url 'echo "${1/cache.ruby-lang.org/mirror.example.com}#$2"'
stub shasum true "echo $checksum"
stub curl "-*I* https://mirror.example.com/packages/package-1.0.0.tar.gz#$checksum : true" \
"-q -o * -*S* https://mirror.example.com/packages/package-1.0.0.tar.gz#$checksum : cp $FIXTURE_ROOT/package-1.0.0.tar.gz \$3"

run_inline_definition <<DEF
install_package "package-1.0.0" "https://cache.ruby-lang.org/packages/package-1.0.0.tar.gz#$checksum" copy
DEF

assert_success
assert [ -x "${INSTALL_ROOT}/bin/package" ]

unstub curl
unstub shasum
unstub get_mirror_url
}