diff options
| -rw-r--r-- | server/config.inc.php | 3 | ||||
| -rw-r--r-- | server/core/Identity.php | 20 | ||||
| -rw-r--r-- | server/core/LibOverride/Builder.php | 169 | ||||
| -rw-r--r-- | server/core/LibOverride/OpenStack.php | 43 | ||||
| -rwxr-xr-x | server/core/LibOverride/Test.php | 220 | ||||
| -rw-r--r-- | server/core/Plugin_Api.php | 4 | ||||
| -rwxr-xr-x[-rw-r--r--] | server/index.php | 27 | ||||
| -rwxr-xr-x[-rw-r--r--] | server/init.php | 45 |
8 files changed, 521 insertions, 10 deletions
diff --git a/server/config.inc.php b/server/config.inc.php index 07adcbd..9767c72 100644 --- a/server/config.inc.php +++ b/server/config.inc.php @@ -1,7 +1,10 @@ <?php + date_default_timezone_set('Europe/Paris'); + $config = Array(); $config["modules_enabled"] = ""; + $config["urlAuth"] = "http://148.60.11.31:5000/v3"; ?> diff --git a/server/core/Identity.php b/server/core/Identity.php index 8d1c8b6..343ed15 100644 --- a/server/core/Identity.php +++ b/server/core/Identity.php @@ -1 +1,19 @@ - +<?php + +class identity { + + protected $oidentity; + + public function __construct($ostack, $apiP){ + + $this->oidentity = $ostack->identityV3(); + $this->plugins = $apiP; + + } + + public function genToken(){ + global $Args; + $token = $this->oidentity->generateToken($Args); + return $token; + } +} diff --git a/server/core/LibOverride/Builder.php b/server/core/LibOverride/Builder.php new file mode 100644 index 0000000..30ea9d3 --- /dev/null +++ b/server/core/LibOverride/Builder.php @@ -0,0 +1,169 @@ +<?php + +namespace OpenStack\Common\Service; + +use GuzzleHttp\Client; +use GuzzleHttp\ClientInterface; +use GuzzleHttp\Middleware as GuzzleMiddleware; +use OpenStack\Common\Auth\ServiceUrlResolver; +use OpenStack\Common\Auth\Token; +use OpenStack\Common\Transport\HandlerStack; +use OpenStack\Common\Transport\Middleware; +use OpenStack\Common\Transport\Utils; +use OpenStack\Identity\v3\Service; + +/** + * A Builder for easily creating OpenStack services. + * + * @package OpenStack\Common\Service + */ +class Builder_override extends Builder +{ + private $globalOptions = []; + + /** + * Defaults that will be applied to options if no values are provided by the user. + * + * @var array + */ + private $defaults = ['urlType' => 'publicURL']; + + public function __construct(array $globalOptions = []) + { + $this->globalOptions = $globalOptions; + parent::__construct($globalOptions); + } + + public function getOptions() + { + return $this->globalOptions; + } + + public function setOptions($Options) + { + $this->globalOptions = $Options; + } + + /** + * Internal method which resolves the API and Service classes for a service. + * + * @param string $serviceName The name of the service, e.g. Compute + * @param int $serviceVersion The major version of the service, e.g. 2 + * + * @return array + */ + private function getClasses($serviceName, $serviceVersion) + { + $rootNamespace = sprintf("OpenStack\\%s\\v%d", $serviceName, $serviceVersion); + + return [ + sprintf("%s\\Api", $rootNamespace), + sprintf("%s\\Service", $rootNamespace), + ]; + } + + /** + * This method will return an OpenStack service ready fully built and ready for use. There is + * some initial setup that may prohibit users from directly instantiating the service class + * directly - this setup includes the configuration of the HTTP client's base URL, and the + * attachment of an authentication handler. + * + * @param $serviceName The name of the service as it appears in the OpenStack\* namespace + * @param $serviceVersion The major version of the service + * @param array $serviceOptions The service-specific options to use + * + * @return \OpenStack\Common\Service\ServiceInterface + * + * @throws \Exception + */ + public function createService($serviceName, $serviceVersion, array $serviceOptions = []) + { + $options = $this->mergeOptions($serviceOptions); + + $this->stockIdentityService($options); + $this->stockAuthHandler($options); + $this->stockHttpClient($options, $serviceName); + + list($apiClass, $serviceClass) = $this->getClasses($serviceName, $serviceVersion); + + return new $serviceClass($options['httpClient'], new $apiClass()); + } + + private function stockHttpClient(array &$options, $serviceName) + { + if (!isset($options['httpClient']) || !($options['httpClient'] instanceof ClientInterface)) { + if (strcasecmp($serviceName, 'identity') === 0) { + $baseUrl = $options['authUrl']; + $stack = $this->getStack($options['authHandler']); + } else { + list($token, $baseUrl) = $options['identityService']->authenticate($options); + $stack = $this->getStack($options['authHandler'], $token); + } + + $this->addDebugMiddleware($options, $stack); + + $options['httpClient'] = $this->httpClient($baseUrl, $stack); + } + } + + /** + * @codeCoverageIgnore + */ + private function addDebugMiddleware(array $options, HandlerStack &$stack) + { + if (!empty($options['debugLog']) + && !empty($options['logger']) + && !empty($options['messageFormatter']) + ) { + $stack->push(GuzzleMiddleware::log($options['logger'], $options['messageFormatter'])); + } + } + + private function stockIdentityService(array &$options) + { + if (!isset($options['identityService'])) { + $httpClient = $this->httpClient($options['authUrl'], HandlerStack::create()); + $options['identityService'] = Service::factory($httpClient); + } + } + + /** + * @param array $options + * @codeCoverageIgnore + */ + private function stockAuthHandler(array &$options) + { + if (!isset($options['authHandler'])) { + $options['authHandler'] = function () use ($options) { + return $options['identityService']->generateToken($options); + }; + } + } + + private function getStack(callable $authHandler, Token $token = null) + { + $stack = HandlerStack::create(); + $stack->push(Middleware::authHandler($authHandler, $token)); + return $stack; + } + + private function httpClient($baseUrl, HandlerStack $stack) + { + return new Client([ + 'base_uri' => Utils::normalizeUrl($baseUrl), + 'handler' => $stack, + ]); + } + + private function mergeOptions(array $serviceOptions) + { + $options = array_merge($this->defaults, $this->globalOptions, $serviceOptions); + + if (!isset($options['authUrl'])) { + throw new \InvalidArgumentException('"authUrl" is a required option'); + } + + return $options; + } +} + diff --git a/server/core/LibOverride/OpenStack.php b/server/core/LibOverride/OpenStack.php new file mode 100644 index 0000000..2b3897a --- /dev/null +++ b/server/core/LibOverride/OpenStack.php @@ -0,0 +1,43 @@ +<?php +namespace OpenStack; + +#use OpenStack\Common\Service\Builder; +use OpenStack\Common\Service\Builder_override; +/** + * This class is the primary entry point for working with the SDK. It allows for the easy creation + * of OpenStack services. + * + * @package OpenStack + */ +class OpenStack_override extends OpenStack +{ + /** + * @param array $options User-defined options + * + * $options['username'] = (string) Your OpenStack username [REQUIRED] + * ['password'] = (string) Your OpenStack password [REQUIRED] + * ['tenantId'] = (string) Your tenant ID [REQUIRED if tenantName omitted] + * ['tenantName'] = (string) Your tenant name [REQUIRED if tenantId omitted] + * ['authUrl'] = (string) The Keystone URL [REQUIRED] + * ['debug'] = (bool) Whether to enable HTTP logging [OPTIONAL] + */ + public function __construct(array $options = [], Builder $builder = null) + { + $this->builder = $builder ?: new Builder_override($options); + parent::__construct($options, $this->builder); + } + + public function getBuilderOptions() + { + + return $this->builder->getOptions(); + + } + + public function setBuilderOptions($options) + { + + $this->builder->setOptions($options); + + } +} diff --git a/server/core/LibOverride/Test.php b/server/core/LibOverride/Test.php new file mode 100755 index 0000000..91ea84d --- /dev/null +++ b/server/core/LibOverride/Test.php @@ -0,0 +1,220 @@ +<?php + +use GuzzleHttp\Client; +use OpenStack\Common\Transport\HandlerStack; +use OpenStack\Common\Transport\Middleware; +use OpenStack\Identity\v3\Service; +use OpenStack\Identity\v3\Api; +use OpenStack\Common\Auth\Token; +use OpenStack\Common\Transport\Utils; +use OpenStack\Identity\v3\Models; + +class genTokenOptions +{ + private $optionsGlobal; + + private $stack; + private $backup = []; + private $httpClient; + + public function __construct($options){ + + $this->stack = HandlerStack::create(); + + $httpClient = new Client([ + 'base_uri' => Utils::normalizeUrl($options['authUrl']), + 'handler' => $this->stack, + ]); + + $this->httpClient = $httpClient; + + $options['identityService'] = Service::factory($httpClient); + + $options['authHandler'] = function () use ($options) { + return $options['identityService']->generateToken($options); + }; + + $this->optionsGlobal['Common'] = $options; + } + + /** + * @codeCoverageIgnore + */ + private function addDebugMiddleware(array $options, HandlerStack &$stack) + { + if (!empty($options['debugLog']) + && !empty($options['logger']) + && !empty($options['messageFormatter']) + ) { + $stack->push(GuzzleMiddleware::log($options['logger'], $options['messageFormatter'])); + } + } + + public function genComputeToken(){ + $options = $this->optionsGlobal['Common']; + $options['catalogName'] = 'nova'; + $options['catalogType'] = 'compute'; + $options['region'] = 'RegionOne'; + + list($token, $baseUrl) = $options['identityService']->authenticate($options); + //var_dump($token); + //$stack = HandlerStack::create(); + $this->stack->push(Middleware::authHandler($options['authHandler'], $token)); + + $this->addDebugMiddleware($options, $this->stack); + + $options['httpClient'] = new Client([ + 'base_uri' => Utils::normalizeUrl($baseUrl), + 'handler' => $this->stack, + ]); + $this->backup['Compute'] = array('token' => $this->serializeToken($token), 'baseUrl' => $baseUrl ); + + $this->optionsGlobal['Compute'] = $options; + } + + public function loadComputeBackup($opt){ + + $options = $this->optionsGlobal['Common']; + $options['catalogName'] = 'nova'; + $options['catalogType'] = 'compute'; + $options['region'] = 'RegionOne'; + + //list($token, $baseUrl) = $options['identityService']->authenticate($options); + $this->backup['Compute'] = unserialize($opt); + //var_dump($this->backup['Compute']); + $token = $this->unserializeToken($this->backup['Compute']['token']); + $baseUrl = $this->backup['Compute']['baseUrl']; + + //$stack = HandlerStack::create(); + + $this->stack->push(Middleware::authHandler($options['authHandler'], $token)); + + $this->addDebugMiddleware($options, $this->stack); + + $options['httpClient'] = new Client([ + 'base_uri' => Utils::normalizeUrl($baseUrl), + 'handler' => $this->stack, + ]); + $this->backup['Compute'] = array('token' => $this->serializeToken($token), 'baseUrl' => $baseUrl ); + $this->optionsGlobal['Compute'] = $options; + } + + public function getBackup($service){ + return serialize($this->backup[$service]); + } + + public function getOptionsCompute(){ + return $this->optionsGlobal['Compute']; + } + + private function serializeToken($token){ + $tokenSerialized = []; + $tokenSerialized["methods"] = serialize($token->methods); + $tokenSerialized["roles"] = []; + //var_dump($token->roles); + foreach($token->roles as $role){ + $tokenSerialized["roles"][serialize($role->name)]["links"] = serialize($role->links); + $tokenSerialized["roles"][serialize($role->name)]["id"] = serialize($role->id); + } + $tokenSerialized["expires"] = serialize($token->expires); + $tokenSerialized["project"]["domainId"] = serialize($token->project->domainId); + $tokenSerialized["project"]["parentId"] = serialize($token->project->parentId); + $tokenSerialized["project"]["enabled"] = serialize($token->project->enabled); + $tokenSerialized["project"]["description"] = serialize($token->project->description); + $tokenSerialized["project"]["id"] = serialize($token->project->id); + $tokenSerialized["project"]["links"] = serialize($token->project->links); + $tokenSerialized["project"]["name"] = serialize($token->project->name); + foreach($token->catalog->services as $service){ + $tokenSerialized["catalog"][serialize($service->id)]["name"] = serialize($service->name); + $tokenSerialized["catalog"][serialize($service->id)]["description"] = serialize($service->description); + $tokenSerialized["catalog"][serialize($service->id)]["type"] = serialize($service->type); + foreach($service->endpoints as $end){ + $tokenSerialized["catalog"][serialize($service->id)]["endpoints"][serialize($end->id)]["interface"] = serialize($end->interface); + $tokenSerialized["catalog"][serialize($service->id)]["endpoints"][serialize($end->id)]["name"] = serialize($end->name); + $tokenSerialized["catalog"][serialize($service->id)]["endpoints"][serialize($end->id)]["serviceId"] = serialize($end->serviceId); + $tokenSerialized["catalog"][serialize($service->id)]["endpoints"][serialize($end->id)]["region"] = serialize($end->region); + $tokenSerialized["catalog"][serialize($service->id)]["endpoints"][serialize($end->id)]["links"] = serialize($end->links); + $tokenSerialized["catalog"][serialize($service->id)]["endpoints"][serialize($end->id)]["url"] = serialize($end->url); + } + $tokenSerialized["roles"][serialize($service->id)]["links"] = serialize($service->links); + } + $tokenSerialized["extras"] = serialize($token->extras); + $tokenSerialized["user"]["domainId"] = serialize($token->user->domainId); + $tokenSerialized["user"]["defaultProjectId"] = serialize($token->user->defaultProjectId); + $tokenSerialized["user"]["id"] = serialize($token->user->id); + $tokenSerialized["user"]["email"] = serialize($token->user->email); + $tokenSerialized["user"]["enabled"] = serialize($token->user->enabled); + $tokenSerialized["user"]["description"] = serialize($token->user->description); + $tokenSerialized["user"]["links"] = serialize($token->user->links); + $tokenSerialized["user"]["name"] = serialize($token->user->name); + $tokenSerialized["issued"] = serialize($token->issued); + $tokenSerialized["id"] = serialize($token->id); + + return $tokenSerialized; + } + + private function unserializeToken($tokenSerialized){ + $api = new Api(); + $token = new Models\Token($this->httpClient, $api); + $token->methods = unserialize($tokenSerialized["methods"]); + $token->roles = []; + foreach($tokenSerialized["roles"] as $key => $role){ + $tmp = new Models\Role($this->httpClient, $api); + + $tmp->name = unserialize($key); + $tmp->links = unserialize($role["links"]); + $tmp->id = unserialize($role["id"]); + + $token->roles[] = $tmp; + } + + $token->expires = unserialize($tokenSerialized["expires"]); + $token->project = new Models\Project($this->httpClient, $api); + $token->project->domainId = unserialize($tokenSerialized["project"]["domainId"]); + $token->project->parentId = unserialize($tokenSerialized["project"]["parentId"]); + $token->project->enabled = unserialize($tokenSerialized["project"]["enabled"]); + $token->project->description = unserialize($tokenSerialized["project"]["description"]); + $token->project->id = unserialize($tokenSerialized["project"]["id"]); + $token->project->links = unserialize($tokenSerialized["project"]["links"]); + $token->project->name = unserialize($tokenSerialized["project"]["name"]); + + $token->catalog = []; + foreach($tokenSerialized["catalog"] as $key => $service){ + $tmp = new Models\Service($this->httpClient, $api); + + $tmp->id = unserialize($key); + $tmp->name = unserialize($service["name"]); + $tmp->description = unserialize($service["description"]); + $tmp->type = unserialize($service["type"]); + $tmp->endpoints = []; + foreach($service["endpoints"] as $key => $end){ + $tmpEnd = new Models\Endpoint($this->httpClient, $api); + $tmpEnd->id = unserialize($key); + $tmpEnd->interface = unserialize($end["interface"]); + $tmpEnd->name = unserialize($end["name"]); + $tmpEnd->serviceId = unserialize($end["serviceId"]); + $tmpEnd->region = unserialize($end["region"]); + $tmpEnd->links = unserialize($end["links"]); + $tmpEnd->url = unserialize($end["url"]); + $tmp->endpoints[] = $tmpEnd; + } + $tmp->links = unserialize($service["links"]); + $token->catalog[] = $tmp; + } + + $token->extras = unserialize($tokenSerialized["extras"]); + $token->user = new Models\User($this->httpClient, $api); + $token->user->domainId = unserialize($tokenSerialized["user"]["domainId"]); + $token->user->defaultProjectId = unserialize($tokenSerialized["user"]["defaultProjectId"]); + $token->user->id = unserialize($tokenSerialized["user"]["id"]); + $token->user->email = unserialize($tokenSerialized["user"]["email"]); + $token->user->enabled = unserialize($tokenSerialized["user"]["enabled"]); + $token->user->links = unserialize($tokenSerialized["user"]["links"]); + $token->user->name = unserialize($tokenSerialized["user"]["name"]); + $token->user->description = unserialize($tokenSerialized["user"]["description"]); + $token->issued = unserialize($tokenSerialized["issued"]); + $token->id = unserialize($tokenSerialized["id"]); + + return $token; + } +} diff --git a/server/core/Plugin_Api.php b/server/core/Plugin_Api.php index c2a9041..20ffd0c 100644 --- a/server/core/Plugin_Api.php +++ b/server/core/Plugin_Api.php @@ -1,9 +1,9 @@ <?php //Init plugin directory -if (!defined('PLUGINS_DIR')) { +/*if (!defined('PLUGINS_DIR')) { define('PLUGINS_DIR', INSTALL_PATH . 'plugins/'); -} +}*/ class plugin_api{ diff --git a/server/index.php b/server/index.php index ca84641..c99e8bc 100644..100755 --- a/server/index.php +++ b/server/index.php @@ -3,7 +3,28 @@ include_once("config.inc.php"); include_once("init.php"); - $task = $_POST["task"]; - $action = $_POST["action"]; +// $task = $_POST["task"]; +// $action = $_POST["action"]; - + + //$id = new identity($openstack_api, $pluginApi); + +// var_dump($id->genToken()); + $compute = $openstack_api->computeV2($array); + $servers = $compute->listServers(true); + //var_dump($servers); + foreach($servers as $server){ + // echo $server->id." !!!!!!!!! "; + } + + $tmp = new genTokenOptions($Args); + $tmp->loadComputeBackup($computBack); + $array = $tmp->getOptionsCompute(); + + $openstackTest = new OpenStack\OpenStack([]); + $computeTest = $openstackTest->computeV2($array); + $serversTest = $computeTest->listServers(true); + foreach($serversTest as $server){ + // echo $server->id." %%%%%% "; + } +// var_dump($openstack_api->getBuilderOptions()); diff --git a/server/init.php b/server/init.php index ff7f90b..3c5f848 100644..100755 --- a/server/init.php +++ b/server/init.php @@ -2,7 +2,11 @@ include_once("config.inc.php"); include_once("core/Plugin_Api.php"); require "vendor/autoload.php"; - + include_once("core/LibOverride/Builder.php"); + include_once("core/LibOverride/OpenStack.php"); + include_once("core/LibOverride/Test.php"); + include_once("core/Identity.php"); + //traitement requete, recuperation data if(isset($_POST["key"])){ //recuperation des donnes sauvegardes @@ -27,11 +31,44 @@ "name" => "Default") ) ), - "authUrl" => $urlAuth + "authUrl" => $config["urlAuth"] + ); + } else { + $user = "admin"; + $password = "ae5or6cn"; + $project = "admin"; + + $Args = Array( + "user" => Array( + "name" => $user, + "password" => $password, + "domain" => Array( + "name" => "Default") + ), + "scope" => Array( + "project" => Array( + "name" => $project, + "domain" => Array( + "name" => "Default") + ) + ), + "authUrl" => $config["urlAuth"] ); } - $openstack_api = new OpenStack\OpenStack($Args); - $pluginApi = plugin_api::getInstance(); + + //$openstack_api = new OpenStack\OpenStack($Args); + //$id = new identity($openstack_api, $pluginApi); + + //$token = $id->genToken(); + + $tmp = new genTokenOptions($Args); + $tmp->genComputeToken(); + $array = $tmp->getOptionsCompute(); + $openstack_api = new OpenStack\OpenStack([]); + + $computBack = $tmp->getBackup("Compute"); + //file_put_contents("token", serialize($tmp)); + ?> |
