Passed
Push — develop ( c8086a...0f7865 )
by Портнов
04:12
created

SystemLoader::startMikoPBX()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 39
rs 9.472
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright (C) 2017-2020 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\Core\System\Configs\BeanstalkConf;
23
use MikoPBX\Core\System\Configs\CronConf;
24
use MikoPBX\Core\System\Configs\IptablesConf;
25
use MikoPBX\Core\System\Configs\NatsConf;
26
use MikoPBX\Core\System\Configs\NginxConf;
27
use MikoPBX\Core\System\Configs\NTPConf;
28
use MikoPBX\Core\System\Configs\PHPConf;
29
use MikoPBX\Core\System\Configs\RedisConf;
30
use MikoPBX\Core\System\Configs\SSHConf;
31
use MikoPBX\Core\System\Configs\SyslogConf;
32
use MikoPBX\Core\System\Configs\VMWareToolsConf;
33
use MikoPBX\Core\System\Upgrade\UpdateDatabase;
34
use MikoPBX\Core\System\Upgrade\UpdateSystemConfig;
35
use Phalcon\Di;
36
37
class SystemLoader extends Di\Injectable
38
{
39
    private string $stageMessage = '';
40
41
    private function echoStartMsg(string $message):void
42
    {
43
        $this->stageMessage = $message;
44
        Util::echoWithSyslog($this->stageMessage);
45
    }
46
47
    private function echoResultMsg(bool $result = true):void
48
    {
49
        Util::echoResult($this->stageMessage, $result);
50
        $this->stageMessage = '';
51
    }
52
53
    /**
54
     * Load system services
55
     */
56
    public function startSystem(): bool
57
    {
58
        $this->di->getShared('registry')->booting = true;
59
60
        $this->echoStartMsg(' - Start beanstalkd daemon...');
61
        $beanstalkConf = new BeanstalkConf();
62
        $beanstalkConf->reStart();
63
        $this->echoResultMsg();
64
65
        $this->echoStartMsg(' - Start redis daemon...');
66
        $redisConf = new RedisConf();
67
        $redisConf->reStart();
68
        $this->echoResultMsg();
69
70
        $system = new System();
71
        $this->echoStartMsg(' - Configuring timezone...');
72
        $system::timezoneConfigure();
73
        $this->echoResultMsg();
74
75
        $storage       = new Storage();
76
        $this->echoStartMsg(' - Mount storage disk...');
77
        $storage->saveFstab();
78
        $storage->configure();
79
        $this->echoResultMsg();
80
81
        $this->echoStartMsg(' - Connect swap...');
82
        $storage->mountSwap();
83
        $this->echoResultMsg();
84
85
        $this->echoStartMsg(' - Start syslogd daemon...');
86
        $syslogConf = new SyslogConf();
87
        $syslogConf->reStart();
88
        $this->echoResultMsg();
89
        
90
        $dbUpdater = new UpdateDatabase();
91
        $dbUpdater->updateDatabaseStructure();
92
93
        $this->echoStartMsg(' - Create modules links and folders...');
94
        $storage->createWorkDirsAfterDBUpgrade();
95
        $this->echoResultMsg();
96
97
        $this->echoStartMsg(' - Update configs and applications...'."\n");
98
        $confUpdate = new UpdateSystemConfig();
99
        $confUpdate->updateConfigs();
100
        $this->echoStartMsg(' - Update configs...');
101
        $this->echoResultMsg();
102
103
        $this->echoStartMsg(' - Load kernel modules...');
104
        $resKernelModules = $system->loadKernelModules();
105
        $this->echoResultMsg($resKernelModules);
106
107
        $this->echoStartMsg(' - Configuring VM tools...');
108
        $vmwareTools    = new VMWareToolsConf();
109
        $resultVMTools  = $vmwareTools->configure();
110
        $this->echoResultMsg($resultVMTools);
111
112
        $this->echoStartMsg(' - Configuring hostname...');
113
        $network = new Network();
114
        $network->hostnameConfigure();
115
        $this->echoResultMsg();
116
117
        $this->echoStartMsg(' - Configuring resolv.conf...');
118
        $network->resolvConfGenerate();
119
        $this->echoResultMsg();
120
121
        $this->echoStartMsg(' - Configuring LAN interface...');
122
        $network->lanConfigure();
123
        $this->echoResultMsg();
124
125
        $this->echoStartMsg(' - Configuring Firewall...');
126
        $firewall = new IptablesConf();
127
        $firewall->applyConfig();
128
        $this->echoResultMsg();
129
130
        $this->echoStartMsg(' - Configuring ntpd...');
131
        NTPConf::configure();
132
        $this->echoResultMsg();
133
134
        $this->echoStartMsg(' - Configuring SSH console...');
135
        $sshConf = new SSHConf();
136
        $resSsh  = $sshConf->configure();
137
        $this->echoResultMsg($resSsh);
138
139
        $this->echoStartMsg(' - Configuring msmtp services...');
140
        $notifications = new Notifications();
141
        $notifications->configure();
142
        $this->echoResultMsg();
143
144
        $this->di->getShared('registry')->booting = false;
145
146
        return true;
147
    }
148
149
    /**
150
     * Load Asterisk and Web interface
151
     *
152
     * @return bool
153
     */
154
    public function startMikoPBX(): bool
155
    {
156
        $this->di->getShared('registry')->booting = true;
157
158
        $this->echoStartMsg(' - Start nats queue daemon...');
159
        $natsConf = new NatsConf();
160
        $natsConf->reStart();
161
        $this->echoResultMsg();
162
163
        $this->echoStartMsg(' - Start php-fpm daemon...');
164
        PHPConf::reStart();
165
        $this->echoResultMsg();
166
167
        $this->echoStartMsg(' - Configuring Asterisk...'.PHP_EOL);
168
        $pbx = new PBX();
169
        $pbx->configure();
170
171
        $this->echoStartMsg(' - Start Asterisk...');
172
        $pbx->start();
173
        $this->echoResultMsg();
174
175
        $this->echoStartMsg(' - Wait asterisk fully booted...');
176
        PBX::waitFullyBooted();
177
        $this->echoResultMsg();
178
179
        $this->echoStartMsg(' - Configuring Cron tasks...');
180
        $cron = new CronConf();
181
        $cron->reStart();
182
        $this->echoResultMsg();
183
184
        $this->echoStartMsg(' - Start Nginx daemon...');
185
        $nginx = new NginxConf();
186
        $nginx->generateConf();
187
        $nginx->reStart();
188
        $this->echoResultMsg();
189
190
        $this->di->getShared('registry')->booting = false;
191
192
        return true;
193
    }
194
}