-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathDetermineRegion.php
More file actions
101 lines (83 loc) · 2.43 KB
/
DetermineRegion.php
File metadata and controls
101 lines (83 loc) · 2.43 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
<?php
declare(strict_types=1);
/**
* @author Aaron Francis <aarondfrancis@gmail.com|https://twitter.com/aarondfrancis>
*/
namespace Hammerstone\Sidecar\Commands\Actions;
use Aws\S3\S3Client;
use Illuminate\Support\Facades\File;
use Throwable;
class DetermineRegion extends BaseAction
{
protected S3Client $client;
protected bool $isVapor = false;
public function invoke(): string
{
$region = config('sidecar.aws_region');
if ($this->isValidAwsRegion($region)) {
$this->progress("Region `$region` already set in configuration.");
} else {
$region = $this->getRegionFromVapor();
}
if (!$region) {
$region = $this->command->choice('What AWS region would you like your functions to be deployed in?', $this->awsRegions(), 'us-east-2');
}
$this->progress("Using region `$region`");
return $region;
}
protected function getRegionFromVapor()
{
if (!File::exists(base_path('vapor.yml'))) {
return;
}
try {
$region = trim(shell_exec('vapor project:describe region'));
if (!$this->isValidAwsRegion($region)) {
return;
}
} catch (Throwable $e) {
return;
}
$question = implode("\n", [
"This Vapor project deploys to the AWS `$region` region.",
' Would you like to use the same region for your Sidecar functions?'
]);
if ($this->command->confirm($question, $default = true)) {
return $region;
}
}
protected function isValidAwsRegion($region)
{
return in_array($region, $this->awsRegions());
}
protected function awsRegions()
{
return [
'us-east-1',
'us-east-2',
'us-west-1',
'us-west-2',
'af-south-1',
'ap-east-1',
'ap-south-1',
'ap-southeast-1',
'ap-northeast-2',
'ap-northeast-3',
'ap-northeast-1',
'ap-southeast-2',
'ca-central-1',
'cn-north-1',
'cn-northwest-1',
'eu-central-1',
'eu-west-1',
'eu-west-2',
'eu-south-1',
'eu-west-3',
'eu-north-1',
'me-south-1',
'sa-east-1',
'us-gov-east-1',
'us-gov-west-1',
];
}
}