Passed
Push — develop ( b3ec5e...1a1ece )
by Nikolay
12:31
created

VKCloud::provision()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 37
rs 9.6
cc 4
nc 4
nop 0
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2024 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\CloudProvisioning;
21
22
use GuzzleHttp\Client;
23
use GuzzleHttp\Exception\GuzzleException;
24
use GuzzleHttp;
25
use MikoPBX\Core\System\SystemMessages;
26
27
class VKCloud extends CloudProvider
28
{
29
    public const CloudID = 'VKCloud';
30
31
    private Client $client;
32
33
    public function __construct()
34
    {
35
        $this->client = new Client(['timeout' => self::HTTP_TIMEOUT]);
36
    }
37
38
    /**
39
     * Performs the VK Cloud Solutions provisioning.
40
     *
41
     * @return bool True if the provisioning was successful, false otherwise.
42
     */
43
    public function provision(): bool
44
    {
45
        // Cloud check
46
        $checkValue = $this->getMetaDataVCS('services/partition');
47
        if ($checkValue==='aws'){
48
            return false; // It is AWS, not VK
49
        }
50
51
        // Update machine name
52
        $hostname = $this->getMetaDataVCS('hostname');
53
        if (empty($hostname)) {
54
            return false;
55
        }
56
57
        SystemMessages::echoToTeletype(PHP_EOL);
58
59
        $this->updateHostName($hostname);
60
61
        // Update LAN settings with the external IP address
62
        $extIp = $this->getMetaDataVCS('public-ipv4');
63
        $this->updateLanSettings($extIp);
64
65
        // Update SSH keys, if available
66
        $sshKey = '';
67
        $sshKeys = $this->getMetaDataVCS('public-keys');
68
        $sshId = explode('=', $sshKeys)[0] ?? '';
69
        if ($sshId !== '') {
70
            $sshKey = $this->getMetaDataVCS("public-keys/$sshId/openssh-key");
71
        }
72
        $this->updateSSHKeys($sshKey);
73
74
        // Update SSH and WEB passwords using some unique identifier from the metadata
75
        $vmId = $this->getMetaDataVCS('instance-id')??'';
76
        $this->updateSSHCredentials('vk-user', $vmId);
77
        $this->updateWebPassword($vmId);
78
79
        return true;
80
    }
81
82
    /**
83
     * Retrieves metadata from the MCS endpoint.
84
     *
85
     * @param string $url The URL of the metadata endpoint.
86
     * @return string The response body.
87
     */
88
    private function getMetaDataVCS(string $url): string
89
    {
90
        $baseUrl = 'http://169.254.169.254/latest/meta-data/';
91
        $headers = [];
92
        $params = [];
93
        $options = [
94
            'timeout' => self::HTTP_TIMEOUT,
95
            'http_errors' => false,
96
            'headers' => $headers
97
        ];
98
99
        $url = "$baseUrl/$url?" . http_build_query($params);
100
        try {
101
            $res = $this->client->request('GET', $url, $options);
102
            $code = $res->getStatusCode();
103
        } catch (GuzzleHttp\Exception\ConnectException $e) {
104
            $code = 0;
105
        } catch (GuzzleException $e) {
106
            $code = 0;
107
        }
108
        $body = '';
109
        if ($code === 200 && isset($res)) {
110
            $body = $res->getBody()->getContents();
111
        }
112
        return $body;
113
    }
114
}