Skip to content

Commit

Permalink
rzup: touch up scripts (risc0#2180)
Browse files Browse the repository at this point in the history
This changes the script to use s3 buckets and adds a test mode

---------

Co-authored-by: Frank Laub <[email protected]>
  • Loading branch information
SchmErik and flaub authored Aug 5, 2024
1 parent f57486e commit c587e7f
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 419 deletions.
243 changes: 201 additions & 42 deletions rzup/install
Original file line number Diff line number Diff line change
@@ -1,54 +1,213 @@
#!/usr/bin/env bash
# Copyright 2024 RISC Zero, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Adapted from https://github.com/foundry-rs/foundry/blob/master/foundryup/install
set -euo pipefail

set -eo pipefail
RZUP_URL="${RZUP_URL:-https://risc0-artifacts.s3.us-west-2.amazonaws.com/rzup}"

echo "💾 Installing rzup"
# Override the download path with the ENV_PATH environment variable (i.e. stage)
ENV_PATH=${ENV_PATH:-prod}

BASE_DIR=${XDG_CONFIG_HOME:-$HOME}
RISC0_DIR=${RISC0_DIR-"$BASE_DIR/.risc0"}
RISC0_BIN_DIR="$RISC0_DIR/bin"
QUIET=no

# TODO: Replace this friendly redirect URL once set up
BIN_URL="https://risc0-artifacts.s3.us-west-2.amazonaws.com/rzup/rzup"
BIN_PATH="$RISC0_BIN_DIR/rzup"
RZUP_BIN_DIR="${HOME}/.risc0/bin"

command -v curl >/dev/null 2>&1 || { echo >&2 "curl is required but it's not installed. Aborting."; exit 1; }
usage() {
cat <<EOF
install
# Create the .rzup bin directory and rzup binary if it doesn't exist.
mkdir -p $RISC0_BIN_DIR
curl -# -L $BIN_URL -o $BIN_PATH
chmod +x $BIN_PATH
The installer for rzup, from RISC Zero.
detect_shell() {
case $SHELL in
*/zsh) PROFILE="${ZDOTDIR:-"$HOME"}/.zshenv"; PREF_SHELL='zsh' ;;
*/bash) PROFILE="$HOME/.bashrc"; PREF_SHELL='bash' ;;
*/fish) PROFILE="$HOME/.config/fish/config.fish"; PREF_SHELL='fish' ;;
*/ash) PROFILE="$HOME/.profile"; PREF_SHELL='ash' ;;
*) echo "Could not detect shell, manually add ${RISC0_BIN_DIR} to your PATH." && exit 1 ;;
Usage: install [OPTIONS]
Options:
-v, --verbose
Enable verbose output
-q, --quiet
Disable progress output
-h, --help
Print help
EOF
}

cleanup() {
[ -n "${_dir:-}" ] && [ -d "$_dir" ] && rm -rf "$_dir"
}

trap cleanup EXIT

main() {
downloader --check
need_cmd uname
need_cmd mktemp
need_cmd chmod
need_cmd mkdir
need_cmd rm
need_cmd rmdir
need_cmd mv

check_rust_installed
get_architecture

_arch="$RETVAL"
assert_nz "$_arch" "arch"

_url="${RZUP_URL}/${ENV_PATH}/${_arch}/rzup"
_dir="$(ensure mktemp -d)"
_file="${_dir}/rzup"

[ -f "$RZUP_BIN_DIR/rzup" ] && rm "$RZUP_BIN_DIR/rzup"

for arg in "$@"; do
case "$arg" in
--help)
usage
exit 0
;;
-q | --quiet) QUIET=yes ;;
*) ;;
esac
done

info 'Downloading installer'

ensure mkdir -p "$_dir"
ensure downloader "$_url" "$_file"
ensure chmod u+x "$_file"
[ ! -x "$_file" ] && err "Cannot execute $_file"

ensure mkdir -p "$RZUP_BIN_DIR"
ensure mv "$_file" "$RZUP_BIN_DIR"

info "rzup has been installed to $RZUP_BIN_DIR"

detect_shell

# add rzup to PATH if it isn't already present
if [[ ":$PATH:" != *":${RZUP_BIN_DIR}:"* ]]; then
info "Adding rzup to PATH in ${PROFILE}"
{
echo
echo "export PATH=\"\$PATH:$RZUP_BIN_DIR\""
} >> "$PROFILE"
else
info "rzup found in PATH"
fi

info "🎉 rzup installed!"
echo "Run the following command to install the zkVM:"
echo " rzup install"
}

check_rust_installed() {
check_cmd rustc || err "Rust is not installed. Please install Rust from https://rustup.rs/ and run this script again."
}

get_architecture() {
_ostype="$(uname -s)"
_cputype="$(uname -m)"

case "$_ostype" in
Linux) _ostype=linux ;;
Darwin) _ostype=darwin ;;
*) err "Unsupported OS type: $_ostype" ;;
esac

case "$_cputype" in
x86_64 | amd64) _cputype=x86_64 ;;
aarch64 | arm64) _cputype=arm64 ;;
*) err "Unsupported CPU type: $_cputype" ;;
esac

case "$_cputype $_ostype" in
"x86_64 linux") _arch=Linux-X64 ;;
"arm64 darwin" ) _arch=macOS-ARM64 ;;
*) err "Unsupported CPU arch pairing: $_cputype $_ostype" ;;
esac

RETVAL="$_arch"
}

detect_shell() {
case $SHELL in
*/zsh)
PROFILE="${ZDOTDIR:-"$HOME"}/.zshrc"
PREF_SHELL='zsh'
;;
*/bash)
PROFILE="$HOME/.bashrc"
PREF_SHELL='bash'
;;
*/fish)
PROFILE="$HOME/.config/fish/config.fish"
PREF_SHELL='fish'
;;
*/ash)
PROFILE="$HOME/.profile"
PREF_SHELL='ash'
;;
*)
err "Could not detect shell. Please manually add ${RZUP_BIN_DIR} to your PATH."
;;
esac
info "Detected your preferred shell as ${PREF_SHELL}"
}

info() {
[ "$QUIET" = "no" ] && printf 'info: %s\n' "$1" >&2
}

err() {
printf 'error: %s\n' "$1" >&2
exit 1
}

warn() {
printf 'warn: %s\n' "$1" >&2
}

need_cmd() {
check_cmd "$1" || err "Command not found: $1"
}

check_cmd() {
command -v "$1" >/dev/null 2>&1
}

assert_nz() {
[ -z "$1" ] && err "$2"
}

ensure() {
"$@" || err "Command failed: $*"
}

downloader() {
if [ "$1" = --check ]; then
check_cmd curl || check_cmd wget || err "Need 'curl' or 'wget' (neither found)"
else
_url="$1"
_file="$2"
if check_cmd curl; then
curl --silent --show-error --fail --location "$_url" --output "$_file" || err "Failed to download with curl"
elif check_cmd wget; then
wget --quiet --output-document="$2" "$1" || err "Failed to download with wget"
else
err "Need 'curl' or 'wget' (neither command found)"
fi
fi
}

detect_shell
echo "✅ Detected your preferred shell as ${PREF_SHELL}"

# add rzup to PATH if it isn't already present
if [[ ":$PATH:" != *":${RISC0_BIN_DIR}:"* ]]; then
echo "➕Adding rzup to PATH in ${PROFILE}"
{
echo
echo "export PATH=\"\$PATH:$RISC0_BIN_DIR\""
} >> "$PROFILE"
else
echo "✅ rzup found in PATH"
fi

echo "🎉 rzup installed!"
echo
echo "▶️ Run the following commands to install the zkVM:"
echo
echo "source ${PROFILE}"
echo "rzup"
echo
main "$@" || exit 1
Loading

0 comments on commit c587e7f

Please sign in to comment.