Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploy to Docker with Dockerfile and script #5

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules/
logs/

*.gz
54 changes: 54 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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 [email protected]; \
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"]
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
110 changes: 110 additions & 0 deletions start.js
Original file line number Diff line number Diff line change
@@ -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);
}
})();
4 changes: 4 additions & 0 deletions start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

sudo /usr/sbin/service postgresql start
/usr/bin/pm2-runtime start leasehold-core