Advertisement
Guest User

lamassu-server install

a guest
Oct 11th, 2018
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.07 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. set -e
  3.  
  4. export LOG_FILE=/tmp/install.log
  5.  
  6. CERT_DIR=/etc/ssl/certs
  7. KEY_DIR=/etc/ssl/private
  8. CONFIG_DIR=/etc/lamassu
  9. MIGRATE_STATE_PATH=$CONFIG_DIR/.migrate
  10. LAMASSU_CA_PATH=$CERT_DIR/Lamassu_CA.pem
  11. CA_KEY_PATH=$KEY_DIR/Lamassu_OP_Root_CA.key
  12. CA_PATH=$CERT_DIR/Lamassu_OP_Root_CA.pem
  13. SERVER_KEY_PATH=$KEY_DIR/Lamassu_OP.key
  14. SERVER_CERT_PATH=$CERT_DIR/Lamassu_OP.pem
  15. SEEDS_DIR=$HOME/seeds
  16. SEED_FILE=$SEEDS_DIR/seed.txt
  17. BACKUP_DIR=/var/backups/postgresql
  18. BLOCKCHAIN_DIR=/mnt/blockchains
  19. OFAC_DATA_DIR=/var/lamassu/ofac
  20.  
  21. # Look into http://unix.stackexchange.com/questions/140734/configure-localtime-dpkg-reconfigure-tzdata
  22.  
  23. decho () {
  24. echo `date +"%H:%M:%S"` $1
  25. echo `date +"%H:%M:%S"` $1 >> $LOG_FILE
  26. }
  27.  
  28. retry() {
  29. local -r -i max_attempts="$1"; shift
  30. local -r cmd="$@"
  31. local -i attempt_num=1
  32.  
  33. until $cmd
  34. do
  35. if (( attempt_num == max_attempts ))
  36. then
  37. echo
  38. echo "****************************************************************"
  39. echo "Attempt $attempt_num failed and there are no more attempts left! ($cmd)"
  40. return 1
  41. else
  42. echo
  43. echo "****************************************************************"
  44. echo "Attempt $attempt_num failed! Trying again in $attempt_num seconds..."
  45. sleep $(( attempt_num++ ))
  46. fi
  47. done
  48. }
  49.  
  50. rm -f $LOG_FILE
  51.  
  52. cat <<'FIG'
  53. _
  54. | | __ _ _ __ ___ __ _ ___ ___ _ _ ___ ___ _ ____ _____ _ __
  55. | |/ _` | '_ ` _ \ / _` / __/ __| | | |_____/ __|/ _ \ '__\ \ / / _ \ '__|
  56. | | (_| | | | | | | (_| \__ \__ \ |_| |_____\__ \ __/ | \ V / __/ |
  57. |_|\__,_|_| |_| |_|\__,_|___/___/\__,_| |___/\___|_| \_/ \___|_|
  58. FIG
  59.  
  60. echo -e "\nStarting \033[1mlamassu-server\033[0m install. This will take a few minutes...\n"
  61.  
  62. if [ "$(whoami)" != "root" ]; then
  63. echo -e "This script has to be run as \033[1mroot\033[0m user"
  64. exit 3
  65. fi
  66.  
  67. release=$(lsb_release -rs)
  68. processor=$(uname -i)
  69. if [ "$release" != "16.04" ] || [ "$processor" != "x86_64" ]; then
  70. echo "You're attempting to install on an unsupported Linux distribution or release."
  71. uname -a
  72. echo "Please return to DigitalOcean and create a droplet running Ubuntu 16.04 x64 instead."
  73. exit 1
  74. fi
  75.  
  76. decho "stopping lamassu-server"
  77. supervisorctl stop lamassu-server >> ${LOG_FILE} 2>&1
  78. supervisorctl stop lamassu-admin-server >> ${LOG_FILE} 2>&1
  79.  
  80. decho "unlinking ${NPM_BIN}/lamassu* old executables"
  81. find ${NPM_BIN} -type l \( -name "lamassu-*" -or -name "hkdf" \) -exec rm -fv {} \; >> ${LOG_FILE} 2>&1
  82.  
  83. if [ -d "/usr/lib/node_modules/lamassu-server" ]; then
  84. decho "renaming old lamassu-server instance to lamassu-server-old"
  85. mv -v "/usr/lib/node_modules/lamassu-server" "/usr/lib/node_modules/lamassu-server-old" >> ${LOG_FILE} 2>&1
  86. fi
  87.  
  88. # So we don't run out of memory
  89. decho "Enabling swap file for install only..."
  90. fallocate -l 1G /swapfile >> $LOG_FILE 2>&1
  91. chmod 600 /swapfile >> $LOG_FILE 2>&1
  92. mkswap /swapfile >> $LOG_FILE 2>&1
  93. swapon /swapfile >> $LOG_FILE 2>&1
  94.  
  95. IP=$(ifconfig eth0 | grep "inet" | grep -v "inet6" | awk -F: '{print $2}' | awk '{print $1}')
  96.  
  97. decho "Updating system..."
  98. sleep 10
  99. curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - >> $LOG_FILE 2>&1
  100. apt update >> $LOG_FILE 2>&1
  101.  
  102. decho "Installing necessary packages..."
  103. apt install nodejs python-minimal build-essential supervisor postgresql libpq-dev -y -q >> $LOG_FILE 2>&1
  104.  
  105. decho "Generating seed..."
  106. mkdir -p $SEEDS_DIR >> $LOG_FILE 2>&1
  107. SEED=$(openssl rand -hex 32)
  108. echo $SEED > $SEED_FILE
  109.  
  110. decho "Installing latest npm package manager for node..."
  111. retry 3 npm -g --unsafe-perm install npm@5 >> $LOG_FILE 2>&1
  112. NODE_MODULES=$(npm -g root)
  113. NPM_BIN=$(npm -g bin)
  114.  
  115. decho "Installing lamassu-server..."
  116. retry 3 npm -g --unsafe-perm install lamassu/lamassu-server#master >> $LOG_FILE 2>&1
  117.  
  118. decho "Creating postgres user..."
  119. POSTGRES_PW=$(hkdf postgres-pw $SEED)
  120. su -l postgres >> $LOG_FILE 2>&1 <<EOF
  121. psql -c "CREATE ROLE lamassu_pg WITH LOGIN SUPERUSER PASSWORD '$POSTGRES_PW';"
  122. createdb lamassu
  123. EOF
  124.  
  125. mkdir -p $CERT_DIR >> $LOG_FILE 2>&1
  126. mkdir -p $CONFIG_DIR >> $LOG_FILE 2>&1
  127.  
  128. decho "Generating SSL certificates..."
  129.  
  130. openssl genrsa \
  131. -out $CA_KEY_PATH \
  132. 4096 >> $LOG_FILE 2>&1
  133.  
  134. openssl req \
  135. -x509 \
  136. -sha256 \
  137. -new \
  138. -nodes \
  139. -key $CA_KEY_PATH \
  140. -days 3560 \
  141. -out $CA_PATH \
  142. -subj "/C=IS/ST=/L=Reykjavik/O=Lamassu Operator CA/CN=lamassu-operator.is" \
  143. >> $LOG_FILE 2>&1
  144.  
  145. openssl genrsa \
  146. -out $SERVER_KEY_PATH \
  147. 4096 >> $LOG_FILE 2>&1
  148.  
  149. openssl req -new \
  150. -key $SERVER_KEY_PATH \
  151. -out /tmp/Lamassu_OP.csr.pem \
  152. -subj "/C=IS/ST=/L=Reykjavik/O=Lamassu Operator/CN=$IP" \
  153. -reqexts SAN \
  154. -sha256 \
  155. -config <(cat /etc/ssl/openssl.cnf \
  156. <(printf "[SAN]\nsubjectAltName=IP.1:$IP")) \
  157. >> $LOG_FILE 2>&1
  158.  
  159. openssl x509 \
  160. -req -in /tmp/Lamassu_OP.csr.pem \
  161. -CA $CA_PATH \
  162. -CAkey $CA_KEY_PATH \
  163. -CAcreateserial \
  164. -out $SERVER_CERT_PATH \
  165. -extfile <(cat /etc/ssl/openssl.cnf \
  166. <(printf "[SAN]\nsubjectAltName=IP.1:$IP")) \
  167. -extensions SAN \
  168. -days 3650 >> $LOG_FILE 2>&1
  169.  
  170. rm /tmp/Lamassu_OP.csr.pem
  171.  
  172. decho "Copying Lamassu certificate authority..."
  173. LAMASSU_CA_FILE=$NODE_MODULES/lamassu-server/Lamassu_CA.pem
  174. cp $LAMASSU_CA_FILE $LAMASSU_CA_PATH
  175.  
  176. mkdir -p $OFAC_DATA_DIR
  177.  
  178. cat <<EOF > $CONFIG_DIR/lamassu.json
  179. {
  180. "postgresql": "postgres://lamassu_pg:$POSTGRES_PW@localhost/lamassu",
  181. "seedPath": "$SEED_FILE",
  182. "lamassuCaPath": "$LAMASSU_CA_PATH",
  183. "caPath": "$CA_PATH",
  184. "certPath": "$SERVER_CERT_PATH",
  185. "keyPath": "$SERVER_KEY_PATH",
  186. "hostname": "$IP",
  187. "logLevel": "info",
  188. "migrateStatePath": "$MIGRATE_STATE_PATH",
  189. "blockchainDir": "$BLOCKCHAIN_DIR",
  190. "ofacDataDir": "$OFAC_DATA_DIR",
  191. "strike": {
  192. "baseUrl": "https://api.strike.acinq.co/api/"
  193. },
  194. "coinAtmRadar": {
  195. "url": "https://coinatmradar.info/api/lamassu/"
  196. }
  197. }
  198. EOF
  199.  
  200. decho "Setting up database tables..."
  201. lamassu-migrate >> $LOG_FILE 2>&1
  202.  
  203. decho "Setting up lamassu-admin..."
  204. ADMIN_REGISTRATION_URL=`lamassu-register admin 2>> $LOG_FILE`
  205. lamassu-apply-defaults >> $LOG_FILE 2>&1
  206.  
  207. decho "Setting up backups..."
  208. BIN=$(npm -g bin)
  209. BACKUP_CMD=$BIN/lamassu-backup-pg
  210. mkdir -p $BACKUP_DIR
  211. BACKUP_CRON="@daily $BACKUP_CMD > /dev/null"
  212. (crontab -l 2>/dev/null || echo -n ""; echo "$BACKUP_CRON") | crontab - >> $LOG_FILE 2>&1
  213. $BACKUP_CMD >> $LOG_FILE 2>&1
  214.  
  215. decho "Setting up firewall..."
  216. ufw allow ssh >> $LOG_FILE 2>&1
  217. ufw allow 443/tcp >> $LOG_FILE 2>&1 # Admin
  218. ufw allow 3000/tcp >> $LOG_FILE 2>&1 # Server
  219. ufw allow 8071/tcp >> $LOG_FILE 2>&1 # Lamassu support
  220. ufw -f enable >> $LOG_FILE 2>&1
  221.  
  222. decho "Setting up supervisor..."
  223. cat <<EOF > /etc/supervisor/conf.d/lamassu-server.conf
  224. [program:lamassu-server]
  225. command=${NPM_BIN}/lamassu-server
  226. autostart=true
  227. autorestart=true
  228. stderr_logfile=/var/log/supervisor/lamassu-server.err.log
  229. stdout_logfile=/var/log/supervisor/lamassu-server.out.log
  230. environment=HOME="/root"
  231. EOF
  232.  
  233. cat <<EOF > /etc/supervisor/conf.d/lamassu-admin-server.conf
  234. [program:lamassu-admin-server]
  235. command=${NPM_BIN}/lamassu-admin-server
  236. autostart=true
  237. autorestart=true
  238. stderr_logfile=/var/log/supervisor/lamassu-admin-server.err.log
  239. stdout_logfile=/var/log/supervisor/lamassu-admin-server.out.log
  240. environment=HOME="/root"
  241. EOF
  242.  
  243. service supervisor restart >> $LOG_FILE 2>&1
  244.  
  245. decho "Disabling swap file..."
  246. swapoff /swapfile >> $LOG_FILE 2>&1
  247.  
  248. # disable exitting on error in case DO changes motd scripts
  249. set +e
  250. chmod -x /etc/update-motd.d/*-release-upgrade
  251. chmod -x /etc/update-motd.d/*-updates-available
  252. chmod -x /etc/update-motd.d/*-reboot-required
  253. chmod -x /etc/update-motd.d/*-help-text
  254. chmod -x /etc/update-motd.d/*-cloudguest
  255. set -e
  256.  
  257. echo
  258. decho "Done! Now it's time to configure Lamassu stack."
  259. echo
  260. echo -e "\n*** IMPORTANT ***"
  261. echo "In a private space, run lamassu-mnemonic, write down the words"
  262. echo "and keep them in a safe place."
  263. echo
  264. echo "This secret will allow you to retrieve system passwords, including "
  265. echo "the keys to some of your crypto accounts."
  266. echo
  267. echo
  268. echo "Activation URL for lamassu-admin:"
  269. echo $ADMIN_REGISTRATION_URL
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement