|
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\PbxSettings; |
|
23
|
|
|
use MikoPBX\Common\Models\PbxSettingsConstants; |
|
24
|
|
|
use MikoPBX\Core\System\CloudProvisioning\AzureCloud; |
|
25
|
|
|
use MikoPBX\Core\System\CloudProvisioning\GoogleCloud; |
|
26
|
|
|
use MikoPBX\Core\System\CloudProvisioning\VKCloud; |
|
27
|
|
|
use MikoPBX\Core\System\CloudProvisioning\YandexCloud; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Class CloudProvisioning |
|
31
|
|
|
* |
|
32
|
|
|
* Handles the provisioning process for cloud solutions. |
|
33
|
|
|
* |
|
34
|
|
|
* @package MikoPBX\Core\System |
|
35
|
|
|
*/ |
|
36
|
|
|
class CloudProvisioning |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* Starts the cloud provisioning process. |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function start(): void |
|
42
|
|
|
{ |
|
43
|
|
|
if (self::checkItNeedToStartProvisioning()===false){ |
|
44
|
|
|
Util::echoResult(" |- Provisioning was already completed."); |
|
45
|
|
|
return; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// Lists of possible cloud providers. |
|
49
|
|
|
$providers = [ |
|
50
|
|
|
YandexCloud::CloudID => new YandexCloud(), |
|
51
|
|
|
VKCloud::CloudID => new VKCloud(), |
|
52
|
|
|
GoogleCloud::CloudID => new GoogleCloud(), |
|
53
|
|
|
AzureCloud::CloudID => new AzureCloud(), |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
foreach ($providers as $cloudId => $provider) { |
|
57
|
|
|
if ($provider->provision()) { |
|
58
|
|
|
self::afterProvisioning($provider, $cloudId); |
|
59
|
|
|
Util::echoResult(" |- Provisioning on $cloudId has been successfully completed."); |
|
60
|
|
|
// Provisioning succeeded, break out of the loop |
|
61
|
|
|
break; |
|
62
|
|
|
} |
|
63
|
|
|
Util::echoResult(" |- Attempting to provision on $cloudId",false); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* After provisioning, perform the following actions: |
|
69
|
|
|
* @param $provider mixed The provider object. |
|
70
|
|
|
* @param string $cloudName The name of the cloud. |
|
71
|
|
|
* @return void |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function afterProvisioning($provider, string $cloudName): void |
|
74
|
|
|
{ |
|
75
|
|
|
// Enable firewall and Fail2Ban |
|
76
|
|
|
$provider->updatePbxSettings(PbxSettingsConstants::PBX_FIREWALL_ENABLED, '1'); |
|
77
|
|
|
$provider->updatePbxSettings(PbxSettingsConstants::PBX_FAIL2BAN_ENABLED, '1'); |
|
78
|
|
|
|
|
79
|
|
|
// Mark provisioning as completed |
|
80
|
|
|
$provider->updatePbxSettings(PbxSettingsConstants::CLOUD_PROVISIONING, '1'); |
|
81
|
|
|
$provider->updatePbxSettings(PbxSettingsConstants::VIRTUAL_HARDWARE_TYPE, $cloudName); |
|
82
|
|
|
|
|
83
|
|
|
// Connect storage |
|
84
|
|
|
self::checkConnectStorage(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Checks and connects the storage disk automatically. |
|
89
|
|
|
*/ |
|
90
|
|
|
private static function checkConnectStorage(): void |
|
91
|
|
|
{ |
|
92
|
|
|
$phpPath = Util::which('php'); |
|
93
|
|
|
Processes::mwExec($phpPath . ' -f /etc/rc/connect.storage auto'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Checks if provisioning is needed. |
|
98
|
|
|
*/ |
|
99
|
|
|
private static function checkItNeedToStartProvisioning(): bool |
|
100
|
|
|
{ |
|
101
|
|
|
if (PbxSettings::findFirst('key="' . PbxSettingsConstants::CLOUD_PROVISIONING . '"') === null) { |
|
102
|
|
|
return true; // Need provision |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// In some Clouds the virtual machine starts immediately before the storage disk was attached |
|
106
|
|
|
$storageMounted = Storage::isStorageDiskMounted(); |
|
107
|
|
|
if ($storageMounted === false) { |
|
108
|
|
|
return true; // Need provision |
|
109
|
|
|
} else { |
|
110
|
|
|
return false; // No need provision |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |