-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathEnvironment.php
More file actions
824 lines (759 loc) · 24.6 KB
/
Environment.php
File metadata and controls
824 lines (759 loc) · 24.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
<?php
namespace Platformsh\Client\Model;
use Cocur\Slugify\Slugify;
use GuzzleHttp\ClientInterface;
use Platformsh\Client\Exception\EnvironmentStateException;
use Platformsh\Client\Exception\OperationUnavailableException;
use Platformsh\Client\Model\Activities\HasActivitiesInterface;
use Platformsh\Client\Model\Activities\HasActivitiesTrait;
use Platformsh\Client\Model\Backups\BackupConfig;
use Platformsh\Client\Model\Backups\Policy;
use Platformsh\Client\Model\Deployment\EnvironmentDeployment;
use Platformsh\Client\Model\Deployment\Worker;
use Platformsh\Client\Model\Git\Commit;
use Platformsh\Client\Model\Type\Duration;
/**
* A Platform.sh environment.
*
* Environments correspond to project Git branches.
*
* @property-read string $id
* The primary ID of the environment. This is the same as the 'name' property.
* @property-read string $status
* The status of the environment: active, inactive, deleting, dirty, or paused.
* @property-read string $head_commit
* The SHA-1 hash identifying the Git commit at the branch's HEAD.
* @property-read string $name
* The Git branch name of the environment.
* @property-read string|null $parent
* The ID (or name) of the parent environment, or null if there is no parent.
* @property-read string $machine_name
* A slug of the ID, sanitized for use in domain names, with a random suffix
* (for uniqueness within a project). Can contain lower-case letters, numbers,
* and hyphens.
* @property-read string $title
* A human-readable title or label for the environment.
* @property-read string $created_at
* The date the environment was created (ISO 8601).
* @property-read string $updated_at
* The date the environment was last updated (ISO 8601).
* @property-read string $project
* The project ID for the environment.
* @property-read bool $is_dirty
* Whether the environment is in a 'dirty' state: deploying or broken.
* @property-read bool $enable_smtp
* Whether outgoing emails should be enabled for an environment.
* @property-read bool $has_code
* Whether the environment has any code committed.
* @property-read string $deployment_target
* The deployment target for an environment (always 'local' for now).
* @property-read array $http_access
* HTTP access control for an environment. An array containing at least
* 'is_enabled' (bool), 'addresses' (array), and 'basic_auth' (array).
* @property-read bool $is_main
* Whether the environment is the main, production one.
* @property-read array $backups
* The backup configuration. It's recommended to use getBackupConfig() instead
* of using this array directly.
* @property-read string $edge_hostname
* The hostname for the edge router that serves the environment.
* @property-read string $type
* The type of the environment. This is predominantly used for access control and is usually one of: production,
* development, or staging.
*/
class Environment extends Resource implements HasActivitiesInterface
{
use HasActivitiesTrait;
/**
* Returns the environment's deployments.
*
* @return EnvironmentDeployment[]
*/
public function getDeployments()
{
return EnvironmentDeployment::getCollection($this->getUri() . '/deployments', 0, [], $this->client);
}
/**
* Get the next deployment of this environment.
*
* @return EnvironmentDeployment|false
*/
public function getNextDeployment()
{
return EnvironmentDeployment::get('next', $this->getUri() . '/deployments', $this->client);
}
/**
* Get the current deployment of this environment.
*
* @param bool $required
* Whether to throw an exception if not found.
* The current deployment would not exist if the environment is inactive.
*
* @throws EnvironmentStateException if no current deployment is found and $required is true
*
* @return EnvironmentDeployment|false
* The deployment, or false if no current deployment is found and $required is false
**/
public function getCurrentDeployment($required = true)
{
$deployment = EnvironmentDeployment::get('current', $this->getUri() . '/deployments', $this->client);
if (!$deployment && $required) {
throw new EnvironmentStateException('Current deployment not found', $this);
}
return $deployment;
}
/**
* Get the Git commit for the HEAD of this environment.
*
* @return Commit|false
*/
public function getHeadCommit()
{
$base = Project::getProjectBaseFromUrl($this->getUri()) . '/git/commits';
return Commit::get($this->head_commit, $base, $this->client);
}
/**
* Get the SSH URL for the environment.
*
* @param string $app
* An application or worker name. If there is no published URL for this
* app name, the 'legacy' URL (without an app name) will be returned.
* @param string|null $instance
* An instance ID. This is usually numeric starting with 0. Some legacy
* dedicated environments have their instances starting from 1. If the
* app does not have multiple instances, leave this as an empty string
* or null.
*
* @throws EnvironmentStateException
* @throws OperationUnavailableException
* @throws \InvalidArgumentException if the $instance is not found
*
* @return string
*/
public function getSshUrl($app = '', $instance = '')
{
$urls = $this->getSshUrls();
$instances = $this->getSshInstanceURLs($app, $urls);
if ($instance !== '' && $instance !== null) {
if (isset($instances[$instance])) {
return $instances[$instance];
}
$message = \sprintf("SSH URL not found for instance '%s' of '%s'.", $instance, $app);
if (count($instances)) {
$message .= \sprintf(' Available instances: %s', implode(', ', array_keys($instances)));
}
throw new \InvalidArgumentException($message);
}
if (isset($urls[$app])) {
return $urls[$app];
}
if (!empty($instances)) {
return reset($instances);
}
// Fall back to the legacy SSH URL.
return $this->constructLegacySshUrl();
}
/**
* List instance URLs for a specific app.
*
* @param string $app The app name.
* @param ?array $sshUrls
*
* @return array<mixed, string>
* An array of SSH URLs for the given app, keyed by instance ID.
*/
public function getSshInstanceURLs($app, $sshUrls = null)
{
$urls = $sshUrls === null ? $this->getSshUrls() : $sshUrls;
$instances = [];
foreach ($urls as $key => $url) {
if (\strpos($key, "$app:") === 0) {
$parts = explode(':', $key, 3);
if (isset($parts[1])) {
$instances[$parts[1]] = $url;
}
}
}
if ($instances === []) {
// Handle legacy dedicated instance URLs.
foreach ($urls as $key => $url) {
if (strpos($key, 'ent-') === 0) {
$instances[substr($key, 4)] = $url;
}
}
}
natsort($instances);
return $instances;
}
/**
* Get the SSH URL for a worker.
*
* Workers themselves can be listed via getCurrentDeployment()->workers.
*
* @param Worker $worker
* @param string $instance
*
* @return string
*/
public function getWorkerSshUrl(Worker $worker, $instance = '')
{
return $this->getSshUrl($worker->name, $instance);
}
/**
* Get the SSH URL via the legacy 'ssh' link.
*
* @return string
*/
private function constructLegacySshUrl()
{
if (!$this->hasLink('ssh')) {
if ($this->data['status'] !== 'active') {
throw new EnvironmentStateException(sprintf(
"No SSH URL found for environment '%s'. It is not currently active (status: %s).",
$this->data['id'], $this->data['status']
), $this);
}
throw new OperationUnavailableException(sprintf(
"No SSH URL found for environment '%s'. You may not have permission to SSH.",
$this->data['id']
));
}
return $this->convertSshUrl($this->getLink('ssh'));
}
/**
* Convert a full SSH URL (with scheme) into a normal SSH connection string.
*
* This can then be used with tools such as scp, etc.
*
* Only the username, host and path will be preserved (the port, password,
* query and fragment will be dropped).
*
* @param string $url The URL (starting with ssh://).
*
* @return string
*/
private function convertSshUrl($url)
{
$parsed = parse_url($url);
if (!$parsed) {
throw new \InvalidArgumentException('Invalid URL: ' . $url);
}
$str = '';
if (!empty($parsed['user'])) {
$str .= $parsed['user'] . '@';
}
$str .= $parsed['host'];
if (!empty($parsed['path'])) {
$str .= ':' . $parsed['path'];
}
return $str;
}
/**
* Returns a list of SSH URLs, keyed by app name.
*
* @return string[]
*/
public function getSshUrls()
{
$prefix = 'pf:ssh:';
$prefixLength = strlen($prefix);
$sshUrls = [];
foreach ($this->data['_links'] as $rel => $link) {
if (strpos($rel, $prefix) === 0 && isset($link['href'])) {
$sshUrls[substr($rel, $prefixLength)] = $this->convertSshUrl($link['href']);
}
}
if (empty($sshUrls) && $this->hasLink('ssh')) {
$sshUrls[''] = $this->convertSshUrl($this->getLink('ssh'));
}
return $sshUrls;
}
/**
* Get the public URL for the environment.
*
* @throws EnvironmentStateException
*
* @deprecated You should use routes to get the correct URL(s)
* @see self::getRouteUrls()
*
* @return string
*/
public function getPublicUrl()
{
if (!$this->hasLink('public-url')) {
$id = $this->data['id'];
if (!$this->isActive()) {
throw new EnvironmentStateException("No public URL found for environment '$id'. It is not currently active.", $this);
}
throw new OperationUnavailableException("No public URL found for environment '$id'.");
}
return $this->getLink('public-url');
}
/**
* Branches an environment (creates a new environment as a child of the current one).
*
* The new environment's code will be the same as the parent environment.
* Some other settings are typically inherited, such as variables.
* Data is cloned from the parent environment (if $cloneParent is left as
* true), including all data from services and file mounts.
*
* @param string $title The title of the new environment.
* @param string|null $id The ID of the new environment. This will be the Git
* branch name. Leave empty to generate automatically
* from the title (not recommended).
* @param bool $cloneParent Whether to clone data from the parent
* environment while branching.
* @param string|null $type The environment type, e.g. 'staging' or 'development'.
* Leave this empty to use the default type for new
* environments ('development' at the time of writing).
*
* @deprecated use instead: runOperation('branch', 'POST', ['name' => 'git-branch-name', 'title' => 'Untitled', 'clone_parent' => true, 'type' => 'development'])
* @see Environment::runOperation()
*
* @return Activity
*/
public function branch($title, $id = null, $cloneParent = true, $type = null)
{
$id = $id ?: $this->sanitizeId($title);
$body = ['name' => $id, 'title' => $title];
if (!$cloneParent) {
$body['clone_parent'] = false;
}
if ($type !== null) {
$body['type'] = $type;
}
return $this->runLongOperation('branch', 'post', $body);
}
/**
* @param string $proposed
*
* @return string
*/
public static function sanitizeId($proposed)
{
$slugify = new Slugify();
return substr($slugify->slugify($proposed), 0, 32);
}
/**
* Validate an environment ID.
*
* @deprecated This is no longer necessary and will be removed in future
* versions.
*
* @param string $id
*
* @return bool
*/
public static function validateId($id)
{
return !empty($id);
}
/**
* Delete the environment.
*
* @throws EnvironmentStateException
*
* @return Result
*/
public function delete()
{
if ($this->isActive()) {
throw new EnvironmentStateException('Active environments cannot be deleted', $this);
}
return parent::delete();
}
/**
* Returns whether the environment is active.
*
* It's recommended to use the status property for more nuance
* ('active', 'paused', etc.).
*
* @see Environment::status
*
* @return bool
*/
public function isActive()
{
return $this->data['status'] === 'active';
}
/**
* Activate the environment.
*
* @throws EnvironmentStateException
*
* @deprecated use instead: runOperation('activate')
* @see Environment::runOperation()
*
* @return Activity
*/
public function activate()
{
if ($this->isActive()) {
throw new EnvironmentStateException('Active environments cannot be activated', $this);
}
return $this->runLongOperation('activate');
}
/**
* Deactivate the environment.
*
* @deprecated use instead: runOperation('deactivate')
* @see Environment::runOperation()
*
* @return Activity
*/
public function deactivate()
{
return $this->runLongOperation('deactivate');
}
/**
* Merge an environment into its parent.
*
* @throws OperationUnavailableException
*
* @deprecated use instead: runOperation('merge')
* @see Environment::runOperation()
*
* @return Activity
*/
public function merge()
{
if (!$this->getProperty('parent')) {
throw new OperationUnavailableException('The environment does not have a parent, so it cannot be merged');
}
return $this->runLongOperation('merge');
}
/**
* Synchronize an environment with its parent.
*
* @param bool $code Synchronize code.
* @param bool $data Synchronize data.
* @param bool $rebase Synchronize code by rebasing instead of merging.
*
* @throws \InvalidArgumentException
*
* @deprecated use instead: runOperation('synchronize', 'POST', ['synchronize_data' => false, 'synchronize_code' => false, 'rebase' => false])
* @see Environment::runOperation()
*
* @return Activity
*/
public function synchronize($data = false, $code = false, $rebase = false)
{
if (!$data && !$code) {
throw new \InvalidArgumentException('Nothing to synchronize: you must specify $data or $code');
}
$body = [
'synchronize_data' => $data,
'synchronize_code' => $code,
];
if ($rebase) {
// @todo always add this (when the rebase option is GA)
$body['rebase'] = $rebase;
}
return $this->runLongOperation('synchronize', 'post', $body);
}
/**
* Create a backup of the environment.
*
* @param bool $unsafeAllowInconsistent
* Whether to allow performing an inconsistent backup (default: false).
* If true, this leaves the environment running and open to connections
* during the backup. So it reduces downtime, at the risk of backing up
* data in an inconsistent state.
*
* @deprecated use instead: runOperation('backup', 'POST', ['safe' => true])
* @see Environment::runOperation()
*
* @return Activity
*/
public function backup($unsafeAllowInconsistent = false)
{
$params = [];
if ($unsafeAllowInconsistent) {
$params['safe'] = false;
}
return $this->runLongOperation('backup', 'post', $params);
}
/**
* Get a list of variables.
*
* @param int $limit
*
* @return Variable[]
*/
public function getVariables($limit = 0)
{
return Variable::getCollection($this->getLink('#variables'), $limit, [], $this->client);
}
/**
* Set a variable
*
* @param string $name
* @param mixed $value
* @param bool $json
* @param bool $enabled
* @param bool $sensitive
*
* @return Result
*/
public function setVariable(
$name,
$value,
$json = false,
$enabled = true,
$sensitive = false
)
{
if (!is_scalar($value)) {
$value = json_encode($value);
$json = true;
}
$values = ['value' => $value, 'is_json' => $json, 'is_enabled' => $enabled];
if ($sensitive) {
$values['is_sensitive'] = $sensitive;
}
$existing = $this->getVariable($name);
if ($existing) {
return $existing->update($values);
}
$values['name'] = $name;
return Variable::create($values, $this->getLink('#manage-variables'), $this->client);
}
/**
* Get a single variable.
*
* @param string $id
*
* @return Variable|false
*/
public function getVariable($id)
{
return Variable::get($id, $this->getLink('#variables'), $this->client);
}
/**
* Get the environment's routes configuration.
*
* @see self::getRouteUrls()
*
* @return Route[]
*/
public function getRoutes()
{
return Route::getCollection($this->getLink('#routes'), 0, [], $this->client);
}
/**
* Get the resolved URLs for the environment's routes.
*
* @return string[]
*/
public function getRouteUrls()
{
$routes = [];
if (isset($this->data['_links']['pf:routes'])) {
foreach ($this->data['_links']['pf:routes'] as $route) {
$routes[] = $route['href'];
}
}
return $routes;
}
/**
* Initialize the environment from an external repository.
*
* This can only work when the repository is empty.
*
* @param string $profile
* The name of the profile. This is shown in the resulting activity log.
* @param string $repository
* A repository URL, optionally followed by an '@' sign and a branch name,
* e.g. 'git://github.com/platformsh/platformsh-examples.git@drupal/7.x'.
* The default branch is 'master'.
*
* @deprecated use instead: runOperation('initialize', 'POST', ['profile' => '', 'repository' => ''])
* @see Environment::runOperation()
*
* @return Activity
*/
public function initialize($profile, $repository)
{
$values = [
'profile' => $profile,
'repository' => $repository,
];
return $this->runLongOperation('initialize', 'post', $values);
}
/**
* Get a user's access to this environment.
*
* @param string $uuid
*
* @return EnvironmentAccess|false
*/
public function getUser($uuid)
{
return EnvironmentAccess::get($uuid, $this->getLink('#access'), $this->client);
}
/**
* Get the users with access to this environment.
*
* @return EnvironmentAccess[]
*/
public function getUsers()
{
return EnvironmentAccess::getCollection($this->getLink('#access'), 0, [], $this->client);
}
/**
* Add a new user to the environment.
*
* @param string $user The user's UUID or email address (see $byUuid).
* @param string $role One of EnvironmentAccess::$roles.
* @param bool $byUuid Set true (default) if $user is a UUID, or false if
* $user is an email address.
*
* @deprecated Users should now be invited via Project::inviteUserByEmail()
*
* @see Project::inviteUserByEmail()
*
* @return Result
*/
public function addUser($user, $role, $byUuid = true)
{
$property = $byUuid ? 'user' : 'email';
$body = [$property => $user, 'role' => $role];
return EnvironmentAccess::create($body, $this->getLink('#manage-access'), $this->client);
}
/**
* Redeploy the environment.
*
* @deprecated use instead: runOperation('redeploy')
* @see Environment::runOperation()
*
* @return Activity
*/
public function redeploy()
{
return $this->runLongOperation('redeploy');
}
/**
* Fetches a single environment backup.
*
* @return Backup|false
*/
public function getBackup($id)
{
return Backup::get($id, $this->getUri() . '/backups', $this->client);
}
/**
* Fetches a list of environment backups.
*
* @param int $limit
* Limit the number of backups to return.
*
* @return Backup[]
*/
public function getBackups($limit = 0)
{
return Backup::getCollection($this->getUri() . '/backups', $limit, [], $this->client);
}
/**
* Get the scheduled backup configuration for this environment.
*
* @return BackupConfig
*/
public function getBackupConfig()
{
// In legacy versions the 'backups' key might not exist on the
// environment.
return BackupConfig::fromData($this->getProperty('backups', false) ?: []);
}
/**
* Add a scheduled backup policy.
*
* @param \Platformsh\Client\Model\Backups\Policy $policy
*
* @return \Platformsh\Client\Model\Result
*/
public function addBackupPolicy(Policy $policy)
{
$backups = isset($this->data['backups']) ? $this->data['backups'] : [];
$backups['schedule'][] = [
'interval' => $policy->getInterval(),
'count' => $policy->getCount(),
];
$backups += ['manual_count' => 3];
// Sort the backup schedule, by interval.
usort($backups['schedule'], function (array $a, array $b) {
return (new Duration($a['interval']))->compare(new Duration($b['interval']));
});
return $this->update(['backups' => $backups]);
}
/**
* Lists source operations.
*
* @return []SourceOperation
*/
public function getSourceOperations()
{
return SourceOperation::getCollection($this->getLink('#source-operations'), 0, [], $this->client);
}
/**
* Lists environment settings.
*
* @return Settings
*/
public function getSettings()
{
$url = $this->getUri() . '/settings';
$request = $this->client->createRequest('get', $url);
$data = self::send($request, $this->client);
return new Settings($data, $url, $this->client);
}
/**
* Runs a source operation.
*
* @param string $name
* The operation name.
* @param array $variables
* Variables to define during the operation, as a nested associative
* array, e.g. ['env'=>['foo'=>'bar']]
*
* @return \Platformsh\Client\Model\Result
*/
public function runSourceOperation($name, array $variables = [])
{
return $this->runOperation('source-operation', 'post', [
'operation' => $name,
'variables' => (object) $variables,
]);
}
/**
* Pauses the environment.
*
* The environment can be resumed via resume() or any deployment.
*
* @see Environment::resume()
* @see Environment::redeploy()
* @see Environment::status
*
* @deprecated use instead: runOperation('pause')
* @see Environment::runOperation()
*
* @return Activity
*/
public function pause()
{
return $this->runLongOperation('pause');
}
/**
* Resumes a paused environment.
*
* @see Environment::status
*
* @deprecated use instead: runOperation('resume')
* @see Environment::runOperation()
*
* @return Activity
*/
public function resume()
{
return $this->runLongOperation('resume');
}
}