2019-05-15 11:37:38 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
if (!process.argv[2] || ['help', '-h', '--help'].includes(process.argv[2])) {
|
|
|
|
console.log(`
|
|
|
|
Sync changes in the javascript code to a running local mode supervisor on a device on the local network
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
./sync-debug.js <device IP>
|
|
|
|
|
|
|
|
Note that the device should be running a debug image.
|
|
|
|
`);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ip = process.argv[2];
|
|
|
|
|
|
|
|
const { Livepush } = require('livepush');
|
|
|
|
const { fs } = require('mz');
|
|
|
|
const dockerode = require('dockerode');
|
|
|
|
const chokidar = require('chokidar');
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
2019-05-30 22:30:49 +00:00
|
|
|
require('ts-node/register');
|
|
|
|
const { ContainerLogs } = require('./src/logging/container');
|
|
|
|
|
|
|
|
const setupLogs = (containerId, docker) => {
|
|
|
|
console.log('Setting up logs');
|
|
|
|
const logs = new ContainerLogs(containerId, docker);
|
|
|
|
logs.on('log', ({ message }) => {
|
|
|
|
if (message.trim().length !== 0) {
|
|
|
|
console.log(message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
logs.attach(Date.now());
|
|
|
|
};
|
|
|
|
|
2019-05-15 11:37:38 +00:00
|
|
|
const docker = new dockerode({
|
|
|
|
host: ip,
|
|
|
|
port: 2375,
|
|
|
|
});
|
|
|
|
|
|
|
|
function extractMessage(msgBuf) {
|
|
|
|
// Non-tty message format from:
|
|
|
|
// https://docs.docker.com/engine/api/v1.30/#operation/ContainerAttach
|
|
|
|
if (
|
|
|
|
_.includes([0, 1, 2], msgBuf[0]) &&
|
|
|
|
_.every(msgBuf.slice(1, 7), c => c === 0)
|
|
|
|
) {
|
|
|
|
// Take the header from this message, and parse it as normal
|
|
|
|
msgBuf = msgBuf.slice(8);
|
|
|
|
}
|
|
|
|
const logLine = msgBuf.toString();
|
|
|
|
const space = logLine.indexOf(' ');
|
|
|
|
if (space > 0) {
|
|
|
|
let timestamp = new Date(logLine.substr(0, space)).getTime();
|
|
|
|
if (_.isNaN(timestamp)) {
|
|
|
|
timestamp = Date.now();
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
timestamp,
|
|
|
|
message: logLine.substr(space + 1),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-29 11:44:48 +00:00
|
|
|
let changedFiles = [];
|
|
|
|
let deletedFiles = [];
|
|
|
|
|
2019-05-30 22:30:49 +00:00
|
|
|
const performLivepush = _.debounce(async (livepush, containerId, docker) => {
|
2019-05-29 11:44:48 +00:00
|
|
|
await livepush.performLivepush(changedFiles, deletedFiles);
|
|
|
|
changedFiles = [];
|
|
|
|
deletedFiles = [];
|
2019-05-30 22:30:49 +00:00
|
|
|
setupLogs(containerId, docker);
|
2019-05-29 11:44:48 +00:00
|
|
|
}, 1000);
|
|
|
|
|
2019-05-15 11:37:38 +00:00
|
|
|
(async () => {
|
2019-05-29 11:44:48 +00:00
|
|
|
console.log('Starting up...');
|
2019-05-15 11:37:38 +00:00
|
|
|
// Get the supervisor container id
|
|
|
|
const container = await docker.getContainer('resin_supervisor').inspect();
|
2019-05-29 11:44:48 +00:00
|
|
|
console.log('Supervisor container id: ', container.Id);
|
2019-05-15 11:37:38 +00:00
|
|
|
const containerId = container.Id;
|
|
|
|
const image = container.Image;
|
|
|
|
|
|
|
|
const livepush = await Livepush.init(
|
|
|
|
await fs.readFile('Dockerfile.debug'),
|
|
|
|
'.',
|
|
|
|
containerId,
|
|
|
|
// a bit of a hack, as the multistage images aren't
|
|
|
|
// present, but it shouldn't make a difference as these
|
|
|
|
// will never change
|
|
|
|
_.times(7, () => image),
|
|
|
|
docker,
|
|
|
|
);
|
|
|
|
|
|
|
|
chokidar
|
|
|
|
.watch('.', {
|
|
|
|
ignored: /((^|[\/\\])\..|node_modules.*)/,
|
2019-05-29 11:44:48 +00:00
|
|
|
ignoreInitial: false,
|
2019-05-15 11:37:38 +00:00
|
|
|
})
|
|
|
|
.on('add', path => {
|
2019-05-29 11:44:48 +00:00
|
|
|
changedFiles.push(path);
|
2019-05-30 22:30:49 +00:00
|
|
|
performLivepush(livepush, containerId, docker);
|
2019-05-15 11:37:38 +00:00
|
|
|
})
|
|
|
|
.on('change', path => {
|
2019-05-29 11:44:48 +00:00
|
|
|
changedFiles.push(path);
|
2019-05-30 22:30:49 +00:00
|
|
|
performLivepush(livepush, containerId, docker);
|
2019-05-15 11:37:38 +00:00
|
|
|
})
|
|
|
|
.on('unlink', path => {
|
2019-05-29 11:44:48 +00:00
|
|
|
deletedFiles.push(path);
|
2019-05-30 22:30:49 +00:00
|
|
|
performLivepush(livepush, containerId, docker);
|
2019-05-15 11:37:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
livepush.on('commandExecute', ({ command }) => {
|
|
|
|
console.log('SYNC: executing:', command);
|
|
|
|
});
|
|
|
|
livepush.on('commandOutput', ({ output }) => {
|
|
|
|
console.log(`\t${output.data.toString()}`);
|
|
|
|
});
|
|
|
|
livepush.on('containerRestart', () => {
|
|
|
|
console.log('SYNC: Restarting container...');
|
|
|
|
});
|
|
|
|
})();
|