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
|
|
|
private static $allowed_actions = [ |
|
|
|
|
15
|
|
|
'history', |
16
|
|
|
'upcoming', |
17
|
|
|
'currentbuild', |
18
|
|
|
'show', |
19
|
|
|
'delete', |
20
|
|
|
'log', |
21
|
|
|
'redeploy', |
22
|
|
|
'createdeployment', |
23
|
|
|
'start' |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
private static $dependencies = [ |
27
|
|
|
'formatter' => '%$DeploynautAPIFormatter' |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \DNProject |
32
|
|
|
*/ |
33
|
|
|
protected $project = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var \DNEnvironment |
37
|
|
|
*/ |
38
|
|
|
protected $environment = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private static $action_types = [ |
|
|
|
|
44
|
|
|
self::ACTION_DEPLOY |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
View Code Duplication |
public function init() { |
|
|
|
|
48
|
|
|
parent::init(); |
49
|
|
|
|
50
|
|
|
$this->project = $this->getCurrentProject(); |
51
|
|
|
|
52
|
|
|
if (!$this->project) { |
53
|
|
|
return $this->project404Response(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Performs canView permission check by limiting visible projects |
57
|
|
|
$this->environment = $this->getCurrentEnvironment($this->project); |
58
|
|
|
if (!$this->environment) { |
59
|
|
|
return $this->environment404Response(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param \SS_HTTPRequest $request |
65
|
|
|
* @return \HTMLText|\SS_HTTPResponse |
66
|
|
|
*/ |
67
|
|
|
public function index(\SS_HTTPRequest $request) { |
68
|
|
|
return $this->redirect(\Controller::join_links($this->Link(), 'history'), 302); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param \SS_HTTPRequest $request |
73
|
|
|
* @return \SS_HTTPResponse |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
public function history(\SS_HTTPRequest $request) { |
|
|
|
|
76
|
|
|
$data = []; |
77
|
|
|
|
78
|
|
|
$list = $this->environment->DeployHistory('DeployStarted'); |
79
|
|
|
|
80
|
|
|
foreach ($list as $deployment) { |
81
|
|
|
$data[] = $this->formatter->getDeploymentData($deployment); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->getAPIResponse([ |
85
|
|
|
'list' => $data, |
86
|
|
|
], 200); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param \SS_HTTPRequest $request |
91
|
|
|
* @return \SS_HTTPResponse |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
public function upcoming(\SS_HTTPRequest $request) { |
|
|
|
|
94
|
|
|
$data = []; |
95
|
|
|
$list = $this->environment->UpcomingDeployments(); |
96
|
|
|
foreach ($list as $deployment) { |
97
|
|
|
$data[] = $this->formatter->getDeploymentData($deployment); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
return $this->getAPIResponse([ |
100
|
|
|
'list' => $data, |
101
|
|
|
], 200); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param \SS_HTTPRequest $request |
106
|
|
|
* @return \SS_HTTPResponse |
107
|
|
|
*/ |
108
|
|
|
public function currentbuild(\SS_HTTPRequest $request) { |
109
|
|
|
$currentBuild = $this->environment->CurrentBuild(); |
110
|
|
|
if (!$currentBuild) { |
111
|
|
|
return $this->getAPIResponse(['deployment' => []], 200); |
112
|
|
|
} |
113
|
|
|
return $this->getAPIResponse(['deployment' => $this->formatter->getDeploymentData($currentBuild)], 200); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param \SS_HTTPRequest $request |
118
|
|
|
* @return \SS_HTTPResponse |
119
|
|
|
*/ |
120
|
|
|
public function show(\SS_HTTPRequest $request) { |
121
|
|
|
$deployment = DNDeployment::get()->byId($request->param('ID')); |
122
|
|
|
$errorResponse = $this->validateDeployment($deployment); |
|
|
|
|
123
|
|
|
if ($errorResponse instanceof \SS_HTTPResponse) { |
124
|
|
|
return $errorResponse; |
125
|
|
|
} |
126
|
|
|
return $this->getAPIResponse(['deployment' => $this->formatter->getDeploymentData($deployment)], 200); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function delete(\SS_HTTPRequest $request) { |
130
|
|
|
if ($request->httpMethod() !== 'POST') { |
131
|
|
|
return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->checkSecurityToken(); |
135
|
|
|
|
136
|
|
|
$deployment = DNDeployment::get()->byId($request->postVar('id')); |
137
|
|
|
$errorResponse = $this->validateDeployment($deployment); |
|
|
|
|
138
|
|
|
if ($errorResponse instanceof \SS_HTTPResponse) { |
139
|
|
|
return $errorResponse; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$id = $deployment->ID; |
143
|
|
|
$deployment->delete(); |
144
|
|
|
|
145
|
|
|
return $this->getAPIResponse([ |
146
|
|
|
'id' => $id, |
147
|
|
|
'message' => 'Deployment has been deleted' |
148
|
|
|
], 201); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param \SS_HTTPRequest $request |
153
|
|
|
* @return \SS_HTTPResponse |
154
|
|
|
*/ |
155
|
|
|
public function log(\SS_HTTPRequest $request) { |
156
|
|
|
$deployment = DNDeployment::get()->byId($request->param('ID')); |
157
|
|
|
$errorResponse = $this->validateDeployment($deployment); |
|
|
|
|
158
|
|
|
if ($errorResponse instanceof \SS_HTTPResponse) { |
159
|
|
|
return $errorResponse; |
160
|
|
|
} |
161
|
|
|
$log = $deployment->log(); |
162
|
|
|
$content = $log->exists() ? $log->content() : 'Waiting for action to start'; |
163
|
|
|
$lines = explode(PHP_EOL, $content); |
164
|
|
|
|
165
|
|
|
return $this->getAPIResponse([ |
166
|
|
|
'message' => $lines, |
167
|
|
|
'status' => $deployment->Status, |
168
|
|
|
'deployment' => $this->formatter->getDeploymentData($deployment), |
|
|
|
|
169
|
|
|
], 200); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param \SS_HTTPRequest $request |
174
|
|
|
* @return \SS_HTTPResponse |
175
|
|
|
*/ |
176
|
|
|
public function redeploy(\SS_HTTPRequest $request) { |
177
|
|
|
$currentBuild = $this->environment->CurrentBuild(); |
178
|
|
|
if (!$currentBuild || !$currentBuild->exists()) { |
179
|
|
|
return $this->redirect(Controller::join_links($this->environment->Link('overview'), 'deployment', 'new')); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$strategy = $this->environment->Backend()->planDeploy($this->environment, [ |
183
|
|
|
'sha' => $currentBuild->SHA, |
184
|
|
|
'ref_type' => GitDispatcher::REF_TYPE_PREVIOUS, |
185
|
|
|
'branch' => $currentBuild->Branch |
|
|
|
|
186
|
|
|
]); |
187
|
|
|
$deployment = $strategy->createDeployment(); |
188
|
|
|
|
189
|
|
|
return $this->redirect($deployment->Link()); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Create deployment. Can't use {@link create()} as it's taken by Object. |
194
|
|
|
* |
195
|
|
|
* @param \SS_HTTPRequest $request |
196
|
|
|
* @return \SS_HTTPResponse |
197
|
|
|
*/ |
198
|
|
|
public function createdeployment(\SS_HTTPRequest $request) { |
199
|
|
|
if ($request->httpMethod() !== 'POST') { |
200
|
|
|
return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$this->checkSecurityToken(); |
204
|
|
|
|
205
|
|
|
// @todo the strategy should have been saved when there has been a request for an |
206
|
|
|
// approval or a bypass. This saved state needs to be checked if it's invalidated |
207
|
|
|
// if another deploy happens before this one |
208
|
|
|
$isBranchDeploy = (int) $request->postVar('ref_type') === GitDispatcher::REF_TYPE_BRANCH; |
209
|
|
|
|
210
|
|
|
$options = [ |
211
|
|
|
'sha' => $request->postVar('ref'), |
212
|
|
|
'ref_type' => $request->postVar('ref_type'), |
213
|
|
|
'branch' => $isBranchDeploy ? $request->postVar('ref_name') : null, |
214
|
|
|
'title' => $request->postVar('title'), |
215
|
|
|
'summary' => $request->postVar('summary') |
216
|
|
|
]; |
217
|
|
|
|
218
|
|
View Code Duplication |
if ($request->postVar('options')) { |
|
|
|
|
219
|
|
|
foreach (explode(',', $request->postVar('options')) as $option) { |
220
|
|
|
$options[$option] = true; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$strategy = $this->environment->Backend()->planDeploy($this->environment, $options); |
225
|
|
|
|
226
|
|
|
$approver = Member::get()->byId($request->postVar('approver_id')); |
227
|
|
|
if ($approver && $approver->exists()) { |
228
|
|
|
if (!$this->project->allowed(ApprovalsDispatcher::ALLOW_APPROVAL, $approver)) { |
|
|
|
|
229
|
|
|
return $this->getAPIResponse(['message' => 'The given approver does not have permissions to approve'], 403); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$deployment = $strategy->createDeployment(); |
234
|
|
|
if ($approver && $approver->exists()) { |
235
|
|
|
$deployment->ApproverID = $approver->ID; |
|
|
|
|
236
|
|
|
$deployment->write(); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $this->getAPIResponse([ |
240
|
|
|
'message' => 'Deployment has been created', |
241
|
|
|
'deployment' => $this->formatter->getDeploymentData($deployment), |
|
|
|
|
242
|
|
|
], 201); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @param \SS_HTTPRequest $request |
247
|
|
|
* @return \SS_HTTPResponse |
248
|
|
|
*/ |
249
|
|
|
public function start(\SS_HTTPRequest $request) { |
250
|
|
|
if ($request->httpMethod() !== 'POST') { |
251
|
|
|
return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$this->checkSecurityToken(); |
255
|
|
|
|
256
|
|
|
$deployment = DNDeployment::get()->byId($request->postVar('id')); |
257
|
|
|
$errorResponse = $this->validateDeployment($deployment); |
|
|
|
|
258
|
|
|
if ($errorResponse instanceof \SS_HTTPResponse) { |
259
|
|
|
return $errorResponse; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
// The deployment cannot be started until it has been approved, or bypassed straight to approved state |
263
|
|
|
if ($deployment->State != DNDeployment::STATE_APPROVED) { |
264
|
|
|
return $this->getAPIResponse(['message' => 'This deployment has not been approved. Cannot deploy'], 403); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
// until we have a system that can invalidate currently scheduled deployments due |
268
|
|
|
// to emergency deploys etc, replan the deployment to check if it's still valid. |
269
|
|
|
$options = $deployment->getDeploymentStrategy()->getOptions(); |
270
|
|
|
$strategy = $this->environment->Backend()->planDeploy($this->environment, $options); |
271
|
|
|
$deployment->Strategy = $strategy->toJSON(); |
272
|
|
|
$deployment->write(); |
273
|
|
|
|
274
|
|
|
$deployment->getMachine()->apply(DNDeployment::TR_QUEUE); |
275
|
|
|
|
276
|
|
|
$location = \Controller::join_links(Director::absoluteBaseURL(), $this->Link('log'), $deployment->ID); |
277
|
|
|
|
278
|
|
|
$response = $this->getAPIResponse([ |
279
|
|
|
'message' => 'Deployment has been queued', |
280
|
|
|
'location' => $location, |
281
|
|
|
'deployment' => $this->formatter->getDeploymentData($deployment), |
|
|
|
|
282
|
|
|
], 201); |
283
|
|
|
|
284
|
|
|
$response->addHeader('Location', $location); |
285
|
|
|
|
286
|
|
|
return $response; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @param string $action |
291
|
|
|
* @return string |
292
|
|
|
*/ |
293
|
|
|
public function Link($action = '') { |
294
|
|
|
return \Controller::join_links($this->environment->Link(), self::ACTION_DEPLOY, $action); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @param string $name |
299
|
|
|
* @return array |
300
|
|
|
*/ |
301
|
|
|
public function getModel($name = '') { |
302
|
|
|
return []; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
307
|
|
|
* an APIResponse with the error, otherwise null. |
308
|
|
|
* |
309
|
|
|
* @param \DNDeployment $deployment |
310
|
|
|
* |
311
|
|
|
* @return null|SS_HTTPResponse |
312
|
|
|
*/ |
313
|
|
View Code Duplication |
protected function validateDeployment($deployment) { |
|
|
|
|
314
|
|
|
if (!$deployment || !$deployment->exists()) { |
315
|
|
|
return $this->getAPIResponse(['message' => 'This deployment does not exist'], 404); |
316
|
|
|
} |
317
|
|
|
if ($deployment->EnvironmentID != $this->environment->ID) { |
318
|
|
|
return $this->getAPIResponse(['message' => 'This deployment does not belong to the environment'], 403); |
319
|
|
|
} |
320
|
|
|
if (!$deployment->canView()) { |
321
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorised to view this deployment'], 403); |
322
|
|
|
} |
323
|
|
|
return null; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
} |
327
|
|
|
|
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
.