2023-05-30 13:52:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2024-06-05 13:14:44 +00:00
|
|
|
use Spatie\Url\Url;
|
|
|
|
use Visus\Cuid2\Cuid2;
|
|
|
|
|
2023-05-30 13:52:17 +00:00
|
|
|
class ApplicationPreview extends BaseModel
|
|
|
|
{
|
2023-09-01 13:52:18 +00:00
|
|
|
protected $guarded = [];
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-11-27 14:25:15 +00:00
|
|
|
protected static function booted()
|
|
|
|
{
|
|
|
|
static::deleting(function ($preview) {
|
|
|
|
if ($preview->application->build_pack === 'dockercompose') {
|
|
|
|
$server = $preview->application->destination->server;
|
|
|
|
$composeFile = $preview->application->parseCompose(pull_request_id: $preview->pull_request_id);
|
|
|
|
$volumes = data_get($composeFile, 'volumes');
|
|
|
|
$networks = data_get($composeFile, 'networks');
|
|
|
|
$networkKeys = collect($networks)->keys();
|
|
|
|
$volumeKeys = collect($volumes)->keys();
|
|
|
|
$volumeKeys->each(function ($key) use ($server) {
|
|
|
|
instant_remote_process(["docker volume rm -f $key"], $server, false);
|
|
|
|
});
|
|
|
|
$networkKeys->each(function ($key) use ($server) {
|
|
|
|
instant_remote_process(["docker network disconnect $key coolify-proxy"], $server, false);
|
|
|
|
instant_remote_process(["docker network rm $key"], $server, false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
|
|
|
public static function findPreviewByApplicationAndPullId(int $application_id, int $pull_request_id)
|
2023-05-30 13:52:17 +00:00
|
|
|
{
|
|
|
|
return self::where('application_id', $application_id)->where('pull_request_id', $pull_request_id)->firstOrFail();
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
|
|
|
public function application()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Application::class);
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
|
|
|
public function generate_preview_fqdn_compose()
|
2024-06-05 13:14:44 +00:00
|
|
|
{
|
|
|
|
$domains = collect(json_decode($this->application->docker_compose_domains)) ?? collect();
|
|
|
|
foreach ($domains as $service_name => $domain) {
|
|
|
|
$domain = data_get($domain, 'domain');
|
|
|
|
$url = Url::fromString($domain);
|
|
|
|
$template = $this->application->preview_url_template;
|
|
|
|
$host = $url->getHost();
|
|
|
|
$schema = $url->getScheme();
|
|
|
|
$random = new Cuid2(7);
|
|
|
|
$preview_fqdn = str_replace('{{random}}', $random, $template);
|
|
|
|
$preview_fqdn = str_replace('{{domain}}', $host, $preview_fqdn);
|
|
|
|
$preview_fqdn = str_replace('{{pr_id}}', $this->pull_request_id, $preview_fqdn);
|
|
|
|
$preview_fqdn = "$schema://$preview_fqdn";
|
|
|
|
$docker_compose_domains = data_get($this, 'docker_compose_domains');
|
|
|
|
$docker_compose_domains = json_decode($docker_compose_domains, true);
|
|
|
|
$docker_compose_domains[$service_name]['domain'] = $preview_fqdn;
|
|
|
|
$docker_compose_domains = json_encode($docker_compose_domains);
|
|
|
|
$this->docker_compose_domains = $docker_compose_domains;
|
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
}
|
2023-05-30 13:52:17 +00:00
|
|
|
}
|