Moved kite functions to utilities.

This commit is contained in:
Sean Li 2014-08-31 19:56:18 -07:00
parent da26b980f6
commit b459247ea4
7 changed files with 82 additions and 70 deletions

View File

@ -116,3 +116,14 @@ Apps.helpers({
});
Apps.attachSchema(schemaApps);
Apps.after.remove(function (userId, app) {
deleteApp(app, function (err) {
if (err) { console.error(err); }
var appPath = path.join(KITE_PATH, app.name);
deleteFolder(appPath);
removeBindFolder(app.name, function () {
console.log('Deleted Kite ' + app.name + ' directory.');
});
});
});

View File

@ -29,6 +29,7 @@ recoverApps = function (callback) {
Meteor.methods({
recoverApps: function () {
this.unblock();
return Meteor._wrapAsync(recoverApps)();
},
configVar: function (appId, configVars) {
@ -48,19 +49,10 @@ Meteor.methods({
if (!app) {
throw new Meteor.Error(403, 'No app found with this ID');
}
deleteApp(app, function (err) {
if (err) { console.error(err); }
var appPath = path.join(KITE_PATH, app.name);
deleteFolder(appPath);
removeBindFolder(app.name, function () {
console.log('Deleted Kite ' + app.name + ' directory.');
Fiber(function () {
Apps.remove({_id: app._id});
}).run();
});
});
Apps.remove({_id: app._id});
},
createApp: function (formData) {
this.unblock();
var validationResult = formValidate(formData, FormSchema.formCreateApp);
if (validationResult.errors) {
throw new Meteor.Error(400, 'Validation Failed.', validationResult.errors);
@ -123,6 +115,7 @@ Meteor.methods({
}
},
resolveWatchers: function () {
this.unblock();
return Meteor._wrapAsync(resolveWatchers)();
}
});

View File

@ -0,0 +1,24 @@
KITE_PATH = path.join(getHomePath(), 'Kitematic');
KITE_TAR_PATH = path.join(KITE_PATH, '.tar');
KITE_IMAGES_PATH = path.join(KITE_PATH, '.images');
if (!fs.existsSync(KITE_PATH)) {
console.log('Created Kitematic directory.');
fs.mkdirSync(KITE_PATH, function (err) {
if (err) { throw err; }
});
}
if (!fs.existsSync(KITE_TAR_PATH)) {
console.log('Created Kitematic .tar directory.');
fs.mkdirSync(KITE_TAR_PATH, function (err) {
if (err) { throw err; }
});
}
if (!fs.existsSync(KITE_IMAGES_PATH)) {
console.log('Created Kitematic .images directory.');
fs.mkdirSync(KITE_IMAGES_PATH, function (err) {
if (err) { throw err; }
});
}

View File

@ -632,12 +632,14 @@ Meteor.methods({
runApp: function (app) {
this.unblock();
var image = Images.findOne({_id: app.imageId});
// Delete old container if one already exists
try {
Docker.removeContainerSync(app.name);
} catch (e) {}
try {
var container = Docker.runContainerSync(app, image);
var containerData = Docker.getContainerDataSync(container.id);
// Set a delay for app to spin up
Meteor.setTimeout(function () {
Apps.update(app._id, {$set: {
docker: containerData,
@ -652,18 +654,23 @@ Meteor.methods({
return DOCKER_HOST;
},
reloadDefaultContainers: function () {
this.unblock();
return Meteor._wrapAsync(reloadDefaultContainers)();
},
checkDefaultImages: function () {
this.unblock();
return Meteor._wrapAsync(checkDefaultImages)();
},
resolveDefaultImages: function () {
this.unblock();
return Meteor._wrapAsync(resolveDefaultImages)();
},
checkDefaultContainers: function () {
this.unblock();
return Meteor._wrapAsync(checkDefaultContainers)();
},
resolveDefaultContainers: function () {
this.unblock();
return Meteor._wrapAsync(resolveDefaultContainers)();
}
});

View File

@ -125,6 +125,7 @@ Meteor.methods({
});
},
validateDirectory: function (directory) {
this.unblock();
if (!hasDockerfile(directory)) {
throw new Meteor.Error(400, "Only directories with Dockerfiles are supported now.");
}

View File

@ -1,59 +0,0 @@
KITE_PATH = path.join(getHomePath(), 'Kitematic');
KITE_TAR_PATH = path.join(KITE_PATH, '.tar');
KITE_IMAGES_PATH = path.join(KITE_PATH, '.images');
if (!fs.existsSync(KITE_PATH)) {
console.log('Created Kitematic directory.');
fs.mkdirSync(KITE_PATH, function (err) {
if (err) { throw err; }
});
}
if (!fs.existsSync(KITE_TAR_PATH)) {
console.log('Created Kitematic .tar directory.');
fs.mkdirSync(KITE_TAR_PATH, function (err) {
if (err) { throw err; }
});
}
if (!fs.existsSync(KITE_IMAGES_PATH)) {
console.log('Created Kitematic .images directory.');
fs.mkdirSync(KITE_IMAGES_PATH, function (err) {
if (err) { throw err; }
});
}
getImageJSON = function (directory) {
var KITE_JSON_PATH = path.join(directory, 'image.json');
if (fs.existsSync(KITE_JSON_PATH)) {
var data = fs.readFileSync(KITE_JSON_PATH, 'utf8');
return JSON.parse(data);
} else {
return null;
}
};
loadKiteVolumes = function (directory, appName) {
var KITE_VOLUMES_PATH = path.join(directory, 'volumes');
if (fs.existsSync(KITE_VOLUMES_PATH)) {
var destinationPath = path.join(KITE_PATH, appName);
copyFolder(KITE_VOLUMES_PATH, destinationPath);
console.log('Copied volumes for: ' + appName);
}
};
saveImageFolder = function (directory, imageId, callback) {
var destinationPath = path.join(KITE_IMAGES_PATH, imageId);
if (!fs.existsSync(destinationPath)) {
fs.mkdirSync(destinationPath, function (err) {
if (err) { callback(err); return; }
});
copyFolder(directory, destinationPath);
console.log('Copied image folder for: ' + imageId);
callback(null);
}
};
saveImageFolderSync = function (directory, imageId) {
return Meteor._wrapAsync(saveImageFolder)(directory, imageId);
};

View File

@ -51,3 +51,38 @@ copyFolder = function (src, dest) {
}
}
};
getImageJSON = function (directory) {
var KITE_JSON_PATH = path.join(directory, 'image.json');
if (fs.existsSync(KITE_JSON_PATH)) {
var data = fs.readFileSync(KITE_JSON_PATH, 'utf8');
return JSON.parse(data);
} else {
return null;
}
};
loadKiteVolumes = function (directory, appName) {
var KITE_VOLUMES_PATH = path.join(directory, 'volumes');
if (fs.existsSync(KITE_VOLUMES_PATH)) {
var destinationPath = path.join(KITE_PATH, appName);
copyFolder(KITE_VOLUMES_PATH, destinationPath);
console.log('Copied volumes for: ' + appName);
}
};
saveImageFolder = function (directory, imageId, callback) {
var destinationPath = path.join(KITE_IMAGES_PATH, imageId);
if (!fs.existsSync(destinationPath)) {
fs.mkdirSync(destinationPath, function (err) {
if (err) { callback(err); return; }
});
copyFolder(directory, destinationPath);
console.log('Copied image folder for: ' + imageId);
callback(null);
}
};
saveImageFolderSync = function (directory, imageId) {
return Meteor._wrapAsync(saveImageFolder)(directory, imageId);
};