Skip to content

Commit

Permalink
Api: implement user-based WSDL cache
Browse files Browse the repository at this point in the history
This introduces a new dependency, php-posix.

fixes #3
  • Loading branch information
Thomas-Gelf committed Jul 2, 2017
1 parent 78eced3 commit a9a2425
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
9 changes: 5 additions & 4 deletions doc/01-Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ Requirements
* Icinga Web 2 (>= 2.4.1)
* PHP (>= 5.3 or 7.x)
* php-soap
* php-posix

Once you got Icinga Web 2 up and running, all required dependencies should
already be there. All, but `php-soap`. It is available on all major Linux
distributions and can be installed with your package manager (yum, apt...).
Same goes also for non-Linux systems. Please do not forget to restart your
web server service afterwards.
already be there. All, but `php-soap` and `php-posix`. They are available on
all major Linux distributions and can be installed with your package manager
(yum, apt...). Same goes also for non-Linux systems. Please do not forget to
restart your web server service afterwards.

Installation from .tar.gz
-------------------------
Expand Down
64 changes: 56 additions & 8 deletions library/Vsphere/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace Icinga\Module\Vsphere;

use Icinga\Application\Benchmark;
use Icinga\Module\Vsphere\ManagedObject\FullTraversal;
use Icinga\Exception\ConfigurationError;
use Icinga\Module\Vsphere\ManagedObject\TraversalHelper;
use SoapVar;

Expand All @@ -29,6 +28,9 @@ class Api
/** @var string */
private $wsdlDir;

/** @var string */
private $cacheDir;

/** @var mixed */
private $serviceInstance;

Expand Down Expand Up @@ -155,12 +157,7 @@ protected function soapClient()
protected function wsdlDir()
{
if ($this->wsdlDir === null) {
$dir = '/tmp/vmwareWsdl';
if (! is_dir($dir)) {
mkdir($dir);
}

$this->wsdlDir = $dir;
$this->wsdlDir = $this->cacheDir();
}

return $this->wsdlDir;
Expand Down Expand Up @@ -295,6 +292,57 @@ public function idLookup()
return $this->idLookup;
}

protected function cacheDir()
{
if ($this->cacheDir === null) {
$user = $this->getCurrentUsername();
$dirname = sprintf(
'%s/%s-%s',
sys_get_temp_dir(),
'iwebVsphere',
$user
);

if (file_exists($dirname)) {
if ($this->uidToName(fileowner($dirname)) !== $user) {
throw new ConfigurationError(
'%s exists, but does not belong to %s',
$dirname,
$user
);
}
} else {
if (! mkdir($dirname, 0700)) {
throw new ConfigurationError(
'Could not create %s',
$dirname
);
}
}

$this->cacheDir = $dirname;
}

return $this->cacheDir;
}

protected function uidToName($uid)
{
$info = posix_getpwuid($uid);
return $info['name'];
}

protected function getCurrentUsername()
{
if (function_exists('posix_geteuid')) {
return $this->uidToName(posix_geteuid());
} else {
throw new ConfigurationError(
'POSIX methods not available, is php-posix installed and enabled?'
);
}
}

/**
* Just a helper method
*
Expand Down

0 comments on commit a9a2425

Please sign in to comment.