|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Build a migration with datastore plugins |
| 4 | +# |
| 5 | +# This script builds a migration with datastore plugins. Specify the |
| 6 | +# migration, such as 10-to-11, and one or more plugin repos to build with. A |
| 7 | +# specific version of a plugin is specified by following it with |
| 8 | +# @<version_or_hash>. The migration binary is built in its module |
| 9 | +# subdirectory. Run the migration binary directly, or copy it into a directory |
| 10 | +# in PATH to be run by ipfs-update or fs-repo-migrations. |
| 11 | +# |
| 12 | +# For each plugin built, the script asks which plugin to load. Use the -y flag |
| 13 | +# to avoid the prompt and choose 'plugin *' automatically. |
| 14 | +# |
| 15 | +# Example: |
| 16 | +# ./build-plugin.sh 10-to-11 github.com/ipfs/go-ds-s3 github.com/ipfs/[email protected] |
| 17 | +# |
| 18 | +set -eou pipefail |
| 19 | + |
| 20 | +function usage() { |
| 21 | + echo "usage: $0 [-y] x-to-y plugin_repo[@<version_or_hash>] ...">&2 |
| 22 | + echo |
| 23 | + echo "example: $0 10-to-11 github.com/ipfs/go-ds-s3" >&2 |
| 24 | +} |
| 25 | + |
| 26 | +AUTO_ANSWER=no |
| 27 | + |
| 28 | +if [ $# -ge 1 ]; then |
| 29 | + if [ "$1" = "-h" -o "$1" = "-?" -o "$1" = "-help" ]; then |
| 30 | + echo "Build a migration with one or more plugins" |
| 31 | + echo |
| 32 | + usage |
| 33 | + echo |
| 34 | + echo "Options and arguments" |
| 35 | + echo "-y Automatically answer 'y' to all promots" |
| 36 | + echo "First postional argument, specifies the migration to build." |
| 37 | + echo "Remaining positional arguments specify plugin repos with optional version." |
| 38 | + exit 0 |
| 39 | + elif [ "$1" = "-y" ]; then |
| 40 | + AUTO_ANSWER=yes |
| 41 | + shift 1 |
| 42 | + else |
| 43 | + echo "unrecognized option $1" >&2 |
| 44 | + echo >&2 |
| 45 | + usage |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | +fi |
| 49 | + |
| 50 | +if [ $# -lt 2 ]; then |
| 51 | + echo "too few arguments" >&2 |
| 52 | + echo >&2 |
| 53 | + usage |
| 54 | + exit 1 |
| 55 | +fi |
| 56 | + |
| 57 | +MIGRATION="$1" |
| 58 | +BUILD_DIR="$(mktemp -d --suffix=migration_build)" |
| 59 | +BUILD_GOIPFS="${BUILD_DIR}/go-ipfs" |
| 60 | +IPFS_REPO="github.com/ipfs/go-ipfs" |
| 61 | +IPFS_REPO_URL="https://${IPFS_REPO}" |
| 62 | + |
| 63 | +function cleanup { |
| 64 | + rm -rf "${BUILD_DIR}" |
| 65 | +} |
| 66 | +trap cleanup EXIT |
| 67 | + |
| 68 | +function get_migration() { |
| 69 | + for i in *${1}; do |
| 70 | + if [ -d "$i" ]; then |
| 71 | + echo "$i" |
| 72 | + return 0 |
| 73 | + fi |
| 74 | + done |
| 75 | +} |
| 76 | + |
| 77 | +function clone_ipfs() { |
| 78 | + local mig="$1" |
| 79 | + if [ ! -d "${mig}/vendor/github.com/ipfs/go-ipfs" ]; then |
| 80 | + echo "migration $mig does not support datastore plugins" >&2 |
| 81 | + return 1 |
| 82 | + fi |
| 83 | + |
| 84 | + pushd "$mig" |
| 85 | + local ver="$(go list -f '{{.Version}}' -m github.com/ipfs/go-ipfs)" |
| 86 | + popd |
| 87 | + if echo "$ver" | grep -E 'v.*.[0-9]{14}-[0-9a-f]{12}$'; then |
| 88 | + local commit="$(echo $ver | rev | cut -d '-' -f 1 | rev)" |
| 89 | + echo "===> Getting go-ipfs commit $commit" |
| 90 | + git clone "$IPFS_REPO_URL" "$BUILD_GOIPFS" |
| 91 | + pushd "$BUILD_GOIPFS" |
| 92 | + git checkout "$commit" |
| 93 | + popd |
| 94 | + else |
| 95 | + echo "===> Getting go-ipfs branch $ver" |
| 96 | + git clone -b "$ver" "$IPFS_REPO_URL" "$BUILD_GOIPFS" |
| 97 | + fi |
| 98 | +} |
| 99 | + |
| 100 | +function ask_yes_no() { |
| 101 | + local prompt="$1" |
| 102 | + while true; do |
| 103 | + read -p "$prompt [y/n]?" yn |
| 104 | + case "$yn" in |
| 105 | + [Yy]* ) return 0;; |
| 106 | + [Nn]* ) return 1;; |
| 107 | + * ) echo "Please answer y or n";; |
| 108 | + esac |
| 109 | + done |
| 110 | +} |
| 111 | + |
| 112 | +function bundle_ipfs_plugin() { |
| 113 | + local plugin_repo="$1" |
| 114 | + echo "===> Bundling plugin $plugin_repo info go-ipfs for migration" |
| 115 | + local plugin_version=latest |
| 116 | + if [[ "$plugin_repo" == *"@"* ]]; then |
| 117 | + plugin_version="$(echo $plugin_repo | cut -d '@' -f 2)" |
| 118 | + plugin_repo="$(echo $plugin_repo | cut -d '@' -f 1)" |
| 119 | + fi |
| 120 | + echo "plugin version: $plugin_version" |
| 121 | + local plugin_name="$(echo $plugin_repo | rev | cut -d '-' -f 1 | rev)" |
| 122 | + |
| 123 | + pushd "$BUILD_GOIPFS" |
| 124 | + go get "${plugin_repo}@${plugin_version}" |
| 125 | + popd |
| 126 | + |
| 127 | + local ds_name="${plugin_name}ds" |
| 128 | + # While there is a plugin name conflict, ask for or generate new name. |
| 129 | + # When run non-interactively, keep appending "1" until no conflict. |
| 130 | + while grep -q "^${ds_name} " "${BUILD_GOIPFS}/plugin/loader/preload_list"; do |
| 131 | + old_ds_name="$ds_name" |
| 132 | + ds_name="${ds_name}1" |
| 133 | + if [ "$AUTO_ANSWER" != "yes" ]; then |
| 134 | + echo -n "\"$old_ds_name\" already in use, " |
| 135 | + while true; do |
| 136 | + if ask_yes_no "use plugin name \"$ds_name\""; then |
| 137 | + break |
| 138 | + else |
| 139 | + read -p "Enter new value: " ds_name |
| 140 | + fi |
| 141 | + done |
| 142 | + fi |
| 143 | + done |
| 144 | + |
| 145 | + # Prompt for plugin to load |
| 146 | + load_plugin='plugin *' |
| 147 | + if [ "$AUTO_ANSWER" != "yes" ]; then |
| 148 | + while true; do |
| 149 | + if ask_yes_no "For $plugin_repo, load '$load_plugin'"; then |
| 150 | + break |
| 151 | + else |
| 152 | + read -p "Enter new value: " load_plugin |
| 153 | + fi |
| 154 | + done |
| 155 | + fi |
| 156 | + echo "$ds_name ${plugin_repo}/${load_plugin}" >> "${BUILD_GOIPFS}/plugin/loader/preload_list" |
| 157 | +} |
| 158 | + |
| 159 | +function build_migration() { |
| 160 | + echo "===> Building go-ipfs with datastore plugins" |
| 161 | + sed -i '/^\tgo fmt .*/a \\tgo mod tidy' "${BUILD_GOIPFS}/plugin/loader/Rules.mk" |
| 162 | + make -C "$BUILD_GOIPFS" build |
| 163 | + |
| 164 | + local mig="$1" |
| 165 | + echo |
| 166 | + echo "===> Building migration $mig with plugins" |
| 167 | + pushd "$mig" |
| 168 | + go mod edit -replace "${IPFS_REPO}=${BUILD_GOIPFS}" |
| 169 | + go mod vendor |
| 170 | + go build -mod=vendor |
| 171 | + # Cleanup temporary modifications |
| 172 | + rm -rf vendor |
| 173 | + git checkout vendor go.mod |
| 174 | + if [ -e go.sum ]; then |
| 175 | + git checkout go.sum |
| 176 | + fi |
| 177 | + popd |
| 178 | + echo "===> Done building migration $mig with plugins" |
| 179 | +} |
| 180 | + |
| 181 | +migration="$(get_migration ${MIGRATION})" |
| 182 | +if [ -z "$migration" ]; then |
| 183 | + echo "migration $migration does not exist" >&2 |
| 184 | + exit 1 |
| 185 | +fi |
| 186 | + |
| 187 | +clone_ipfs "$migration" |
| 188 | +if [ $? -ne 0 ]; then |
| 189 | + continue |
| 190 | +fi |
| 191 | + |
| 192 | +shift 1 |
| 193 | +for repo in "$@"; do |
| 194 | + bundle_ipfs_plugin "$repo" |
| 195 | +done |
| 196 | + |
| 197 | +build_migration "$migration" |
0 commit comments