Passed
Push — develop ( ef97e9...11ccd2 )
by Nikolay
04:33
created

VKCloud   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 38
c 1
b 0
f 0
dl 0
loc 71
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A provision() 0 28 3
A getMetaDataVCS() 0 26 5
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\Exception\GuzzleException;
23
use GuzzleHttp;
24
use MikoPBX\Core\System\Util;
25
26
class VKCloud extends CloudProvider
27
{
28
    const CLOUD_NAME='VKCloud';
29
30
    /**
31
     * Performs the VK Cloud Solutions provisioning.
32
     *
33
     * @return bool True if the provisioning was successful, false otherwise.
34
     */
35
    public function provision(): bool
36
    {
37
        Util::sysLogMsg(__CLASS__, "Try VK Cloud provisioning... ");
38
39
        $hostname = $this->getMetaDataVCS('hostname');
40
        if (empty($hostname)) {
41
            return false;
42
        }
43
44
        // Update LAN settings with hostname and external IP address
45
        $extIp = $this->getMetaDataVCS('public-ipv4');
46
        $this->updateLanSettings($hostname, $extIp);
47
48
        // Update SSH keys, if available
49
        $sshKey = '';
50
        $sshKeys = $this->getMetaDataVCS('public-keys');
51
        $sshId = explode('=', $sshKeys)[0] ?? '';
52
        if ($sshId !== '') {
53
            $sshKey = $this->getMetaDataVCS("public-keys/$sshId/openssh-key");
54
        }
55
        $this->updateSSHKeys($sshKey);
56
57
        // Update SSH and WEB passwords using some unique identifier from the metadata
58
        $vmId = $this->getMetaDataVCS('instance-id')??'';
59
        $this->updateSSHPassword($vmId);
60
        $this->updateWebPassword($vmId);
61
62
        return true;
63
    }
64
65
    /**
66
     * Retrieves metadata from the MCS endpoint.
67
     *
68
     * @param string $url The URL of the metadata endpoint.
69
     * @return string The response body.
70
     */
71
    private function getMetaDataVCS(string $url): string
72
    {
73
        $baseUrl = 'http://169.254.169.254/latest/meta-data/';
74
        $client = new GuzzleHttp\Client();
75
        $headers = [];
76
        $params = [];
77
        $options = [
78
            'timeout' => self::HTTP_TIMEOUT,
79
            'http_errors' => false,
80
            'headers' => $headers
81
        ];
82
83
        $url = "$baseUrl/$url?" . http_build_query($params);
84
        try {
85
            $res = $client->request('GET', $url, $options);
86
            $code = $res->getStatusCode();
87
        } catch (GuzzleHttp\Exception\ConnectException $e) {
88
            $code = 0;
89
        } catch (GuzzleException $e) {
90
            $code = 0;
91
        }
92
        $body = '';
93
        if ($code === 200 && isset($res)) {
94
            $body = $res->getBody()->getContents();
95
        }
96
        return $body;
97
    }
98
}