comments and adjustments

master
Conduitry 4 years ago
parent 70eb4df2b9
commit 9beb3b93b5
  1. 20
      shim.js

@ -5,7 +5,12 @@ const socketPath = '//./pipe/docker_engine';
const apiVersion = '1.37';
const container = process.argv[2];
if (!container) {
console.error('Argument required: container name or id');
process.exit(1);
}
// given a ClientRequest object, return a promise resolving to a Buffer of the entire response
const response = request =>
new Promise((resolve, reject) =>
request
@ -18,9 +23,12 @@ const response = request =>
.on('error', error => reject(error)),
);
// handle a watch event
const watchHandler = async (target, filename) => {
// determine the path inside the container
const dest = target + '/' + filename.replace(/\\/g, '/');
console.log(`Event: ${dest}`);
console.log(`Changed: ${dest}`);
// create an exec instance for calling chmod
const { Id } = JSON.parse(
(await response(
request({
@ -31,6 +39,7 @@ const watchHandler = async (target, filename) => {
}).end(JSON.stringify({ Cmd: ['chmod', '+', dest] })),
)).toString(),
);
// start the exec instance
request({
socketPath,
method: 'post',
@ -39,18 +48,19 @@ const watchHandler = async (target, filename) => {
}).end(JSON.stringify({ Detach: true }));
};
// attach a watcher for the given bind mount
const attachWatcher = (source, target) => {
// debounce the fs.watch events and handle them
const timeouts = new Map();
watch(source, { recursive: true }, async (eventType, filename) => {
if (timeouts.has(filename)) {
clearTimeout(timeouts.get(filename));
}
clearTimeout(timeouts.get(filename));
timeouts.set(filename, setTimeout(watchHandler, 10, target, filename));
});
console.log(`Watching ${source} => ${target}`);
};
(async () => {
// inspect the container
const info = JSON.parse(
(await response(
request({
@ -60,8 +70,10 @@ const attachWatcher = (source, target) => {
}).end(),
)).toString(),
);
// attach a watcher for each bind mount
for (const { Type, Source, Destination } of info.Mounts) {
if (Type === 'bind' && Source.startsWith('/host_mnt/')) {
// determine the host path of the mount
attachWatcher(
Source[10] + ':' + Source.slice(11).replace(/\//g, '\\'),
Destination,