|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This dispatcher takes care of updating and returning information about this |
|
5
|
|
|
* projects git repository |
|
6
|
|
|
*/ |
|
7
|
|
|
class DeployDispatcher extends Dispatcher { |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
const ACTION_DEPLOY = 'deploys'; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
public static $allowed_actions = [ |
|
15
|
|
|
'history', |
|
16
|
|
|
'upcoming', |
|
17
|
|
|
'currentbuild', |
|
18
|
|
|
'show', |
|
19
|
|
|
'log', |
|
20
|
|
|
'start', |
|
21
|
|
|
'save' |
|
22
|
|
|
|
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var \DNProject |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $project = null; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var \DNEnvironment |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $environment = null; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
private static $action_types = [ |
|
|
|
|
|
|
39
|
|
|
self::ACTION_DEPLOY |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* This is a per request cache of $this->project()->listMembers() |
|
44
|
|
|
* |
|
45
|
|
|
* @var null|array |
|
46
|
|
|
*/ |
|
47
|
|
|
private static $_cache_project_members = null; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* This is a per request cache of $this->environment->CurrentBuild(); |
|
51
|
|
|
* |
|
52
|
|
|
* @var null|DNDeployment |
|
53
|
|
|
*/ |
|
54
|
|
|
private static $_cache_current_build = null; |
|
55
|
|
|
|
|
56
|
|
View Code Duplication |
public function init() { |
|
|
|
|
|
|
57
|
|
|
parent::init(); |
|
58
|
|
|
|
|
59
|
|
|
$this->project = $this->getCurrentProject(); |
|
60
|
|
|
|
|
61
|
|
|
if (!$this->project) { |
|
62
|
|
|
return $this->project404Response(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
// Performs canView permission check by limiting visible projects |
|
66
|
|
|
$this->environment = $this->getCurrentEnvironment($this->project); |
|
67
|
|
|
if (!$this->environment) { |
|
68
|
|
|
return $this->environment404Response(); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param \SS_HTTPRequest $request |
|
74
|
|
|
* @return \HTMLText|\SS_HTTPResponse |
|
75
|
|
|
*/ |
|
76
|
|
|
public function index(\SS_HTTPRequest $request) { |
|
77
|
|
|
return $this->redirect(\Controller::join_links($this->Link(), 'history'), 302); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param \SS_HTTPRequest $request |
|
82
|
|
|
* @return \SS_HTTPResponse |
|
83
|
|
|
*/ |
|
84
|
|
View Code Duplication |
public function history(SS_HTTPRequest $request) { |
|
|
|
|
|
|
85
|
|
|
$data = []; |
|
86
|
|
|
|
|
87
|
|
|
$list = $this->environment->DeployHistory('DeployStarted'); |
|
88
|
|
|
|
|
89
|
|
|
foreach ($list as $deployment) { |
|
90
|
|
|
$data[] = $this->getDeploymentData($deployment); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $this->getAPIResponse([ |
|
94
|
|
|
'list' => $data, |
|
95
|
|
|
], 200); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param \SS_HTTPRequest $request |
|
100
|
|
|
* @return \SS_HTTPResponse |
|
101
|
|
|
*/ |
|
102
|
|
View Code Duplication |
public function upcoming(SS_HTTPRequest $request) { |
|
|
|
|
|
|
103
|
|
|
$data = []; |
|
104
|
|
|
$list = $this->environment->UpcomingDeployments(); |
|
105
|
|
|
foreach ($list as $deployment) { |
|
106
|
|
|
$data[] = $this->getDeploymentData($deployment); |
|
107
|
|
|
} |
|
108
|
|
|
return $this->getAPIResponse([ |
|
109
|
|
|
'list' => $data, |
|
110
|
|
|
], 200); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param \SS_HTTPRequest $request |
|
115
|
|
|
* @return \SS_HTTPResponse |
|
116
|
|
|
*/ |
|
117
|
|
|
public function currentbuild(SS_HTTPRequest $request) { |
|
118
|
|
|
$currentBuild = $this->environment->CurrentBuild(); |
|
119
|
|
|
if (!$currentBuild) { |
|
120
|
|
|
return $this->getAPIResponse(['deployment' => []], 200); |
|
121
|
|
|
} |
|
122
|
|
|
return $this->getAPIResponse(['deployment' => $this->getDeploymentData($currentBuild)], 200); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param \SS_HTTPRequest $request |
|
127
|
|
|
* @return \SS_HTTPResponse |
|
128
|
|
|
*/ |
|
129
|
|
|
public function show(SS_HTTPRequest $request) { |
|
130
|
|
|
$deployment = DNDeployment::get()->byId($request->param('ID')); |
|
131
|
|
|
$errorResponse = $this->validateDeployment($deployment); |
|
|
|
|
|
|
132
|
|
|
if ($errorResponse instanceof \SS_HTTPResponse) { |
|
133
|
|
|
return $errorResponse; |
|
134
|
|
|
} |
|
135
|
|
|
return $this->getAPIResponse(['deployment' => $this->getDeploymentData($deployment)], 200); |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param \SS_HTTPRequest $request |
|
140
|
|
|
* @return \SS_HTTPResponse |
|
141
|
|
|
*/ |
|
142
|
|
|
public function log(SS_HTTPRequest $request) { |
|
143
|
|
|
$deployment = DNDeployment::get()->byId($request->param('ID')); |
|
144
|
|
|
$errorResponse = $this->validateDeployment($deployment); |
|
|
|
|
|
|
145
|
|
|
if ($errorResponse instanceof \SS_HTTPResponse) { |
|
146
|
|
|
return $errorResponse; |
|
147
|
|
|
} |
|
148
|
|
|
$log = $deployment->log(); |
|
149
|
|
|
$content = $log->exists() ? $log->content() : 'Waiting for action to start'; |
|
150
|
|
|
$lines = explode(PHP_EOL, $content); |
|
151
|
|
|
|
|
152
|
|
|
return $this->getAPIResponse([ |
|
153
|
|
|
'message' => $lines, |
|
154
|
|
|
'status' => $deployment->Status, |
|
155
|
|
|
'deployment' => $this->getDeploymentData($deployment), |
|
|
|
|
|
|
156
|
|
|
], 200); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function save(\SS_HTTPRequest $request) { |
|
160
|
|
|
|
|
161
|
|
|
if ($request->httpMethod() !== 'POST') { |
|
162
|
|
|
return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$this->checkSecurityToken(); |
|
166
|
|
|
if (!$this->environment->canDeploy(Member::currentUser())) { |
|
|
|
|
|
|
167
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorised to deploy this environment'], 403); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
// @todo the strategy should have been saved when there has been a request for an |
|
171
|
|
|
// approval or a bypass. This saved state needs to be checked if it's invalidated |
|
172
|
|
|
// if another deploy happens before this one |
|
173
|
|
|
$options = [ |
|
174
|
|
|
'sha' => $request->requestVar('ref'), |
|
175
|
|
|
'ref_type' => $request->requestVar('ref_type'), |
|
176
|
|
|
'branch' => $request->requestVar('ref_name'), |
|
177
|
|
|
'summary' => $request->requestVar('summary') |
|
178
|
|
|
]; |
|
179
|
|
|
$strategy = $this->environment->Backend()->planDeploy($this->environment, $options); |
|
180
|
|
|
|
|
181
|
|
|
$strategy->fromArray($request->requestVars()); |
|
|
|
|
|
|
182
|
|
|
$deployment = $strategy->createDeployment(); |
|
183
|
|
|
$deployment->getMachine()->apply(DNDeployment::TR_SUBMIT); |
|
184
|
|
|
return $this->getAPIResponse([ |
|
185
|
|
|
'message' => 'deployment has been created', |
|
186
|
|
|
'id' => $deployment->ID, |
|
187
|
|
|
'deployment' => $this->getDeploymentData($deployment), |
|
188
|
|
|
], 201); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param \SS_HTTPRequest $request |
|
193
|
|
|
* @return \SS_HTTPResponse |
|
194
|
|
|
*/ |
|
195
|
|
|
public function start(SS_HTTPRequest $request) { |
|
196
|
|
|
if ($request->httpMethod() !== 'POST') { |
|
197
|
|
|
return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$this->checkSecurityToken(); |
|
201
|
|
|
|
|
202
|
|
|
$deployment = DNDeployment::get()->byId($request->param('ID')); |
|
203
|
|
|
|
|
204
|
|
|
if (!$deployment || !$deployment->exists()) { |
|
205
|
|
|
return $this->getAPIResponse(['message' => 'This deployment does not exist'], 404); |
|
206
|
|
|
} |
|
207
|
|
|
if (!$this->environment->canDeploy(Member::currentUser())) { |
|
|
|
|
|
|
208
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorised to deploy this environment'], 403); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
// until we have a system that can invalidate currently scheduled deployments due |
|
212
|
|
|
// to emergency deploys etc, replan the deployment to check if it's still valid. |
|
213
|
|
|
|
|
214
|
|
|
$options = $deployment->getDeploymentStrategy()->getOptions(); |
|
215
|
|
|
|
|
216
|
|
|
$strategy = $this->environment->Backend()->planDeploy($this->environment, $options); |
|
217
|
|
|
$deployment->Strategy = $strategy->toJSON(); |
|
218
|
|
|
$deployment->write(); |
|
219
|
|
|
|
|
220
|
|
|
$deployment->getMachine()->apply(DNDeployment::TR_QUEUE); |
|
221
|
|
|
|
|
222
|
|
|
$location = \Controller::join_links(Director::absoluteBaseURL(), $this->Link('log'), $deployment->ID); |
|
223
|
|
|
|
|
224
|
|
|
$response = $this->getAPIResponse([ |
|
225
|
|
|
'message' => 'deployment has been queued', |
|
226
|
|
|
'id' => $deployment->ID, |
|
227
|
|
|
'location' => $location, |
|
228
|
|
|
'deployment' => $this->getDeploymentData($deployment), |
|
|
|
|
|
|
229
|
|
|
], 201); |
|
230
|
|
|
$response->addHeader('Location', $location); |
|
231
|
|
|
return $response; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @param string $action |
|
236
|
|
|
* @return string |
|
237
|
|
|
*/ |
|
238
|
|
|
public function Link($action = '') { |
|
239
|
|
|
return \Controller::join_links($this->environment->Link(), self::ACTION_DEPLOY, $action); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @param string $name |
|
244
|
|
|
* @return array |
|
245
|
|
|
*/ |
|
246
|
|
|
public function getModel($name = '') { |
|
247
|
|
|
return []; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Return data about a single deployment for use in API response. |
|
252
|
|
|
* @param DNDeployment $deployment |
|
253
|
|
|
* @return array |
|
254
|
|
|
*/ |
|
255
|
|
|
protected function getDeploymentData(DNDeployment $deployment) { |
|
256
|
|
|
|
|
257
|
|
|
if (self::$_cache_current_build === null) { |
|
258
|
|
|
self::$_cache_current_build = $this->environment->CurrentBuild(); |
|
|
|
|
|
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
$deployer = $deployment->Deployer(); |
|
262
|
|
|
$deployerData = null; |
|
263
|
|
|
if ($deployer && $deployer->exists()) { |
|
264
|
|
|
$deployerData = $this->getStackMemberData($deployer); |
|
265
|
|
|
} |
|
266
|
|
|
$approver = $deployment->Approver(); |
|
|
|
|
|
|
267
|
|
|
$approverData = null; |
|
268
|
|
|
if ($approver && $approver->exists()) { |
|
269
|
|
|
$approverData = $this->getStackMemberData($approver); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
// failover for older deployments |
|
273
|
|
|
$started = $deployment->Created; |
|
274
|
|
|
if($deployment->DeployStarted) { |
|
275
|
|
|
$started = $deployment->DeployStarted; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
$requested = $deployment->Created; |
|
279
|
|
|
if($deployment->DeployRequested) { |
|
280
|
|
|
$requested = $deployment->DeployRequested; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
$isCurrentBuild = self::$_cache_current_build ? ($deployment->ID === self::$_cache_current_build->ID) : false; |
|
284
|
|
|
|
|
285
|
|
|
return [ |
|
286
|
|
|
'id' => $deployment->ID, |
|
287
|
|
|
'date_created' => $deployment->Created, |
|
288
|
|
|
'date_started' => $started, |
|
289
|
|
|
'date_requested' => $requested, |
|
290
|
|
|
'date_updated' => $deployment->LastEdited, |
|
291
|
|
|
'summary' => $deployment->Summary, |
|
|
|
|
|
|
292
|
|
|
'branch' => $deployment->Branch, |
|
|
|
|
|
|
293
|
|
|
'tags' => $deployment->getTags()->toArray(), |
|
294
|
|
|
'changes' => $deployment->getDeploymentStrategy()->getChanges(), |
|
295
|
|
|
'sha' => $deployment->SHA, |
|
296
|
|
|
'ref_type' => $deployment->RefType, |
|
297
|
|
|
'commit_message' => $deployment->getCommitMessage(), |
|
298
|
|
|
'commit_url' => $deployment->getCommitURL(), |
|
299
|
|
|
'deployer' => $deployerData, |
|
300
|
|
|
'approver' => $approverData, |
|
301
|
|
|
'state' => $deployment->State, |
|
302
|
|
|
'is_current_build' => $isCurrentBuild |
|
303
|
|
|
]; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* Return data about a particular {@link Member} of the stack for use in API response. |
|
308
|
|
|
* Note that role can be null in the response. This is the case of an admin, or an operations |
|
309
|
|
|
* user who can create the deployment but is not part of the stack roles. |
|
310
|
|
|
* |
|
311
|
|
|
* @param Member $member |
|
312
|
|
|
* @return array |
|
313
|
|
|
*/ |
|
314
|
|
|
protected function getStackMemberData(Member $member) { |
|
315
|
|
|
if (self::$_cache_project_members === null) { |
|
316
|
|
|
self::$_cache_project_members = $this->project->listMembers(); |
|
|
|
|
|
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
$role = null; |
|
320
|
|
|
|
|
321
|
|
|
foreach (self::$_cache_project_members as $stackMember) { |
|
322
|
|
|
if ($stackMember['MemberID'] !== $member->ID) { |
|
323
|
|
|
continue; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
$role = $stackMember['RoleTitle']; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
return [ |
|
330
|
|
|
'id' => $member->ID, |
|
331
|
|
|
'email' => $member->Email, |
|
332
|
|
|
'role' => $role, |
|
333
|
|
|
'name' => $member->getName() |
|
334
|
|
|
]; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
|
339
|
|
|
* an APIResponse with the error, otherwise null. |
|
340
|
|
|
* |
|
341
|
|
|
* @param \DNDeployment $deployment |
|
342
|
|
|
* |
|
343
|
|
|
* @return null|SS_HTTPResponse |
|
344
|
|
|
*/ |
|
345
|
|
|
protected function validateDeployment(\DNDeployment $deployment) { |
|
346
|
|
|
if (!$deployment || !$deployment->exists()) { |
|
347
|
|
|
return $this->getAPIResponse(['message' => 'This deployment does not exist'], 404); |
|
348
|
|
|
} |
|
349
|
|
|
if (!$deployment->canView()) { |
|
350
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorised to view this deployment'], 403); |
|
351
|
|
|
} |
|
352
|
|
|
return null; |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
} |
|
356
|
|
|
|
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.