From 72271a3d20bcbe6be2e05d21fa54f1173e01d891 Mon Sep 17 00:00:00 2001 From: Sean Li Date: Mon, 1 Sep 2014 00:59:32 -0700 Subject: [PATCH] Refactored app code. --- meteor/.jshintrc | 7 ------- meteor/client/main.js | 4 ++-- meteor/collections/apps.js | 20 ++++++++++++-------- meteor/lib/utilities.js | 4 ++++ meteor/server/apps.js | 37 +++++++------------------------------ meteor/server/docker.js | 23 ++++++----------------- meteor/server/images.js | 23 +++++++++++++++++++++-- 7 files changed, 52 insertions(+), 66 deletions(-) diff --git a/meteor/.jshintrc b/meteor/.jshintrc index 300055089f..139808b8bf 100755 --- a/meteor/.jshintrc +++ b/meteor/.jshintrc @@ -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, diff --git a/meteor/client/main.js b/meteor/client/main.js index 7abe7d59a1..4b47333ece 100755 --- a/meteor/client/main.js +++ b/meteor/client/main.js @@ -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; -}; \ No newline at end of file +}; diff --git a/meteor/collections/apps.js b/meteor/collections/apps.js index 1b7ee7ce97..ba40c9b2e9 100755 --- a/meteor/collections/apps.js +++ b/meteor/collections/apps.js @@ -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.'); }); }); diff --git a/meteor/lib/utilities.js b/meteor/lib/utilities.js index 73aea70f5f..2497df3b53 100644 --- a/meteor/lib/utilities.js +++ b/meteor/lib/utilities.js @@ -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')); +}; diff --git a/meteor/server/apps.js b/meteor/server/apps.js index 6cc531bc13..a8e7c8dce7 100755 --- a/meteor/server/apps.js +++ b/meteor/server/apps.js @@ -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); } }); } diff --git a/meteor/server/docker.js b/meteor/server/docker.js index 447359cf2d..ae1e759985 100755 --- a/meteor/server/docker.js +++ b/meteor/server/docker.js @@ -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 [ { diff --git a/meteor/server/images.js b/meteor/server/images.js index a2c91c538b..3befdd5e2b 100755 --- a/meteor/server/images.js +++ b/meteor/server/images.js @@ -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."); } },