Skip to content

Commit

Permalink
working prototype (with ufw service)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark committed Apr 8, 2013
1 parent 832dc36 commit 9ba0700
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 11 deletions.
3 changes: 2 additions & 1 deletion app.js
Expand Up @@ -2,7 +2,8 @@ var Savetime = require('./lib/Savetime');
var SavetimePluginSocketio = require('./lib/Savetime/PluginSocketio');

var serviceMachine = require('./lib/ServiceMachine');
var ServiceLogic = require('./lib/ServiceLogic');
// var ServiceLogic = require('./lib/ServiceLogic/Fake');
var ServiceLogic = require('./lib/ServiceLogic/Ufw');
serviceMachine.register(ServiceLogic);

var billingMachine = require('./lib/BillingMachine');
Expand Down
16 changes: 6 additions & 10 deletions lib/ServiceLogic.js
Expand Up @@ -7,7 +7,7 @@ var ServiceLogic = module.exports = boop.extend({

IN_switching_on : function (session, state, transition, msg) {
console.log('Service IN:', state.name);
this.doIfup(function (error) {
this.doSwitchUp(function (error) {
if(error) {
session.maybeDispatch({msgId:'error'});
return;
Expand All @@ -23,7 +23,7 @@ var ServiceLogic = module.exports = boop.extend({

IN_switching_off : function (session, state, transition, msg) {
console.log('Service IN:', state.name);
this.doIfdown(function (error) {
this.doSwitchDown(function (error) {
if(error) {
session.maybeDispatch({msgId:'error'});
return;
Expand All @@ -42,15 +42,11 @@ var ServiceLogic = module.exports = boop.extend({
session.output.emit(state.name);
},

doIfup : function (next) {
setTimeout(function () {
next(null);
}, 1000);
doSwitchUp : function (next) {
throw new Error('EABSTRACT');
},

doIfdown : function (next) {
setTimeout(function () {
next(null);
}, 1000);
doSwitchDown : function (next) {
throw new Error('EABSTRACT');
}
});
16 changes: 16 additions & 0 deletions lib/ServiceLogic/Fake.js
@@ -0,0 +1,16 @@
var ServiceLogic = require('../ServiceLogic');

var ServiceLogic_Fake = module.exports = ServiceLogic.extend({

doSwitchUp : function (next) {
setTimeout(function () {
next(null);
}, 1000);
},

doSwitchDown : function (next) {
setTimeout(function () {
next(null);
}, 1000);
}
});
30 changes: 30 additions & 0 deletions lib/ServiceLogic/Ufw.js
@@ -0,0 +1,30 @@
var path = require('path');
var child_process = require('child_process');
var ServiceLogic = require('../ServiceLogic');

var SCRIPT_UP = path.resolve(__dirname + '/ufw/ufw-up.sh');
var SCRIPT_DOWN = path.resolve(__dirname + '/ufw/ufw-down.sh');


var ServiceLogic_Ufw = module.exports = ServiceLogic.extend({

doSwitchUp : function (next) {
child_process.exec(SCRIPT_UP, function (error, stdout, stderr) {
if(error) {
next(error);
return;
}
next();
});
},

doSwitchDown : function (next) {
child_process.exec(SCRIPT_DOWN, function (error, stdout, stderr) {
if(error) {
next(error);
return;
}
next();
});
}
});
4 changes: 4 additions & 0 deletions lib/ServiceLogic/ufw/ufw-down.sh
@@ -0,0 +1,4 @@
#!/bin/sh
UFW=`which ufw`
${UFW} reject out http
${UFW} reject out https
4 changes: 4 additions & 0 deletions lib/ServiceLogic/ufw/ufw-up.sh
@@ -0,0 +1,4 @@
#!/bin/sh
UFW=`which ufw`
${UFW} --force delete reject out http
${UFW} --force delete reject out https

0 comments on commit 9ba0700

Please sign in to comment.