summaryrefslogtreecommitdiffstats
path: root/library/Director/Deployment/DeploymentStatus.php
blob: ae850c61b3325a38b0b85788148a5bef8be46be6 (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
<?php

namespace Icinga\Module\Director\Deployment;

use Exception;
use Icinga\Module\Director\Core\CoreApi;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\DirectorDeploymentLog;

class DeploymentStatus
{
    protected $db;

    protected $api;

    public function __construct(Db $db, CoreApi $api)
    {
        $this->db = $db;
        $this->api = $api;
    }

    public function getDeploymentStatus($configs = null, $activities = null)
    {
        try {
            if (DirectorDeploymentLog::hasUncollected($this->db)) {
                $this->api->collectLogFiles($this->db);
            }
        } catch (Exception $e) {
            // Ignore eventual issues while talking to Icinga
        }

        $activeConfiguration = null;
        $lastActivityLogChecksum = null;
        $configChecksum = null;
        if ($stageName = $this->api->getActiveStageName()) {
            $activityLogChecksum = DirectorDeploymentLog::getRelatedToActiveStage($this->api, $this->db);
            if ($activityLogChecksum === null) {
                $activeConfiguration = [
                    'stage_name' => $stageName,
                    'config'   => null,
                    'activity' => null
                ];
            } else {
                $lastActivityLogChecksum = bin2hex($activityLogChecksum->get('last_activity_checksum'));
                $configChecksum = $this->getConfigChecksumForStageName($stageName);
                $activeConfiguration = [
                    'stage_name' => $stageName,
                    'config'   => ($configChecksum) ? : null,
                    'activity' => $lastActivityLogChecksum
                ];
            }
        }
        $result = [
            'active_configuration' => (object) $activeConfiguration,
        ];

        if ($configs) {
            $result['configs'] = (object) $this->getDeploymentStatusForConfigChecksums(
                explode(',', $configs),
                $configChecksum
            );
        }

        if ($activities) {
            $result['activities'] = (object) $this->getDeploymentStatusForActivityLogChecksums(
                explode(',', $activities),
                $lastActivityLogChecksum
            );
        }
        return (object) $result;
    }

    public function getConfigChecksumForStageName($stageName)
    {
        $db = $this->db->getDbAdapter();
        $query = $db->select()->from(
            ['l' => 'director_deployment_log'],
            ['checksum' => $this->db->dbHexFunc('l.config_checksum')]
        )->where('l.stage_name = ?', $stageName);

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

    public function getDeploymentStatusForConfigChecksums($configChecksums, $activeConfigChecksum)
    {
        $db = $this->db->getDbAdapter();
        $results = array_combine($configChecksums, array_map(function () {
            return 'unknown';
        }, $configChecksums));
        $binaryConfigChecksums = [];
        foreach ($configChecksums as $singleConfigChecksum) {
            $binaryConfigChecksums[$singleConfigChecksum] = $this->db->quoteBinary(hex2bin($singleConfigChecksum));
        }
        $deployedConfigs = $this->getDeployedConfigs(array_values($binaryConfigChecksums));

        foreach ($results as $singleChecksum => &$status) {
            // active if it's equal to the provided active
            if ($singleChecksum === $activeConfigChecksum) {
                $status = 'active';
            } else {
                if (isset($deployedConfigs[$singleChecksum])) {
                    $status = ($deployedConfigs[$singleChecksum] === 'y') ? 'deployed' : 'failed';
                } else {
                    // check if it's in generated_config table it is undeployed
                    $generatedConfigQuery = $db->select()->from(
                        ['g' => 'director_generated_config'],
                        ['checksum' => 'g.checksum']
                    )->where('g.checksum = ?', $binaryConfigChecksums[$singleChecksum]);
                    if ($db->fetchOne($generatedConfigQuery)) {
                        $status = 'undeployed';
                    }
                }
                // otherwise leave unknown
            }
        }

        return $results;
    }

    public function getDeploymentStatusForActivityLogChecksums($activityLogChecksums, $activeActivityLogChecksum)
    {
        $db = $this->db->getDbAdapter();
        $results = array_combine($activityLogChecksums, array_map(function () {
            return 'unknown';
        }, $activityLogChecksums));

        foreach ($results as $singleActivityLogChecksum => &$status) {
            // active if it's equal to the provided active
            if ($singleActivityLogChecksum === $activeActivityLogChecksum) {
                $status = 'active';
            } else {
                // get last deployed activity id and check if it's less than the passed one
                $generatedConfigQuery = $db->select()->from(
                    ['a' => 'director_activity_log'],
                    ['id' => 'a.id']
                )->where('a.checksum = ?', $this->db->quoteBinary(hex2bin($singleActivityLogChecksum)));
                if ($singleActivityLogData = $db->fetchOne($generatedConfigQuery)) {
                    if ($lastDeploymentActivityLogId = $this->db->getLastDeploymentActivityLogId()) {
                        if ((int) $singleActivityLogData > $lastDeploymentActivityLogId) {
                            $status = 'undeployed';
                        } else {
                            $status = 'deployed';
                        }
                    }
                }
            }
        }
        return $results;
    }

    /**
     * @param array $binaryConfigChecksums
     * @return array
     */
    public function getDeployedConfigs(array $binaryConfigChecksums)
    {
        $db = $this->db->getDbAdapter();
        $deploymentLogQuery = $db->select()->from(['l' => 'director_deployment_log'], [
            'checksum' => $this->db->dbHexFunc('l.config_checksum'),
            'deployed' => 'l.startup_succeeded'
        ])->where('l.config_checksum IN (?)', $binaryConfigChecksums);
        return $db->fetchPairs($deploymentLogQuery);
    }
}