|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* DNEnvironment |
|
5
|
|
|
* |
|
6
|
|
|
* This dataobject represents a target environment that source code can be deployed to. |
|
7
|
|
|
* Permissions are controlled by environment, see the various many-many relationships. |
|
8
|
|
|
* |
|
9
|
|
|
* @property string $Filename |
|
10
|
|
|
* @property string $Name |
|
11
|
|
|
* @property string $URL |
|
12
|
|
|
* @property string $BackendIdentifier |
|
13
|
|
|
* @property bool $Usage |
|
14
|
|
|
* |
|
15
|
|
|
* @method DNProject Project() |
|
16
|
|
|
* @property int $ProjectID |
|
17
|
|
|
* |
|
18
|
|
|
* @method HasManyList Deployments() |
|
19
|
|
|
* @method HasManyList DataArchives() |
|
20
|
|
|
* |
|
21
|
|
|
* @method ManyManyList Viewers() |
|
22
|
|
|
* @method ManyManyList ViewerGroups() |
|
23
|
|
|
* @method ManyManyList Deployers() |
|
24
|
|
|
* @method ManyManyList DeployerGroups() |
|
25
|
|
|
* @method ManyManyList CanRestoreMembers() |
|
26
|
|
|
* @method ManyManyList CanRestoreGroups() |
|
27
|
|
|
* @method ManyManyList CanBackupMembers() |
|
28
|
|
|
* @method ManyManyList CanBackupGroups() |
|
29
|
|
|
* @method ManyManyList ArchiveUploaders() |
|
30
|
|
|
* @method ManyManyList ArchiveUploaderGroups() |
|
31
|
|
|
* @method ManyManyList ArchiveDownloaders() |
|
32
|
|
|
* @method ManyManyList ArchiveDownloaderGroups() |
|
33
|
|
|
* @method ManyManyList ArchiveDeleters() |
|
34
|
|
|
* @method ManyManyList ArchiveDeleterGroups() |
|
35
|
|
|
*/ |
|
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
|
|
|
]; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var array |
|
65
|
|
|
*/ |
|
66
|
|
|
public static $many_many = [ |
|
67
|
|
|
"Viewers" => "Member", // Who can view this environment |
|
68
|
|
|
"ViewerGroups" => "Group", |
|
69
|
|
|
"Deployers" => "Member", // Who can deploy to this environment |
|
70
|
|
|
"DeployerGroups" => "Group", |
|
71
|
|
|
"CanRestoreMembers" => "Member", // Who can restore archive files to this environment |
|
72
|
|
|
"CanRestoreGroups" => "Group", |
|
73
|
|
|
"CanBackupMembers" => "Member", // Who can backup archive files from this environment |
|
74
|
|
|
"CanBackupGroups" => "Group", |
|
75
|
|
|
"ArchiveUploaders" => "Member", // Who can upload archive files linked to this environment |
|
76
|
|
|
"ArchiveUploaderGroups" => "Group", |
|
77
|
|
|
"ArchiveDownloaders" => "Member", // Who can download archive files from this environment |
|
78
|
|
|
"ArchiveDownloaderGroups" => "Group", |
|
79
|
|
|
"ArchiveDeleters" => "Member", // Who can delete archive files from this environment, |
|
80
|
|
|
"ArchiveDeleterGroups" => "Group", |
|
81
|
|
|
]; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @var array |
|
85
|
|
|
*/ |
|
86
|
|
|
public static $summary_fields = [ |
|
87
|
|
|
"Name" => "Environment Name", |
|
88
|
|
|
"Usage" => "Usage", |
|
89
|
|
|
"URL" => "URL", |
|
90
|
|
|
"DeployersList" => "Can Deploy List", |
|
91
|
|
|
"CanRestoreMembersList" => "Can Restore List", |
|
92
|
|
|
"CanBackupMembersList" => "Can Backup List", |
|
93
|
|
|
"ArchiveUploadersList" => "Can Upload List", |
|
94
|
|
|
"ArchiveDownloadersList" => "Can Download List", |
|
95
|
|
|
"ArchiveDeletersList" => "Can Delete List", |
|
96
|
|
|
]; |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @var array |
|
100
|
|
|
*/ |
|
101
|
|
|
public static $searchable_fields = [ |
|
102
|
|
|
"Name", |
|
103
|
|
|
]; |
|
104
|
|
|
|
|
105
|
|
|
private static $singular_name = 'Capistrano Environment'; |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
private static $plural_name = 'Capistrano Environments'; |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @var string |
|
111
|
|
|
*/ |
|
112
|
|
|
private static $default_sort = 'Name'; |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @var array |
|
116
|
|
|
*/ |
|
117
|
|
|
public static $has_one = [ |
|
118
|
|
|
"Project" => "DNProject", |
|
119
|
|
|
"CreateEnvironment" => "DNCreateEnvironment" |
|
120
|
|
|
]; |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* If this is set to a full pathfile, it will be used as template |
|
124
|
|
|
* file when creating a new capistrano environment config file. |
|
125
|
|
|
* |
|
126
|
|
|
* If not set, the default 'environment.template' from the module |
|
127
|
|
|
* root is used |
|
128
|
|
|
* |
|
129
|
|
|
* @config |
|
130
|
|
|
* @var string |
|
131
|
|
|
*/ |
|
132
|
|
|
private static $template_file = ''; |
|
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Set this to true to allow editing of the environment files via the web admin |
|
136
|
|
|
* |
|
137
|
|
|
* @var bool |
|
138
|
|
|
*/ |
|
139
|
|
|
private static $allow_web_editing = false; |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @var array |
|
143
|
|
|
*/ |
|
144
|
|
|
private static $casting = [ |
|
|
|
|
|
|
145
|
|
|
'DeployHistory' => 'Text' |
|
146
|
|
|
]; |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Allowed backends. A map of Injector identifier to human-readable label. |
|
150
|
|
|
* |
|
151
|
|
|
* @config |
|
152
|
|
|
* @var array |
|
153
|
|
|
*/ |
|
154
|
|
|
private static $allowed_backends = []; |
|
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Used by the sync task |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $path |
|
160
|
|
|
* @return \DNEnvironment |
|
161
|
|
|
*/ |
|
162
|
|
|
public static function create_from_path($path) { |
|
163
|
|
|
$e = DNEnvironment::create(); |
|
|
|
|
|
|
164
|
|
|
$e->Filename = $path; |
|
165
|
|
|
$e->Name = basename($e->Filename, '.rb'); |
|
166
|
|
|
|
|
167
|
|
|
// add each administrator member as a deployer of the new environment |
|
168
|
|
|
$adminGroup = Group::get()->filter('Code', 'administrators')->first(); |
|
169
|
|
|
$e->DeployerGroups()->add($adminGroup); |
|
170
|
|
|
return $e; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Get the deployment backend used for this environment. |
|
175
|
|
|
* |
|
176
|
|
|
* Enforces compliance with the allowed_backends setting; if the DNEnvironment.BackendIdentifier value is |
|
177
|
|
|
* illegal then that value is ignored. |
|
178
|
|
|
* |
|
179
|
|
|
* @return DeploymentBackend |
|
180
|
|
|
*/ |
|
181
|
|
|
public function Backend() { |
|
182
|
|
|
$backends = array_keys($this->config()->get('allowed_backends', Config::FIRST_SET)); |
|
183
|
|
|
switch (sizeof($backends)) { |
|
184
|
|
|
// Nothing allowed, use the default value "DeploymentBackend" |
|
185
|
|
|
case 0: |
|
186
|
|
|
$backend = "DeploymentBackend"; |
|
187
|
|
|
break; |
|
188
|
|
|
|
|
189
|
|
|
// Only 1 thing allowed, use that |
|
190
|
|
|
case 1: |
|
191
|
|
|
$backend = $backends[0]; |
|
192
|
|
|
break; |
|
193
|
|
|
|
|
194
|
|
|
// Multiple choices, use our choice if it's legal, otherwise default to the first item on the list |
|
195
|
|
|
default: |
|
196
|
|
|
$backend = $this->BackendIdentifier; |
|
197
|
|
|
if (!in_array($backend, $backends)) { |
|
198
|
|
|
$backend = $backends[0]; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return Injector::inst()->get($backend); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @param SS_HTTPRequest $request |
|
207
|
|
|
* |
|
208
|
|
|
* @return DeploymentStrategy |
|
209
|
|
|
*/ |
|
210
|
|
|
public function getDeployStrategy(\SS_HTTPRequest $request) { |
|
211
|
|
|
return $this->Backend()->planDeploy($this, $request->requestVars()); |
|
|
|
|
|
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
public function Menu() { |
|
215
|
|
|
$list = new ArrayList(); |
|
216
|
|
|
|
|
217
|
|
|
$controller = Controller::curr(); |
|
218
|
|
|
$actionType = $controller->getField('CurrentActionType'); |
|
219
|
|
|
|
|
220
|
|
|
$list->push(new ArrayData([ |
|
221
|
|
|
'Link' => sprintf('naut/project/%s/environment/%s', $this->Project()->Name, $this->Name), |
|
222
|
|
|
'Title' => 'Deployments', |
|
223
|
|
|
'IsCurrent' => $this->isCurrent(), |
|
224
|
|
|
'IsSection' => $this->isSection() && $actionType == DNRoot::ACTION_DEPLOY |
|
225
|
|
|
])); |
|
226
|
|
|
|
|
227
|
|
|
$this->extend('updateMenu', $list); |
|
228
|
|
|
|
|
229
|
|
|
return $list; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Return the current object from $this->Menu() |
|
234
|
|
|
* Good for making titles and things |
|
235
|
|
|
*/ |
|
236
|
|
|
public function CurrentMenu() { |
|
237
|
|
|
return $this->Menu()->filter('IsSection', true)->First(); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Return a name for this environment. |
|
242
|
|
|
* |
|
243
|
|
|
* @param string $separator The string used when concatenating project with env name |
|
244
|
|
|
* @return string |
|
245
|
|
|
*/ |
|
246
|
|
|
public function getFullName($separator = ':') { |
|
247
|
|
|
return sprintf('%s%s%s', $this->Project()->Name, $separator, $this->Name); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* URL for the environment that can be used if no explicit URL is set. |
|
252
|
|
|
*/ |
|
253
|
|
|
public function getDefaultURL() { |
|
254
|
|
|
return null; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
public function getBareURL() { |
|
258
|
|
|
$url = parse_url($this->URL); |
|
259
|
|
|
if (isset($url['host'])) { |
|
260
|
|
|
return strtolower($url['host']); |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
public function getBareDefaultURL() { |
|
265
|
|
|
$url = parse_url($this->getDefaultURL()); |
|
266
|
|
|
if (isset($url['host'])) { |
|
267
|
|
|
return strtolower($url['host']); |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Environments are only viewable by people that can view the environment. |
|
273
|
|
|
* |
|
274
|
|
|
* @param Member|null $member |
|
275
|
|
|
* @return boolean |
|
276
|
|
|
*/ |
|
277
|
|
|
public function canView($member = null) { |
|
278
|
|
|
if (!$member) { |
|
279
|
|
|
$member = Member::currentUser(); |
|
280
|
|
|
} |
|
281
|
|
|
if (!$member) { |
|
282
|
|
|
return false; |
|
283
|
|
|
} |
|
284
|
|
|
// Must be logged in to check permissions |
|
285
|
|
|
|
|
286
|
|
|
if (Permission::checkMember($member, 'ADMIN')) { |
|
287
|
|
|
return true; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
// if no Viewers or ViewerGroups defined, fallback to DNProject::canView permissions |
|
291
|
|
|
if ($this->Viewers()->exists() || $this->ViewerGroups()->exists()) { |
|
292
|
|
|
return $this->Viewers()->byID($member->ID) |
|
293
|
|
|
|| $member->inGroups($this->ViewerGroups()); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
return $this->Project()->canView($member); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* Allow deploy only to some people. |
|
301
|
|
|
* |
|
302
|
|
|
* @param Member|null $member |
|
303
|
|
|
* @return boolean |
|
304
|
|
|
*/ |
|
305
|
|
View Code Duplication |
public function canDeploy($member = null) { |
|
|
|
|
|
|
306
|
|
|
if (!$member) { |
|
307
|
|
|
$member = Member::currentUser(); |
|
308
|
|
|
} |
|
309
|
|
|
if (!$member) { |
|
310
|
|
|
return false; |
|
311
|
|
|
} |
|
312
|
|
|
// Must be logged in to check permissions |
|
313
|
|
|
|
|
314
|
|
|
if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
|
315
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_PROD_DEPLOYMENT, $member)) { |
|
|
|
|
|
|
316
|
|
|
return true; |
|
317
|
|
|
} |
|
318
|
|
|
} else { |
|
319
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_DEPLOYMENT, $member)) { |
|
320
|
|
|
return true; |
|
321
|
|
|
} |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
return $this->Deployers()->byID($member->ID) |
|
325
|
|
|
|| $member->inGroups($this->DeployerGroups()); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* Provide reason why the user cannot deploy. |
|
330
|
|
|
* |
|
331
|
|
|
* @return string |
|
332
|
|
|
*/ |
|
333
|
|
|
public function getCannotDeployMessage() { |
|
334
|
|
|
return 'You cannot deploy to this environment.'; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* Allows only selected {@link Member} objects to restore {@link DNDataArchive} objects into this |
|
339
|
|
|
* {@link DNEnvironment}. |
|
340
|
|
|
* |
|
341
|
|
|
* @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
|
342
|
|
|
* @return boolean true if $member can restore, and false if they can't. |
|
343
|
|
|
*/ |
|
344
|
|
View Code Duplication |
public function canRestore($member = null) { |
|
|
|
|
|
|
345
|
|
|
if (!$member) { |
|
346
|
|
|
$member = Member::currentUser(); |
|
347
|
|
|
} |
|
348
|
|
|
if (!$member) { |
|
349
|
|
|
return false; |
|
350
|
|
|
} |
|
351
|
|
|
// Must be logged in to check permissions |
|
352
|
|
|
|
|
353
|
|
|
if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
|
354
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) { |
|
|
|
|
|
|
355
|
|
|
return true; |
|
356
|
|
|
} |
|
357
|
|
|
} else { |
|
358
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) { |
|
|
|
|
|
|
359
|
|
|
return true; |
|
360
|
|
|
} |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
return $this->CanRestoreMembers()->byID($member->ID) |
|
364
|
|
|
|| $member->inGroups($this->CanRestoreGroups()); |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
/** |
|
368
|
|
|
* Allows only selected {@link Member} objects to backup this {@link DNEnvironment} to a {@link DNDataArchive} |
|
369
|
|
|
* file. |
|
370
|
|
|
* |
|
371
|
|
|
* @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
|
372
|
|
|
* @return boolean true if $member can backup, and false if they can't. |
|
373
|
|
|
*/ |
|
374
|
|
View Code Duplication |
public function canBackup($member = null) { |
|
|
|
|
|
|
375
|
|
|
$project = $this->Project(); |
|
376
|
|
|
if ($project->HasDiskQuota() && $project->HasExceededDiskQuota()) { |
|
377
|
|
|
return false; |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
if (!$member) { |
|
381
|
|
|
$member = Member::currentUser(); |
|
382
|
|
|
} |
|
383
|
|
|
// Must be logged in to check permissions |
|
384
|
|
|
if (!$member) { |
|
385
|
|
|
return false; |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
|
389
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) { |
|
390
|
|
|
return true; |
|
391
|
|
|
} |
|
392
|
|
|
} else { |
|
393
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) { |
|
|
|
|
|
|
394
|
|
|
return true; |
|
395
|
|
|
} |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
return $this->CanBackupMembers()->byID($member->ID) |
|
399
|
|
|
|| $member->inGroups($this->CanBackupGroups()); |
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
|
|
/** |
|
403
|
|
|
* Allows only selected {@link Member} objects to upload {@link DNDataArchive} objects linked to this |
|
404
|
|
|
* {@link DNEnvironment}. |
|
405
|
|
|
* |
|
406
|
|
|
* Note: This is not uploading them to the actual environment itself (e.g. uploading to the live site) - it is the |
|
407
|
|
|
* process of uploading a *.sspak file into Deploynaut for later 'restoring' to an environment. See |
|
408
|
|
|
* {@link self::canRestore()}. |
|
409
|
|
|
* |
|
410
|
|
|
* @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
|
411
|
|
|
* @return boolean true if $member can upload archives linked to this environment, false if they can't. |
|
412
|
|
|
*/ |
|
413
|
|
View Code Duplication |
public function canUploadArchive($member = null) { |
|
|
|
|
|
|
414
|
|
|
$project = $this->Project(); |
|
415
|
|
|
if ($project->HasDiskQuota() && $project->HasExceededDiskQuota()) { |
|
416
|
|
|
return false; |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
if (!$member) { |
|
420
|
|
|
$member = Member::currentUser(); |
|
421
|
|
|
} |
|
422
|
|
|
if (!$member) { |
|
423
|
|
|
return false; |
|
424
|
|
|
} |
|
425
|
|
|
// Must be logged in to check permissions |
|
426
|
|
|
|
|
427
|
|
|
if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
|
428
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) { |
|
|
|
|
|
|
429
|
|
|
return true; |
|
430
|
|
|
} |
|
431
|
|
|
} else { |
|
432
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) { |
|
|
|
|
|
|
433
|
|
|
return true; |
|
434
|
|
|
} |
|
435
|
|
|
} |
|
436
|
|
|
|
|
437
|
|
|
return $this->ArchiveUploaders()->byID($member->ID) |
|
438
|
|
|
|| $member->inGroups($this->ArchiveUploaderGroups()); |
|
439
|
|
|
} |
|
440
|
|
|
|
|
441
|
|
|
/** |
|
442
|
|
|
* Allows only selected {@link Member} objects to download {@link DNDataArchive} objects from this |
|
443
|
|
|
* {@link DNEnvironment}. |
|
444
|
|
|
* |
|
445
|
|
|
* @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
|
446
|
|
|
* @return boolean true if $member can download archives from this environment, false if they can't. |
|
447
|
|
|
*/ |
|
448
|
|
View Code Duplication |
public function canDownloadArchive($member = null) { |
|
|
|
|
|
|
449
|
|
|
if (!$member) { |
|
450
|
|
|
$member = Member::currentUser(); |
|
451
|
|
|
} |
|
452
|
|
|
if (!$member) { |
|
453
|
|
|
return false; |
|
454
|
|
|
} |
|
455
|
|
|
// Must be logged in to check permissions |
|
456
|
|
|
|
|
457
|
|
|
if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
|
458
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) { |
|
|
|
|
|
|
459
|
|
|
return true; |
|
460
|
|
|
} |
|
461
|
|
|
} else { |
|
462
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) { |
|
|
|
|
|
|
463
|
|
|
return true; |
|
464
|
|
|
} |
|
465
|
|
|
} |
|
466
|
|
|
|
|
467
|
|
|
return $this->ArchiveDownloaders()->byID($member->ID) |
|
468
|
|
|
|| $member->inGroups($this->ArchiveDownloaderGroups()); |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
/** |
|
472
|
|
|
* Allows only selected {@link Member} objects to delete {@link DNDataArchive} objects from this |
|
473
|
|
|
* {@link DNEnvironment}. |
|
474
|
|
|
* |
|
475
|
|
|
* @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
|
476
|
|
|
* @return boolean true if $member can delete archives from this environment, false if they can't. |
|
477
|
|
|
*/ |
|
478
|
|
View Code Duplication |
public function canDeleteArchive($member = null) { |
|
|
|
|
|
|
479
|
|
|
if (!$member) { |
|
480
|
|
|
$member = Member::currentUser(); |
|
481
|
|
|
} |
|
482
|
|
|
if (!$member) { |
|
483
|
|
|
return false; |
|
484
|
|
|
} |
|
485
|
|
|
// Must be logged in to check permissions |
|
486
|
|
|
|
|
487
|
|
|
if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
|
488
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) { |
|
|
|
|
|
|
489
|
|
|
return true; |
|
490
|
|
|
} |
|
491
|
|
|
} else { |
|
492
|
|
|
if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) { |
|
493
|
|
|
return true; |
|
494
|
|
|
} |
|
495
|
|
|
} |
|
496
|
|
|
|
|
497
|
|
|
return $this->ArchiveDeleters()->byID($member->ID) |
|
498
|
|
|
|| $member->inGroups($this->ArchiveDeleterGroups()); |
|
499
|
|
|
} |
|
500
|
|
|
|
|
501
|
|
|
/** |
|
502
|
|
|
* Get a string of groups/people that are allowed to deploy to this environment. |
|
503
|
|
|
* Used in DNRoot_project.ss to list {@link Member}s who have permission to perform this action. |
|
504
|
|
|
* |
|
505
|
|
|
* @return string |
|
506
|
|
|
*/ |
|
507
|
|
|
public function getDeployersList() { |
|
508
|
|
|
return implode( |
|
509
|
|
|
", ", |
|
510
|
|
|
array_merge( |
|
511
|
|
|
$this->DeployerGroups()->column("Title"), |
|
512
|
|
|
$this->Deployers()->column("FirstName") |
|
513
|
|
|
) |
|
514
|
|
|
); |
|
515
|
|
|
} |
|
516
|
|
|
|
|
517
|
|
|
/** |
|
518
|
|
|
* Get a string of groups/people that are allowed to restore {@link DNDataArchive} objects into this environment. |
|
519
|
|
|
* |
|
520
|
|
|
* @return string |
|
521
|
|
|
*/ |
|
522
|
|
|
public function getCanRestoreMembersList() { |
|
523
|
|
|
return implode( |
|
524
|
|
|
", ", |
|
525
|
|
|
array_merge( |
|
526
|
|
|
$this->CanRestoreGroups()->column("Title"), |
|
527
|
|
|
$this->CanRestoreMembers()->column("FirstName") |
|
528
|
|
|
) |
|
529
|
|
|
); |
|
530
|
|
|
} |
|
531
|
|
|
|
|
532
|
|
|
/** |
|
533
|
|
|
* Get a string of groups/people that are allowed to backup {@link DNDataArchive} objects from this environment. |
|
534
|
|
|
* |
|
535
|
|
|
* @return string |
|
536
|
|
|
*/ |
|
537
|
|
|
public function getCanBackupMembersList() { |
|
538
|
|
|
return implode( |
|
539
|
|
|
", ", |
|
540
|
|
|
array_merge( |
|
541
|
|
|
$this->CanBackupGroups()->column("Title"), |
|
542
|
|
|
$this->CanBackupMembers()->column("FirstName") |
|
543
|
|
|
) |
|
544
|
|
|
); |
|
545
|
|
|
} |
|
546
|
|
|
|
|
547
|
|
|
/** |
|
548
|
|
|
* Get a string of groups/people that are allowed to upload {@link DNDataArchive} |
|
549
|
|
|
* objects linked to this environment. |
|
550
|
|
|
* |
|
551
|
|
|
* @return string |
|
552
|
|
|
*/ |
|
553
|
|
|
public function getArchiveUploadersList() { |
|
554
|
|
|
return implode( |
|
555
|
|
|
", ", |
|
556
|
|
|
array_merge( |
|
557
|
|
|
$this->ArchiveUploaderGroups()->column("Title"), |
|
558
|
|
|
$this->ArchiveUploaders()->column("FirstName") |
|
559
|
|
|
) |
|
560
|
|
|
); |
|
561
|
|
|
} |
|
562
|
|
|
|
|
563
|
|
|
/** |
|
564
|
|
|
* Get a string of groups/people that are allowed to download {@link DNDataArchive} objects from this environment. |
|
565
|
|
|
* |
|
566
|
|
|
* @return string |
|
567
|
|
|
*/ |
|
568
|
|
|
public function getArchiveDownloadersList() { |
|
569
|
|
|
return implode( |
|
570
|
|
|
", ", |
|
571
|
|
|
array_merge( |
|
572
|
|
|
$this->ArchiveDownloaderGroups()->column("Title"), |
|
573
|
|
|
$this->ArchiveDownloaders()->column("FirstName") |
|
574
|
|
|
) |
|
575
|
|
|
); |
|
576
|
|
|
} |
|
577
|
|
|
|
|
578
|
|
|
/** |
|
579
|
|
|
* Get a string of groups/people that are allowed to delete {@link DNDataArchive} objects from this environment. |
|
580
|
|
|
* |
|
581
|
|
|
* @return string |
|
582
|
|
|
*/ |
|
583
|
|
|
public function getArchiveDeletersList() { |
|
584
|
|
|
return implode( |
|
585
|
|
|
", ", |
|
586
|
|
|
array_merge( |
|
587
|
|
|
$this->ArchiveDeleterGroups()->column("Title"), |
|
588
|
|
|
$this->ArchiveDeleters()->column("FirstName") |
|
589
|
|
|
) |
|
590
|
|
|
); |
|
591
|
|
|
} |
|
592
|
|
|
|
|
593
|
|
|
/** |
|
594
|
|
|
* @return DNData |
|
595
|
|
|
*/ |
|
596
|
|
|
public function DNData() { |
|
597
|
|
|
return DNData::inst(); |
|
598
|
|
|
} |
|
599
|
|
|
|
|
600
|
|
|
/** |
|
601
|
|
|
* Get the current deployed build for this environment |
|
602
|
|
|
* |
|
603
|
|
|
* Dear people of the future: If you are looking to optimize this, simply create a CurrentBuildSHA(), which can be |
|
604
|
|
|
* a lot faster. I presume you came here because of the Project display template, which only needs a SHA. |
|
605
|
|
|
* |
|
606
|
|
|
* @return false|DNDeployment |
|
607
|
|
|
*/ |
|
608
|
|
|
public function CurrentBuild() { |
|
609
|
|
|
// The DeployHistory function is far too slow to use for this |
|
610
|
|
|
|
|
611
|
|
|
/** @var DNDeployment $deploy */ |
|
612
|
|
|
$deploy = DNDeployment::get()->filter([ |
|
613
|
|
|
'EnvironmentID' => $this->ID, |
|
614
|
|
|
'State' => DNDeployment::STATE_COMPLETED |
|
615
|
|
|
])->sort('LastEdited DESC')->first(); |
|
616
|
|
|
|
|
617
|
|
|
if (!$deploy || (!$deploy->SHA)) { |
|
|
|
|
|
|
618
|
|
|
return false; |
|
619
|
|
|
} |
|
620
|
|
|
|
|
621
|
|
|
$repo = $this->Project()->getRepository(); |
|
622
|
|
|
if (!$repo) { |
|
623
|
|
|
return $deploy; |
|
624
|
|
|
} |
|
625
|
|
|
|
|
626
|
|
|
try { |
|
627
|
|
|
$commit = $this->getCommit($deploy->SHA); |
|
628
|
|
|
if ($commit) { |
|
629
|
|
|
$deploy->Message = Convert::raw2xml($this->getCommitMessage($commit)); |
|
|
|
|
|
|
630
|
|
|
$deploy->Committer = Convert::raw2xml($commit->getCommitterName()); |
|
|
|
|
|
|
631
|
|
|
$deploy->CommitDate = $commit->getCommitterDate()->Format('d/m/Y g:ia'); |
|
|
|
|
|
|
632
|
|
|
$deploy->Author = Convert::raw2xml($commit->getAuthorName()); |
|
|
|
|
|
|
633
|
|
|
$deploy->AuthorDate = $commit->getAuthorDate()->Format('d/m/Y g:ia'); |
|
|
|
|
|
|
634
|
|
|
} |
|
635
|
|
|
// We can't find this SHA, so we ignore adding a commit message to the deployment |
|
636
|
|
|
} catch (Exception $ex) { |
|
637
|
|
|
} |
|
638
|
|
|
|
|
639
|
|
|
return $deploy; |
|
640
|
|
|
} |
|
641
|
|
|
|
|
642
|
|
|
/** |
|
643
|
|
|
* This is a proxy call to gitonmy that caches the information per project and sha |
|
644
|
|
|
* |
|
645
|
|
|
* @param string $sha |
|
646
|
|
|
* @return \Gitonomy\Git\Commit |
|
647
|
|
|
*/ |
|
648
|
|
|
public function getCommit($sha) { |
|
649
|
|
|
return $this->Project()->getCommit($sha); |
|
650
|
|
|
} |
|
651
|
|
|
|
|
652
|
|
|
public function getCommitMessage(\Gitonomy\Git\Commit $commit) { |
|
653
|
|
|
return $this->Project()->getCommitMessage($commit); |
|
654
|
|
|
} |
|
655
|
|
|
|
|
656
|
|
|
public function getCommitTags(\Gitonomy\Git\Commit $commit) { |
|
657
|
|
|
return $this->Project()->getCommitTags($commit); |
|
658
|
|
|
} |
|
659
|
|
|
|
|
660
|
|
|
/** |
|
661
|
|
|
* A list of past deployments. |
|
662
|
|
|
* @param string $orderBy - the name of a DB column to sort in descending order |
|
663
|
|
|
* @return \ArrayList |
|
664
|
|
|
*/ |
|
665
|
|
|
public function DeployHistory($orderBy = '') { |
|
666
|
|
|
$sort = []; |
|
667
|
|
|
if ($orderBy != '') { |
|
668
|
|
|
$sort[$orderBy] = 'DESC'; |
|
669
|
|
|
} |
|
670
|
|
|
// default / fallback sort order |
|
671
|
|
|
$sort['LastEdited'] = 'DESC'; |
|
672
|
|
|
|
|
673
|
|
|
return $this->Deployments() |
|
674
|
|
|
->where('"SHA" IS NOT NULL') |
|
675
|
|
|
->filter('State', [ |
|
676
|
|
|
DNDeployment::STATE_COMPLETED, |
|
677
|
|
|
DNDeployment::STATE_FAILED, |
|
678
|
|
|
DNDeployment::STATE_INVALID |
|
679
|
|
|
]) |
|
680
|
|
|
->sort($sort); |
|
681
|
|
|
} |
|
682
|
|
|
|
|
683
|
|
|
/** |
|
684
|
|
|
* A list of upcoming or current deployments. |
|
685
|
|
|
* @return ArrayList |
|
686
|
|
|
*/ |
|
687
|
|
|
public function UpcomingDeployments() { |
|
688
|
|
|
return $this->Deployments() |
|
689
|
|
|
->where('"SHA" IS NOT NULL') |
|
690
|
|
|
->filter('State', [ |
|
691
|
|
|
DNDeployment::STATE_NEW, |
|
692
|
|
|
DNDeployment::STATE_SUBMITTED, |
|
693
|
|
|
DNDeployment::STATE_ABORTING, |
|
694
|
|
|
DNDeployment::STATE_QUEUED, |
|
695
|
|
|
DNDeployment::STATE_DEPLOYING, |
|
696
|
|
|
]) |
|
697
|
|
|
->sort('LastEdited DESC'); |
|
698
|
|
|
} |
|
699
|
|
|
|
|
700
|
|
|
/** |
|
701
|
|
|
* @param string $action |
|
702
|
|
|
* |
|
703
|
|
|
* @return string |
|
704
|
|
|
*/ |
|
705
|
|
|
public function Link($action = '') { |
|
706
|
|
|
return \Controller::join_links($this->Project()->Link(), "environment", $this->Name, $action); |
|
707
|
|
|
} |
|
708
|
|
|
|
|
709
|
|
|
/** |
|
710
|
|
|
* Is this environment currently at the root level of the controller that handles it? |
|
711
|
|
|
* @return bool |
|
712
|
|
|
*/ |
|
713
|
|
|
public function isCurrent() { |
|
714
|
|
|
return $this->isSection() && Controller::curr()->getAction() == 'environment'; |
|
715
|
|
|
} |
|
716
|
|
|
|
|
717
|
|
|
/** |
|
718
|
|
|
* Is this environment currently in a controller that is handling it or performing a sub-task? |
|
719
|
|
|
* @return bool |
|
720
|
|
|
*/ |
|
721
|
|
|
public function isSection() { |
|
722
|
|
|
$controller = Controller::curr(); |
|
723
|
|
|
$environment = $controller->getField('CurrentEnvironment'); |
|
724
|
|
|
return $environment && $environment->ID == $this->ID; |
|
725
|
|
|
} |
|
726
|
|
|
|
|
727
|
|
|
/** |
|
728
|
|
|
* @return FieldList |
|
729
|
|
|
*/ |
|
730
|
|
|
public function getCMSFields() { |
|
731
|
|
|
$fields = new FieldList(new TabSet('Root')); |
|
732
|
|
|
|
|
733
|
|
|
$project = $this->Project(); |
|
734
|
|
|
if ($project && $project->exists()) { |
|
735
|
|
|
$viewerGroups = $project->Viewers(); |
|
736
|
|
|
$groups = $viewerGroups->sort('Title')->map()->toArray(); |
|
737
|
|
|
$members = []; |
|
738
|
|
|
foreach ($viewerGroups as $group) { |
|
739
|
|
|
foreach ($group->Members()->map() as $k => $v) { |
|
740
|
|
|
$members[$k] = $v; |
|
741
|
|
|
} |
|
742
|
|
|
} |
|
743
|
|
|
asort($members); |
|
744
|
|
|
} else { |
|
745
|
|
|
$groups = []; |
|
746
|
|
|
$members = []; |
|
747
|
|
|
} |
|
748
|
|
|
|
|
749
|
|
|
// Main tab |
|
750
|
|
|
$fields->addFieldsToTab('Root.Main', [ |
|
751
|
|
|
// The Main.ProjectID |
|
752
|
|
|
TextField::create('ProjectName', 'Project') |
|
753
|
|
|
->setValue(($project = $this->Project()) ? $project->Name : null) |
|
754
|
|
|
->performReadonlyTransformation(), |
|
755
|
|
|
|
|
756
|
|
|
// The Main.Name |
|
757
|
|
|
TextField::create('Name', 'Environment name') |
|
758
|
|
|
->setDescription('A descriptive name for this environment, e.g. staging, uat, production'), |
|
759
|
|
|
|
|
760
|
|
|
$this->obj('Usage')->scaffoldFormField('Environment usage'), |
|
761
|
|
|
|
|
762
|
|
|
// The Main.URL field |
|
763
|
|
|
TextField::create('URL', 'Server URL') |
|
764
|
|
|
->setDescription('This url will be used to provide the front-end with a link to this environment'), |
|
765
|
|
|
|
|
766
|
|
|
// The Main.Filename |
|
767
|
|
|
TextField::create('Filename') |
|
768
|
|
|
->setDescription('The capistrano environment file name') |
|
769
|
|
|
->performReadonlyTransformation(), |
|
770
|
|
|
]); |
|
771
|
|
|
|
|
772
|
|
|
// Backend identifier - pick from a named list of configurations specified in YML config |
|
773
|
|
|
$backends = $this->config()->get('allowed_backends', Config::FIRST_SET); |
|
774
|
|
|
// If there's only 1 backend, then user selection isn't needed |
|
775
|
|
|
if (sizeof($backends) > 1) { |
|
776
|
|
|
$fields->addFieldToTab('Root.Main', DropdownField::create('BackendIdentifier', 'Deployment backend') |
|
777
|
|
|
->setSource($backends) |
|
778
|
|
|
->setDescription('What kind of deployment system should be used to deploy to this environment')); |
|
779
|
|
|
} |
|
780
|
|
|
|
|
781
|
|
|
$fields->addFieldsToTab('Root.UserPermissions', [ |
|
782
|
|
|
// The viewers of the environment |
|
783
|
|
|
$this |
|
784
|
|
|
->buildPermissionField('ViewerGroups', 'Viewers', $groups, $members) |
|
785
|
|
|
->setTitle('Who can view this environment?') |
|
786
|
|
|
->setDescription('Groups or Users who can view this environment'), |
|
787
|
|
|
|
|
788
|
|
|
// The Main.Deployers |
|
789
|
|
|
$this |
|
790
|
|
|
->buildPermissionField('DeployerGroups', 'Deployers', $groups, $members) |
|
791
|
|
|
->setTitle('Who can deploy?') |
|
792
|
|
|
->setDescription('Groups or Users who can deploy to this environment'), |
|
793
|
|
|
|
|
794
|
|
|
// A box to select all snapshot options. |
|
795
|
|
|
$this |
|
796
|
|
|
->buildPermissionField('TickAllSnapshotGroups', 'TickAllSnapshot', $groups, $members) |
|
797
|
|
|
->setTitle("<em>All snapshot permissions</em>") |
|
798
|
|
|
->addExtraClass('tickall') |
|
799
|
|
|
->setDescription('UI shortcut to select all snapshot-related options - not written to the database.'), |
|
800
|
|
|
|
|
801
|
|
|
// The Main.CanRestoreMembers |
|
802
|
|
|
$this |
|
803
|
|
|
->buildPermissionField('CanRestoreGroups', 'CanRestoreMembers', $groups, $members) |
|
804
|
|
|
->setTitle('Who can restore?') |
|
805
|
|
|
->setDescription('Groups or Users who can restore archives from Deploynaut into this environment'), |
|
806
|
|
|
|
|
807
|
|
|
// The Main.CanBackupMembers |
|
808
|
|
|
$this |
|
809
|
|
|
->buildPermissionField('CanBackupGroups', 'CanBackupMembers', $groups, $members) |
|
810
|
|
|
->setTitle('Who can backup?') |
|
811
|
|
|
->setDescription('Groups or Users who can backup archives from this environment into Deploynaut'), |
|
812
|
|
|
|
|
813
|
|
|
// The Main.ArchiveDeleters |
|
814
|
|
|
$this |
|
815
|
|
|
->buildPermissionField('ArchiveDeleterGroups', 'ArchiveDeleters', $groups, $members) |
|
816
|
|
|
->setTitle('Who can delete?') |
|
817
|
|
|
->setDescription("Groups or Users who can delete archives from this environment's staging area."), |
|
818
|
|
|
|
|
819
|
|
|
// The Main.ArchiveUploaders |
|
820
|
|
|
$this |
|
821
|
|
|
->buildPermissionField('ArchiveUploaderGroups', 'ArchiveUploaders', $groups, $members) |
|
822
|
|
|
->setTitle('Who can upload?') |
|
823
|
|
|
->setDescription( |
|
824
|
|
|
'Users who can upload archives linked to this environment into Deploynaut.<br />' . |
|
825
|
|
|
'Linking them to an environment allows limiting download permissions (see below).' |
|
826
|
|
|
), |
|
827
|
|
|
|
|
828
|
|
|
// The Main.ArchiveDownloaders |
|
829
|
|
|
$this |
|
830
|
|
|
->buildPermissionField('ArchiveDownloaderGroups', 'ArchiveDownloaders', $groups, $members) |
|
831
|
|
|
->setTitle('Who can download?') |
|
832
|
|
|
->setDescription(<<<PHP |
|
833
|
|
|
Users who can download archives from this environment to their computer.<br /> |
|
834
|
|
|
Since this implies access to the snapshot, it is also a prerequisite for restores |
|
835
|
|
|
to other environments, alongside the "Who can restore" permission.<br> |
|
836
|
|
|
Should include all users with upload permissions, otherwise they can't download |
|
837
|
|
|
their own uploads. |
|
838
|
|
|
PHP |
|
839
|
|
|
) |
|
840
|
|
|
|
|
841
|
|
|
]); |
|
842
|
|
|
|
|
843
|
|
|
// The Main.DeployConfig |
|
844
|
|
|
if ($this->Project()->exists()) { |
|
845
|
|
|
$this->setDeployConfigurationFields($fields); |
|
846
|
|
|
} |
|
847
|
|
|
|
|
848
|
|
|
// The DataArchives |
|
849
|
|
|
$dataArchiveConfig = GridFieldConfig_RecordViewer::create(); |
|
850
|
|
|
$dataArchiveConfig->removeComponentsByType('GridFieldAddNewButton'); |
|
851
|
|
|
if (class_exists('GridFieldBulkManager')) { |
|
852
|
|
|
$dataArchiveConfig->addComponent(new GridFieldBulkManager()); |
|
853
|
|
|
} |
|
854
|
|
|
$dataArchive = GridField::create('DataArchives', 'Data Archives', $this->DataArchives(), $dataArchiveConfig); |
|
855
|
|
|
$fields->addFieldToTab('Root.DataArchive', $dataArchive); |
|
856
|
|
|
|
|
857
|
|
|
// Deployments |
|
858
|
|
|
$deploymentsConfig = GridFieldConfig_RecordEditor::create(); |
|
859
|
|
|
$deploymentsConfig->removeComponentsByType('GridFieldAddNewButton'); |
|
860
|
|
|
if (class_exists('GridFieldBulkManager')) { |
|
861
|
|
|
$deploymentsConfig->addComponent(new GridFieldBulkManager()); |
|
862
|
|
|
} |
|
863
|
|
|
$deployments = GridField::create('Deployments', 'Deployments', $this->Deployments(), $deploymentsConfig); |
|
864
|
|
|
$fields->addFieldToTab('Root.Deployments', $deployments); |
|
865
|
|
|
|
|
866
|
|
|
Requirements::javascript('deploynaut/javascript/environment.js'); |
|
867
|
|
|
|
|
868
|
|
|
// Add actions |
|
869
|
|
|
$action = new FormAction('check', 'Check Connection'); |
|
870
|
|
|
$action->setUseButtonTag(true); |
|
871
|
|
|
$dataURL = Director::absoluteBaseURL() . 'naut/api/' . $this->Project()->Name . '/' . $this->Name . '/ping'; |
|
872
|
|
|
$action->setAttribute('data-url', $dataURL); |
|
873
|
|
|
$fields->insertBefore($action, 'Name'); |
|
|
|
|
|
|
874
|
|
|
|
|
875
|
|
|
// Allow extensions |
|
876
|
|
|
$this->extend('updateCMSFields', $fields); |
|
877
|
|
|
return $fields; |
|
878
|
|
|
} |
|
879
|
|
|
|
|
880
|
|
|
/** |
|
881
|
|
|
*/ |
|
882
|
|
|
public function onBeforeWrite() { |
|
883
|
|
|
parent::onBeforeWrite(); |
|
884
|
|
|
if ($this->Name && $this->Name . '.rb' != $this->Filename) { |
|
885
|
|
|
$this->Filename = $this->Name . '.rb'; |
|
886
|
|
|
} |
|
887
|
|
|
$this->checkEnvironmentPath(); |
|
888
|
|
|
$this->writeConfigFile(); |
|
889
|
|
|
} |
|
890
|
|
|
|
|
891
|
|
|
public function onAfterWrite() { |
|
892
|
|
|
parent::onAfterWrite(); |
|
893
|
|
|
|
|
894
|
|
|
if ($this->Usage === self::PRODUCTION || $this->Usage === self::UAT) { |
|
895
|
|
|
$conflicting = DNEnvironment::get() |
|
|
|
|
|
|
896
|
|
|
->filter('ProjectID', $this->ProjectID) |
|
897
|
|
|
->filter('Usage', $this->Usage) |
|
898
|
|
|
->exclude('ID', $this->ID); |
|
899
|
|
|
|
|
900
|
|
|
foreach ($conflicting as $otherEnvironment) { |
|
901
|
|
|
$otherEnvironment->Usage = self::UNSPECIFIED; |
|
902
|
|
|
$otherEnvironment->write(); |
|
903
|
|
|
} |
|
904
|
|
|
} |
|
905
|
|
|
} |
|
906
|
|
|
|
|
907
|
|
|
/** |
|
908
|
|
|
* Delete any related config files |
|
909
|
|
|
*/ |
|
910
|
|
|
public function onAfterDelete() { |
|
911
|
|
|
parent::onAfterDelete(); |
|
912
|
|
|
// Create a basic new environment config from a template |
|
913
|
|
|
if ($this->config()->get('allow_web_editing') && $this->envFileExists()) { |
|
914
|
|
|
unlink($this->getConfigFilename()); |
|
915
|
|
|
} |
|
916
|
|
|
|
|
917
|
|
|
$create = $this->CreateEnvironment(); |
|
|
|
|
|
|
918
|
|
|
if ($create && $create->exists()) { |
|
919
|
|
|
$create->delete(); |
|
920
|
|
|
} |
|
921
|
|
|
} |
|
922
|
|
|
|
|
923
|
|
|
/** |
|
924
|
|
|
* Returns the path to the ruby config file |
|
925
|
|
|
* |
|
926
|
|
|
* @return string |
|
927
|
|
|
*/ |
|
928
|
|
|
public function getConfigFilename() { |
|
929
|
|
|
if (!$this->Project()->exists()) { |
|
930
|
|
|
return ''; |
|
931
|
|
|
} |
|
932
|
|
|
if (!$this->Filename) { |
|
933
|
|
|
return ''; |
|
934
|
|
|
} |
|
935
|
|
|
return $this->DNData()->getEnvironmentDir() . '/' . $this->Project()->Name . '/' . $this->Filename; |
|
936
|
|
|
} |
|
937
|
|
|
|
|
938
|
|
|
/** |
|
939
|
|
|
* Helper function to convert a multi-dimensional array (associative or indexed) to an {@link ArrayList} or |
|
940
|
|
|
* {@link ArrayData} object structure, so that values can be used in templates. |
|
941
|
|
|
* |
|
942
|
|
|
* @param array $array The (single- or multi-dimensional) array to convert |
|
943
|
|
|
* @return object Either an {@link ArrayList} or {@link ArrayData} object, or the original item ($array) if $array |
|
944
|
|
|
* isn't an array. |
|
945
|
|
|
*/ |
|
946
|
|
|
public static function array_to_viewabledata($array) { |
|
947
|
|
|
// Don't transform non-arrays |
|
948
|
|
|
if (!is_array($array)) { |
|
949
|
|
|
return $array; |
|
950
|
|
|
} |
|
951
|
|
|
|
|
952
|
|
|
// Figure out whether this is indexed or associative |
|
953
|
|
|
$keys = array_keys($array); |
|
954
|
|
|
$assoc = ($keys != array_keys($keys)); |
|
955
|
|
|
if ($assoc) { |
|
956
|
|
|
// Treat as viewable data |
|
957
|
|
|
$data = new ArrayData([]); |
|
958
|
|
|
foreach ($array as $key => $value) { |
|
959
|
|
|
$data->setField($key, self::array_to_viewabledata($value)); |
|
960
|
|
|
} |
|
961
|
|
|
return $data; |
|
962
|
|
|
} else { |
|
963
|
|
|
// Treat this as basic non-associative list |
|
964
|
|
|
$list = new ArrayList(); |
|
965
|
|
|
foreach ($array as $value) { |
|
966
|
|
|
$list->push(self::array_to_viewabledata($value)); |
|
967
|
|
|
} |
|
968
|
|
|
return $list; |
|
969
|
|
|
} |
|
970
|
|
|
} |
|
971
|
|
|
|
|
972
|
|
|
/** |
|
973
|
|
|
* Fetchs all deployments in progress. Limits to 1 hour to prevent deployments |
|
974
|
|
|
* if an old deployment is stuck. |
|
975
|
|
|
* |
|
976
|
|
|
* @return DataList |
|
977
|
|
|
*/ |
|
978
|
|
|
public function runningDeployments() { |
|
979
|
|
|
return DNDeployment::get() |
|
980
|
|
|
->filter([ |
|
981
|
|
|
'EnvironmentID' => $this->ID, |
|
982
|
|
|
'State' => [ |
|
983
|
|
|
DNDeployment::STATE_QUEUED, |
|
984
|
|
|
DNDeployment::STATE_DEPLOYING, |
|
985
|
|
|
DNDeployment::STATE_ABORTING |
|
986
|
|
|
], |
|
987
|
|
|
'Created:GreaterThan' => strtotime('-1 hour') |
|
988
|
|
|
]); |
|
989
|
|
|
} |
|
990
|
|
|
|
|
991
|
|
|
/** |
|
992
|
|
|
* @param string $sha |
|
993
|
|
|
* @return array |
|
994
|
|
|
*/ |
|
995
|
|
|
protected function getCommitData($sha) { |
|
996
|
|
|
try { |
|
997
|
|
|
$repo = $this->Project()->getRepository(); |
|
998
|
|
|
if ($repo !== false) { |
|
999
|
|
|
$commit = new \Gitonomy\Git\Commit($repo, $sha); |
|
1000
|
|
|
return [ |
|
1001
|
|
|
'AuthorName' => (string) Convert::raw2xml($commit->getAuthorName()), |
|
1002
|
|
|
'AuthorEmail' => (string) Convert::raw2xml($commit->getAuthorEmail()), |
|
1003
|
|
|
'Message' => (string) Convert::raw2xml($this->getCommitMessage($commit)), |
|
1004
|
|
|
'ShortHash' => Convert::raw2xml($commit->getFixedShortHash(8)), |
|
1005
|
|
|
'Hash' => Convert::raw2xml($commit->getHash()) |
|
1006
|
|
|
]; |
|
1007
|
|
|
} |
|
1008
|
|
|
} catch (\Gitonomy\Git\Exception\ReferenceNotFoundException $exc) { |
|
1009
|
|
|
SS_Log::log($exc, SS_Log::WARN); |
|
1010
|
|
|
} |
|
1011
|
|
|
return [ |
|
1012
|
|
|
'AuthorName' => '(unknown)', |
|
1013
|
|
|
'AuthorEmail' => '(unknown)', |
|
1014
|
|
|
'Message' => '(unknown)', |
|
1015
|
|
|
'ShortHash' => $sha, |
|
1016
|
|
|
'Hash' => '(unknown)', |
|
1017
|
|
|
]; |
|
1018
|
|
|
} |
|
1019
|
|
|
|
|
1020
|
|
|
/** |
|
1021
|
|
|
* Build a set of multi-select fields for assigning permissions to a pair of group and member many_many relations |
|
1022
|
|
|
* |
|
1023
|
|
|
* @param string $groupField Group field name |
|
1024
|
|
|
* @param string $memberField Member field name |
|
1025
|
|
|
* @param array $groups List of groups |
|
1026
|
|
|
* @param array $members List of members |
|
1027
|
|
|
* @return FieldGroup |
|
1028
|
|
|
*/ |
|
1029
|
|
|
protected function buildPermissionField($groupField, $memberField, $groups, $members) { |
|
1030
|
|
|
return FieldGroup::create( |
|
1031
|
|
|
ListboxField::create($groupField, false, $groups) |
|
1032
|
|
|
->setMultiple(true) |
|
1033
|
|
|
->setAttribute('data-placeholder', 'Groups') |
|
1034
|
|
|
->setAttribute('placeholder', 'Groups') |
|
1035
|
|
|
->setAttribute('style', 'width: 400px;'), |
|
1036
|
|
|
|
|
1037
|
|
|
ListboxField::create($memberField, false, $members) |
|
1038
|
|
|
->setMultiple(true) |
|
1039
|
|
|
->setAttribute('data-placeholder', 'Members') |
|
1040
|
|
|
->setAttribute('placeholder', 'Members') |
|
1041
|
|
|
->setAttribute('style', 'width: 400px;') |
|
1042
|
|
|
); |
|
1043
|
|
|
} |
|
1044
|
|
|
|
|
1045
|
|
|
/** |
|
1046
|
|
|
* @param FieldList $fields |
|
1047
|
|
|
*/ |
|
1048
|
|
|
protected function setDeployConfigurationFields(&$fields) { |
|
1049
|
|
|
if (!$this->config()->get('allow_web_editing')) { |
|
1050
|
|
|
return; |
|
1051
|
|
|
} |
|
1052
|
|
|
|
|
1053
|
|
|
if ($this->envFileExists()) { |
|
1054
|
|
|
$deployConfig = new TextareaField('DeployConfig', 'Deploy config', $this->getEnvironmentConfig()); |
|
1055
|
|
|
$deployConfig->setRows(40); |
|
1056
|
|
|
$fields->insertAfter($deployConfig, 'Filename'); |
|
|
|
|
|
|
1057
|
|
|
return; |
|
1058
|
|
|
} |
|
1059
|
|
|
|
|
1060
|
|
|
$warning = 'Warning: This environment doesn\'t have deployment configuration.'; |
|
1061
|
|
|
$noDeployConfig = new LabelField('noDeployConfig', $warning); |
|
1062
|
|
|
$noDeployConfig->addExtraClass('message warning'); |
|
1063
|
|
|
$fields->insertAfter($noDeployConfig, 'Filename'); |
|
|
|
|
|
|
1064
|
|
|
$createConfigField = new CheckboxField('CreateEnvConfig', 'Create Config'); |
|
1065
|
|
|
$createConfigField->setDescription('Would you like to create the capistrano deploy configuration?'); |
|
1066
|
|
|
$fields->insertAfter($createConfigField, 'noDeployConfig'); |
|
|
|
|
|
|
1067
|
|
|
} |
|
1068
|
|
|
|
|
1069
|
|
|
/** |
|
1070
|
|
|
* Ensure that environment paths are setup on the local filesystem |
|
1071
|
|
|
*/ |
|
1072
|
|
|
protected function checkEnvironmentPath() { |
|
1073
|
|
|
// Create folder if it doesn't exist |
|
1074
|
|
|
$configDir = dirname($this->getConfigFilename()); |
|
1075
|
|
|
if (!file_exists($configDir) && $configDir) { |
|
1076
|
|
|
mkdir($configDir, 0777, true); |
|
1077
|
|
|
} |
|
1078
|
|
|
} |
|
1079
|
|
|
|
|
1080
|
|
|
/** |
|
1081
|
|
|
* Write the deployment config file to filesystem |
|
1082
|
|
|
*/ |
|
1083
|
|
|
protected function writeConfigFile() { |
|
1084
|
|
|
if (!$this->config()->get('allow_web_editing')) { |
|
1085
|
|
|
return; |
|
1086
|
|
|
} |
|
1087
|
|
|
|
|
1088
|
|
|
// Create a basic new environment config from a template |
|
1089
|
|
|
if (!$this->envFileExists() |
|
1090
|
|
|
&& $this->Filename |
|
1091
|
|
|
&& $this->CreateEnvConfig |
|
|
|
|
|
|
1092
|
|
|
) { |
|
1093
|
|
|
$templateFile = $this->config()->template_file ?: BASE_PATH . '/deploynaut/environment.template'; |
|
1094
|
|
|
file_put_contents($this->getConfigFilename(), file_get_contents($templateFile)); |
|
1095
|
|
|
} else if ($this->envFileExists() && $this->DeployConfig) { |
|
|
|
|
|
|
1096
|
|
|
file_put_contents($this->getConfigFilename(), $this->DeployConfig); |
|
|
|
|
|
|
1097
|
|
|
} |
|
1098
|
|
|
} |
|
1099
|
|
|
|
|
1100
|
|
|
/** |
|
1101
|
|
|
* @return string |
|
1102
|
|
|
*/ |
|
1103
|
|
|
protected function getEnvironmentConfig() { |
|
1104
|
|
|
if (!$this->envFileExists()) { |
|
1105
|
|
|
return ''; |
|
1106
|
|
|
} |
|
1107
|
|
|
return file_get_contents($this->getConfigFilename()); |
|
1108
|
|
|
} |
|
1109
|
|
|
|
|
1110
|
|
|
/** |
|
1111
|
|
|
* @return boolean |
|
1112
|
|
|
*/ |
|
1113
|
|
|
protected function envFileExists() { |
|
1114
|
|
|
if (!$this->getConfigFilename()) { |
|
1115
|
|
|
return false; |
|
1116
|
|
|
} |
|
1117
|
|
|
return file_exists($this->getConfigFilename()); |
|
1118
|
|
|
} |
|
1119
|
|
|
|
|
1120
|
|
|
protected function validate() { |
|
1121
|
|
|
$result = parent::validate(); |
|
1122
|
|
|
$backend = $this->Backend(); |
|
1123
|
|
|
|
|
1124
|
|
|
if (strcasecmp('test', $this->Name) === 0 && get_class($backend) == 'CapistranoDeploymentBackend') { |
|
1125
|
|
|
$result->error('"test" is not a valid environment name when using Capistrano backend.'); |
|
1126
|
|
|
} |
|
1127
|
|
|
|
|
1128
|
|
|
return $result; |
|
1129
|
|
|
} |
|
1130
|
|
|
|
|
1131
|
|
|
} |
|
1132
|
|
|
|
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.