forked from brefphp/bref
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbref
More file actions
executable file
·160 lines (135 loc) · 5.47 KB
/
Copy pathbref
File metadata and controls
executable file
·160 lines (135 loc) · 5.47 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
#!/usr/bin/env php
<?php
declare(strict_types=1);
use Bref\Lambda\InvocationFailed;
use Bref\Lambda\SimpleLambdaClient;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
} elseif (file_exists(__DIR__ . '/../autoload.php')) {
/** @noinspection PhpIncludeInspection */
require_once __DIR__ . '/../autoload.php';
} else {
/** @noinspection PhpIncludeInspection */
require_once __DIR__ . '/../../autoload.php';
}
$app = new Silly\Application('Deploy serverless PHP applications');
$app->command('init', function (SymfonyStyle $io) {
$exeFinder = new ExecutableFinder();
if (! $exeFinder->find('sam')) {
$io->error(
'The `sam` command is not installed.' . PHP_EOL .
'Please follow the instructions at https://bref.sh/docs/installation.html'
);
return 1;
}
if (getenv('AWS_ACCESS_KEY_ID') === false || getenv('AWS_SECRET_ACCESS_KEY') === false) {
// Credentials are not configured with environment variables, check aws cli
$awsProcess1 = new Process(['aws', 'configure', 'get', 'aws_access_key_id']);
$awsProcess2 = new Process(['aws', 'configure', 'get', 'aws_secret_access_key']);
if (($awsProcess1->run() !== 0) || ($awsProcess2->run() !== 0)) {
$io->error([
'AWS credentials not found.',
'Please follow the instructions at https://bref.sh/docs/installation.html',
]);
return 1;
}
}
if (file_exists('template.yaml') || file_exists('index.php')) {
$io->error('The directory already contains a `template.yaml` and/or `index.php` file.');
return 1;
}
$choice = $io->choice(
'What kind of lambda do you want to create? (you will be able to add more functions later by editing `template.yaml`)',
[
'PHP function',
'HTTP application',
'Console application',
],
'PHP function'
);
$templateDirectory = [
'PHP function' => 'default',
'HTTP application' => 'http',
'Console application' => 'console',
][$choice];
$fs = new Filesystem;
$rootPath = __DIR__ . "/template/$templateDirectory";
$filesToGitAdd = [];
foreach (scandir($rootPath, SCANDIR_SORT_NONE) as $file) {
if (in_array($file, ['.', '..'])) {
continue;
}
$io->writeln("Creating $file");
$fs->copy("$rootPath/$file", $file);
$filesToGitAdd[] = $file;
}
$regionChoice = $io->ask(
'In which AWS region do you want to deploy your application? (`us-east-1` is highly recommended for first time users)',
'us-east-1'
);
if ($regionChoice !== 'us-east-1') {
file_put_contents('template.yaml', str_replace('us-east-1', $regionChoice, file_get_contents('template.yaml')));
}
/*
* We check if this is a git repository to automatically add files to git.
*/
if ((new Process(['git', 'rev-parse', '--is-inside-work-tree']))->run() === 0) {
foreach ($filesToGitAdd as $file) {
(new Process(['git', 'add', $file]))->run();
}
$io->success([
'Project initialized and ready to test or deploy.',
'The files created were automatically added to git.',
]);
} else {
$io->success('Project initialized and ready to test or deploy.');
}
return 0;
});
/**
* Run a CLI command in the remote environment.
*/
$app->command('cli function [--region=] [arguments]*', function (string $function, ?string $region, array $arguments, SymfonyStyle $io) {
$lambda = new SimpleLambdaClient(($region ?: getenv('AWS_DEFAULT_REGION')) ?: 'us-east-1');
try {
$result = $lambda->invoke($function, json_encode([
'cli' => implode(' ', $arguments),
]));
} catch (InvocationFailed $e) {
$io->getErrorStyle()->writeln('<info>' . $e->getInvocationLogs() . '</info>');
$io->error($e->getMessage());
return 1;
}
$payload = $result->getPayload();
if (isset($payload['output'])) {
$io->writeln($payload['output']);
} else {
$io->error('The command did not return a valid response.');
$io->writeln('<info>Logs:</info>');
$io->write('<comment>' . $result->getLogs() . '</comment>');
$io->writeln('<info>Lambda result payload:</info>');
$io->writeln(json_encode($payload, JSON_PRETTY_PRINT));
return 1;
}
return (int) ($payload['exitCode'] ?? 1);
});
$app->command('invoke function [--region=] [-e|--event=]', function (string $function, ?string $region, ?string $event, SymfonyStyle $io) {
$lambda = new SimpleLambdaClient(($region ?: getenv('AWS_DEFAULT_REGION')) ?: 'us-east-1');
try {
$result = $lambda->invoke($function, $event);
} catch (InvocationFailed $e) {
$io->getErrorStyle()->writeln('<info>' . $e->getInvocationLogs() . '</info>');
$io->error($e->getMessage());
return 1;
}
$io->getErrorStyle()->writeln('<info>' . $result->getLogs() . '</info>');
$io->writeln(json_encode($result->getPayload(), JSON_PRETTY_PRINT));
return 0;
})->descriptions('Invoke the lambda on the serverless provider', [
'--event' => 'Event data as JSON, e.g. `--event \'{"name":"matt"}\'`',
]);
$app->run();