summaryrefslogtreecommitdiffstats
path: root/library/Director/Objects/DirectorDeploymentLog.php
blob: 0794a3c3fd98bcde66463cee20ad362c399fd90d (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php

namespace Icinga\Module\Director\Objects;

use Exception;
use Icinga\Exception\NotFoundError;
use Icinga\Module\Director\Core\CoreApi;
use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\Util;

class DirectorDeploymentLog extends DbObject
{
    protected $table = 'director_deployment_log';

    protected $keyName = 'id';

    protected $autoincKeyName = 'id';

    protected $config;

    protected $defaultProperties = [
        'id'                     => null,
        'config_checksum'        => null,
        'last_activity_checksum' => null,
        'peer_identity'          => null,
        'start_time'             => null,
        'end_time'               => null,
        'abort_time'             => null,
        'duration_connection'    => null,
        'duration_dump'          => null,
        'stage_name'             => null,
        'stage_collected'        => null,
        'connection_succeeded'   => null,
        'dump_succeeded'         => null,
        'startup_succeeded'      => null,
        'username'               => null,
        'startup_log'            => null,
    ];

    protected $binaryProperties = [
        'config_checksum',
        'last_activity_checksum'
    ];

    public function getConfigHexChecksum()
    {
        return bin2hex($this->config_checksum);
    }

    public function getConfig()
    {
        if ($this->config === null) {
            $this->config = IcingaConfig::load($this->config_checksum, $this->connection);
        }

        return $this->config;
    }

    public function isPending()
    {
        return $this->dump_succeeded === 'y' && $this->startup_log === null;
    }

    public function succeeded()
    {
        return $this->startup_succeeded === 'y';
    }

    public function configEquals(IcingaConfig $config)
    {
        return $this->config_checksum === $config->getChecksum();
    }

    public function getDeploymentTimestamp()
    {
        return strtotime($this->start_time);
    }

    public static function hasDeployments(Db $connection)
    {
        $db = $connection->getDbAdapter();
        $query = $db->select()->from(
            'director_deployment_log',
            array('c' => 'COUNT(*)')
        );

        return (int) $db->fetchOne($query) > 0;
    }

    public static function getConfigChecksumForStageName(Db $connection, $stage)
    {
        if ($stage === null) {
            return null;
        }
        $db = $connection->getDbAdapter();
        $query = $db->select()
            ->from(
                array('l' => 'director_deployment_log'),
                array('c' => $connection->dbHexFunc('l.config_checksum'))
            )->where('l.stage_name = ?');

        return $db->fetchOne($query, $stage);
    }

    /**
     * @param Db $connection
     * @return DirectorDeploymentLog
     * @throws NotFoundError
     */
    public static function loadLatest(Db $connection)
    {
        $db = $connection->getDbAdapter();
        $query = $db->select()->from(
            array('l' => 'director_deployment_log'),
            array('id' => 'MAX(l.id)')
        );

        return static::load($db->fetchOne($query), $connection);
    }

    /**
     * @param Db $connection
     * @return ?DirectorDeploymentLog
     */
    public static function optionalLatest(Db $connection)
    {
        try {
            return static::loadLatest($connection);
        } catch (NotFoundError $exception) {
            return null;
        }
    }

    /**
     * @param CoreApi $api
     * @param Db $connection
     * @return DirectorDeploymentLog
     */
    public static function getRelatedToActiveStage(CoreApi $api, Db $connection)
    {
        try {
            return static::requireRelatedToActiveStage($api, $connection);
        } catch (Exception $e) {
            return null;
        }
    }

    /**
     * @param CoreApi $api
     * @param Db $connection
     * @return DirectorDeploymentLog
     * @throws NotFoundError
     */
    public static function requireRelatedToActiveStage(CoreApi $api, Db $connection)
    {
        $stage = $api->getActiveStageName();

        if (! strlen($stage)) {
            throw new NotFoundError('Got no active stage name');
        }
        $db = $connection->getDbAdapter();
        $query = $db->select()->from(
            ['l' => 'director_deployment_log'],
            ['id' => 'MAX(l.id)']
        )->where('l.stage_name = ?', $stage);

        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;
    }
}