1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cobak78\RancherApi\Tests; |
4
|
|
|
|
5
|
|
|
use Cobak78\RancherApi\Clients\HttpClient; |
6
|
|
|
use Cobak78\RancherApi\Clients\WSClient; |
7
|
|
|
|
8
|
|
|
class RancherApiTest |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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 getContainer($url) |
84
|
|
|
{ |
85
|
|
|
$fpmService = $this->client->get($url, true); |
86
|
|
|
|
87
|
|
|
return $this->client->get('1a5/containers/' . $fpmService->instanceIds[0]); |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
View Code Duplication |
private function executeOn($container) |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
$response = $this->client->post($container->actions->execute, [ |
95
|
|
|
"attachStdin" => true, |
96
|
|
|
"attachStdout" => true, |
97
|
|
|
"command" => |
98
|
|
|
array_merge([$this->args['commandArg']], $this->args['params']), |
99
|
|
|
"tty" => false |
100
|
|
|
], true); |
101
|
|
|
|
102
|
|
|
$contents = json_decode($response->getBody()->getContents(), true); |
103
|
|
|
|
104
|
|
|
if ($this->client->isSocket()) { |
105
|
|
|
|
106
|
|
|
$wsClient = new WSClient(); |
107
|
|
|
|
108
|
|
|
$wsClient->socketConnect($contents['url'], $contents['token']); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function execute() |
113
|
|
|
{ |
114
|
|
|
$stack = $this->getStack(); |
115
|
|
|
|
116
|
|
|
$service = $this->getService($stack); |
117
|
|
|
|
118
|
|
|
$container = $this->getContainer($service); |
119
|
|
|
|
120
|
|
|
$this->executeOn($container); |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.