build(example): require the runtime from Packagist, and deploy it locally like ScalewayUnverified
The example now requires jdecool/scaleway-runtime dev-main without any repository, as Scaleway installs the dependencies from the shipped lock. "composer link-runtime" adds the path repository on the fly for local work and CI; the committed lock is the linked one until the first publication. e2e/deploy-locally.sh packages a copy, installs the zip with composer install --no-dev without the function's variables and serves it in prod, the checkout standing in for Packagist; CI runs the scenarios against it and against the dev Emulator. The post-install-cmd warms the prod cache. Terraform recreates the CRON triggers when the function is replaced, and passes an optional host as SCALEWAY_HOST.
c20f007 parent: 74ba5e0 modified
.github/workflows/ci.yml +19 -15 | @@ -46,6 +46,8 @@ jobs: | ||
| 46 | 46 | example: |
| 47 | 47 | name: Example application through the Emulator |
| 48 | 48 | runs-on: ubuntu-latest |
| 49 | + env: | |
| 50 | + SCALEWAY_CRON_SECRET: ci-cron-secret | |
| 49 | 51 | steps: |
| 50 | 52 | - uses: actions/checkout@v7 |
| 51 | 53 | |
| @@ -55,25 +57,27 @@ jobs: | ||
| 55 | 57 | coverage: none |
| 56 | 58 | extensions: zip |
| 57 | 59 | |
| 58 | - - uses: ramsey/composer-install@v4 | |
| 59 | - with: | |
| 60 | - working-directory: example | |
| 61 | - | |
| 62 | - - name: Package | |
| 60 | + - name: Install with the runtime of this checkout | |
| 63 | 61 | working-directory: example |
| 64 | - run: vendor/bin/scaleway-package | |
| 62 | + run: composer link-runtime | |
| 65 | 63 | |
| 66 | - # Serves the zip as Scaleway would run it: extracted elsewhere, read-only, in prod. | |
| 67 | - - name: Serve the packaged function | |
| 64 | + - name: Serve in dev | |
| 68 | 65 | run: | |
| 69 | - unzip -q example/function.zip -d "$RUNNER_TEMP/function" | |
| 70 | - chmod -R a-w "$RUNNER_TEMP/function" | |
| 71 | - APP_ENV=prod APP_SECRET=ci SCALEWAY_CRON_SECRET=ci-cron-secret php "$RUNNER_TEMP/function/vendor/bin/scaleway-serve" 127.0.0.1:8000 > "$RUNNER_TEMP/serve.log" 2>&1 & | |
| 66 | + (cd example && vendor/bin/scaleway-serve 127.0.0.1:8000 > "$RUNNER_TEMP/serve.log" 2>&1 &) | |
| 72 | 67 | timeout 10 bash -c 'until curl -sf http://127.0.0.1:8000/hello > /dev/null; do sleep 0.2; done' |
| 73 | 68 | |
| 74 | - - name: End-to-end scenarios | |
| 75 | - run: SCALEWAY_CRON_SECRET=ci-cron-secret example/e2e/run.sh http://127.0.0.1:8000 | |
| 69 | + - name: End-to-end scenarios in dev | |
| 70 | + run: example/e2e/run.sh http://127.0.0.1:8000 | |
| 71 | + | |
| 72 | + # Packages the example, installs the zip like Scaleway at deploy time and serves it in prod. | |
| 73 | + - name: Deploy locally | |
| 74 | + run: | | |
| 75 | + APP_SECRET=ci example/e2e/deploy-locally.sh 127.0.0.1:8001 > "$RUNNER_TEMP/deploy.log" 2>&1 & | |
| 76 | + timeout 300 bash -c 'until curl -sf http://127.0.0.1:8001/hello > /dev/null; do sleep 1; done' | |
| 77 | + | |
| 78 | + - name: End-to-end scenarios on the deployed zip | |
| 79 | + run: example/e2e/run.sh http://127.0.0.1:8001 | |
| 76 | 80 | |
| 77 | - - name: Emulator log | |
| 81 | + - name: Emulator logs | |
| 78 | 82 | if: always() |
| 79 | - run: cat "$RUNNER_TEMP/serve.log" | |
| 83 | + run: cat "$RUNNER_TEMP/serve.log" "$RUNNER_TEMP/deploy.log" | |
| @@ -46,6 +46,8 @@ jobs: | |||
| 46 | example: | 46 | example: |
| 47 | name: Example application through the Emulator | 47 | name: Example application through the Emulator |
| 48 | runs-on: ubuntu-latest | 48 | runs-on: ubuntu-latest |
| 49 | + env: | ||
| 50 | + SCALEWAY_CRON_SECRET: ci-cron-secret | ||
| 49 | steps: | 51 | steps: |
| 50 | - uses: actions/checkout@v7 | 52 | - uses: actions/checkout@v7 |
| 51 | 53 | ||
| @@ -55,25 +57,27 @@ jobs: | |||
| 55 | coverage: none | 57 | coverage: none |
| 56 | extensions: zip | 58 | extensions: zip |
| 57 | 59 | ||
| 58 | - - uses: ramsey/composer-install@v4 | 60 | + - name: Install with the runtime of this checkout |
| 59 | - with: | ||
| 60 | - working-directory: example | ||
| 61 | - | ||
| 62 | - - name: Package | ||
| 63 | working-directory: example | 61 | working-directory: example |
| 64 | - run: vendor/bin/scaleway-package | 62 | + run: composer link-runtime |
| 65 | 63 | ||
| 66 | - # Serves the zip as Scaleway would run it: extracted elsewhere, read-only, in prod. | 64 | + - name: Serve in dev |
| 67 | - - name: Serve the packaged function | ||
| 68 | run: | | 65 | run: | |
| 69 | - unzip -q example/function.zip -d "$RUNNER_TEMP/function" | 66 | + (cd example && vendor/bin/scaleway-serve 127.0.0.1:8000 > "$RUNNER_TEMP/serve.log" 2>&1 &) |
| 70 | - chmod -R a-w "$RUNNER_TEMP/function" | ||
| 71 | - APP_ENV=prod APP_SECRET=ci SCALEWAY_CRON_SECRET=ci-cron-secret php "$RUNNER_TEMP/function/vendor/bin/scaleway-serve" 127.0.0.1:8000 > "$RUNNER_TEMP/serve.log" 2>&1 & | ||
| 72 | timeout 10 bash -c 'until curl -sf http://127.0.0.1:8000/hello > /dev/null; do sleep 0.2; done' | 67 | timeout 10 bash -c 'until curl -sf http://127.0.0.1:8000/hello > /dev/null; do sleep 0.2; done' |
| 73 | 68 | ||
| 74 | - - name: End-to-end scenarios | 69 | + - name: End-to-end scenarios in dev |
| 75 | - run: SCALEWAY_CRON_SECRET=ci-cron-secret example/e2e/run.sh http://127.0.0.1:8000 | 70 | + run: example/e2e/run.sh http://127.0.0.1:8000 |
| 71 | + | ||
| 72 | + # Packages the example, installs the zip like Scaleway at deploy time and serves it in prod. | ||
| 73 | + - name: Deploy locally | ||
| 74 | + run: | | ||
| 75 | + APP_SECRET=ci example/e2e/deploy-locally.sh 127.0.0.1:8001 > "$RUNNER_TEMP/deploy.log" 2>&1 & | ||
| 76 | + timeout 300 bash -c 'until curl -sf http://127.0.0.1:8001/hello > /dev/null; do sleep 1; done' | ||
| 77 | + | ||
| 78 | + - name: End-to-end scenarios on the deployed zip | ||
| 79 | + run: example/e2e/run.sh http://127.0.0.1:8001 | ||
| 76 | 80 | ||
| 77 | - - name: Emulator log | 81 | + - name: Emulator logs |
| 78 | if: always() | 82 | if: always() |
| 79 | - run: cat "$RUNNER_TEMP/serve.log" | 83 | + run: cat "$RUNNER_TEMP/serve.log" "$RUNNER_TEMP/deploy.log" |
modified
example/README.md +24 -11 | @@ -1,6 +1,6 @@ | ||
| 1 | 1 | # Example application |
| 2 | 2 | |
| 3 | -A minimal Symfony application running on Scaleway Serverless Functions through `jdecool/scaleway-runtime`, which it installs from the parent directory with a Composer `path` repository. | |
| 3 | +A minimal Symfony application running on Scaleway Serverless Functions through `jdecool/scaleway-runtime`, which it requires from Packagist. | |
| 4 | 4 | |
| 5 | 5 | | Route | What it shows | |
| 6 | 6 | | --- | --- | |
| @@ -12,16 +12,18 @@ A minimal Symfony application running on Scaleway Serverless Functions through ` | ||
| 12 | 12 | | `GET /counter` | A per-process invocation counter: it keeps growing while a Warm Process serves the Invocations | |
| 13 | 13 | | `POST /` | Where CRON Invocations land, with the trigger's arguments as JSON body | |
| 14 | 14 | |
| 15 | +Its `post-install-cmd` warms the prod cache when Scaleway installs the dependencies at deploy time, as the warmup needs no secret; without it, the first Invocation of each Warm Process builds the cache. | |
| 16 | + | |
| 15 | 17 | The `app:greet` console command (`src/Command/GreetCommand.php`) runs as a Command Invocation: a `POST /` whose JSON body is `{"command": "app:greet", "args": {"name": "...", "--yell": true}, "secret": "..."}`, the secret matching the function's `SCALEWAY_CRON_SECRET`. |
| 16 | 18 | Its `--fail` option shows how a failed command answers: a 500 with the command output. |
| 17 | -Without the secret, the same body is a plain HTTP body and lands on `POST /`. | |
| 19 | +Over HTTP, even with the secret, the same body is a plain HTTP body and lands on `POST /`. | |
| 18 | 20 | `bin/console` works as a Front Controller too, for a function that only runs commands: `APP_RUNTIME_OPTIONS='{"front_controller":"bin/console"}'`. |
| 19 | 21 | |
| 20 | 22 | `public/hello.php` is a second, function-like Front Controller: a closure taking the `Request` and returning a `Response`, called for every Invocation without any Kernel. |
| 21 | 23 | Select it with the `front_controller` runtime option, for instance `APP_RUNTIME_OPTIONS='{"front_controller":"public/hello.php"}' vendor/bin/scaleway-serve`, then `curl 'http://127.0.0.1:8000/?name=Scaleway'`. |
| 22 | 24 | |
| 23 | 25 | A Front Controller may also return a PSR-15 `RequestHandlerInterface`, built once and reused like the Kernel, once the application requires `symfony/psr-http-message-bridge` and `nyholm/psr7`. |
| 24 | -Its PSR-7 request sees the client, scheme and host resolved from the platform's `X-Forwarded-*` headers, in `getUri()` as in the `REMOTE_ADDR`, `HTTPS` and `HTTP_HOST` server params. | |
| 26 | +Its PSR-7 request sees the client, scheme and host the runtime resolved, in `getUri()` as in the `REMOTE_ADDR`, `HTTPS` and `HTTP_HOST` server params. | |
| 25 | 27 | |
| 26 | 28 | A Front Controller whose closure returns another `Closure` talks to Scaleway without any translation: that raw callable, built once and reused like the Kernel, receives every Event and context as they are, CRON Invocations included, and returns the Function Response itself. |
| 27 | 29 | |
| @@ -35,28 +37,38 @@ return static fn (): Closure => static fn (array $event, mixed $context): array | ||
| 35 | 37 | |
| 36 | 38 | ## Run it locally |
| 37 | 39 | |
| 40 | +The package is not on Packagist yet, and the example works on the runtime of this checkout anyway: `composer link-runtime` installs it from the parent directory, through a `path` repository it adds to `composer.json`. | |
| 41 | +Never commit that repository: `scaleway-package` refuses it, as Scaleway cannot resolve it. | |
| 42 | +Until the first publication, the committed `composer.lock` is the one `link-runtime` writes; once the package is on Packagist, it is regenerated without the repository (`composer update jdecool/scaleway-runtime`) and committed, which makes the example deployable as is. | |
| 43 | + | |
| 38 | 44 | ```sh |
| 39 | 45 | cd example |
| 40 | -composer install | |
| 46 | +composer link-runtime | |
| 41 | 47 | export SCALEWAY_CRON_SECRET=local-secret # lets the Emulator and the scenarios run Command Invocations |
| 42 | 48 | vendor/bin/scaleway-serve # the Emulator, on http://127.0.0.1:8000 |
| 43 | 49 | e2e/run.sh http://127.0.0.1:8000 # in another terminal, with the same SCALEWAY_CRON_SECRET |
| 44 | 50 | ``` |
| 45 | 51 | |
| 46 | -To run exactly what gets deployed, serve the packaged zip from a read-only copy: | |
| 52 | +A CRON trigger is simulated with a `POST` on the Emulator's `/_scaleway/cron`, whose body is the trigger's arguments: | |
| 47 | 53 | |
| 48 | 54 | ```sh |
| 49 | -vendor/bin/scaleway-package | |
| 50 | -unzip -q function.zip -d /tmp/function && chmod -R a-w /tmp/function | |
| 51 | -APP_ENV=prod APP_SECRET=local SCALEWAY_CRON_SECRET=local-secret php /tmp/function/vendor/bin/scaleway-serve | |
| 55 | +curl -X POST http://127.0.0.1:8000/_scaleway/cron --data '{"command": "app:greet", "args": {"name": "CRON"}, "secret": "local-secret"}' | |
| 56 | +``` | |
| 57 | + | |
| 58 | +To run what Scaleway deploys, `e2e/deploy-locally.sh` packages a copy of the application, installs the zip with `composer install --no-dev` and without the function's variables, as Scaleway does, and serves it in prod, all in a temporary directory: | |
| 59 | + | |
| 60 | +```sh | |
| 61 | +APP_SECRET=local SCALEWAY_CRON_SECRET=local-secret e2e/deploy-locally.sh 127.0.0.1:8001 | |
| 62 | +e2e/run.sh http://127.0.0.1:8001 | |
| 52 | 63 | ``` |
| 53 | 64 | |
| 65 | +It publishes the runtime of this checkout to that install in place of Packagist, through a Composer repository configured outside `composer.json`. | |
| 66 | + | |
| 54 | 67 | ## Deploy it |
| 55 | 68 | |
| 69 | +Deploying needs the package on Packagist and the lock regenerated against it, see above: until then, `scaleway-package` refuses the committed lock. | |
| 56 | 70 | The Terraform code is unvalidated: it was written against the `scaleway/scaleway` provider v2.92 documentation and never applied. |
| 57 | -Scaleway documents that it installs the dependencies of a shipped `composer.json` at deploy time. | |
| 58 | -If it does so over the shipped `vendor/`, deploying this example likely fails: its `composer.lock` installs the runtime from `..`, which is not in the zip. | |
| 59 | -The [probe](../probe) settles whether and how Composer runs. | |
| 71 | +Set `host` to the function's domain (or a custom domain) for absolute URLs to be right; it cannot default to the function's own `domain_name`, so apply once, then again with `-var host=...`. | |
| 60 | 72 | |
| 61 | 73 | ```sh |
| 62 | 74 | cd example |
| @@ -70,3 +82,4 @@ terraform -chdir=terraform destroy | ||
| 70 | 82 | ``` |
| 71 | 83 | |
| 72 | 84 | Rerun `scaleway-package` and `terraform apply` to deploy a new version. |
| 85 | +Against a deployment, `e2e/run.sh` skips the CRON scenarios, as only the platform triggers CRON Invocations. | |
| @@ -1,6 +1,6 @@ | |||
| 1 | # Example application | 1 | # Example application |
| 2 | 2 | ||
| 3 | -A minimal Symfony application running on Scaleway Serverless Functions through `jdecool/scaleway-runtime`, which it installs from the parent directory with a Composer `path` repository. | 3 | +A minimal Symfony application running on Scaleway Serverless Functions through `jdecool/scaleway-runtime`, which it requires from Packagist. |
| 4 | 4 | ||
| 5 | | Route | What it shows | | 5 | | Route | What it shows | |
| 6 | | --- | --- | | 6 | | --- | --- | |
| @@ -12,16 +12,18 @@ A minimal Symfony application running on Scaleway Serverless Functions through ` | |||
| 12 | | `GET /counter` | A per-process invocation counter: it keeps growing while a Warm Process serves the Invocations | | 12 | | `GET /counter` | A per-process invocation counter: it keeps growing while a Warm Process serves the Invocations | |
| 13 | | `POST /` | Where CRON Invocations land, with the trigger's arguments as JSON body | | 13 | | `POST /` | Where CRON Invocations land, with the trigger's arguments as JSON body | |
| 14 | 14 | ||
| 15 | +Its `post-install-cmd` warms the prod cache when Scaleway installs the dependencies at deploy time, as the warmup needs no secret; without it, the first Invocation of each Warm Process builds the cache. | ||
| 16 | + | ||
| 15 | The `app:greet` console command (`src/Command/GreetCommand.php`) runs as a Command Invocation: a `POST /` whose JSON body is `{"command": "app:greet", "args": {"name": "...", "--yell": true}, "secret": "..."}`, the secret matching the function's `SCALEWAY_CRON_SECRET`. | 17 | The `app:greet` console command (`src/Command/GreetCommand.php`) runs as a Command Invocation: a `POST /` whose JSON body is `{"command": "app:greet", "args": {"name": "...", "--yell": true}, "secret": "..."}`, the secret matching the function's `SCALEWAY_CRON_SECRET`. |
| 16 | Its `--fail` option shows how a failed command answers: a 500 with the command output. | 18 | Its `--fail` option shows how a failed command answers: a 500 with the command output. |
| 17 | -Without the secret, the same body is a plain HTTP body and lands on `POST /`. | 19 | +Over HTTP, even with the secret, the same body is a plain HTTP body and lands on `POST /`. |
| 18 | `bin/console` works as a Front Controller too, for a function that only runs commands: `APP_RUNTIME_OPTIONS='{"front_controller":"bin/console"}'`. | 20 | `bin/console` works as a Front Controller too, for a function that only runs commands: `APP_RUNTIME_OPTIONS='{"front_controller":"bin/console"}'`. |
| 19 | 21 | ||
| 20 | `public/hello.php` is a second, function-like Front Controller: a closure taking the `Request` and returning a `Response`, called for every Invocation without any Kernel. | 22 | `public/hello.php` is a second, function-like Front Controller: a closure taking the `Request` and returning a `Response`, called for every Invocation without any Kernel. |
| 21 | Select it with the `front_controller` runtime option, for instance `APP_RUNTIME_OPTIONS='{"front_controller":"public/hello.php"}' vendor/bin/scaleway-serve`, then `curl 'http://127.0.0.1:8000/?name=Scaleway'`. | 23 | Select it with the `front_controller` runtime option, for instance `APP_RUNTIME_OPTIONS='{"front_controller":"public/hello.php"}' vendor/bin/scaleway-serve`, then `curl 'http://127.0.0.1:8000/?name=Scaleway'`. |
| 22 | 24 | ||
| 23 | A Front Controller may also return a PSR-15 `RequestHandlerInterface`, built once and reused like the Kernel, once the application requires `symfony/psr-http-message-bridge` and `nyholm/psr7`. | 25 | A Front Controller may also return a PSR-15 `RequestHandlerInterface`, built once and reused like the Kernel, once the application requires `symfony/psr-http-message-bridge` and `nyholm/psr7`. |
| 24 | -Its PSR-7 request sees the client, scheme and host resolved from the platform's `X-Forwarded-*` headers, in `getUri()` as in the `REMOTE_ADDR`, `HTTPS` and `HTTP_HOST` server params. | 26 | +Its PSR-7 request sees the client, scheme and host the runtime resolved, in `getUri()` as in the `REMOTE_ADDR`, `HTTPS` and `HTTP_HOST` server params. |
| 25 | 27 | ||
| 26 | A Front Controller whose closure returns another `Closure` talks to Scaleway without any translation: that raw callable, built once and reused like the Kernel, receives every Event and context as they are, CRON Invocations included, and returns the Function Response itself. | 28 | A Front Controller whose closure returns another `Closure` talks to Scaleway without any translation: that raw callable, built once and reused like the Kernel, receives every Event and context as they are, CRON Invocations included, and returns the Function Response itself. |
| 27 | 29 | ||
| @@ -35,28 +37,38 @@ return static fn (): Closure => static fn (array $event, mixed $context): array | |||
| 35 | 37 | ||
| 36 | ## Run it locally | 38 | ## Run it locally |
| 37 | 39 | ||
| 40 | +The package is not on Packagist yet, and the example works on the runtime of this checkout anyway: `composer link-runtime` installs it from the parent directory, through a `path` repository it adds to `composer.json`. | ||
| 41 | +Never commit that repository: `scaleway-package` refuses it, as Scaleway cannot resolve it. | ||
| 42 | +Until the first publication, the committed `composer.lock` is the one `link-runtime` writes; once the package is on Packagist, it is regenerated without the repository (`composer update jdecool/scaleway-runtime`) and committed, which makes the example deployable as is. | ||
| 43 | + | ||
| 38 | ```sh | 44 | ```sh |
| 39 | cd example | 45 | cd example |
| 40 | -composer install | 46 | +composer link-runtime |
| 41 | export SCALEWAY_CRON_SECRET=local-secret # lets the Emulator and the scenarios run Command Invocations | 47 | export SCALEWAY_CRON_SECRET=local-secret # lets the Emulator and the scenarios run Command Invocations |
| 42 | vendor/bin/scaleway-serve # the Emulator, on http://127.0.0.1:8000 | 48 | vendor/bin/scaleway-serve # the Emulator, on http://127.0.0.1:8000 |
| 43 | e2e/run.sh http://127.0.0.1:8000 # in another terminal, with the same SCALEWAY_CRON_SECRET | 49 | e2e/run.sh http://127.0.0.1:8000 # in another terminal, with the same SCALEWAY_CRON_SECRET |
| 44 | ``` | 50 | ``` |
| 45 | 51 | ||
| 46 | -To run exactly what gets deployed, serve the packaged zip from a read-only copy: | 52 | +A CRON trigger is simulated with a `POST` on the Emulator's `/_scaleway/cron`, whose body is the trigger's arguments: |
| 47 | 53 | ||
| 48 | ```sh | 54 | ```sh |
| 49 | -vendor/bin/scaleway-package | 55 | +curl -X POST http://127.0.0.1:8000/_scaleway/cron --data '{"command": "app:greet", "args": {"name": "CRON"}, "secret": "local-secret"}' |
| 50 | -unzip -q function.zip -d /tmp/function && chmod -R a-w /tmp/function | 56 | +``` |
| 51 | -APP_ENV=prod APP_SECRET=local SCALEWAY_CRON_SECRET=local-secret php /tmp/function/vendor/bin/scaleway-serve | 57 | + |
| 58 | +To run what Scaleway deploys, `e2e/deploy-locally.sh` packages a copy of the application, installs the zip with `composer install --no-dev` and without the function's variables, as Scaleway does, and serves it in prod, all in a temporary directory: | ||
| 59 | + | ||
| 60 | +```sh | ||
| 61 | +APP_SECRET=local SCALEWAY_CRON_SECRET=local-secret e2e/deploy-locally.sh 127.0.0.1:8001 | ||
| 62 | +e2e/run.sh http://127.0.0.1:8001 | ||
| 52 | ``` | 63 | ``` |
| 53 | 64 | ||
| 65 | +It publishes the runtime of this checkout to that install in place of Packagist, through a Composer repository configured outside `composer.json`. | ||
| 66 | + | ||
| 54 | ## Deploy it | 67 | ## Deploy it |
| 55 | 68 | ||
| 69 | +Deploying needs the package on Packagist and the lock regenerated against it, see above: until then, `scaleway-package` refuses the committed lock. | ||
| 56 | The Terraform code is unvalidated: it was written against the `scaleway/scaleway` provider v2.92 documentation and never applied. | 70 | The Terraform code is unvalidated: it was written against the `scaleway/scaleway` provider v2.92 documentation and never applied. |
| 57 | -Scaleway documents that it installs the dependencies of a shipped `composer.json` at deploy time. | 71 | +Set `host` to the function's domain (or a custom domain) for absolute URLs to be right; it cannot default to the function's own `domain_name`, so apply once, then again with `-var host=...`. |
| 58 | -If it does so over the shipped `vendor/`, deploying this example likely fails: its `composer.lock` installs the runtime from `..`, which is not in the zip. | ||
| 59 | -The [probe](../probe) settles whether and how Composer runs. | ||
| 60 | 72 | ||
| 61 | ```sh | 73 | ```sh |
| 62 | cd example | 74 | cd example |
| @@ -70,3 +82,4 @@ terraform -chdir=terraform destroy | |||
| 70 | ``` | 82 | ``` |
| 71 | 83 | ||
| 72 | Rerun `scaleway-package` and `terraform apply` to deploy a new version. | 84 | Rerun `scaleway-package` and `terraform apply` to deploy a new version. |
| 85 | +Against a deployment, `e2e/run.sh` skips the CRON scenarios, as only the platform triggers CRON Invocations. | ||
modified
example/composer.json +13 -7 | @@ -5,19 +5,13 @@ | ||
| 5 | 5 | "license": "MIT", |
| 6 | 6 | "require": { |
| 7 | 7 | "php": ">=8.4", |
| 8 | - "jdecool/scaleway-runtime": "*@dev", | |
| 8 | + "jdecool/scaleway-runtime": "dev-main", | |
| 9 | 9 | "symfony/console": "^8.1", |
| 10 | 10 | "symfony/dotenv": "^8.1", |
| 11 | 11 | "symfony/framework-bundle": "^8.1", |
| 12 | 12 | "symfony/runtime": "^8.1", |
| 13 | 13 | "symfony/yaml": "^8.1" |
| 14 | 14 | }, |
| 15 | - "repositories": [ | |
| 16 | - { | |
| 17 | - "type": "path", | |
| 18 | - "url": ".." | |
| 19 | - } | |
| 20 | - ], | |
| 21 | 15 | "autoload": { |
| 22 | 16 | "psr-4": { |
| 23 | 17 | "App\\": "src/" |
| @@ -28,5 +22,17 @@ | ||
| 28 | 22 | "symfony/runtime": true |
| 29 | 23 | }, |
| 30 | 24 | "sort-packages": true |
| 25 | + }, | |
| 26 | + "scripts": { | |
| 27 | + "post-install-cmd": [ | |
| 28 | + "@php bin/console cache:warmup --env=prod" | |
| 29 | + ], | |
| 30 | + "link-runtime": [ | |
| 31 | + "@composer config repositories.local path ..", | |
| 32 | + "@composer update jdecool/scaleway-runtime" | |
| 33 | + ] | |
| 34 | + }, | |
| 35 | + "scripts-descriptions": { | |
| 36 | + "link-runtime": "Installs jdecool/scaleway-runtime from the parent directory: never commit the repository it adds" | |
| 31 | 37 | } |
| 32 | 38 | } |
| @@ -5,19 +5,13 @@ | |||
| 5 | "license": "MIT", | 5 | "license": "MIT", |
| 6 | "require": { | 6 | "require": { |
| 7 | "php": ">=8.4", | 7 | "php": ">=8.4", |
| 8 | - "jdecool/scaleway-runtime": "*@dev", | 8 | + "jdecool/scaleway-runtime": "dev-main", |
| 9 | "symfony/console": "^8.1", | 9 | "symfony/console": "^8.1", |
| 10 | "symfony/dotenv": "^8.1", | 10 | "symfony/dotenv": "^8.1", |
| 11 | "symfony/framework-bundle": "^8.1", | 11 | "symfony/framework-bundle": "^8.1", |
| 12 | "symfony/runtime": "^8.1", | 12 | "symfony/runtime": "^8.1", |
| 13 | "symfony/yaml": "^8.1" | 13 | "symfony/yaml": "^8.1" |
| 14 | }, | 14 | }, |
| 15 | - "repositories": [ | ||
| 16 | - { | ||
| 17 | - "type": "path", | ||
| 18 | - "url": ".." | ||
| 19 | - } | ||
| 20 | - ], | ||
| 21 | "autoload": { | 15 | "autoload": { |
| 22 | "psr-4": { | 16 | "psr-4": { |
| 23 | "App\\": "src/" | 17 | "App\\": "src/" |
| @@ -28,5 +22,17 @@ | |||
| 28 | "symfony/runtime": true | 22 | "symfony/runtime": true |
| 29 | }, | 23 | }, |
| 30 | "sort-packages": true | 24 | "sort-packages": true |
| 25 | + }, | ||
| 26 | + "scripts": { | ||
| 27 | + "post-install-cmd": [ | ||
| 28 | + "@php bin/console cache:warmup --env=prod" | ||
| 29 | + ], | ||
| 30 | + "link-runtime": [ | ||
| 31 | + "@composer config repositories.local path ..", | ||
| 32 | + "@composer update jdecool/scaleway-runtime" | ||
| 33 | + ] | ||
| 34 | + }, | ||
| 35 | + "scripts-descriptions": { | ||
| 36 | + "link-runtime": "Installs jdecool/scaleway-runtime from the parent directory: never commit the repository it adds" | ||
| 31 | } | 37 | } |
| 32 | } | 38 | } |
modified
example/composer.lock +12 -4 | @@ -4,7 +4,7 @@ | ||
| 4 | 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", |
| 5 | 5 | "This file is @generated automatically" |
| 6 | 6 | ], |
| 7 | - "content-hash": "2d500a2e01d80ef89cbe545467a32d11", | |
| 7 | + "content-hash": "86fdec512442a38f766c855ef64b0bc1", | |
| 8 | 8 | "packages": [ |
| 9 | 9 | { |
| 10 | 10 | "name": "jdecool/scaleway-runtime", |
| @@ -12,7 +12,7 @@ | ||
| 12 | 12 | "dist": { |
| 13 | 13 | "type": "path", |
| 14 | 14 | "url": "..", |
| 15 | - "reference": "c1cabead8c1f0269ad37d043feeb6d5754a059d3" | |
| 15 | + "reference": "74ba5e04d1f150a20d38a91c1bfe52163847656f" | |
| 16 | 16 | }, |
| 17 | 17 | "require": { |
| 18 | 18 | "php": ">=8.4", |
| @@ -23,13 +23,21 @@ | ||
| 23 | 23 | }, |
| 24 | 24 | "require-dev": { |
| 25 | 25 | "friendsofphp/php-cs-fixer": "^3.95", |
| 26 | + "nyholm/psr7": "^1.8", | |
| 26 | 27 | "phpstan/phpstan": "^2.3", |
| 27 | 28 | "phpstan/phpstan-phpunit": "^2.1", |
| 28 | 29 | "phpunit/phpunit": "^13.4", |
| 29 | - "symfony/framework-bundle": "^7.4 || ^8.0" | |
| 30 | + "psr/http-server-handler": "^1.0", | |
| 31 | + "symfony/console": "^7.4 || ^8.0", | |
| 32 | + "symfony/dotenv": "^7.4 || ^8.0", | |
| 33 | + "symfony/framework-bundle": "^7.4 || ^8.0", | |
| 34 | + "symfony/psr-http-message-bridge": "^7.4 || ^8.0" | |
| 30 | 35 | }, |
| 31 | 36 | "suggest": { |
| 32 | - "ext-zip": "To build the deployment zip with vendor/bin/scaleway-package" | |
| 37 | + "ext-zip": "To build the deployment zip with vendor/bin/scaleway-package", | |
| 38 | + "nyholm/psr7": "To run Front Controllers returning a PSR-15 request handler", | |
| 39 | + "symfony/console": "To run Command Invocations and Front Controllers returning a Console Application or a Command", | |
| 40 | + "symfony/psr-http-message-bridge": "To run Front Controllers returning a PSR-15 request handler" | |
| 33 | 41 | }, |
| 34 | 42 | "bin": [ |
| 35 | 43 | "bin/scaleway-init", |
| @@ -4,7 +4,7 @@ | |||
| 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", | 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", |
| 5 | "This file is @generated automatically" | 5 | "This file is @generated automatically" |
| 6 | ], | 6 | ], |
| 7 | - "content-hash": "2d500a2e01d80ef89cbe545467a32d11", | 7 | + "content-hash": "86fdec512442a38f766c855ef64b0bc1", |
| 8 | "packages": [ | 8 | "packages": [ |
| 9 | { | 9 | { |
| 10 | "name": "jdecool/scaleway-runtime", | 10 | "name": "jdecool/scaleway-runtime", |
| @@ -12,7 +12,7 @@ | |||
| 12 | "dist": { | 12 | "dist": { |
| 13 | "type": "path", | 13 | "type": "path", |
| 14 | "url": "..", | 14 | "url": "..", |
| 15 | - "reference": "c1cabead8c1f0269ad37d043feeb6d5754a059d3" | 15 | + "reference": "74ba5e04d1f150a20d38a91c1bfe52163847656f" |
| 16 | }, | 16 | }, |
| 17 | "require": { | 17 | "require": { |
| 18 | "php": ">=8.4", | 18 | "php": ">=8.4", |
| @@ -23,13 +23,21 @@ | |||
| 23 | }, | 23 | }, |
| 24 | "require-dev": { | 24 | "require-dev": { |
| 25 | "friendsofphp/php-cs-fixer": "^3.95", | 25 | "friendsofphp/php-cs-fixer": "^3.95", |
| 26 | + "nyholm/psr7": "^1.8", | ||
| 26 | "phpstan/phpstan": "^2.3", | 27 | "phpstan/phpstan": "^2.3", |
| 27 | "phpstan/phpstan-phpunit": "^2.1", | 28 | "phpstan/phpstan-phpunit": "^2.1", |
| 28 | "phpunit/phpunit": "^13.4", | 29 | "phpunit/phpunit": "^13.4", |
| 29 | - "symfony/framework-bundle": "^7.4 || ^8.0" | 30 | + "psr/http-server-handler": "^1.0", |
| 31 | + "symfony/console": "^7.4 || ^8.0", | ||
| 32 | + "symfony/dotenv": "^7.4 || ^8.0", | ||
| 33 | + "symfony/framework-bundle": "^7.4 || ^8.0", | ||
| 34 | + "symfony/psr-http-message-bridge": "^7.4 || ^8.0" | ||
| 30 | }, | 35 | }, |
| 31 | "suggest": { | 36 | "suggest": { |
| 32 | - "ext-zip": "To build the deployment zip with vendor/bin/scaleway-package" | 37 | + "ext-zip": "To build the deployment zip with vendor/bin/scaleway-package", |
| 38 | + "nyholm/psr7": "To run Front Controllers returning a PSR-15 request handler", | ||
| 39 | + "symfony/console": "To run Command Invocations and Front Controllers returning a Console Application or a Command", | ||
| 40 | + "symfony/psr-http-message-bridge": "To run Front Controllers returning a PSR-15 request handler" | ||
| 33 | }, | 41 | }, |
| 34 | "bin": [ | 42 | "bin": [ |
| 35 | "bin/scaleway-init", | 43 | "bin/scaleway-init", |
added
example/e2e/deploy-locally.sh +47 -0 | new file mode 100755 | ||
| @@ -0,0 +1,47 @@ | ||
| 1 | +#!/usr/bin/env bash | |
| 2 | +# | |
| 3 | +# Serves the example as Scaleway deploys it, with the jdecool/scaleway-runtime of this checkout: | |
| 4 | +# packages a copy of the application, installs the zip with "composer install --no-dev" without the | |
| 5 | +# function's variables, as Scaleway does at deploy time, then serves it in prod through the Emulator. | |
| 6 | +# Usage: [APP_SECRET=...] [SCALEWAY_CRON_SECRET=...] example/e2e/deploy-locally.sh [<host>:<port>] | |
| 7 | +# Requires bash, composer, php with the zip extension, tar and unzip. Leaves the working tree untouched. | |
| 8 | + | |
| 9 | +set -euo pipefail | |
| 10 | + | |
| 11 | +address=${1:-127.0.0.1:8000} | |
| 12 | +example=$(cd "$(dirname "$0")/.." && pwd) | |
| 13 | +runtime=$(dirname "$example") | |
| 14 | +tmp=$(mktemp -d) | |
| 15 | +trap 'chmod -R u+w "$tmp"; rm -rf "$tmp"' EXIT | |
| 16 | + | |
| 17 | +echo "> Publishing the runtime of $runtime as dev-main" | |
| 18 | +# Stands in for Packagist: a repository outside composer.json, so the zip ships the files users ship. | |
| 19 | +composer archive --working-dir="$runtime" --format=zip --dir="$tmp" --file=runtime --quiet | |
| 20 | +COMPOSER_CACHE_DIR=${COMPOSER_CACHE_DIR:-$(composer config --global cache-dir)} | |
| 21 | +export COMPOSER_CACHE_DIR COMPOSER_HOME="$tmp/composer-home" | |
| 22 | +composer config --global repositories.runtime "$(php -r ' | |
| 23 | + $package = json_decode(file_get_contents($argv[1]), true); | |
| 24 | + $package["version"] = "dev-main"; | |
| 25 | + $package["dist"] = ["type" => "zip", "url" => $argv[2]]; | |
| 26 | + echo json_encode(["type" => "package", "package" => $package], JSON_UNESCAPED_SLASHES); | |
| 27 | +' "$runtime/composer.json" "$tmp/runtime.zip")" | |
| 28 | + | |
| 29 | +echo "> Packaging a copy of $example" | |
| 30 | +mkdir "$tmp/app" | |
| 31 | +tar -C "$example" --exclude=./vendor --exclude=./var --exclude=./function.zip -cf - . | tar -C "$tmp/app" -xf - | |
| 32 | +# The repository "composer link-runtime" adds, if any, which scaleway-package would rightly refuse. | |
| 33 | +composer config --working-dir="$tmp/app" --unset repositories.local | |
| 34 | +composer update jdecool/scaleway-runtime --working-dir="$tmp/app" --no-interaction --no-progress --quiet | |
| 35 | +(cd "$tmp/app" && php vendor/bin/scaleway-package "$tmp/function.zip") | |
| 36 | + | |
| 37 | +echo "> Installing the zip like Scaleway: composer install --no-dev, without the function's variables" | |
| 38 | +unzip -q "$tmp/function.zip" -d "$tmp/function" | |
| 39 | +env -u APP_ENV -u APP_DEBUG -u APP_SECRET -u SCALEWAY_CRON_SECRET -u SCALEWAY_HOST \ | |
| 40 | + composer install --working-dir="$tmp/function" --no-dev --no-interaction --no-progress | |
| 41 | +if [[ -d "$tmp/function/var/cache/prod" ]]; then | |
| 42 | + echo "> The post-install-cmd warmed the prod cache" | |
| 43 | +else | |
| 44 | + echo "> No prod cache warmed at deploy time: the first Invocation builds it" | |
| 45 | +fi | |
| 46 | + | |
| 47 | +APP_ENV=prod php "$tmp/function/vendor/bin/scaleway-serve" "$address" | |
| new file mode 100755 | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | +#!/usr/bin/env bash | ||
| 2 | +# | ||
| 3 | +# Serves the example as Scaleway deploys it, with the jdecool/scaleway-runtime of this checkout: | ||
| 4 | +# packages a copy of the application, installs the zip with "composer install --no-dev" without the | ||
| 5 | +# function's variables, as Scaleway does at deploy time, then serves it in prod through the Emulator. | ||
| 6 | +# Usage: [APP_SECRET=...] [SCALEWAY_CRON_SECRET=...] example/e2e/deploy-locally.sh [<host>:<port>] | ||
| 7 | +# Requires bash, composer, php with the zip extension, tar and unzip. Leaves the working tree untouched. | ||
| 8 | + | ||
| 9 | +set -euo pipefail | ||
| 10 | + | ||
| 11 | +address=${1:-127.0.0.1:8000} | ||
| 12 | +example=$(cd "$(dirname "$0")/.." && pwd) | ||
| 13 | +runtime=$(dirname "$example") | ||
| 14 | +tmp=$(mktemp -d) | ||
| 15 | +trap 'chmod -R u+w "$tmp"; rm -rf "$tmp"' EXIT | ||
| 16 | + | ||
| 17 | +echo "> Publishing the runtime of $runtime as dev-main" | ||
| 18 | +# Stands in for Packagist: a repository outside composer.json, so the zip ships the files users ship. | ||
| 19 | +composer archive --working-dir="$runtime" --format=zip --dir="$tmp" --file=runtime --quiet | ||
| 20 | +COMPOSER_CACHE_DIR=${COMPOSER_CACHE_DIR:-$(composer config --global cache-dir)} | ||
| 21 | +export COMPOSER_CACHE_DIR COMPOSER_HOME="$tmp/composer-home" | ||
| 22 | +composer config --global repositories.runtime "$(php -r ' | ||
| 23 | + $package = json_decode(file_get_contents($argv[1]), true); | ||
| 24 | + $package["version"] = "dev-main"; | ||
| 25 | + $package["dist"] = ["type" => "zip", "url" => $argv[2]]; | ||
| 26 | + echo json_encode(["type" => "package", "package" => $package], JSON_UNESCAPED_SLASHES); | ||
| 27 | +' "$runtime/composer.json" "$tmp/runtime.zip")" | ||
| 28 | + | ||
| 29 | +echo "> Packaging a copy of $example" | ||
| 30 | +mkdir "$tmp/app" | ||
| 31 | +tar -C "$example" --exclude=./vendor --exclude=./var --exclude=./function.zip -cf - . | tar -C "$tmp/app" -xf - | ||
| 32 | +# The repository "composer link-runtime" adds, if any, which scaleway-package would rightly refuse. | ||
| 33 | +composer config --working-dir="$tmp/app" --unset repositories.local | ||
| 34 | +composer update jdecool/scaleway-runtime --working-dir="$tmp/app" --no-interaction --no-progress --quiet | ||
| 35 | +(cd "$tmp/app" && php vendor/bin/scaleway-package "$tmp/function.zip") | ||
| 36 | + | ||
| 37 | +echo "> Installing the zip like Scaleway: composer install --no-dev, without the function's variables" | ||
| 38 | +unzip -q "$tmp/function.zip" -d "$tmp/function" | ||
| 39 | +env -u APP_ENV -u APP_DEBUG -u APP_SECRET -u SCALEWAY_CRON_SECRET -u SCALEWAY_HOST \ | ||
| 40 | + composer install --working-dir="$tmp/function" --no-dev --no-interaction --no-progress | ||
| 41 | +if [[ -d "$tmp/function/var/cache/prod" ]]; then | ||
| 42 | + echo "> The post-install-cmd warmed the prod cache" | ||
| 43 | +else | ||
| 44 | + echo "> No prod cache warmed at deploy time: the first Invocation builds it" | ||
| 45 | +fi | ||
| 46 | + | ||
| 47 | +APP_ENV=prod php "$tmp/function/vendor/bin/scaleway-serve" "$address" | ||
modified
example/terraform/main.tf +17 -3 | @@ -28,6 +28,13 @@ variable "app_secret" { | ||
| 28 | 28 | sensitive = true |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | +# The public host of the function, for absolute URLs: its own domain_name cannot be referenced here (a cycle), | |
| 32 | +# so set it to a custom domain, or to the endpoint output after a first apply. Empty keeps the runtime's localhost. | |
| 33 | +variable "host" { | |
| 34 | + type = string | |
| 35 | + default = "" | |
| 36 | +} | |
| 37 | + | |
| 31 | 38 | # Lets CRON triggers run console commands; see docs/adr/0005-authenticate-command-invocations-with-a-shared-secret.md. |
| 32 | 39 | variable "cron_secret" { |
| 33 | 40 | type = string |
| @@ -58,9 +65,7 @@ resource "scaleway_function" "example" { | ||
| 58 | 65 | zip_hash = filesha256(var.zip_file) |
| 59 | 66 | deploy = true |
| 60 | 67 | |
| 61 | - environment_variables = { | |
| 62 | - APP_ENV = "prod" | |
| 63 | - } | |
| 68 | + environment_variables = merge({ APP_ENV = "prod" }, var.host == "" ? {} : { SCALEWAY_HOST = var.host }) | |
| 64 | 69 | secret_environment_variables = { |
| 65 | 70 | APP_SECRET = var.app_secret |
| 66 | 71 | SCALEWAY_CRON_SECRET = var.cron_secret |
| @@ -73,6 +78,11 @@ resource "scaleway_function_cron" "example" { | ||
| 73 | 78 | name = "example-cron" |
| 74 | 79 | schedule = "0 * * * *" |
| 75 | 80 | args = jsonencode({ job = "hourly" }) |
| 81 | + | |
| 82 | + # Replacing the function deletes its CRON triggers server side: recreate them in the same apply. | |
| 83 | + lifecycle { | |
| 84 | + replace_triggered_by = [scaleway_function.example.id] | |
| 85 | + } | |
| 76 | 86 | } |
| 77 | 87 | |
| 78 | 88 | # Runs the app:greet console command every hour: the matching secret turns the arguments into a Command Invocation. |
| @@ -81,6 +91,10 @@ resource "scaleway_function_cron" "example_command" { | ||
| 81 | 91 | name = "example-command" |
| 82 | 92 | schedule = "30 * * * *" |
| 83 | 93 | args = jsonencode({ command = "app:greet", args = { name = "CRON" }, secret = var.cron_secret }) |
| 94 | + | |
| 95 | + lifecycle { | |
| 96 | + replace_triggered_by = [scaleway_function.example.id] | |
| 97 | + } | |
| 84 | 98 | } |
| 85 | 99 | |
| 86 | 100 | output "endpoint" { |
| @@ -28,6 +28,13 @@ variable "app_secret" { | |||
| 28 | sensitive = true | 28 | sensitive = true |
| 29 | } | 29 | } |
| 30 | 30 | ||
| 31 | +# The public host of the function, for absolute URLs: its own domain_name cannot be referenced here (a cycle), | ||
| 32 | +# so set it to a custom domain, or to the endpoint output after a first apply. Empty keeps the runtime's localhost. | ||
| 33 | +variable "host" { | ||
| 34 | + type = string | ||
| 35 | + default = "" | ||
| 36 | +} | ||
| 37 | + | ||
| 31 | # Lets CRON triggers run console commands; see docs/adr/0005-authenticate-command-invocations-with-a-shared-secret.md. | 38 | # Lets CRON triggers run console commands; see docs/adr/0005-authenticate-command-invocations-with-a-shared-secret.md. |
| 32 | variable "cron_secret" { | 39 | variable "cron_secret" { |
| 33 | type = string | 40 | type = string |
| @@ -58,9 +65,7 @@ resource "scaleway_function" "example" { | |||
| 58 | zip_hash = filesha256(var.zip_file) | 65 | zip_hash = filesha256(var.zip_file) |
| 59 | deploy = true | 66 | deploy = true |
| 60 | 67 | ||
| 61 | - environment_variables = { | 68 | + environment_variables = merge({ APP_ENV = "prod" }, var.host == "" ? {} : { SCALEWAY_HOST = var.host }) |
| 62 | - APP_ENV = "prod" | ||
| 63 | - } | ||
| 64 | secret_environment_variables = { | 69 | secret_environment_variables = { |
| 65 | APP_SECRET = var.app_secret | 70 | APP_SECRET = var.app_secret |
| 66 | SCALEWAY_CRON_SECRET = var.cron_secret | 71 | SCALEWAY_CRON_SECRET = var.cron_secret |
| @@ -73,6 +78,11 @@ resource "scaleway_function_cron" "example" { | |||
| 73 | name = "example-cron" | 78 | name = "example-cron" |
| 74 | schedule = "0 * * * *" | 79 | schedule = "0 * * * *" |
| 75 | args = jsonencode({ job = "hourly" }) | 80 | args = jsonencode({ job = "hourly" }) |
| 81 | + | ||
| 82 | + # Replacing the function deletes its CRON triggers server side: recreate them in the same apply. | ||
| 83 | + lifecycle { | ||
| 84 | + replace_triggered_by = [scaleway_function.example.id] | ||
| 85 | + } | ||
| 76 | } | 86 | } |
| 77 | 87 | ||
| 78 | # Runs the app:greet console command every hour: the matching secret turns the arguments into a Command Invocation. | 88 | # Runs the app:greet console command every hour: the matching secret turns the arguments into a Command Invocation. |
| @@ -81,6 +91,10 @@ resource "scaleway_function_cron" "example_command" { | |||
| 81 | name = "example-command" | 91 | name = "example-command" |
| 82 | schedule = "30 * * * *" | 92 | schedule = "30 * * * *" |
| 83 | args = jsonencode({ command = "app:greet", args = { name = "CRON" }, secret = var.cron_secret }) | 93 | args = jsonencode({ command = "app:greet", args = { name = "CRON" }, secret = var.cron_secret }) |
| 94 | + | ||
| 95 | + lifecycle { | ||
| 96 | + replace_triggered_by = [scaleway_function.example.id] | ||
| 97 | + } | ||
| 84 | } | 98 | } |
| 85 | 99 | ||
| 86 | output "endpoint" { | 100 | output "endpoint" { |