This commit is contained in:
Andras Bacsai
2023-03-30 15:52:19 +02:00
parent 7f3e8bdad9
commit da2f657342
15 changed files with 245 additions and 107 deletions

View File

@@ -70,22 +70,7 @@ class RunRemoteProcess
$port = $this->activity->getExtraProperty('port');
$command = $this->activity->getExtraProperty('command');
$delimiter = 'EOF-COOLIFY-SSH';
Storage::disk('local')->makeDirectory('.ssh');
$ssh_command = "ssh "
. "-i {$private_key_location} "
. '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '
. '-o PasswordAuthentication=no '
. '-o RequestTTY=no '
. '-o LogLevel=ERROR '
. '-o ControlMaster=auto -o ControlPersist=1m -o ControlPath=/var/www/html/storage/app/.ssh/ssh_mux_%h_%p_%r '
. "-p {$port} "
. "{$user}@{$server_ip} "
. " 'bash -se' << \\$delimiter" . PHP_EOL
. $command . PHP_EOL
. $delimiter;
return $ssh_command;
return generateSshCommand($private_key_location, $server_ip, $user, $port, $command);
}
protected function handleOutput(string $type, string $output)

View File

@@ -2,6 +2,7 @@
namespace App\Http\Livewire;
use App\Jobs\ContainerStatusJob;
use App\Models\Application;
use App\Models\CoolifyInstanceSettings;
use DateTimeImmutable;
@@ -220,7 +221,7 @@ class DeployApplication extends Component
$this->execute_in_builder("docker build -f {$workdir}/Dockerfile --build-arg SOURCE_COMMIT=$(cat {$workdir}/.git-commit) --progress plain -t {$this->application->uuid}:$(cat {$workdir}/.git-commit) {$workdir}");
$this->command[] = "echo 'Done.'";
$this->execute_in_builder("test ! -z \"$(docker ps --format '{{.State}}' --filter 'name={$this->application->uuid}')\" && docker rm -f {$this->application->uuid} >/dev/null 2>&1");
// $this->execute_in_builder("test ! -z \"$(docker ps --format '{{.State}}' --filter 'name={$this->application->uuid}')\" && docker rm -f {$this->application->uuid} >/dev/null 2>&1");
$this->command[] = "echo -n 'Deploying... '";
@@ -235,6 +236,7 @@ class DeployApplication extends Component
}
public function cancel()
{
ContainerStatusJob::dispatch();
}
public function render()
{

View File

@@ -0,0 +1,61 @@
<?php
namespace App\Jobs;
use App\Actions\RemoteProcess\RunRemoteProcess;
use App\Models\Application;
use App\Models\Server;
use App\Traits\Docker;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Process;
use Tests\Support\Output;
class ContainerStatusJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct()
{
//
}
public function handle(): void
{
try {
$server = Server::all()->where('settings->is_build_server', '=', false)->first();
$applications = Application::all();
$not_found_applications = $applications;
$containers = [];
// foreach ($servers as $server) {
$private_key_location = savePrivateKey($server);
$ssh_command = generateSshCommand($private_key_location, $server->ip, $server->user, $server->port, 'docker ps -a -q --format \'{{json .}}\'');
$process = Process::run($ssh_command);
$output = trim($process->output());
$containers = formatDockerCmdOutputToJson($output);
foreach ($containers as $container) {
$found_application = $applications->filter(function ($value, $key) use ($container) {
return $value->uuid == $container['Names'];
})->first();
if ($found_application) {
$not_found_applications = $not_found_applications->filter(function ($value, $key) use ($found_application) {
return $value->uuid != $found_application->uuid;
});
$found_application->status = $container['State'];
$found_application->save();
Log::info('Found application: ' . $found_application->uuid . ' settings status to: ' . $found_application->status);
}
}
foreach ($not_found_applications as $not_found_application) {
$not_found_application->status = 'exited';
$not_found_application->save();
Log::info('Not found application: ' . $not_found_application->uuid . ' settings status to: ' . $not_found_application->status);
}
} catch (\Exception $e) {
Log::error($e->getMessage());
}
}
}

View File

@@ -4,8 +4,20 @@ namespace App\Models;
class Server extends BaseModel
{
protected static function booted()
{
static::created(function ($server) {
ServerSetting::create([
'server_id' => $server->id,
]);
});
}
public function privateKey()
{
return $this->belongsTo(PrivateKey::class);
}
public function settings()
{
return $this->hasOne(ServerSetting::class);
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace App\Models;
class ServerSetting extends BaseModel
{
public function server()
{
return $this->belongsTo(Server::class);
}
}