|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* @package: chapi |
|
5
|
|
|
* |
|
6
|
|
|
* @author: bthapaliya |
|
7
|
|
|
* @since: 2016-12-14 |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Chapi\BusinessCase\Comparison; |
|
12
|
|
|
|
|
13
|
|
|
use Chapi\Component\Comparison\DiffCompareInterface; |
|
14
|
|
|
use Chapi\Entity\Chronos\ChronosJobEntity; |
|
15
|
|
|
use Chapi\Entity\JobEntityInterface; |
|
16
|
|
|
use Chapi\Entity\Marathon\AppEntity\DockerPortMapping; |
|
17
|
|
|
use Chapi\Entity\Marathon\AppEntity\Fetch; |
|
18
|
|
|
use Chapi\Entity\Marathon\AppEntity\Network; |
|
19
|
|
|
use Chapi\Entity\Marathon\MarathonAppEntity; |
|
20
|
|
|
use Chapi\Service\JobRepository\JobRepositoryInterface; |
|
21
|
|
|
|
|
22
|
|
|
class MarathonJobComparisonBusinessCase extends AbstractJobComparisionBusinessCase |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @param JobRepositoryInterface $localRepository |
|
26
|
|
|
* @param JobRepositoryInterface $remoteRepository |
|
27
|
|
|
* @param DiffCompareInterface $diffCompare |
|
28
|
|
|
*/ |
|
29
|
25 |
|
public function __construct( |
|
30
|
|
|
JobRepositoryInterface $localRepository, |
|
31
|
|
|
JobRepositoryInterface $remoteRepository, |
|
32
|
|
|
DiffCompareInterface $diffCompare |
|
33
|
|
|
) { |
|
34
|
25 |
|
$this->remoteRepository = $remoteRepository; |
|
35
|
25 |
|
$this->localRepository = $localRepository; |
|
36
|
25 |
|
$this->diffCompare = $diffCompare; |
|
37
|
25 |
|
} |
|
38
|
|
|
|
|
39
|
5 |
|
protected function preCompareModifications(JobEntityInterface &$localJob, JobEntityInterface &$remoteJob) |
|
40
|
|
|
{ |
|
41
|
5 |
|
if (!$localJob instanceof MarathonAppEntity || |
|
42
|
5 |
|
!$remoteJob instanceof MarathonAppEntity |
|
43
|
|
|
) { |
|
44
|
|
|
throw new \RuntimeException('Required MarathonAppEntity. Something else encountered.'); |
|
45
|
|
|
} |
|
46
|
|
|
// marathon returns portDefinitions values for auto configured port as well |
|
47
|
|
|
// we want to only check if the port is defined in local file. |
|
48
|
|
|
// otherwise we ignore the remote values. |
|
49
|
5 |
|
if (!$localJob->portDefinitions) { |
|
|
|
|
|
|
50
|
5 |
|
$remoteJob->portDefinitions = null; |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// convert uris to fetchers |
|
54
|
5 |
|
foreach ($localJob->uris as $uri) { |
|
55
|
|
|
$localJob->fetch[] = new Fetch(["uri" => $uri, "extract" => true]); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
5 |
|
$localJob->uris = []; |
|
58
|
5 |
|
foreach ($remoteJob->uris as $uri) { |
|
59
|
|
|
$remoteJob->fetch[] = new Fetch(["uri" => $uri, "extract" => true]); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
5 |
|
$remoteJob->uris = []; |
|
62
|
|
|
|
|
63
|
5 |
|
if ($localJob->container && $remoteJob->container) { |
|
64
|
|
|
|
|
65
|
2 |
|
$localPortMappings = $localJob->container->portMappings; |
|
66
|
2 |
|
$remotePortMappings = $remoteJob->container->portMappings; |
|
67
|
|
|
|
|
68
|
2 |
|
usort($localPortMappings, DockerPortMapping::class . '::less'); |
|
69
|
2 |
|
usort($remotePortMappings, DockerPortMapping::class . '::less'); |
|
70
|
|
|
|
|
71
|
2 |
|
foreach ($localPortMappings as $index => $localPortMapping) { |
|
72
|
1 |
|
if ($localPortMapping->servicePort !== 0) { |
|
73
|
|
|
continue; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
if (!isset($remotePortMappings[$index])) { |
|
77
|
|
|
continue; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
$remotePortMapping = $remotePortMappings[$index]; |
|
81
|
|
|
|
|
82
|
1 |
|
if (DockerPortMapping::less($remotePortMapping, $localPortMapping) != 0) { |
|
83
|
1 |
|
$fixedPortMapping = clone $remotePortMapping; |
|
84
|
1 |
|
$fixedPortMapping->servicePort = 0; |
|
85
|
|
|
|
|
86
|
1 |
|
if (DockerPortMapping::less($fixedPortMapping, $localPortMapping) == 0) { |
|
87
|
1 |
|
unset($localPortMappings[$index]); |
|
88
|
1 |
|
unset($remotePortMappings[$index]); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
$localJob->container->portMappings = array_values($localPortMappings); |
|
|
|
|
|
|
94
|
2 |
|
$remoteJob->container->portMappings = array_values($remotePortMappings); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// set network to "host" when not containerized |
|
98
|
5 |
|
if (!$localJob->container) { |
|
99
|
3 |
|
$localJob->networks = [new Network(["mode" => "host"])]; |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
5 |
|
if (!$remoteJob->container) { |
|
102
|
3 |
|
$remoteJob->networks = [new Network(["mode" => "host"])]; |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
5 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return JobEntityInterface |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function getEntitySetWithDefaults() |
|
110
|
|
|
{ |
|
111
|
|
|
return new MarathonAppEntity(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param JobEntityInterface|ChronosJobEntity $jobEntityA |
|
116
|
|
|
* @param JobEntityInterface|ChronosJobEntity $jobEntityB |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
|
|
public function hasSameJobType(JobEntityInterface $jobEntityA, JobEntityInterface $jobEntityB) |
|
120
|
|
|
{ |
|
121
|
|
|
// for now we don't have a concrete seperation |
|
122
|
|
|
// of types for marathon. |
|
123
|
|
|
return true; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param $property |
|
128
|
|
|
* @param $jobEntityA |
|
129
|
|
|
* @param $jobEntityB |
|
130
|
|
|
* @return bool |
|
131
|
|
|
*/ |
|
132
|
19 |
|
protected function isEntityEqual($property, JobEntityInterface $jobEntityA, JobEntityInterface $jobEntityB) |
|
133
|
|
|
{ |
|
134
|
19 |
|
if (!$jobEntityA instanceof MarathonAppEntity || |
|
135
|
19 |
|
!$jobEntityB instanceof MarathonAppEntity |
|
136
|
|
|
) { |
|
137
|
|
|
throw new \RuntimeException('Required MarathonAppEntity. Something else encountered.'); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
19 |
|
return $this->isEqual($jobEntityA->{$property}, $jobEntityB->{$property}); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param mixed $valueA |
|
145
|
|
|
* @param mixed $valueB |
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
20 |
|
private function isEqual($valueA, $valueB) |
|
149
|
|
|
{ |
|
150
|
20 |
|
if (is_array($valueA) && is_array($valueB)) { |
|
151
|
8 |
|
return $this->isArrayEqual($valueA, $valueB); |
|
152
|
19 |
|
} elseif (is_object($valueA) && is_object($valueB)) { |
|
153
|
9 |
|
return $this->isArrayEqual(get_object_vars($valueA), get_object_vars($valueB)); |
|
154
|
17 |
|
} elseif ((is_scalar($valueA) && is_scalar($valueB)) || (is_null($valueA) && is_null($valueB))) { |
|
155
|
13 |
|
return $valueA == $valueB; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
5 |
|
return false; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @param array $valuesA |
|
163
|
|
|
* @param array $valuesB |
|
164
|
|
|
* @return bool |
|
165
|
|
|
*/ |
|
166
|
13 |
|
private function isArrayEqual(array $valuesA, array $valuesB) |
|
167
|
|
|
{ |
|
168
|
13 |
|
return $this->isArrayHalfEqual($valuesA, $valuesB) && $this->isArrayHalfEqual($valuesB, $valuesA); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @param array $valuesA |
|
173
|
|
|
* @param array $valuesB |
|
174
|
|
|
* @return bool |
|
175
|
|
|
*/ |
|
176
|
13 |
|
private function isArrayHalfEqual(array $valuesA, array $valuesB) |
|
177
|
|
|
{ |
|
178
|
13 |
|
foreach ($valuesA as $keyA => $valueA) { |
|
179
|
12 |
|
if (is_string($keyA)) { |
|
180
|
9 |
|
if (!array_key_exists($keyA, $valuesB) || !$this->isEqual($valueA, $valuesB[$keyA])) { |
|
181
|
9 |
|
return false; |
|
182
|
|
|
} |
|
183
|
|
|
} else { |
|
184
|
6 |
|
foreach ($valuesB as $valueB) { |
|
185
|
6 |
|
if ($this->isEqual($valueA, $valueB)) { |
|
186
|
6 |
|
continue 2; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
9 |
|
return false; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
10 |
|
return true; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.