From 47dfb0775db9002313b16108f9a66d7656dfbd31 Mon Sep 17 00:00:00 2001 From: Peter Nguyen Date: Sat, 6 Sep 2014 22:14:09 +0200 Subject: [PATCH] Added feature to start/stop apps --- meteor/.meteor/packages | 1 - meteor/client/lib/apputil.js | 41 ++++++++++++++++++- meteor/client/lib/docker.js | 26 ++++++++++++ .../dashboard/apps/dashboard-single-app.html | 14 +++++-- .../dashboard/apps/dashboard-single-app.js | 8 ++++ 5 files changed, 85 insertions(+), 5 deletions(-) diff --git a/meteor/.meteor/packages b/meteor/.meteor/packages index 6d8743e310..0a499fa9ec 100755 --- a/meteor/.meteor/packages +++ b/meteor/.meteor/packages @@ -10,6 +10,5 @@ iron-router handlebar-helpers underscore-string-latest collection-helpers -octicons fast-render iron-router-ga diff --git a/meteor/client/lib/apputil.js b/meteor/client/lib/apputil.js index 03ff2b4bf1..4af94d476d 100644 --- a/meteor/client/lib/apputil.js +++ b/meteor/client/lib/apputil.js @@ -47,6 +47,45 @@ AppUtil.restartHelper = function (app) { } }; +AppUtil.start = function (appId) { + var app = Apps.findOne(appId); + if (app && app.docker) { + Apps.update(app._id, {$set: { + status: 'STARTING' + }}); + Docker.getContainerData(app.docker.Id, function (err, data) { + if (err) { console.error(err); } + // Use dig to refresh the DNS + exec('/usr/bin/dig ' + app.name + '.kite @172.17.42.1', function(err, stdout, stderr) { + console.log(err); + console.log(stdout); + console.log(stderr); + Apps.update(app._id, {$set: { + status: 'READY', + docker: data + }}); + }); + }); + } +}; + +AppUtil.stop = function (appId) { + var app = Apps.findOne(appId); + if (app && app.docker) { + Apps.update(app._id, {$set: { + status: 'STOPPING' + }}); + Docker.stopContainer(app.docker.Id, function (err) { + if (err) { console.error(err); } + Meteor.setTimeout(function () { + Apps.update(app._id, {$set: { + status: 'STOPPED' + }}); + }, 2500); + }); + } +}; + AppUtil.restart = function (appId) { var app = Apps.findOne(appId); if (app && app.docker) { @@ -115,7 +154,7 @@ AppUtil.recover = function () { } var container = Docker.client().getContainer(app.docker.Id); container.inspect(function (err, data) { - if (app.status !== 'STARTING' && data && data.State && !data.State.Running) { + if (app.status !== 'STARTING' && app.status !== 'STOPPING' && app.status !== 'STOPPED' && data && data.State && !data.State.Running) { console.log('Restarting: ' + app.name); console.log(app.docker.Id); AppUtil.restartHelper(app, function (err) { diff --git a/meteor/client/lib/docker.js b/meteor/client/lib/docker.js index 2f592ba865..f4c4e70855 100644 --- a/meteor/client/lib/docker.js +++ b/meteor/client/lib/docker.js @@ -81,6 +81,32 @@ Docker.runContainer = function (app, image, callback) { }); }; +Docker.startContainer = function (containerId, callback) { + var container = docker.getContainer(containerId); + container.stop(function (err) { + if (err) { + console.log(err); + callback(err); + return; + } + console.log('Started container: ' + containerId); + callback(null); + }); +}; + +Docker.stopContainer = function (containerId, callback) { + var container = docker.getContainer(containerId); + container.stop(function (err) { + if (err) { + console.log(err); + callback(err); + return; + } + console.log('Stopped container: ' + containerId); + callback(null); + }); +}; + Docker.restartContainer = function (containerId, callback) { var container = docker.getContainer(containerId); container.restart(function (err) { diff --git a/meteor/client/views/dashboard/apps/dashboard-single-app.html b/meteor/client/views/dashboard/apps/dashboard-single-app.html index f1e3740ff8..e8fd11b7ac 100755 --- a/meteor/client/views/dashboard/apps/dashboard-single-app.html +++ b/meteor/client/views/dashboard/apps/dashboard-single-app.html @@ -7,10 +7,14 @@ {{#if $eq status 'STARTING'}} {{else}} - {{#if $eq status 'ERROR'}} - + {{#if $eq status 'STOPPING'}} + {{else}} - + {{#if $eq status 'ERROR'}} + + {{else}} + + {{/if}} {{/if}} {{/if}} {{/if}} @@ -18,7 +22,11 @@ {{image.meta.name}}
+ {{#if $eq status 'STOPPED'}} + + {{/if}} {{#if $eq status 'READY'}} + {{#if url}} {{/if}} diff --git a/meteor/client/views/dashboard/apps/dashboard-single-app.js b/meteor/client/views/dashboard/apps/dashboard-single-app.js index 1b6190205c..8bd9da0b01 100755 --- a/meteor/client/views/dashboard/apps/dashboard-single-app.js +++ b/meteor/client/views/dashboard/apps/dashboard-single-app.js @@ -32,6 +32,14 @@ Template.dashboard_single_app.events({ } }); }, + 'click .btn-start': function () { + AppUtil.start(this._id); + $('.btn-icon').tooltip('hide'); + }, + 'click .btn-stop': function () { + AppUtil.stop(this._id); + $('.btn-icon').tooltip('hide'); + }, 'click .btn-restart': function () { AppUtil.restart(this._id); },