1
Fork 0

initial version of shim.js

This commit is contained in:
Conduitry 2018-07-06 15:54:22 -04:00
parent 9ec8c183ed
commit 8381088db7
1 changed files with 64 additions and 0 deletions

64
shim.js Normal file
View File

@ -0,0 +1,64 @@
const { watch } = require('fs');
const { request } = require('http');
const socketPath = '//./pipe/docker_engine';
const apiVersion = '1.37';
const container = process.argv[2];
const response = request =>
new Promise((resolve, reject) =>
request
.on('response', response => {
const chunks = [];
response
.on('data', chunk => chunks.push(chunk))
.on('end', () => resolve(Buffer.concat(chunks)));
})
.on('error', error => reject(error)),
);
const attachWatcher = (source, target) => {
watch(source, { recursive: true }, async (eventType, filename) => {
await new Promise(res => setTimeout(res, 10));
const dest = target + '/' + filename.replace(/\\/g, '/');
console.log(`Event: ${dest}`);
const { Id } = JSON.parse(
(await response(
request({
socketPath,
method: 'post',
path: `/v${apiVersion}/containers/${container}/exec`,
headers: { 'content-type': 'application/json' },
}).end(JSON.stringify({ Cmd: ['chmod', '+', dest] })),
)).toString(),
);
request({
socketPath,
method: 'post',
path: `/v${apiVersion}/exec/${Id}/start`,
headers: { 'content-type': 'application/json' },
}).end(JSON.stringify({ Detach: true }));
});
console.log(`Watching ${source} => ${target}`);
};
(async () => {
const info = JSON.parse(
(await response(
request({
socketPath,
method: 'get',
path: `/v${apiVersion}/containers/${container}/json`,
}).end(),
)).toString(),
);
for (const { Type, Source, Destination } of info.Mounts) {
if (Type === 'bind' && Source.startsWith('/host_mnt/')) {
attachWatcher(
Source[10] + ':' + Source.slice(11).replace(/\//g, '\\'),
Destination,
);
}
}
})();