summaryrefslogtreecommitdiffstats
path: root/application/forms/SelfServiceSettingsForm.php
blob: 4471bcf473f7d18cb2476a51ef5b9bb219c5b7c3 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php

namespace Icinga\Module\Director\Forms;

use Exception;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Settings;
use Icinga\Module\Director\Web\Form\DirectorForm;

class SelfServiceSettingsForm extends DirectorForm
{
    /** @var Settings */
    protected $settings;

    public function setup()
    {
        $settings = $this->settings;
        $this->addElement('select', 'agent_name', [
            'label'       => $this->translate('Host Name'),
            'description' => $this->translate(
                'What to use as your Icinga 2 Agent\'s Host Name'
            ),
            'multiOptions' => [
                'fqdn'     => $this->translate('Fully qualified domain name (FQDN)'),
                'hostname' => $this->translate('Host name (local part, without domain)'),
            ],
            'value'  => $settings->getStoredOrDefaultValue('self-service/agent_name')
        ]);

        $this->addElement('select', 'transform_hostname', [
            'label'       => $this->translate('Transform Host Name'),
            'description' => $this->translate(
                'Whether to adjust your host name'
            ),
            'multiOptions' => [
                '0'   => $this->translate('Do not transform at all'),
                '1'   => $this->translate('Transform to lowercase'),
                '2'   => $this->translate('Transform to uppercase'),
            ],
            'value'  => $settings->getStoredOrDefaultValue('self-service/transform_hostname')
        ]);
        $this->addElement('select', 'resolve_parent_host', [
            'label'       => $this->translate('Transform Parent Host to IP'),
            'description' => $this->translate(
                'This is only important in case your master/satellite nodes do not'
                . ' have IP addresses as their "host" property. The Agent can be'
                . ' told to issue related DNS lookups on it\' own'
            ),
            'multiOptions' => [
                '0'   => $this->translate("Don't care, my host settings are fine"),
                '1'   => $this->translate('My Agents should use DNS to look up Endpoint names'),
            ],
            'value'  => $settings->getStoredOrDefaultValue('self-service/resolve_parent_host')
        ]);

        $this->addElement('extensibleSet', 'global_zones', [
            'label'       => $this->translate('Global Zones'),
            'description' => $this->translate(
                'To ensure downloaded packages are build by the Icinga Team'
                . ' and not compromised by third parties, you will be able'
                . ' to provide an array of SHA1 hashes here. In case you have'
                . ' defined any hashses, the module will not continue with'
                . ' updating / installing the Agent in case the SHA1 hash of'
                . ' the downloaded MSI package is not matching one of the'
                . ' provided hashes of this setting'
            ),
            'multiOptions' => $this->enumGlobalZones(),
            'value'  => $settings->getStoredOrDefaultValue('self-service/global_zones'),
        ]);

        $this->addElement('select', 'download_type', [
            'label'       => $this->translate('Installation Source'),
            'description' => $this->translate(
                'You might want to let the generated Powershell script install'
                . ' the Icinga 2 Agent in an automated way. If so, please choose'
                . ' where your Windows nodes should fetch the Agent installer'
            ),
            'multiOptions' => [
                null       => $this->translate('- no automatic installation -'),
                // TODO: not yet
                // 'director' => $this->translate('Download via the Icinga Director'),
                'icinga'   => $this->translate('Download from packages.icinga.com'),
                'url'      => $this->translate('Download from a custom url'),
                'file'     => $this->translate('Use a local file or network share'),
            ],
            'value'  => $settings->getStoredOrDefaultValue('self-service/download_type'),
            'class' => 'autosubmit'
        ]);

        $downloadType = $this->getSentValue(
            'download_type',
            $settings->getStoredOrDefaultValue('self-service/download_type')
        );

        if ($downloadType) {
            $this->addInstallSettings($downloadType, $settings);
        }

        $this->addEventuallyConfiguredBoolean('flush_api_dir', [
            'label'       => $this->translate('Flush API directory'),
            'description' => $this->translate(
                'In case the Icinga Agent will accept configuration from the parent'
                . ' Icinga 2 system, it will possibly write data to /var/lib/icinga2/api/*.'
                . ' By setting this parameter to true, all content inside the api directory'
                . ' will be flushed before an eventual restart of the Icinga 2 Agent'
            ),
            'required' => true,
        ]);
    }

    protected function addInstallSettings($downloadType, Settings $settings)
    {
        $this->addElement('text', 'download_url', [
            'label'       => $this->translate('Source Path'),
            'description' => $this->translate(
                'Define a download Url or local directory from which the a specific'
                . ' Icinga 2 Agent MSI Installer package should be fetched. Please'
                . ' ensure to only define the base download Url or Directory. The'
                . ' Module will generate the MSI file name based on your operating'
                . ' system architecture and the version to install. The Icinga 2 MSI'
                . ' Installer name is internally build as follows:'
                . ' Icinga2-v[InstallAgentVersion]-[OSArchitecture].msi (full example:'
                . ' Icinga2-v2.6.3-x86_64.msi)'
            ),
            'value'  => $settings->getStoredOrDefaultValue('self-service/download_url'),
        ]);

        // TODO: offer to check for available versions
        if ($downloadType === 'icinga') {
            $el = $this->getElement('download_url');
            $el->setAttrib('disabled', 'disabled');
            $value = 'https://packages.icinga.com/windows/';
            $el->setValue($value);
            $this->setSentValue('download_url', $value);
        }
        if ($downloadType === 'director') {
            $el = $this->getElement('download_url');
            $el->setAttrib('disabled', 'disabled');

            $r = $this->getRequest();
            $scheme = $r->getServer('HTTP_X_FORWARDED_PROTO', $r->getScheme());

            $value = sprintf(
                '%s://%s%s/director/download/windows/',
                $scheme,
                $r->getHttpHost(),
                $this->getRequest()->getBaseUrl()
            );
            $el->setValue($value);
            $this->setSentValue('download_url', $value);
        }

        $this->addElement('text', 'agent_version', [
            'label'       => $this->translate('Agent Version'),
            'description' => $this->translate(
                'In case the Icinga 2 Agent should be automatically installed,'
                . ' this has to be a string value like: 2.6.3'
            ),
            'value'  => $settings->getStoredOrDefaultValue('self-service/agent_version'),
            'required' => true,
        ]);

        $hashes = $settings->getStoredOrDefaultValue('self-service/installer_hashes');
        $this->addElement('extensibleSet', 'installer_hashes', [
            'label'       => $this->translate('Installer Hashes'),
            'description' => $this->translate(
                'To ensure downloaded packages are build by the Icinga Team'
                . ' and not compromised by third parties, you will be able'
                . ' to provide an array of SHA1 hashes here. In case you have'
                . ' defined any hashses, the module will not continue with'
                . ' updating / installing the Agent in case the SHA1 hash of'
                . ' the downloaded MSI package is not matching one of the'
                . ' provided hashes of this setting'
            ),
            'value'  => $hashes,
        ]);

        $this->addElement('text', 'icinga_service_user', [
            'label'       => $this->translate('Service User'),
            'description' => $this->translate(
                'The user that should run the Icinga 2 service on Windows.'
            ),
            'value'  => $settings->getStoredOrDefaultValue('self-service/icinga_service_user'),
        ]);

        $this->addEventuallyConfiguredBoolean('allow_updates', [
            'label'        => $this->translate('Allow Updates'),
            'description' => $this->translate(
                'In case the Icinga 2 Agent is already installed on the system,'
                . ' this parameter will allow you to configure if you wish to'
                . ' upgrade / downgrade to a specified version with the as well.'
            ),
            'required' => true,
        ]);

        $this->addNscpSettings();
    }

    protected function addNscpSettings()
    {
        $this->addEventuallyConfiguredBoolean('install_nsclient', [
            'label'       => $this->translate('Install NSClient++'),
            'description' => $this->translate(
                'Also install NSClient++. It can be used through the Icinga Agent'
                . ' and comes with a bunch of additional Check Plugins'
            ),
            'required' => true,
        ]);
        /*
         * TODO: eventually add those:
        if ($settings->get('self-service/install_nsclient') === 'y') {
            $params['install_nsclient'] = true;
            $this->addBooleanSettingsToParams($settings, [
                'nsclient_add_defaults',
                'nsclient_firewall',
                'nsclient_service',
            ], $params);


            $this->addStringSettingsToParams($settings, [
                'nsclient_directory',
                'nsclient_installer_path'
            ], $params);
        }
        */
    }

    public static function create(Db $db, Settings $settings)
    {
        return static::load()->setDb($db)->setSettings($settings);
    }

    protected function addEventuallyConfiguredBoolean($name, $params)
    {
        $key = "self-service/$name";
        $value = $this->settings->getStoredValue($key);
        $params['value'] = $value;
        $params['multiOptions'] = $this->eventuallyConfiguredEnum($name, [
            'y' => $this->translate('Yes'),
            'n' => $this->translate('No'),
        ]);

        return $this->addElement('select', $name, $params);
    }

    protected function eventuallyConfiguredEnum($name, $enum)
    {
        $key = "self-service/$name";
        $default = $this->settings->getDefaultValue($key);
        if ($default === null) {
            return [
                null => $this->translate('- please choose -')
            ] + $enum;
        } else {
            return [
                null => sprintf($this->translate('%s (default)'), $enum[$default])
            ] + $enum;
        }
    }

    protected function setSentValue($key, $value)
    {
        $this->getRequest()->setPost($key, $value);
        return $this;
    }

    protected function enumGlobalZones()
    {
        $db = $this->getDb()->getDbAdapter();
        $zones = $db->fetchCol(
            $db->select()->from('icinga_zone', 'object_name')
                ->where('disabled = ?', 'n')
                ->where('is_global = ?', 'y')
                ->order('object_name')
        );

        return array_combine($zones, $zones);
    }

    public function setSettings(Settings $settings)
    {
        $this->settings = $settings;
        return $this;
    }

    public function onSuccess()
    {
        try {
            foreach ($this->getValues() as $key => $value) {
                if ($value === '') {
                    $value = null;
                }

                $this->settings->set("self-service/$key", $value);
            }

            $this->setSuccessMessage($this->translate(
                'Self Service Settings have been stored'
            ));

            parent::onSuccess();
        } catch (Exception $e) {
            $this->addException($e);
        }
    }
}