|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Console\Commands; |
| 6 | + |
| 7 | +use App\Models\ReverbApplication; |
| 8 | +use Illuminate\Console\Command; |
| 9 | +use Illuminate\Support\Facades\Validator; |
| 10 | +use JsonException; |
| 11 | +use Throwable; |
| 12 | + |
| 13 | +/** |
| 14 | + * Seed reverb_applications rows from the REVERB_SEED_APPS env var. |
| 15 | + * |
| 16 | + * The env carries a JSON array of app definitions. Run as a oneshot at |
| 17 | + * pod boot (after migrate, before php-fpm/reverb) so the instance comes |
| 18 | + * up with its apps already provisioned — no race with the admin API. |
| 19 | + * |
| 20 | + * Idempotent: existing rows are upserted by id, so secret rotation or |
| 21 | + * allowed-origin changes just need a redeploy with new env. Rows that |
| 22 | + * exist in the DB but are absent from the env are left alone — the |
| 23 | + * admin API is the authority for ad-hoc app creation, the env is only |
| 24 | + * for the bootstrap set. |
| 25 | + * |
| 26 | + * Exit codes: |
| 27 | + * 0 — success (including no env set / empty array) |
| 28 | + * 1 — invalid JSON or schema, or write failure |
| 29 | + * |
| 30 | + * Schema per entry: |
| 31 | + * id string, required (primary key) |
| 32 | + * key string, required (must be unique across apps) |
| 33 | + * secret string, required |
| 34 | + * name string, required |
| 35 | + * allowed_origins array of string, optional, default ["*"] |
| 36 | + * enable_client_messages bool, optional, default false |
| 37 | + * max_connections int|null, optional |
| 38 | + * max_message_size int, optional, default 10000 |
| 39 | + * options object, optional |
| 40 | + */ |
| 41 | +class SeedReverbAppsCommand extends Command |
| 42 | +{ |
| 43 | + protected $signature = 'reverb:seed-from-env'; |
| 44 | + |
| 45 | + protected $description = 'Seed reverb_applications from the REVERB_SEED_APPS env var (JSON array).'; |
| 46 | + |
| 47 | + public function handle(): int |
| 48 | + { |
| 49 | + $raw = (string) env('REVERB_SEED_APPS', ''); |
| 50 | + if (trim($raw) === '') { |
| 51 | + $this->info('REVERB_SEED_APPS empty — nothing to seed.'); |
| 52 | + |
| 53 | + return self::SUCCESS; |
| 54 | + } |
| 55 | + |
| 56 | + try { |
| 57 | + $decoded = json_decode($raw, associative: true, flags: JSON_THROW_ON_ERROR); |
| 58 | + } catch (JsonException $e) { |
| 59 | + $this->error('REVERB_SEED_APPS is not valid JSON: '.$e->getMessage()); |
| 60 | + |
| 61 | + return self::FAILURE; |
| 62 | + } |
| 63 | + |
| 64 | + if (! is_array($decoded)) { |
| 65 | + $this->error('REVERB_SEED_APPS must decode to a JSON array.'); |
| 66 | + |
| 67 | + return self::FAILURE; |
| 68 | + } |
| 69 | + |
| 70 | + $validator = Validator::make(['apps' => $decoded], [ |
| 71 | + 'apps' => ['array'], |
| 72 | + 'apps.*.id' => ['required', 'string'], |
| 73 | + 'apps.*.key' => ['required', 'string'], |
| 74 | + 'apps.*.secret' => ['required', 'string'], |
| 75 | + 'apps.*.name' => ['required', 'string'], |
| 76 | + 'apps.*.allowed_origins' => ['sometimes', 'array'], |
| 77 | + 'apps.*.allowed_origins.*' => ['string'], |
| 78 | + 'apps.*.enable_client_messages' => ['sometimes', 'boolean'], |
| 79 | + 'apps.*.max_connections' => ['sometimes', 'nullable', 'integer'], |
| 80 | + 'apps.*.max_message_size' => ['sometimes', 'integer'], |
| 81 | + 'apps.*.options' => ['sometimes', 'array'], |
| 82 | + ]); |
| 83 | + |
| 84 | + if ($validator->fails()) { |
| 85 | + $this->error('REVERB_SEED_APPS schema invalid:'); |
| 86 | + foreach ($validator->errors()->all() as $err) { |
| 87 | + $this->line(' - '.$err); |
| 88 | + } |
| 89 | + |
| 90 | + return self::FAILURE; |
| 91 | + } |
| 92 | + |
| 93 | + $created = 0; |
| 94 | + $updated = 0; |
| 95 | + |
| 96 | + try { |
| 97 | + foreach ($decoded as $entry) { |
| 98 | + $attrs = [ |
| 99 | + 'key' => $entry['key'], |
| 100 | + 'secret' => $entry['secret'], |
| 101 | + 'name' => $entry['name'], |
| 102 | + 'allowed_origins' => $entry['allowed_origins'] ?? ['*'], |
| 103 | + 'enable_client_messages' => $entry['enable_client_messages'] ?? false, |
| 104 | + 'max_connections' => $entry['max_connections'] ?? null, |
| 105 | + 'max_message_size' => $entry['max_message_size'] ?? 10000, |
| 106 | + 'options' => $entry['options'] ?? null, |
| 107 | + ]; |
| 108 | + |
| 109 | + $existing = ReverbApplication::query()->find($entry['id']); |
| 110 | + |
| 111 | + if ($existing === null) { |
| 112 | + ReverbApplication::query()->create(['id' => $entry['id'], ...$attrs]); |
| 113 | + $created++; |
| 114 | + $this->info("Created app [{$entry['id']}]"); |
| 115 | + } else { |
| 116 | + $existing->fill($attrs); |
| 117 | + if ($existing->isDirty()) { |
| 118 | + $existing->save(); |
| 119 | + $updated++; |
| 120 | + $this->info("Updated app [{$entry['id']}]"); |
| 121 | + } else { |
| 122 | + $this->line("App [{$entry['id']}] already up-to-date"); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } catch (Throwable $e) { |
| 127 | + $this->error('Failed to upsert app: '.$e->getMessage()); |
| 128 | + |
| 129 | + return self::FAILURE; |
| 130 | + } |
| 131 | + |
| 132 | + $this->info(sprintf( |
| 133 | + 'Seed complete — %d created, %d updated, %d total in env.', |
| 134 | + $created, |
| 135 | + $updated, |
| 136 | + count($decoded), |
| 137 | + )); |
| 138 | + |
| 139 | + return self::SUCCESS; |
| 140 | + } |
| 141 | +} |
0 commit comments