1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package: chapi |
4
|
|
|
* |
5
|
|
|
* @author: msiebeneicher |
6
|
|
|
* @since: 2015-07-31 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Chapi\Service\JobRepository; |
11
|
|
|
|
12
|
|
|
use Chapi\Component\DatePeriod\DatePeriodFactoryInterface; |
13
|
|
|
use Chapi\Entity\Chronos\JobEntity; |
14
|
|
|
use Chapi\Exception\DatePeriodException; |
15
|
|
|
|
16
|
|
|
class JobEntityValidatorService implements JobEntityValidatorServiceInterface |
17
|
|
|
{ |
18
|
|
|
const REG_EX_VALID_NAME = '/^[a-zA-Z0-9_-]*$/'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var DatePeriodFactoryInterface |
22
|
|
|
*/ |
23
|
|
|
private $oDatePeriodFactory; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param DatePeriodFactoryInterface $oDatePeriodFactory |
27
|
|
|
*/ |
28
|
13 |
|
public function __construct( |
29
|
|
|
DatePeriodFactoryInterface $oDatePeriodFactory |
30
|
|
|
) |
31
|
|
|
{ |
32
|
13 |
|
$this->oDatePeriodFactory = $oDatePeriodFactory; |
33
|
13 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param JobEntity $oJobEntity |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
12 |
|
public function isEntityValid(JobEntity $oJobEntity) |
40
|
|
|
{ |
41
|
12 |
|
return (!in_array(false, $this->validateJobEntity($oJobEntity))); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param JobEntity $oJobEntity |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
13 |
|
public function validateJobEntity(JobEntity $oJobEntity) |
49
|
|
|
{ |
50
|
13 |
|
$_aValidProperties = []; |
51
|
|
|
|
52
|
13 |
|
foreach ($oJobEntity as $_sProperty => $mValue) |
53
|
|
|
{ |
54
|
|
|
switch ($_sProperty) |
55
|
|
|
{ |
56
|
13 |
|
case 'name': |
57
|
13 |
|
$_aValidProperties[$_sProperty] = $this->isNamePropertyValid($mValue); |
58
|
13 |
|
break; |
59
|
|
|
|
60
|
13 |
|
case 'command': |
61
|
13 |
|
case 'description': |
62
|
13 |
|
case 'owner': |
63
|
13 |
|
case 'ownerName': |
64
|
13 |
|
$_aValidProperties[$_sProperty] = (!empty($oJobEntity->{$_sProperty})); |
65
|
13 |
|
break; |
66
|
|
|
|
67
|
13 |
|
case 'epsilon': |
68
|
13 |
|
$_aValidProperties[$_sProperty] = $this->isEpsilonPropertyValid($oJobEntity); |
69
|
13 |
|
break; |
70
|
|
|
|
71
|
13 |
|
case 'async': |
72
|
13 |
|
case 'disabled': |
73
|
13 |
|
case 'softError': |
74
|
13 |
|
case 'highPriority': |
75
|
13 |
|
$_aValidProperties[$_sProperty] = (is_bool($oJobEntity->{$_sProperty})); |
76
|
13 |
|
break; |
77
|
|
|
|
78
|
13 |
|
case 'schedule': |
79
|
13 |
|
$_aValidProperties[$_sProperty] = $this->isSchedulePropertyValid($oJobEntity); |
80
|
13 |
|
break; |
81
|
|
|
|
82
|
13 |
|
case 'parents': |
83
|
13 |
|
$_aValidProperties[$_sProperty] = (is_array($oJobEntity->{$_sProperty})); |
84
|
13 |
|
break; |
85
|
|
|
|
86
|
13 |
|
case 'retries': |
87
|
13 |
|
$_aValidProperties[$_sProperty] = ($oJobEntity->{$_sProperty} >= 0); |
88
|
13 |
|
break; |
89
|
|
|
|
90
|
13 |
|
case 'constraints': |
91
|
13 |
|
$_aValidProperties[$_sProperty] = $this->isConstraintsPropertyValid($mValue); |
92
|
13 |
|
break; |
93
|
|
|
|
94
|
13 |
|
case 'container': |
95
|
13 |
|
$_aValidProperties[$_sProperty] = $this->isContainerPropertyValid($mValue); |
96
|
13 |
|
break; |
97
|
|
|
} |
98
|
13 |
|
} |
99
|
|
|
|
100
|
13 |
|
return $_aValidProperties; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param JobEntity $oJobEntity |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
1 |
|
public function getInvalidProperties(JobEntity $oJobEntity) |
108
|
|
|
{ |
109
|
1 |
|
$_aValidationFields = $this->validateJobEntity($oJobEntity); |
110
|
|
|
|
111
|
1 |
|
$_aInvalidFields = []; |
112
|
1 |
|
foreach ($_aValidationFields as $_sProperty => $_bIsValid) |
113
|
|
|
{ |
114
|
1 |
|
if (false == $_bIsValid) |
115
|
1 |
|
{ |
116
|
1 |
|
$_aInvalidFields[] = $_sProperty; |
117
|
1 |
|
} |
118
|
1 |
|
} |
119
|
|
|
|
120
|
1 |
|
return $_aInvalidFields; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $sName |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
13 |
|
private function isNamePropertyValid($sName) |
128
|
|
|
{ |
129
|
13 |
|
return (!empty($sName) && preg_match(self::REG_EX_VALID_NAME, $sName)); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param JobEntity $oJobEntity |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
13 |
|
private function isSchedulePropertyValid(JobEntity $oJobEntity) |
137
|
|
|
{ |
138
|
13 |
|
if (empty($oJobEntity->schedule) && !empty($oJobEntity->parents)) |
139
|
13 |
|
{ |
140
|
1 |
|
return true; |
141
|
|
|
} |
142
|
|
|
|
143
|
12 |
|
if (!empty($oJobEntity->schedule) && empty($oJobEntity->parents)) |
144
|
12 |
|
{ |
145
|
|
|
try |
146
|
|
|
{ |
147
|
11 |
|
$_oDataPeriod = $this->oDatePeriodFactory->createDatePeriod($oJobEntity->schedule, $oJobEntity->scheduleTimeZone); |
148
|
11 |
|
return (false !== $_oDataPeriod); |
149
|
|
|
} |
150
|
1 |
|
catch (DatePeriodException $oException) |
151
|
|
|
{ |
152
|
|
|
// invalid: Iso8601 is not valid and/or DatePeriodFactory is able to create a valid DatePeriod |
153
|
|
|
} |
154
|
1 |
|
} |
155
|
|
|
|
156
|
2 |
|
return false; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param JobEntity $oJobEntity |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
13 |
|
private function isEpsilonPropertyValid(JobEntity $oJobEntity) |
164
|
|
|
{ |
165
|
13 |
|
if ($oJobEntity->isSchedulingJob() && !empty($oJobEntity->epsilon)) |
166
|
13 |
|
{ |
167
|
|
|
try |
168
|
|
|
{ |
169
|
11 |
|
$_oDateIntervalEpsilon = new \DateInterval($oJobEntity->epsilon); |
170
|
10 |
|
$_iIntervalEpsilon = (int) $_oDateIntervalEpsilon->format('%Y%M%D%H%I%S'); |
171
|
|
|
|
172
|
10 |
|
if ($_iIntervalEpsilon > 30) // if epsilon > "PT30S" |
173
|
10 |
|
{ |
174
|
9 |
|
$_oIso8601Entity = $this->oDatePeriodFactory->createIso8601Entity($oJobEntity->schedule); |
175
|
|
|
|
176
|
9 |
|
$_oDateIntervalScheduling = new \DateInterval($_oIso8601Entity->sInterval); |
177
|
9 |
|
$_iIntervalScheduling = (int) $_oDateIntervalScheduling->format('%Y%M%D%H%I%S'); |
178
|
|
|
|
179
|
9 |
|
return ($_iIntervalScheduling > $_iIntervalEpsilon); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
// if epsilon is less or equal than 30sec the not empty check is enough |
183
|
1 |
|
return true; |
184
|
|
|
} |
185
|
1 |
|
catch (\Exception $_oException) |
186
|
|
|
{ |
187
|
|
|
// can't init \DateInterval instance |
188
|
1 |
|
return false; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
// else |
193
|
3 |
|
return (!empty($oJobEntity->epsilon)); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param array $aConstraints |
198
|
|
|
* @return bool |
199
|
|
|
*/ |
200
|
13 |
|
private function isConstraintsPropertyValid(array $aConstraints) |
201
|
|
|
{ |
202
|
13 |
|
if (!empty($aConstraints)) |
203
|
13 |
|
{ |
204
|
1 |
|
foreach ($aConstraints as $_aConstraint) |
205
|
|
|
{ |
206
|
1 |
|
if (!is_array($_aConstraint) || count($_aConstraint) != 3) |
207
|
1 |
|
{ |
208
|
1 |
|
return false; |
209
|
|
|
} |
210
|
1 |
|
} |
211
|
1 |
|
} |
212
|
|
|
|
213
|
13 |
|
return true; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param JobEntity\ContainerEntity $oContainer |
218
|
|
|
* @return bool |
219
|
|
|
* |
220
|
|
|
* @see http://mesos.github.io/chronos/docs/api.html#adding-a-docker-job |
221
|
|
|
* This contains the subfields for the Docker container: |
222
|
|
|
* type (required), image (required), forcePullImage (optional), network (optional), |
223
|
|
|
* and volumes (optional) |
224
|
|
|
*/ |
225
|
13 |
|
private function isContainerPropertyValid($oContainer) |
226
|
|
|
{ |
227
|
13 |
|
if (is_null($oContainer)) |
228
|
13 |
|
{ |
229
|
10 |
|
return true; |
230
|
|
|
} |
231
|
|
|
|
232
|
3 |
|
if (empty($oContainer->type) || empty($oContainer->image)) |
233
|
3 |
|
{ |
234
|
2 |
|
return false; |
235
|
|
|
} |
236
|
|
|
|
237
|
2 |
|
if (!is_array($oContainer->volumes)) |
238
|
2 |
|
{ |
239
|
|
|
return false; |
240
|
|
|
} |
241
|
|
|
|
242
|
2 |
|
foreach ($oContainer->volumes as $_oVolume) |
243
|
|
|
{ |
244
|
2 |
|
if (!in_array($_oVolume->mode, ['RO', 'RW'])) |
245
|
2 |
|
{ |
246
|
1 |
|
return false; |
247
|
|
|
} |
248
|
2 |
|
} |
249
|
|
|
|
250
|
2 |
|
return true; |
251
|
|
|
} |
252
|
|
|
} |