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
|
|
|
$this->args['params'] = $this->parseParameters($this->args['params']); |
58
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
private function getStack($url = '1a1/stacks/') |
63
|
|
|
{ |
64
|
|
|
$stacks = $this->client->get($url)->data; |
65
|
|
|
|
66
|
|
|
foreach ($stacks as $stack) |
67
|
|
|
{ |
68
|
|
|
if ($stack->name == $this->args['projectArg']) { |
69
|
|
|
return $stack->links->services; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function getService($url) |
75
|
|
|
{ |
76
|
|
|
$services = $this->client->get($url, true)->data; |
77
|
|
|
|
78
|
|
|
foreach ($services as $service) |
79
|
|
|
{ |
80
|
|
|
if ($service->name == $this->args['containerArg']) { |
81
|
|
|
return $service->links->self; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function getContainers($url) |
87
|
|
|
{ |
88
|
|
|
$fpmService = $this->client->get($url, true); |
89
|
|
|
|
90
|
|
|
$instances = $this->client->get($fpmService->instances, true); |
91
|
|
|
|
92
|
|
|
return $instances->data; |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function executeOn($container) |
98
|
|
|
{ |
99
|
|
|
$response = $this->client->post($container->actions->execute, [ |
100
|
|
|
"attachStdin" => true, |
101
|
|
|
"attachStdout" => true, |
102
|
|
|
"command" => |
103
|
|
|
array_merge([$this->args['commandArg']], $this->args['params']), |
104
|
|
|
"tty" => false |
105
|
|
|
], true); |
106
|
|
|
|
107
|
|
|
$contents = json_decode($response->getBody()->getContents(), true); |
108
|
|
|
|
109
|
|
|
if ($this->client->isSocket()) { |
110
|
|
|
|
111
|
|
|
$wsClient = new WSClient(); |
112
|
|
|
|
113
|
|
|
$wsClient->socketConnect($contents['url'], $contents['token']); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param bool $onAllContainers |
119
|
|
|
*/ |
120
|
|
|
public function execute($onAllContainers = false) |
121
|
|
|
{ |
122
|
|
|
$stack = $this->getStack(); |
123
|
|
|
|
124
|
|
|
$service = $this->getService($stack); |
125
|
|
|
|
126
|
|
|
$containers = $this->getContainers($service); |
127
|
|
|
|
128
|
|
|
foreach ($containers as $container) |
129
|
|
|
{ |
130
|
|
|
$this->executeOn($container); |
131
|
|
|
|
132
|
|
|
if ($onAllContainers) { |
133
|
|
|
break; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param $params |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
public function parseParameters($params) |
143
|
|
|
{ |
144
|
|
|
|
145
|
|
|
$newParams = []; |
146
|
|
|
|
147
|
|
|
for ($i = 0; $i < count($params) ;$i++) { |
|
|
|
|
148
|
|
|
if (false == strpos($params[$i],'=') && false !== strpos($params[$i],'-')) { |
|
|
|
|
149
|
|
|
|
150
|
|
|
if ($i + 1 < count($params)) { |
151
|
|
|
if (false == strpos($params[$i+1],'=')){ |
|
|
|
|
152
|
|
|
$newParams[] = $params[$i] . ' ' . $params[$i+1]; |
153
|
|
|
$i++; |
154
|
|
|
} else { |
155
|
|
|
$newParams[] = $params[$i]; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
} else { |
160
|
|
|
$newParams[] = $params[$i]; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
return $newParams; |
164
|
|
|
|
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: