Skip to content

Commit

Permalink
Merge pull request #55 from brefphp/rework
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Feb 15, 2023
2 parents b353a8e + 14df077 commit 6b5d2c8
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 93 deletions.
17 changes: 17 additions & 0 deletions layers/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

if (getenv('BREF_AUTOLOAD_PATH')) {
require getenv('BREF_AUTOLOAD_PATH');
} else {
$appRoot = getenv('LAMBDA_TASK_ROOT');

require $appRoot . '/vendor/autoload.php';
}

$runtimeClass = getenv('RUNTIME_CLASS');

if (! class_exists($runtimeClass)) {
throw new RuntimeException("Bref is not installed in your application (could not find the class \"$runtimeClass\" in Composer dependencies). Did you run \"composer require bref/bref\"?");
}

$runtimeClass::run();
19 changes: 5 additions & 14 deletions layers/console/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,21 @@
ARG PHP_VERSION
ARG CPU_PREFIX

FROM alpine:3.14 as console-runtime

RUN apk add composer

RUN mkdir -p /opt/bref/console-runtime
WORKDIR /opt/bref/console-runtime
COPY --link composer.json composer.json
RUN composer install --ignore-platform-req=ext-posix --ignore-platform-req=ext-simplexml


# This image only contains what's going in the layer zip file,
# i.e. the files specific to the console layer.
# It's used to create the layer zip file.
FROM alpine:3.14 as console-zip

# Copy the /opt/bref/console-runtime directory
COPY --link --from=console-runtime /opt/bref /opt/bref

# Overwrite the "function" bootstrap file
COPY --link bref/bootstrap.php /opt/bref/bootstrap.php
# Overwrite the "function" bootstrap
COPY --link bootstrap.sh /opt/bootstrap
# Copy files to /var/runtime to support deploying as a Docker image
COPY --link bootstrap.sh /var/runtime/bootstrap


# This image is published to Docker Hub and replicates Lambda
FROM bref/${CPU_PREFIX}php-$PHP_VERSION as console

# Copy the console layer into /opt
COPY --link --from=console-zip /opt /opt
COPY --link --from=console-zip /var/runtime/bootstrap /var/runtime/bootstrap
24 changes: 24 additions & 0 deletions layers/console/bootstrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh

# Fail on error
set -e

export RUNTIME_CLASS="Bref\ConsoleRuntime\Main"

while true
do
if [ -z "${EXPERIMENTAL_AWS_LAMBDA_EXEC_WRAPPER}" ]; then
# Default behavior

# We redirect stderr to stdout so that everything
# written on the output ends up in Cloudwatch automatically
/opt/bin/php "/opt/bref/bootstrap.php" 2>&1
else
# A wrapper script is configured
# See https://docs.aws.amazon.com/lambda/latest/dg/runtimes-modify.html#runtime-wrapper
# NOTE: EXPERIMENTAL FEATURE, DO NOT USE!!!
# Note: If you do use it, open an issue or GitHub discussion or Slack thread
# and let us know why it's useful to you, we might turn it into an official feature
"${EXPERIMENTAL_AWS_LAMBDA_EXEC_WRAPPER}" /opt/bin/php "/opt/bref/bootstrap.php" 2>&1
fi
done
5 changes: 0 additions & 5 deletions layers/console/bref/bootstrap.php

This file was deleted.

12 changes: 0 additions & 12 deletions layers/console/composer.json

This file was deleted.

22 changes: 19 additions & 3 deletions layers/fpm/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
# Fail on error
set -e

# We redirect stderr to stdout so that everything
# written on the output ends up in Cloudwatch automatically
/opt/bin/php "/opt/bref/php-fpm-runtime/vendor/bref/php-fpm-runtime/src/bootstrap.php" 2>&1
export RUNTIME_CLASS="Bref\FpmRuntime\Main"

while true
do
if [ -z "${EXPERIMENTAL_AWS_LAMBDA_EXEC_WRAPPER}" ]; then
# Default behavior

# We redirect stderr to stdout so that everything
# written on the output ends up in Cloudwatch automatically
/opt/bin/php "/opt/bref/bootstrap.php" 2>&1
else
# A wrapper script is configured
# See https://docs.aws.amazon.com/lambda/latest/dg/runtimes-modify.html#runtime-wrapper
# NOTE: EXPERIMENTAL FEATURE, DO NOT USE!!!
# Note: If you do use it, open an issue or GitHub discussion or Slack thread
# and let us know why it's useful to you, we might turn it into an official feature
"${EXPERIMENTAL_AWS_LAMBDA_EXEC_WRAPPER}" /opt/bin/php "/opt/bref/bootstrap.php" 2>&1
fi
done
17 changes: 0 additions & 17 deletions layers/function/bootstrap.php

This file was deleted.

2 changes: 2 additions & 0 deletions layers/function/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Fail on error
set -e

export RUNTIME_CLASS="Bref\FunctionRuntime\Main"

while true
do
if [ -z "${EXPERIMENTAL_AWS_LAMBDA_EXEC_WRAPPER}" ]; then
Expand Down
17 changes: 3 additions & 14 deletions php-80/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,9 @@ FROM public.ecr.aws/lambda/provided:al2-${IMAGE_VERSION_SUFFIX} as isolation
# We selected the files in /bref-layer, now we copy them to /opt (the real directory for the Lambda layer)
COPY --link --from=build-environment /bref-layer /opt

COPY --link layers/bootstrap.php /opt/bref/bootstrap.php


FROM isolation as function

COPY --link layers/function/bref.ini /opt/bref/etc/php/conf.d/
Expand All @@ -457,8 +460,6 @@ COPY --link layers/function/bootstrap.sh /opt/bootstrap
COPY --link layers/function/bootstrap.sh /var/runtime/bootstrap
RUN chmod +x /opt/bootstrap && chmod +x /var/runtime/bootstrap

COPY --link layers/function/bootstrap.php /opt/bref/bootstrap.php


# Up until here the entire file has been designed as a top-down reading/execution.
# Everything necessary for the `function` layer has been installed, isolated and
Expand All @@ -471,16 +472,6 @@ RUN cp ${INSTALL_DIR}/sbin/php-fpm /bref-layer/bin/php-fpm
RUN php /bref/lib-copy/copy-dependencies.php /bref-layer/bin/php-fpm /bref-layer/lib


# Embed the https://github.com/brefphp/php-fpm-runtime in the layer
FROM composer:2 as fpm-runtime

RUN mkdir -p /bref-layer/bref/php-fpm-runtime
WORKDIR /bref-layer/bref/php-fpm-runtime

COPY --link layers/fpm/composer.json composer.json
RUN composer install --ignore-platform-req=ext-posix --ignore-platform-req=ext-simplexml


FROM isolation as fpm

COPY --link --from=fpm-extension /bref-layer /opt
Expand All @@ -493,5 +484,3 @@ COPY --link layers/fpm/bootstrap.sh /var/runtime/bootstrap
RUN chmod +x /opt/bootstrap && chmod +x /var/runtime/bootstrap

COPY --link layers/fpm/php-fpm.conf /opt/bref/etc/php-fpm.conf

COPY --link --from=fpm-runtime /bref-layer/bref/php-fpm-runtime /opt/bref/php-fpm-runtime
17 changes: 3 additions & 14 deletions php-81/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,9 @@ FROM public.ecr.aws/lambda/provided:al2-${IMAGE_VERSION_SUFFIX} as isolation
# We selected the files in /bref-layer, now we copy them to /opt (the real directory for the Lambda layer)
COPY --link --from=build-environment /bref-layer /opt

COPY --link layers/bootstrap.php /opt/bref/bootstrap.php


FROM isolation as function

COPY --link layers/function/bref.ini /opt/bref/etc/php/conf.d/
Expand All @@ -457,8 +460,6 @@ COPY --link layers/function/bootstrap.sh /opt/bootstrap
COPY --link layers/function/bootstrap.sh /var/runtime/bootstrap
RUN chmod +x /opt/bootstrap && chmod +x /var/runtime/bootstrap

COPY --link layers/function/bootstrap.php /opt/bref/bootstrap.php


# Up until here the entire file has been designed as a top-down reading/execution.
# Everything necessary for the `function` layer has been installed, isolated and
Expand All @@ -471,16 +472,6 @@ RUN cp ${INSTALL_DIR}/sbin/php-fpm /bref-layer/bin/php-fpm
RUN php /bref/lib-copy/copy-dependencies.php /bref-layer/bin/php-fpm /bref-layer/lib


# Embed the https://github.com/brefphp/php-fpm-runtime in the layer
FROM composer:2 as fpm-runtime

RUN mkdir -p /bref-layer/bref/php-fpm-runtime
WORKDIR /bref-layer/bref/php-fpm-runtime

COPY --link layers/fpm/composer.json composer.json
RUN composer install --ignore-platform-req=ext-posix --ignore-platform-req=ext-simplexml


FROM isolation as fpm

COPY --link --from=fpm-extension /bref-layer /opt
Expand All @@ -493,5 +484,3 @@ COPY --link layers/fpm/bootstrap.sh /var/runtime/bootstrap
RUN chmod +x /opt/bootstrap && chmod +x /var/runtime/bootstrap

COPY --link layers/fpm/php-fpm.conf /opt/bref/etc/php-fpm.conf

COPY --link --from=fpm-runtime /bref-layer/bref/php-fpm-runtime /opt/bref/php-fpm-runtime
17 changes: 3 additions & 14 deletions php-82/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,9 @@ FROM public.ecr.aws/lambda/provided:al2-${IMAGE_VERSION_SUFFIX} as isolation
# We selected the files in /bref-layer, now we copy them to /opt (the real directory for the Lambda layer)
COPY --link --from=build-environment /bref-layer /opt

COPY --link layers/bootstrap.php /opt/bref/bootstrap.php


FROM isolation as function

COPY --link layers/function/bref.ini /opt/bref/etc/php/conf.d/
Expand All @@ -457,8 +460,6 @@ COPY --link layers/function/bootstrap.sh /opt/bootstrap
COPY --link layers/function/bootstrap.sh /var/runtime/bootstrap
RUN chmod +x /opt/bootstrap && chmod +x /var/runtime/bootstrap

COPY --link layers/function/bootstrap.php /opt/bref/bootstrap.php


# Up until here the entire file has been designed as a top-down reading/execution.
# Everything necessary for the `function` layer has been installed, isolated and
Expand All @@ -471,16 +472,6 @@ RUN cp ${INSTALL_DIR}/sbin/php-fpm /bref-layer/bin/php-fpm
RUN php /bref/lib-copy/copy-dependencies.php /bref-layer/bin/php-fpm /bref-layer/lib


# Embed the https://github.com/brefphp/php-fpm-runtime in the layer
FROM composer:2 as fpm-runtime

RUN mkdir -p /bref-layer/bref/php-fpm-runtime
WORKDIR /bref-layer/bref/php-fpm-runtime

COPY --link layers/fpm/composer.json composer.json
RUN composer install --ignore-platform-req=ext-posix --ignore-platform-req=ext-simplexml


FROM isolation as fpm

COPY --link --from=fpm-extension /bref-layer /opt
Expand All @@ -493,5 +484,3 @@ COPY --link layers/fpm/bootstrap.sh /var/runtime/bootstrap
RUN chmod +x /opt/bootstrap && chmod +x /var/runtime/bootstrap

COPY --link layers/fpm/php-fpm.conf /opt/bref/etc/php-fpm.conf

COPY --link --from=fpm-runtime /bref-layer/bref/php-fpm-runtime /opt/bref/php-fpm-runtime

0 comments on commit 6b5d2c8

Please sign in to comment.