From 48679a7ce9c6c2668985144e35ccb6b0e284ed81 Mon Sep 17 00:00:00 2001 From: Bruno Bottazzini Date: Wed, 30 Mar 2016 14:30:57 -0300 Subject: [PATCH] Run FBP program in repo instead of copying stucture to tmp The running method was focused in building a run environment before running the project. When DECLARE was created, Soletta Dev-App started creating a building environment wrongly when DECLARE was used with relative PATH. With this patch Soletta Dev-App will run the project directly in the right PATH of the repo, making DECLARE working with Relative paths. Fixes the issue: Devapp wasn't finding the external FBP when using DECLARE in a relative path. --- client/js/controllers/editor.js | 61 +++++++---- scripts/fbp-runner.sh | 8 +- server/routes.js | 180 +++++++++++++++++--------------- server/tools.js | 16 +++ 4 files changed, 158 insertions(+), 107 deletions(-) diff --git a/client/js/controllers/editor.js b/client/js/controllers/editor.js index d845197..efef597 100644 --- a/client/js/controllers/editor.js +++ b/client/js/controllers/editor.js @@ -22,6 +22,7 @@ function ($compile, $scope, $http, $interval, $document, broadcastService, FetchFileFactory, usSpinnerService, svConf) { var isRunningSyntax = false; + var runningFBP; var promiseCheckSyntax; var promiseServiceStatus; var promiseRunViewer; @@ -290,7 +291,7 @@ $http.get('/api/journald', { params: { - "unit_name": "fbp-runner@" + "unit_path": runningFBP, } }).success(function(data) { $scope.RunViewer = data; @@ -302,11 +303,14 @@ $scope.run = function() { if ($scope.isServiceRunning) { //Post stop service - $http.post('/api/fbp/stop').success(function(data) { - if (data == 1) { - alert("FBP Service failed to stop"); - } - }); + $http.post('/api/fbp/stop', + { + params: {"fbp_path": runningFBP} + }).success(function(data) { + if (data == 1) { + alert("FBP Service failed to stop"); + } + }); } else if ($scope.fbpType === true && $scope.buttonSyncDisabled === false) { var fbpCode = editor.getSession().getValue(); var fbpName = $scope.fileName; @@ -315,14 +319,19 @@ if (conf === "none") { conf = null; } + if (!filePath) { + filePath = "/tmp/cached.fbp"; + } $http.post('/api/fbp/run', {params: { "fbp_name": fbpName, + "fbp_path": filePath, "code": fbpCode, "conf": conf } }).success(function(data) { if (data == 0) { + runningFBP = filePath; $scope.openRunDialog(); } else { alert("FBP Failed to run"); @@ -344,11 +353,15 @@ window.onbeforeunload = onBeforeUnload_Handler; function onBeforeUnload_Handler() { if ($scope.isServiceRunning) { - $http.post('/api/fbp/stop').success(function(data) { - if (data == 1) { - alert("FBP Service failed to stop. Process should be stopped manually"); - } - }); + $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"); + } + }); } if ($scope.shouldSave === true) { @@ -366,15 +379,19 @@ if (conf === "none") { conf = null; } + if (!filePath) { + filePath = "/tmp/cached.fbp"; + } $http.get('/api/check/fbp', {params: { + "fbp_path": filePath, "code": fbpCode, "conf": conf } }).success(function(data) { $scope.FBPSyntax = data.trim(); - var errorline = data.match(/fbp_syntax\.fbp:[0-9]+:[0-9]+/g); - var errorDesc = data.split(/fbp_syntax\.fbp:[0-9]+:[0-9]+/g); + var errorline = data.match(/.+\.fbp:[0-9]+:[0-9]+/g); + var errorDesc = data.split(/.+\.fbp:[0-9]+:[0-9]+/g); editor.getSession().removeMarker(markId); editor.getSession().clearAnnotations(); if (errorline) { @@ -651,23 +668,27 @@ }; $scope.getServiceStatus = function () { - $http.get('/api/service/status', - {params: { - "service": "fbp-runner.service" - } + if (runningFBP) { + $http.get('/api/service/status', + { + params: { "fbp_path": runningFBP } }).success(function(data) { - $scope.startServiceStatus(); $scope.ServiceStatus = data.trim(); if ($scope.ServiceStatus.indexOf("active (running)") > -1) { $scope.isServiceRunning = true; } else { $scope.isServiceRunning = false; } - $scope.ServiceStatus = $scope.ServiceStatus.replace(/since.*;/,""); + if (runningFBP) { + $scope.ServiceStatus = $scope.ServiceStatus.replace(/since.*;/,""); + } else { + $scope.ServiceStatus = runningFBP + " - " + $scope.ServiceStatus.replace(/since.*;/,""); + } }).error(function(){ - $scope.startServiceStatus(); $scope.ServiceStatus = "Failed to get service information"; }); + } + $scope.startServiceStatus(); }; $scope.refreshTree = function () { diff --git a/scripts/fbp-runner.sh b/scripts/fbp-runner.sh index 3a0a65a..d924354 100644 --- a/scripts/fbp-runner.sh +++ b/scripts/fbp-runner.sh @@ -20,11 +20,11 @@ export SOL_LOG_PRINT_FUNCTION="journal" if [ $# -eq 3 ]; then export SOL_FLOW_MODULE_RESOLVER_CONFFILE=$3 fi -USER_TMP="$2" -echo "USER_TMP="$USER_TMP -SERVICE="fbp-runner@"$(systemd-escape $USER_TMP) +FBP_PATH="$2" +echo "FBP_PATH="$FBP_PATH +SERVICE="fbp-runner@"$(systemd-escape $FBP_PATH) echo "SERVICE="$SERVICE -SCRIPT="$USER_TMP/fbp_runner.fbp" +SCRIPT="$FBP_PATH" systemctl stop $SERVICE if [ $1 == "start" ]; then syntax=`sol-fbp-runner -c $SCRIPT | grep OK` diff --git a/server/routes.js b/server/routes.js index c240119..7ee6a7d 100644 --- a/server/routes.js +++ b/server/routes.js @@ -25,7 +25,6 @@ require('./configuration.js')(); var jConf = getConfigurationJson(); - var runningFBPName; /* GET home page. */ router.get('/', function(req, res) { @@ -60,29 +59,29 @@ /* Journald Service Status */ router.get('/api/service/status', function(req, res) { if (jConf.journal_access === true) { - var service = req.query.service; - if (!service) { - res.send("Failed to run command on server"); - } var exec = require('child_process').exec; var stdout = ""; - var child = exec(scripts_dir() + 'systemctl-unit.sh ' + service + ' ' + tmp_dir(current_user(req))); - if (!child) { - res.send("Failed to run command on server"); - } - child.stdout.on('data', function(data) { - stdout += data; - }); - child.stderr.on('data', function(data) { - stdout = "Failed to run command on server"; - }); - child.on('close', function(code) { - stdout = stdout.replace(/Active:/, '').trim(); - if (runningFBPName) { - stdout = runningFBPName + " - " + 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); + if (!child) { + res.send("Failed to run command on server"); } - res.send(stdout); - }); + child.stdout.on('data', function(data) { + stdout += data; + }); + child.stderr.on('data', function(data) { + console.log("Err: " + data); + stdout = "Failed to run command on server"; + }); + child.on('close', function(code) { + stdout = stdout.replace(/Active:/, '').trim(); + res.send(stdout); + }); + } else { + res.send("There is no running FBP.") + } } else { res.status(404).send("Unsupported api"); } @@ -122,9 +121,9 @@ var spawn = require('child_process').spawn; var stdout = ""; var error = false; - var unit_name = req.query.unit_name; + var path = req.query.unit_path; var child; - if (!unit_name) { + if (!path) { child = spawn('journalctl', ['-o', 'json-pretty', '-n', '100', '--no-pager']); @@ -146,9 +145,10 @@ } }); } else { + var unit_path = generateHiddenPath(path); var script = scripts_dir() + "/journalctl-unit.sh"; child = spawn(script, - [tmp_dir(current_user(req))]); + [unit_path]); child.on('error', function(err) { error = true; }); @@ -178,8 +178,15 @@ if (!file_path || !file_body) { res.status(400).send("Failed to get file path or its body"); } else { - if(!writeFile(file_path, file_body)) { - res.sendStatus(0); + var hidden_fbp = generateHiddenPath(file_path); + if (hidden_fbp) { + if(!writeFile(file_path, file_body)) { + execOnServer("rm -f " + hidden_fbp, function(returns) { + res.sendStatus(0); + }); + } else { + res.status(400).send("Failed to write file " + file_path.split("/").pop()); + } } else { res.status(400).send("Failed to write file " + file_path.split("/").pop()); } @@ -190,25 +197,21 @@ router.post('/api/fbp/run', function(req, res) { if (jConf.run_fbp_access === true) { var exec = require('child_process').exec; - var name = req.body.params.fbp_name; + var path = req.body.params.fbp_path; var code = req.body.params.code; var conf = req.body.params.conf; - if (!code) { + if (!path || !code) { res.sendStatus(1); } else { var child; var stdout = ""; - var err = writeFile(tmp_dir(current_user(req)) + "fbp_runner.fbp", code); - if (err) { - console.log('Write File error'); - res.sendStatus(1); - } else { - var script = scripts_dir() + "/fbp-runner.sh"; - script = script + ' start ' + tmp_dir(current_user(req)); + var script = scripts_dir() + "/fbp-runner.sh"; + var fbp_path = generateHiddenPath(path); + if (fbp_path) { + script = script + ' start ' + fbp_path; if (conf) { script = script + " " + conf; } - console.log("SCRIPT->" + script); getConfigureFile(current_user(req), conf, function (error) { child = exec("sh " + script); child.stdout.on('data', function(data) { @@ -220,12 +223,11 @@ }); child.on('close', function(code) { console.log('closing code: ' + code); - if (name) { - runningFBPName = name; - } res.sendStatus(code); }); }); + } else { + res.status(404).send("Failed to get FBP file"); } } } else { @@ -236,53 +238,59 @@ //SOL_FLOW_MODULE_RESOLVER_CONFFILE=sol-flow-new.json sol-fbp-runner example.fbp router.get('/api/check/fbp', function(req, res) { var spawn = require('child_process').spawn; + var path = req.query.fbp_path; var code = req.query.code; var conf = req.query.conf; - if (!code) { + if (!path || !code) { res.send("Error: Empty should not being checked!"); } else { var child; var error; var stdout = ""; - var err = writeFile(tmp_dir(current_user(req)) + "fbp_syntax.fbp", code); - if (err) { - console.log('Write File error'); - res.send(err); - } - console.log('Running command sol-fbp-runner -c '+ tmp_dir(current_user(req)) + 'fbp_syntax.fbp'); - if (conf) { - child = spawn("sol-fbp-runner", ['-c', tmp_dir(current_user(req)) + 'fbp_syntax.fbp'], - {env: {SOL_FLOW_MODULE_RESOLVER_CONFFILE: conf}}); - } else { - child = spawn("sol-fbp-runner", ['-c', tmp_dir(current_user(req)) + 'fbp_syntax.fbp']); - } - child.on('error', function(err) { - error = true; - }); - child.stdout.on('data', function(data) { - stdout += "Syntax OK"; - console.log('stdout: ' + data); - }); - child.stderr.on('data', function(data) { - console.log('stderr: ' + data); - if (data) { - if (stdout) { - stdout += data; - } else { - stdout += "\n" + data; - } - } else { - stdout = "Unidentified error."; + var fbp_path = generateHiddenPath (path); + if (fbp_path) { + var err = writeFile(fbp_path, code); + if (err) { + console.log('Write File error'); + res.send(err); } - }); - child.on('close', function(code) { - if (!error) { - console.log('closing code: ' + code); - res.sendStatus(stdout); + console.log('Running command sol-fbp-runner -c '+ fbp_path); + if (conf) { + child = spawn("sol-fbp-runner", ['-c', fbp_path], + {env: {SOL_FLOW_MODULE_RESOLVER_CONFFILE: conf}}); } else { - res.sendStatus("Failed to run command on server"); + child = spawn("sol-fbp-runner", ['-c', fbp_path]); } - }); + child.on('error', function(err) { + error = true; + }); + child.stdout.on('data', function(data) { + stdout += "Syntax OK"; + console.log('stdout: ' + data); + }); + child.stderr.on('data', function(data) { + console.log('stderr: ' + data); + if (data) { + if (stdout) { + stdout += data; + } else { + stdout += "\n" + data; + } + } else { + stdout = "Unidentified error."; + } + }); + child.on('close', function(code) { + if (!error) { + console.log('closing code: ' + code); + res.sendStatus(stdout); + } else { + res.sendStatus("Failed to run command on server"); + } + }); + } else { + res.sendStatus("Invalid FBP file or path."); + } } }); @@ -437,13 +445,19 @@ if (jConf.run_fbp_access === true) { var exec = require('child_process').exec; var child; - var script = scripts_dir() + "/fbp-runner.sh"; - script = script + ' stop ' + tmp_dir(current_user(req)); - child = exec("sh " + script); - child.on('close', function(code) { - console.log('closing code: ' + code); - res.sendStatus(code); - }); + 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); + }); + } } else { res.status(404).send("Unsupported api"); } diff --git a/server/tools.js b/server/tools.js index 619d1bb..8e0a8ba 100644 --- a/server/tools.js +++ b/server/tools.js @@ -78,6 +78,22 @@ module.exports = function () { }; }; + this.generateHiddenPath = function(path) { + var array_path; + var fbp_name; + var fbp_path; + + if (!path) { + return null; + } + + array_path = path.split("/"); + fbp_name = array_path.pop(); + fbp_path = array_path.join("/"); + + return fbp_path + "/." + fbp_name; + } + this.writeFile = function(path, body) { var fs = require('fs'); var err = fs.writeFileSync(path, body);