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