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 $action_types = [ |
15
|
|
|
self::ACTION_DEPLOY |
16
|
|
|
]; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
public static $allowed_actions = [ |
22
|
|
|
'history', |
23
|
|
|
'currentbuild', |
24
|
|
|
'show', |
25
|
|
|
'log', |
26
|
|
|
'start' |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \DNProject |
31
|
|
|
*/ |
32
|
|
|
protected $project = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \DNEnvironment |
36
|
|
|
*/ |
37
|
|
|
protected $environment = null; |
38
|
|
|
|
39
|
|
View Code Duplication |
public function init() { |
|
|
|
|
40
|
|
|
parent::init(); |
41
|
|
|
|
42
|
|
|
$this->project = $this->getCurrentProject(); |
43
|
|
|
|
44
|
|
|
if (!$this->project) { |
45
|
|
|
return $this->project404Response(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Performs canView permission check by limiting visible projects |
49
|
|
|
$this->environment = $this->getCurrentEnvironment($this->project); |
50
|
|
|
if (!$this->environment) { |
51
|
|
|
return $this->environment404Response(); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param \SS_HTTPRequest $request |
57
|
|
|
* @return \HTMLText|\SS_HTTPResponse |
58
|
|
|
*/ |
59
|
|
|
public function index(\SS_HTTPRequest $request) { |
60
|
|
|
return $this->redirect(\Controller::join_links($this->Link(), 'history'), 302); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param \SS_HTTPRequest $request |
65
|
|
|
* @return \SS_HTTPResponse |
66
|
|
|
*/ |
67
|
|
|
public function history(SS_HTTPRequest $request) { |
68
|
|
|
$data = []; |
69
|
|
|
$list = $this->DeployHistory(); |
|
|
|
|
70
|
|
|
$page = $request->getVar('page') ?: 1; |
71
|
|
|
if ($page > $list->TotalPages()) { |
72
|
|
|
$page = 1; |
73
|
|
|
} |
74
|
|
|
if ($page < 1) { |
75
|
|
|
$page = 1; |
76
|
|
|
} |
77
|
|
|
$start = ($page - 1) * $list->getPageLength(); |
78
|
|
|
$list->setPageStart((int) $start); |
79
|
|
|
if (empty($list)) { |
80
|
|
|
return $this->getAPIResponse(['list' => []], 200); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
foreach ($list as $deployment) { |
84
|
|
|
$data[] = $this->getDeploymentData($deployment); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->getAPIResponse([ |
88
|
|
|
'list' => $data, |
89
|
|
|
'page_length' => $list->getPageLength(), |
90
|
|
|
'total_pages' => $list->TotalPages(), |
91
|
|
|
'current_page' => $list->CurrentPage() |
92
|
|
|
], 200); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param \SS_HTTPRequest $request |
97
|
|
|
* @return \SS_HTTPResponse |
98
|
|
|
*/ |
99
|
|
|
public function currentbuild(SS_HTTPRequest $request) { |
100
|
|
|
$currentBuild = $this->environment->CurrentBuild(); |
101
|
|
|
if (!$currentBuild) { |
102
|
|
|
return $this->getAPIResponse(['deployment' => []], 200); |
103
|
|
|
} |
104
|
|
|
return $this->getAPIResponse(['deployment' => $this->getDeploymentData($currentBuild)], 200); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param \SS_HTTPRequest $request |
109
|
|
|
* @return \SS_HTTPResponse |
110
|
|
|
*/ |
111
|
|
|
public function show(SS_HTTPRequest $request) { |
112
|
|
|
$deployment = DNDeployment::get()->byId($request->param('ID')); |
113
|
|
|
if (!$deployment || !$deployment->exists()) { |
114
|
|
|
return $this->getAPIResponse(['message' => 'This deployment does not exist'], 404); |
115
|
|
|
} |
116
|
|
|
if (!$deployment->canView()) { |
117
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorized to deploy this environment'], 403); |
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->params('ID')); |
|
|
|
|
128
|
|
|
if (!$deployment || !$deployment->exists) { |
129
|
|
|
return $this->getAPIResponse(['message' => 'This deployment does not exist'], 404); |
130
|
|
|
} |
131
|
|
|
if(!$deployment->canView()) { |
132
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorized to view this environment'], 403); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$log = $deployment->log(); |
136
|
|
|
$content = $log->exists() ? $log->content() : 'Waiting for action to start'; |
137
|
|
|
$lines = explode(PHP_EOL, $content); |
138
|
|
|
|
139
|
|
|
return $this->getAPIResponse(['message' => $lines, 'status' => $deployment->Status], 200); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param \SS_HTTPRequest $request |
144
|
|
|
* @return \SS_HTTPResponse |
145
|
|
|
*/ |
146
|
|
|
public function start(SS_HTTPRequest $request) { |
147
|
|
|
$this->checkSecurityToken(); |
148
|
|
|
|
149
|
|
|
if (!$this->environment->canDeploy(Member::currentUser())) { |
|
|
|
|
150
|
|
|
return $this->getAPIResponse(['message' => 'You are not authorized to deploy this environment'], 403); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// @todo the strategy should have been saved when there has been a request for an |
154
|
|
|
// approval or a bypass. This saved state needs to be checked if it's invalidated |
155
|
|
|
// if another deploy happens before this one |
156
|
|
|
$options = [ |
157
|
|
|
'sha' => $request->requestVar('sha'), |
158
|
|
|
]; |
159
|
|
|
$strategy = $this->environment->Backend()->planDeploy($this->environment, $options); |
160
|
|
|
|
161
|
|
|
$strategy->fromArray($request->requestVars()); |
|
|
|
|
162
|
|
|
$deployment = $strategy->createDeployment(); |
163
|
|
|
$deployment->Summary = null; // todo add this |
|
|
|
|
164
|
|
|
$deployment->DatePlanned = null; // todo add this |
|
|
|
|
165
|
|
|
|
166
|
|
|
// Skip through the approval state for now. |
167
|
|
|
$deployment->getMachine()->apply(DNDeployment::TR_SUBMIT); |
168
|
|
|
$deployment->getMachine()->apply(DNDeployment::TR_QUEUE); |
169
|
|
|
|
170
|
|
|
$location = \Controller::join_links(Director::absoluteBaseURL(), $this->Link('log'), $deployment->ID); |
171
|
|
|
|
172
|
|
|
$response = $this->getAPIResponse([ |
173
|
|
|
'message' => 'deployment has been queued', |
174
|
|
|
'id' => $deployment->ID, |
175
|
|
|
'location' => $location |
176
|
|
|
], 201); |
177
|
|
|
$response->addHeader('Location', $location); |
178
|
|
|
return $response; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param string $action |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
public function Link($action = '') { |
186
|
|
|
return \Controller::join_links($this->environment->Link(), self::ACTION_DEPLOY, $action); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param string $name |
191
|
|
|
* @return array |
192
|
|
|
*/ |
193
|
|
|
public function getModel($name = '') { |
194
|
|
|
return []; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Return data about a single deployment for use in API response. |
199
|
|
|
* @param DNDeployment $deployment |
200
|
|
|
* @return array |
201
|
|
|
*/ |
202
|
|
|
protected function getDeploymentData(DNDeployment $deployment) { |
203
|
|
|
$currentBuild = $this->environment->CurrentBuild(); |
204
|
|
|
|
205
|
|
|
$deployer = $deployment->Deployer(); |
206
|
|
|
$deployerData = null; |
207
|
|
|
if ($deployer && $deployer->exists()) { |
208
|
|
|
$deployerData = $this->getStackMemberData($deployer); |
209
|
|
|
} |
210
|
|
|
$approver = $deployment->Approver(); |
|
|
|
|
211
|
|
|
$approverData = null; |
212
|
|
|
if ($approver && $approver->exists()) { |
213
|
|
|
$approverData = $this->getStackMemberData($approver); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return [ |
217
|
|
|
'id' => $deployment->ID, |
218
|
|
|
'created' => $deployment->Created, |
219
|
|
|
'date_planned' => $deployment->DatePlanned, |
|
|
|
|
220
|
|
|
'summary' => $deployment->Summary, |
|
|
|
|
221
|
|
|
'branch' => $deployment->Branch, |
|
|
|
|
222
|
|
|
'tags' => $deployment->getTags()->toArray(), |
223
|
|
|
'changes' => $deployment->getDeploymentStrategy()->getChanges(), |
224
|
|
|
'sha' => $deployment->SHA, |
225
|
|
|
'commit_message' => $deployment->getCommitMessage(), |
226
|
|
|
'commit_url' => $deployment->getCommitURL(), |
227
|
|
|
'deployer' => $deployerData, |
228
|
|
|
'approver' => $approverData, |
229
|
|
|
'state' => $deployment->State, |
230
|
|
|
'is_current_build' => $currentBuild ? ($deployment->ID === $currentBuild->ID) : null |
231
|
|
|
]; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Return data about a particular {@link Member} of the stack for use in API response. |
236
|
|
|
* Note that role can be null in the response. This is the case of an admin, or an operations |
237
|
|
|
* user who can create the deployment but is not part of the stack roles. |
238
|
|
|
* |
239
|
|
|
* @param Member $member |
240
|
|
|
* @return array |
241
|
|
|
*/ |
242
|
|
|
protected function getStackMemberData(Member $member) { |
243
|
|
|
$stackMembers = $this->project->listMembers(); |
|
|
|
|
244
|
|
|
$role = null; |
245
|
|
|
|
246
|
|
|
foreach ($stackMembers as $stackMember) { |
247
|
|
|
if ($stackMember['MemberID'] !== $member->ID) { |
248
|
|
|
continue; |
249
|
|
|
} |
250
|
|
|
$role = $stackMember['RoleTitle']; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return [ |
254
|
|
|
'id' => $member->ID, |
255
|
|
|
'email' => $member->Email, |
256
|
|
|
'role' => $role, |
257
|
|
|
'name' => $member->getName() |
258
|
|
|
]; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
} |
262
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.