Skip to content

Commit

Permalink
feat: wip auto install bundle on instagoric
Browse files Browse the repository at this point in the history
  • Loading branch information
frazarshad committed Aug 30, 2024
1 parent 2fb1df2 commit 9ecfb3d
Show file tree
Hide file tree
Showing 4 changed files with 338 additions and 17 deletions.
5 changes: 5 additions & 0 deletions bases/shared/instagoric-server/bundle1.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion bases/shared/instagoric-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"build": "exit 0",
"test": "exit 0",
"start": "NAMESPACE=${NAMESPACE-instagoric} node server.js",
"start": "NAMESPACE=${NAMESPACE-instagoric} nodemon server.js",
"test:c8": "c8 $C8_OPTIONS ava --config=ava-nesm.config.js",
"test:xs": "exit 0",
"lint-fix": "yarn lint:eslint --fix",
Expand All @@ -28,6 +28,7 @@
"eslint-plugin-jsdoc": "^39.2.9",
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-prettier": "^4.0.0",
"nodemon": "^3.1.4",
"npm-run-all": "^4.1.5",
"typescript": "~4.6.3"
},
Expand All @@ -36,6 +37,7 @@
"@endo/init": "^0.5.60",
"@endo/promise-kit": "^0.2.41",
"express": "^4.17.1",
"multer": "^1.4.5-lts.1",
"prettier": "^2.6.2",
"tmp": "^0.2.1",
"zx": "^6.1.0"
Expand Down
101 changes: 91 additions & 10 deletions bases/shared/instagoric-server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import express from 'express';
import https from 'https';
import tmp from 'tmp';
import { $, fetch, fs, sleep } from 'zx';
import multer from 'multer';

import { makeSubscriptionKit } from '@agoric/notifier';

const upload = multer({ dest: 'uploads/' })
const { details: X } = globalThis.assert;

const CLIENT_AMOUNT =
Expand All @@ -20,14 +21,17 @@ const DELEGATE_AMOUNT =
const DOCKERTAG = process.env.DOCKERTAG; // Optional.
const DOCKERIMAGE = process.env.DOCKERIMAGE; // Optional.
const FAUCET_KEYNAME =
process.env.FAUCET_KEYNAME || process.env.WHALE_KEYNAME || 'self';
process.env.FAUCET_KEYNAME || process.env.WHALE_KEYNAME || 'bootstrap';
const NETNAME = process.env.NETNAME || 'devnet';
const NETDOMAIN = process.env.NETDOMAIN || '.agoric.net';
const AG0_MODE = (process.env.AG0_MODE || 'false') === 'true';
const agBinary = AG0_MODE ? 'ag0' : 'agd';
const podname = process.env.POD_NAME || 'validator-primary';
const INCLUDE_SEED = process.env.SEED_ENABLE || 'yes';
const NODE_ID = process.env.NODE_ID || 'fb86a0993c694c981a28fa1ebd1fd692f345348b';
const INCLUDE_SEED = process.env.SEED_ENABLE || 'yes';
const NODE_ID =
process.env.NODE_ID || 'fb86a0993c694c981a28fa1ebd1fd692f345348b';
const agoricHome =
'~/Projects/agoric-sdk/packages/cosmic-swingset/t1/bootstrap';

const FAKE = process.env.FAKE || process.argv[2] === '--fake';
if (FAKE) {
Expand All @@ -42,11 +46,10 @@ if (FAKE) {
});
// Create the temporary key.
console.log(`Creating temporary key`, { tmpDir, FAUCET_KEYNAME });
await $`${agBinary} --home=${tmpDir} keys --keyring-backend=test add ${FAUCET_KEYNAME}`;
// await $`${agBinary} --home=${agoricHome} keys --keyring-backend=test add ${FAUCET_KEYNAME}`;
process.env.AGORIC_HOME = tmpDir;
}

const agoricHome = process.env.AGORIC_HOME;
assert(agoricHome, X`AGORIC_HOME not set`);

const chainId = process.env.CHAIN_ID;
Expand All @@ -62,7 +65,7 @@ const namespace =
});

const revision =
process.env.AG0_MODE === 'true'
1 || process.env.AG0_MODE === 'true'
? 'ag0'
: fs
.readFileSync(
Expand Down Expand Up @@ -150,11 +153,12 @@ const getNetworkConfig = async () => {
`${podname}.${namespace}.svc.cluster.local`,
);
ap.peers[0] = ap.peers[0].replace(
'fb86a0993c694c981a28fa1ebd1fd692f345348b', `${NODE_ID}`,
'fb86a0993c694c981a28fa1ebd1fd692f345348b',
`${NODE_ID}`,
);
ap.rpcAddrs = [`https://${NETNAME}.rpc${NETDOMAIN}:443`];
ap.apiAddrs = [`https://${NETNAME}.api${NETDOMAIN}:443`];
if (INCLUDE_SEED==='yes') {
if (INCLUDE_SEED === 'yes') {
ap.seeds[0] = ap.seeds[0].replace(
'seed.instagoric.svc.cluster.local',
svc.get('seed-ext') || `seed.${namespace}.svc.cluster.local`,
Expand Down Expand Up @@ -251,7 +255,9 @@ Chain: ${chainId}${
: ''
}
Revision: ${revision}
Docker Image: ${DOCKERIMAGE || dockerImage.split(':')[0]}:${DOCKERTAG || dockerImage.split(':')[1]}
Docker Image: ${DOCKERIMAGE || dockerImage.split(':')[0]}:${
DOCKERTAG || dockerImage.split(':')[1]
}
Revision Link: <a href="https://github.com/Agoric/agoric-sdk/tree/${revision}">https://github.com/Agoric/agoric-sdk/tree/${revision}</a>
Network Config: <a href="https://${netname}${domain}/network-config">https://${netname}${domain}/network-config</a>
Docker Compose: <a href="https://${netname}${domain}/docker-compose.yml">https://${netname}${domain}/docker-compose.yml</a>
Expand Down Expand Up @@ -287,6 +293,7 @@ publicapp.get('/metrics-config', async (req, res) => {
res.send(result);
});


const dockerComposeYaml = (dockerimage, dockertag, netname, netdomain) => `\
version: "2.2"
services:
Expand Down Expand Up @@ -507,6 +514,80 @@ faucetapp.post('/go', (req, res) => {
}
});

publicapp.get('/install-bundle', async (req, res) => {

res.send(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Upload</title>
</head>
<body>
<h1>Upload a File</h1>
<form id="uploadForm" action="/install-bundle" method="POST" enctype="multipart/form-data">
<input type="file" name="file" id="fileInput" required>
<br><br>
<button type="submit">Upload</button>
</form>
<script>
document.getElementById('uploadForm').addEventListener('submit', function(event) {
event.preventDefault(); // Prevent the default form submission
const formData = new FormData();
const fileInput = document.getElementById('fileInput');
if (fileInput.files.length === 0) {
alert("Please select a file.");
return;
}
formData.append('file', fileInput.files[0]);
fetch('/install-bundle', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
alert('File uploaded successfully!');
console.log(data);
})
.catch(error => {
alert('Failed to upload file.');
console.error('Error:', error);
});
});
</script>
</body>
</html>
`);

});

publicapp.post('/install-bundle', upload.single('file') ,async (req, res) => {
// Getting file from request and storing as temp file
const bundle = (req.file?.filename);
console.log(bundle);
const result = await $`\
${agBinary} tx swingset install-bundle --compress "@uploads/${bundle}" \
--from ${FAUCET_KEYNAME} --keyring-backend=test --keyring-dir=${agoricHome} --gas=auto \
--chain-id=${chainId} -b block --yes
`;
console.log('\n\n',result);
if (result.exitCode !== 0) {
res.status(500).send(result.stderr);
return;
}
res.status(200).send('success');
});

faucetapp.listen(faucetport, () => {
console.log(`faucetapp listening on port ${faucetport}`);
});
Expand Down
Loading

0 comments on commit 9ecfb3d

Please sign in to comment.