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