SystemLoader::startMikoPBX()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 62
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 62
rs 9.312
c 0
b 0
f 0
cc 4
nc 4
nop 0

How to fix   Long Method   

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