summaryrefslogtreecommitdiffstats
path: root/application/forms/DeployConfigForm.php
blob: 0b817fa0486be79ae14ac3d320f4af2302e6bb81 (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
<?php

namespace Icinga\Module\Director\Forms;

use Icinga\Exception\IcingaException;
use Icinga\Module\Director\Core\DeploymentApiInterface;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\Web\Form\DirectorForm;

class DeployConfigForm extends DirectorForm
{
    use DeployFormsBug7530;

    /** @var DeploymentApiInterface */
    private $api;

    /** @var string */
    private $checksum;

    /** @var int */
    private $deploymentId;

    public function init()
    {
        $this->setAttrib('class', 'inline');
    }

    public function setup()
    {
        $activities = $this->db->countActivitiesSinceLastDeployedConfig();
        if ($this->deploymentId) {
            $label = $this->translate('Re-deploy now');
        } elseif ($activities === 0) {
            $label = $this->translate('There are no pending changes. Deploy anyway');
        } else {
            $label = sprintf(
                $this->translate('Deploy %d pending changes'),
                $activities
            );
        }

        if ($this->deploymentId) {
            $deployIcon = 'reply-all';
        } else {
            $deployIcon = 'forward';
        }

        $this->addHtml(
            $this->getView()->icon(
                $deployIcon,
                $label,
                array('class' => 'link-color')
            ) . '<nobr>'
        );

        $el = $this->createElement('submit', 'btn_deploy', array(
            'label' => $label,
            'escape' => false,
            'decorators' => array('ViewHelper'),
            'class' => 'link-button ' . $deployIcon,
            ));

        $this->addHtml('</nobr>');
        $this->submitButtonName = $el->getName();
        $this->setSubmitLabel($label);
        $this->addElement($el);
    }

    public function onSuccess()
    {
        if ($this->skipBecauseOfBug7530()) {
            return;
        }

        $db = $this->db;
        $msg = $this->translate('Config has been submitted, validation is going on');
        $this->setSuccessMessage($msg);

        $isApiRequest = $this->getRequest()->isApiRequest();
        if ($this->checksum) {
            $config = IcingaConfig::load(hex2bin($this->checksum), $db);
        } else {
            $config = IcingaConfig::generate($db);
        }

        $this->api->wipeInactiveStages($db);

        if ($this->api->dumpConfig($config, $db)) {
            if ($isApiRequest) {
                die('Api not ready');
               //  return $this->sendJson((object) array('checksum' => $checksum));
            } else {
                $this->setSuccessUrl('director/config/deployments');
                $this->setSuccessMessage(
                    $this->translate('Config has been submitted, validation is going on')
                );
            }
            parent::onSuccess();
        } else {
            throw new IcingaException($this->translate('Config deployment failed'));
        }
    }

    public function setChecksum($checksum)
    {
        $this->checksum = $checksum;
        return $this;
    }

    public function setDeploymentId($id)
    {
        $this->deploymentId = $id;
        return $this;
    }

    public function setApi(DeploymentApiInterface $api)
    {
        $this->api = $api;
        return $this;
    }
}