RancherApi::__construct()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
cc 6
eloc 15
nc 4
nop 4
1
<?php
2
3
namespace Cobak78\RancherApi;
4
5
use Cobak78\RancherApi\Clients\HttpClient;
6
use Cobak78\RancherApi\Clients\WSClient;
7
8
class RancherApi
9
{
10
    /**
11
     * @var HttpClient
12
     */
13
    private $client;
14
15
    /**
16
     * @var WSClient
17
     */
18
    private $wsClient;
19
20
    /**
21
     * @var array
22
     */
23
    private $args;
24
25
    /**
26
     * RancherApi constructor.
27
     * @param HttpClient $client
28
     * @param WSClient $wsClient
29
     * @param array $argv
30
     * @param int $argc
31
     */
32
    public function __construct(
33
        HttpClient $client,
34
        WSClient $wsClient,
35
        array $argv,
36
        int $argc
37
    )
38
    {
39
        $this->client = $client;
40
        $this->wsClient = $wsClient;
41
42
        if (!isset($argv[1]) || !isset($argv[2]) || !isset($argv[3])) {
43
            throw new \UnexpectedValueException('Unknown environment');
44
        }
45
46
        $this->args['projectArg'] = $argv[1];
47
        $this->args['containerArg'] = $argv[2];
48
        $this->args['commandArg'] = $argv[3];
49
50
        for ($i = 4; $i <= $argc; $i++) {
51
52
            if (!array_key_exists($i, $argv)) break;
53
54
            $this->args['params'][] = $argv[$i];
55
        }
56
    }
57
58
59
    private function getStack($url = '1a1/stacks/')
60
    {
61
        $stacks = $this->client->get($url)->data;
62
63
        foreach ($stacks as $stack)
64
        {
65
            if ($stack->name == $this->args['projectArg']) {
66
                return $stack->links->services;
67
            }
68
        }
69
    }
70
71
    private function getService($url)
72
    {
73
        $services = $this->client->get($url, true)->data;
74
75
        foreach ($services as $service)
76
        {
77
            if ($service->name == $this->args['containerArg']) {
78
                return $service->links->self;
79
            }
80
        }
81
    }
82
83
    private function getContainers($url)
84
    {
85
        $fpmService = $this->client->get($url, true);
86
87
        $instances = $this->client->get($fpmService->instances, true);
88
89
        return $instances->data;
90
91
92
    }
93
94
    private function executeOn($container)
95
    {
96
        $response = $this->client->post($container->actions->execute, [
97
            "attachStdin" => true,
98
            "attachStdout" => true,
99
            "command" =>
100
                array_merge([$this->args['commandArg']], $this->args['params']),
101
            "tty" => false
102
        ], true);
103
104
        $contents = json_decode($response->getBody()->getContents(), true);
105
106
        if ($this->client->isSocket()) {
107
108
            $wsClient = new WSClient();
109
110
            $wsClient->socketConnect($contents['url'], $contents['token']);
111
        }
112
    }
113
114
    /**
115
     * @param bool $onAllContainers
116
     */
117
    public function execute($onAllContainers = false)
118
    {
119
        $stack = $this->getStack();
120
121
        $service = $this->getService($stack);
122
123
        $containers = $this->getContainers($service);
124
125
        foreach ($containers as $container)
126
        {
127
            $this->executeOn($container);
128
129
            if ($onAllContainers) {
130
                break;
131
            }
132
        }
133
    }
134
}
135