Skip to content
This repository was archived by the owner on Nov 30, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/Client/Resources/EnvironmentsResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ public function withDefaultIncludes(): static
'database',
'cache',
'websocketApplication',
'buckets',
);
}
}
124 changes: 124 additions & 0 deletions app/Commands/EnvironmentUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace App\Commands;

use App\Client\Requests\UpdateEnvironmentRequestData;
use App\Dto\BucketKey;
use App\Dto\Cache;
use App\Dto\Database;
use App\Dto\Environment;
use App\Dto\ObjectStorageBucket;
use App\Dto\WebsocketApplication;
use App\Dto\WebsocketCluster;
use App\Enums\EnvironmentColor;
Expand Down Expand Up @@ -61,6 +63,7 @@ class EnvironmentUpdate extends BaseCommand
{--database-id= : Database ID to attach (empty string to detach)}
{--cache-id= : Cache ID to attach (empty string to detach)}
{--websocket-application-id= : WebSocket application ID to attach (empty string to detach)}
{--bucket-keys= : Bucket keys to attach as comma-separated KEY_ID:DISK entries, with :default on at most one (fk-1:local:default,fk-2:public). Replaces every attached bucket; empty string to detach all}
{--force : Force update without confirmation}';

protected $description = 'Update an environment';
Expand Down Expand Up @@ -97,6 +100,8 @@ public function handle()

protected function updateEnvironment(Environment $environment): Environment
{
$filesystemKeys = $this->filesystemKeys($environment);

// The API turns basic auth on only alongside a password, so one option covers both: a password turns it on, empty turns it off.
$httpBasicAuthPassword = $this->form()->get('http_basic_auth_password');

Expand Down Expand Up @@ -134,6 +139,7 @@ protected function updateEnvironment(Environment $environment): Environment
responseHeadersHsts: $this->hsts($environment),
firewallBlockPath: $this->form()->boolean('firewall_block_path'),
firewallBrowserIntegrityCheck: $this->form()->boolean('firewall_browser_integrity_check'),
filesystemKeys: $filesystemKeys,
usesHttpBasicAuth: $httpBasicAuthPassword === null ? null : filled($httpBasicAuthPassword),
httpBasicAuthPassword: filled($httpBasicAuthPassword) ? $httpBasicAuthPassword : null,
),
Expand All @@ -144,6 +150,56 @@ protected function updateEnvironment(Environment $environment): Environment
return $this->client->environments()->get($environment->id);
}

/**
* The API takes the attached buckets as one list and replaces it, so the option names every bucket the environment should end up with.
*
* @return array<int, array{id: string, disk: string, is_default_disk: bool}>|null
*/
protected function filesystemKeys(Environment $environment): ?array
{
$value = $this->form()->get('filesystem_keys');

if ($value === null) {
return null;
}

$keys = $this->parseBucketKeys($value);

if ($environment->bucketIds !== []) {
$this->confirmDestructive($keys === []
? "Detach every bucket from {$environment->name}?"
: "Replace the buckets attached to {$environment->name}? Any attached bucket not listed will be detached.");
}

return $keys;
}

/**
* @return array<int, array{id: string, disk: string, is_default_disk: bool}>
*/
protected function parseBucketKeys(string $value): array
{
if (trim($value) === '') {
return [];
}

$keys = collect(explode(',', $value))->map(function (string $entry) {
$parts = array_map(trim(...), explode(':', $entry));

if (count($parts) < 2 || count($parts) > 3 || $parts[0] === '' || $parts[1] === '' || (isset($parts[2]) && $parts[2] !== 'default')) {
$this->failAndExit("Invalid bucket key entry: {$entry}. Use KEY_ID:DISK, or KEY_ID:DISK:default for the default disk.");
}

return ['id' => $parts[0], 'disk' => $parts[1], 'is_default_disk' => isset($parts[2])];
});

if ($keys->where('is_default_disk', true)->count() > 1) {
$this->failAndExit('Only one bucket can be the default disk.');
}

return $keys->values()->all();
}

protected function clearableInteger(string $key): int|string|null
{
$value = $this->form()->get($key);
Expand Down Expand Up @@ -376,6 +432,15 @@ protected function defineFields(Environment $environment): void
),
'websocket-application-id',
)->setPreviousValue($environment->websocketApplicationId ?? '');

// Named after the API field so a validation error on filesystem_keys.* asks for the buckets again.
$this->form()->define(
'filesystem_keys',
fn ($resolver) => $resolver->fromInput(
fn ($value) => $this->selectBucketKeys(),
),
'bucket-keys',
)->setLabel('Buckets');
}

/**
Expand Down Expand Up @@ -562,6 +627,65 @@ protected function selectWebsocketApplication(?string $value, ?string $currentId
);
}

protected function selectBucketKeys(): string
{
$buckets = spin(
fn () => $this->client->objectStorageBuckets()->list()->collect(),
'Fetching buckets...',
);

if ($buckets->isEmpty()) {
$this->failAndExit('No buckets found. Create one with `cloud bucket:create`.');
}

$entries = [];
$hasDefault = false;

do {
$bucketId = (string) select(
label: $entries === [] ? 'Bucket' : 'Another bucket',
options: ($entries === [] ? ['' => '— Detach all —'] : []) + $buckets->mapWithKeys(
fn (ObjectStorageBucket $bucket) => [$bucket->id => $bucket->name],
)->all(),
);

if ($bucketId === '') {
return '';
}

$keys = spin(
fn () => $this->client->bucketKeys()->list($bucketId)->collect(),
'Fetching bucket keys...',
);

if ($keys->isEmpty()) {
$this->failAndExit('That bucket has no keys. Create one with `cloud bucket-key:create`.');
}

$keyId = (string) select(
label: 'Bucket key',
options: $keys->mapWithKeys(fn (BucketKey $key) => [$key->id => "{$key->name} ({$key->permission})"])->all(),
);

// The API's own rule, which also keeps out the ':' and ',' the entries are joined with.
$disk = text(
label: 'Disk name',
placeholder: 'public',
required: true,
validate: fn (string $name) => preg_match('/^[A-Za-z0-9 _-]{1,40}$/', $name)
? null
: 'Use up to 40 letters, numbers, spaces, dashes, or underscores.',
);

$isDefault = ! $hasDefault && confirm(label: 'Make it the default disk?', default: false);
$hasDefault = $hasDefault || $isDefault;

$entries[] = $isDefault ? "{$keyId}:{$disk}:default" : "{$keyId}:{$disk}";
} while (confirm(label: 'Attach another bucket?', default: false));

return implode(',', $entries);
}

protected function collectDataAndUpdate(Environment $environment): Environment
{
$selection = multiselect(
Expand Down
6 changes: 6 additions & 0 deletions app/Dto/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public function __construct(
public readonly ?string $databaseSchemaId = null,
public readonly ?string $cacheId = null,
public readonly ?string $websocketApplicationId = null,
/** IDs of the attached buckets. The API does not say which key or disk each one uses. */
public readonly array $bucketIds = [],
#[DataCollectionOf(Secret::class)]
public readonly array $secrets = [],
public readonly ?EnvironmentResend $resend = null,
Expand Down Expand Up @@ -161,6 +163,10 @@ public static function createFromResponse(array $response): self
$transformed['websocketApplicationId'] = $relationships['websocketApplication']['data']['id'];
}

if (isset($relationships['buckets']['data'])) {
$transformed['bucketIds'] = array_column($relationships['buckets']['data'], 'id');
}

if (isset($relationships['secrets']['data'])) {
$secretIds = array_column($relationships['secrets']['data'], 'id');

Expand Down
2 changes: 2 additions & 0 deletions skills/deploying-to-cloud/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ Determine the task and follow the matching path:

- Secrets: `echo "$VALUE" | cloud secret:create --name=NAME --json -n`, then `cloud environment-secret:attach`

- Buckets: `cloud environment:update {environment} --bucket-keys=KEY_ID:DISK:default,KEY_ID:public --json -n --force` attaches bucket keys, read from `cloud bucket-key:list {bucket} --json -n`. It replaces every attached bucket, so list all of them; `--force` is needed when buckets are already attached

- Provision infrastructure: `cloud <resource>:create --json -n`

- Brand new project (no code yet): `cloud new --starter-kit=<kit> --repository=<owner>/<new-repo> --path=<dir> -n` creates a private GitHub repository from the starter kit, deploys it (with a database for Laravel kits), and clones it to `<dir>`. The repository must not exist yet. Run `cloud new -h` for the kits.
Expand Down
1 change: 1 addition & 0 deletions skills/deploying-to-cloud/reference/checklists.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Use these checklists when performing complex setups. Copy the relevant checklist
- [ ] Create the environment
- [ ] Provision the database (`database-cluster:create` then `database:create`)
- [ ] Provision the cache (`cache:create`)
- [ ] Create and attach any buckets (`bucket:create`, then `environment:update --bucket-keys`)
- [ ] Set environment variables (`environment:variables`)
- [ ] Create and attach any secrets (`secret:create` then `environment-secret:attach`)
- [ ] Attach custom domain (`domain:create` then `domain:verify`)
Expand Down
Loading