Skip to content
Open
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,14 @@ Many commands accept an optional resource ID/name and support `--json` for machi
| `cloud environment:metrics` | View environment compute and HTTP metrics |
| `cloud environment:purge-edge-cache` | Purge the edge cache (everything, or a path, prefix, or tag) |
| `cloud environment:vanity-domain` | Change the environment's Laravel Cloud domain |
| `cloud environment:primary-domain` | Set or clear the environment's primary domain |

`environment:purge-edge-cache` purges everything cached at the edge for the environment unless you pass one of `--path`, `--prefix`, or `--tag`. Paths and prefixes start with `/`; purging by tag needs a dedicated edge network. Cloud queues the purge, and repeating the same purge right away has no effect.

`environment:vanity-domain` takes only the first part of the domain: `--name=my-app` gives `my-app.laravel.cloud`. The new domain takes effect on the next deploy, and the domain can be changed only once every 30 minutes, so the command asks for confirmation (or `--force`). Afterward it offers to deploy the environment; pass `--deploy` to do that without asking.

`environment:primary-domain` makes one of the environment's custom domains its main address. Pass `--domain` with a domain ID or name, or `--clear` to remove the primary domain; clearing leaves the domain attached. Cloud accepts only a verified root domain that doesn't redirect to another domain.

### Secrets

Secrets are organization-wide encrypted values that can be attached to environments. Values are encrypted locally with the organization's public key before they are sent, so plaintext never leaves your machine.
Expand Down
2 changes: 2 additions & 0 deletions app/Client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ $data = $response->json();
- `DeleteEnvironmentVariablesRequest` - Delete environment variables by key
- `StartEnvironmentRequest` - Start an environment
- `StopEnvironmentRequest` - Stop an environment
- `UpdatePrimaryDomainRequest` - Set an environment's primary domain
- `ClearPrimaryDomainRequest` - Clear an environment's primary domain

#### Deployments
- `ListDeploymentsRequest` - List deployments for an environment
Expand Down
20 changes: 20 additions & 0 deletions app/Client/Requests/UpdateEnvironmentPrimaryDomainRequestData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Client\Requests;

class UpdateEnvironmentPrimaryDomainRequestData extends RequestData
{
public function __construct(
public readonly string $environmentId,
public readonly string $domainId,
) {
//
}

public function toRequestData(): array
{
return [
'domain_id' => $this->domainId,
];
}
}
5 changes: 4 additions & 1 deletion app/Client/Requests/UpdateEnvironmentRequestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function __construct(
public readonly ?string $websocketApplicationId = null,
/** @var string|null '' detaches it */
public readonly ?string $modelGatewayKeyId = null,
/** @var string|null '' removes it */
public readonly ?string $awsRoleArn = null,
public readonly ?bool $usesOctane = null,
public readonly ?int $sleepTimeout = null,
/** @var int|string|null '' disables it */
Expand Down Expand Up @@ -79,6 +81,7 @@ public function toRequestData(): array
'cache_id' => $this->cacheId,
'websocket_application_id' => $this->websocketApplicationId,
'model_gateway_key_id' => $this->modelGatewayKeyId,
'aws_role_arn' => $this->awsRoleArn,
'uses_octane' => $this->usesOctane,
'sleep_timeout' => $this->sleepTimeout,
'hibernation_wake_up_interval' => $this->hibernationWakeUpInterval,
Expand All @@ -99,7 +102,7 @@ public function toRequestData(): array
]);

// filter() drops nulls, so the CLI's '' stands in for the explicit null the API needs to clear these.
foreach (['start_command', 'hibernation_wake_up_interval', 'nightwatch_token'] as $key) {
foreach (['start_command', 'hibernation_wake_up_interval', 'nightwatch_token', 'aws_role_arn'] as $key) {
if (($data[$key] ?? null) === '') {
$data[$key] = null;
}
Expand Down
22 changes: 22 additions & 0 deletions app/Client/Resources/Environments/ClearPrimaryDomainRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Client\Resources\Environments;

use Saloon\Enums\Method;
use Saloon\Http\Request;

class ClearPrimaryDomainRequest extends Request
{
protected Method $method = Method::DELETE;

public function __construct(
protected string $environmentId,
) {
//
}

public function resolveEndpoint(): string
{
return "/environments/{$this->environmentId}/primary-domain";
}
}
32 changes: 32 additions & 0 deletions app/Client/Resources/Environments/UpdatePrimaryDomainRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Client\Resources\Environments;

use App\Client\Requests\UpdateEnvironmentPrimaryDomainRequestData;
use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Traits\Body\HasJsonBody;

class UpdatePrimaryDomainRequest extends Request implements HasBody
{
use HasJsonBody;

protected Method $method = Method::PUT;

public function __construct(
protected UpdateEnvironmentPrimaryDomainRequestData $data,
) {
//
}

public function resolveEndpoint(): string
{
return "/environments/{$this->data->environmentId}/primary-domain";
}

protected function defaultBody(): array
{
return $this->data->toRequestData();
}
}
13 changes: 13 additions & 0 deletions app/Client/Resources/EnvironmentsResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
use App\Client\Requests\PurgeEdgeCacheRequestData;
use App\Client\Requests\StartEnvironmentRequestData;
use App\Client\Requests\StopEnvironmentRequestData;
use App\Client\Requests\UpdateEnvironmentPrimaryDomainRequestData;
use App\Client\Requests\UpdateEnvironmentRequestData;
use App\Client\Requests\UpdateVanityDomainRequestData;
use App\Client\Resources\Environments\AddEnvironmentVariablesRequest;
use App\Client\Resources\Environments\AttachEnvironmentSecretsRequest;
use App\Client\Resources\Environments\ClearPrimaryDomainRequest;
use App\Client\Resources\Environments\CreateEnvironmentRequest;
use App\Client\Resources\Environments\DeleteEnvironmentRequest;
use App\Client\Resources\Environments\DeleteEnvironmentVariablesRequest;
Expand All @@ -25,6 +27,7 @@
use App\Client\Resources\Environments\StartEnvironmentRequest;
use App\Client\Resources\Environments\StopEnvironmentRequest;
use App\Client\Resources\Environments\UpdateEnvironmentRequest;
use App\Client\Resources\Environments\UpdatePrimaryDomainRequest;
use App\Client\Resources\Environments\UpdateVanityDomainRequest;
use App\Dto\Environment;
use App\Dto\EnvironmentLogPage;
Expand Down Expand Up @@ -145,6 +148,16 @@ public function updateVanityDomain(UpdateVanityDomainRequestData $data): Environ
return $request->createDtoFromResponse($response);
}

public function updatePrimaryDomain(UpdateEnvironmentPrimaryDomainRequestData $data): void
{
$this->send(new UpdatePrimaryDomainRequest($data));
}

public function clearPrimaryDomain(string $environmentId): void
{
$this->send(new ClearPrimaryDomainRequest($environmentId));
}

public function deleteVariables(DeleteEnvironmentVariablesRequestData $data): void
{
$this->send(new DeleteEnvironmentVariablesRequest($data));
Expand Down
157 changes: 157 additions & 0 deletions app/Commands/EnvironmentPrimaryDomain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

namespace App\Commands;

use App\Client\Requests\UpdateEnvironmentPrimaryDomainRequestData;
use App\Dto\Domain;
use App\Dto\Environment;
use Illuminate\Support\Collection;

use function Laravel\Prompts\intro;
use function Laravel\Prompts\select;
use function Laravel\Prompts\spin;
use function Laravel\Prompts\warning;

class EnvironmentPrimaryDomain extends BaseCommand
{
protected const NONE = 'none';

protected ?string $jsonDataClass = Environment::class;

protected $signature = 'environment:primary-domain
{environment? : The environment ID or name}
{--domain= : The domain ID or name to make primary}
{--clear : Clear the primary domain (the domain stays attached)}';

protected $description = "Set or clear an environment's primary domain";

protected $aliases = ['env:primary-domain'];

public function handle()
{
$this->ensureClient();

intro('Setting Primary Domain');

if ($this->option('clear') && $this->option('domain')) {
$this->failAndExit('Pass either --domain or --clear, not both.');
}

$environment = $this->resolvers()->environment()->from($this->argument('environment'));

if ($this->option('clear')) {
return $this->clear($environment);
}

$domains = $this->fetchList(
fn () => $this->client->domains()->list($environment->id),
'Fetching domains...',
);

$domain = $this->option('domain')
? $this->findDomain($environment, $domains, $this->option('domain'))
: $this->pickDomain($environment, $domains);

if (! $domain) {
return $this->clear($environment);
}

if ($domain->id === $environment->primaryDomainId) {
warning("{$domain->name} is already the primary domain of {$environment->name}.");

$this->outputJsonIfWanted($environment);

return self::SUCCESS;
}

$updated = spin(
function () use ($environment, $domain) {
$this->client->environments()->updatePrimaryDomain(
new UpdateEnvironmentPrimaryDomainRequestData(
environmentId: $environment->id,
domainId: $domain->id,
),
);

return $this->client->environments()->get($environment->id);
},
'Setting primary domain...',
);

$this->outputJsonIfWanted($updated);

success("{$domain->name} is now the primary domain of {$updated->name}.");

return self::SUCCESS;
}

protected function clear(Environment $environment): int
{
if (! $environment->primaryDomainId) {
warning("{$environment->name} has no primary domain.");

$this->outputJsonIfWanted($environment);

return self::SUCCESS;
}

$updated = spin(
function () use ($environment) {
$this->client->environments()->clearPrimaryDomain($environment->id);

return $this->client->environments()->get($environment->id);
},
'Clearing primary domain...',
);

$this->outputJsonIfWanted($updated);

success("Primary domain cleared for {$updated->name}.");

return self::SUCCESS;
}

/**
* @param Collection<int, Domain> $domains
*/
protected function findDomain(Environment $environment, Collection $domains, string $idOrName): Domain
{
$domain = $domains->firstWhere('id', $idOrName) ?? $domains->firstWhere('name', $idOrName);

if (! $domain) {
$this->failAndExit("Domain {$idOrName} is not attached to {$environment->name}. Run `cloud domain:list {$environment->id} --json` to see its domains.");
}

return $domain;
}

/**
* Only a root domain can be primary, so the others aren't offered. Returns null to clear.
*
* @param Collection<int, Domain> $domains
*/
protected function pickDomain(Environment $environment, Collection $domains): ?Domain
{
$this->ensureInteractive('Provide a domain with --domain, or pass --clear.');

$options = $domains
->filter(fn (Domain $domain) => $domain->type === 'root')
->mapWithKeys(fn (Domain $domain) => [$domain->id => $domain->name]);

if ($environment->primaryDomainId) {
$options->put(self::NONE, 'None (clear the primary domain)');
}

if ($options->isEmpty()) {
$this->failAndExit("{$environment->name} has no root domains to make primary. Add one with `cloud domain:create`.");
}

$selected = select(
label: 'Primary domain',
options: $options->all(),
default: $environment->primaryDomainId ?? $options->keys()->first(),
);

return $selected === self::NONE ? null : $domains->firstWhere('id', $selected);
}
}
17 changes: 17 additions & 0 deletions app/Commands/EnvironmentUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class EnvironmentUpdate extends BaseCommand
{--cache-id= : Cache ID to attach (empty string to detach)}
{--websocket-application-id= : WebSocket application ID to attach (empty string to detach)}
{--model-gateway-key-id= : Model gateway key ID to attach, exposed as LARAVEL_CLOUD_MODEL_GATEWAY_API_KEY from the next deployment (empty string to detach)}
{--aws-role-arn= : IAM role ARN the application assumes, on a dedicated cluster with bring your own IAM role (empty string to remove)}
{--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}';

Expand Down Expand Up @@ -130,6 +131,7 @@ protected function updateEnvironment(Environment $environment): Environment
cacheId: $this->form()->get('cache_id'),
websocketApplicationId: $this->form()->get('websocket_application_id'),
modelGatewayKeyId: $this->form()->get('model_gateway_key_id'),
awsRoleArn: $this->form()->get('aws_role_arn'),
usesOctane: $this->form()->boolean('uses_octane'),
sleepTimeout: $this->form()->integer('sleep_timeout'),
hibernationWakeUpInterval: $this->clearableInteger('hibernation_wake_up_interval'),
Expand Down Expand Up @@ -451,6 +453,21 @@ protected function defineFields(Environment $environment): void
'model-gateway-key-id',
)->setPreviousValue($environment->modelGatewayKeyId ?? '')->setLabel('Model gateway key');

// Only an environment on a dedicated cluster can assume a role of its own, so the others aren't offered it.
if ($environment->clusterId !== null || $this->option('aws-role-arn') !== null) {
$this->form()->define(
'aws_role_arn',
fn ($resolver) => $resolver->fromInput(
fn ($value) => text(
label: 'AWS role ARN',
default: $value ?? $environment->awsRoleArn ?? '',
hint: 'Leave empty to remove the role',
),
),
'aws-role-arn',
)->setPreviousValue($environment->awsRoleArn ?? '')->setLabel('AWS role ARN');
}

// Named after the API field so a validation error on filesystem_keys.* asks for the buckets again.
$this->form()->define(
'filesystem_keys',
Expand Down
8 changes: 8 additions & 0 deletions app/Dto/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public function __construct(
public readonly ?string $postDeployCommand = null,
public readonly bool $usesHttpBasicAuth = false,
public readonly ?EnvironmentInternalNetworking $internalNetworking = null,
/** Null on shared compute. */
public readonly ?string $clusterId = null,
/** The API sends it only for an environment on a dedicated cluster, in an organization with bring your own IAM role. */
public readonly ?string $awsRoleArn = null,
public readonly ?string $awsTrustPolicy = null,
) {
//
}
Expand Down Expand Up @@ -115,6 +120,9 @@ public static function createFromResponse(array $response): self
'postDeployCommand' => $attributes['post_deploy_command'] ?? null,
'usesHttpBasicAuth' => $attributes['uses_http_basic_auth'] ?? false,
'internalNetworking' => isset($attributes['internal_networking']) ? EnvironmentInternalNetworking::createFromResponse($attributes['internal_networking']) : null,
'clusterId' => $attributes['cluster_id'] ?? null,
'awsRoleArn' => $attributes['aws_role_arn'] ?? null,
'awsTrustPolicy' => $attributes['aws_trust_policy'] ?? null,
'resend' => isset($attributes['resend']) ? EnvironmentResend::createFromResponse($attributes['resend']) : null,
];

Expand Down
1 change: 1 addition & 0 deletions skills/deploying-to-cloud/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Determine the task and follow the matching path:
- Subdomain (`app.example.com`): pass `--www-redirect=none`. Nobody serves `www.app.example.com`, so a www variant never validates.
- Apex (`example.com`): ask the user whether www redirects to the root (`www_to_root`) or the reverse (`root_to_www`).
- Leave `--wildcard-enabled` off unless the user asks for wildcard subdomains.
- Primary domain: once a root domain is verified, `cloud environment:primary-domain {environment} --domain=<domain> -n` makes it the environment's main address; `--clear` removes it and leaves the domain attached.

- Edge cache: `cloud environment:purge-edge-cache {environment} -n` purges everything; pass one of `--path=/blog/post`, `--prefix=/blog/`, or `--tag=<cache-tag>` to purge less. Tags need a dedicated edge network.

Expand Down
Loading