2021-04-11 19:53:39 -04:00
|
|
|
import Signale from 'signale';
|
|
|
|
|
import { execSync, spawn } from 'child_process';
|
|
|
|
|
import Datastore from 'nedb';
|
|
|
|
|
import { config } from '../src/lib/config/index.js';
|
|
|
|
|
import express from 'express';
|
|
|
|
|
|
2021-04-11 20:36:31 -04:00
|
|
|
|
2021-02-15 09:30:56 -05:00
|
|
|
(async () => {
|
2021-04-11 19:53:39 -04:00
|
|
|
|
|
|
|
|
const log = Signale.scope('SRVC');
|
2021-02-14 02:15:25 -05:00
|
|
|
const branch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
|
2020-11-11 22:03:46 -05:00
|
|
|
let proc;
|
2021-02-07 15:53:48 -05:00
|
|
|
const logs = new Datastore({
|
|
|
|
|
filename: 'svc.log',
|
|
|
|
|
autoload: true
|
|
|
|
|
});
|
2021-02-10 19:00:15 -05:00
|
|
|
const app = express();
|
2021-02-07 15:53:48 -05:00
|
|
|
|
2021-04-11 20:36:31 -04:00
|
|
|
|
2021-04-01 15:44:39 -04:00
|
|
|
try {
|
2021-04-11 20:36:31 -04:00
|
|
|
logp('Attempting yarn install...')
|
2021-04-01 15:44:39 -04:00
|
|
|
appendLogs('yarn', execSync(`yarn`));
|
|
|
|
|
} catch (e) {
|
2021-04-11 19:53:39 -04:00
|
|
|
logp('failed to yarn install...')
|
2021-04-01 15:44:39 -04:00
|
|
|
}
|
2021-04-01 15:12:35 -04:00
|
|
|
|
2021-02-07 15:53:48 -05:00
|
|
|
logp('==================================');
|
|
|
|
|
logp('Starting Valnet Node as a Service!');
|
2021-02-14 02:15:25 -05:00
|
|
|
logp('Syncing to branch: ' + branch);
|
2021-02-07 15:53:48 -05:00
|
|
|
logp('==================================');
|
2020-11-11 19:38:39 -05:00
|
|
|
|
2020-11-11 22:03:46 -05:00
|
|
|
setInterval(function update() {
|
2021-02-14 02:15:25 -05:00
|
|
|
const remoteHash = execSync('git ls-remote https://github.com/marcus13345/valnet.git').toString()
|
|
|
|
|
.split('\n')
|
|
|
|
|
.filter(test => {
|
|
|
|
|
return test.trim().endsWith(branch);
|
|
|
|
|
})[0]
|
|
|
|
|
.split('\t')[0]
|
|
|
|
|
.trim();
|
2020-11-11 21:02:58 -05:00
|
|
|
const localHash = execSync(`git rev-parse ${branch}`).toString().trim();
|
|
|
|
|
if(remoteHash !== localHash) {
|
2021-02-07 15:53:48 -05:00
|
|
|
logp(`remote hash: ${remoteHash}`);
|
|
|
|
|
logp(`local hash: ${localHash}`);
|
2020-11-11 21:42:23 -05:00
|
|
|
|
2021-02-07 15:53:48 -05:00
|
|
|
logp('killing relay...');
|
2020-11-11 22:03:46 -05:00
|
|
|
try {
|
|
|
|
|
proc.kill();
|
|
|
|
|
} catch (e) {
|
2021-02-07 15:53:48 -05:00
|
|
|
logp('failed to kill active relay...', 'error');
|
|
|
|
|
logp(e, 'error');
|
2020-11-11 22:03:46 -05:00
|
|
|
}
|
2020-11-11 21:02:58 -05:00
|
|
|
}
|
2020-11-11 22:03:46 -05:00
|
|
|
}, 5000);
|
2020-11-11 19:38:39 -05:00
|
|
|
|
2020-11-11 22:03:46 -05:00
|
|
|
(function keepAlive() {
|
2021-04-11 19:53:39 -04:00
|
|
|
proc = spawn('node', ['./relay/index.mjs'], {
|
2021-02-07 15:53:48 -05:00
|
|
|
stdio: 'pipe'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
proc.stdout.on('data', (data) => {
|
|
|
|
|
process.stdout.write(data);
|
|
|
|
|
appendLogs('relay', data.toString(), 'stdout');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
proc.stderr.on('data', (data) => {
|
|
|
|
|
process.stderr.write(data);
|
|
|
|
|
appendLogs('relay', data.toString(), 'stderr');
|
|
|
|
|
});
|
2020-11-11 19:38:39 -05:00
|
|
|
|
2020-11-11 22:03:46 -05:00
|
|
|
proc.on('exit', () => {
|
2021-02-07 15:53:48 -05:00
|
|
|
logp('relay exitted');
|
|
|
|
|
logp('attempting to fetch new version');
|
2020-11-11 22:25:08 -05:00
|
|
|
|
2021-02-07 15:53:48 -05:00
|
|
|
appendLogs('fetch', execSync(`git fetch`));
|
2021-02-15 09:38:19 -05:00
|
|
|
appendLogs('update', execSync(`git pull`));
|
2021-02-07 15:53:48 -05:00
|
|
|
appendLogs('yarn', execSync(`yarn`));
|
2020-11-11 22:25:08 -05:00
|
|
|
|
2021-02-07 15:53:48 -05:00
|
|
|
logp('restarting...')
|
2020-11-11 22:03:46 -05:00
|
|
|
setTimeout(() => {
|
|
|
|
|
keepAlive();
|
|
|
|
|
}, 1000);
|
|
|
|
|
})
|
|
|
|
|
})();
|
2020-11-11 19:38:39 -05:00
|
|
|
|
2021-02-07 15:53:48 -05:00
|
|
|
function logp(message, type = 'info') {
|
|
|
|
|
log[type](message);
|
2021-02-15 09:30:56 -05:00
|
|
|
appendLogs('service', message + '\n')
|
2021-02-07 15:53:48 -05:00
|
|
|
}
|
2020-11-11 19:38:39 -05:00
|
|
|
|
2021-02-07 15:53:48 -05:00
|
|
|
function appendLogs(source, data, type = 'output') {
|
|
|
|
|
logs.insert({
|
|
|
|
|
message: data.toString(),
|
|
|
|
|
type: type,
|
|
|
|
|
src: source,
|
|
|
|
|
timestamp: new Date().getTime()
|
|
|
|
|
})
|
|
|
|
|
}
|
2020-11-11 19:38:39 -05:00
|
|
|
|
2021-02-10 19:00:15 -05:00
|
|
|
app.get('/', (req, res) => {
|
2021-02-21 22:58:05 -05:00
|
|
|
res.end('<a href="/logs">Logs</a>');
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
app.get('/logs', (req, res) => {
|
|
|
|
|
res.redirect(`/logs/${Date.now() - (1000 * 60 * 60 * 24)}`)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
app.get('/logs/:time', (req, res) => {
|
|
|
|
|
|
2021-02-10 19:00:15 -05:00
|
|
|
logs.find({
|
2021-02-21 22:58:05 -05:00
|
|
|
timestamp: { $gt: parseInt(req.params.time) }
|
2021-02-15 09:30:56 -05:00
|
|
|
}, {}).sort({
|
2021-04-22 23:42:49 -04:00
|
|
|
timestamp: -1
|
|
|
|
|
}).limit(100).exec((err, docs) => {
|
2021-02-10 19:00:15 -05:00
|
|
|
|
2021-04-22 23:42:49 -04:00
|
|
|
res.end(Template.logs(docs.reverse().map(v => v.message)));
|
2021-02-21 22:58:05 -05:00
|
|
|
|
2021-02-10 19:00:15 -05:00
|
|
|
if(err) {
|
|
|
|
|
res.end(err.toString());
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-02-15 09:30:56 -05:00
|
|
|
// ${new Date(logItem.timestamp).toLocaleString().padStart(40)}:
|
2021-04-22 23:42:49 -04:00
|
|
|
res.end();
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const Template = {
|
|
|
|
|
logs(messages) {
|
|
|
|
|
return `<html>
|
2021-02-15 09:30:56 -05:00
|
|
|
<head>
|
|
|
|
|
<meta charset="UTF-16">
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<style>
|
|
|
|
|
table {
|
|
|
|
|
border-spacing: 0px;
|
|
|
|
|
font-size: 13px;
|
|
|
|
|
}
|
|
|
|
|
tr {
|
|
|
|
|
vertical-align: top;
|
|
|
|
|
}
|
2021-02-21 23:15:54 -05:00
|
|
|
html {
|
|
|
|
|
background: #0E1419;
|
|
|
|
|
color: #F8F8F2;
|
|
|
|
|
}
|
2021-02-15 09:30:56 -05:00
|
|
|
</style>
|
|
|
|
|
<pre>
|
2021-04-22 23:42:49 -04:00
|
|
|
${messages.join('').replace(/\u001B\[.*?[A-Za-z]/g, '')}
|
2021-02-15 09:30:56 -05:00
|
|
|
</pre>
|
2021-02-21 23:15:54 -05:00
|
|
|
<br><br><br><br><br><br>
|
|
|
|
|
<script>
|
2021-04-01 15:10:22 -04:00
|
|
|
// requestAnimationFrame(_ => {
|
|
|
|
|
// requestAnimationFrame(_ => {
|
|
|
|
|
// window.scrollTo(0,document.body.scrollHeight);
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
// setTimeout(_ => {
|
|
|
|
|
// location.reload();
|
|
|
|
|
// }, 2000);
|
2021-02-21 23:15:54 -05:00
|
|
|
</script>
|
2021-02-15 09:30:56 -05:00
|
|
|
</body>
|
2021-04-22 23:42:49 -04:00
|
|
|
</html>`;
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-11-11 19:38:39 -05:00
|
|
|
|
2021-02-14 02:21:35 -05:00
|
|
|
app.listen(config.ports.service);
|
2020-11-11 19:38:39 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
})();
|