jdecool/scaleway-runtimepublic Fork
⑂ 4276070
Commits
⬇ Clone ▾
git clone https://git.rickub.com/jdecool/scaleway-runtime.git
git clone ssh://git@rickub.com/jdecool/scaleway-runtime.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP

feat(example): run a demo console command from a CRON triggerUnverified

Adds the app:greet command, e2e scenarios proving a command body without the
CRON secret stays an HTTP body while one with it runs the command (200, or 500
with the output on failure), and a Terraform CRON trigger carrying the secret.
Jérémy DECOOL committed 2026-11-13T23:03:55+01:00 Browse files
4276070 parent: 955033d
modified .github/workflows/ci.yml +2 -2
@@ -68,11 +68,11 @@ jobs:
6868 run: |
6969 unzip -q example/function.zip -d "$RUNNER_TEMP/function"
7070 chmod -R a-w "$RUNNER_TEMP/function"
71- APP_ENV=prod APP_SECRET=ci php "$RUNNER_TEMP/function/vendor/bin/scaleway-serve" 127.0.0.1:8000 > "$RUNNER_TEMP/serve.log" 2>&1 &
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 &
7272 timeout 10 bash -c 'until curl -sf http://127.0.0.1:8000/hello > /dev/null; do sleep 0.2; done'
7373
7474 - name: End-to-end scenarios
75- run: example/e2e/run.sh http://127.0.0.1:8000
75+ run: SCALEWAY_CRON_SECRET=ci-cron-secret example/e2e/run.sh http://127.0.0.1:8000
7676
7777 - name: Emulator log
7878 if: always()
@@ -68,11 +68,11 @@ jobs:
68 run: |68 run: |
69 unzip -q example/function.zip -d "$RUNNER_TEMP/function"69 unzip -q example/function.zip -d "$RUNNER_TEMP/function"
70 chmod -R a-w "$RUNNER_TEMP/function"70 chmod -R a-w "$RUNNER_TEMP/function"
71- APP_ENV=prod APP_SECRET=ci php "$RUNNER_TEMP/function/vendor/bin/scaleway-serve" 127.0.0.1:8000 > "$RUNNER_TEMP/serve.log" 2>&1 &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'72 timeout 10 bash -c 'until curl -sf http://127.0.0.1:8000/hello > /dev/null; do sleep 0.2; done'
73 73
74 - name: End-to-end scenarios74 - name: End-to-end scenarios
75- run: example/e2e/run.sh http://127.0.0.1:800075+ run: SCALEWAY_CRON_SECRET=ci-cron-secret example/e2e/run.sh http://127.0.0.1:8000
76 76
77 - name: Emulator log77 - name: Emulator log
78 if: always()78 if: always()
modified example/README.md +11 -4
@@ -12,6 +12,11 @@ A minimal Symfony application running on Scaleway Serverless Functions through `
1212 | `GET /counter` | A per-process invocation counter: it keeps growing while a Warm Process serves the Invocations |
1313 | `POST /` | Where CRON Invocations land, with the trigger's arguments as JSON body |
1414
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`.
16+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 /`.
18+`bin/console` works as a Front Controller too, for a function that only runs commands: `APP_RUNTIME_OPTIONS='{"front_controller":"bin/console"}'`.
19+
1520 `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.
1621 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'`.
1722
@@ -23,8 +28,9 @@ Its PSR-7 request sees the client, scheme and host resolved from the platform's
2328 ```sh
2429 cd example
2530 composer install
26-vendor/bin/scaleway-serve # the Emulator, on http://127.0.0.1:8000
27-e2e/run.sh http://127.0.0.1:8000 # in another terminal
31+export SCALEWAY_CRON_SECRET=local-secret # lets the Emulator and the scenarios run Command Invocations
32+vendor/bin/scaleway-serve # the Emulator, on http://127.0.0.1:8000
33+e2e/run.sh http://127.0.0.1:8000 # in another terminal, with the same SCALEWAY_CRON_SECRET
2834 ```
2935
3036 To run exactly what gets deployed, serve the packaged zip from a read-only copy:
@@ -32,7 +38,7 @@ To run exactly what gets deployed, serve the packaged zip from a read-only copy:
3238 ```sh
3339 vendor/bin/scaleway-package
3440 unzip -q function.zip -d /tmp/function && chmod -R a-w /tmp/function
35-APP_ENV=prod APP_SECRET=local php /tmp/function/vendor/bin/scaleway-serve
41+APP_ENV=prod APP_SECRET=local SCALEWAY_CRON_SECRET=local-secret php /tmp/function/vendor/bin/scaleway-serve
3642 ```
3743
3844 ## Deploy it
@@ -47,7 +53,8 @@ cd example
4753 vendor/bin/scaleway-package
4854 export SCW_ACCESS_KEY=... SCW_SECRET_KEY=... SCW_DEFAULT_PROJECT_ID=...
4955 terraform -chdir=terraform init
50-terraform -chdir=terraform apply -var app_secret="$(openssl rand -hex 16)"
56+export SCALEWAY_CRON_SECRET=$(openssl rand -hex 32)
57+terraform -chdir=terraform apply -var app_secret="$(openssl rand -hex 16)" -var cron_secret="$SCALEWAY_CRON_SECRET"
5158 e2e/run.sh "$(terraform -chdir=terraform output -raw endpoint)"
5259 terraform -chdir=terraform destroy
5360 ```
@@ -12,6 +12,11 @@ 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+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.
17+Without 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"}'`.
19+
15 `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.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.
16 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'`.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'`.
17 22
@@ -23,8 +28,9 @@ Its PSR-7 request sees the client, scheme and host resolved from the platform's
23 ```sh28 ```sh
24 cd example29 cd example
25 composer install30 composer install
26-vendor/bin/scaleway-serve # the Emulator, on http://127.0.0.1:800031+export SCALEWAY_CRON_SECRET=local-secret # lets the Emulator and the scenarios run Command Invocations
27-e2e/run.sh http://127.0.0.1:8000 # in another terminal32+vendor/bin/scaleway-serve # the Emulator, on http://127.0.0.1:8000
33+e2e/run.sh http://127.0.0.1:8000 # in another terminal, with the same SCALEWAY_CRON_SECRET
28 ```34 ```
29 35
30 To run exactly what gets deployed, serve the packaged zip from a read-only copy:36 To run exactly what gets deployed, serve the packaged zip from a read-only copy:
@@ -32,7 +38,7 @@ To run exactly what gets deployed, serve the packaged zip from a read-only copy:
32 ```sh38 ```sh
33 vendor/bin/scaleway-package39 vendor/bin/scaleway-package
34 unzip -q function.zip -d /tmp/function && chmod -R a-w /tmp/function40 unzip -q function.zip -d /tmp/function && chmod -R a-w /tmp/function
35-APP_ENV=prod APP_SECRET=local php /tmp/function/vendor/bin/scaleway-serve41+APP_ENV=prod APP_SECRET=local SCALEWAY_CRON_SECRET=local-secret php /tmp/function/vendor/bin/scaleway-serve
36 ```42 ```
37 43
38 ## Deploy it44 ## Deploy it
@@ -47,7 +53,8 @@ cd example
47 vendor/bin/scaleway-package53 vendor/bin/scaleway-package
48 export SCW_ACCESS_KEY=... SCW_SECRET_KEY=... SCW_DEFAULT_PROJECT_ID=...54 export SCW_ACCESS_KEY=... SCW_SECRET_KEY=... SCW_DEFAULT_PROJECT_ID=...
49 terraform -chdir=terraform init55 terraform -chdir=terraform init
50-terraform -chdir=terraform apply -var app_secret="$(openssl rand -hex 16)"56+export SCALEWAY_CRON_SECRET=$(openssl rand -hex 32)
57+terraform -chdir=terraform apply -var app_secret="$(openssl rand -hex 16)" -var cron_secret="$SCALEWAY_CRON_SECRET"
51 e2e/run.sh "$(terraform -chdir=terraform output -raw endpoint)"58 e2e/run.sh "$(terraform -chdir=terraform output -raw endpoint)"
52 terraform -chdir=terraform destroy59 terraform -chdir=terraform destroy
53 ```60 ```
modified example/e2e/run.sh +25 -1
@@ -1,7 +1,7 @@
11 #!/usr/bin/env bash
22 #
33 # End-to-end scenarios for the example application.
4-# Usage: example/e2e/run.sh <base-url>
4+# Usage: [SCALEWAY_CRON_SECRET=...] example/e2e/run.sh <base-url>
55 # against the Emulator: example/e2e/run.sh http://127.0.0.1:8000
66 # against the deployment: example/e2e/run.sh "$(terraform -chdir=example/terraform output -raw endpoint)"
77 # Requires bash, curl and php (used to read JSON and hash files).
@@ -116,6 +116,30 @@ expect "unknown route" "$status" 404
116116 status=$(request cron -X POST -H 'Content-Type: application/json' --data '{"job":"cleanup"}' "$base_url/")
117117 expect "CRON-like Invocation" "$(field cron args.job)" cleanup
118118
119+# A body naming a command runs nothing without the CRON secret: it is a plain HTTP body.
120+status=$(request forged -X POST -H 'Content-Type: application/json' \
121+ --data '{"command":"app:greet","args":{"name":"Mallory"},"secret":"guess"}' "$base_url/")
122+expect "command without the CRON secret stays HTTP" "$status $(field forged args.command)" "200 app:greet"
123+
124+if [[ -n "${SCALEWAY_CRON_SECRET:-}" ]]; then
125+ # command_body <name> <args JSON>: the arguments a CRON trigger running a command carries.
126+ command_body() {
127+ php -r 'echo json_encode(["command" => $argv[1], "args" => json_decode($argv[2]), "secret" => $argv[3]]);' "$1" "$2" "$SCALEWAY_CRON_SECRET"
128+ }
129+ status=$(request command -X POST --data "$(command_body app:greet '{"name":"Scaleway","--yell":true}')" "$base_url/")
130+ expect "Command Invocation status" "$status" 200
131+ expect "Command Invocation output" "$(cat "$tmp/command.body")" "HELLO SCALEWAY!"
132+
133+ status=$(request failed -X POST --data "$(command_body app:greet '{"--fail":true}')" "$base_url/")
134+ expect "failed command status" "$status" 500
135+ expect "failed command output" "$(grep -c 'Failing on purpose.' "$tmp/failed.body")" 1
136+
137+ request after "$base_url/hello?name=again" > /dev/null
138+ expect "HTTP Invocation after commands" "$(field after message)" "Hello again!"
139+else
140+ echo "skip Command Invocations: set SCALEWAY_CRON_SECRET to the function's"
141+fi
142+
119143 if ((failures > 0)); then
120144 echo "$failures check(s) failed"
121145 exit 1
@@ -1,7 +1,7 @@
1 #!/usr/bin/env bash1 #!/usr/bin/env bash
2 #2 #
3 # End-to-end scenarios for the example application.3 # End-to-end scenarios for the example application.
4-# Usage: example/e2e/run.sh <base-url>4+# Usage: [SCALEWAY_CRON_SECRET=...] example/e2e/run.sh <base-url>
5 # against the Emulator: example/e2e/run.sh http://127.0.0.1:80005 # against the Emulator: example/e2e/run.sh http://127.0.0.1:8000
6 # against the deployment: example/e2e/run.sh "$(terraform -chdir=example/terraform output -raw endpoint)"6 # against the deployment: example/e2e/run.sh "$(terraform -chdir=example/terraform output -raw endpoint)"
7 # Requires bash, curl and php (used to read JSON and hash files).7 # Requires bash, curl and php (used to read JSON and hash files).
@@ -116,6 +116,30 @@ expect "unknown route" "$status" 404
116 status=$(request cron -X POST -H 'Content-Type: application/json' --data '{"job":"cleanup"}' "$base_url/")116 status=$(request cron -X POST -H 'Content-Type: application/json' --data '{"job":"cleanup"}' "$base_url/")
117 expect "CRON-like Invocation" "$(field cron args.job)" cleanup117 expect "CRON-like Invocation" "$(field cron args.job)" cleanup
118 118
119+# A body naming a command runs nothing without the CRON secret: it is a plain HTTP body.
120+status=$(request forged -X POST -H 'Content-Type: application/json' \
121+ --data '{"command":"app:greet","args":{"name":"Mallory"},"secret":"guess"}' "$base_url/")
122+expect "command without the CRON secret stays HTTP" "$status $(field forged args.command)" "200 app:greet"
123+
124+if [[ -n "${SCALEWAY_CRON_SECRET:-}" ]]; then
125+ # command_body <name> <args JSON>: the arguments a CRON trigger running a command carries.
126+ command_body() {
127+ php -r 'echo json_encode(["command" => $argv[1], "args" => json_decode($argv[2]), "secret" => $argv[3]]);' "$1" "$2" "$SCALEWAY_CRON_SECRET"
128+ }
129+ status=$(request command -X POST --data "$(command_body app:greet '{"name":"Scaleway","--yell":true}')" "$base_url/")
130+ expect "Command Invocation status" "$status" 200
131+ expect "Command Invocation output" "$(cat "$tmp/command.body")" "HELLO SCALEWAY!"
132+
133+ status=$(request failed -X POST --data "$(command_body app:greet '{"--fail":true}')" "$base_url/")
134+ expect "failed command status" "$status" 500
135+ expect "failed command output" "$(grep -c 'Failing on purpose.' "$tmp/failed.body")" 1
136+
137+ request after "$base_url/hello?name=again" > /dev/null
138+ expect "HTTP Invocation after commands" "$(field after message)" "Hello again!"
139+else
140+ echo "skip Command Invocations: set SCALEWAY_CRON_SECRET to the function's"
141+fi
142+
119 if ((failures > 0)); then143 if ((failures > 0)); then
120 echo "$failures check(s) failed"144 echo "$failures check(s) failed"
121 exit 1145 exit 1
added example/src/Command/GreetCommand.php +31 -0
new file mode 100644
@@ -0,0 +1,31 @@
1+<?php
2+
3+namespace App\Command;
4+
5+use Symfony\Component\Console\Attribute\Argument;
6+use Symfony\Component\Console\Attribute\AsCommand;
7+use Symfony\Component\Console\Attribute\Option;
8+use Symfony\Component\Console\Command\Command;
9+use Symfony\Component\Console\Style\SymfonyStyle;
10+
11+// Run by a CRON trigger whose arguments are {"command": "app:greet", "args": {"name": "..."}, "secret": "..."}.
12+#[AsCommand('app:greet', 'Greets someone, or fails on demand to show how a failed command is reported')]
13+final class GreetCommand
14+{
15+ public function __invoke(
16+ SymfonyStyle $io,
17+ #[Argument] string $name = 'world',
18+ #[Option] bool $yell = false,
19+ #[Option] bool $fail = false,
20+ ): int {
21+ $greeting = \sprintf('Hello %s!', $name);
22+ $io->writeln($yell ? strtoupper($greeting) : $greeting);
23+ if ($fail) {
24+ $io->error('Failing on purpose.');
25+
26+ return Command::FAILURE;
27+ }
28+
29+ return Command::SUCCESS;
30+ }
31+}
new file mode 100644
@@ -0,0 +1,31 @@
1+<?php
2+
3+namespace App\Command;
4+
5+use Symfony\Component\Console\Attribute\Argument;
6+use Symfony\Component\Console\Attribute\AsCommand;
7+use Symfony\Component\Console\Attribute\Option;
8+use Symfony\Component\Console\Command\Command;
9+use Symfony\Component\Console\Style\SymfonyStyle;
10+
11+// Run by a CRON trigger whose arguments are {"command": "app:greet", "args": {"name": "..."}, "secret": "..."}.
12+#[AsCommand('app:greet', 'Greets someone, or fails on demand to show how a failed command is reported')]
13+final class GreetCommand
14+{
15+ public function __invoke(
16+ SymfonyStyle $io,
17+ #[Argument] string $name = 'world',
18+ #[Option] bool $yell = false,
19+ #[Option] bool $fail = false,
20+ ): int {
21+ $greeting = \sprintf('Hello %s!', $name);
22+ $io->writeln($yell ? strtoupper($greeting) : $greeting);
23+ if ($fail) {
24+ $io->error('Failing on purpose.');
25+
26+ return Command::FAILURE;
27+ }
28+
29+ return Command::SUCCESS;
30+ }
31+}
modified example/terraform/main.tf +16 -1
@@ -28,6 +28,12 @@ variable "app_secret" {
2828 sensitive = true
2929 }
3030
31+# Lets CRON triggers run console commands; see docs/adr/0005-authenticate-command-invocations-with-a-shared-secret.md.
32+variable "cron_secret" {
33+ type = string
34+ sensitive = true
35+}
36+
3137 provider "scaleway" {
3238 region = var.region
3339 }
@@ -56,7 +62,8 @@ resource "scaleway_function" "example" {
5662 APP_ENV = "prod"
5763 }
5864 secret_environment_variables = {
59- APP_SECRET = var.app_secret
65+ APP_SECRET = var.app_secret
66+ SCALEWAY_CRON_SECRET = var.cron_secret
6067 }
6168 }
6269
@@ -68,6 +75,14 @@ resource "scaleway_function_cron" "example" {
6875 args = jsonencode({ job = "hourly" })
6976 }
7077
78+# Runs the app:greet console command every hour: the matching secret turns the arguments into a Command Invocation.
79+resource "scaleway_function_cron" "example_command" {
80+ function_id = scaleway_function.example.id
81+ name = "example-command"
82+ schedule = "30 * * * *"
83+ args = jsonencode({ command = "app:greet", args = { name = "CRON" }, secret = var.cron_secret })
84+}
85+
7186 output "endpoint" {
7287 value = "https://${scaleway_function.example.domain_name}"
7388 }
@@ -28,6 +28,12 @@ variable "app_secret" {
28 sensitive = true28 sensitive = true
29 }29 }
30 30
31+# Lets CRON triggers run console commands; see docs/adr/0005-authenticate-command-invocations-with-a-shared-secret.md.
32+variable "cron_secret" {
33+ type = string
34+ sensitive = true
35+}
36+
31 provider "scaleway" {37 provider "scaleway" {
32 region = var.region38 region = var.region
33 }39 }
@@ -56,7 +62,8 @@ resource "scaleway_function" "example" {
56 APP_ENV = "prod"62 APP_ENV = "prod"
57 }63 }
58 secret_environment_variables = {64 secret_environment_variables = {
59- APP_SECRET = var.app_secret65+ APP_SECRET = var.app_secret
66+ SCALEWAY_CRON_SECRET = var.cron_secret
60 }67 }
61 }68 }
62 69
@@ -68,6 +75,14 @@ resource "scaleway_function_cron" "example" {
68 args = jsonencode({ job = "hourly" })75 args = jsonencode({ job = "hourly" })
69 }76 }
70 77
78+# Runs the app:greet console command every hour: the matching secret turns the arguments into a Command Invocation.
79+resource "scaleway_function_cron" "example_command" {
80+ function_id = scaleway_function.example.id
81+ name = "example-command"
82+ schedule = "30 * * * *"
83+ args = jsonencode({ command = "app:greet", args = { name = "CRON" }, secret = var.cron_secret })
84+}
85+
71 output "endpoint" {86 output "endpoint" {
72 value = "https://${scaleway_function.example.domain_name}"87 value = "https://${scaleway_function.example.domain_name}"
73 }88 }