Skip to content
Snippets Groups Projects
Commit b79c55cc authored by anxietypb's avatar anxietypb
Browse files

Initial commit

parents
Branches
No related tags found
No related merge requests found
Showing
with 622 additions and 0 deletions
# virl-scheduler
\ No newline at end of file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /virl-scheduler/api/index.php [L,QSA]
<?php
$pdo = new PDO('sqlite:db.sqlite3');
$time = time();
echo "Cronjob<br>";
echo "Current time: ".$time."<br>";
$query = $pdo->prepare('SELECT * FROM simulations WHERE timestamp+repeat_interval * repeat_count < ? AND (status="scheduled" OR status="repeating");');
$query->execute(array($time));
//if this has any rows, we got sims that should be started now.
$sims = $query->fetchAll();
foreach ($sims as $sim){
$url = "192.168.76.210:19399/simengine/rest/launch";
echo "Attempting to start simulation called ".$sim["sessionname"]."<br>";
if ($sim["status"] == "scheduled")
$query = $pdo->prepare("UPDATE simulations SET status = 'Done' WHERE id = ?");
elseif($sim["status"] == "repeating")
$query = $pdo->prepare("UPDATE simulations SET repeat_count = repeat_count+1 WHERE id = ?");
$query->execute(array($sim["id"]));
$headers = array("authorization: Basic ".$sim["token"],"Content-type: text/xml;charset=\"utf-8\"");
var_dump($headers);
echo "<br><br>";
$content = $sim["topo_xml"];
$url.="?session=".$sim["sessionname"];
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS,$content);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output=curl_exec($ch);
var_dump($output);
$curlinfo = curl_getinfo ($ch);
echo "<br><br>";
var_dump($curlinfo);
$response = json_decode($output,true);
$return = array("response"=>$response,"httpinfo"=>$curlinfo);
curl_close($ch);
}
\ No newline at end of file
<?php
error_reporting(0);
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
$app = new \Slim\App;
$container = $app->getContainer();
$container['db'] = function ($c) {
$db = $c['settings']['db'];
$pdo = new PDO('sqlite:db.sqlite3');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$pdo-> exec("CREATE TABLE IF NOT EXISTS `simulations` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`timestamp` INTEGER,
`status` VARCHAR NOT NULL DEFAULT 'scheduled',
`token` VARCHAR NOT NULL,
`username` VARCHAR NOT NULL,
`topo_xml` TEXT NOT NULL DEFAULT '',
`sessionname` VARCHAR,
`repeat_interval` INTEGER,
`repeat_count` INTEGER NOT NULL DEFAULT 0
);");
return $pdo;
};
$container['errorHandler'] = function ($container) {
return function ($request, $response, $exception) use ($container) {
return $container['response']->withStatus(500)
->withHeader('Content-Type', 'text/html')
->withJson(["statusText"=>$exception->getMessage()]);
};
};
$app->get('/plannedsims/{id}', function (Request $request, Response $response) {
$id = $request->getAttribute('id');
$query = $this->db->prepare('SELECT * FROM simulations WHERE id = ? LIMIT 1;');
$array = array($id);
$query->execute($array);
$result = $query->fetch();
if ($result)
return $response->withJson();
});
$app->delete('/plannedsims/{id}', function (Request $request, Response $response) {
$id = $request->getAttribute('id');
$query = $this->db->prepare('DELETE FROM simulations WHERE id = ?;');
$array = array($id);
$query->execute($array);
return $response->withJson(array("statusText"=>"Simulation mit id ".$id." erfolgreich gelöscht."));
});
$app->get('/plannedsims', function (Request $request, Response $response ) {
$data=$this->db->query("SELECT * FROM simulations;")->fetchAll();
$newResponse = $response->withJson($data);
return $newResponse;
});
$app->post('/plannedsims/new', function (Request $request, Response $response) {
$params = $request->getQueryParams();
$sessionName = $params["session"];
$timestamp = $params["timestamp"];
$token = $params["token"];
$username = $params["username"];
$repeat = $params["repeat"];
switch ($repeat){
case "daily":
$repeat = strtotime('+1 day',0);
break;
case "weekly":
$repeat = strtotime('1 week',0);
break;
default:
$repeat = 0;
break;
}
if (!is_numeric($repeat) && $repeat <=0)
$repeat = null;
$XML = $request->getBody()->getContents();
if ($repeat != 0)
$sth = $this->db->prepare('INSERT INTO simulations (timestamp, topo_xml,sessionname,token,username,repeat_interval,repeat_count,status) VALUES (?, ?, ?,?,?,?,0,"repeating");');
else
$sth = $this->db->prepare('INSERT INTO simulations (timestamp, topo_xml,sessionname,token,username,repeat_interval,repeat_count) VALUES (?, ?, ?,?,?,?,0);');
$sth->execute([intval($timestamp),$XML,$sessionName,$token,$username,$repeat]);
return $response->withJson(array("statusText"=>"Simulation erfolgreich hinzugefügt."));
});
$app->run();
\ No newline at end of file
angular.module('topologyManager').controller('ApplicationController', function($scope,VIRLRestangular,CustomSimRestangular) {
console.log("Application Controller initialized");
$scope.credentials = {username:"",password:"",token:""};
$scope.generateToken = function(){
$scope.credentials.token = btoa($scope.credentials.username+":"+$scope.credentials.password);
VIRLRestangular.setDefaultHeaders({authorization: "Basic ".concat($scope.credentials.token)});
console.log($scope.credentials.token);
};
$scope.verifyCredentials = function() {
VIRLRestangular.one('/simengine/rest/test').get() // GET: /users
.then(function(response) {
$scope.addAlert("Valid credentials","success");
}, function(response) {
console.log(response);
});
};
$scope.alerts = [
];
$scope.addAlert = function(message,alertType) {
$scope.alerts.push({msg: message,type:alertType});
};
$scope.closeAlert = function(index) {
$scope.alerts.splice(index, 1);
};
});
angular.module('topologyManager').controller('IndexCtrl', function($scope,VIRLRestangular,CustomSimRestangular,$interval) {
console.log("IndexCtrl initialized");
$scope.repeat = "none";
$scope.plannedSims = [];
$scope.topology = {sessionName: "",filecontent: "",repeat:"none",datetime: new Date()};
$scope.removePlannedSim = function(id){
console.log(id);
CustomSimRestangular.one('plannedsims',id).remove();
}
$scope.stopSim = function(key){
console.log(key);
VIRLRestangular.one('/simengine/rest/stop',key).get().then(function(response){
console.log(response);
});
}
$scope.updater = function(){
stop = $interval(function() {
$scope.getScheduledSims();
$scope.getRunningSims();
}, 1000);
};
$scope.updater();
$scope.getScheduledSims = function (){
CustomSimRestangular.all("plannedsims").getList().then(function(response){
// console.log(response);
$scope.plannedSims = response;
});
};
$scope.getRunningSims = function(){
VIRLRestangular.one('/simengine/rest/list').get().then(function(response){
// console.log(response);
$scope.runningSims = response.data.simulations;
});
};
$scope.send = function () {
var topology = $scope.topology;
var unix_timestamp = new moment(topology.datetime).unix();
CustomSimRestangular.one('plannedsims/new').customPOST(topology.filecontent,undefined,{session:topology.sessionName,timestamp:unix_timestamp,token:$scope.credentials.token,username:$scope.credentials.username,repeat:$scope.topology.repeat},
{ 'Content-Type': "text/xml;charset=UTF-8"}
)
.then(function(response) {
// returns a list of users
$scope.addAlert(response.statusText,"success");
}, function(response) {
console.log(response);
$scope.addAlert(response.statusText,"danger");
});
};
$scope.popup1 = {
opened: false
};
$scope.dateOptions = {
dateDisabled: false,
formatYear: 'yy',
maxDate: new Date(2020, 5, 22),
minDate: new Date(),
startingDay: 1
};
$scope.openDatePicker = function() {
console.log("h");
$scope.popup1.opened = true;
};
});
\ No newline at end of file
angular.module('topologyManager', ['ngRoute','restangular','ui.bootstrap'])
.config(function($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider
.when('/', {
templateUrl: 'partials/index.html',
controller: 'IndexCtrl',
})
_.contains = _.includes;
})
.factory('VIRLRestangular', function(Restangular) {
return Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer.setBaseUrl('http://192.168.76.210:19399');
RestangularConfigurer.setFullResponse(true);
});
})
.factory('CustomSimRestangular', function(Restangular) {
return Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer.setBaseUrl('/virl-scheduler/api/');
});
})
{
"require": {
"slim/slim": "^3.0"
}
}
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "6f5d3d05d70b58db9144bb1ca70c8698",
"content-hash": "3b0766dbcef4dfb1a4a2012fef8611d8",
"packages": [
{
"name": "container-interop/container-interop",
"version": "1.1.0",
"source": {
"type": "git",
"url": "https://github.com/container-interop/container-interop.git",
"reference": "fc08354828f8fd3245f77a66b9e23a6bca48297e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/container-interop/container-interop/zipball/fc08354828f8fd3245f77a66b9e23a6bca48297e",
"reference": "fc08354828f8fd3245f77a66b9e23a6bca48297e",
"shasum": ""
},
"type": "library",
"autoload": {
"psr-4": {
"Interop\\Container\\": "src/Interop/Container/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "Promoting the interoperability of container objects (DIC, SL, etc.)",
"time": "2014-12-30 15:22:37"
},
{
"name": "nikic/fast-route",
"version": "v1.0.1",
"source": {
"type": "git",
"url": "https://github.com/nikic/FastRoute.git",
"reference": "8ea928195fa9b907f0d6e48312d323c1a13cc2af"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/nikic/FastRoute/zipball/8ea928195fa9b907f0d6e48312d323c1a13cc2af",
"reference": "8ea928195fa9b907f0d6e48312d323c1a13cc2af",
"shasum": ""
},
"require": {
"php": ">=5.4.0"
},
"type": "library",
"autoload": {
"psr-4": {
"FastRoute\\": "src/"
},
"files": [
"src/functions.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Nikita Popov",
"email": "nikic@php.net"
}
],
"description": "Fast request router for PHP",
"keywords": [
"router",
"routing"
],
"time": "2016-06-12 19:08:51"
},
{
"name": "pimple/pimple",
"version": "v3.0.2",
"source": {
"type": "git",
"url": "https://github.com/silexphp/Pimple.git",
"reference": "a30f7d6e57565a2e1a316e1baf2a483f788b258a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/silexphp/Pimple/zipball/a30f7d6e57565a2e1a316e1baf2a483f788b258a",
"reference": "a30f7d6e57565a2e1a316e1baf2a483f788b258a",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.0.x-dev"
}
},
"autoload": {
"psr-0": {
"Pimple": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
}
],
"description": "Pimple, a simple Dependency Injection Container",
"homepage": "http://pimple.sensiolabs.org",
"keywords": [
"container",
"dependency injection"
],
"time": "2015-09-11 15:10:35"
},
{
"name": "psr/http-message",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/php-fig/http-message.git",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Message\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for HTTP messages",
"homepage": "https://github.com/php-fig/http-message",
"keywords": [
"http",
"http-message",
"psr",
"psr-7",
"request",
"response"
],
"time": "2016-08-06 14:39:51"
},
{
"name": "slim/slim",
"version": "3.5.0",
"source": {
"type": "git",
"url": "https://github.com/slimphp/Slim.git",
"reference": "184352bc1913d7ba552ab4131d62f4730ddb0893"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/slimphp/Slim/zipball/184352bc1913d7ba552ab4131d62f4730ddb0893",
"reference": "184352bc1913d7ba552ab4131d62f4730ddb0893",
"shasum": ""
},
"require": {
"container-interop/container-interop": "^1.1",
"nikic/fast-route": "^1.0",
"php": ">=5.5.0",
"pimple/pimple": "^3.0",
"psr/http-message": "^1.0"
},
"provide": {
"psr/http-message-implementation": "1.0"
},
"require-dev": {
"phpunit/phpunit": "^4.0",
"squizlabs/php_codesniffer": "^2.5"
},
"type": "library",
"autoload": {
"psr-4": {
"Slim\\": "Slim"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Rob Allen",
"email": "rob@akrabat.com",
"homepage": "http://akrabat.com"
},
{
"name": "Josh Lockhart",
"email": "hello@joshlockhart.com",
"homepage": "https://joshlockhart.com"
},
{
"name": "Gabriel Manricks",
"email": "gmanricks@me.com",
"homepage": "http://gabrielmanricks.com"
},
{
"name": "Andrew Smith",
"email": "a.smith@silentworks.co.uk",
"homepage": "http://silentworks.co.uk"
}
],
"description": "Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs",
"homepage": "http://slimframework.com",
"keywords": [
"api",
"framework",
"micro",
"router"
],
"time": "2016-07-26 15:12:13"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": []
}
This diff is collapsed.
This diff is collapsed.
File added
This diff is collapsed.
File added
File added
File added
<html lang="en" ng-app="topologyManager" >
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Angular Material style sheet -->
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap-material-design.min.css">
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Roboto:300,400,500,700">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<base href="/virl-scheduler/">
</head>
<style>
.navbar .btn {
margin:0;
}
.alerts {
position:absolute;
right:0px;
top:50px;
max-width:500px;
}
</style>
<body ng-cloak ng-controller="ApplicationController">
<ng-include
src="'partials/navbar.html'">
</ng-include>
<div class="alerts">
<div uib-alert ng-repeat="alert in alerts" ng-class="'alert-' + (alert.type || 'warning')" close="closeAlert($index)">{{alert.msg}}</div>
</div>
<div ng-view >
</div>
<!-- Angular Material requires Angular.js Libraries -->
<script src="js/angular.min.js"></script>
<script src="js/angular-route.js"></script>
<script src="js/lodash.js"></script>
<script src="js/restangular.min.js"></script>
<script src="js/moment.js"></script>
<script src="js/ui-bootstrap-tpls-2.2.0.min.js"></script>
<!-- Angular Material Library -->
<!-- Your application bootstrap -->
<script src="app/topologyManager.js"></script>
<script src="app/index/controllers/ApplicationController.js"></script>
<script src="app/index/controllers/IndexCtrl.js"></script>
</body>
</html>
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment