summaryrefslogtreecommitdiffstats
path: root/library/Director/Dashboard/Dashlet/DeploymentDashlet.php
blob: 83b4ceaa5c1cb963fd09ce14e42e744c20fe2ab5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php

namespace Icinga\Module\Director\Dashboard\Dashlet;

use Exception;
use Icinga\Module\Director\Auth\Permission;
use Icinga\Module\Director\Objects\DirectorDeploymentLog;

class DeploymentDashlet extends Dashlet
{
    protected $icon = 'wrench';

    protected $undeployedActivities;

    protected $lastDeployment;

    public function getTitle()
    {
        return $this->translate('Config Deployment');
    }

    public function hasUndeployedActivities()
    {
        return $this->undeployedActivities() > 0;
    }

    public function undeployedActivities()
    {
        if ($this->undeployedActivities === null) {
            try {
                $this->undeployedActivities = $this->db
                    ->countActivitiesSinceLastDeployedConfig();
            } catch (Exception $e) {
                $this->undeployedActivities = 0;
            }
        }

        return $this->undeployedActivities;
    }

    public function lastDeploymentFailed()
    {
        return ! $this->lastDeployment()->succeeded();
    }

    public function lastDeploymentPending()
    {
        return $this->lastDeployment()->isPending();
    }

    public function listCssClasses()
    {
        try {
            if ($this->lastDeploymentFailed()) {
                return array('state-critical');
            } elseif ($this->lastDeploymentPending()) {
                return array('state-pending');
            } elseif ($this->hasUndeployedActivities()) {
                return array('state-warning');
            } else {
                return array('state-ok');
            }
        } catch (Exception $e) {
            return null;
        }
    }

    protected function lastDeployment()
    {
        if ($this->lastDeployment === null) {
            $this->lastDeployment = DirectorDeploymentLog::loadLatest($this->db);
        }

        return $this->lastDeployment;
    }

    public function getSummary()
    {
        $msgs = array();
        $cnt = $this->undeployedActivities();

        try {
            if ($this->lastDeploymentFailed()) {
                $msgs[] = $this->translate('The last deployment did not succeed');
            } elseif ($this->lastDeploymentPending()) {
                $msgs[] = $this->translate('The last deployment is currently pending');
            }
        } catch (Exception $e) {
        }

        if ($cnt === 0) {
            $msgs[] = $this->translate('There are no pending changes');
        } else {
            $msgs[] = sprintf(
                $this->translate(
                    'A total of %d config changes happened since your last'
                    . ' deployed config has been rendered'
                ),
                $cnt
            );
        }

        return implode('. ', $msgs) . '.';
    }

    public function getUrl()
    {
        return 'director/config/deployments';
    }

    public function listRequiredPermissions()
    {
        return [Permission::DEPLOY];
    }
}