Refactored app code into hooks.

This commit is contained in:
Sean Li 2014-08-31 20:27:49 -07:00
parent e59c03d620
commit eb46a9dee4
4 changed files with 36 additions and 30 deletions

View File

@ -201,7 +201,7 @@
"restartApp": true,
"deleteApp": true,
"deleteFolder": true,
"loadKiteVolumes": true,
"copyVolumes": true,
"getAppLogs": true,
"hasDockerfile": true,
"createTarFile": true,

View File

@ -117,6 +117,29 @@ Apps.helpers({
Apps.attachSchema(schemaApps);
Apps.after.insert(function (userId, app) {
// Give app an unique environment variable
var appId = this._id;
Apps.update(appId, {
$set: {
'config.APP_ID': appId
}
});
var image = Images.findOne(app.imageId);
copyVolumes(image.path, app.name);
app = Apps.findOne(appId);
removeBindFolder(app.name, function (err) {
if (err) {
console.error(err);
}
Fiber(function () {
Meteor.call('runApp', app, function (err) {
if (err) { throw err; }
});
}).run();
});
});
Apps.after.remove(function (userId, app) {
deleteApp(app, function (err) {
if (err) { console.error(err); }

View File

@ -58,39 +58,22 @@ Meteor.methods({
throw new Meteor.Error(400, 'Validation Failed.', validationResult.errors);
} else {
var cleaned = validationResult.cleaned;
var appObj = {
name: cleaned.name,
imageId: cleaned.imageId,
status: 'STARTING',
config: {}
};
var appId = Apps.insert(appObj);
var appPath = path.join(KITE_PATH, appObj.name);
var appName = cleaned.name;
var appPath = path.join(KITE_PATH, appName);
if (!fs.existsSync(appPath)) {
console.log('Created Kite ' + appObj.name + ' directory.');
console.log('Created Kite ' + appName + ' directory.');
fs.mkdirSync(appPath, function (err) {
if (err) { throw err; }
});
}
Apps.update(appId, {
$set: {
'config.APP_ID': appId,
var appObj = {
name: appName,
imageId: cleaned.imageId,
status: 'STARTING',
config: {},
path: appPath
}
});
var image = Images.findOne(appObj.imageId);
loadKiteVolumes(image.path, appObj.name);
var app = Apps.findOne(appId);
removeBindFolder(app.name, function (err) {
if (err) {
console.error(err);
}
Fiber(function () {
Meteor.call('runApp', app, function (err) {
if (err) { throw err; }
});
}).run();
});
};
Apps.insert(appObj);
}
},
getAppLogs: function (appId) {

View File

@ -62,7 +62,7 @@ getImageJSON = function (directory) {
}
};
loadKiteVolumes = function (directory, appName) {
copyVolumes = function (directory, appName) {
var KITE_VOLUMES_PATH = path.join(directory, 'volumes');
if (fs.existsSync(KITE_VOLUMES_PATH)) {
var destinationPath = path.join(KITE_PATH, appName);