Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DNEnvironment often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DNEnvironment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class DNEnvironment extends DataObject { |
||
37 | |||
38 | const UAT = 'UAT'; |
||
39 | |||
40 | const PRODUCTION = 'Production'; |
||
41 | |||
42 | const UNSPECIFIED = 'Unspecified'; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | public static $db = [ |
||
48 | "Filename" => "Varchar(255)", |
||
49 | "Name" => "Varchar(255)", |
||
50 | "URL" => "Varchar(255)", |
||
51 | "BackendIdentifier" => "Varchar(255)", // Injector identifier of the DeploymentBackend |
||
52 | "Usage" => "Enum('Production, UAT, Test, Unspecified', 'Unspecified')", |
||
53 | ]; |
||
54 | |||
55 | /** |
||
56 | * @var array |
||
57 | */ |
||
58 | public static $has_many = [ |
||
59 | "Deployments" => "DNDeployment", |
||
60 | "DataArchives" => "DNDataArchive", |
||
61 | "DataTransfers" => "DNDataTransfer", |
||
62 | "Pings" => "DNPing" |
||
63 | ]; |
||
64 | |||
65 | /** |
||
66 | * @var array |
||
67 | */ |
||
68 | public static $many_many = [ |
||
69 | "Viewers" => "Member", // Who can view this environment |
||
70 | "ViewerGroups" => "Group", |
||
71 | "Deployers" => "Member", // Who can deploy to this environment |
||
72 | "DeployerGroups" => "Group", |
||
73 | "CanRestoreMembers" => "Member", // Who can restore archive files to this environment |
||
74 | "CanRestoreGroups" => "Group", |
||
75 | "CanBackupMembers" => "Member", // Who can backup archive files from this environment |
||
76 | "CanBackupGroups" => "Group", |
||
77 | "ArchiveUploaders" => "Member", // Who can upload archive files linked to this environment |
||
78 | "ArchiveUploaderGroups" => "Group", |
||
79 | "ArchiveDownloaders" => "Member", // Who can download archive files from this environment |
||
80 | "ArchiveDownloaderGroups" => "Group", |
||
81 | "ArchiveDeleters" => "Member", // Who can delete archive files from this environment, |
||
82 | "ArchiveDeleterGroups" => "Group", |
||
83 | ]; |
||
84 | |||
85 | /** |
||
86 | * @var array |
||
87 | */ |
||
88 | public static $summary_fields = [ |
||
89 | "Name" => "Environment Name", |
||
90 | "Usage" => "Usage", |
||
91 | "URL" => "URL", |
||
92 | "DeployersList" => "Can Deploy List", |
||
93 | "CanRestoreMembersList" => "Can Restore List", |
||
94 | "CanBackupMembersList" => "Can Backup List", |
||
95 | "ArchiveUploadersList" => "Can Upload List", |
||
96 | "ArchiveDownloadersList" => "Can Download List", |
||
97 | "ArchiveDeletersList" => "Can Delete List", |
||
98 | ]; |
||
99 | |||
100 | /** |
||
101 | * @var array |
||
102 | */ |
||
103 | public static $searchable_fields = [ |
||
104 | "Name", |
||
105 | ]; |
||
106 | |||
107 | private static $singular_name = 'Capistrano Environment'; |
||
108 | |||
109 | private static $plural_name = 'Capistrano Environments'; |
||
110 | |||
111 | /** |
||
112 | * @var string |
||
113 | */ |
||
114 | private static $default_sort = 'Name'; |
||
115 | |||
116 | /** |
||
117 | * @var array |
||
118 | */ |
||
119 | public static $has_one = [ |
||
120 | "Project" => "DNProject", |
||
121 | "CreateEnvironment" => "DNCreateEnvironment" |
||
122 | ]; |
||
123 | |||
124 | /** |
||
125 | * If this is set to a full pathfile, it will be used as template |
||
126 | * file when creating a new capistrano environment config file. |
||
127 | * |
||
128 | * If not set, the default 'environment.template' from the module |
||
129 | * root is used |
||
130 | * |
||
131 | * @config |
||
132 | * @var string |
||
133 | */ |
||
134 | private static $template_file = ''; |
||
135 | |||
136 | /** |
||
137 | * Set this to true to allow editing of the environment files via the web admin |
||
138 | * |
||
139 | * @var bool |
||
140 | */ |
||
141 | private static $allow_web_editing = false; |
||
142 | |||
143 | /** |
||
144 | * @var array |
||
145 | */ |
||
146 | private static $casting = [ |
||
147 | 'DeployHistory' => 'Text' |
||
148 | ]; |
||
149 | |||
150 | /** |
||
151 | * Allowed backends. A map of Injector identifier to human-readable label. |
||
152 | * |
||
153 | * @config |
||
154 | * @var array |
||
155 | */ |
||
156 | private static $allowed_backends = []; |
||
157 | |||
158 | /** |
||
159 | * Used by the sync task |
||
160 | * |
||
161 | * @param string $path |
||
162 | * @return \DNEnvironment |
||
163 | */ |
||
164 | public static function create_from_path($path) { |
||
165 | $e = DNEnvironment::create(); |
||
166 | $e->Filename = $path; |
||
167 | $e->Name = basename($e->Filename, '.rb'); |
||
168 | |||
169 | // add each administrator member as a deployer of the new environment |
||
170 | $adminGroup = Group::get()->filter('Code', 'administrators')->first(); |
||
171 | $e->DeployerGroups()->add($adminGroup); |
||
172 | return $e; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Get the deployment backend used for this environment. |
||
177 | * |
||
178 | * Enforces compliance with the allowed_backends setting; if the DNEnvironment.BackendIdentifier value is |
||
179 | * illegal then that value is ignored. |
||
180 | * |
||
181 | * @return DeploymentBackend |
||
182 | */ |
||
183 | public function Backend() { |
||
184 | $backends = array_keys($this->config()->get('allowed_backends', Config::FIRST_SET)); |
||
185 | switch (sizeof($backends)) { |
||
186 | // Nothing allowed, use the default value "DeploymentBackend" |
||
187 | case 0: |
||
188 | $backend = "DeploymentBackend"; |
||
189 | break; |
||
190 | |||
191 | // Only 1 thing allowed, use that |
||
192 | case 1: |
||
193 | $backend = $backends[0]; |
||
194 | break; |
||
195 | |||
196 | // Multiple choices, use our choice if it's legal, otherwise default to the first item on the list |
||
197 | default: |
||
198 | $backend = $this->BackendIdentifier; |
||
199 | if (!in_array($backend, $backends)) { |
||
200 | $backend = $backends[0]; |
||
201 | } |
||
202 | } |
||
203 | |||
204 | return Injector::inst()->get($backend); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @param SS_HTTPRequest $request |
||
209 | * |
||
210 | * @return DeploymentStrategy |
||
211 | */ |
||
212 | public function getDeployStrategy(\SS_HTTPRequest $request) { |
||
213 | return $this->Backend()->planDeploy($this, $request->requestVars()); |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Return the supported options for this environment. |
||
218 | * @return ArrayList |
||
219 | */ |
||
220 | public function getSupportedOptions() { |
||
221 | return $this->Backend()->getDeployOptions($this); |
||
222 | } |
||
223 | |||
224 | public function Menu() { |
||
225 | $list = new ArrayList(); |
||
226 | |||
227 | $controller = Controller::curr(); |
||
228 | $actionType = $controller->getField('CurrentActionType'); |
||
229 | |||
230 | $list->push(new ArrayData([ |
||
231 | 'Link' => $this->DeploymentsLink(), |
||
232 | 'Title' => 'Deployments', |
||
233 | 'IsCurrent' => $this->isCurrent(), |
||
234 | 'IsSection' => $this->isSection() && ($actionType == DNRoot::ACTION_DEPLOY || $actionType == \EnvironmentOverview::ACTION_OVERVIEW) |
||
235 | ])); |
||
236 | |||
237 | $this->extend('updateMenu', $list); |
||
238 | |||
239 | return $list; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Return the current object from $this->Menu() |
||
244 | * Good for making titles and things |
||
245 | */ |
||
246 | public function CurrentMenu() { |
||
247 | return $this->Menu()->filter('IsSection', true)->First(); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Return a name for this environment. |
||
252 | * |
||
253 | * @param string $separator The string used when concatenating project with env name |
||
254 | * @return string |
||
255 | */ |
||
256 | public function getFullName($separator = ':') { |
||
257 | return sprintf('%s%s%s', $this->Project()->Name, $separator, $this->Name); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * URL for the environment that can be used if no explicit URL is set. |
||
262 | */ |
||
263 | public function getDefaultURL() { |
||
264 | return null; |
||
265 | } |
||
266 | |||
267 | public function getBareURL() { |
||
268 | $url = parse_url($this->URL); |
||
269 | if (isset($url['host'])) { |
||
270 | return strtolower($url['host']); |
||
271 | } |
||
272 | } |
||
273 | |||
274 | public function getBareDefaultURL() { |
||
275 | $url = parse_url($this->getDefaultURL()); |
||
276 | if (isset($url['host'])) { |
||
277 | return strtolower($url['host']); |
||
278 | } |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Environments are only viewable by people that can view the environment. |
||
283 | * |
||
284 | * @param Member|null $member |
||
285 | * @return boolean |
||
286 | */ |
||
287 | public function canView($member = null) { |
||
288 | if (!$member) { |
||
289 | $member = Member::currentUser(); |
||
290 | } |
||
291 | if (!$member) { |
||
292 | return false; |
||
293 | } |
||
294 | // Must be logged in to check permissions |
||
295 | |||
296 | if (Permission::checkMember($member, 'ADMIN')) { |
||
297 | return true; |
||
298 | } |
||
299 | |||
300 | // if no Viewers or ViewerGroups defined, fallback to DNProject::canView permissions |
||
301 | if ($this->Viewers()->exists() || $this->ViewerGroups()->exists()) { |
||
302 | return $this->Viewers()->byID($member->ID) |
||
303 | || $member->inGroups($this->ViewerGroups()); |
||
304 | } |
||
305 | |||
306 | return $this->Project()->canView($member); |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Allow deploy only to some people. |
||
311 | * |
||
312 | * @param Member|null $member |
||
313 | * @return boolean |
||
314 | */ |
||
315 | View Code Duplication | public function canDeploy($member = null) { |
|
316 | if (!$member) { |
||
317 | $member = Member::currentUser(); |
||
318 | } |
||
319 | if (!$member) { |
||
320 | return false; |
||
321 | } |
||
322 | // Must be logged in to check permissions |
||
323 | |||
324 | if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
||
325 | if ($this->Project()->allowed(DNRoot::ALLOW_PROD_DEPLOYMENT, $member)) { |
||
326 | return true; |
||
327 | } |
||
328 | } else { |
||
329 | if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_DEPLOYMENT, $member)) { |
||
330 | return true; |
||
331 | } |
||
332 | } |
||
333 | |||
334 | return $this->Deployers()->byID($member->ID) |
||
335 | || $member->inGroups($this->DeployerGroups()); |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Provide reason why the user cannot deploy. |
||
340 | * |
||
341 | * @return string |
||
342 | */ |
||
343 | public function getCannotDeployMessage() { |
||
344 | return 'You cannot deploy to this environment.'; |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Allows only selected {@link Member} objects to restore {@link DNDataArchive} objects into this |
||
349 | * {@link DNEnvironment}. |
||
350 | * |
||
351 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
352 | * @return boolean true if $member can restore, and false if they can't. |
||
353 | */ |
||
354 | View Code Duplication | public function canRestore($member = null) { |
|
355 | if (!$member) { |
||
356 | $member = Member::currentUser(); |
||
357 | } |
||
358 | if (!$member) { |
||
359 | return false; |
||
360 | } |
||
361 | // Must be logged in to check permissions |
||
362 | |||
363 | if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
||
364 | if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) { |
||
365 | return true; |
||
366 | } |
||
367 | } else { |
||
368 | if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) { |
||
369 | return true; |
||
370 | } |
||
371 | } |
||
372 | |||
373 | return $this->CanRestoreMembers()->byID($member->ID) |
||
374 | || $member->inGroups($this->CanRestoreGroups()); |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * Allows only selected {@link Member} objects to backup this {@link DNEnvironment} to a {@link DNDataArchive} |
||
379 | * file. |
||
380 | * |
||
381 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
382 | * @return boolean true if $member can backup, and false if they can't. |
||
383 | */ |
||
384 | View Code Duplication | public function canBackup($member = null) { |
|
385 | $project = $this->Project(); |
||
386 | if ($project->HasDiskQuota() && $project->HasExceededDiskQuota()) { |
||
387 | return false; |
||
388 | } |
||
389 | |||
390 | if (!$member) { |
||
391 | $member = Member::currentUser(); |
||
392 | } |
||
393 | // Must be logged in to check permissions |
||
394 | if (!$member) { |
||
395 | return false; |
||
396 | } |
||
397 | |||
398 | if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
||
399 | if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) { |
||
400 | return true; |
||
401 | } |
||
402 | } else { |
||
403 | if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) { |
||
404 | return true; |
||
405 | } |
||
406 | } |
||
407 | |||
408 | return $this->CanBackupMembers()->byID($member->ID) |
||
409 | || $member->inGroups($this->CanBackupGroups()); |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * Allows only selected {@link Member} objects to upload {@link DNDataArchive} objects linked to this |
||
414 | * {@link DNEnvironment}. |
||
415 | * |
||
416 | * Note: This is not uploading them to the actual environment itself (e.g. uploading to the live site) - it is the |
||
417 | * process of uploading a *.sspak file into Deploynaut for later 'restoring' to an environment. See |
||
418 | * {@link self::canRestore()}. |
||
419 | * |
||
420 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
421 | * @return boolean true if $member can upload archives linked to this environment, false if they can't. |
||
422 | */ |
||
423 | View Code Duplication | public function canUploadArchive($member = null) { |
|
424 | $project = $this->Project(); |
||
425 | if ($project->HasDiskQuota() && $project->HasExceededDiskQuota()) { |
||
426 | return false; |
||
427 | } |
||
428 | |||
429 | if (!$member) { |
||
430 | $member = Member::currentUser(); |
||
431 | } |
||
432 | if (!$member) { |
||
433 | return false; |
||
434 | } |
||
435 | // Must be logged in to check permissions |
||
436 | |||
437 | if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
||
438 | if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) { |
||
439 | return true; |
||
440 | } |
||
441 | } else { |
||
442 | if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) { |
||
443 | return true; |
||
444 | } |
||
445 | } |
||
446 | |||
447 | return $this->ArchiveUploaders()->byID($member->ID) |
||
448 | || $member->inGroups($this->ArchiveUploaderGroups()); |
||
449 | } |
||
450 | |||
451 | /** |
||
452 | * Allows only selected {@link Member} objects to download {@link DNDataArchive} objects from this |
||
453 | * {@link DNEnvironment}. |
||
454 | * |
||
455 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
456 | * @return boolean true if $member can download archives from this environment, false if they can't. |
||
457 | */ |
||
458 | View Code Duplication | public function canDownloadArchive($member = null) { |
|
459 | if (!$member) { |
||
460 | $member = Member::currentUser(); |
||
461 | } |
||
462 | if (!$member) { |
||
463 | return false; |
||
464 | } |
||
465 | // Must be logged in to check permissions |
||
466 | |||
467 | if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
||
468 | if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) { |
||
469 | return true; |
||
470 | } |
||
471 | } else { |
||
472 | if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) { |
||
473 | return true; |
||
474 | } |
||
475 | } |
||
476 | |||
477 | return $this->ArchiveDownloaders()->byID($member->ID) |
||
478 | || $member->inGroups($this->ArchiveDownloaderGroups()); |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * Allows only selected {@link Member} objects to delete {@link DNDataArchive} objects from this |
||
483 | * {@link DNEnvironment}. |
||
484 | * |
||
485 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
486 | * @return boolean true if $member can delete archives from this environment, false if they can't. |
||
487 | */ |
||
488 | View Code Duplication | public function canDeleteArchive($member = null) { |
|
489 | if (!$member) { |
||
490 | $member = Member::currentUser(); |
||
491 | } |
||
492 | if (!$member) { |
||
493 | return false; |
||
494 | } |
||
495 | // Must be logged in to check permissions |
||
496 | |||
497 | if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
||
498 | if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) { |
||
499 | return true; |
||
500 | } |
||
501 | } else { |
||
502 | if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) { |
||
503 | return true; |
||
504 | } |
||
505 | } |
||
506 | |||
507 | return $this->ArchiveDeleters()->byID($member->ID) |
||
508 | || $member->inGroups($this->ArchiveDeleterGroups()); |
||
509 | } |
||
510 | |||
511 | /** |
||
512 | * Get a string of groups/people that are allowed to deploy to this environment. |
||
513 | * Used in DNRoot_project.ss to list {@link Member}s who have permission to perform this action. |
||
514 | * |
||
515 | * @return string |
||
516 | */ |
||
517 | public function getDeployersList() { |
||
518 | return implode( |
||
519 | ", ", |
||
520 | array_merge( |
||
521 | $this->DeployerGroups()->column("Title"), |
||
522 | $this->Deployers()->column("FirstName") |
||
523 | ) |
||
524 | ); |
||
525 | } |
||
526 | |||
527 | /** |
||
528 | * Get a string of groups/people that are allowed to restore {@link DNDataArchive} objects into this environment. |
||
529 | * |
||
530 | * @return string |
||
531 | */ |
||
532 | public function getCanRestoreMembersList() { |
||
533 | return implode( |
||
534 | ", ", |
||
535 | array_merge( |
||
536 | $this->CanRestoreGroups()->column("Title"), |
||
537 | $this->CanRestoreMembers()->column("FirstName") |
||
538 | ) |
||
539 | ); |
||
540 | } |
||
541 | |||
542 | /** |
||
543 | * Get a string of groups/people that are allowed to backup {@link DNDataArchive} objects from this environment. |
||
544 | * |
||
545 | * @return string |
||
546 | */ |
||
547 | public function getCanBackupMembersList() { |
||
548 | return implode( |
||
549 | ", ", |
||
550 | array_merge( |
||
551 | $this->CanBackupGroups()->column("Title"), |
||
552 | $this->CanBackupMembers()->column("FirstName") |
||
553 | ) |
||
554 | ); |
||
555 | } |
||
556 | |||
557 | /** |
||
558 | * Get a string of groups/people that are allowed to upload {@link DNDataArchive} |
||
559 | * objects linked to this environment. |
||
560 | * |
||
561 | * @return string |
||
562 | */ |
||
563 | public function getArchiveUploadersList() { |
||
564 | return implode( |
||
565 | ", ", |
||
566 | array_merge( |
||
567 | $this->ArchiveUploaderGroups()->column("Title"), |
||
568 | $this->ArchiveUploaders()->column("FirstName") |
||
569 | ) |
||
570 | ); |
||
571 | } |
||
572 | |||
573 | /** |
||
574 | * Get a string of groups/people that are allowed to download {@link DNDataArchive} objects from this environment. |
||
575 | * |
||
576 | * @return string |
||
577 | */ |
||
578 | public function getArchiveDownloadersList() { |
||
579 | return implode( |
||
580 | ", ", |
||
581 | array_merge( |
||
582 | $this->ArchiveDownloaderGroups()->column("Title"), |
||
583 | $this->ArchiveDownloaders()->column("FirstName") |
||
584 | ) |
||
585 | ); |
||
586 | } |
||
587 | |||
588 | /** |
||
589 | * Get a string of groups/people that are allowed to delete {@link DNDataArchive} objects from this environment. |
||
590 | * |
||
591 | * @return string |
||
592 | */ |
||
593 | public function getArchiveDeletersList() { |
||
594 | return implode( |
||
595 | ", ", |
||
596 | array_merge( |
||
597 | $this->ArchiveDeleterGroups()->column("Title"), |
||
598 | $this->ArchiveDeleters()->column("FirstName") |
||
599 | ) |
||
600 | ); |
||
601 | } |
||
602 | |||
603 | /** |
||
604 | * @return DNData |
||
605 | */ |
||
606 | public function DNData() { |
||
607 | return DNData::inst(); |
||
608 | } |
||
609 | |||
610 | /** |
||
611 | * Get the current deployed build for this environment |
||
612 | * |
||
613 | * Dear people of the future: If you are looking to optimize this, simply create a CurrentBuildSHA(), which can be |
||
614 | * a lot faster. I presume you came here because of the Project display template, which only needs a SHA. |
||
615 | * |
||
616 | * @return false|DNDeployment |
||
617 | */ |
||
618 | public function CurrentBuild() { |
||
619 | // The DeployHistory function is far too slow to use for this |
||
620 | |||
621 | /** @var DNDeployment $deploy */ |
||
622 | $deploy = DNDeployment::get()->filter([ |
||
623 | 'EnvironmentID' => $this->ID, |
||
624 | 'State' => DNDeployment::STATE_COMPLETED |
||
625 | ])->sort('LastEdited DESC')->first(); |
||
626 | |||
627 | if (!$deploy || (!$deploy->SHA)) { |
||
628 | return false; |
||
629 | } |
||
630 | |||
631 | $repo = $this->Project()->getRepository(); |
||
632 | if (!$repo) { |
||
633 | return $deploy; |
||
634 | } |
||
635 | |||
636 | try { |
||
637 | $commit = $this->getCommit($deploy->SHA); |
||
638 | if ($commit) { |
||
639 | $deploy->Message = Convert::raw2xml($this->getCommitMessage($commit)); |
||
640 | $deploy->Committer = Convert::raw2xml($commit->getCommitterName()); |
||
641 | $deploy->CommitDate = $commit->getCommitterDate()->Format('d/m/Y g:ia'); |
||
642 | $deploy->Author = Convert::raw2xml($commit->getAuthorName()); |
||
643 | $deploy->AuthorDate = $commit->getAuthorDate()->Format('d/m/Y g:ia'); |
||
644 | } |
||
645 | // We can't find this SHA, so we ignore adding a commit message to the deployment |
||
646 | } catch (Exception $ex) { |
||
647 | } |
||
648 | |||
649 | return $deploy; |
||
650 | } |
||
651 | |||
652 | /** |
||
653 | * This is a proxy call to gitonmy that caches the information per project and sha |
||
654 | * |
||
655 | * @param string $sha |
||
656 | * @return \Gitonomy\Git\Commit |
||
657 | */ |
||
658 | public function getCommit($sha) { |
||
659 | return $this->Project()->getCommit($sha); |
||
660 | } |
||
661 | |||
662 | public function getCommitMessage(\Gitonomy\Git\Commit $commit) { |
||
663 | return $this->Project()->getCommitMessage($commit); |
||
664 | } |
||
665 | |||
666 | public function getCommitTags(\Gitonomy\Git\Commit $commit) { |
||
667 | return $this->Project()->getCommitTags($commit); |
||
668 | } |
||
669 | |||
670 | /** |
||
671 | * A list of past deployments. |
||
672 | * @param string $orderBy - the name of a DB column to sort in descending order |
||
673 | * @return \ArrayList |
||
674 | */ |
||
675 | public function DeployHistory($orderBy = '') { |
||
676 | $sort = []; |
||
677 | if ($orderBy != '') { |
||
678 | $sort[$orderBy] = 'DESC'; |
||
679 | } |
||
680 | // default / fallback sort order |
||
681 | $sort['LastEdited'] = 'DESC'; |
||
682 | |||
683 | |||
684 | $deployments = $this->Deployments() |
||
685 | ->where('"SHA" IS NOT NULL') |
||
686 | ->sort($sort); |
||
687 | |||
688 | if($this->IsNewDeployEnabled()){ |
||
689 | $deployments->filter('State', [ |
||
690 | DNDeployment::STATE_COMPLETED, |
||
691 | DNDeployment::STATE_FAILED, |
||
692 | DNDeployment::STATE_INVALID |
||
693 | ]); |
||
694 | } |
||
695 | return $deployments; |
||
696 | } |
||
697 | |||
698 | /** |
||
699 | * Check if the new deployment form is enabled by whether the project has it, |
||
700 | * falling back to environment variables on whether it's enabled. |
||
701 | * |
||
702 | * @return bool |
||
703 | */ |
||
704 | public function IsNewDeployEnabled() { |
||
705 | if ($this->Project()->IsNewDeployEnabled) { |
||
706 | return true; |
||
707 | } |
||
708 | // Check for feature flags: |
||
709 | // - FLAG_NEWDEPLOY_ENABLED: set to true to enable globally |
||
710 | // - FLAG_NEWDEPLOY_ENABLED_FOR_MEMBERS: set to semicolon-separated list of email addresses of allowed users. |
||
711 | if (defined('FLAG_NEWDEPLOY_ENABLED') && FLAG_NEWDEPLOY_ENABLED) { |
||
712 | return true; |
||
713 | } |
||
714 | if (defined('FLAG_NEWDEPLOY_ENABLED_FOR_MEMBERS') && FLAG_NEWDEPLOY_ENABLED_FOR_MEMBERS) { |
||
715 | $allowedMembers = explode(';', FLAG_NEWDEPLOY_ENABLED_FOR_MEMBERS); |
||
716 | $member = Member::currentUser(); |
||
717 | if ($allowedMembers && $member && in_array($member->Email, $allowedMembers)) { |
||
718 | return true; |
||
719 | } |
||
720 | } |
||
721 | return false; |
||
722 | } |
||
723 | |||
724 | /** |
||
725 | * This provides the link to the deployments depending on whether |
||
726 | * the feature flag for the new deployment is enabled. |
||
727 | * |
||
728 | * @return string |
||
729 | */ |
||
730 | public function DeploymentsLink() { |
||
731 | if ($this->IsNewDeployEnabled()) { |
||
732 | return $this->Link(\EnvironmentOverview::ACTION_OVERVIEW); |
||
733 | } |
||
734 | return $this->Link(); |
||
735 | } |
||
736 | |||
737 | /** |
||
738 | * @param string $action |
||
739 | * |
||
740 | * @return string |
||
741 | */ |
||
742 | public function Link($action = '') { |
||
743 | return \Controller::join_links($this->Project()->Link(), 'environment', $this->Name, $action); |
||
744 | } |
||
745 | |||
746 | /** |
||
747 | * Is this environment currently at the root level of the controller that handles it? |
||
748 | * @return bool |
||
749 | */ |
||
750 | public function isCurrent() { |
||
751 | return $this->isSection() && Controller::curr()->getAction() == 'environment'; |
||
752 | } |
||
753 | |||
754 | /** |
||
755 | * Is this environment currently in a controller that is handling it or performing a sub-task? |
||
756 | * @return bool |
||
757 | */ |
||
758 | public function isSection() { |
||
759 | $controller = Controller::curr(); |
||
760 | $environment = $controller->getField('CurrentEnvironment'); |
||
761 | return $environment && $environment->ID == $this->ID; |
||
762 | } |
||
763 | |||
764 | /** |
||
765 | * @return FieldList |
||
766 | */ |
||
767 | public function getCMSFields() { |
||
768 | $fields = new FieldList(new TabSet('Root')); |
||
769 | |||
770 | $project = $this->Project(); |
||
771 | if ($project && $project->exists()) { |
||
772 | $viewerGroups = $project->Viewers(); |
||
773 | $groups = $viewerGroups->sort('Title')->map()->toArray(); |
||
774 | $members = []; |
||
775 | foreach ($viewerGroups as $group) { |
||
776 | foreach ($group->Members()->map() as $k => $v) { |
||
777 | $members[$k] = $v; |
||
778 | } |
||
779 | } |
||
780 | asort($members); |
||
781 | } else { |
||
782 | $groups = []; |
||
783 | $members = []; |
||
784 | } |
||
785 | |||
786 | // Main tab |
||
787 | $fields->addFieldsToTab('Root.Main', [ |
||
788 | // The Main.ProjectID |
||
789 | TextField::create('ProjectName', 'Project') |
||
790 | ->setValue(($project = $this->Project()) ? $project->Name : null) |
||
791 | ->performReadonlyTransformation(), |
||
792 | |||
793 | // The Main.Name |
||
794 | TextField::create('Name', 'Environment name') |
||
795 | ->setDescription('A descriptive name for this environment, e.g. staging, uat, production'), |
||
796 | |||
797 | $this->obj('Usage')->scaffoldFormField('Environment usage'), |
||
798 | |||
799 | // The Main.URL field |
||
800 | TextField::create('URL', 'Server URL') |
||
801 | ->setDescription('This url will be used to provide the front-end with a link to this environment'), |
||
802 | |||
803 | // The Main.Filename |
||
804 | TextField::create('Filename') |
||
805 | ->setDescription('The capistrano environment file name') |
||
806 | ->performReadonlyTransformation(), |
||
807 | ]); |
||
808 | |||
809 | // Backend identifier - pick from a named list of configurations specified in YML config |
||
810 | $backends = $this->config()->get('allowed_backends', Config::FIRST_SET); |
||
811 | // If there's only 1 backend, then user selection isn't needed |
||
812 | if (sizeof($backends) > 1) { |
||
813 | $fields->addFieldToTab('Root.Main', DropdownField::create('BackendIdentifier', 'Deployment backend') |
||
814 | ->setSource($backends) |
||
815 | ->setDescription('What kind of deployment system should be used to deploy to this environment')); |
||
816 | } |
||
817 | |||
818 | $fields->addFieldsToTab('Root.UserPermissions', [ |
||
819 | // The viewers of the environment |
||
820 | $this |
||
821 | ->buildPermissionField('ViewerGroups', 'Viewers', $groups, $members) |
||
822 | ->setTitle('Who can view this environment?') |
||
823 | ->setDescription('Groups or Users who can view this environment'), |
||
824 | |||
825 | // The Main.Deployers |
||
826 | $this |
||
827 | ->buildPermissionField('DeployerGroups', 'Deployers', $groups, $members) |
||
828 | ->setTitle('Who can deploy?') |
||
829 | ->setDescription('Groups or Users who can deploy to this environment'), |
||
830 | |||
831 | // A box to select all snapshot options. |
||
832 | $this |
||
833 | ->buildPermissionField('TickAllSnapshotGroups', 'TickAllSnapshot', $groups, $members) |
||
834 | ->setTitle("<em>All snapshot permissions</em>") |
||
835 | ->addExtraClass('tickall') |
||
836 | ->setDescription('UI shortcut to select all snapshot-related options - not written to the database.'), |
||
837 | |||
838 | // The Main.CanRestoreMembers |
||
839 | $this |
||
840 | ->buildPermissionField('CanRestoreGroups', 'CanRestoreMembers', $groups, $members) |
||
841 | ->setTitle('Who can restore?') |
||
842 | ->setDescription('Groups or Users who can restore archives from Deploynaut into this environment'), |
||
843 | |||
844 | // The Main.CanBackupMembers |
||
845 | $this |
||
846 | ->buildPermissionField('CanBackupGroups', 'CanBackupMembers', $groups, $members) |
||
847 | ->setTitle('Who can backup?') |
||
848 | ->setDescription('Groups or Users who can backup archives from this environment into Deploynaut'), |
||
849 | |||
850 | // The Main.ArchiveDeleters |
||
851 | $this |
||
852 | ->buildPermissionField('ArchiveDeleterGroups', 'ArchiveDeleters', $groups, $members) |
||
853 | ->setTitle('Who can delete?') |
||
854 | ->setDescription("Groups or Users who can delete archives from this environment's staging area."), |
||
855 | |||
856 | // The Main.ArchiveUploaders |
||
857 | $this |
||
858 | ->buildPermissionField('ArchiveUploaderGroups', 'ArchiveUploaders', $groups, $members) |
||
859 | ->setTitle('Who can upload?') |
||
860 | ->setDescription( |
||
861 | 'Users who can upload archives linked to this environment into Deploynaut.<br />' . |
||
862 | 'Linking them to an environment allows limiting download permissions (see below).' |
||
863 | ), |
||
864 | |||
865 | // The Main.ArchiveDownloaders |
||
866 | $this |
||
867 | ->buildPermissionField('ArchiveDownloaderGroups', 'ArchiveDownloaders', $groups, $members) |
||
868 | ->setTitle('Who can download?') |
||
869 | ->setDescription(<<<PHP |
||
870 | Users who can download archives from this environment to their computer.<br /> |
||
871 | Since this implies access to the snapshot, it is also a prerequisite for restores |
||
872 | to other environments, alongside the "Who can restore" permission.<br> |
||
873 | Should include all users with upload permissions, otherwise they can't download |
||
874 | their own uploads. |
||
875 | PHP |
||
876 | ) |
||
877 | |||
878 | ]); |
||
879 | |||
880 | // The Main.DeployConfig |
||
881 | if ($this->Project()->exists()) { |
||
882 | $this->setDeployConfigurationFields($fields); |
||
883 | } |
||
884 | |||
885 | // The DataArchives |
||
886 | $dataArchiveConfig = GridFieldConfig_RecordViewer::create(); |
||
887 | $dataArchiveConfig->removeComponentsByType('GridFieldAddNewButton'); |
||
888 | if (class_exists('GridFieldBulkManager')) { |
||
889 | $dataArchiveConfig->addComponent(new GridFieldBulkManager()); |
||
890 | } |
||
891 | $dataArchive = GridField::create('DataArchives', 'Data Archives', $this->DataArchives(), $dataArchiveConfig); |
||
892 | $fields->addFieldToTab('Root.DataArchive', $dataArchive); |
||
893 | |||
894 | // Deployments |
||
895 | $deploymentsConfig = GridFieldConfig_RecordEditor::create(); |
||
896 | $deploymentsConfig->removeComponentsByType('GridFieldAddNewButton'); |
||
897 | if (class_exists('GridFieldBulkManager')) { |
||
898 | $deploymentsConfig->addComponent(new GridFieldBulkManager()); |
||
899 | } |
||
900 | $deployments = GridField::create('Deployments', 'Deployments', $this->Deployments(), $deploymentsConfig); |
||
901 | $fields->addFieldToTab('Root.Deployments', $deployments); |
||
902 | |||
903 | Requirements::javascript('deploynaut/javascript/environment.js'); |
||
904 | |||
905 | // Add actions |
||
906 | $action = new FormAction('check', 'Check Connection'); |
||
907 | $action->setUseButtonTag(true); |
||
908 | $dataURL = Director::absoluteBaseURL() . 'naut/api/' . $this->Project()->Name . '/' . $this->Name . '/ping'; |
||
909 | $action->setAttribute('data-url', $dataURL); |
||
910 | $fields->insertBefore($action, 'Name'); |
||
911 | |||
912 | // Allow extensions |
||
913 | $this->extend('updateCMSFields', $fields); |
||
914 | return $fields; |
||
915 | } |
||
916 | |||
917 | /** |
||
918 | */ |
||
919 | public function onBeforeWrite() { |
||
927 | |||
928 | public function onAfterWrite() { |
||
943 | |||
944 | /** |
||
945 | * Delete any related config files |
||
946 | */ |
||
947 | public function onAfterDelete() { |
||
948 | parent::onAfterDelete(); |
||
949 | |||
950 | // Create a basic new environment config from a template |
||
951 | if ($this->config()->get('allow_web_editing') && $this->envFileExists()) { |
||
952 | unlink($this->getConfigFilename()); |
||
953 | } |
||
954 | |||
955 | $deployments = $this->Deployments(); |
||
956 | if ($deployments && $deployments->exists()) { |
||
957 | foreach ($deployments as $deployment) { |
||
958 | $deployment->delete(); |
||
959 | } |
||
960 | } |
||
961 | |||
962 | $archives = $this->DataArchives(); |
||
963 | if ($archives && $archives->exists()) { |
||
964 | foreach ($archives as $archive) { |
||
965 | $archive->delete(); |
||
966 | } |
||
967 | } |
||
968 | |||
969 | $transfers = $this->DataTransfers(); |
||
970 | if ($transfers && $transfers->exists()) { |
||
971 | foreach ($transfers as $transfer) { |
||
972 | $transfer->delete(); |
||
973 | } |
||
974 | } |
||
975 | |||
976 | $pings = $this->Pings(); |
||
977 | if ($pings && $pings->exists()) { |
||
978 | foreach ($pings as $ping) { |
||
979 | $ping->delete(); |
||
988 | |||
989 | /** |
||
990 | * Returns the path to the ruby config file |
||
991 | * |
||
992 | * @return string |
||
993 | */ |
||
994 | public function getConfigFilename() { |
||
1003 | |||
1004 | /** |
||
1005 | * Helper function to convert a multi-dimensional array (associative or indexed) to an {@link ArrayList} or |
||
1006 | * {@link ArrayData} object structure, so that values can be used in templates. |
||
1007 | * |
||
1008 | * @param array $array The (single- or multi-dimensional) array to convert |
||
1009 | * @return object Either an {@link ArrayList} or {@link ArrayData} object, or the original item ($array) if $array |
||
1010 | * isn't an array. |
||
1011 | */ |
||
1012 | public static function array_to_viewabledata($array) { |
||
1037 | |||
1038 | /** |
||
1039 | * Fetchs all deployments in progress. Limits to 1 hour to prevent deployments |
||
1040 | * if an old deployment is stuck. |
||
1041 | * |
||
1042 | * @return DataList |
||
1043 | */ |
||
1044 | public function runningDeployments() { |
||
1056 | |||
1057 | /** |
||
1058 | * @param string $sha |
||
1059 | * @return array |
||
1060 | */ |
||
1061 | protected function getCommitData($sha) { |
||
1085 | |||
1086 | /** |
||
1087 | * Build a set of multi-select fields for assigning permissions to a pair of group and member many_many relations |
||
1088 | * |
||
1089 | * @param string $groupField Group field name |
||
1090 | * @param string $memberField Member field name |
||
1091 | * @param array $groups List of groups |
||
1092 | * @param array $members List of members |
||
1093 | * @return FieldGroup |
||
1094 | */ |
||
1095 | protected function buildPermissionField($groupField, $memberField, $groups, $members) { |
||
1110 | |||
1111 | /** |
||
1112 | * @param FieldList $fields |
||
1113 | */ |
||
1114 | protected function setDeployConfigurationFields(&$fields) { |
||
1134 | |||
1135 | /** |
||
1136 | * Ensure that environment paths are setup on the local filesystem |
||
1137 | */ |
||
1138 | protected function checkEnvironmentPath() { |
||
1145 | |||
1146 | /** |
||
1147 | * Write the deployment config file to filesystem |
||
1148 | */ |
||
1149 | protected function writeConfigFile() { |
||
1165 | |||
1166 | /** |
||
1167 | * @return string |
||
1168 | */ |
||
1169 | protected function getEnvironmentConfig() { |
||
1175 | |||
1176 | /** |
||
1177 | * @return boolean |
||
1178 | */ |
||
1179 | protected function envFileExists() { |
||
1185 | |||
1186 | protected function validate() { |
||
1196 | |||
1197 | } |
||
1198 |
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
.