Skip to content

Commit 1e44e86

Browse files
committed
MFH
2 parents 2c04b0d + 8c4b206 commit 1e44e86

File tree

5 files changed

+56
-51
lines changed

5 files changed

+56
-51
lines changed

ChangeLog.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,25 @@ AWS Lambda change log
33

44
## ?.?.? / ????-??-??
55

6-
## 2.4.0 / 2021-11-11
6+
## 3.1.0 / 2021-11-11
77

88
* Enabled SimpleXML extension in order to be able to support AWS SDK
99
see https://github.com/xp-forge/lambda/issues/8#issuecomment-966308720
10+
11+
## 3.0.2 / 2021-10-21
12+
13+
* Made library compatible with new major release of `xp-forge/json`
14+
(@thekid)
15+
16+
## 3.0.1 / 2021-10-21
17+
18+
* Made library compatible with XP 11, newer `xp-framework/zip` library
19+
(@thekid)
20+
21+
## 3.0.0 / 2021-09-26
22+
23+
* Merged PR #13: Refactor container execution to Process API provided by
24+
XP 10.14. Drops BC with XP 9 and lower, see xp-framework/rfc#341.
1025
(@thekid)
1126

1227
## 2.3.0 / 2021-09-24

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
"description" : "AWS Lambda for the XP Framework",
77
"keywords": ["module", "xp"],
88
"require" : {
9-
"xp-framework/core": "^10.0 | ^9.0 | ^8.0 | ^7.0",
10-
"xp-framework/http": "^10.0 | ^9.0 | ^8.0 | ^7.0",
11-
"xp-framework/zip": "^9.0 | ^8.0 | ^7.0",
12-
"xp-forge/json": "^4.0",
9+
"xp-framework/core": "^11.0 | ^10.14",
10+
"xp-framework/http": "^10.0 | ^9.0",
11+
"xp-framework/zip": "^10.0 | ^9.0",
12+
"xp-forge/json": "^5.0 | ^4.0",
1313
"php": ">=7.0.0"
1414
},
1515
"require-dev" : {
16-
"xp-framework/unittest": "^11.0 | ^10.0 | ^9.0 | ^8.0 |^7.0"
16+
"xp-framework/unittest": "^11.0 | ^10.0"
1717
},
1818
"bin": ["bin/xp.xp-forge.lambda"],
1919
"autoload" : {

src/main/php/xp/lambda/CreateRuntime.class.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public function __construct(string $version, Path $target, bool $rebuild= false)
1515
}
1616

1717
public function run(): int {
18-
$docker= $this->command();
1918

2019
// Build test and runtime containers when -b is given
2120
if ($this->rebuild) {
@@ -29,20 +28,20 @@ public function run(): int {
2928
$code= "echo ' => PHP ', PHP_VERSION, ' & Zend ', zend_version(), ' @ ', php_uname(), PHP_EOL, ' => ', implode(' ', get_loaded_extensions()), PHP_EOL;";
3029
Console::writeLine();
3130
Console::writeLine("[+] Running {$runtime}\e[34m");
32-
passthru("{$docker} run --rm {$runtime} /opt/php/bin/php -r \"{$code}\"", $result);
31+
$this->passthru(['run', '--rm', $runtime, '/opt/php/bin/php', '-r', $code]);
3332
Console::writeLine("\e[0m");
3433

3534
// Extract runtime
3635
$container= uniqid();
3736
$commands= [
38-
"{$docker} create --name {$container} {$runtime}",
39-
"{$docker} cp {$container}:/opt/php/runtime.zip {$this->target}",
40-
"{$docker} rm -f {$container}",
37+
['create', '--name', $container, $runtime],
38+
['cp', "{$container}:/opt/php/runtime.zip", $this->target],
39+
['rm', '-f', $container],
4140
];
4241
Console::writeLine('[+] Creating ', $this->target);
4342
foreach ($commands as $command) {
44-
Console::writeLinef("\e[34m => %s\e[0m", $command);
45-
exec($command, $out, $result);
43+
Console::writeLinef("\e[34m => %s\e[0m", implode(' ', $command));
44+
$result= $this->passthru($command);
4645
}
4746

4847
Console::writeLine();

src/main/php/xp/lambda/Docker.class.php

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,24 @@
11
<?php namespace xp\lambda;
22

33
use io\Path;
4-
use lang\CommandLine;
4+
use lang\{CommandLine, Process};
55

66
trait Docker {
77
private $command= null;
88

99
/**
10-
* Resolves a list of commands. Copy of `lang.Process::resolve()` from
11-
* XP 10.14 in order to be compatible with older XP versions.
10+
* Resolves a list of commands. Raises an exception if none of the passed
11+
* commands is found.
1212
*
13-
* @see https://github.com/xp-framework/core/pull/279
1413
* @param string[] $commands
1514
* @return string
1615
* @throws lang.IllegalStateException
1716
*/
1817
private function resolve($commands) {
19-
clearstatcache();
20-
21-
// PATHEXT is in form ".{EXT}[;.{EXT}[;...]]"
22-
$extensions= array_merge([''], explode(PATH_SEPARATOR, getenv('PATHEXT')));
23-
$paths= $paths= explode(PATH_SEPARATOR, getenv('PATH'));
18+
$c= CommandLine::forName(PHP_OS);
2419
foreach ($commands as $command) {
25-
26-
// If the command is in fully qualified form and refers to a file
27-
// that does not exist (e.g. "C:\DoesNotExist.exe", "\DoesNotExist.com"
28-
// or /usr/bin/doesnotexist), do not attempt to search for it.
29-
if ((DIRECTORY_SEPARATOR === $command[0]) || ((strncasecmp(PHP_OS, 'Win', 3) === 0) &&
30-
strlen($command) > 1 && (':' === $command[1] || '/' === $command[0])
31-
)) {
32-
foreach ($extensions as $ext) {
33-
$q= $command.$ext;
34-
if (file_exists($q) && !is_dir($q)) return realpath($q);
35-
}
36-
continue;
37-
}
38-
39-
// Check the PATH environment setting for possible locations of the
40-
// executable if its name is not a fully qualified path name.
41-
foreach ($paths as $path) {
42-
foreach ($extensions as $ext) {
43-
$q= $path.DIRECTORY_SEPARATOR.$command.$ext;
44-
if (file_exists($q) && !is_dir($q)) return realpath($q);
45-
}
20+
foreach ($c->resolve($command) as $resolved) {
21+
return $resolved;
4622
}
4723
}
4824

@@ -51,15 +27,27 @@ private function resolve($commands) {
5127

5228
/** Returns docker runtime */
5329
private function command() {
54-
return $this->command ?? $this->command= CommandLine::forName(PHP_OS)->compose($this->resolve(['docker', 'podman']));
30+
return $this->command ?? $this->command= $this->resolve(['docker', 'podman']);
31+
}
32+
33+
/** Runs docker with arguments */
34+
private function passthru($arguments) {
35+
return (new Process($this->command(), $arguments, null, null, [STDIN, STDOUT, STDERR]))->close();
5536
}
5637

5738
/** Returns a given docker image, building it if necessary */
5839
private function image(string $name, string $version, array $dependencies= [], bool $rebuild= false): array {
5940
$image= "lambda-xp-{$name}:{$version}";
6041
$images= [$name => $image];
6142

62-
$rebuild ? $out= [] : exec("{$this->command()} image ls -q {$image}", $out, $result);
43+
if ($rebuild) {
44+
$out= null;
45+
} else {
46+
$p= new Process($this->command(), ['image', 'ls', '-q', $image], null, null, [0 => STDIN, 2 => STDERR]);
47+
$out= $p->out->readLine();
48+
$p->close();
49+
}
50+
6351
if (empty($out)) {
6452

6553
// Support 3-digit `6.1.0` as well as 4-digit `6.1.0.1234` formats
@@ -71,8 +59,14 @@ private function image(string $name, string $version, array $dependencies= [], b
7159
}
7260

7361
// Build this
74-
$file= new Path(__DIR__, 'Dockerfile.'.$name);
75-
passthru("{$this->command()} build -t {$image} --build-arg php_version={$version} --build-arg xp_version={$runners} -f {$file} .", $result);
62+
$result= $this->passthru([
63+
'build',
64+
'-t', $image,
65+
'--build-arg', "php_version={$version}",
66+
'--build-arg', "xp_version={$runners}",
67+
'-f', new Path(__DIR__, 'Dockerfile.'.$name),
68+
'.'
69+
]);
7670
0 === $result || $images[$name]= null;
7771
}
7872

src/main/php/xp/lambda/TestLambda.class.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@ public function __construct(string $version, Path $path, string $handler, string
1616
}
1717

1818
public function run(): int {
19-
$docker= $this->command();
2019
$test= $this->image('test', $this->version, ['runtime' => []])['test'];
2120
if (null === $test) return 1;
2221

23-
$payload= '"'.str_replace('"', '\\"', $this->payload).'"';
24-
passthru("{$docker} run --rm -v {$this->path}:/var/task:ro {$test} {$this->handler} {$payload}", $result);
25-
return $result;
22+
return $this->passthru(['run', '--rm', '-v', "{$this->path}:/var/task:ro", $test, $this->handler, $this->payload]);
2623
}
2724
}

0 commit comments

Comments
 (0)