Skip to content

Commit 9ecfb3d

Browse files
committed
feat: wip auto install bundle on instagoric
1 parent 2fb1df2 commit 9ecfb3d

4 files changed

Lines changed: 338 additions & 17 deletions

File tree

bases/shared/instagoric-server/bundle1.json

Lines changed: 5 additions & 0 deletions
Large diffs are not rendered by default.

bases/shared/instagoric-server/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"build": "exit 0",
88
"test": "exit 0",
9-
"start": "NAMESPACE=${NAMESPACE-instagoric} node server.js",
9+
"start": "NAMESPACE=${NAMESPACE-instagoric} nodemon server.js",
1010
"test:c8": "c8 $C8_OPTIONS ava --config=ava-nesm.config.js",
1111
"test:xs": "exit 0",
1212
"lint-fix": "yarn lint:eslint --fix",
@@ -28,6 +28,7 @@
2828
"eslint-plugin-jsdoc": "^39.2.9",
2929
"eslint-plugin-jsx-a11y": "^6.5.1",
3030
"eslint-plugin-prettier": "^4.0.0",
31+
"nodemon": "^3.1.4",
3132
"npm-run-all": "^4.1.5",
3233
"typescript": "~4.6.3"
3334
},
@@ -36,6 +37,7 @@
3637
"@endo/init": "^0.5.60",
3738
"@endo/promise-kit": "^0.2.41",
3839
"express": "^4.17.1",
40+
"multer": "^1.4.5-lts.1",
3941
"prettier": "^2.6.2",
4042
"tmp": "^0.2.1",
4143
"zx": "^6.1.0"

bases/shared/instagoric-server/server.js

Lines changed: 91 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import express from 'express';
77
import https from 'https';
88
import tmp from 'tmp';
99
import { $, fetch, fs, sleep } from 'zx';
10+
import multer from 'multer';
1011

1112
import { makeSubscriptionKit } from '@agoric/notifier';
12-
13+
const upload = multer({ dest: 'uploads/' })
1314
const { details: X } = globalThis.assert;
1415

1516
const CLIENT_AMOUNT =
@@ -20,14 +21,17 @@ const DELEGATE_AMOUNT =
2021
const DOCKERTAG = process.env.DOCKERTAG; // Optional.
2122
const DOCKERIMAGE = process.env.DOCKERIMAGE; // Optional.
2223
const FAUCET_KEYNAME =
23-
process.env.FAUCET_KEYNAME || process.env.WHALE_KEYNAME || 'self';
24+
process.env.FAUCET_KEYNAME || process.env.WHALE_KEYNAME || 'bootstrap';
2425
const NETNAME = process.env.NETNAME || 'devnet';
2526
const NETDOMAIN = process.env.NETDOMAIN || '.agoric.net';
2627
const AG0_MODE = (process.env.AG0_MODE || 'false') === 'true';
2728
const agBinary = AG0_MODE ? 'ag0' : 'agd';
2829
const podname = process.env.POD_NAME || 'validator-primary';
29-
const INCLUDE_SEED = process.env.SEED_ENABLE || 'yes';
30-
const NODE_ID = process.env.NODE_ID || 'fb86a0993c694c981a28fa1ebd1fd692f345348b';
30+
const INCLUDE_SEED = process.env.SEED_ENABLE || 'yes';
31+
const NODE_ID =
32+
process.env.NODE_ID || 'fb86a0993c694c981a28fa1ebd1fd692f345348b';
33+
const agoricHome =
34+
'~/Projects/agoric-sdk/packages/cosmic-swingset/t1/bootstrap';
3135

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

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

5255
const chainId = process.env.CHAIN_ID;
@@ -62,7 +65,7 @@ const namespace =
6265
});
6366

6467
const revision =
65-
process.env.AG0_MODE === 'true'
68+
1 || process.env.AG0_MODE === 'true'
6669
? 'ag0'
6770
: fs
6871
.readFileSync(
@@ -150,11 +153,12 @@ const getNetworkConfig = async () => {
150153
`${podname}.${namespace}.svc.cluster.local`,
151154
);
152155
ap.peers[0] = ap.peers[0].replace(
153-
'fb86a0993c694c981a28fa1ebd1fd692f345348b', `${NODE_ID}`,
156+
'fb86a0993c694c981a28fa1ebd1fd692f345348b',
157+
`${NODE_ID}`,
154158
);
155159
ap.rpcAddrs = [`https://${NETNAME}.rpc${NETDOMAIN}:443`];
156160
ap.apiAddrs = [`https://${NETNAME}.api${NETDOMAIN}:443`];
157-
if (INCLUDE_SEED==='yes') {
161+
if (INCLUDE_SEED === 'yes') {
158162
ap.seeds[0] = ap.seeds[0].replace(
159163
'seed.instagoric.svc.cluster.local',
160164
svc.get('seed-ext') || `seed.${namespace}.svc.cluster.local`,
@@ -251,7 +255,9 @@ Chain: ${chainId}${
251255
: ''
252256
}
253257
Revision: ${revision}
254-
Docker Image: ${DOCKERIMAGE || dockerImage.split(':')[0]}:${DOCKERTAG || dockerImage.split(':')[1]}
258+
Docker Image: ${DOCKERIMAGE || dockerImage.split(':')[0]}:${
259+
DOCKERTAG || dockerImage.split(':')[1]
260+
}
255261
Revision Link: <a href="https://github.com/Agoric/agoric-sdk/tree/${revision}">https://github.com/Agoric/agoric-sdk/tree/${revision}</a>
256262
Network Config: <a href="https://${netname}${domain}/network-config">https://${netname}${domain}/network-config</a>
257263
Docker Compose: <a href="https://${netname}${domain}/docker-compose.yml">https://${netname}${domain}/docker-compose.yml</a>
@@ -287,6 +293,7 @@ publicapp.get('/metrics-config', async (req, res) => {
287293
res.send(result);
288294
});
289295

296+
290297
const dockerComposeYaml = (dockerimage, dockertag, netname, netdomain) => `\
291298
version: "2.2"
292299
services:
@@ -507,6 +514,80 @@ faucetapp.post('/go', (req, res) => {
507514
}
508515
});
509516

517+
publicapp.get('/install-bundle', async (req, res) => {
518+
519+
res.send(`
520+
<!DOCTYPE html>
521+
<html lang="en">
522+
<head>
523+
<meta charset="UTF-8">
524+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
525+
<title>File Upload</title>
526+
</head>
527+
<body>
528+
529+
<h1>Upload a File</h1>
530+
531+
<form id="uploadForm" action="/install-bundle" method="POST" enctype="multipart/form-data">
532+
<input type="file" name="file" id="fileInput" required>
533+
<br><br>
534+
<button type="submit">Upload</button>
535+
</form>
536+
537+
<script>
538+
document.getElementById('uploadForm').addEventListener('submit', function(event) {
539+
event.preventDefault(); // Prevent the default form submission
540+
541+
const formData = new FormData();
542+
const fileInput = document.getElementById('fileInput');
543+
544+
if (fileInput.files.length === 0) {
545+
alert("Please select a file.");
546+
return;
547+
}
548+
549+
formData.append('file', fileInput.files[0]);
550+
551+
fetch('/install-bundle', {
552+
method: 'POST',
553+
body: formData
554+
})
555+
.then(response => response.json())
556+
.then(data => {
557+
alert('File uploaded successfully!');
558+
console.log(data);
559+
})
560+
.catch(error => {
561+
alert('Failed to upload file.');
562+
console.error('Error:', error);
563+
});
564+
});
565+
</script>
566+
567+
</body>
568+
</html>
569+
570+
`);
571+
572+
});
573+
574+
publicapp.post('/install-bundle', upload.single('file') ,async (req, res) => {
575+
// Getting file from request and storing as temp file
576+
const bundle = (req.file?.filename);
577+
console.log(bundle);
578+
const result = await $`\
579+
${agBinary} tx swingset install-bundle --compress "@uploads/${bundle}" \
580+
--from ${FAUCET_KEYNAME} --keyring-backend=test --keyring-dir=${agoricHome} --gas=auto \
581+
--chain-id=${chainId} -b block --yes
582+
`;
583+
console.log('\n\n',result);
584+
if (result.exitCode !== 0) {
585+
res.status(500).send(result.stderr);
586+
return;
587+
}
588+
res.status(200).send('success');
589+
});
590+
510591
faucetapp.listen(faucetport, () => {
511592
console.log(`faucetapp listening on port ${faucetport}`);
512593
});

0 commit comments

Comments
 (0)