servermgrv2 init...

This commit is contained in:
최준흠 2023-07-19 16:23:30 +09:00
parent a3c5e26b28
commit bb717a7cd6
7 changed files with 133 additions and 107 deletions

View File

@ -167,11 +167,14 @@ define('STATUS', [
//Upload , Download 관련 //Upload , Download 관련
define('PATHS', [ define('PATHS', [
'EXCEL' => getenv('path.excel') ? getenv('path.excel') : "../writable/Excel", 'EXCEL' => getenv('path.excel') ? getenv('path.excel') : "../writable/Excel",
'UPLOADS' => getenv('path.upload') ? getenv('path.upload') : "../writable/uploads", 'UPLOAD' => getenv('path.upload') ? getenv('path.upload') : "../writable/uploads",
'DOWNLOAD' => getenv('path.download') ? getenv('path.download') : "../writable/download", 'DOWNLOAD' => getenv('path.download') ? getenv('path.download') : "../writable/download",
'API' => getenv('path.api') ? getenv('path.api') : "../writable/api",
]); ]);
if (!is_dir(APPPATH . PATHS['EXCEL'])) { foreach (PATHS as $key => $path) {
mkdir(APPPATH . PATHS['EXCEL'], 0640); if (!is_dir(APPPATH . $path)) {
mkdir(APPPATH . $path, 0640);
}
} }
//아이콘 및 Sound관련 //아이콘 및 Sound관련

View File

@ -65,8 +65,8 @@ class HPILOController extends \App\Controllers\Admin\AdminController
private function getAdapter(HPILOEntity $entity) private function getAdapter(HPILOEntity $entity)
{ {
if (is_null($this->_adapter)) { if (is_null($this->_adapter)) {
$adapterClass = HPILOS['ADAPTER']; $adapterClass = getenv('hpilo.api.adapter') ? getenv('hpilo.api.adapter') : "\App\Libraries\Log\GuzzleAdapter";
$this->_adapter = new $adapterClass($entity, HPILOS['DEBUG']); $this->_adapter = new $adapterClass($entity, getenv('hpilo.api.debug') ? getenv('hpilo.api.debug') : false);
} }
return $this->_adapter; return $this->_adapter;
} }

View File

@ -59,38 +59,27 @@ class UserSNSController extends \App\Controllers\Admin\AdminController
} }
} }
//Insert관련
protected function insert_process()
{
// return parent::insert_process();
}
//Update관련
protected function update_process($entity)
{
// return parent::update_process($entity);
}
////Action 모음 ////Action 모음
//Insert관련 //Insert관련
final public function insert() // final public function insert()
{ // {
// return $this->insert_procedure(); // // return $this->insert_procedure();
} // }
//Update관련 // //Update관련
final public function update($uid) // final public function update($uid)
{ // {
// return $this->update_procedure($uid); // // return $this->update_procedure($uid);
} // }
//Toggle관련 // //Toggle관련
final public function toggle($uid, string $field) // final public function toggle($uid, string $field)
{ // {
// return $this->toggle_procedure($uid, $field); // // return $this->toggle_procedure($uid, $field);
} // }
//Batchjob 관련 // //Batchjob 관련
final public function batchjob() // final public function batchjob()
{ // {
// return $this->batchjob_procedure(); // // return $this->batchjob_procedure();
} // }
//Delete 관련 //Delete 관련
final public function delete($uid) final public function delete($uid)
{ {

View File

@ -5,17 +5,19 @@ namespace App\Libraries\Adapter\API;
use \App\Entities\HPILOEntity; use \App\Entities\HPILOEntity;
// 참고:https://github.com/SyntaxPhoenix/iloclient // 참고:https://github.com/SyntaxPhoenix/iloclient
class Adapter abstract class Adapter
{ {
private $_entity = null; private $_entity = null;
protected $_client = null; protected $_client = null;
protected $_debug = false; protected $_debug = false;
public function __construct($entity, $debug = false) protected function __construct($entity, $debug = false)
{ {
$this->_entity = $entity; $this->_entity = $entity;
$this->_debug = $debug; $this->_debug = $debug;
} }
protected function getEntity(): HPILOEntity abstract protected function getClient();
abstract protected function requestURL(string $url, string $method, array $datas = []): object;
final protected function getEntity()
{ {
return $this->_entity; return $this->_entity;
} }
@ -28,6 +30,19 @@ class Adapter
//type: basic , digest //type: basic , digest
return array($this->getEntity()->getID(), $this->getEntity()->getPassword(), $type); return array($this->getEntity()->getID(), $this->getEntity()->getPassword(), $type);
} }
protected function isSSLVerifiy(): bool
{
return getenv('hpilo.ssl') == 'true' ? true : false;
}
protected function getCookieFile()
{
return PATHS['API'] . getenv('api.cookie') ? getenv('api.cookie') : "api-cookie_" . date("Ymd") . ".log";
}
protected function getDebugFile()
{
return PATHS['API'] . getenv('api.debug') ? getenv('api.debug') : "api-debug_" . date("Ymd") . ".log";
}
final public function get(string $url): object final public function get(string $url): object
{ {
return $this->requestURL($url, 'GET'); return $this->requestURL($url, 'GET');
@ -36,47 +51,4 @@ class Adapter
{ {
return $this->requestURL($url, 'POST', $datas); return $this->requestURL($url, 'POST', $datas);
} }
protected function getClient()
{
if (is_null($this->_client)) {
// 참조:https://www.codeigniter.com/user_guide/libraries/curlrequest.html?highlight=curl#
// ex:)$options = [ 'baseURI' => 'http://www.foo.com/1.0/', 'timeout' => 0, 'allow_redirects' => false, 'proxy' => '192.168.16.1:10' ]
$options = [
'baseURI' => $this->getServerInfo(),
'auth' => $this->getAccountInfo(),
'verify' => getenv('hpilo.verify') == 'true' ? true : false,
'cookie' => HPILOS['PATH'] . getenv('hpilo.cookie.file'),
];
if ($this->_debug) {
$options['debug'] = HPILOS['PATH'] . getenv('hpilo.debug.file'); //or true
}
$this->_client = \Config\Services::curlrequest($options);
}
return $this->_client;
}
protected function requestURL(string $url, string $method, array $datas = []): object
{
// dd($this->getClient());
$options = array();
switch ($method) {
case 'POST':
$response = $this->getClient()->setBody($datas)->request($method, $url, $options);
break;
case 'HEAD':
break;
default:
$response = $this->getClient()->request($method, $url, $options);
break;
}
dd($response);
if ($response->getStatusCode() != 200) {
throw new \Exception(sprintf(
"오류가 발생하였습니다.\n%s\n%s",
$response->getHeaderLine('content-type'),
$response->getBody()
));
}
return json_decode($response->getBody());
}
} }

View File

@ -7,19 +7,19 @@ namespace App\Libraries\Adapter\API;
// https://github.com/SyntaxPhoenix/iloclient // https://github.com/SyntaxPhoenix/iloclient
class CurlAdapter extends Adapter class CurlAdapter extends Adapter
{ {
public function __construct(\App\Entities\HPILOEntity $entity, $debug = false) public function __construct($entity, $debug = false)
{ {
parent::__construct($entity, $debug); parent::__construct($entity, $debug);
} }
private function debug($ch, $response, array $datas = array()) private function debugging($response, array $datas = array())
{ {
if (!$this->_debug) { if (!$this->_debug) {
return false; return false;
} }
if ($response === false) { if ($response === false) {
log_message('error', curl_error($ch)); log_message('error', curl_error($this->getClient()));
} }
$info = curl_getinfo($ch); $info = curl_getinfo($this->getClient());
log_message('debug', var_export($info, true)); log_message('debug', var_export($info, true));
log_message('debug', var_export($datas, true)); log_message('debug', var_export($datas, true));
log_message('debug', "{$info['total_time']}초, url:{$info['url']}, return:{$info['http_code']}"); log_message('debug', "{$info['total_time']}초, url:{$info['url']}, return:{$info['http_code']}");
@ -152,36 +152,43 @@ class CurlAdapter extends Adapter
break; break;
} }
} }
protected function getClient()
{
if (is_null($this->_client)) {
$this->_client = curl_init();
}
return $this->_client;
}
protected function requestURL(string $url, string $method, array $datas = []): object protected function requestURL(string $url, string $method, array $datas = []): object
{ {
$ch = curl_init($this->getServerInfo() . $url); curl_setopt($this->getClient(), CURLOPT_URL, $this->getServerInfo() . $url);
switch ($method) { switch ($method) {
case 'POST': case 'POST':
curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($this->getClient(), CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); curl_setopt($this->getClient(), CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($datas)); curl_setopt($this->getClient(), CURLOPT_POSTFIELDS, json_encode($datas));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($this->getClient(), CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//cookie값 파일저장용 //cookie값 파일저장용
curl_setopt($ch, CURLOPT_COOKIEJAR, HPILOS['CURL_COOKIE_FILE']); curl_setopt($this->getClient(), CURLOPT_COOKIEJAR, $this->getCookieFile());
curl_setopt($ch, CURLOPT_COOKIEFILE, HPILOS['CURL_COOKIE_FILE']); curl_setopt($this->getClient(), CURLOPT_COOKIEFILE, $this->getCookieFile());
break; break;
default: default:
break; break;
} }
//cookie값 전달용 //cookie값 전달용
foreach (curl_getinfo($ch, CURLINFO_COOKIELIST) as $cookie_line) { foreach (curl_getinfo($this->getClient(), CURLINFO_COOKIELIST) as $cookie_line) {
curl_setopt($ch, CURLOPT_COOKIELIST, $cookie_line); curl_setopt($this->getClient(), CURLOPT_COOKIELIST, $cookie_line);
} }
//SSL 확인여부용 //SSL 확인여부용
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, HPILOS['SSL']); curl_setopt($this->getClient(), CURLOPT_SSL_VERIFYPEER, $this->isSSLVerifiy());
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, HPILOS['SSL']); curl_setopt($this->getClient(), CURLOPT_SSL_VERIFYHOST, $this->isSSLVerifiy());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($this->getClient(), CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($this->getClient(), CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, implode(":", $this->getAccountInfo())); curl_setopt($this->getClient(), CURLOPT_USERPWD, implode(":", $this->getAccountInfo()));
//header값 전달용 //header값 전달용
$headers = []; $headers = [];
curl_setopt( curl_setopt(
$ch, $this->getClient(),
CURLOPT_HEADERFUNCTION, CURLOPT_HEADERFUNCTION,
function ($curl, $header) use (&$headers) { function ($curl, $header) use (&$headers) {
$length = strlen($header); $length = strlen($header);
@ -193,9 +200,9 @@ class CurlAdapter extends Adapter
return $length; return $length;
} }
); );
$response = curl_exec($ch); $response = curl_exec($this->getClient());
$this->debug($ch, $response); $this->debugging($response);
curl_close($ch); curl_close($this->getClient());
if (is_null($response)) { if (is_null($response)) {
throw new \Exception("해당서버[{$this->getServerInfo()}]의 ILO접속 오류가 발생하였습니다."); throw new \Exception("해당서버[{$this->getServerInfo()}]의 ILO접속 오류가 발생하였습니다.");
} }

View File

@ -8,7 +8,7 @@ use GuzzleHttp\Exception\ClientException;
class GuzzleAdapter extends Adapter class GuzzleAdapter extends Adapter
{ {
private $_jar = null; private $_jar = null;
public function __construct(\App\Entities\HPILOEntity $entity, $debug = false) public function __construct($entity, $debug = false)
{ {
parent::__construct($entity, $debug); parent::__construct($entity, $debug);
} }
@ -26,7 +26,8 @@ class GuzzleAdapter extends Adapter
log_message('debug', var_export($this->getCookieJar()->getCookieByName('session' . $key), true)); log_message('debug', var_export($this->getCookieJar()->getCookieByName('session' . $key), true));
} }
} }
protected function getClient()
protected function getClient() //Guzzle이용시
{ {
if (is_null($this->_client)) { if (is_null($this->_client)) {
// 참조:https://docs.guzzlephp.org/en/stable/request-options.html // 참조:https://docs.guzzlephp.org/en/stable/request-options.html
@ -34,8 +35,8 @@ class GuzzleAdapter extends Adapter
$options = [ $options = [
'base_uri' => $this->getServerInfo(), 'base_uri' => $this->getServerInfo(),
'auth' => $this->getAccountInfo(), 'auth' => $this->getAccountInfo(),
'verify' => HPILOS['SSL'], 'verify' => $this->isSSLVerifiy(),
'cookie' => HPILOS['GUZZLE_COOKIE'], 'cookie' => $this->getCookieFile(),
// \GuzzleHttp\RequestOptions::ALLOW_REDIRECTS => [ // \GuzzleHttp\RequestOptions::ALLOW_REDIRECTS => [
// 'max' => 10, // allow at most 10 redirects. // 'max' => 10, // allow at most 10 redirects.
// 'strict' => true, // use "strict" RFC compliant redirects. // 'strict' => true, // use "strict" RFC compliant redirects.
@ -47,6 +48,7 @@ class GuzzleAdapter extends Adapter
} }
return $this->_client; return $this->_client;
} }
protected function requestURL(string $url, string $method, array $datas = []): object protected function requestURL(string $url, string $method, array $datas = []): object
{ {
try { try {

View File

@ -0,0 +1,53 @@
<?php
namespace App\Libraries\Adapter\API;
class LocalAdapter extends Adapter
{
public function __construct($entity, $debug = false)
{
parent::__construct($entity, $debug);
}
protected function getClient() //Codeigniter4 Service의 curlrequest이용시
{
if (is_null($this->_client)) {
// 참조:https://www.codeigniter.com/user_guide/libraries/curlrequest.html?highlight=curl#
// ex:)$options = [ 'baseURI' => 'http://www.foo.com/1.0/', 'timeout' => 0, 'allow_redirects' => false, 'proxy' => '192.168.16.1:10' ]
$options = [
'baseURI' => $this->getServerInfo(),
'auth' => $this->getAccountInfo(),
'verify' => $this->isSSLVerifiy(),
'cookie' => $this->getCookieFile(),
];
if ($this->_debug) {
$options['debug'] = PATHS['API'] . getenv('api.adapter.debug') ? getenv('api.adapter.debug') : "api-debug_" . date("Ymd");
}
$this->_client = \Config\Services::curlrequest($options);
}
return $this->_client;
}
protected function requestURL(string $url, string $method, array $datas = []): object
{
$options = array();
switch ($method) {
case 'POST':
$response = $this->getClient()->setBody($datas)->request($method, $url, $options);
break;
case 'HEAD':
break;
default:
$response = $this->getClient()->request($method, $url, $options);
break;
}
dd($response);
if ($response->getStatusCode() != 200) {
throw new \Exception(sprintf(
"오류가 발생하였습니다.\n%s\n%s",
$response->getHeaderLine('content-type'),
$response->getBody()
));
}
return json_decode($response->getBody());
}
}