Passed
Push — develop ( 0816d1...716ffe )
by Портнов
05:11
created

SystemLoader::getInfoMessage()   C

Complexity

Conditions 16
Paths 144

Size

Total Lines 91
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 58
c 0
b 0
f 0
dl 0
loc 91
rs 5.1999
cc 16
nc 144
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace MikoPBX\Core\System;
21
22
use MikoPBX\Common\Models\LanInterfaces;
23
use MikoPBX\Common\Models\PbxSettings;
24
use MikoPBX\Common\Models\PbxSettingsConstants;
25
use MikoPBX\Common\Providers\RegistryProvider;
26
use MikoPBX\Core\Asterisk\Configs\SIPConf;
27
use MikoPBX\Core\System\Configs\ACPIDConf;
28
use MikoPBX\Core\System\Configs\BeanstalkConf;
29
use MikoPBX\Core\System\Configs\CronConf;
30
use MikoPBX\Core\System\Configs\IptablesConf;
31
use MikoPBX\Core\System\Configs\NatsConf;
32
use MikoPBX\Core\System\Configs\NginxConf;
33
use MikoPBX\Core\System\Configs\NTPConf;
34
use MikoPBX\Core\System\Configs\PHPConf;
35
use MikoPBX\Core\System\Configs\RedisConf;
36
use MikoPBX\Core\System\Configs\SentryConf;
37
use MikoPBX\Core\System\Configs\SSHConf;
38
use MikoPBX\Core\System\Configs\SyslogConf;
39
use MikoPBX\Core\System\Configs\VmToolsConf;
40
use MikoPBX\Core\System\Upgrade\UpdateDatabase;
41
use MikoPBX\Core\System\Upgrade\UpdateSystemConfig;
42
use Phalcon\Di;
43
44
/**
45
 * SystemLoader class
46
 *
47
 * This class is responsible for loading the system services.
48
 *
49
 * @package MikoPBX\Core\System
50
 * @property \Phalcon\Config config
51
 */
52
class SystemLoader extends Di\Injectable
53
{
54
    /**
55
     * Message displayed during the start of a stage.
56
     *
57
     * @var string
58
     */
59
    private string $stageMessage = '';
60
61
    /**
62
     * Check if the system is running in Docker
63
     *
64
     * @var bool
65
     */
66
    private bool $isDocker = false;
67
68
69
    /**
70
     * Check if the system is running from live cd
71
     *
72
     * @var bool
73
     */
74
    private bool $isRecoveryMode = false;
75
76
77
    /**
78
     * Constructor
79
     */
80
    public function __construct()
81
    {
82
        $this->isDocker = Util::isDocker();
83
        $this->isRecoveryMode = Util::isRecoveryMode();
84
    }
85
86
87
    /**
88
     * Echoes the starting message for a stage.
89
     *
90
     * @param string $message The message to echo.
91
     */
92
    private function echoStartMsg(string $message): void
93
    {
94
        SystemMessages::echoStartMsg($message);
95
        $this->stageMessage = $message;
96
    }
97
98
    /**
99
     * Echoes the result message for a stage.
100
     *
101
     * @param string $result The result of the stage.
102
     */
103
    private function echoResultMsg(string $result = SystemMessages::RESULT_DONE): void
104
    {
105
        SystemMessages::echoResultMsg($this->stageMessage, $result);
106
        $this->stageMessage = '';
107
    }
108
109
    /**
110
     * Starts the system services.
111
     *
112
     * @return bool True on success, false otherwise.
113
     */
114
    public function startSystem(): bool
115
    {
116
        $system = new System();
117
        // Is the configuration default?
118
        // Try restore config...
119
        if ($system->isDefaultConf() && !$this->isRecoveryMode) {
120
            $this->echoStartMsg(' - Try restore backup of settings... ');
121
            $system->tryRestoreConf();
122
            $this->echoResultMsg();
123
        }
124
125
        // Check if the system is running on T2SDELinux
126
        $itIsT2SDELinux = Util::isT2SdeLinux();
127
128
        // Mark the registry as booting
129
        $this->di->getShared(RegistryProvider::SERVICE_NAME)->booting = true;
130
131
        // Start the ACPID daemon
132
        $this->echoStartMsg(PHP_EOL);
133
        $this->echoStartMsg(' - Start acpid daemon...');
134
        $ACPIDConf = new ACPIDConf();
135
        $ACPIDConf->reStart();
136
        $this->echoResultMsg();
137
138
        // Start the Beanstalkd daemon
139
        $this->echoStartMsg(' - Start beanstalkd daemon...');
140
        $beanstalkConf = new BeanstalkConf();
141
        $beanstalkConf->reStart();
142
        $this->echoResultMsg();
143
144
        // Start the Redis daemon
145
        $this->echoStartMsg(' - Start redis daemon...');
146
        $redisConf = new RedisConf();
147
        $redisConf->reStart();
148
        $this->echoResultMsg();
149
150
        // Configure Sentry error logger
151
        $this->echoStartMsg(' - Configuring sentry error logger ...');
152
        if (!$this->isRecoveryMode) {
153
            $sentryConf = new SentryConf();
154
            $sentryConf->configure();
155
            $this->echoResultMsg();
156
        } else {
157
            $this->echoResultMsg(SystemMessages::RESULT_SKIPPED);
158
        }
159
160
        // Configure the system timezone
161
        $this->echoStartMsg(' - Configuring timezone...');
162
        if (!$this->isRecoveryMode) {
163
            System::timezoneConfigure();
164
            $this->echoResultMsg();
165
        } else {
166
            $this->echoResultMsg(SystemMessages::RESULT_SKIPPED);
167
        }
168
169
        // Mount the storage disk
170
        $storage = new Storage();
171
        if ($itIsT2SDELinux) {
172
            // Do not need to set on Docker or Debian linux
173
            $storage->saveFstab();
174
        }
175
        $storage->configure();
176
        $this->echoStartMsg(' - Mount storage disk...');
177
        $this->echoResultMsg();
178
179
        // Additional tasks for T2SDELinux
180
        if ($itIsT2SDELinux) {
181
            $this->echoStartMsg(' - Connect swap...');
182
            Processes::mwExecBg('/etc/rc/connect_swap');
183
            $this->echoResultMsg();
184
        }
185
186
        // Start the syslogd daemon
187
        $this->echoStartMsg(' - Start syslogd daemon...');
188
        $syslogConf = new SyslogConf();
189
        $syslogConf->reStart();
190
        $this->echoResultMsg();
191
192
        // Update the database structure
193
        $dbUpdater = new UpdateDatabase();
194
        $dbUpdater->updateDatabaseStructure();
195
196
        // Create directories required by modules after DB upgrade
197
        $this->echoStartMsg(' - Create modules links and folders...');
198
        $storage->createWorkDirsAfterDBUpgrade();
199
        $this->echoResultMsg();
200
201
        // Update the system configuration and applications
202
        $this->echoStartMsg(' - Update configs and applications...' . PHP_EOL);
203
        $confUpdate = new UpdateSystemConfig();
204
        $confUpdate->updateConfigs();
205
        $this->echoStartMsg(' - Update configs...');
206
        $this->echoResultMsg();
207
208
        // Configure VM tools
209
        $this->echoStartMsg(' - Configuring VM tools...');
210
        if (!$this->isRecoveryMode) {
211
            $vmwareTools = new VmToolsConf();
212
            $resultVMTools = $vmwareTools->configure();
213
            $this->echoResultMsg((string)$resultVMTools);
214
        } else {
215
            $this->echoResultMsg(SystemMessages::RESULT_SKIPPED);
216
        }
217
218
        // Configure the system hostname
219
        $this->echoStartMsg(' - Configuring hostname...');
220
        $network = new Network();
221
        $network->hostnameConfigure();
222
        $this->echoResultMsg();
223
224
        // Generate resolv.conf
225
        $this->echoStartMsg(' - Configuring resolv.conf...');
226
        $network->resolvConfGenerate();
227
        $this->echoResultMsg();
228
229
        // Configure LAN interface
230
        $this->echoStartMsg(' - Configuring LAN interface...');
231
        if ($this->isDocker) {
232
            $network->configureLanInDocker();
233
        } else {
234
            $network->lanConfigure();
235
        }
236
        $this->echoResultMsg();
237
238
        // SSL rehash
239
        $this->echoStartMsg(' - SSL rehash...');
240
        System::sslRehash();
241
        $this->echoResultMsg();
242
243
        // Configure the firewall
244
        $this->echoStartMsg(' - Configuring Firewall...');
245
        $firewall = new IptablesConf();
246
        $firewall->applyConfig();
247
        $this->echoResultMsg();
248
249
        // Configure NTP
250
        $this->echoStartMsg(' - Configuring ntpd...');
251
        if (!$this->isRecoveryMode) {
252
            NTPConf::configure();
253
            $this->echoResultMsg();
254
        } else {
255
            $this->echoResultMsg(SystemMessages::RESULT_SKIPPED);
256
        }
257
258
        // Do not need to set Debian SSH service
259
        if (!Util::isSystemctl()) {
260
            $this->echoStartMsg(' - Configuring SSH console...');
261
            $sshConf = new SSHConf();
262
            $resSsh = $sshConf->configure();
263
            $this->echoResultMsg((string)$resSsh);
264
        }
265
266
        // Start cloud provisioning
267
        if (!$this->isDocker && !$this->isRecoveryMode) {
268
            $this->echoStartMsg(' - Attempt to cloud provisioning...' . PHP_EOL);
269
            CloudProvisioning::start();
270
        } else {
271
            $this->echoStartMsg(' - Attempt to cloud provisioning...');
272
            $this->echoResultMsg(SystemMessages::RESULT_SKIPPED);
273
        }
274
275
        // Connect storage in a cloud if needed
276
        $this->echoStartMsg(' - Auto connect storage for a cloud ...');
277
        if (!$this->isDocker && !$this->isRecoveryMode) {
278
            $connectResult = Storage::connectStorageInCloud();
279
            $this->echoResultMsg($connectResult);
280
        } else {
281
            $this->echoResultMsg(SystemMessages::RESULT_SKIPPED);
282
        }
283
284
        // Update external IP if needed
285
        if (!$this->isRecoveryMode) {
286
            $this->echoStartMsg(' - Update external IP...');
287
            $network->updateExternalIp();
288
            $this->echoResultMsg();
289
        } else {
290
            $this->echoResultMsg(SystemMessages::RESULT_SKIPPED);
291
        }
292
        $this->di->getShared(RegistryProvider::SERVICE_NAME)->booting = false;
293
294
        return true;
295
    }
296
297
    /**
298
     * Load Asterisk and Web interface
299
     *
300
     * @return bool
301
     */
302
    public function startMikoPBX(): bool
303
    {
304
        $this->di->getShared(RegistryProvider::SERVICE_NAME)->booting = true;
305
306
        // Start the NATS queue daemon
307
        $this->echoStartMsg(' - Start nats queue daemon...');
308
        $natsConf = new NatsConf();
309
        $natsConf->reStart();
310
        $this->echoResultMsg();
311
312
        // Start the PHP-FPM daemon
313
        $this->echoStartMsg(' - Start php-fpm daemon...');
314
        PHPConf::reStart();
315
        $this->echoResultMsg();
316
317
        // Configure Asterisk and start it
318
        $this->echoStartMsg(' - Configuring Asterisk...' . PHP_EOL);
319
        $pbx = new PBX();
320
        $pbx->configure();
321
322
        $this->echoStartMsg(' - Start Asterisk...');
323
        $pbx->start();
324
        $this->echoResultMsg();
325
326
        // Wait for Asterisk to fully boot and reload SIP settings
327
        $this->echoStartMsg(' - Wait asterisk fully booted...');
328
        $asteriskResult = PBX::waitFullyBooted();
329
        $this->echoResultMsg((string)$asteriskResult);
330
        if ($asteriskResult) {
331
            $this->echoStartMsg(' - Reload SIP settings in AstDB...');
332
            $sip = new SIPConf();
333
            $sip->updateAsteriskDatabase();
334
            $this->echoResultMsg();
335
        }
336
337
        // Configure and restart cron tasks
338
        $this->echoStartMsg(' - Configuring Cron tasks...');
339
        $cron = new CronConf();
340
        $cron->reStart();
341
        $this->echoResultMsg();
342
343
        // Start the Nginx daemon
344
        $this->echoStartMsg(' - Start Nginx daemon...');
345
        $nginx = new NginxConf();
346
        $nginx->generateConf();
347
        $nginx->reStart();
348
        $this->echoResultMsg();
349
350
        // Display network information
351
        $this->echoStartMsg(SystemMessages::getInfoMessage(true));
352
353
        $this->di->getShared(RegistryProvider::SERVICE_NAME)->booting = false;
354
355
        return true;
356
    }
357
358
359
}