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,
"stopFixInterval": true,
"runSetup": true,
"removeBindFolder": true,
"removeAppWatcher": true,
"addAppWatcher": true,
"resolveWatchers": true,
"recoverApps": true,
"restartApp": true,
"deleteApp": true,
"getAppLogs": true,
"hasDockerfile": true,
"createTarFile": true,
"createTarFileSync": true,
"deleteImage": true,
@ -232,7 +226,6 @@
"FormSchema": true,
"showFormSuccess": true,
"resetForm": true,
"deleteAppSync": true,
// Testing
"require": false,

View File

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

View File

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

View File

@ -66,3 +66,7 @@ Util.copyVolumes = function (directory, 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) {
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) {
Apps.restart = function (app, callback) {
if (app.docker && app.docker.Id) {
try {
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) {
var container = docker.getContainer(app.docker.Id);
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) {
exec(path.join(Util.getBinDir(), 'boot2docker') + ' ssh "sudo rm -rf /var/lib/docker/binds/' + name + '"', function(err, stdout) {
callback(err, stdout);
});
};
recoverApps = function (callback) {
Apps.recover = function (callback) {
var apps = Apps.find({}).fetch();
_.each(apps, function (app) {
// Update the app with the latest container info
@ -85,7 +62,7 @@ recoverApps = function (callback) {
console.log('restarting: ' + app.name);
console.log(app.docker.Id);
Fiber(function () {
restartApp(app, function (err) {
Apps.restart(app, function (err) {
if (err) { console.error(err); }
});
}).run();
@ -98,7 +75,7 @@ recoverApps = function (callback) {
Meteor.methods({
recoverApps: function () {
this.unblock();
return Meteor._wrapAsync(recoverApps)();
return Meteor._wrapAsync(Apps.recover)();
},
configVar: function (appId, configVars) {
this.unblock();
@ -148,7 +125,7 @@ Meteor.methods({
this.unblock();
var app = Apps.findOne(appId);
if (app) {
getAppLogs(app, function (err) {
Apps.logs(app, function (err) {
if (err) { throw err; }
});
}
@ -160,7 +137,7 @@ Meteor.methods({
Apps.update(app._id, {$set: {
status: 'STARTING'
}});
restartApp(app, function (err) {
Apps.restart(app, function (err) {
if (err) { console.error(err); }
});
}

View File

@ -5,10 +5,6 @@ docker = new Dockerode({host: '192.168.59.103', port: '2375'});
Docker = {};
hasDockerfile = function (directory) {
return fs.existsSync(path.join(directory, 'Dockerfile'));
};
Docker.removeContainer = function (containerId, callback) {
var container = docker.getContainer(containerId);
container.kill(function (err) {
@ -103,19 +99,6 @@ Docker.restartContainerSync = function (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 result = [];
if (obj !== null && typeof obj === 'object') {
@ -161,6 +144,12 @@ Docker.removeImageSync = function (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 () {
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) {
var patternString = "(FROM)(.*)";
var regex = new RegExp(patternString, "g");
@ -272,7 +285,13 @@ Meteor.methods({
if (apps.length > 0) {
_.each(apps, function (app) {
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, {
$set: {
'docker.Id': null,
@ -306,7 +325,7 @@ Meteor.methods({
},
validateDirectory: function (directory) {
this.unblock();
if (!hasDockerfile(directory)) {
if (!Util.hasDockerfile(directory)) {
throw new Meteor.Error(400, "Only directories with Dockerfiles are supported now.");
}
},