istic-openstack/server/core/Automating.php

134 lines
3.4 KiB
PHP
Raw Permalink Normal View History

2016-03-23 15:46:57 +01:00
<?php
/**
2016-03-27 19:40:36 +02:00
* File containing the Automating Class.
2016-03-23 15:46:57 +01:00
*
* @version 1.0 Initialisation of this file
* @since 1.0 Core application's file
*
2016-04-26 20:42:31 +02:00
* @author Evan Pisani 'yogg at epsina . com', bhupi
2016-03-23 15:46:57 +01:00
*
*/
2016-05-05 19:16:10 +02:00
require_once("Image.php");
require_once("Network.php");
require_once("Compute.php");
require_once("NetworkLayer3.php");
require_once("CoreInterface.php");
2016-03-23 15:46:57 +01:00
2016-04-27 14:22:59 +02:00
/**
* automating Class of the back-end application
*
* Contains the different function to automate some action
*
*/
class automating implements Core{
2016-04-27 14:22:59 +02:00
/** @var App $compute protected, contains a Core compute object */
2016-04-21 23:57:37 +02:00
protected $compute;
2016-04-27 14:22:59 +02:00
/** @var App $image protected, contains a Core image object */
2016-04-21 23:57:37 +02:00
protected $image;
2016-04-27 14:22:59 +02:00
/** @var App $network protected, contains a Core network object */
2016-04-21 23:57:37 +02:00
protected $network;
2016-04-27 14:22:59 +02:00
/** @var App $networkLayer3 protected, contains a Core networkLayer3 object */
2016-04-21 23:57:37 +02:00
protected $networkLayer3;
2016-04-27 14:22:59 +02:00
/** @var App $app protected, contains the main app object */
2016-03-30 18:30:44 +02:00
protected $app;
2016-03-23 15:46:57 +01:00
/**
2016-04-27 14:22:59 +02:00
* automating class constructor
2016-03-23 15:46:57 +01:00
*
2016-04-27 14:22:59 +02:00
* @param App $app the main app object
2016-03-23 15:46:57 +01:00
*
2016-04-27 14:22:59 +02:00
* @return automating Object
2016-03-23 15:46:57 +01:00
*/
public function __construct($app){
2016-03-30 18:30:44 +02:00
$this->app = $app;
2016-04-21 23:57:37 +02:00
$compute = new Compute($app);
$image = new Image($app);
$network = new Network($app);
$networkLayer3 = new NetworkLayer3($app);
2016-03-23 15:46:57 +01:00
}
/**
* Execute an action
*
* @param String $action name of another function of this class
*
2016-04-27 14:22:59 +02:00
* @return void
2016-03-23 15:46:57 +01:00
*/
public function action($action){
2016-04-26 20:42:31 +02:00
$this->{$action.""}();
2016-03-23 15:46:57 +01:00
}
2016-03-30 18:30:44 +02:00
2016-04-18 17:38:25 +02:00
/**
* create a new server and associate a public ip
*
2016-04-21 23:57:37 +02:00
* @param String $networkId the id of the network where the server will be created
2016-04-18 17:38:25 +02:00
* @param String $imageName name of the new image
* @param String $serverName name ofthe new server
* @param String $flavor kind of server
*
2016-04-27 14:22:59 +02:00
* @return void
2016-04-18 17:38:25 +02:00
*/
2016-04-21 23:57:37 +02:00
private function createPublicServer()
2016-03-30 18:30:44 +02:00
{
2016-04-21 23:57:37 +02:00
$networkId = $this->app->getPostParam('networkId');
2016-03-30 18:30:44 +02:00
$imageName = $this->app->getPostParam('imageName');
$serverName = $this->app->getPostParam('serverName');
$flavor = $this->app->getPostParam('flavor');
2016-03-23 15:46:57 +01:00
2016-04-18 17:38:25 +02:00
if(!isset($imageName)){
$this->app->setOutput("Error", "Incorrect imageName parameter");
}
else if(!isset($serverName)){
$this->app->setOutput("Error", "Incorrect serverName parameter");
}
else if(!isset($flavor)){
$this->app->setOutput("Error", "Incorrect flavor parameter");
}
else{
// Création image
$opt = array();
$opt['name'] = $imageName;
2016-04-21 23:57:37 +02:00
$image->setPostParam('opt', $opt);
$image->action("createImage");
2016-04-18 17:38:25 +02:00
$image = json_decode($this->app->show(), true)["Images"];
2016-03-23 15:46:57 +01:00
2016-04-18 17:38:25 +02:00
// Création server
2016-04-21 23:57:37 +02:00
$compute->setPostParam('name', $serverName);
$compute->setPostParam('imageId', $image['id']);
$compute->setPostParam('flavorId', $flavor);
$compute->action("createServer");
2016-04-18 17:38:25 +02:00
$server = json_decode($this->app->show(), true)["Compute"];
2016-03-27 19:54:38 +02:00
2016-04-21 23:57:37 +02:00
// liste des adresses ip publiques diponibles
$networkLayer3->action("listFloatingIp");
$listFloatingIp = json_decode($App->show(), true)["NetworkLayer3"];
$ip = null;
foreach ($listFloatingIp as $f) {
if(strcmp($f['status'], "DOWN")){
$ip = $f;
}
}
// Si pas d'ip publique disponible on en créé une
if(!isset($ip)){
// Ajout adresse IP public
$optIp = array();
$opt['floatingNetworkId'] = $networkId;
$floatingIp->setPostParam('opt', $optIp);
$networkLayer3->action("createFloatingIp");
$ip = json_decode($App->show(), true)["NetworkLayer3"];
}
// Association de l'ip publique au serveur
/*
* API non diponible pour le moment
*/
2016-03-27 19:54:38 +02:00
2016-04-18 17:38:25 +02:00
}
2016-03-23 15:46:57 +01:00
}
}
?>