Passed
Push — develop ( 2d3c3b...ea7628 )
by Nikolay
04:49
created

SystemLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Providers\RegistryProvider;
23
use MikoPBX\Core\Asterisk\Configs\SIPConf;
24
use MikoPBX\Core\System\Configs\ACPIDConf;
25
use MikoPBX\Core\System\Configs\BeanstalkConf;
26
use MikoPBX\Core\System\Configs\CronConf;
27
use MikoPBX\Core\System\Configs\IptablesConf;
28
use MikoPBX\Core\System\Configs\NatsConf;
29
use MikoPBX\Core\System\Configs\NginxConf;
30
use MikoPBX\Core\System\Configs\NTPConf;
31
use MikoPBX\Core\System\Configs\PHPConf;
32
use MikoPBX\Core\System\Configs\RedisConf;
33
use MikoPBX\Core\System\Configs\SentryConf;
34
use MikoPBX\Core\System\Configs\SSHConf;
35
use MikoPBX\Core\System\Configs\SyslogConf;
36
use MikoPBX\Core\System\Configs\VmToolsConf;
37
use MikoPBX\Core\System\Upgrade\UpdateDatabase;
38
use MikoPBX\Core\System\Upgrade\UpdateSystemConfig;
39
use Phalcon\Di;
40
41
/**
42
 * SystemLoader class
43
 *
44
 * This class is responsible for loading the system services.
45
 *
46
 * @package MikoPBX\Core\System
47
 * @property \Phalcon\Config config
48
 */
49
50
class SystemLoader extends Di\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
65
    private bool $isDocker = false;
66
67
    /**
68
     * Constructor
69
     */
70
    public function __construct()
71
    {
72
        $this->isDocker = Util::isDocker();
73
    }
74
75
76
    /**
77
     * Echoes the starting message for a stage.
78
     *
79
     * @param string $message The message to echo.
80
     */
81
    private function echoStartMsg(string $message):void
82
    {
83
        $this->stageMessage = $message;
84
        if (!$this->isDocker){
85
            SystemMessages::echoToTeletype($this->stageMessage);
86
        }
87
        SystemMessages::echoWithSyslog($this->stageMessage);
88
    }
89
90
    /**
91
     * Echoes the result message for a stage.
92
     *
93
     * @param string $result The result of the stage.
94
     */
95
    private function echoResultMsg(string $result = SystemMessages::RESULT_DONE):void
96
    {
97
        SystemMessages::echoResult($this->stageMessage, $result);
98
        if (!$this->isDocker){
99
            SystemMessages::teletypeEchoResult($this->stageMessage, $result);
100
        }
101
        $this->stageMessage = '';
102
    }
103
104
    /**
105
     * Starts the system services.
106
     *
107
     * @return bool True on success, false otherwise.
108
     */
109
    public function startSystem(): bool
110
    {
111
        $system = new System();
112
        // Is the configuration default?
113
        // Try restore config...
114
        if($system->isDefaultConf() && !file_exists('/offload/livecd')){
115
            $this->echoStartMsg(' - Try restore backup of settings... ');
116
            $system->tryRestoreConf();
117
            $this->echoResultMsg();
118
        }
119
120
        // Check if the system is running on T2SDELinux
121
        $itIsT2SDELinux = Util::isT2SdeLinux();
122
123
        // Mark the registry as booting
124
        $this->di->getShared(RegistryProvider::SERVICE_NAME)->booting = true;
125
126
        // Start the ACPID daemon
127
        $this->echoStartMsg(PHP_EOL);
128
        $this->echoStartMsg(' - Start acpid daemon...');
129
        $ACPIDConf = new ACPIDConf();
130
        $ACPIDConf->reStart();
131
        $this->echoResultMsg();
132
133
        // Start the Beanstalkd daemon
134
        $this->echoStartMsg(' - Start beanstalkd daemon...');
135
        $beanstalkConf = new BeanstalkConf();
136
        $beanstalkConf->reStart();
137
        $this->echoResultMsg();
138
139
        // Start the Redis daemon
140
        $this->echoStartMsg(' - Start redis daemon...');
141
        $redisConf = new RedisConf();
142
        $redisConf->reStart();
143
        $this->echoResultMsg();
144
145
        // Configure Sentry error logger
146
        $this->echoStartMsg(' - Configuring sentry error logger ...');
147
        $sentryConf = new SentryConf();
148
        $sentryConf->configure();
149
        $this->echoResultMsg();
150
151
        // Configure the system timezone
152
        $this->echoStartMsg(' - Configuring timezone...');
153
        $system::timezoneConfigure();
154
        $this->echoResultMsg();
155
156
        // Mount the storage disk
157
        $storage       = new Storage();
158
        if($itIsT2SDELinux){
159
            // Do not need to set on Docker or Debian linux
160
            $storage->saveFstab();
161
        }
162
        $storage->configure();
163
        $this->echoStartMsg(' - Mount storage disk...');
164
        $this->echoResultMsg();
165
166
        // Additional tasks for T2SDELinux
167
        if($itIsT2SDELinux) {
168
            $this->echoStartMsg(' - Connect swap...');
169
            Processes::mwExecBg('/etc/rc/connect-swap');
170
            $this->echoResultMsg();
171
        }
172
173
        // Start the syslogd daemon
174
        $this->echoStartMsg(' - Start syslogd daemon...');
175
        $syslogConf = new SyslogConf();
176
        $syslogConf->reStart();
177
        $this->echoResultMsg();
178
179
        // Update the database structure
180
        $dbUpdater = new UpdateDatabase();
181
        $dbUpdater->updateDatabaseStructure();
182
183
        // Create directories required by modules after DB upgrade
184
        $this->echoStartMsg(' - Create modules links and folders...');
185
        $storage->createWorkDirsAfterDBUpgrade();
186
        $this->echoResultMsg();
187
188
        // Update the system configuration and applications
189
        $this->echoStartMsg(' - Update configs and applications...'.PHP_EOL);
190
        $confUpdate = new UpdateSystemConfig();
191
        $confUpdate->updateConfigs();
192
        $this->echoStartMsg(' - Update configs...');
193
        $this->echoResultMsg();
194
195
        // Configure VM tools
196
        $this->echoStartMsg(' - Configuring VM tools...');
197
        $vmwareTools    = new VmToolsConf();
198
        $resultVMTools  = $vmwareTools->configure();
199
        $this->echoResultMsg($resultVMTools);
0 ignored issues
show
Bug introduced by
$resultVMTools of type boolean is incompatible with the type string expected by parameter $result of MikoPBX\Core\System\SystemLoader::echoResultMsg(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

199
        $this->echoResultMsg(/** @scrutinizer ignore-type */ $resultVMTools);
Loading history...
200
201
        // Configure the system hostname
202
        $this->echoStartMsg(' - Configuring hostname...');
203
        $network = new Network();
204
        $network->hostnameConfigure();
205
        $this->echoResultMsg();
206
207
        // Generate resolv.conf
208
        $this->echoStartMsg(' - Configuring resolv.conf...');
209
        $network->resolvConfGenerate();
210
        $this->echoResultMsg();
211
212
        // Configure LAN interface
213
        $this->echoStartMsg(' - Configuring LAN interface...');
214
        if ($this->isDocker){
215
            $network->configureLanInDocker();
216
        } else {
217
            $network->lanConfigure();
218
        }
219
        $this->echoResultMsg();
220
221
        // SSL rehash
222
        $this->echoStartMsg(' - SSL rehash...');
223
        System::sslRehash();
224
        $this->echoResultMsg();
225
226
        // Configure the firewall
227
        $this->echoStartMsg(' - Configuring Firewall...');
228
        $firewall = new IptablesConf();
229
        $firewall->applyConfig();
230
        $this->echoResultMsg();
231
232
        // Configure NTP
233
        $this->echoStartMsg(' - Configuring ntpd...');
234
        NTPConf::configure();
235
        $this->echoResultMsg();
236
237
        // Do not need to set Debian SSH service
238
        if(!Util::isSystemctl()){
239
            $this->echoStartMsg(' - Configuring SSH console...');
240
            $sshConf = new SSHConf();
241
            $resSsh  = $sshConf->configure();
242
            $this->echoResultMsg($resSsh);
243
        }
244
245
        // Start cloud provisioning
246
        if (!$this->isDocker) {
247
            $this->echoStartMsg(' - Cloud provisioning...'."\n");
248
            CloudProvisioning::start();
249
            $this->echoResultMsg();
250
251
            // Connect storage in a cloud if needed
252
            $this->echoStartMsg(' - Auto connect storage for a cloud ..."\n"');
253
            $connectResult = Storage::connectStorageInCloud();
254
            $this->echoResultMsg($connectResult);
255
        }
256
257
        // Update external IP if needed
258
        $this->echoStartMsg(' - Update external IP...');
259
        $network->updateExternalIp();
260
        $this->echoResultMsg();
261
262
        $this->di->getShared(RegistryProvider::SERVICE_NAME)->booting = false;
263
264
        return true;
265
    }
266
267
    /**
268
     * Load Asterisk and Web interface
269
     *
270
     * @return bool
271
     */
272
    public function startMikoPBX(): bool
273
    {
274
        $this->di->getShared(RegistryProvider::SERVICE_NAME)->booting = true;
275
276
        // Start the NATS queue daemon
277
        $this->echoStartMsg(' - Start nats queue daemon...');
278
        $natsConf = new NatsConf();
279
        $natsConf->reStart();
280
        $this->echoResultMsg();
281
282
        // Start the PHP-FPM daemon
283
        $this->echoStartMsg(' - Start php-fpm daemon...');
284
        PHPConf::reStart();
285
        $this->echoResultMsg();
286
287
        // Configure Asterisk and start it
288
        $this->echoStartMsg(' - Configuring Asterisk...'.PHP_EOL);
289
        $pbx = new PBX();
290
        $pbx->configure();
291
292
        $this->echoStartMsg(' - Start Asterisk...');
293
        $pbx->start();
294
        $this->echoResultMsg();
295
296
        // Wait for Asterisk to fully boot and reload SIP settings
297
        $this->echoStartMsg(' - Wait asterisk fully booted...');
298
        $asteriskResult = PBX::waitFullyBooted();
299
        $this->echoResultMsg($asteriskResult);
0 ignored issues
show
Bug introduced by
$asteriskResult of type boolean is incompatible with the type string expected by parameter $result of MikoPBX\Core\System\SystemLoader::echoResultMsg(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

299
        $this->echoResultMsg(/** @scrutinizer ignore-type */ $asteriskResult);
Loading history...
300
        if($asteriskResult){
301
            $this->echoStartMsg(' - Reload SIP settings in AstDB...');
302
            $sip = new SIPConf();
303
            $sip->updateAsteriskDatabase();
304
            $this->echoResultMsg();
305
        }
306
307
        // Configure and restart cron tasks
308
        $this->echoStartMsg(' - Configuring Cron tasks...');
309
        $cron = new CronConf();
310
        $cron->reStart();
311
        $this->echoResultMsg();
312
313
        // Start the Nginx daemon
314
        $this->echoStartMsg(' - Start Nginx daemon...');
315
        $nginx = new NginxConf();
316
        $nginx->generateConf();
317
        $nginx->reStart();
318
        $this->echoResultMsg();
319
320
        // Log that all services are fully loaded if Asterisk is fully booted
321
        if($asteriskResult){
322
            $this->echoStartMsg(' - All services are fully loaded');
323
        }
324
325
        // Display network information
326
        $this->echoStartMsg(Network::getInfoMessage());
327
328
        $this->di->getShared(RegistryProvider::SERVICE_NAME)->booting = false;
329
330
        return true;
331
    }
332
}