Skip to content
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
83 changes: 29 additions & 54 deletions app/Commands/DeployCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DeployCommand extends Command
{
use EnsureHasToken, HasPloiConfiguration, InteractWithServer, InteractWithSite;

protected $signature = 'deploy {--server=} {--site=} {--schedule=} {--stream : Stream deployment logs in real-time} {--deployment-id= : Specific deployment ID to stream}';
protected $signature = 'deploy {--server=} {--site=} {--schedule=} {--no-stream : Disable real-time deployment log streaming}';

protected $description = 'Deploy your site to Ploi.io with optional log streaming.';

Expand All @@ -34,7 +34,7 @@ public function handle(): void
$data = [];
$isScheduled = false;

if ($this->ploi->getSiteDetails($serverId, $siteId)['data']['has_staging']) {
if ($this->site['has_staging']) {
$this->warn("{$this->site['domain']} has a staging environment.");
$deployToProduction = confirm(
label: 'Do you want to deploy to production? (yes/no)',
Expand All @@ -58,17 +58,6 @@ public function handle(): void
}

$this->deploy($serverId, $siteId, $this->site['domain'], $data, $isScheduled);

// Handle streaming after deployment
if ($this->option('stream') && ! $isScheduled) {
$deploymentId = $this->option('deployment-id') ?? $this->getLatestDeploymentId($serverId, $siteId);

if ($deploymentId) {
$this->streamDeploymentLogs($serverId, $siteId, $deploymentId);
} else {
$this->error('No deployment found to stream');
}
}
}

protected function validateScheduleDatetime(string $datetime): void
Expand Down Expand Up @@ -106,21 +95,27 @@ protected function deploy($serverId, $siteId, $domain, $data, $isScheduled = fal
return;
}

$this->pollDeploymentStatus($serverId, $siteId, $domain);
if ($this->option('no-stream')) {
$this->pollDeploymentStatus($serverId, $siteId, $domain);

return;
}

$this->streamAndAwait($serverId, $siteId, $domain);
}

protected function pollDeploymentStatus(string $serverId, string $siteId, string $domain): void
{
$maxAttempts = 60; // Maximum number of polling attempts (10 minutes total with 10-second delay)
$delay = 5; // Delay between each attempt in seconds
$maxAttempts = 60; // Maximum number of polling attempts (5 minutes total with 5-second delay)
$delay = 5; // Delay between each attempt in seconds

$this->info('Deployment initiated!');

$status = spin(
callback: function () use ($serverId, $siteId, $domain, $maxAttempts, $delay) {
for ($attempt = 1; $attempt <= $maxAttempts; $attempt++) {
$deploymentStatus = $this->ploi->getSiteDetails($serverId, $siteId)['data']['status'] ?? 'deploying';

// If we get a final status, return it
if (in_array($deploymentStatus, ['active', 'deploy-failed'])) {
return [
'status' => $deploymentStatus,
Expand All @@ -131,7 +126,6 @@ protected function pollDeploymentStatus(string $serverId, string $siteId, string
sleep($delay);
}

// If we've exceeded max attempts, return timeout status
return [
'status' => 'timeout',
'domain' => $domain,
Expand All @@ -140,59 +134,41 @@ protected function pollDeploymentStatus(string $serverId, string $siteId, string
message: 'Checking deployment status...'
);

// Handle the deployment result
match ($status['status']) {
'active' => $this->handleSuccessfulDeployment($serverId, $siteId, $status['domain']),
'deploy-failed' => $this->handleFailedDeployment($serverId, $siteId),
'timeout' => $this->warn('Deployment status check timed out. Please check manually.'),
default => $this->warn('Deployment status is unknown. Please check manually.')
default => $this->warn('Deployment status is unknown. Please check manually.'),
};
}

/**
* Stream deployment logs in real-time
*
* @return void
*/
private function streamDeploymentLogs(int $serverId, int $siteId, int $deploymentId)
private function streamAndAwait(int $serverId, int $siteId, string $domain): void
{
$this->info('🔄 Streaming deployment logs...');
$this->newLine();

$poller = new DeploymentLogPoller(config('ploi.token'));
$poller = new DeploymentLogPoller($this->ploi);

try {
$poller->pollDeploymentLogs($serverId, $siteId, $deploymentId, function ($line) {
// Format the log line with timestamp
$status = $poller->pollDeploymentLogs($serverId, $siteId, function ($line) {
$timestamp = now()->format('H:i:s');
$this->line("<fg=gray>[{$timestamp}]</fg=gray> {$line}");
$this->line("<fg=gray>[{$timestamp}]</> {$line}");
});

$this->newLine();
$this->success('✅ Deployment streaming completed!');

} catch (Exception $e) {
$this->newLine();
$this->error('❌ Streaming failed: '.$e->getMessage());
}
}
$this->warn('Log streaming failed ('.$e->getMessage().'), falling back to status checks...');
$this->pollDeploymentStatus($serverId, $siteId, $domain);

/**
* Get the latest deployment ID for streaming
*/
private function getLatestDeploymentId(int $serverId, int $siteId): ?int
{
$poller = new DeploymentLogPoller(config('ploi.token'));

try {
$deployment = $poller->getLatestDeployment($serverId, $siteId);
return;
}

return isset($deployment['id']) ? (int) $deployment['id'] : null;
} catch (Exception $e) {
$this->error('Failed to get latest deployment: '.$e->getMessage());
$this->newLine();

return null;
}
match ($status) {
'active' => $this->handleSuccessfulDeployment($serverId, $siteId, $domain),
'deploy-failed' => $this->handleFailedDeployment($serverId, $siteId),
default => $this->warn('Deployment status check timed out. Please check manually.'),
};
}

/**
Expand All @@ -210,8 +186,8 @@ private function handleFailedDeployment(string $serverId, string $siteId): void
{
$this->error('Your recent deployment has failed, please check recent deploy log for errors.');

// Show link to deployment logs if not streaming
if (! $this->option('stream')) {
// When streaming, the failed log output is already shown inline.
if ($this->option('no-stream')) {
$this->showLogLink($serverId, $siteId);
}
}
Expand All @@ -222,7 +198,6 @@ private function handleFailedDeployment(string $serverId, string $siteId): void
private function showLogLink(string $serverId, string $siteId): void
{
try {
// Get the latest deployment log ID
$logs = $this->ploi->getSiteLogs($serverId, $siteId, 1)['data'];

if (! empty($logs)) {
Expand Down
38 changes: 18 additions & 20 deletions app/Commands/LogsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class LogsCommand extends Command
{
use EnsureHasToken;

protected $signature = 'logs:stream {server} {site} {--deployment-id= : Specific deployment ID to stream}';
protected $signature = 'logs:stream {server} {site}';

protected $description = 'Stream deployment logs for a specific site';

Expand All @@ -20,39 +20,37 @@ public function handle(): void

$serverId = (int) $this->argument('server');
$siteId = (int) $this->argument('site');
$deploymentId = $this->option('deployment-id');

$poller = new DeploymentLogPoller(config('ploi.token'));

try {
// If no deployment ID provided, get the latest or active deployment
if (! $deploymentId) {
$deployment = $poller->getActiveDeployment($serverId, $siteId)
?? $poller->getLatestDeployment($serverId, $siteId);
$site = $this->ploi->getSiteDetails($serverId, $siteId)['data'] ?? null;

if (! $site) {
$this->error('Site not found.');

return;
}

if (! $deployment) {
$this->error('No deployment found for this site');
$currentLog = $site['current_deploy_log'] ?? null;
$status = $site['status'] ?? null;

return;
}
if ($currentLog === null && $status !== 'deploying') {
$this->info('No deployment in progress for this site.');

$deploymentId = $deployment['id'];
$this->info("Streaming logs for deployment #{$deploymentId} (status: {$deployment['status']})");
} else {
$this->info("Streaming logs for deployment #{$deploymentId}");
return;
}

$this->info('🔄 Streaming deployment logs... (Press Ctrl+C to stop)');
$this->info("🔄 Streaming deployment logs for {$site['domain']}... (Press Ctrl+C to stop)");
$this->newLine();

$poller->pollDeploymentLogs($serverId, $siteId, $deploymentId, function ($line) {
$poller = new DeploymentLogPoller($this->ploi);

$poller->pollDeploymentLogs($serverId, $siteId, function ($line) {
$timestamp = now()->format('H:i:s');
$this->line("<fg=gray>[{$timestamp}]</fg=gray> {$line}");
$this->line("<fg=gray>[{$timestamp}]</> {$line}");
});

$this->newLine();
$this->success('✅ Deployment log streaming completed!');

} catch (Exception $e) {
$this->newLine();
$this->error('❌ Streaming failed: '.$e->getMessage());
Expand Down
Loading