diff --git a/.gitignore b/.gitignore index 9240b19..877c800 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ node_modules/ logs/ - +*.gz diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..46146c2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,54 @@ +FROM ubuntu:20.04 + +# not to prompt `Configuring tzdata` +ARG DEBIAN_FRONTEND=noninteractive + +RUN useradd -ms /bin/bash leasehold; \ + apt-get update; \ + apt-get install -qqy git curl wget build-essential gzip lsb-release sudo; \ + curl -sL https://deb.nodesource.com/setup_10.x | bash -; \ + wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - && sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -sc)-pgdg main" > /etc/apt/sources.list.d/PostgreSQL.list'; \ + apt-get update; \ + apt-get install -qqy postgresql-10 nodejs; \ + sed -i 's/max_connections = 100/max_connections = 300/g' /etc/postgresql/10/main/postgresql.conf; \ + sed -i 's/shared_buffers = 128MB/shared_buffers = 256MB/g' /etc/postgresql/10/main/postgresql.conf; \ + echo 'leasehold ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers; \ + npm install pm2 -g; \ + npm i -g lisk-commander@2.2.3; \ + usermod -aG sudo leasehold + +USER postgres + +RUN service postgresql start && \ + psql -c "CREATE USER leasehold" && \ + psql -c "ALTER ROLE leasehold WITH SUPERUSER" && \ + psql -c "CREATE DATABASE lisk_main OWNER leasehold" && \ + psql -c "CREATE DATABASE leasehold_main OWNER leasehold" && \ + psql -c "ALTER USER leasehold WITH PASSWORD 'password'" + +USER leasehold + +RUN mkdir -p /home/leasehold/leasehold-core +RUN chown -R leasehold:leasehold /home/leasehold + +COPY --chown=leasehold . /home/leasehold/leasehold-core/ +WORKDIR /home/leasehold/ + +RUN sudo service postgresql start && \ + gzip --decompress --to-stdout ./leasehold-core/leasehold_main_backup_27022021.gz | psql -U leasehold leasehold_main -w && \ + gzip --decompress --to-stdout ./leasehold-core/lisk_main_backup-14576795 | psql -U leasehold lisk_main -w -w; \ + rm -f ./leasehold-core/leasehold_main_backup_27022021.gz; \ + rm -f ./leasehold-core/lisk_main_backup-14576795 + +RUN npm install --prefix ./leasehold-core; \ + lisk config:set api.nodes http://2.56.213.101:8010/ + +EXPOSE 5432 +EXPOSE 8001 +EXPOSE 8010 + +RUN ls + +RUN chmod +x ./leasehold-core/start.sh + +CMD ["./leasehold-core/start.sh"] diff --git a/package.json b/package.json index a2def19..d73029e 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "A full Leasehold node implementation", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "start": "node start.js" }, "repository": { "type": "git", diff --git a/start.js b/start.js new file mode 100644 index 0000000..f73d2e2 --- /dev/null +++ b/start.js @@ -0,0 +1,110 @@ +const util = require('util'); +const { spawn } = require('child_process'); +const readline = require('readline'); +const exec = util.promisify(require('child_process').exec); + +const NETWORK = 'mainnet'; +const LEASEHOLD_SNAPSHOT = 'leasehold_main_backup_27022021.gz'; +const LISK_SNAPSHOT = 'lisk_main_backup-14576795.gz'; +const CONTAINER_NAME = 'leasehold-core'; +const IMAGE_NAME = 'leasehold'; + +const commandLog = (msg, warning = false) => + console.log(`\n${warning ? '\x1b[31m' : ''}\x1b[1m${msg}\x1b[0m`); + +const errorLog = (msg) => console.error(`\x1b[31m\x1b[1m${msg}\x1b[0m`); + +const execCommand = (command) => { + console.log(`Executing command: ${command}`); + return new Promise((res, rej) => { + command = command.split(' '); + const process = spawn(command[0], command.slice(1)); + readline + .createInterface({ + input: process.stdout, + terminal: false, + }) + .on('line', function (line) { + console.log(line); + }); + process.on('close', (code) => res(code)); + process.on('error', (err) => rej(err)); + }); + + // const { stdout, stderr } = await exec(command, { shell: true }); + // if (stderr) { + // errorLog(stderr); + // } + + // console.log(stdout); +}; + +(async () => { + try { + const { stdout: ls } = await exec(`ls`); + if (!ls.includes(LISK_SNAPSHOT)) { + commandLog( + 'IMPORTANT: Getting Lisk snapshot. This process will take a while!', + true, + ); + await execCommand( + `wget --no-check-certificate https://snapshots.lisk.io/${NETWORK}/${LISK_SNAPSHOT}`, + ); + } else { + commandLog(`Skipping ${LISK_SNAPSHOT} it already exists.`); + } + + if (!ls.includes(LEASEHOLD_SNAPSHOT)) { + commandLog( + 'IMPORTANT: Getting Leasehold snapshot. This process will take a while!', + true, + ); + await execCommand( + `wget --no-check-certificate https://testnet.leasehold.io/snapshots/${NETWORK}/${LEASEHOLD_SNAPSHOT}`, + ); + } else { + commandLog(`Skipping ${LEASEHOLD_SNAPSHOT} it already exists.`); + } + + commandLog('Building docker container'); + commandLog( + 'IMPORTANT: This process will take a while!', + true, + ); + await execCommand(`docker build -t ${IMAGE_NAME} .`); + + commandLog('Removing container if exists.'); + await execCommand(`docker rm --force ${CONTAINER_NAME}`); + + commandLog(`Running container with name ${CONTAINER_NAME}`); + await execCommand( + `docker run -d -p 8010:8010 -p 8001:8001 --name ${CONTAINER_NAME} ${IMAGE_NAME}`, + ); + + commandLog('Getting IP of container'); + const { stdout } = await exec(`docker inspect ${CONTAINER_NAME}`); + const ip = JSON.parse(stdout)[0].NetworkSettings.IPAddress; + if (ip === '') + throw new Error('No IP Address available, container probably exited.'); + + commandLog(`IP Found: ${ip}`) + + // commandLog( + // 'IMPORTANT: Writing the snapshot to the DB. This process might take a while!', + // true, + // ); + // await execCommand( + // `gzip --decompress --to-stdout ./${LISK_SNAPSHOT} | psql -U leasehold -h ${ip} lisk_main -w`, + // ); + + // commandLog( + // 'IMPORTANT: Writing the snapshot to the DB. This process might take a while!', + // true, + // ); + // await execCommand( + // `gzip --decompress --to-stdout ./${LEASEHOLD_SNAPSHOT} | psql -U leasehold -h ${ip} leasehold_main -w`, + // ); + } catch (err) { + errorLog(err.message); + } +})(); diff --git a/start.sh b/start.sh new file mode 100644 index 0000000..592a7ae --- /dev/null +++ b/start.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +sudo /usr/sbin/service postgresql start +/usr/bin/pm2-runtime start leasehold-core