wip
This commit is contained in:
@@ -5,8 +5,8 @@ namespace App\Jobs;
|
||||
use App\Enums\ApplicationDeploymentStatus;
|
||||
use App\Models\Application;
|
||||
use App\Models\ApplicationDeploymentQueue;
|
||||
use App\Models\Server;
|
||||
use App\Traits\ExecuteRemoteCommandNew;
|
||||
use Exception;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
@@ -21,77 +21,117 @@ class ApplicationDeployDockerImageJob implements ShouldQueue, ShouldBeEncrypted
|
||||
|
||||
public $timeout = 3600;
|
||||
public $tries = 1;
|
||||
public string $applicationDeploymentQueueId;
|
||||
public $remoteCommandOutputs = [];
|
||||
public Server $server;
|
||||
public string $containerName;
|
||||
|
||||
public function __construct(string $applicationDeploymentQueueId)
|
||||
public function __construct(public ApplicationDeploymentQueue $deploymentQueueEntry, public Application $application)
|
||||
{
|
||||
$this->applicationDeploymentQueueId = $applicationDeploymentQueueId;
|
||||
}
|
||||
public function handle()
|
||||
{
|
||||
ray()->clearAll();
|
||||
ray('Deploying Docker Image');
|
||||
static::$batch_counter = 0;
|
||||
try {
|
||||
$applicationDeploymentQueue = ApplicationDeploymentQueue::find($this->applicationDeploymentQueueId);
|
||||
$deploymentUuid = data_get($this->deploymentQueueEntry, 'deployment_uuid');
|
||||
$pullRequestId = data_get($this->deploymentQueueEntry, 'pull_request_id');
|
||||
|
||||
$deploymentUuid = data_get($applicationDeploymentQueue, 'deployment_uuid');
|
||||
$pullRequestId = data_get($applicationDeploymentQueue, 'pull_request_id');
|
||||
$this->server = data_get($this->application->destination, 'server');
|
||||
$network = data_get($this->application->destination, 'network');
|
||||
|
||||
$application = Application::find($applicationDeploymentQueue->application_id)->firstOrFail();
|
||||
$server = data_get($application->destination, 'server');
|
||||
$network = data_get($application->destination, 'network');
|
||||
|
||||
$dockerImage = data_get($application, 'docker_registry_image_name');
|
||||
$dockerImageTag = data_get($application, 'docker_registry_image_tag');
|
||||
$dockerImage = data_get($this->application, 'docker_registry_image_name');
|
||||
$dockerImageTag = data_get($this->application, 'docker_registry_image_tag');
|
||||
|
||||
$productionImageName = str("{$dockerImage}:{$dockerImageTag}");
|
||||
|
||||
$containerName = generateApplicationContainerName($application, $pullRequestId);
|
||||
savePrivateKeyToFs($server);
|
||||
$this->containerName = generateApplicationContainerName($this->application, $pullRequestId);
|
||||
savePrivateKeyToFs($this->server);
|
||||
|
||||
ray("echo 'Starting deployment of {$productionImageName}.'");
|
||||
|
||||
$applicationDeploymentQueue->update([
|
||||
$this->deploymentQueueEntry->update([
|
||||
'status' => ApplicationDeploymentStatus::IN_PROGRESS->value,
|
||||
]);
|
||||
$server->executeRemoteCommand(
|
||||
commands: prepareHelperContainer($server, $network, $deploymentUuid),
|
||||
loggingModel: $applicationDeploymentQueue
|
||||
|
||||
$this->deploymentQueueEntry->addLogEntry('Starting deployment of ' . $productionImageName);
|
||||
|
||||
$this->server->executeRemoteCommand(
|
||||
commands: collect(
|
||||
[
|
||||
[
|
||||
"name" => "ls",
|
||||
"command" => 'ls -la',
|
||||
"hidden" => true,
|
||||
],
|
||||
[
|
||||
"name" => "pwd",
|
||||
"command" => 'pwd',
|
||||
"hidden" => true,
|
||||
]
|
||||
],
|
||||
),
|
||||
loggingModel: $this->deploymentQueueEntry
|
||||
);
|
||||
$server->executeRemoteCommand(
|
||||
$this->server->executeRemoteCommand(
|
||||
commands: prepareHelperContainer($this->server, $network, $deploymentUuid),
|
||||
loggingModel: $this->deploymentQueueEntry
|
||||
);
|
||||
$this->server->executeRemoteCommand(
|
||||
commands: generateComposeFile(
|
||||
deploymentUuid: $deploymentUuid,
|
||||
server: $server,
|
||||
server: $this->server,
|
||||
network: $network,
|
||||
application: $application,
|
||||
containerName: $containerName,
|
||||
application: $this->application,
|
||||
containerName: $this->containerName,
|
||||
imageName: $productionImageName,
|
||||
pullRequestId: $pullRequestId
|
||||
),
|
||||
loggingModel: $applicationDeploymentQueue
|
||||
loggingModel: $this->deploymentQueueEntry
|
||||
);
|
||||
$server->executeRemoteCommand(
|
||||
commands: rollingUpdate(application: $application, deploymentUuid: $deploymentUuid),
|
||||
loggingModel: $applicationDeploymentQueue
|
||||
);
|
||||
$applicationDeploymentQueue->update([
|
||||
$this->deploymentQueueEntry->addLogEntry('----------------------------------------');
|
||||
|
||||
// Rolling update not possible
|
||||
if (count($this->application->ports_mappings_array) > 0) {
|
||||
$this->deploymentQueueEntry->addLogEntry('Application has ports mapped to the host system, rolling update is not supported.');
|
||||
$this->deploymentQueueEntry->addLogEntry('Stopping running container.');
|
||||
$this->server->stopApplicationRelatedRunningContainers($this->application->id, $this->containerName);
|
||||
} else {
|
||||
$this->deploymentQueueEntry->addLogEntry('Rolling update started.');
|
||||
// TODO
|
||||
$this->server->executeRemoteCommand(
|
||||
commands: startNewApplication(application: $this->application, deploymentUuid: $deploymentUuid, loggingModel: $this->deploymentQueueEntry),
|
||||
loggingModel: $this->deploymentQueueEntry
|
||||
);
|
||||
// $this->server->executeRemoteCommand(
|
||||
// commands: healthCheckContainer(application: $this->application, containerName: $this->containerName , loggingModel: $this->deploymentQueueEntry),
|
||||
// loggingModel: $this->deploymentQueueEntry
|
||||
// );
|
||||
|
||||
}
|
||||
|
||||
ray($this->remoteCommandOutputs);
|
||||
$this->deploymentQueueEntry->update([
|
||||
'status' => ApplicationDeploymentStatus::FINISHED->value,
|
||||
]);
|
||||
} catch (Throwable $e) {
|
||||
$this->executeRemoteCommand(
|
||||
server: $server,
|
||||
logModel: $applicationDeploymentQueue,
|
||||
commands: [
|
||||
"echo 'Oops something is not okay, are you okay? 😢'",
|
||||
"echo '{$e->getMessage()}'",
|
||||
"echo -n 'Deployment failed. Removing the new version of your application.'",
|
||||
executeInDocker($deploymentUuid, "docker rm -f $containerName >/dev/null 2>&1"),
|
||||
]
|
||||
);
|
||||
// $this->next(ApplicationDeploymentStatus::FAILED->value);
|
||||
$this->fail($e);
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
public function failed(Throwable $exception): void
|
||||
{
|
||||
$this->deploymentQueueEntry->addLogEntry('Oops something is not okay, are you okay? 😢', 'error');
|
||||
$this->deploymentQueueEntry->addLogEntry($exception->getMessage(), 'error');
|
||||
$this->deploymentQueueEntry->addLogEntry('Deployment failed. Removing the new version of your application.');
|
||||
|
||||
$this->server->executeRemoteCommand(
|
||||
commands: removeOldDeployment($this->containerName),
|
||||
loggingModel: $this->deploymentQueueEntry
|
||||
);
|
||||
$this->deploymentQueueEntry->update([
|
||||
'status' => ApplicationDeploymentStatus::FAILED->value,
|
||||
]);
|
||||
}
|
||||
// private function next(string $status)
|
||||
// {
|
||||
// // If the deployment is cancelled by the user, don't update the status
|
||||
|
||||
Reference in New Issue
Block a user