1+ #! /bin/bash
2+ # <UDF name="username" Label="Username" example="lbry" />
3+ # <UDF name="password" Label="Password" example="ASDF123!@#" />
4+ # <UDF name="private_ip_prefix" Label="PrivateIPPrefix" example="192" default="192" />
5+ # <UDF name="snapshot_url" Label="SnapshotURL" default="https://snapshots.lbry.com/blockchain/lbcd/data-1052K.zip" />
6+ # <UDF name="AWS_ACCESS_KEY_ID" Label="AccessKey" default="" />
7+ # <UDF name="AWS_SECRET_ACCESS_KEY" Label="SecretKey" default="" />
8+ # <UDF name="endpoint_url" Label="BlockStorageEndpoint" default="" />
9+ # <UDF name="docker_compose_file_url" Label="ConfigURL" default="https://picardtek-linode-storage.us-east-1.linodeobjects.com/config/docker-compose-lbcd.yml" />
10+
11+ source < ssinclude StackScriptID=1>
12+
13+ # For debugging
14+ exec > >( tee -i /var/log/stackscript.log) 2>&1
15+ set -xeo pipefail
16+
17+ function user_add_sudo {
18+ USERNAME=" $1 "
19+ USERPASS=" $2 "
20+ if [ ! -n " $USERNAME " ] || [ ! -n " $USERPASS " ]; then
21+ echo " No new username and/or password entered"
22+ return 1;
23+ fi
24+ adduser " $USERNAME " --disabled-password --gecos " "
25+ echo " $USERNAME :$USERPASS " | chpasswd
26+ apt-get install -y sudo
27+ usermod -aG sudo " $USERNAME "
28+ }
29+
30+ function download_snapshot {
31+ if [ -z " ${AWS_ACCESS_KEY_ID} " ]; then
32+ wget " ${SNAPSHOT_URL} "
33+ else
34+ echo " [default]
35+ access_key = ${AWS_ACCESS_KEY_ID}
36+ secret_key = ${AWS_SECRET_ACCESS_KEY} " > ~ /.s3cfg
37+ if [ -z " ${ENDPOINT_URL} " ]; then
38+ s4cmd --verbose get " ${SNAPSHOT_URL} "
39+ else
40+ s4cmd --verbose get " ${SNAPSHOT_URL} " --endpoint-url " ${ENDPOINT_URL} "
41+ fi
42+ fi
43+ }
44+
45+ function download_and_start {
46+ download_snapshot
47+ # get the snapshot data into place
48+ SNAPSHOT_FILE_NAME=$( echo " ${SNAPSHOT_URL} " | rev | cut -d/ -f1 | rev)
49+ unzip " ${SNAPSHOT_FILE_NAME} " -d ~ /.lbcd/
50+ mv ~ /.lbcd/" ${SNAPSHOT_FILE_NAME} " ~ /.lbcd/data
51+ rm " ${SNAPSHOT_FILE_NAME} "
52+ # get our private ip
53+ PRIVATE_IP=$( ip addr | grep " ${PRIVATE_IP_PREFIX} " | cut -d' /' -f1 | rev | cut -d" " -f 1 | rev)
54+ # download the compose-compose and put our private ip in the for RPC endpoint
55+ wget " ${DOCKER_COMPOSE_FILE_URL} " -O - | sed ' s/REPLACE_ME/' " ${PRIVATE_IP} " ' /g' > docker-compose.yml
56+ # Create our volume and start lbcd
57+ docker volume create --driver local \
58+ --opt type=none \
59+ --opt device=~ /.lbcd\
60+ --opt o=bind lbcd
61+ docker-compose up -d
62+ }
63+ # add a non-root sudo user
64+ user_add_sudo " ${USERNAME} " " ${PASSWORD} "
65+ # Update and install dependencies
66+ sudo apt-get update && sudo apt-get upgrade -y
67+ sudo apt-get install -y unzip wget s4cmd
68+ apt install -y apt-transport-https ca-certificates curl software-properties-common && \
69+ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - && \
70+ add-apt-repository " deb [arch=amd64] https://download.docker.com/linux/ubuntu $( lsb_release -cs) stable" && \
71+ apt install -y docker-ce docker-ce-cli containerd.io && \
72+ systemctl enable docker && sudo systemctl start docker && \
73+ curl -L " https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$( uname -s) -$( uname -m) " -o /usr/local/bin/docker-compose && \
74+ chmod +x /usr/local/bin/docker-compose
75+ # make sure we can use docker
76+ usermod -aG docker $USERNAME
77+ export -f download_and_start
78+ export -f download_snapshot
79+ su " ${USERNAME} " -c ' bash -c "cd ~ && download_and_start"'
0 commit comments