|
| 1 | +KOII_RELEASE=v1.14.21 |
| 2 | +KOII_INSTALL_INIT_ARGS=v1.14.21 |
| 3 | + |
| 4 | +#!/bin/sh |
| 5 | +# Copyright 2016 The Rust Project Developers. See the COPYRIGHT |
| 6 | +# file at the top-level directory of this distribution and at |
| 7 | +# http://rust-lang.org/COPYRIGHT. |
| 8 | +# |
| 9 | +# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 11 | +# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 12 | +# option. This file may not be copied, modified, or distributed |
| 13 | +# except according to those terms. |
| 14 | + |
| 15 | +# This is just a little script that can be downloaded from the internet to |
| 16 | +# install koii-install. It just does platform detection, downloads the installer |
| 17 | +# and runs it. |
| 18 | + |
| 19 | +{ # this ensures the entire script is downloaded # |
| 20 | + |
| 21 | +if [ -z "$KOII_DOWNLOAD_ROOT" ]; then |
| 22 | + KOII_DOWNLOAD_ROOT="https://github.com/koii-network/k2-release/releases/download" |
| 23 | +fi |
| 24 | +GH_LATEST_RELEASE="https://api.github.com/repos/koii-network/k2-release/releases/latest" |
| 25 | + |
| 26 | +set -e |
| 27 | + |
| 28 | +usage() { |
| 29 | + cat 1>&2 <<EOF |
| 30 | +koii-install-init |
| 31 | +initializes a new installation |
| 32 | +
|
| 33 | +USAGE: |
| 34 | + koii-install-init [FLAGS] [OPTIONS] --data_dir <PATH> --pubkey <PUBKEY> |
| 35 | +
|
| 36 | +FLAGS: |
| 37 | + -h, --help Prints help information |
| 38 | + --no-modify-path Don't configure the PATH environment variable |
| 39 | +
|
| 40 | +OPTIONS: |
| 41 | + -d, --data-dir <PATH> Directory to store install data |
| 42 | + -u, --url <URL> JSON RPC URL for the koii cluster |
| 43 | + -p, --pubkey <PUBKEY> Public key of the update manifest |
| 44 | +EOF |
| 45 | +} |
| 46 | + |
| 47 | +main() { |
| 48 | + downloader --check |
| 49 | + need_cmd uname |
| 50 | + need_cmd mktemp |
| 51 | + need_cmd chmod |
| 52 | + need_cmd mkdir |
| 53 | + need_cmd rm |
| 54 | + need_cmd sed |
| 55 | + need_cmd grep |
| 56 | + |
| 57 | + for arg in "$@"; do |
| 58 | + case "$arg" in |
| 59 | + -h|--help) |
| 60 | + usage |
| 61 | + exit 0 |
| 62 | + ;; |
| 63 | + *) |
| 64 | + ;; |
| 65 | + esac |
| 66 | + done |
| 67 | + |
| 68 | + _ostype="$(uname -s)" |
| 69 | + _cputype="$(uname -m)" |
| 70 | + |
| 71 | + case "$_ostype" in |
| 72 | + Linux) |
| 73 | + _ostype=unknown-linux-gnu |
| 74 | + ;; |
| 75 | + Darwin) |
| 76 | + if [[ $_cputype = arm64 ]]; then |
| 77 | + _cputype=aarch64 |
| 78 | + fi |
| 79 | + _ostype=apple-darwin |
| 80 | + ;; |
| 81 | + *) |
| 82 | + err "machine architecture is currently unsupported" |
| 83 | + ;; |
| 84 | + esac |
| 85 | + TARGET="${_cputype}-${_ostype}" |
| 86 | + |
| 87 | + temp_dir="$(mktemp -d 2>/dev/null || ensure mktemp -d -t koii-install-init)" |
| 88 | + ensure mkdir -p "$temp_dir" |
| 89 | + |
| 90 | + # Check for KOII_RELEASE environment variable override. Otherwise fetch |
| 91 | + # the latest release tag from github |
| 92 | + if [ -n "$KOII_RELEASE" ]; then |
| 93 | + release="$KOII_RELEASE" |
| 94 | + else |
| 95 | + release_file="$temp_dir/release" |
| 96 | + printf 'looking for latest release\n' 1>&2 |
| 97 | + ensure downloader "$GH_LATEST_RELEASE" "$release_file" |
| 98 | + release=$(\ |
| 99 | + grep -m 1 \"tag_name\": "$release_file" \ |
| 100 | + | sed -ne 's/^ *"tag_name": "\([^"]*\)",$/\1/p' \ |
| 101 | + ) |
| 102 | + if [ -z "$release" ]; then |
| 103 | + err 'Unable to figure latest release' |
| 104 | + fi |
| 105 | + fi |
| 106 | + |
| 107 | + download_url="$KOII_DOWNLOAD_ROOT/$release/koii-install-init-$TARGET" |
| 108 | + koii_install_init="$temp_dir/koii-install-init" |
| 109 | + |
| 110 | + printf 'downloading %s installer\n' "$release" 1>&2 |
| 111 | + |
| 112 | + ensure mkdir -p "$temp_dir" |
| 113 | + ensure downloader "$download_url" "$koii_install_init" |
| 114 | + ensure chmod u+x "$koii_install_init" |
| 115 | + if [ ! -x "$koii_install_init" ]; then |
| 116 | + printf '%s\n' "Cannot execute $koii_install_init (likely because of mounting /tmp as noexec)." 1>&2 |
| 117 | + printf '%s\n' "Please copy the file to a location where you can execute binaries and run ./koii-install-init." 1>&2 |
| 118 | + exit 1 |
| 119 | + fi |
| 120 | + |
| 121 | + if [ -z "$1" ]; then |
| 122 | + #shellcheck disable=SC2086 |
| 123 | + ignore "$koii_install_init" $KOII_INSTALL_INIT_ARGS |
| 124 | + else |
| 125 | + ignore "$koii_install_init" "$@" |
| 126 | + fi |
| 127 | + retval=$? |
| 128 | + |
| 129 | + ignore rm "$koii_install_init" |
| 130 | + ignore rm -rf "$temp_dir" |
| 131 | + |
| 132 | + return "$retval" |
| 133 | +} |
| 134 | + |
| 135 | +err() { |
| 136 | + printf 'koii-install-init: %s\n' "$1" >&2 |
| 137 | + exit 1 |
| 138 | +} |
| 139 | + |
| 140 | +need_cmd() { |
| 141 | + if ! check_cmd "$1"; then |
| 142 | + err "need '$1' (command not found)" |
| 143 | + fi |
| 144 | +} |
| 145 | + |
| 146 | +check_cmd() { |
| 147 | + command -v "$1" > /dev/null 2>&1 |
| 148 | +} |
| 149 | + |
| 150 | +# Run a command that should never fail. If the command fails execution |
| 151 | +# will immediately terminate with an error showing the failing |
| 152 | +# command. |
| 153 | +ensure() { |
| 154 | + if ! "$@"; then |
| 155 | + err "command failed: $*" |
| 156 | + fi |
| 157 | +} |
| 158 | + |
| 159 | +# This is just for indicating that commands' results are being |
| 160 | +# intentionally ignored. Usually, because it's being executed |
| 161 | +# as part of error handling. |
| 162 | +ignore() { |
| 163 | + "$@" |
| 164 | +} |
| 165 | + |
| 166 | +# This wraps curl or wget. Try curl first, if not installed, |
| 167 | +# use wget instead. |
| 168 | +downloader() { |
| 169 | + if check_cmd curl; then |
| 170 | + program=curl |
| 171 | + elif check_cmd wget; then |
| 172 | + program=wget |
| 173 | + else |
| 174 | + program='curl or wget' # to be used in error message of need_cmd |
| 175 | + fi |
| 176 | + |
| 177 | + if [ "$1" = --check ]; then |
| 178 | + need_cmd "$program" |
| 179 | + elif [ "$program" = curl ]; then |
| 180 | + curl -sSfL "$1" -o "$2" |
| 181 | + elif [ "$program" = wget ]; then |
| 182 | + wget "$1" -O "$2" |
| 183 | + else |
| 184 | + err "Unknown downloader" # should not reach here |
| 185 | + fi |
| 186 | +} |
| 187 | + |
| 188 | +main "$@" |
| 189 | + |
| 190 | +} # this ensures the entire script is downloaded # |
0 commit comments