|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -Eeuo pipefail |
| 3 | + |
| 4 | +# pin-params.sh |
| 5 | +# |
| 6 | +# - Add the directory of params to the local ipfs node |
| 7 | +# - Grab the CID for the previous params from proofs.filecoin.io |
| 8 | +# - Add the old params as a `prev` dir to the new params dir to keep them around. |
| 9 | +# - Pin the new cid on cluster |
| 10 | +# - Publish the new cid as a dnslink to proofs.filecoin.io |
| 11 | +# - The gateways will pin the new dir by checking proofs.filecoin.io hourly. |
| 12 | +# |
| 13 | +# Requires: |
| 14 | +# - `ipfs-cluster-ctl` - download from https://dist.ipfs.io/#ipfs-cluster-ctl |
| 15 | +# - `npx`, as provide `npm` >= v6 |
| 16 | +# - `ipfs` |
| 17 | +# |
| 18 | +# You _must_ provide the following env vars |
| 19 | +# |
| 20 | +# - CLUSTER_TOKEN - the basic auth string as "username:password" |
| 21 | +# - DNSIMPLE_TOKEN - an api key for a dnsimple account with a zone for proofs.filecoin.io |
| 22 | +# |
| 23 | +# Optional: you can override the input dir by passing a path as the first param. |
| 24 | +# |
| 25 | +# Usage: |
| 26 | +# CLUSTER_TOKEN="user:pass" DNSIMPLE_TOKEN="xyz" ./pin-params.sh |
| 27 | +# |
| 28 | + |
| 29 | +INPUT_DIR=${1:-"/var/tmp/filecoin-proof-parameters"} |
| 30 | +: "${CLUSTER_TOKEN:?please set CLUSTER_TOKEN env var}" |
| 31 | +: "${DNSIMPLE_TOKEN:?please set DNSIMPLE_TOKEN env var}" |
| 32 | + |
| 33 | +echo "checking $INPUT_DIR" |
| 34 | + |
| 35 | +# Grab the version number from the files in the dir. |
| 36 | +# Fail if more than 1 version or doesnt match a version string like vNN, e.g v12 |
| 37 | +if ls -A $INPUT_DIR &> /dev/null; then |
| 38 | + # version will be a list if there is more than one... |
| 39 | + VERSION=$(ls $INPUT_DIR | sort -r | cut -c 1-3 | uniq) |
| 40 | + echo found $VERSION |
| 41 | + |
| 42 | + if [[ $(echo $VERSION | wc -w) -eq 1 && $VERSION =~ ^v[0-9]+ ]]; then |
| 43 | + # we have 1 version, lets go... |
| 44 | + COUNT=$(ls -l $INPUT_DIR | wc -l | xargs echo -n) |
| 45 | + echo "adding $COUNT files to ipfs..." |
| 46 | + |
| 47 | + else |
| 48 | + echo "Error: input dir should contain just the current version of the params" |
| 49 | + exit 1 |
| 50 | + fi |
| 51 | +else |
| 52 | + echo "Error: input dir '$INPUT_DIR' should contain the params" |
| 53 | + exit 1 |
| 54 | +fi |
| 55 | + |
| 56 | +CLUSTER_HOST="/dnsaddr/cluster.ipfs.io" |
| 57 | +CLUSTER_PRIMARY="/dns4/cluster0.fsn.dwebops.pub/udp/4001/quic/p2p/QmUEMvxS2e7iDrereVYc5SWPauXPyNwxcy9BXZrC1QTcHE" |
| 58 | +CLUSTER_PIN_NAME="filecoin-proof-parameters-$VERSION" |
| 59 | +DNSLINK_DOMAIN="proofs.filecoin.io" |
| 60 | + |
| 61 | +# Pin to ipfs |
| 62 | +ROOT_CID=$(ipfs add --quieter --recursive $INPUT_DIR) |
| 63 | +echo "ok! root cid is $ROOT_CID" |
| 64 | + |
| 65 | +echo "linking to previous version..." |
| 66 | +# trim off the /ipfs prefix, so it's consistent with the other vars |
| 67 | +PREV_CID=$(ipfs dns $DNSLINK_DOMAIN | cut -c 7-) |
| 68 | + |
| 69 | +# Add a `prev` dir to the new params dir that links back to the older params |
| 70 | +LINKED_CID=$(ipfs object patch add-link $ROOT_CID prev $PREV_CID) |
| 71 | + |
| 72 | +# guard against multiple runs with no change... |
| 73 | +# if we remove the `prev` dir from the last published PREV_CID and it matches |
| 74 | +# the current ROOT_DIR, then dont nest it inside itself again. |
| 75 | +if ipfs object stat $PREV_CID/prev > /dev/null; then |
| 76 | + PREV_ROOT_CID=$(ipfs object patch rm-link $PREV_CID prev) |
| 77 | + if [[ $PREV_ROOT_CID == "$ROOT_CID" ]]; then |
| 78 | + LINKED_CID=$PREV_CID |
| 79 | + echo "linked cid is already published, re-using $PREV_CID" |
| 80 | + echo "continuing to ensure $PREV_CID is pinned to cluster" |
| 81 | + fi |
| 82 | +fi |
| 83 | + |
| 84 | +echo "ok! linked cid is $LINKED_CID" |
| 85 | +echo "pinning linked cid to cluster..." |
| 86 | + |
| 87 | +# Connect to cluster to speed up discovery |
| 88 | +ipfs swarm connect $CLUSTER_PRIMARY |
| 89 | + |
| 90 | +# Ask cluster to fetch the linked cid from us |
| 91 | +ipfs-cluster-ctl \ |
| 92 | + --host $CLUSTER_HOST \ |
| 93 | + --basic-auth $CLUSTER_TOKEN \ |
| 94 | + pin add $LINKED_CID \ |
| 95 | + --name $CLUSTER_PIN_NAME \ |
| 96 | + --wait |
| 97 | + |
| 98 | +# Publist the new cid to the dnslink |
| 99 | +npx dnslink-dnsimple --domain proofs.filecoin.io --link "/ipfs/$LINKED_CID" |
| 100 | + |
| 101 | +echo "done!" |
0 commit comments