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
|
|
|
'currentbuild', |
17
|
|
|
'show', |
18
|
|
|
'log', |
19
|
|
|
'start', |
20
|
|
|
'save' |
21
|
|
|
|
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
|
|
View Code Duplication |
public function init() { |
|
|
|
|
42
|
|
|
parent::init(); |
43
|
|
|
|
44
|
|
|
$this->project = $this->getCurrentProject(); |
45
|
|
|
|
46
|
|
|
if (!$this->project) { |
47
|
|
|
return $this->project404Response(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// Performs canView permission check by limiting visible projects |
51
|
|
|
$this->environment = $this->getCurrentEnvironment($this->project); |
52
|
|
|
if (!$this->environment) { |
53
|
|
|
return $this->environment404Response(); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param \SS_HTTPRequest $request |
59
|
|
|
* @return \HTMLText|\SS_HTTPResponse |
60
|
|
|
*/ |
61
|
|
|
public function index(\SS_HTTPRequest $request) { |
62
|
|
|
return $this->redirect(\Controller::join_links($this->Link(), 'history'), 302); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param \SS_HTTPRequest $request |
67
|
|
|
* @return \SS_HTTPResponse |
68
|
|
|
*/ |
69
|
|
|
public function history(SS_HTTPRequest $request) { |
70
|
|
|
$data = []; |
71
|
|
|
$list = $this->DeployHistory(); |
|
|
|
|
72
|
|
|
$page = $request->getVar('page') ?: 1; |
73
|
|
|
if ($page > $list->TotalPages()) { |
74
|
|
|
$page = 1; |
75
|
|
|
} |
76
|
|
|
if ($page < 1) { |
77
|
|
|
$page = 1; |
78
|
|
|
} |
79
|
|
|
$start = ($page - 1) * $list->getPageLength(); |
80
|
|
|
$list->setPageStart((int) $start); |
81
|
|
|
if (empty($list)) { |
82
|
|
|
return $this->getAPIResponse(['list' => []], 200); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
foreach ($list as $deployment) { |
86
|
|
|
$data[] = $this->getDeploymentData($deployment); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->getAPIResponse([ |
90
|
|
|
'list' => $data, |
91
|
|
|
'page_length' => $list->getPageLength(), |
92
|
|
|
'total_pages' => $list->TotalPages(), |
93
|
|
|
'current_page' => $list->CurrentPage() |
94
|
|
|
], 200); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param \SS_HTTPRequest $request |
99
|
|
|
* @return \SS_HTTPResponse |
100
|
|
|
*/ |
101
|
|
|
public function currentbuild(SS_HTTPRequest $request) { |
102
|
|
|
$currentBuild = $this->environment->CurrentBuild(); |
103
|
|
|
if (!$currentBuild) { |
104
|
|
|
return $this->getAPIResponse(['deployment' => []], 200); |
105
|
|
|
} |
106
|
|
|
return $this->getAPIResponse(['deployment' => $this->getDeploymentData($currentBuild)], 200); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param \SS_HTTPRequest $request |
111
|
|
|
* @return \SS_HTTPResponse |
112
|
|
|
*/ |
113
|
|
|
public function show(SS_HTTPRequest $request) { |
114
|
|
|
$deployment = DNDeployment::get()->byId($request->param('ID')); |
115
|
|
|
$errorResponse = $this->validateDeployment($deployment); |
|
|
|
|
116
|
|
|
if ($errorResponse instanceof \SS_HTTPResponse) { |
117
|
|
|
return $errorResponse; |
118
|
|
|
} |
119
|
|
|
return $this->getAPIResponse(['deployment' => $this->getDeploymentData($deployment)], 200); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param \SS_HTTPRequest $request |
124
|
|
|
* @return \SS_HTTPResponse |
125
|
|
|
*/ |
126
|
|
|
public function log(SS_HTTPRequest $request) { |
127
|
|
|
$deployment = DNDeployment::get()->byId($request->param('ID')); |
128
|
|
|
$errorResponse = $this->validateDeployment($deployment); |
|
|
|
|
129
|
|
|
if ($errorResponse instanceof \SS_HTTPResponse) { |
130
|
|
|
return $errorResponse; |
131
|
|
|
} |
132
|
|
|
$log = $deployment->log(); |
133
|
|
|
$content = $log->exists() ? $log->content() : 'Waiting for action to start'; |
134
|
|
|
$lines = explode(PHP_EOL, $content); |
135
|
|
|
|
136
|
|
|
return $this->getAPIResponse(['message' => $lines, 'status' => $deployment->Status], 200); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function save(\SS_HTTPRequest $request) { |
140
|
|
|
|
141
|
|
|
if (strtolower($request->httpMethod()) !== 'post') { |
142
|
|
|
return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$this->checkSecurityToken(); |
146
|
|
|
if (!$this->environment->canDeploy(Member::currentUser())) { |
|
|
|
|
147
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorised to deploy this environment'], 403); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// @todo the strategy should have been saved when there has been a request for an |
151
|
|
|
// approval or a bypass. This saved state needs to be checked if it's invalidated |
152
|
|
|
// if another deploy happens before this one |
153
|
|
|
$options = [ |
154
|
|
|
'sha' => $request->requestVar('ref'), |
155
|
|
|
'ref_type' => $request->requestVar('ref_type'), |
156
|
|
|
'branch' => $request->requestVar('ref_name'), |
157
|
|
|
'summary' => $request->requestVar('summary') |
158
|
|
|
]; |
159
|
|
|
$strategy = $this->environment->Backend()->planDeploy($this->environment, $options); |
160
|
|
|
|
161
|
|
|
$strategy->fromArray($request->requestVars()); |
|
|
|
|
162
|
|
|
$deployment = $strategy->createDeployment(); |
163
|
|
|
$deployment->getMachine()->apply(DNDeployment::TR_SUBMIT); |
164
|
|
|
return $this->getAPIResponse([ |
165
|
|
|
'message' => 'deployment has been created', |
166
|
|
|
'id' => $deployment->ID, |
167
|
|
|
], 201); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param \SS_HTTPRequest $request |
172
|
|
|
* @return \SS_HTTPResponse |
173
|
|
|
*/ |
174
|
|
|
public function start(SS_HTTPRequest $request) { |
175
|
|
|
$this->checkSecurityToken(); |
176
|
|
|
|
177
|
|
|
if (!$this->environment->canDeploy(Member::currentUser())) { |
|
|
|
|
178
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorised to deploy this environment'], 403); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// @todo the strategy should have been saved when there has been a request for an |
182
|
|
|
// approval or a bypass. This saved state needs to be checked if it's invalidated |
183
|
|
|
// if another deploy happens before this one |
184
|
|
|
$options = [ |
185
|
|
|
'sha' => $request->requestVar('sha'), |
186
|
|
|
]; |
187
|
|
|
$strategy = $this->environment->Backend()->planDeploy($this->environment, $options); |
188
|
|
|
|
189
|
|
|
$strategy->fromArray($request->requestVars()); |
|
|
|
|
190
|
|
|
$deployment = $strategy->createDeployment(); |
191
|
|
|
|
192
|
|
|
// Skip through the approval state for now. |
193
|
|
|
$deployment->getMachine()->apply(DNDeployment::TR_SUBMIT); |
194
|
|
|
$deployment->getMachine()->apply(DNDeployment::TR_QUEUE); |
195
|
|
|
|
196
|
|
|
$location = \Controller::join_links(Director::absoluteBaseURL(), $this->Link('log'), $deployment->ID); |
197
|
|
|
|
198
|
|
|
$response = $this->getAPIResponse([ |
199
|
|
|
'message' => 'deployment has been queued', |
200
|
|
|
'id' => $deployment->ID, |
201
|
|
|
'location' => $location |
202
|
|
|
], 201); |
203
|
|
|
$response->addHeader('Location', $location); |
204
|
|
|
return $response; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param string $action |
209
|
|
|
* @return string |
210
|
|
|
*/ |
211
|
|
|
public function Link($action = '') { |
212
|
|
|
return \Controller::join_links($this->environment->Link(), self::ACTION_DEPLOY, $action); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param string $name |
217
|
|
|
* @return array |
218
|
|
|
*/ |
219
|
|
|
public function getModel($name = '') { |
220
|
|
|
return []; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Return data about a single deployment for use in API response. |
225
|
|
|
* @param DNDeployment $deployment |
226
|
|
|
* @return array |
227
|
|
|
*/ |
228
|
|
|
protected function getDeploymentData(DNDeployment $deployment) { |
229
|
|
|
$currentBuild = $this->environment->CurrentBuild(); |
230
|
|
|
|
231
|
|
|
$deployer = $deployment->Deployer(); |
232
|
|
|
$deployerData = null; |
233
|
|
|
if ($deployer && $deployer->exists()) { |
234
|
|
|
$deployerData = $this->getStackMemberData($deployer); |
235
|
|
|
} |
236
|
|
|
$approver = $deployment->Approver(); |
|
|
|
|
237
|
|
|
$approverData = null; |
238
|
|
|
if ($approver && $approver->exists()) { |
239
|
|
|
$approverData = $this->getStackMemberData($approver); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return [ |
243
|
|
|
'id' => $deployment->ID, |
244
|
|
|
'created' => $deployment->Created, |
245
|
|
|
'date_planned' => $deployment->DatePlanned, |
|
|
|
|
246
|
|
|
'summary' => $deployment->Summary, |
|
|
|
|
247
|
|
|
'branch' => $deployment->Branch, |
|
|
|
|
248
|
|
|
'tags' => $deployment->getTags()->toArray(), |
249
|
|
|
'changes' => $deployment->getDeploymentStrategy()->getChanges(), |
250
|
|
|
'sha' => $deployment->SHA, |
251
|
|
|
'ref_type' => $deployment->RefType, |
252
|
|
|
'commit_message' => $deployment->getCommitMessage(), |
253
|
|
|
'commit_url' => $deployment->getCommitURL(), |
254
|
|
|
'deployer' => $deployerData, |
255
|
|
|
'approver' => $approverData, |
256
|
|
|
'state' => $deployment->State, |
257
|
|
|
'is_current_build' => $currentBuild ? ($deployment->ID === $currentBuild->ID) : null |
258
|
|
|
]; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Return data about a particular {@link Member} of the stack for use in API response. |
263
|
|
|
* Note that role can be null in the response. This is the case of an admin, or an operations |
264
|
|
|
* user who can create the deployment but is not part of the stack roles. |
265
|
|
|
* |
266
|
|
|
* @param Member $member |
267
|
|
|
* @return array |
268
|
|
|
*/ |
269
|
|
|
protected function getStackMemberData(Member $member) { |
270
|
|
|
$stackMembers = $this->project->listMembers(); |
|
|
|
|
271
|
|
|
$role = null; |
272
|
|
|
|
273
|
|
|
foreach ($stackMembers as $stackMember) { |
274
|
|
|
if ($stackMember['MemberID'] !== $member->ID) { |
275
|
|
|
continue; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
$role = $stackMember['RoleTitle']; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
return [ |
282
|
|
|
'id' => $member->ID, |
283
|
|
|
'email' => $member->Email, |
284
|
|
|
'role' => $role, |
285
|
|
|
'name' => $member->getName() |
286
|
|
|
]; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
291
|
|
|
* an APIResponse with the error, otherwise null. |
292
|
|
|
* |
293
|
|
|
* @param \DNDeployment $deployment |
294
|
|
|
* |
295
|
|
|
* @return null|SS_HTTPResponse |
296
|
|
|
*/ |
297
|
|
|
protected function validateDeployment(\DNDeployment $deployment) { |
298
|
|
|
if (!$deployment || !$deployment->exists()) { |
299
|
|
|
return $this->getAPIResponse(['message' => 'This deployment does not exist'], 404); |
300
|
|
|
} |
301
|
|
|
if (!$deployment->canView()) { |
302
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorised to view this deployment'], 403); |
303
|
|
|
} |
304
|
|
|
return null; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
} |
308
|
|
|
|
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
.