Refactored app code.

This commit is contained in:
Sean Li 2014-09-01 00:59:32 -07:00
parent c299ef3a07
commit 72271a3d20
7 changed files with 52 additions and 66 deletions

View File

@ -194,15 +194,9 @@
"fixInterval": true, "fixInterval": true,
"stopFixInterval": true, "stopFixInterval": true,
"runSetup": true, "runSetup": true,
"removeBindFolder": true,
"removeAppWatcher": true, "removeAppWatcher": true,
"addAppWatcher": true, "addAppWatcher": true,
"resolveWatchers": true, "resolveWatchers": true,
"recoverApps": true,
"restartApp": true,
"deleteApp": true,
"getAppLogs": true,
"hasDockerfile": true,
"createTarFile": true, "createTarFile": true,
"createTarFileSync": true, "createTarFileSync": true,
"deleteImage": true, "deleteImage": true,
@ -232,7 +226,6 @@
"FormSchema": true, "FormSchema": true,
"showFormSuccess": true, "showFormSuccess": true,
"resetForm": true, "resetForm": true,
"deleteAppSync": true,
// Testing // Testing
"require": false, "require": false,

View File

@ -148,7 +148,7 @@ startFixInterval = function () {
resolveWatchers(function () {}); resolveWatchers(function () {});
fixBoot2DockerVM(function (err) { fixBoot2DockerVM(function (err) {
if (err) { console.log(err); return; } if (err) { console.log(err); return; }
// Meteor.call('recoverApps'); Meteor.call('recoverApps');
fixDefaultImages(function (err) { fixDefaultImages(function (err) {
if (err) { console.log(err); return; } if (err) { console.log(err); return; }
fixDefaultContainers(function (err) { fixDefaultContainers(function (err) {

View File

@ -128,7 +128,7 @@ Apps.after.insert(function (userId, app) {
var image = Images.findOne(app.imageId); var image = Images.findOne(app.imageId);
Util.copyVolumes(image.path, app.name); Util.copyVolumes(image.path, app.name);
app = Apps.findOne(appId); app = Apps.findOne(appId);
removeBindFolder(app.name, function (err) { Docker.removeBindFolder(app.name, function (err) {
if (err) { if (err) {
console.error(err); console.error(err);
} }
@ -141,12 +141,16 @@ Apps.after.insert(function (userId, app) {
}); });
Apps.after.remove(function (userId, app) { Apps.after.remove(function (userId, app) {
deleteApp(app, function (err) { if (app.docker) {
if (err) { console.error(err); } try {
Docker.removeContainerSync(app.docker.Id);
} catch (e) {
console.error(e);
}
}
var appPath = path.join(KITE_PATH, app.name); var appPath = path.join(KITE_PATH, app.name);
Util.deleteFolder(appPath); Util.deleteFolder(appPath);
removeBindFolder(app.name, function () { Docker.removeBindFolder(app.name, function () {
console.log('Deleted Kite ' + app.name + ' directory.'); console.log('Deleted Kite ' + app.name + ' directory.');
}); });
});
}); });

View File

@ -66,3 +66,7 @@ Util.copyVolumes = function (directory, appName) {
console.log('Copied volumes for: ' + appName); console.log('Copied volumes for: ' + appName);
} }
}; };
Util.hasDockerfile = function (directory) {
return fs.existsSync(path.join(directory, 'Dockerfile'));
};

View File

@ -1,21 +1,4 @@
deleteApp = function (app, callback) { Apps.restart = function (app, callback) {
if (!app.docker) {
callback(null);
return;
}
try {
Docker.removeContainerSync(app.docker.Id);
} catch (e) {
console.error(e);
}
callback(null);
};
deleteAppSync = function (app) {
return Meteor._wrapAsync(deleteApp)(app);
};
restartApp = function (app, callback) {
if (app.docker && app.docker.Id) { if (app.docker && app.docker.Id) {
try { try {
Docker.restartContainerSync(app.docker.Id); Docker.restartContainerSync(app.docker.Id);
@ -37,7 +20,7 @@ restartApp = function (app, callback) {
} }
}; };
getAppLogs = function (app) { Apps.logs = function (app) {
if (app.docker && app.docker.Id) { if (app.docker && app.docker.Id) {
var container = docker.getContainer(app.docker.Id); var container = docker.getContainer(app.docker.Id);
container.logs({follow: false, stdout: true, stderr: true, timestamps: true, tail: 300}, function (err, response) { container.logs({follow: false, stdout: true, stderr: true, timestamps: true, tail: 300}, function (err, response) {
@ -66,13 +49,7 @@ getAppLogs = function (app) {
} }
}; };
removeBindFolder = function (name, callback) { Apps.recover = function (callback) {
exec(path.join(Util.getBinDir(), 'boot2docker') + ' ssh "sudo rm -rf /var/lib/docker/binds/' + name + '"', function(err, stdout) {
callback(err, stdout);
});
};
recoverApps = function (callback) {
var apps = Apps.find({}).fetch(); var apps = Apps.find({}).fetch();
_.each(apps, function (app) { _.each(apps, function (app) {
// Update the app with the latest container info // Update the app with the latest container info
@ -85,7 +62,7 @@ recoverApps = function (callback) {
console.log('restarting: ' + app.name); console.log('restarting: ' + app.name);
console.log(app.docker.Id); console.log(app.docker.Id);
Fiber(function () { Fiber(function () {
restartApp(app, function (err) { Apps.restart(app, function (err) {
if (err) { console.error(err); } if (err) { console.error(err); }
}); });
}).run(); }).run();
@ -98,7 +75,7 @@ recoverApps = function (callback) {
Meteor.methods({ Meteor.methods({
recoverApps: function () { recoverApps: function () {
this.unblock(); this.unblock();
return Meteor._wrapAsync(recoverApps)(); return Meteor._wrapAsync(Apps.recover)();
}, },
configVar: function (appId, configVars) { configVar: function (appId, configVars) {
this.unblock(); this.unblock();
@ -148,7 +125,7 @@ Meteor.methods({
this.unblock(); this.unblock();
var app = Apps.findOne(appId); var app = Apps.findOne(appId);
if (app) { if (app) {
getAppLogs(app, function (err) { Apps.logs(app, function (err) {
if (err) { throw err; } if (err) { throw err; }
}); });
} }
@ -160,7 +137,7 @@ Meteor.methods({
Apps.update(app._id, {$set: { Apps.update(app._id, {$set: {
status: 'STARTING' status: 'STARTING'
}}); }});
restartApp(app, function (err) { Apps.restart(app, function (err) {
if (err) { console.error(err); } if (err) { console.error(err); }
}); });
} }

View File

@ -5,10 +5,6 @@ docker = new Dockerode({host: '192.168.59.103', port: '2375'});
Docker = {}; Docker = {};
hasDockerfile = function (directory) {
return fs.existsSync(path.join(directory, 'Dockerfile'));
};
Docker.removeContainer = function (containerId, callback) { Docker.removeContainer = function (containerId, callback) {
var container = docker.getContainer(containerId); var container = docker.getContainer(containerId);
container.kill(function (err) { container.kill(function (err) {
@ -103,19 +99,6 @@ Docker.restartContainerSync = function (containerId) {
return Meteor._wrapAsync(Docker.restartContainer)(containerId); return Meteor._wrapAsync(Docker.restartContainer)(containerId);
}; };
createTarFile = function (image, callback) {
var TAR_PATH = path.join(KITE_TAR_PATH, image._id + '.tar');
exec('tar czf ' + TAR_PATH + ' -C ' + image.path + ' .', function (err) {
if (err) { callback(err, null); return; }
console.log('Created tar file: ' + TAR_PATH);
callback(null, TAR_PATH);
});
};
createTarFileSync = function (image) {
return Meteor._wrapAsync(createTarFile)(image);
};
var convertVolumeObjToArray = function (obj) { var convertVolumeObjToArray = function (obj) {
var result = []; var result = [];
if (obj !== null && typeof obj === 'object') { if (obj !== null && typeof obj === 'object') {
@ -161,6 +144,12 @@ Docker.removeImageSync = function (imageId) {
return Meteor._wrapAsync(Docker.removeImage)(imageId); return Meteor._wrapAsync(Docker.removeImage)(imageId);
}; };
Docker.removeBindFolder = function (name, callback) {
exec(path.join(Util.getBinDir(), 'boot2docker') + ' ssh "sudo rm -rf /var/lib/docker/binds/' + name + '"', function (err, stdout) {
callback(err, stdout);
});
};
var defaultContainerOptions = function () { var defaultContainerOptions = function () {
return [ return [
{ {

View File

@ -1,3 +1,16 @@
createTarFile = function (image, callback) {
var TAR_PATH = path.join(KITE_TAR_PATH, image._id + '.tar');
exec('tar czf ' + TAR_PATH + ' -C ' + image.path + ' .', function (err) {
if (err) { callback(err, null); return; }
console.log('Created tar file: ' + TAR_PATH);
callback(null, TAR_PATH);
});
};
createTarFileSync = function (image) {
return Meteor._wrapAsync(createTarFile)(image);
};
getFromImage = function (dockerfile) { getFromImage = function (dockerfile) {
var patternString = "(FROM)(.*)"; var patternString = "(FROM)(.*)";
var regex = new RegExp(patternString, "g"); var regex = new RegExp(patternString, "g");
@ -272,7 +285,13 @@ Meteor.methods({
if (apps.length > 0) { if (apps.length > 0) {
_.each(apps, function (app) { _.each(apps, function (app) {
console.log('Updating app: ' + app.name); console.log('Updating app: ' + app.name);
deleteAppSync(app); if (app.docker) {
try {
Docker.removeContainerSync(app.docker.Id);
} catch (e) {
console.error(e);
}
}
Apps.update(app._id, { Apps.update(app._id, {
$set: { $set: {
'docker.Id': null, 'docker.Id': null,
@ -306,7 +325,7 @@ Meteor.methods({
}, },
validateDirectory: function (directory) { validateDirectory: function (directory) {
this.unblock(); this.unblock();
if (!hasDockerfile(directory)) { if (!Util.hasDockerfile(directory)) {
throw new Meteor.Error(400, "Only directories with Dockerfiles are supported now."); throw new Meteor.Error(400, "Only directories with Dockerfiles are supported now.");
} }
}, },