Skip to content

Commit 6cf5db7

Browse files
author
Jean-François Hivert
committed
First release
0 parents  commit 6cf5db7

42 files changed

Lines changed: 4441 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# PHP-CLI SHELL for PATCHMANAGER
2+
3+
This repository is the addon for PHP-CLI SHELL about PATCHMANAGER service.
4+
You have to use base PHP-CLI SHELL project that is here: https://github.com/cloudwatt/php-cli-shell_base
5+
6+
7+
# PRE-REQUIS
8+
9+
#### PATCHMANAGER
10+
* Import profiles which are in ressources/dcim
11+
* formats: ressources/dcim/formats
12+
* Reports: ressources/dcim/reports
13+
* Searches: ressources/dcim/searches
14+
15+
16+
# INSTALLATION
17+
18+
#### APT PHP
19+
__*https://launchpad.net/~ondrej/+archive/ubuntu/php*__
20+
* add-apt-repository ppa:ondrej/php
21+
* apt-get update
22+
* apt install php7.1-cli php7.1-mbstring php7.1-readline php7.1-soap
23+
__Do not forget to install php7.1-soap__
24+
25+
#### REPOSITORIES
26+
* git clone https://github.com/cloudwatt/php-cli-shell_base
27+
* git checkout tags/v1.0
28+
* git clone https://github.com/cloudwatt/php-cli-shell_patchmanager
29+
* git checkout tags/v1.0
30+
* Merge these two repositories
31+
32+
#### CONFIGURATION FILE
33+
* mv config.json.example config.json
34+
* vim config.json
35+
* servers field contains all PatchManager server addresses which must be identified by custom key [PM_SERVER_KEY]
36+
__server key must be unique and you will use it on next steps. You have an example in config file__
37+
* userAttrs field contains all custom attributes which must be created on your PatchManager
38+
__If you have a serial number custom attribute, change [PM_ATTR_SN] with the name of this attribute__
39+
40+
#### PHP LAUNCHER FILE
41+
* mv dcim.php.example patchmanager.php
42+
* vim patchmanager.php
43+
* Change [PM_SERVER_KEY] with the key of your PatchManager server in configuration file
44+
45+
#### CREDENTIALS FILE
46+
/!\ For security reason, use a read only account!
47+
__*Change informations which are between []*__
48+
* vim credentialsFile
49+
* read -sr USER_PASSWORD_INPUT
50+
* export DCIM_[PM_SERVER_KEY]_LOGIN=[YourLoginHere]
51+
* export DCIM_[PM_SERVER_KEY]_PASSWORD=$USER_PASSWORD_INPUT
52+
__Change [PM_SERVER_KEY] with the key of your PatchManager server in configuration file__
53+
54+
55+
# EXECUTION
56+
57+
#### SHELL
58+
Launch PHP-CLI Shell for PatchManager service
59+
* source credentialsFile
60+
* php patchmanager.php
61+
62+
#### COMMAND
63+
Get command result in order to handle with your OS shell.
64+
/!\ The result is JSON so you can use JQ https://stedolan.github.io/jq/
65+
__*Change informations which are between []*__
66+
* source credentialsFile
67+
* php patchmanager.php "[myCommandHere]"

classes/soap.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
class SOAP
3+
{
4+
protected $_service;
5+
protected $_server;
6+
protected $_options;
7+
protected $_handle;
8+
9+
protected $_debug = false;
10+
11+
12+
public function __construct($server, $service = null)
13+
{
14+
$this->_server = $server;
15+
$this->_service = (Tools::is('string&&!empty', $service)) ? ($service) : ('Unknown');
16+
17+
$this->_init();
18+
}
19+
20+
protected function _init()
21+
{
22+
$this->resetOpts();
23+
}
24+
25+
public function enableTrace()
26+
{
27+
$this->_options['trace'] = 1;
28+
return $this;
29+
}
30+
31+
public function disableTrace()
32+
{
33+
$this->_options['trace'] = 0;
34+
return $this;
35+
}
36+
37+
public function resetOpts()
38+
{
39+
$this->_options = array('trace' => 1);
40+
return $this;
41+
}
42+
43+
public function getOpt($optName)
44+
{
45+
if(array_key_exists($optName, $this->_options)) {
46+
return $this->_options[$optName];
47+
}
48+
else {
49+
return false;
50+
}
51+
}
52+
53+
public function getOpts()
54+
{
55+
return $this->_options;
56+
}
57+
58+
public function setOpt($optName, $optValue)
59+
{
60+
$this->_options[$optName] = $optValue;
61+
return $this;
62+
}
63+
64+
public function setOpts(array $opts)
65+
{
66+
$this->_options = $opts;
67+
return $this;
68+
}
69+
70+
public function start()
71+
{
72+
if($this->_handle === null) {
73+
$this->_handle = new SoapClient($this->_server, $this->_options);
74+
}
75+
76+
return true;
77+
}
78+
79+
public function __call($name, array $arguments)
80+
{
81+
$status = $this->start();
82+
83+
if($status) {
84+
return call_user_func_array(array($this->_handle, $name), $arguments);
85+
}
86+
else {
87+
throw new Exception("It is not possible to execute SOAP call for ".$this->_service." service", E_USER_ERROR);
88+
}
89+
}
90+
91+
public function debug($debug = true)
92+
{
93+
$this->_debug = (bool) $debug;
94+
return $this;
95+
}
96+
}

config.json.example

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"DEFAULT": {
3+
"sys": {
4+
"browserCmd": "xdg-open"
5+
}
6+
},
7+
8+
"DCIM": {
9+
"servers": {
10+
"[PM_SERVER_KEY]": "https://myPatchManagerServerAddress.ext",
11+
"myPmKey": "https://patchmanager.com/pmserver/"
12+
},
13+
14+
"userAttrs": {
15+
"default": {
16+
"prefix": "",
17+
"labels": {
18+
"serialNumber": "[PM_ATTR_SN]"
19+
}
20+
}
21+
}
22+
}
23+
}

dcim.php.example

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
include_once('services/dcim.php');
3+
4+
/**
5+
* Change [PM_SERVER_KEY] with the key of your PatchManager server in configuration file
6+
* Example: $MAIN = new Service_Dcim(__DIR__ . '/config.json', 'myPmKey');
7+
*/
8+
$MAIN = new Service_Dcim(__DIR__ . '/config.json', '[PM_SERVER_KEY]');
9+
10+
echo "\r\n";
11+
exit();

dcim/abstract.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
include_once(__DIR__ . '/main.php');
3+
include_once(__DIR__ . '/connector/abstract.php');
4+
include_once(__DIR__ . '/connector/soap.php');
5+
6+
abstract class DCIM_Abstract extends DCIM_Main
7+
{
8+
protected $_aDCIM = array();
9+
protected $_oDCIM = null;
10+
11+
12+
public function __construct(array $servers, $printInfoMessages = true)
13+
{
14+
$config = CONFIG::getInstance()->DCIM;
15+
16+
foreach($servers as $server)
17+
{
18+
$server = mb_strtoupper($server);
19+
20+
if(!$config->servers->key_exists($server)) {
21+
throw new Exception("Unable to retreive DCIM server @ ".$server, E_USER_ERROR);
22+
}
23+
else
24+
{
25+
$login = getenv('DCIM_'.$server.'_LOGIN');
26+
$password = getenv('DCIM_'.$server.'_PASSWORD');
27+
28+
if($login === false || $password === false) {
29+
throw new Exception("Unable to retreive DCIM credentials for [".$server."] from env", E_USER_ERROR);
30+
}
31+
32+
$this->_aDCIM[$server] = new DCIM_Connector_Soap($config->servers[$server], $login, $password, $printInfoMessages);
33+
}
34+
}
35+
36+
if(count($this->_aDCIM) === 1) {
37+
$this->_oDCIM = current($this->_aDCIM);
38+
}
39+
}
40+
41+
public function getJnlpUrl($server = false, $version = 64)
42+
{
43+
if($server === false && $this->_oDCIM !== null) {
44+
return $this->_oDCIM->getJnlpUrl($version);
45+
}
46+
elseif(array_key_exists($server, $this->_aDCIM)) {
47+
return $this->_aDCIM[$server]->getJnlpUrl($version);
48+
}
49+
50+
return false;
51+
}
52+
53+
public function getDcim($equipLabel = null)
54+
{
55+
if($this->_oDCIM !== null) {
56+
return $this->_oDCIM;
57+
}
58+
elseif($equipLabel !== null)
59+
{
60+
if(preg_match('#^(.*-[ps]-.*)$#i', $equipLabel)) {
61+
return $this->_aDCIM['SEC'];
62+
}
63+
elseif(preg_match('#^(.*-[il]-.*)$#i', $equipLabel)) {
64+
return $this->_aDCIM['CORP'];
65+
}
66+
elseif(preg_match('#^(.*-[d]-.*)$#i', $equipLabel)) {
67+
return $this->_aDCIM['DEV'];
68+
}
69+
}
70+
71+
throw new Exception('Impossible de retourner le service DCIM adapté', E_USER_ERROR);
72+
}
73+
74+
public function __get($name)
75+
{
76+
if($this->_oDCIM !== null) {
77+
return $this->_oDCIM;
78+
}
79+
else
80+
{
81+
switch(mb_strtolower($name))
82+
{
83+
case 'sec':
84+
return $this->_aDCIM['SEC'];
85+
case 'corp':
86+
return $this->_aDCIM['CORP'];
87+
case 'dev':
88+
return $this->_aDCIM['DEV'];
89+
}
90+
}
91+
92+
throw new Exception("Le DCIM ".$name." n'existe pas", E_USER_ERROR);
93+
}
94+
95+
public function __call($name, array $arguments)
96+
{
97+
if($this->_oDCIM !== null) {
98+
return call_user_func_array(array($this->_oDCIM, $name), $arguments);
99+
}
100+
else
101+
{
102+
$results = array();
103+
104+
foreach($_aDCIM as $dcim) {
105+
$result[] = call_user_func_array(array($dcim, $name), $arguments);
106+
}
107+
108+
return $results;
109+
}
110+
}
111+
112+
public static function __callStatic($name, array $arguments)
113+
{
114+
return forward_static_call_array(array('DCIM_Connector_Soap', $name), $arguments);
115+
}
116+
}

0 commit comments

Comments
 (0)