Skip to content
This repository has been archived by the owner on Jun 27, 2019. It is now read-only.

Commit

Permalink
run FBP using Environment file instead of running directly
Browse files Browse the repository at this point in the history
Systemd environment file guarantees that each user will be able
to run only one FBP code (Avoiding zombie fbp_runner service).

Also, it exports correctly the configuration file, fixing an issue that the
runner is unable to find the configuration file that is located in another
folder.

This patch also fixes some code indentation

Signed-off-by: Bruno Bottazzini <bruno.bottazzini@intel.com>
  • Loading branch information
Bruno Bottazzini committed Apr 15, 2016
1 parent 3e44972 commit 793ea64
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 92 deletions.
68 changes: 28 additions & 40 deletions client/js/controllers/editor.js
Expand Up @@ -22,7 +22,6 @@
function ($compile, $scope, $http, $interval, $document, broadcastService,
FetchFileFactory, usSpinnerService, svConf) {
var isRunningSyntax = false;
var runningFBP;
var promiseCheckSyntax;
var promiseServiceStatus;
var promiseRunViewer;
Expand Down Expand Up @@ -294,7 +293,7 @@
$http.get('/api/journald',
{
params: {
"unit_path": runningFBP,
"unit_path": "fbp-runner@",
}
}).success(function(data) {
$scope.RunViewer = data;
Expand All @@ -306,14 +305,11 @@
$scope.run = function() {
if ($scope.isServiceRunning) {
//Post stop service
$http.post('/api/fbp/stop',
{
params: {"fbp_path": runningFBP}
}).success(function(data) {
$http.post('/api/fbp/stop').success(function(data) {
if (data == 1) {
alert("FBP Service failed to stop");
}
});
});
} else if ($scope.fbpType === true) {
var fbpCode = editor.getSession().getValue();
var fbpName = $scope.fileName;
Expand All @@ -334,7 +330,6 @@
}
}).success(function(data) {
if (data == 0) {
runningFBP = filePath;
$scope.openRunDialog();
} else {
alert("FBP Failed to run");
Expand All @@ -356,15 +351,11 @@
window.onbeforeunload = onBeforeUnload_Handler;
function onBeforeUnload_Handler() {
if ($scope.isServiceRunning) {
$http.post('/api/fbp/stop',
{params: {
"fbp_path": runningFBP
}
}).success(function(data) {
if (data == 1) {
alert("FBP Service failed to stop. Process should be stopped manually");
}
});
$http.post('/api/fbp/stop').success(function(data) {
if (data == 1) {
alert("FBP Service failed to stop. Process should be stopped manually");
}
});
}

if ($scope.shouldSave === true) {
Expand Down Expand Up @@ -768,30 +759,27 @@
};

$scope.getServiceStatus = function () {
if (runningFBP) {
$http.get('/api/service/status',
{
params: { "fbp_path": runningFBP }
}).success(function(data) {
$scope.ServiceStatus = data.trim();
if ($scope.ServiceStatus.indexOf("active (running)") > -1) {
$scope.isServiceRunning = true;
} else {
$scope.isServiceRunning = false;
}
if (runningFBP) {
$scope.ServiceStatus = $scope.ServiceStatus.replace(/since.*;/,"");
} else {
$scope.ServiceStatus = runningFBP + " - " + $scope.ServiceStatus.replace(/since.*;/,"");
}
if ($scope.ServiceStatus) {
$scope.ServiceStatus = "FBP Running Status: " + $scope.ServiceStatus
}
}).error(function(){
$scope.ServiceStatus = "Failed to get service information";
});
$http.get('/api/service/status',
{
params: { "service": "fbp-runner.service"}
}).success(function(data) {
$scope.ServiceStatus = data.trim();
if ($scope.ServiceStatus.indexOf("active (running)") > -1) {
$scope.isServiceRunning = true;
} else {
$scope.isServiceRunning = false;
}
$scope.startServiceStatus();

$scope.ServiceStatus = $scope.ServiceStatus.replace(/since.*;/,"");

if ($scope.ServiceStatus) {
$scope.ServiceStatus = "FBP Running Status: " + $scope.ServiceStatus
}

}).error(function(){
$scope.ServiceStatus = "Failed to get service information";
});
$scope.startServiceStatus();
};

$scope.refreshTree = function () {
Expand Down
17 changes: 7 additions & 10 deletions scripts/fbp-runner.sh
Expand Up @@ -17,22 +17,19 @@
# limitations under the License.
export SOL_LOG_PRINT_FUNCTION="journal"

if [ $# -eq 3 ]; then
export SOL_FLOW_MODULE_RESOLVER_CONFFILE=$3
fi
FBP_PATH="$2"
echo "FBP_PATH="$FBP_PATH
SERVICE="fbp-runner@"$(systemd-escape $FBP_PATH)
echo "SERVICE="$SERVICE
SCRIPT="$FBP_PATH"
SCRIPT="$3"
ENV_PATH="$2"
SERVICE="fbp-runner@"$(systemd-escape $ENV_PATH)

systemctl stop $SERVICE

if [ $1 == "start" ]; then
syntax=`sol-fbp-runner -c $SCRIPT | grep OK`
systemctl $1 $SERVICE
if [ -n "$syntax" ]; then
exit 0
exit 0
else
exit 1
exit 1
fi
else
st=`systemctl status $SERVICE | grep "Active:"`
Expand Down
4 changes: 2 additions & 2 deletions scripts/units/fbp-runner@.service
Expand Up @@ -3,8 +3,8 @@ Description=Run FBP Script when using Soletta Devapp in web browser

[Service]
StandardOutput=journal+console
WorkingDirectory=%I
ExecStart=/usr/bin/sol-fbp-runner %I/fbp_runner.fbp
EnvironmentFile=%I
ExecStart=/usr/bin/sol-fbp-runner $FBP_FILE

[Install]
WantedBy=multi-user.target
90 changes: 50 additions & 40 deletions server/routes.js
Expand Up @@ -25,6 +25,7 @@
require('./configuration.js')();

var jConf = getConfigurationJson();
var runningFBPName;

/* GET home page. */
router.get('/', function(req, res) {
Expand Down Expand Up @@ -59,12 +60,13 @@
/* Journald Service Status */
router.get('/api/service/status', function(req, res) {
if (jConf.journal_access === true) {
var exec = require('child_process').exec;
var stdout = "";
var path = req.query.fbp_path;
var fbp_path = generateHiddenPath(path);
if (fbp_path) {
var child = exec(scripts_dir() + 'systemctl-unit.sh fbp-runner ' + fbp_path);
var service = req.query.service;
if (service) {
var exec = require('child_process').exec;
var stdout = "";
var child = exec(scripts_dir() + 'systemctl-unit.sh ' +
service + " " +
env_file(current_user(req)));
if (!child) {
res.send("Failed to run command on server");
}
Expand All @@ -77,10 +79,11 @@
});
child.on('close', function(code) {
stdout = stdout.replace(/Active:/, '').trim();
if (runningFBPName) {
stdout = runningFBPName + " - " + stdout
}
res.send(stdout);
});
} else {
res.send("There is no running FBP.")
}
} else {
res.status(404).send("Unsupported api");
Expand Down Expand Up @@ -120,9 +123,9 @@
var spawn = require('child_process').spawn;
var stdout = "";
var error = false;
var path = req.query.unit_path;
var unit_name = req.query.unit_name;
var child;
if (!path) {
if (!unit_name) {
child = spawn('journalctl',
['-o', 'json-pretty', '-n', '100', '--no-pager']);

Expand All @@ -144,10 +147,9 @@
}
});
} else {
var unit_path = generateHiddenPath(path);
var script = scripts_dir() + "/journalctl-unit.sh";
child = spawn(script,
[unit_path]);
[env_file(current_user(req))]);
child.on('error', function(err) {
error = true;
});
Expand Down Expand Up @@ -203,28 +205,42 @@
res.sendStatus(1);
} else {
var child;
var err;
var stdout = "";
var script = scripts_dir() + "/fbp-runner.sh";
var fbp_path = generateHiddenPath(path);
if (fbp_path) {
script = script + ' start ' + fbp_path;
err = writeFile(fbp_path, code);
script = script + ' start ' + env_file(current_user(req)) + ' ' + fbp_path;
if (conf) {
script = script + " " + conf;
err = writeFile(env_file(current_user(req)),
'FBP_FILE="' + fbp_path + '"\n'
+ 'SOL_FLOW_MODULE_RESOLVER_CONFFILE="' + conf + '"');
} else {
err = writeFile(env_file(current_user(req)),
'FBP_FILE="' + fbp_path + '"\n'
+ 'SOL_FLOW_MODULE_RESOLVER_CONFFILE=""');
}
getConfigureFile(current_user(req), conf, function (error) {
child = exec("sh " + script);
child.stdout.on('data', function(data) {
stdout += data;
console.log('stdout: ' + data);
});
child.stderr.on('data', function(data) {
console.log('stderr: ' + data);
});
child.on('close', function(code) {
console.log('closing code: ' + code);
res.sendStatus(code);
if (!err) {
getConfigureFile(current_user(req), conf, function (error) {
child = exec("sh " + script);
child.stdout.on('data', function(data) {
stdout += data;
console.log('stdout: ' + data);
});
child.stderr.on('data', function(data) {
console.log('stderr: ' + data);
});
child.on('close', function(code) {
console.log('closing code: ' + code);
var array_path = fbp_path.split("/");
runningFBPName = array_path.pop();
res.sendStatus(code);
});
});
});
} else {
res.status(404).send("Failed to write envrioment file");
}
} else {
res.status(404).send("Failed to get FBP file");
}
Expand Down Expand Up @@ -459,19 +475,13 @@
if (jConf.run_fbp_access === true) {
var exec = require('child_process').exec;
var child;
var path = req.body.params.fbp_path;
var fbp_path = generateHiddenPath(path);
if (!fbp_path) {
res.status(400).send("Failed to get running FBP path");
} else {
var script = scripts_dir() + "/fbp-runner.sh";
script = script + ' stop ' + fbp_path;
child = exec("sh " + script);
child.on('close', function(code) {
console.log('closing code: ' + code);
res.sendStatus(code);
});
}
var script = scripts_dir() + "/fbp-runner.sh";
script = script + ' stop ' + env_file(current_user(req));
child = exec("sh " + script);
child.on('close', function(code) {
console.log('closing code: ' + code);
res.sendStatus(code);
});
} else {
res.status(404).send("Unsupported api");
}
Expand Down
4 changes: 4 additions & 0 deletions server/tools.js
Expand Up @@ -35,6 +35,10 @@ module.exports = function () {
return __dirname + '/../scripts/';
};

this.env_file = function(user) {
return "/tmp/" + user + '/fbp_run.env';
};

this.current_user = function (req) {
var jConf = getConfigurationJson();
if (jConf.session_system === true) {
Expand Down

0 comments on commit 793ea64

Please sign in to comment.