Deployment: move logic to dedicated classes

Extended DirectorDeploymentLog, new DeploymentInfo class
This commit is contained in:
Thomas Gelf 2017-07-20 08:22:31 +02:00
parent 8c8c78b7c0
commit cca4092151
5 changed files with 92 additions and 34 deletions

View File

@ -567,7 +567,7 @@ constants
public function collectLogFiles(Db $db)
{
$existing = $this->listModuleStages('director');
foreach ($db->getUncollectedDeployments() as $deployment) {
foreach (DirectorDeploymentLog::getUncollected($db) as $deployment) {
$stage = $deployment->get('stage_name');
if (! in_array($stage, $existing)) {
continue;
@ -603,7 +603,7 @@ constants
public function wipeInactiveStages(Db $db)
{
$uncollected = $db->getUncollectedDeployments();
$uncollected = DirectorDeploymentLog::getUncollected($db);
$moduleName = 'director';
foreach ($this->listModuleStages($moduleName, false) as $stage) {
if (array_key_exists($stage, $uncollected)) {

View File

@ -35,7 +35,7 @@ class LegacyDeploymentApi implements DeploymentApiInterface
{
$existing = $this->listModuleStages('director');
foreach ($db->getUncollectedDeployments() as $deployment) {
foreach (DirectorDeploymentLog::getUncollected($db) as $deployment) {
$stage = $deployment->get('stage_name');
if (! in_array($stage, $existing)) {
continue;
@ -77,7 +77,7 @@ class LegacyDeploymentApi implements DeploymentApiInterface
*/
public function wipeInactiveStages(Db $db)
{
$uncollected = $db->getUncollectedDeployments();
$uncollected = DirectorDeploymentLog::getUncollected($db);
$moduleName = 'director';
$currentStage = $this->getActiveStageName();

View File

@ -672,33 +672,4 @@ class Db extends DbConnection
return $db->fetchPairs($query);
}
/**
* @return DirectorDeploymentLog[]
*/
public function getUncollectedDeployments()
{
$db = $this->db();
$query = $db->select()
->from('director_deployment_log')
->where('stage_name IS NOT NULL')
->where('stage_collected IS NULL')
->where('startup_succeeded IS NULL')
->order('stage_name');
return DirectorDeploymentLog::loadAll($this, $query, 'stage_name');
}
public function hasUncollectedDeployments()
{
$db = $this->db();
$query = $db->select()
->from('director_deployment_log', array('cnt' => 'COUNT(*)'))
->where('stage_name IS NOT NULL')
->where('stage_collected IS NULL')
->where('startup_succeeded IS NULL');
return $db->fetchOne($query) > 0;
}
}

View File

@ -0,0 +1,59 @@
<?php
namespace Icinga\Module\Director\Deployment;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\IcingaObject;
class DeploymentInfo
{
/** @var IcingaObject */
protected $object;
protected $db;
/** @var int */
protected $totalChanges;
/** @var int */
protected $objectChanges;
public function __construct(Db $db)
{
$this->db = $db;
}
public function setObject(IcingaObject $object)
{
$this->object = $object;
return $this;
}
public function getTotalChanges()
{
if ($this->totalChanges === null) {
$this->totalChanges = $this->db->countActivitiesSinceLastDeployedConfig();
}
return $this->totalChanges;
}
public function getSingleObjectChanges()
{
if ($this->objectChanges === null) {
if ($this->object === null) {
$this->objectChanges = 0;
} else {
$this->objectChanges = $this->db
->countActivitiesSinceLastDeployedConfig($this->object);
}
}
return $this->objectChanges;
}
public function hasUndeployedChanges()
{
return $this->getSingleObjectChanges() > 0 && $this->getTotalChanges() > 0;
}
}

View File

@ -44,7 +44,7 @@ class DirectorDeploymentLog extends DbObject
public function getConfig()
{
if ($this->config === null) {
$this->config = IcingaConfig::load($this->config_checksum);
$this->config = IcingaConfig::load($this->config_checksum, $this->connection);
}
return $this->config;
@ -103,4 +103,32 @@ class DirectorDeploymentLog extends DbObject
return static::load($db->fetchOne($query), $connection);
}
/**
* @return static[]
*/
public static function getUncollected(Db $connection)
{
$db = $connection->getDbAdapter();
$query = $db->select()
->from('director_deployment_log')
->where('stage_name IS NOT NULL')
->where('stage_collected IS NULL')
->where('startup_succeeded IS NULL')
->order('stage_name');
return static::loadAll($connection, $query, 'stage_name');
}
public static function hasUncollected(Db $connection)
{
$db = $connection->getDbAdapter();
$query = $db->select()
->from('director_deployment_log', ['cnt' => 'COUNT(*)'])
->where('stage_name IS NOT NULL')
->where('stage_collected IS NULL')
->where('startup_succeeded IS NULL');
return $db->fetchOne($query) > 0;
}
}