From 8b4016fc49dd4c358919782464a651f6ee1dfa91 Mon Sep 17 00:00:00 2001 From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch> Date: Mon, 2 Sep 2024 16:00:20 +0200 Subject: [PATCH] update all plugins --- .../plugins/build/tgz/build_tgz.php | 2 + .../plugins/build/zip/build_zip.php | 2 + .../plugins/rollout/awx/rollout_awx.php | 72 ++++++++++++------- .../rollout/default/rollout_default.php | 27 ++++--- .../plugins/rollout/ssh/rollout_ssh.php | 25 ++++--- .../plugins/shellcmd/load/plugin.php | 28 ++++---- .../plugins/shellcmd/processes/plugin.php | 16 +++-- .../plugins/shellcmd/top/plugin.php | 16 +++-- 8 files changed, 121 insertions(+), 67 deletions(-) diff --git a/public_html/deployment/plugins/build/tgz/build_tgz.php b/public_html/deployment/plugins/build/tgz/build_tgz.php index 2b7ffd4a..5ca737d1 100644 --- a/public_html/deployment/plugins/build/tgz/build_tgz.php +++ b/public_html/deployment/plugins/build/tgz/build_tgz.php @@ -5,6 +5,8 @@ * Build plugin - TGZ * * @author <axel.hahn@iml.unibe.ch> + * + * 2024-09-02 Axel php8 only; added variable types; short array syntax */ class build_tgz extends build_base { diff --git a/public_html/deployment/plugins/build/zip/build_zip.php b/public_html/deployment/plugins/build/zip/build_zip.php index c5512f0a..a36fcee0 100644 --- a/public_html/deployment/plugins/build/zip/build_zip.php +++ b/public_html/deployment/plugins/build/zip/build_zip.php @@ -5,6 +5,8 @@ * Build plugin - TGZ * * @author <axel.hahn@iml.unibe.ch> + * + * 2024-09-02 Axel php8 only; added variable types; short array syntax */ class build_zip extends build_base { diff --git a/public_html/deployment/plugins/rollout/awx/rollout_awx.php b/public_html/deployment/plugins/rollout/awx/rollout_awx.php index 910adccf..5c791f2e 100644 --- a/public_html/deployment/plugins/rollout/awx/rollout_awx.php +++ b/public_html/deployment/plugins/rollout/awx/rollout_awx.php @@ -4,18 +4,23 @@ * * Rollout plugin - awx * - * Run an Https POST request to AWX + * Run an Https GET or POST request to AWX * * @author <axel.hahn@iml.unibe.ch> + * + * 2024-09-02 Axel php8 only; added variable types; short array syntax */ class rollout_awx extends rollout_base { // url part for AWX API request to set count of results per page - protected $_sAwxApiPaging = '&page_size=10000&page=1'; + protected string $_sAwxApiPaging = '&page_size=10000&page=1'; /** - * check requirements if the plugin could work + * Get an array with shell commands to check requirements if the plugin + * can work + * + * @return array */ public function checkRequirements(): array { @@ -24,26 +29,31 @@ class rollout_awx extends rollout_base } /** - * check access to a deploy target + * Get an array with shell commands to check access to a deploy target + * + * @return array */ - public function checkConnectionToTarget() + public function checkConnectionToTarget(): array { // do nothing ... always empty return []; } /** - * make an http get request and return the response body + * Make an http get request and return the response body * it is called by _makeRequest * $aRequest contains subkeys - * - url - * - method; one of GET|POST|PUT|DELETE - * - postdata; for POST only + * - url relative urr; part behind api base url + * - method one of GET|POST|PUT|DELETE + * - postdata for POST only + * - ignore-ssl-error flag: if true it willignores ssl verifiction (not recommended) + * - user, password authentication with "user:password" * - * @param array $aRequest arrayurl for Foreman API - * @return array + * @param array $aRequest array with request data + * @param integer $iTimeout timeout in seconds + * @return array ... with subkeys "header" and "body" - or "error" if something went wrong */ - protected function _httpRequest($aRequest = false, $iTimeout = 5) + protected function _httpRequest(array $aRequest = [], int $iTimeout = 5): array { if (!function_exists("curl_init")) { @@ -51,8 +61,10 @@ class rollout_awx extends rollout_base } $aConfig = $this->getConfig(); + $sAwxApiUrl=$aConfig['url'] . $aRequest['url']; + // base url --^ ^-- relative url to api - $ch = curl_init($aConfig['url'] . $aRequest['url']); + $ch = curl_init($sAwxApiUrl); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $aRequest['method']); if ($aRequest['method'] === 'POST') { @@ -74,6 +86,14 @@ class rollout_awx extends rollout_base curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $res = curl_exec($ch); + if (!$res) { + $iErrorCode = curl_errno($ch); + $sErrorMsg = curl_error($ch); + curl_close($ch); + return [ + 'error' => "Failed to fetch $sAwxApiUrl - curl error #$iErrorCode: $sErrorMsg" + ]; + } $aReturn = ['info' => curl_getinfo($ch), 'error' => curl_error($ch)]; curl_close($ch); @@ -87,17 +107,19 @@ class rollout_awx extends rollout_base } /** - * get AWX inventories and return them as array for select box - * [id] => array('value' => [ID], 'label' => [NAME] [ID]) + * Get AWX inventories and return them as array for select box + * [id] => ['value' => <ID>, 'label' => <NAME> <ID>] + * It returns false if the status code in the response is not 200 + * * @return bool|array */ - public function getAwxInventories() + public function getAwxInventories(): bool|array { $aResponse = $this->_httpRequest( - array( + [ 'url' => '/inventories/?order_by=name' . $this->_sAwxApiPaging, 'method' => 'GET', - ) + ] ); if (!isset($aResponse['info']['http_code']) || $aResponse['info']['http_code'] !== 200) { @@ -131,16 +153,16 @@ class rollout_awx extends rollout_base } /** * get AWX Job Templates and return them as array for select box - * [id] => array('value' => [ID], 'label' => [PLAYBOOK] [ID]) + * [id] => ['value' => [ID], 'label' => [PLAYBOOK] [ID]] * @return bool|array */ public function getAwxJobTemplates() { $aResponse = $this->_httpRequest( - array( + [ 'url' => '/job_templates/?order_by=name' . $this->_sAwxApiPaging, 'method' => 'GET', - ) + ] ); if (!isset($aResponse['info']['http_code']) || $aResponse['info']['http_code'] !== 200) { @@ -173,15 +195,15 @@ class rollout_awx extends rollout_base /** - * get array with commands to execute to deploy a package + * Get an array with shell commands to execute for deployment of built file * * @param string $sPhase * @param boolean $bMask Flag for public output; if true then mask your secrets * @return array */ - public function getDeployCommands($sPhase, $bMask = false) + public function getDeployCommands(string $sPhase, bool $bMask = false): array { - $aReturn = array(); + $aReturn = []; $aConfig = $this->getConfig($sPhase, $bMask); // ----- Checks: @@ -199,7 +221,7 @@ class rollout_awx extends rollout_base } // ----- Send variables having values only - $aBodyvars = array(); + $aBodyvars = []; foreach (['inventory' => 'inventory', 'limit' => 'limit', 'job_tags' => 'tags', 'extra_vars' => 'extravars'] as $sParam => $sVarkey) { if (isset($aConfig[$sVarkey]) && $aConfig[$sVarkey]) { $aBodyvars[$sParam] = $aConfig[$sVarkey]; diff --git a/public_html/deployment/plugins/rollout/default/rollout_default.php b/public_html/deployment/plugins/rollout/default/rollout_default.php index 56e742e4..da7fc3fc 100644 --- a/public_html/deployment/plugins/rollout/default/rollout_default.php +++ b/public_html/deployment/plugins/rollout/default/rollout_default.php @@ -6,12 +6,17 @@ * no action * * @author <axel.hahn@iml.unibe.ch> + * + * 2024-09-02 Axel php8 only; added variable types; short array syntax */ class rollout_default extends rollout_base { /** - * check requirements if the plugin could work + * Get an array with shell commands to check requirements if the plugin + * can work + * + * @return array */ public function checkRequirements(): array { @@ -20,22 +25,24 @@ class rollout_default extends rollout_base } /** - * check access to a deploy target + * Get an array with shell commands to check access to a deploy target + * + * @return array */ - public function checkConnectionToTarget() + public function checkConnectionToTarget(): array { // do nothing ... always empty return []; } /** - * get array with commands to execute to deploy a package + * Get an array with shell commands to execute for deployment of built file * * @param string $sPhase * @param boolean $bMask Flag for public output; if true then mask your secrets * @return array */ - public function getDeployCommands($sPhase, $bMask = false) + public function getDeployCommands(string $sPhase, bool $bMask = false): array { return [ 'echo "SKIP"' @@ -48,18 +55,18 @@ class rollout_default extends rollout_base * * @return string */ - public function renderFormdata4Project() + public function renderFormdata4Project(): string { return $this->_t('no-cfg'); } /** - * override general form renderer: show a single message that no - * configuration items exist - * + * override of form renderer: show configuration for a given phase + + * @param string $sPhase phaese; one of preview|stage|live * @return string */ - public function renderFormdata4Phase($sPhase) + public function renderFormdata4Phase(string $sPhase): string { return $this->_t('no-cfg'); } diff --git a/public_html/deployment/plugins/rollout/ssh/rollout_ssh.php b/public_html/deployment/plugins/rollout/ssh/rollout_ssh.php index 8ebac30a..27e6c82d 100644 --- a/public_html/deployment/plugins/rollout/ssh/rollout_ssh.php +++ b/public_html/deployment/plugins/rollout/ssh/rollout_ssh.php @@ -7,35 +7,44 @@ * Run a SSH command on remote targets * * @author <axel.hahn@iml.unibe.ch> + * + * 2024-09-02 Axel php8 only; added variable types; short array syntax */ class rollout_ssh extends rollout_base { /** - * check requirements if the plugin could work + * Get an array with shell commands to check requirements if the plugin + * can work + * * @return array */ - public function checkRequirements(): array { + public function checkRequirements(): array + { // no specific checks needed ... always empty return []; } /** - * check access to a deploy target + * Get an array with shell commands to check access to a deploy target + * + * @return array */ - public function checkConnectionToTarget() { + public function checkConnectionToTarget(): array + { // do nothing ... always empty return []; } /** - * get array with commands to execute to deploy a package + * Get an array with shell commands to execute for deployment of built file * - * @param string $sPhase phase + * @param string $sPhase * @param boolean $bMask Flag for public output; if true then mask your secrets * @return array */ - public function getDeployCommands($sPhase,$bMask=false){ - $aReturn=array(); + public function getDeployCommands(string $sPhase, bool $bMask = false): array + { + $aReturn=[]; $aConfig=$this->getConfig($sPhase); // loop over hosts and create shell commands for it diff --git a/public_html/deployment/plugins/shellcmd/load/plugin.php b/public_html/deployment/plugins/shellcmd/load/plugin.php index 1eb0a12c..ff5fec24 100644 --- a/public_html/deployment/plugins/shellcmd/load/plugin.php +++ b/public_html/deployment/plugins/shellcmd/load/plugin.php @@ -4,26 +4,30 @@ * SHELLCMD PLUGIN :: LOAD * * ---------------------------------------------------------------------- - * 2022-08-05 axel.hahn@iml.unibe.ch - * 2023-12-13 axel.hahn@unibe.ch minified + * Axel: axel.hahn@unibe.ch + * 2022-08-05 Axel initial version + * 2023-12-13 Axel minified + * 2024-09-02 Axel php8 only; added variable types; short array syntax */ -class shellcmd_load { +class shellcmd_load +{ /** * parse output and extract wanted values in section "data" * @return array */ - public function parsedata($aResult){ - $aTmp1=array_reverse(explode(',', $aResult['output'][0])); - $aResult['data']=[ - 'uptime'=>(isset($aTmp1[5]) + public function parsedata(array $aResult): array + { + $aTmp1 = array_reverse(explode(',', $aResult['output'][0])); + $aResult['data'] = [ + 'uptime' => (isset($aTmp1[5]) ? trim(substr($aTmp1[5], 10) . $aTmp1[4]) : trim(substr($aTmp1[4], 10)) - ).' h' + ) . ' h' , - 'users'=>trim(str_replace(' users', '', $aTmp1[3])), - 'load'=>trim(preg_replace('/^.*:/', '', $aTmp1[2])), - 'load5'=>trim($aTmp1[1]), - 'load15'=>trim($aTmp1[0]), + 'users' => trim(str_replace(' users', '', $aTmp1[3])), + 'load' => trim(preg_replace('/^.*:/', '', $aTmp1[2])), + 'load5' => trim($aTmp1[1]), + 'load15' => trim($aTmp1[0]), ]; return $aResult; } diff --git a/public_html/deployment/plugins/shellcmd/processes/plugin.php b/public_html/deployment/plugins/shellcmd/processes/plugin.php index ef858fea..ac6f0995 100644 --- a/public_html/deployment/plugins/shellcmd/processes/plugin.php +++ b/public_html/deployment/plugins/shellcmd/processes/plugin.php @@ -4,17 +4,21 @@ * SHELLCMD PLUGIN :: Processes * * ---------------------------------------------------------------------- - * 2022-09-19 axel.hahn@iml.unibe.ch - * 2023-12-13 axel.hahn@unibe.ch minified + * Axel: axel.hahn@unibe.ch + * 2022-09-19 Axel initial version + * 2023-12-13 Axel minified + * 2024-09-02 Axel php8 only; added variable types; short array syntax */ -class shellcmd_processes { +class shellcmd_processes +{ /** * parse output and extract wanted values in section "data" * @return array */ - public function parsedata($aResult){ - $aResult['data']=[ - 'count'=>count($aResult['output'])-1, + public function parsedata(array $aResult): array + { + $aResult['data'] = [ + 'count' => count($aResult['output']) - 1, ]; return $aResult; } diff --git a/public_html/deployment/plugins/shellcmd/top/plugin.php b/public_html/deployment/plugins/shellcmd/top/plugin.php index b0a5fa56..a9c20409 100644 --- a/public_html/deployment/plugins/shellcmd/top/plugin.php +++ b/public_html/deployment/plugins/shellcmd/top/plugin.php @@ -4,17 +4,21 @@ * SHELLCMD PLUGIN :: top * * ---------------------------------------------------------------------- - * 2023-11-23 axel.hahn@unibe.ch - * 2023-12-13 axel.hahn@unibe.ch minified + * Axel: axel.hahn@unibe.ch + * 2023-11-23 Axel initial version + * 2023-12-13 Axel minified + * 2024-09-02 Axel php8 only; added variable types; short array syntax */ -class shellcmd_top { +class shellcmd_top +{ /** * parse output and extract wanted values in section "data" * @return array */ - public function parsedata($aResult){ - $aResult['data']=[ - 'count'=>count($aResult['output'])-1, + public function parsedata(array $aResult): array + { + $aResult['data'] = [ + 'count' => count($aResult['output']) - 1, ]; return $aResult; } -- GitLab