1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-02 12:48:26 +02:00

Partial teardown functionality for cPanel deployer

Also renamed class Prepare_cPanel to cPanelDeployer
This commit is contained in:
Deltik
2018-02-07 05:28:52 -06:00
parent ee84c3de76
commit ba9a27787e
2 changed files with 37 additions and 17 deletions

View File

@@ -1,13 +1,15 @@
<?php <?php
include_once(__DIR__ . "/cpaneluapi/cpaneluapi.class.php"); include_once(__DIR__ . "/../cpaneluapi/cpaneluapi.class.php");
define('ACCEPTANCE_TEST_PREFIX', 'acceptance-test-'); define('ACCEPTANCE_TEST_PREFIX', 'acceptance-test-');
class Prepare_cPanel class cPanelDeployer
{ {
protected $credentials; protected $credentials;
protected $cPanel; protected $cPanel;
protected $homedir;
protected $docroot;
protected $domain; protected $domain;
protected $run_id; protected $run_id;
@@ -24,7 +26,7 @@ class Prepare_cPanel
public function start() public function start()
{ {
self::println(); self::println();
self::println("=== Prepare cPanel ==="); self::println("=== cPanel Deployer Bring Up ===");
$creds = $this->credentials; $creds = $this->credentials;
if (!$creds['hostname'] || if (!$creds['hostname'] ||
!$creds['username'] || !$creds['username'] ||
@@ -52,28 +54,28 @@ class Prepare_cPanel
throw new Exception("Cannot connect to cPanel at \"${hostname}\" with username \"${username}\" and password \"${password}\""); throw new Exception("Cannot connect to cPanel at \"${hostname}\" with username \"${username}\" and password \"${password}\"");
} }
$userdata = $domains_data->{'data'}; $userdata = $domains_data->{'data'};
$homedir = $userdata->{'main_domain'}->{'homedir'}; $this->homedir = $homedir = $userdata->{'main_domain'}->{'homedir'};
$docroot = $userdata->{'main_domain'}->{'documentroot'}; $this->docroot = $docroot = $userdata->{'main_domain'}->{'documentroot'};
$this->domain = $domain = $userdata->{'main_domain'}->{'domain'}; $this->domain = $domain = $userdata->{'main_domain'}->{'domain'};
self::println("Obtained domain name from cPanel: " . $this->domain); self::println("Obtained home directory from cPanel: " . $this->homedir);
self::println("Obtained document root from cPanel: " . $this->docroot);
self::println("Obtained domain name from cPanel: " . $this->domain);
self::println("Retrieving existing registered tests from cPanel account…");
$acceptance_tests = self::get_active_acceptance_tests($cPanel, $homedir); $acceptance_tests = self::get_active_acceptance_tests($cPanel, $homedir);
self::println("Adding this test (".$this->run_id.") to registered tests list…");
$run_time = microtime(true); $run_time = microtime(true);
array_push($acceptance_tests, array_push($acceptance_tests,
['id' => $run_id, ['id' => $run_id,
'time' => $run_time 'time' => $run_time
]); ]);
self::println("Writing this test to registered tests list in cPanel account…");
self::write_acceptance_tests($cPanel, $homedir, $acceptance_tests); self::write_acceptance_tests($cPanel, $homedir, $acceptance_tests);
$valid_acceptance_test_ids = self::get_acceptance_test_ids($acceptance_tests); $valid_acceptance_test_ids = self::get_acceptance_test_ids($acceptance_tests);
self::println("Current unexpired tests: [".implode(", ", $valid_acceptance_test_ids)."]"); self::println("Current unexpired tests: [".implode(", ", $valid_acceptance_test_ids)."]");
self::println("Pruning expired tests…");
self::prune_inactive_acceptance_test_resources($cPanel, $valid_acceptance_test_ids); self::prune_inactive_acceptance_test_resources($cPanel, $valid_acceptance_test_ids);
$db_id = "${username}_${run_id}"; $db_id = "${username}_${run_id}";
@@ -95,9 +97,16 @@ class Prepare_cPanel
public function stop() public function stop()
{ {
$acceptance_tests = self::get_active_acceptance_tests($cPanel, $homedir); self::println("=== cPanel Deployer Tear Down ===");
$acceptance_tests = self::prune_acceptance_tests($acceptance_tests, $this->run_id); $cPanel = $this->cPanel;
self::write_acceptance_tests($cPanel, $homedir, $acceptance_tests); $acceptance_tests = self::get_active_acceptance_tests($cPanel, $this->homedir);
self::println("Removing this test (".$this->run_id.") from registered tests list…");
self::prune_acceptance_tests($acceptance_tests, $this->run_id);
self::write_acceptance_tests($cPanel, $this->homedir, $acceptance_tests);
$valid_acceptance_test_ids = self::get_acceptance_test_ids($acceptance_tests);
self::println("Current unexpired tests: [".implode(", ", $valid_acceptance_test_ids)."]");
self::prune_inactive_acceptance_test_resources($cPanel, $valid_acceptance_test_ids);
} }
private static function println($text = '') private static function println($text = '')
@@ -107,6 +116,7 @@ class Prepare_cPanel
private static function prune_inactive_acceptance_test_resources($cPanel, $valid_acceptance_test_ids) private static function prune_inactive_acceptance_test_resources($cPanel, $valid_acceptance_test_ids)
{ {
self::println("Pruning expired tests…");
$listdbs = $cPanel->api2->MysqlFE->listdbs()->{'cpanelresult'}->{'data'}; $listdbs = $cPanel->api2->MysqlFE->listdbs()->{'cpanelresult'}->{'data'};
self::prune_mysql_databases($listdbs, $valid_acceptance_test_ids, $cPanel); self::prune_mysql_databases($listdbs, $valid_acceptance_test_ids, $cPanel);
@@ -116,12 +126,13 @@ class Prepare_cPanel
private static function get_active_acceptance_tests($cPanel, $homedir) private static function get_active_acceptance_tests($cPanel, $homedir)
{ {
self::println("Retrieving existing registered tests from cPanel account…");
$acceptance_tests = []; $acceptance_tests = [];
$acceptance_tests_apiresponse = $cPanel->uapi->Fileman->get_file_content(['dir' => $homedir, 'file' => 'acceptance_tests.status.txt']); $acceptance_tests_apiresponse = $cPanel->uapi->Fileman->get_file_content(['dir' => $homedir, 'file' => 'acceptance_tests.status.txt']);
if (!is_null($acceptance_tests_apiresponse->{'data'})) if (!is_null($acceptance_tests_apiresponse->{'data'}))
{ {
$acceptance_tests_raw = $acceptance_tests_apiresponse->{'data'}->{'content'}; $acceptance_tests_raw = $acceptance_tests_apiresponse->{'data'}->{'content'};
$acceptance_tests = json_decode($acceptance_tests_raw, true); $acceptance_tests = (array) json_decode($acceptance_tests_raw, true);
self::prune_acceptance_tests($acceptance_tests); self::prune_acceptance_tests($acceptance_tests);
} }
return $acceptance_tests; return $acceptance_tests;
@@ -138,6 +149,7 @@ class Prepare_cPanel
} }
} }
$list = array_values($list); $list = array_values($list);
return $list;
} }
private static function get_acceptance_test_ids(array $list) private static function get_acceptance_test_ids(array $list)
@@ -154,6 +166,7 @@ class Prepare_cPanel
{ {
$acceptance_tests_json = json_encode($acceptance_tests, JSON_PRETTY_PRINT); $acceptance_tests_json = json_encode($acceptance_tests, JSON_PRETTY_PRINT);
self::println("Saving registered tests list to cPanel account…");
$cPanel->uapi->Fileman->save_file_content(['dir' => $homedir, 'file' => 'acceptance_tests.status.txt', 'content' => $acceptance_tests_json]); $cPanel->uapi->Fileman->save_file_content(['dir' => $homedir, 'file' => 'acceptance_tests.status.txt', 'content' => $acceptance_tests_json]);
} }

View File

@@ -1,24 +1,31 @@
<?php <?php
namespace Helper; namespace Helper;
include_once(__DIR__ . "/../../../lib/prepare_cpanel.php"); include_once(__DIR__ . "/../../../lib/deployers/cpanel_deployer.php");
// here you can define custom actions // here you can define custom actions
// all public methods declared in helper class will be available in $I // all public methods declared in helper class will be available in $I
class Acceptance extends \Codeception\Module class Acceptance extends \Codeception\Module
{ {
protected $deployer;
public function _beforeSuite($settings = array()) public function _beforeSuite($settings = array())
{ {
$secrets = $settings['secrets']; $secrets = $settings['secrets'];
if ($secrets['cpanel']['enabled'] === '1') if ($secrets['cpanel']['enabled'] === '1')
{ {
$prepare = new \Prepare_cPanel($secrets['cpanel']); $this->deployer = new \cPanelDeployer($secrets['cpanel']);
$retcode = $prepare->start(); $retcode = $this->deployer->start();
if ($retcode === true) if ($retcode === true)
{ {
$domain = $prepare->getDomain(); $domain = $this->deployer->getDomain();
$this->getModule('PhpBrowser')->_reconfigure(array('url' => "http://${domain}")); $this->getModule('PhpBrowser')->_reconfigure(array('url' => "http://${domain}"));
} }
} }
} }
public function _afterSuite()
{
$this->deployer->stop();
}
} }