Skip to content

Commit

Permalink
Merge pull request #241 from valory-xyz/fix/tendermint_installation
Browse files Browse the repository at this point in the history
Fix missing tendermint installation
  • Loading branch information
jmoreira-valory authored Jul 25, 2024
2 parents 54ef518 + 8eaa5bb commit f2870cc
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 194 deletions.
2 changes: 1 addition & 1 deletion electron/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const isProd = !isDev;
// Paths
const dotOperateDirectory = isProd
? path.join(os.homedir(), '.operate')
: '.operate';
: path.join(process.cwd(), '.operate');

const paths = {
dotOperateDirectory,
Expand Down
205 changes: 23 additions & 182 deletions electron/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const sudo = require('sudo-prompt');
const process = require('process');
const axios = require('axios');
const { spawnSync } = require('child_process');
const { logger } = require('./logger');

const { paths } = require('./constants');

Expand All @@ -13,17 +14,19 @@ const { paths } = require('./constants');
* - use "" (nothing as a suffix) for latest release candidate, for example "0.1.0rc26"
* - use "alpha" for alpha release, for example "0.1.0rc26-alpha"
*/
const OlasMiddlewareVersion = '0.1.0rc71';
const OlasMiddlewareVersion = '0.1.0rc87';

const Env = {
...process.env,
PATH: `${process.env.PATH}:/opt/homebrew/bin:/usr/local/bin`,
HOMEBREW_NO_AUTO_UPDATE: '1',
};

const SudoOptions = {
name: 'Pearl',
env: Env,
};

const TendermintUrls = {
darwin: {
x64: 'https://github.com/tendermint/tendermint/releases/download/v0.34.19/tendermint_0.34.19_darwin_amd64.tar.gz',
Expand All @@ -48,19 +51,8 @@ function getBinPath(command) {
.trim();
}

function appendInstallationLog(log) {
fs.appendFileSync(paths.OperateInstallationLog, `${log}\n`, {
encoding: 'utf-8',
});
return log;
}

function runCmdUnix(command, options) {
console.log(
appendInstallationLog(
`Running ${command} with options ${JSON.stringify(options)}`,
),
);
logger.electron(`Running ${command} with options ${JSON.stringify(options)}`);
let bin = getBinPath(command);
if (!bin) {
throw new Error(`Command ${command} not found; Path : ${Env.PATH}`);
Expand All @@ -72,9 +64,9 @@ function runCmdUnix(command, options) {
Error: ${output.error}; Stdout: ${output.stdout}; Stderr: ${output.stderr}`,
);
}
console.log(appendInstallationLog(`Executed ${command} ${options} with`));
console.log(appendInstallationLog(`===== stdout ===== \n${output.stdout}`));
console.log(appendInstallationLog(`===== stderr ===== \n${output.stderr}`));
logger.electron(`Executed ${command} ${options} with`);
logger.electron(`===== stdout ===== \n${output.stdout}`);
logger.electron(`===== stderr ===== \n${output.stderr}`);
}

function runSudoUnix(command, options) {
Expand All @@ -98,15 +90,9 @@ function runSudoUnix(command, options) {
Error: ${output.error}; Stdout: ${output.stdout}; Stderr: ${output.stderr}`,
);
}
console.log(
appendInstallationLog(`Executed ${command} ${options} with`),
);
console.log(
appendInstallationLog(`===== stdout ===== \n${output.stdout}`),
);
console.log(
appendInstallationLog(`===== stderr ===== \n${output.stderr}`),
);
logger.electron(`Executed ${command} ${options} with`);
logger.electron(`===== stdout ===== \n${output.stdout}`);
logger.electron(`===== stderr ===== \n${output.stderr}`);
resolve();
},
);
Expand Down Expand Up @@ -137,90 +123,28 @@ async function downloadFile(url, dest) {
}

async function installTendermintUnix() {
logger.electron(`Installing tendermint for ${os.platform()}-${process.arch}`);
const cwd = process.cwd();
process.chdir(paths.tempDir);

console.log(
appendInstallationLog(
`Installing tendermint for ${os.platform()}-${process.arch}`,
),
);
const url = TendermintUrls[os.platform()][process.arch];

console.log(
appendInstallationLog(`Downloading ${url}, might take a while...`),
);
logger.electron(`Downloading ${url} to ${paths.tempDir}. This might take a while...`);
await downloadFile(url, `${paths.tempDir}/tendermint.tar.gz`);

console.log(appendInstallationLog(`Installing tendermint binary`));
logger.electron(`Installing tendermint binary`);
runCmdUnix('tar', ['-xvf', 'tendermint.tar.gz']);

// TOFIX: Install tendermint in .operate instead of globally
if (!Env.CI) {
if (!fs.existsSync('/usr/local/bin')) {
await runSudoUnix('mkdir', '/usr/local/bin');
}
await runSudoUnix('install', 'tendermint /usr/local/bin/tendermint');
await runSudoUnix('install', `${paths.tempDir}/tendermint /usr/local/bin/tendermint`);
}
process.chdir(cwd);
}

function createVirtualEnvUnix(path) {
runCmdUnix('python3.10', ['-m', 'venv', path]);
}

function isPythonInstalledUbuntu() {
return Boolean(getBinPath('python3.10'));
}

function isGitInstalledUbuntu() {
return Boolean(getBinPath('git'));
}

function installPythonUbuntu() {
return runSudoUnix('apt', 'install -y python3.10 python3.10-dev python3-pip');
}

function installGitUbuntu() {
return runSudoUnix('apt', 'install -y git');
}

function installOperatePackageUnix(path) {
runCmdUnix(`${path}/venv/bin/python3.10`, [
'-m',
'pip',
'install',
`olas-operate-middleware==${OlasMiddlewareVersion}`,
]);
}

function reInstallOperatePackageUnix(path) {
console.log(appendInstallationLog('Reinstalling pearl CLI'));
runCmdUnix(`${path}/venv/bin/python3.10`, [
'-m',
'pip',
'install',
`olas-operate-middleware==${OlasMiddlewareVersion}`,
'--force-reinstall',
]);
}

function installOperateCli(path) {
let installPath = `${path}/operate`;
if (fs.existsSync(installPath)) {
fs.rmSync(installPath);
}
return new Promise((resolve, _reject) => {
fs.copyFile(
`${paths.dotOperateDirectory}/venv/bin/operate`,
installPath,
function (error, _stdout, _stderr) {
resolve(!error);
},
);
});
}

function createDirectory(path) {
if (fs.existsSync(path)) {
return;
Expand All @@ -232,118 +156,35 @@ function createDirectory(path) {
});
}

function writeVersion() {
fs.writeFileSync(paths.versionFile, OlasMiddlewareVersion);
}

function versionBumpRequired() {
if (!fs.existsSync(paths.versionFile)) {
return true;
}
const olasMiddlewareVersionInFile = fs
.readFileSync(paths.versionFile)
.toString();
return olasMiddlewareVersionInFile != OlasMiddlewareVersion;
}

function removeLogFile() {
if (fs.existsSync(paths.LogFile)) {
fs.rmSync(paths.LogFile);
}
}

function removeInstallationLogFile() {
if (fs.existsSync(paths.OperateInstallationLog)) {
fs.rmSync(paths.OperateInstallationLog);
}
}

/*******************************/
// NOTE: "Installing" is string matched in loading.html to detect installation
/*******************************/

async function setupDarwin(ipcChannel) {
removeInstallationLogFile();
console.log(appendInstallationLog('Creating required directories'));
logger.electron('Creating required directories');
await createDirectory(`${paths.dotOperateDirectory}`);
await createDirectory(`${paths.dotOperateDirectory}/temp`);
await createDirectory(`${paths.tempDir}`);

console.log(appendInstallationLog('Checking tendermint installation'));
logger.electron('Checking tendermint installation');
if (!isTendermintInstalledUnix()) {
ipcChannel.send('response', 'Installing Pearl Daemon');
console.log(appendInstallationLog('Installing tendermint'));
logger.electron('Installing tendermint');
await installTendermintUnix();
}

console.log(appendInstallationLog('Checking if upgrade is required'));
if (versionBumpRequired()) {
console.log(
appendInstallationLog(
`Upgrading pearl daemon to ${OlasMiddlewareVersion}`,
),
);
writeVersion();
removeLogFile();
// reInstallOperatePackageUnix(OperateDirectory);
}
}

// TODO: Add Tendermint installation
async function setupUbuntu(ipcChannel) {
removeInstallationLogFile();

console.log(appendInstallationLog('Checking python installation'));
if (!isPythonInstalledUbuntu()) {
ipcChannel.send('response', 'Installing Pearl Daemon');
console.log(appendInstallationLog('Installing Python'));
await installPythonUbuntu(paths.dotOperateDirectory);
}

console.log(appendInstallationLog('Checking git installation'));
if (!isGitInstalledUbuntu()) {
ipcChannel.send('response', 'Installing Pearl Daemon');
console.log(appendInstallationLog('Installing git'));
await installGitUbuntu(paths.dotOperateDirectory);
}

console.log(appendInstallationLog('Creating required directories'));
logger.electron('Creating required directories');
await createDirectory(`${paths.dotOperateDirectory}`);
await createDirectory(`${paths.dotOperateDirectory}/temp`);
await createDirectory(`${paths.tempDir}`);

console.log(appendInstallationLog('Checking tendermint installation'));
logger.electron('Checking tendermint installation');
if (!isTendermintInstalledUnix()) {
ipcChannel.send('response', 'Installing Pearl Daemon');
console.log(appendInstallationLog('Installing tendermint'));
logger.electron('Installing tendermint');
await installTendermintUnix();
}

if (!fs.existsSync(paths.venvDir)) {
ipcChannel.send('response', 'Installing Pearl Daemon');
console.log(appendInstallationLog('Creating virtual environment'));
createVirtualEnvUnix(paths.venvDir);

console.log(appendInstallationLog('Installing pearl backend'));
installOperatePackageUnix(paths.dotOperateDirectory);
}

console.log(appendInstallationLog('Checking if upgrade is required'));
if (versionBumpRequired()) {
console.log(
appendInstallationLog(
`Upgrading pearl daemon to ${OlasMiddlewareVersion}`,
),
);
reInstallOperatePackageUnix(paths.dotOperateDirectory);
writeVersion();
removeLogFile();
}

if (!fs.existsSync(`${paths.dotOperateDirectory}/venv/bin/operate`)) {
reInstallOperatePackageUnix(paths.dotOperateDirectory);
}

console.log(appendInstallationLog('Installing pearl CLI'));
await installOperateCli('/usr/local/bin');
}

module.exports = {
Expand Down
17 changes: 8 additions & 9 deletions electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const http = require('http');
const AdmZip = require('adm-zip');
const { TRAY_ICONS, TRAY_ICONS_PATHS } = require('./icons');

const { Env } = require('./install');
const { setupDarwin, setupUbuntu, Env } = require('./install');

const { paths } = require('./constants');
const { killProcesses } = require('./processes');
Expand Down Expand Up @@ -427,14 +427,13 @@ ipcMain.on('check', async function (event, _argument) {
// Setup
try {
event.sender.send('response', 'Checking installation');
if (!isDev) {
if (platform === 'darwin') {
//await setupDarwin(event.sender);
} else if (platform === 'win32') {
// TODO
} else {
//await setupUbuntu(event.sender);
}

if (platform === 'darwin') {
await setupDarwin(event.sender);
} else if (platform === 'win32') {
// TODO
} else {
await setupUbuntu(event.sender);
}

if (isDev) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@
"start:frontend": "cd frontend && yarn start",
"test:frontend": "cd frontend && yarn test"
},
"version": "0.1.0-rc71"
"version": "0.1.0-rc87"
}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "olas-operate-middleware"
version = "0.1.0-rc71"
version = "0.1.0-rc87"
description = ""
authors = ["David Vilela <[email protected]>", "Viraj Patel <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit f2870cc

Please sign in to comment.