Skip to content

Commit

Permalink
Merge pull request #151 from valory-xyz/feat/host-runtime
Browse files Browse the repository at this point in the history
Host runtime for agent deployments
  • Loading branch information
truemiller authored May 31, 2024
2 parents d83af3c + 2793e61 commit 48ba975
Show file tree
Hide file tree
Showing 16 changed files with 1,916 additions and 228 deletions.
541 changes: 541 additions & 0 deletions .gitleaks.toml

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions .gitleaksignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ daf41a143aa8c483db584ba1e7222e8eafec1d3b:backend/controller.py:generic-api-key:2
af77e930289cbc87987567bff0efc25936484df2:backend/controller.py:generic-api-key:354b04972639d66053109596d3b73a1d91688964ebb:electron/constants/publishOptions.js:github-fine-grained-pat:3
b04972639d66053109596d3b73a1d91688964ebb:electron/constants/publishOptions.js:github-fine-grained-pat:3
af77e930289cbc87987567bff0efc25936484df2:backend/controller.py:generic-api-key:354
e7de9ce0b902ed6d68f8c5b033d044f39b08f5a1:operate/data/contracts/service_staking_token/contract.yaml:generic-api-key:10
d8149e9b5b7bd6a7ed7bc1039900702f1d4f287b:operate/services/manage.py:generic-api-key:405
d8149e9b5b7bd6a7ed7bc1039900702f1d4f287b:operate/services/manage.py:generic-api-key:406
d8149e9b5b7bd6a7ed7bc1039900702f1d4f287b:operate/services/manage.py:generic-api-key:454
d8149e9b5b7bd6a7ed7bc1039900702f1d4f287b:operate/services/manage.py:generic-api-key:455
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ E0611: no-name-in-module
R0903: Too few public methods

[IMPORTS]
ignored-modules=os,io
ignored-modules=os,io,psutil

[DESIGN]
# min-public-methods=1
Expand Down
2 changes: 1 addition & 1 deletion electron/constants/publishOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const publishOptions = {
releaseType: 'draft',
token: process.env.GH_TOKEN,
private: false,
publishAutoUpdate: true,
publishAutoUpdate: false,
};

module.exports = { publishOptions };
94 changes: 75 additions & 19 deletions electron/install.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
// Installation helpers.

const https = require('https');
const path = require('path');
const fs = require('fs');
const os = require('os');
const sudo = require('sudo-prompt');
const process = require('process');
const { spawnSync } = require('child_process');
const axios = require("axios")

const Docker = require('dockerode');
const { spawnSync } = require('child_process');

const Version = '0.1.0rc26';
const Version = '0.1.0rc34';
const OperateDirectory = `${os.homedir()}/.operate`;
const VenvDir = `${OperateDirectory}/venv`;
const TempDir = `${OperateDirectory}/temp`;
const VersionFile = `${OperateDirectory}/version.txt`;
const LogFile = `${OperateDirectory}/logs.txt`;
const OperateInstallationLog = `${os.homedir()}/operate.log`;
Expand All @@ -22,6 +27,20 @@ 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",
arm64: "https://github.com/tendermint/tendermint/releases/download/v0.34.19/tendermint_0.34.19_darwin_arm64.tar.gz",
},
linux: {
x64: "https://github.com/tendermint/tendermint/releases/download/v0.34.19/tendermint_0.34.19_linux_amd64.tar.gz",
arm64: "https://github.com/tendermint/tendermint/releases/download/v0.34.19/tendermint_0.34.19_linux_arm64.tar.gz",
},
win32: {
x64: "https://github.com/tendermint/tendermint/releases/download/v0.34.19/tendermint_0.34.19_windows_amd64.tar.gz",
arm64: "https://github.com/tendermint/tendermint/releases/download/v0.34.19/tendermint_0.34.19_windows_arm64.tar.gz"
}
}

function getBinPath(command) {
return spawnSync('/usr/bin/which', [command], { env: Env })
Expand Down Expand Up @@ -104,6 +123,46 @@ function installBrew() {
]);
}

function isTendermintInstalledUnix() {
return Boolean(getBinPath('tendermint'));
}

async function downloadFile(url, dest) {
const writer = fs.createWriteStream(dest);
try {
const response = await axios({
url,
method: 'GET',
responseType: 'stream'
});
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
} catch (err) {
fs.unlink(dest, () => { }); // Delete the file if there is an error
console.error('Error downloading the file:', err.message);
}
}

async function installTendermintUnix() {
const cwd = process.cwd()
process.chdir(TempDir)

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

console.log(appendLog(`Downloading ${url}, might take a while...`))
await downloadFile(url, `${TempDir}/tendermint.tar.gz`)

console.log(appendLog(`Installing tendermint binary`))
await runCmdUnix("tar", ["-xvf", "tendermint.tar.gz"])
await runSudoUnix("install", "tendermint /usr/local/bin")
process.chdir(cwd)
}


function isDockerInstalledDarwin() {
return Boolean(getBinPath('docker'));
}
Expand Down Expand Up @@ -212,7 +271,7 @@ function versionBumpRequired() {
}

function removeLogFile() {
if (fs.existsSync()) {
if (fs.existsSync(LogFile)) {
fs.rmSync(LogFile);
}
}
Expand All @@ -236,13 +295,6 @@ async function setupDarwin(ipcChannel) {
installBrew();
}

console.log(appendLog('Checking docker installation'));
if (!isDockerInstalledDarwin()) {
ipcChannel.send('response', 'Installing Pearl Daemon');
console.log(appendLog('Installing docker'));
installDockerDarwin();
}

console.log(appendLog('Checking python installation'));
if (!isPythonInstalledDarwin()) {
ipcChannel.send('response', 'Installing Pearl Daemon');
Expand All @@ -254,6 +306,13 @@ async function setupDarwin(ipcChannel) {
await createDirectory(`${OperateDirectory}`);
await createDirectory(`${OperateDirectory}/temp`);

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

if (!fs.existsSync(VenvDir)) {
ipcChannel.send('response', 'Installing Pearl Daemon');
console.log(appendLog('Creating virtual environment'));
Expand All @@ -279,14 +338,9 @@ async function setupDarwin(ipcChannel) {
await installOperateCli('/opt/homebrew/bin/operate');
}

// TODO: Add Tendermint installation
async function setupUbuntu(ipcChannel) {
removeInstallationLogFile();
console.log(appendLog('Checking docker installation'));
if (!isDockerInstalledUbuntu()) {
ipcChannel.send('response', 'Installing Pearl Daemon');
console.log(appendLog('Installing docker'));
await installDockerUbuntu();
}

console.log(appendLog('Checking python installation'));
if (!isPythonInstalledUbuntu()) {
Expand All @@ -306,9 +360,11 @@ async function setupUbuntu(ipcChannel) {
await createDirectory(`${OperateDirectory}`);
await createDirectory(`${OperateDirectory}/temp`);

if (versionBumpRequired()) {
// removePreviousInstallation();
writeVersion();
console.log(appendLog('Checking tendermint installation'));
if (!isTendermintInstalledUnix()) {
ipcChannel.send('response', 'Installing Pearl Daemon');
console.log(appendLog('Installing tendermint'));
await installTendermintUnix()
}

if (!fs.existsSync(VenvDir)) {
Expand Down
3 changes: 0 additions & 3 deletions electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const {
setupUbuntu,
OperateCmd,
OperateDirectory,
startDocker,
Env,
dirs,
} = require('./install');
Expand Down Expand Up @@ -442,8 +441,6 @@ ipcMain.on('check', async function (event, _argument) {
}
}

startDocker(event.sender);

if (isDev) {
event.sender.send(
'response',
Expand Down
28 changes: 1 addition & 27 deletions operate/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@
from pathlib import Path

from aea.helpers.logging import setup_logger
from autonomy.constants import (
AUTONOMY_IMAGE_NAME,
AUTONOMY_IMAGE_VERSION,
TENDERMINT_IMAGE_NAME,
TENDERMINT_IMAGE_VERSION,
)
from autonomy.deploy.generators.docker_compose.base import get_docker_client
from clea import group, params, run
from compose.project import ProjectError
from docker.errors import APIError
Expand Down Expand Up @@ -159,23 +152,6 @@ def create_app( # pylint: disable=too-many-locals, unused-argument, too-many-st
shutdown_endpoint
)

def pull_latest_images() -> None:
"""Pull latest docker images."""
logger.info("Pulling latest images")
client = get_docker_client()

logger.info(f"Pulling {AUTONOMY_IMAGE_NAME}:{AUTONOMY_IMAGE_VERSION}")
client.images.pull(
repository=AUTONOMY_IMAGE_NAME,
tag=AUTONOMY_IMAGE_VERSION,
)

logger.info(f"Pulling {TENDERMINT_IMAGE_NAME}:{TENDERMINT_IMAGE_VERSION}")
client.images.pull(
repository=TENDERMINT_IMAGE_NAME,
tag=TENDERMINT_IMAGE_VERSION,
)

def schedule_funding_job(
service: str,
from_safe: bool = True,
Expand Down Expand Up @@ -203,9 +179,7 @@ def cancel_funding_job(service: str) -> None:
if not status:
logger.info(f"Funding job cancellation for {service} failed")

app = FastAPI(
on_startup=[pull_latest_images],
)
app = FastAPI()

app.add_middleware(
CORSMiddleware,
Expand Down
10 changes: 5 additions & 5 deletions operate/services/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,8 @@ def deploy_service_onchain_from_safe( # pylint: disable=too-many-statements,too
if service.chain_data.on_chain_state == OnChainState.MINTED:
cost_of_bond = user_params.cost_of_bond
if user_params.use_staking:
token_utility = "0xa45E64d13A30a51b91ae0eb182e88a40e9b18eD8"
olas_token = "0xcE11e14225575945b8E6Dc0D4F2dD4C570f79d9f"
token_utility = "0xa45E64d13A30a51b91ae0eb182e88a40e9b18eD8" # nosec
olas_token = "0xcE11e14225575945b8E6Dc0D4F2dD4C570f79d9f" # nosec
self.logger.info(
f"Approving OLAS as bonding token from {wallet.safe} to {token_utility}"
)
Expand Down Expand Up @@ -451,8 +451,8 @@ def deploy_service_onchain_from_safe( # pylint: disable=too-many-statements,too
if service.chain_data.on_chain_state == OnChainState.ACTIVATED:
cost_of_bond = user_params.cost_of_bond
if user_params.use_staking:
token_utility = "0xa45E64d13A30a51b91ae0eb182e88a40e9b18eD8"
olas_token = "0xcE11e14225575945b8E6Dc0D4F2dD4C570f79d9f"
token_utility = "0xa45E64d13A30a51b91ae0eb182e88a40e9b18eD8" # nosec
olas_token = "0xcE11e14225575945b8E6Dc0D4F2dD4C570f79d9f" # nosec
self.logger.info(
f"Approving OLAS as bonding token from {wallet.safe} to {token_utility}"
)
Expand Down Expand Up @@ -865,7 +865,7 @@ def deploy_service_locally(self, hash: str, force: bool = True) -> Deployment:
deployment.start()
return deployment

def stop_service_locally(self, hash: str, delete: bool) -> Deployment:
def stop_service_locally(self, hash: str, delete: bool = False) -> Deployment:
"""
Stop service locally
Expand Down
Loading

0 comments on commit 48ba975

Please sign in to comment.