Skip to content

Commit

Permalink
Merge pull request #8027 from Agoric/gibson-2023-07-agoric-cli
Browse files Browse the repository at this point in the history
chore(agoric-cli): Clean up `agoric start` code
  • Loading branch information
gibson042 authored and mhofman committed Aug 7, 2023
2 parents 09a4046 + bd28a59 commit ff446f8
Showing 1 changed file with 43 additions and 51 deletions.
94 changes: 43 additions & 51 deletions packages/agoric-cli/src/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const DELEGATE0_COINS = `50000000${STAKING_DENOM}`;
const SOLO_COINS = `13000000${STAKING_DENOM},500000000${CENTRAL_DENOM}`;
const CHAIN_ID = 'agoriclocal';

const SERVERS_ROOT_DIR = '_agstate/agoric-servers';
const FAKE_CHAIN_DELAY =
process.env.FAKE_CHAIN_DELAY === undefined
? 0
Expand Down Expand Up @@ -159,6 +160,12 @@ export default async function startMain(progname, rawArgs, powers, opts) {
}
};

const rmVerbose = async filePath => {
log(chalk.green(`removing ${filePath}`));
// rm is available on all the unix-likes, so use it for speed.
await pspawn('rm', ['-rf', filePath]);
};

let agSolo;
let agSoloBuild;
if (opts.dockerTag) {
Expand All @@ -171,12 +178,10 @@ export default async function startMain(progname, rawArgs, powers, opts) {
const fakeDelay =
popts.delay === undefined ? FAKE_CHAIN_DELAY : Number(popts.delay);

const agServer = `_agstate/agoric-servers/${profileName}`;
const serverDir = `${SERVERS_ROOT_DIR}/${profileName}`;

if (popts.reset) {
log(chalk.green(`removing ${agServer}`));
// rm is available on all the unix-likes, so use it for speed.
await pspawn('rm', ['-rf', agServer]);
rmVerbose(serverDir);
}

if (!opts.dockerTag) {
Expand All @@ -197,14 +202,14 @@ export default async function startMain(progname, rawArgs, powers, opts) {
}

const fakeGCI = 'sim-chain';
const serverExists = await exists(agServer);
const serverExists = await exists(serverDir);
if (!serverExists) {
log(chalk.yellow(`initializing ${profileName}`));
await pspawn(
agSolo,
['init', profileName, '--egresses=fake', `--webport=${HOST_PORT}`],
{
cwd: '_agstate/agoric-servers',
cwd: SERVERS_ROOT_DIR,
},
);
}
Expand All @@ -215,7 +220,7 @@ export default async function startMain(progname, rawArgs, powers, opts) {
agSolo,
['set-fake-chain', `--delay=${fakeDelay}`, fakeGCI],
{
cwd: agServer,
cwd: serverDir,
},
);
}
Expand All @@ -226,7 +231,7 @@ export default async function startMain(progname, rawArgs, powers, opts) {
}

const ps = pspawn(agSolo, [...debugOpts, 'start'], {
cwd: agServer,
cwd: serverDir,
env: nodeDebugEnv,
});
process.on('SIGINT', () => ps.childProcess.kill('SIGINT'));
Expand Down Expand Up @@ -263,17 +268,15 @@ export default async function startMain(progname, rawArgs, powers, opts) {
}
}

const agServer = `_agstate/agoric-servers/${profileName}-${portNum}`;
const serverDir = `${SERVERS_ROOT_DIR}/${profileName}-${portNum}`;
if (popts.reset) {
log(chalk.green(`removing ${agServer}`));
// rm is available on all the unix-likes, so use it for speed.
await pspawn('rm', ['-rf', agServer]);
rmVerbose(serverDir);
}

let chainSpawn;
if (!popts.dockerTag) {
chainSpawn = (args, spawnOpts = undefined) => {
return pspawn(cosmosChain, [...args, `--home=${agServer}`], spawnOpts);
return pspawn(cosmosChain, [...args, `--home=${serverDir}`], spawnOpts);
};
} else {
chainSpawn = (args, spawnOpts = undefined, dockerArgs = []) =>
Expand All @@ -287,13 +290,13 @@ export default async function startMain(progname, rawArgs, powers, opts) {
...terminalOnlyFlags(`-it`),
SDK_IMAGE,
...args,
`--home=/usr/src/dapp/${agServer}`,
`--home=/usr/src/dapp/${serverDir}`,
],
spawnOpts,
);
}

const serverExists = await exists(agServer);
const serverExists = await exists(serverDir);
if (!serverExists) {
const exitStatus = await chainSpawn([
'init',
Expand Down Expand Up @@ -331,7 +334,7 @@ export default async function startMain(progname, rawArgs, powers, opts) {
/* eslint-enable no-await-in-loop */
}

const genesisFile = `${agServer}/config/genesis.json`;
const genesisFile = `${serverDir}/config/genesis.json`;
const stampExists = await exists(`${genesisFile}.stamp`);
if (!stampExists) {
let exitStatus;
Expand Down Expand Up @@ -361,7 +364,7 @@ export default async function startMain(progname, rawArgs, powers, opts) {
`--keyring-dir=${keysHome}`,
'--keyring-backend=test',
`--chain-id=${CHAIN_ID}`,
`${DELEGATE0_COINS}`,
DELEGATE0_COINS,
]);
if (exitStatus) {
return exitStatus;
Expand All @@ -382,8 +385,8 @@ export default async function startMain(progname, rawArgs, powers, opts) {

// Complete the genesis file and launch the chain.
log('read ag-chain-cosmos config');
const configFile = `${agServer}/config/config.toml`;
const appFile = `${agServer}/config/app.toml`;
const configFile = `${serverDir}/config/config.toml`;
const appFile = `${serverDir}/config/app.toml`;
const [genesisJson, configToml, appToml] = await Promise.all([
fs.readFile(genesisFile, 'utf-8'),
fs.readFile(configFile, 'utf-8'),
Expand Down Expand Up @@ -446,7 +449,7 @@ export default async function startMain(progname, rawArgs, powers, opts) {
return 1;
}

const agServer = `_agstate/agoric-servers/${profileName}-${portNum}`;
const serverDir = `${SERVERS_ROOT_DIR}/${profileName}-${portNum}`;

const { cosmosClientBuild } = getSDKBinaries(sdkPrefixes);
if (popts.pull || popts.rebuild) {
Expand Down Expand Up @@ -474,9 +477,7 @@ export default async function startMain(progname, rawArgs, powers, opts) {
}

if (popts.reset) {
log(chalk.green(`removing ${agServer}`));
// rm is available on all the unix-likes, so use it for speed.
await pspawn('rm', ['-rf', agServer]);
rmVerbose(serverDir);
}

let soloSpawn;
Expand All @@ -491,7 +492,7 @@ export default async function startMain(progname, rawArgs, powers, opts) {
'run',
`--volume=${process.cwd()}:/usr/src/dapp`,
`--volume=${process.env.HOME}/.agoric:/root/.agoric`,
`-eAG_SOLO_BASEDIR=/usr/src/dapp/${agServer}`,
`-eAG_SOLO_BASEDIR=/usr/src/dapp/${serverDir}`,
`--rm`,
...terminalOnlyFlags(`-it`),
`--entrypoint=ag-solo`,
Expand All @@ -503,15 +504,15 @@ export default async function startMain(progname, rawArgs, powers, opts) {
);
}

const serverExists = await exists(agServer);
const serverExists = await exists(serverDir);
// Initialise the solo directory and key.
if (!serverExists) {
const initArgs = [`--webport=${portNum}`];
if (opts.dockerTag) {
initArgs.push(`--webhost=0.0.0.0`);
}
const exitStatus = await soloSpawn(
['init', agServer, ...initArgs],
['init', serverDir, ...initArgs],
undefined,
[`--workdir=/usr/src/dapp`],
);
Expand All @@ -522,15 +523,15 @@ export default async function startMain(progname, rawArgs, powers, opts) {

// Create the full economy chain config.
const agServerResolve = spec =>
require.resolve(spec, { paths: [agServer] });
require.resolve(spec, { paths: [serverDir] });
const coreConfigPath = agServerResolve(
'@agoric/vats/decentral-core-config.json',
);
const economyTemplPath = agServerResolve(
'@agoric/cosmic-swingset/economy-template.json',
);
const [rawSoloAddr, coreConfigJson, economyTemplJson] = await Promise.all([
fs.readFile(`${agServer}/ag-cosmos-helper-address`, 'utf-8'),
fs.readFile(`${serverDir}/ag-cosmos-helper-address`, 'utf-8'),
fs.readFile(coreConfigPath, 'utf-8'),
fs.readFile(economyTemplPath, 'utf-8'),
]);
Expand All @@ -541,15 +542,15 @@ export default async function startMain(progname, rawArgs, powers, opts) {
const economyConfig = JSON.parse(coreConfigJson);
economyConfig.coreProposals = economyProposals;
await fs.writeFile(
`${agServer}/decentral-economy-config.json`,
`${serverDir}/decentral-economy-config.json`,
JSON.stringify(economyConfig, null, 2),
);

if (!opts.restart) {
return 0;
}

const gciFile = `_agstate/agoric-servers/local-chain-${CHAIN_PORT}/config/genesis.json.sha256`;
const gciFile = `${SERVERS_ROOT_DIR}/local-chain-${CHAIN_PORT}/config/genesis.json.sha256`;
process.stdout.write(`Waiting for local-chain-${CHAIN_PORT} to start...`);
let hasGci = false;
for await (const _ of untilTrue(() => hasGci)) {
Expand All @@ -576,7 +577,7 @@ export default async function startMain(progname, rawArgs, powers, opts) {

const spawnOpts = {};
if (!popts.dockerTag) {
spawnOpts.cwd = agServer;
spawnOpts.cwd = serverDir;
}

const rpcAddrs = [`localhost:${CHAIN_PORT}`];
Expand Down Expand Up @@ -699,20 +700,18 @@ export default async function startMain(progname, rawArgs, powers, opts) {

const port = startArgs[0] || PORT;
const netconfig = startArgs[1] || DEFAULT_NETCONFIG;
const agServer = `_agstate/agoric-servers/${profileName}-${port}`;
const serverDir = `${SERVERS_ROOT_DIR}/${profileName}-${port}`;

if (popts.reset) {
log(chalk.green(`removing ${agServer}`));
// rm is available on all the unix-likes, so use it for speed.
await pspawn('rm', ['-rf', agServer]);
rmVerbose(serverDir);
}

const setupRun = (...bonusArgs) =>
pspawn('docker', [
'run',
`-p127.0.0.1:${HOST_PORT}:${port}`,
`--volume=${process.cwd()}:/usr/src/dapp`,
`-eAG_SOLO_BASEDIR=/usr/src/dapp/${agServer}`,
`-eAG_SOLO_BASEDIR=/usr/src/dapp/${serverDir}`,
`--rm`,
...terminalOnlyFlags(`-it`),
SOLO_IMAGE,
Expand All @@ -727,44 +726,37 @@ export default async function startMain(progname, rawArgs, powers, opts) {
async function startTestnetSdk(profileName, startArgs, popts) {
const port = startArgs[0] || PORT;
const netconfig = startArgs[1] || DEFAULT_NETCONFIG;
const agServer = `_agstate/agoric-servers/${profileName}-${port}`;
const serverDir = `${SERVERS_ROOT_DIR}/${profileName}-${port}`;

if (popts.reset) {
log(chalk.green(`removing ${agServer}`));
// rm is available on all the unix-likes, so use it for speed.
await pspawn('rm', ['-rf', agServer]);
rmVerbose(serverDir);
}

const setupRun = (...bonusArgs) =>
pspawn(agSolo, [`--webport=${port}`, ...bonusArgs], {
env: { ...pspawnEnv, AG_SOLO_BASEDIR: agServer },
env: { ...pspawnEnv, AG_SOLO_BASEDIR: serverDir },
});

return setupRun('setup', `--netconfig=${netconfig}`);
}

const profiles = {
__proto__: null,
dev: startFakeChain,
'local-chain': startLocalChain,
'local-solo': startLocalSolo,
testnet: opts.dockerTag ? startTestnetDocker : startTestnetSdk,
};

const popts = opts;

const args = rawArgs.slice(1);
const profileName = args[0] || 'dev';
const [_command = 'start', profileName = 'dev', ...args] = rawArgs;
const startFn = profiles[profileName];
if (!startFn) {
const profileNames = Object.keys(profiles).join(', ');
log.error(
`unrecognized profile name ${profileName}; use one of: ${Object.keys(
profiles,
)
.sort()
.join(', ')}`,
`unrecognized profile name ${profileName}; use one of: ${profileNames}`,
);
return 1;
}

return startFn(profileName, args[0] ? args.slice(1) : args, popts);
return startFn(profileName, args, opts);
}

0 comments on commit ff446f8

Please sign in to comment.