2023-05-24 12:26:50 +00:00
|
|
|
<?php
|
|
|
|
|
2023-10-20 12:51:01 +00:00
|
|
|
use App\Models\Application;
|
2023-11-27 13:28:21 +00:00
|
|
|
use App\Models\ApplicationPreview;
|
2023-11-24 14:48:23 +00:00
|
|
|
use App\Models\EnvironmentVariable;
|
2023-06-12 10:00:01 +00:00
|
|
|
use App\Models\InstanceSettings;
|
2023-11-24 14:48:23 +00:00
|
|
|
use App\Models\LocalFileVolume;
|
|
|
|
use App\Models\LocalPersistentVolume;
|
2023-09-28 08:53:00 +00:00
|
|
|
use App\Models\Server;
|
2023-10-20 12:51:01 +00:00
|
|
|
use App\Models\Service;
|
2023-11-24 14:48:23 +00:00
|
|
|
use App\Models\ServiceApplication;
|
|
|
|
use App\Models\ServiceDatabase;
|
2023-10-25 08:43:07 +00:00
|
|
|
use App\Models\StandaloneMariadb;
|
2023-10-20 12:51:01 +00:00
|
|
|
use App\Models\StandaloneMongodb;
|
2023-10-25 08:43:07 +00:00
|
|
|
use App\Models\StandaloneMysql;
|
2023-10-20 12:51:01 +00:00
|
|
|
use App\Models\StandalonePostgresql;
|
|
|
|
use App\Models\StandaloneRedis;
|
2023-08-16 14:03:30 +00:00
|
|
|
use App\Models\Team;
|
2023-09-14 16:22:08 +00:00
|
|
|
use App\Models\User;
|
2023-09-06 12:31:38 +00:00
|
|
|
use App\Notifications\Channels\DiscordChannel;
|
|
|
|
use App\Notifications\Channels\EmailChannel;
|
|
|
|
use App\Notifications\Channels\TelegramChannel;
|
2023-08-16 14:03:30 +00:00
|
|
|
use App\Notifications\Internal\GeneralNotification;
|
|
|
|
use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException;
|
2023-05-24 12:26:50 +00:00
|
|
|
use Illuminate\Database\QueryException;
|
2023-08-17 13:19:37 +00:00
|
|
|
use Illuminate\Mail\Message;
|
|
|
|
use Illuminate\Notifications\Messages\MailMessage;
|
2023-09-27 10:45:53 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2023-09-15 09:19:36 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2023-10-04 12:40:26 +00:00
|
|
|
use Illuminate\Support\Facades\File;
|
2023-05-24 12:26:50 +00:00
|
|
|
use Illuminate\Support\Facades\Http;
|
2023-12-11 19:01:54 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2023-08-17 13:19:37 +00:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
2023-12-11 19:01:54 +00:00
|
|
|
use Illuminate\Support\Facades\Request;
|
2023-05-24 12:26:50 +00:00
|
|
|
use Illuminate\Support\Facades\Route;
|
|
|
|
use Illuminate\Support\Str;
|
2023-09-27 10:45:53 +00:00
|
|
|
use Illuminate\Support\Stringable;
|
2023-08-10 13:52:54 +00:00
|
|
|
use Poliander\Cron\CronExpression;
|
2023-08-08 09:51:36 +00:00
|
|
|
use Visus\Cuid2\Cuid2;
|
2023-08-22 15:44:49 +00:00
|
|
|
use phpseclib3\Crypt\RSA;
|
2023-09-22 12:47:25 +00:00
|
|
|
use Spatie\Url\Url;
|
2023-11-24 14:48:23 +00:00
|
|
|
use Symfony\Component\Yaml\Yaml;
|
2023-05-24 12:26:50 +00:00
|
|
|
|
2023-09-21 15:48:31 +00:00
|
|
|
function base_configuration_dir(): string
|
|
|
|
{
|
|
|
|
return '/data/coolify';
|
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
function application_configuration_dir(): string
|
|
|
|
{
|
2023-09-21 15:48:31 +00:00
|
|
|
return base_configuration_dir() . "/applications";
|
|
|
|
}
|
|
|
|
function service_configuration_dir(): string
|
|
|
|
{
|
|
|
|
return base_configuration_dir() . "/services";
|
2023-08-09 12:44:36 +00:00
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
function database_configuration_dir(): string
|
|
|
|
{
|
2023-09-21 15:48:31 +00:00
|
|
|
return base_configuration_dir() . "/databases";
|
2023-08-09 12:44:36 +00:00
|
|
|
}
|
2023-09-07 11:23:34 +00:00
|
|
|
function database_proxy_dir($uuid): string
|
|
|
|
{
|
2023-09-21 15:48:31 +00:00
|
|
|
return base_configuration_dir() . "/databases/$uuid/proxy";
|
2023-09-07 11:23:34 +00:00
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
function backup_dir(): string
|
|
|
|
{
|
2023-09-21 15:48:31 +00:00
|
|
|
return base_configuration_dir() . "/backups";
|
2023-08-09 12:44:36 +00:00
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
|
|
|
|
function generate_readme_file(string $name, string $updated_at): string
|
|
|
|
{
|
|
|
|
return "Resource name: $name\nLatest Deployment Date: $updated_at";
|
2023-08-09 12:44:36 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 15:44:49 +00:00
|
|
|
function isInstanceAdmin()
|
2023-08-14 14:56:13 +00:00
|
|
|
{
|
2023-08-22 15:44:49 +00:00
|
|
|
return auth()?->user()?->isInstanceAdmin() ?? false;
|
2023-08-14 14:56:13 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 15:44:49 +00:00
|
|
|
function currentTeam()
|
|
|
|
{
|
|
|
|
return auth()?->user()?->currentTeam() ?? null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function showBoarding(): bool
|
|
|
|
{
|
2023-08-27 13:44:36 +00:00
|
|
|
return currentTeam()->show_boarding ?? false;
|
2023-08-22 15:44:49 +00:00
|
|
|
}
|
2023-09-08 16:33:26 +00:00
|
|
|
function refreshSession(?Team $team = null): void
|
2023-08-22 15:44:49 +00:00
|
|
|
{
|
2023-09-08 16:33:26 +00:00
|
|
|
if (!$team) {
|
2023-09-15 15:30:26 +00:00
|
|
|
if (auth()->user()?->currentTeam()) {
|
2023-09-15 09:28:44 +00:00
|
|
|
$team = Team::find(auth()->user()->currentTeam()->id);
|
|
|
|
} else {
|
|
|
|
$team = User::find(auth()->user()->id)->teams->first();
|
|
|
|
}
|
2023-09-08 16:33:26 +00:00
|
|
|
}
|
|
|
|
Cache::forget('team:' . auth()->user()->id);
|
2023-09-15 13:34:25 +00:00
|
|
|
Cache::remember('team:' . auth()->user()->id, 3600, function () use ($team) {
|
2023-09-08 16:33:26 +00:00
|
|
|
return $team;
|
|
|
|
});
|
2023-08-30 14:01:38 +00:00
|
|
|
session(['currentTeam' => $team]);
|
2023-08-22 15:44:49 +00:00
|
|
|
}
|
2023-09-15 13:34:25 +00:00
|
|
|
function handleError(?Throwable $error = null, ?Livewire\Component $livewire = null, ?string $customErrorMessage = null)
|
|
|
|
{
|
2023-11-20 09:32:06 +00:00
|
|
|
if ($error instanceof TooManyRequestsException) {
|
|
|
|
if (isset($livewire)) {
|
2023-12-07 18:06:32 +00:00
|
|
|
return $livewire->dispatch('error', "Too many requests. Please try again in {$error->secondsUntilAvailable} seconds.");
|
2023-11-20 09:32:06 +00:00
|
|
|
}
|
|
|
|
return "Too many requests. Please try again in {$error->secondsUntilAvailable} seconds.";
|
|
|
|
}
|
|
|
|
|
2023-09-15 13:34:25 +00:00
|
|
|
if ($error instanceof Throwable) {
|
|
|
|
$message = $error->getMessage();
|
|
|
|
} else {
|
|
|
|
$message = null;
|
|
|
|
}
|
|
|
|
if ($customErrorMessage) {
|
|
|
|
$message = $customErrorMessage . ' ' . $message;
|
|
|
|
}
|
2023-11-20 09:32:06 +00:00
|
|
|
|
2023-09-15 13:34:25 +00:00
|
|
|
if (isset($livewire)) {
|
2023-12-07 18:06:32 +00:00
|
|
|
return $livewire->dispatch('error', $message);
|
2023-09-15 13:34:25 +00:00
|
|
|
}
|
2023-11-21 11:07:06 +00:00
|
|
|
throw new Exception($message);
|
2023-05-24 12:26:50 +00:00
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
function get_route_parameters(): array
|
2023-05-24 12:26:50 +00:00
|
|
|
{
|
|
|
|
return Route::current()->parameters();
|
|
|
|
}
|
|
|
|
|
2023-08-09 13:57:53 +00:00
|
|
|
function get_latest_version_of_coolify(): string
|
2023-05-24 12:26:50 +00:00
|
|
|
{
|
2023-06-19 13:43:53 +00:00
|
|
|
try {
|
|
|
|
$response = Http::get('https://cdn.coollabs.io/coolify/versions.json');
|
|
|
|
$versions = $response->json();
|
|
|
|
return data_get($versions, 'coolify.v4.version');
|
2023-09-15 13:34:25 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-11 15:36:30 +00:00
|
|
|
//throw $e;
|
|
|
|
ray($e->getMessage());
|
2023-06-19 13:43:53 +00:00
|
|
|
return '0.0.0';
|
|
|
|
}
|
2023-05-24 12:26:50 +00:00
|
|
|
}
|
|
|
|
|
2023-09-19 13:51:13 +00:00
|
|
|
function generate_random_name(?string $cuid = null): string
|
2023-05-24 12:26:50 +00:00
|
|
|
{
|
2023-11-13 10:44:13 +00:00
|
|
|
$generator = new \Nubs\RandomNameGenerator\All(
|
|
|
|
[
|
|
|
|
new \Nubs\RandomNameGenerator\Alliteration(),
|
|
|
|
]
|
|
|
|
);
|
2023-09-19 13:51:13 +00:00
|
|
|
if (is_null($cuid)) {
|
|
|
|
$cuid = new Cuid2(7);
|
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
return Str::kebab("{$generator->getName()}-$cuid");
|
2023-05-24 12:26:50 +00:00
|
|
|
}
|
2023-08-22 15:44:49 +00:00
|
|
|
function generateSSHKey()
|
|
|
|
{
|
|
|
|
$key = RSA::createKey();
|
|
|
|
return [
|
|
|
|
'private' => $key->toString('PKCS1'),
|
2023-08-31 13:00:59 +00:00
|
|
|
'public' => $key->getPublicKey()->toString('OpenSSH', ['comment' => 'coolify-generated-ssh-key'])
|
2023-08-22 15:44:49 +00:00
|
|
|
];
|
|
|
|
}
|
2023-08-31 13:00:59 +00:00
|
|
|
function formatPrivateKey(string $privateKey)
|
|
|
|
{
|
2023-08-22 15:44:49 +00:00
|
|
|
$privateKey = trim($privateKey);
|
|
|
|
if (!str_ends_with($privateKey, "\n")) {
|
|
|
|
$privateKey .= "\n";
|
|
|
|
}
|
|
|
|
return $privateKey;
|
|
|
|
}
|
2023-09-19 13:51:13 +00:00
|
|
|
function generate_application_name(string $git_repository, string $git_branch, ?string $cuid = null): string
|
2023-05-24 13:47:04 +00:00
|
|
|
{
|
2023-09-19 13:51:13 +00:00
|
|
|
if (is_null($cuid)) {
|
|
|
|
$cuid = new Cuid2(7);
|
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
return Str::kebab("$git_repository:$git_branch-$cuid");
|
2023-05-24 13:47:04 +00:00
|
|
|
}
|
2023-06-12 10:00:01 +00:00
|
|
|
|
2023-08-09 13:57:53 +00:00
|
|
|
function is_transactional_emails_active(): bool
|
2023-06-12 10:00:01 +00:00
|
|
|
{
|
2023-08-31 13:00:59 +00:00
|
|
|
return isEmailEnabled(InstanceSettings::get());
|
2023-06-12 10:00:01 +00:00
|
|
|
}
|
2023-06-12 14:39:48 +00:00
|
|
|
|
2023-08-31 13:00:59 +00:00
|
|
|
function set_transanctional_email_settings(InstanceSettings | null $settings = null): string|null
|
2023-06-12 14:39:48 +00:00
|
|
|
{
|
2023-08-15 12:11:38 +00:00
|
|
|
if (!$settings) {
|
|
|
|
$settings = InstanceSettings::get();
|
|
|
|
}
|
2023-09-02 11:39:44 +00:00
|
|
|
config()->set('mail.from.address', data_get($settings, 'smtp_from_address'));
|
|
|
|
config()->set('mail.from.name', data_get($settings, 'smtp_from_name'));
|
2023-08-31 13:00:59 +00:00
|
|
|
if (data_get($settings, 'resend_enabled')) {
|
|
|
|
config()->set('mail.default', 'resend');
|
|
|
|
config()->set('resend.api_key', data_get($settings, 'resend_api_key'));
|
|
|
|
return 'resend';
|
2023-08-09 13:57:53 +00:00
|
|
|
}
|
2023-08-31 13:00:59 +00:00
|
|
|
if (data_get($settings, 'smtp_enabled')) {
|
|
|
|
config()->set('mail.default', 'smtp');
|
|
|
|
config()->set('mail.mailers.smtp', [
|
|
|
|
"transport" => "smtp",
|
|
|
|
"host" => data_get($settings, 'smtp_host'),
|
|
|
|
"port" => data_get($settings, 'smtp_port'),
|
|
|
|
"encryption" => data_get($settings, 'smtp_encryption'),
|
|
|
|
"username" => data_get($settings, 'smtp_username'),
|
|
|
|
"password" => data_get($settings, 'smtp_password'),
|
|
|
|
"timeout" => data_get($settings, 'smtp_timeout'),
|
|
|
|
"local_domain" => null,
|
|
|
|
]);
|
|
|
|
return 'smtp';
|
|
|
|
}
|
|
|
|
return null;
|
2023-06-12 14:39:48 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-08-09 13:57:53 +00:00
|
|
|
function base_ip(): string
|
2023-06-16 11:37:02 +00:00
|
|
|
{
|
2023-08-27 13:23:47 +00:00
|
|
|
if (isDev()) {
|
2023-08-15 13:39:15 +00:00
|
|
|
return "localhost";
|
2023-07-28 11:40:47 +00:00
|
|
|
}
|
2023-06-16 11:35:35 +00:00
|
|
|
$settings = InstanceSettings::get();
|
2023-08-15 13:39:15 +00:00
|
|
|
if ($settings->public_ipv4) {
|
|
|
|
return "$settings->public_ipv4";
|
|
|
|
}
|
|
|
|
if ($settings->public_ipv6) {
|
|
|
|
return "$settings->public_ipv6";
|
|
|
|
}
|
|
|
|
return "localhost";
|
2023-06-16 11:35:35 +00:00
|
|
|
}
|
2023-09-24 15:39:12 +00:00
|
|
|
function getFqdnWithoutPort(String $fqdn)
|
2023-09-24 10:10:36 +00:00
|
|
|
{
|
2023-09-22 12:47:25 +00:00
|
|
|
$url = Url::fromString($fqdn);
|
|
|
|
$host = $url->getHost();
|
|
|
|
$scheme = $url->getScheme();
|
2023-09-24 15:39:12 +00:00
|
|
|
$path = $url->getPath();
|
|
|
|
return "$scheme://$host$path";
|
2023-09-22 12:47:25 +00:00
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
/**
|
|
|
|
* If fqdn is set, return it, otherwise return public ip.
|
|
|
|
*/
|
|
|
|
function base_url(bool $withPort = true): string
|
2023-06-13 13:01:11 +00:00
|
|
|
{
|
|
|
|
$settings = InstanceSettings::get();
|
|
|
|
if ($settings->fqdn) {
|
|
|
|
return $settings->fqdn;
|
|
|
|
}
|
2023-06-14 07:05:13 +00:00
|
|
|
$port = config('app.port');
|
|
|
|
if ($settings->public_ipv4) {
|
2023-06-16 09:01:27 +00:00
|
|
|
if ($withPort) {
|
2023-08-27 13:23:47 +00:00
|
|
|
if (isDev()) {
|
2023-08-09 13:57:53 +00:00
|
|
|
return "http://localhost:$port";
|
2023-06-16 09:01:27 +00:00
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
return "http://$settings->public_ipv4:$port";
|
2023-06-16 09:01:27 +00:00
|
|
|
}
|
2023-08-27 13:23:47 +00:00
|
|
|
if (isDev()) {
|
2023-06-16 09:01:27 +00:00
|
|
|
return "http://localhost";
|
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
return "http://$settings->public_ipv4";
|
2023-06-14 07:05:13 +00:00
|
|
|
}
|
|
|
|
if ($settings->public_ipv6) {
|
2023-06-16 09:01:27 +00:00
|
|
|
if ($withPort) {
|
2023-08-09 13:57:53 +00:00
|
|
|
return "http://$settings->public_ipv6:$port";
|
2023-06-16 09:01:27 +00:00
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
return "http://$settings->public_ipv6";
|
2023-06-14 07:05:13 +00:00
|
|
|
}
|
|
|
|
return url('/');
|
2023-06-13 13:01:11 +00:00
|
|
|
}
|
2023-06-16 09:01:27 +00:00
|
|
|
|
2023-08-27 13:23:47 +00:00
|
|
|
function isDev(): bool
|
2023-06-16 09:01:27 +00:00
|
|
|
{
|
|
|
|
return config('app.env') === 'local';
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-08-31 07:56:37 +00:00
|
|
|
function isCloud(): bool
|
2023-07-13 13:07:42 +00:00
|
|
|
{
|
2023-07-14 10:09:56 +00:00
|
|
|
return !config('coolify.self_hosted');
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
|
2023-08-10 13:52:54 +00:00
|
|
|
function validate_cron_expression($expression_to_validate): bool
|
|
|
|
{
|
|
|
|
$isValid = false;
|
|
|
|
$expression = new CronExpression($expression_to_validate);
|
|
|
|
$isValid = $expression->isValid();
|
|
|
|
|
|
|
|
if (isset(VALID_CRON_STRINGS[$expression_to_validate])) {
|
|
|
|
$isValid = true;
|
|
|
|
}
|
|
|
|
return $isValid;
|
|
|
|
}
|
2023-08-16 14:03:30 +00:00
|
|
|
function send_internal_notification(string $message): void
|
|
|
|
{
|
|
|
|
try {
|
2023-09-06 10:07:34 +00:00
|
|
|
$baseUrl = config('app.name');
|
2023-08-16 14:03:30 +00:00
|
|
|
$team = Team::find(0);
|
2023-11-22 14:18:37 +00:00
|
|
|
$team?->notify(new GeneralNotification("👀 {$baseUrl}: " . $message));
|
2023-10-06 22:51:01 +00:00
|
|
|
ray("👀 {$baseUrl}: " . $message);
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
ray($e->getMessage());
|
2023-08-16 14:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-12 12:51:35 +00:00
|
|
|
function send_user_an_email(MailMessage $mail, string $email, ?string $cc = null): void
|
2023-08-17 13:19:37 +00:00
|
|
|
{
|
|
|
|
$settings = InstanceSettings::get();
|
2023-08-31 13:00:59 +00:00
|
|
|
$type = set_transanctional_email_settings($settings);
|
|
|
|
if (!$type) {
|
|
|
|
throw new Exception('No email settings found.');
|
|
|
|
}
|
2023-09-12 12:51:35 +00:00
|
|
|
if ($cc) {
|
|
|
|
Mail::send(
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
fn (Message $message) => $message
|
|
|
|
->to($email)
|
2023-09-14 16:41:21 +00:00
|
|
|
->replyTo($email)
|
2023-09-12 12:51:35 +00:00
|
|
|
->cc($cc)
|
|
|
|
->subject($mail->subject)
|
|
|
|
->html((string) $mail->render())
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
Mail::send(
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
fn (Message $message) => $message
|
|
|
|
->to($email)
|
|
|
|
->subject($mail->subject)
|
|
|
|
->html((string) $mail->render())
|
|
|
|
);
|
|
|
|
}
|
2023-08-31 13:00:59 +00:00
|
|
|
}
|
2023-10-06 08:42:32 +00:00
|
|
|
function isTestEmailEnabled($notifiable)
|
|
|
|
{
|
2023-10-06 08:47:48 +00:00
|
|
|
if (data_get($notifiable, 'use_instance_email_settings') && isInstanceAdmin()) {
|
|
|
|
return true;
|
|
|
|
} else if (data_get($notifiable, 'smtp_enabled') || data_get($notifiable, 'resend_enabled') && auth()->user()->isAdminFromSession()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2023-10-06 08:42:32 +00:00
|
|
|
}
|
2023-08-31 13:00:59 +00:00
|
|
|
function isEmailEnabled($notifiable)
|
|
|
|
{
|
|
|
|
return data_get($notifiable, 'smtp_enabled') || data_get($notifiable, 'resend_enabled') || data_get($notifiable, 'use_instance_email_settings');
|
|
|
|
}
|
2023-09-06 12:31:38 +00:00
|
|
|
function setNotificationChannels($notifiable, $event)
|
|
|
|
{
|
|
|
|
$channels = [];
|
|
|
|
$isEmailEnabled = isEmailEnabled($notifiable);
|
|
|
|
$isDiscordEnabled = data_get($notifiable, 'discord_enabled');
|
|
|
|
$isTelegramEnabled = data_get($notifiable, 'telegram_enabled');
|
2023-10-05 12:37:16 +00:00
|
|
|
$isSubscribedToEmailEvent = data_get($notifiable, "smtp_notifications_$event");
|
2023-09-06 12:31:38 +00:00
|
|
|
$isSubscribedToDiscordEvent = data_get($notifiable, "discord_notifications_$event");
|
|
|
|
$isSubscribedToTelegramEvent = data_get($notifiable, "telegram_notifications_$event");
|
|
|
|
|
|
|
|
if ($isDiscordEnabled && $isSubscribedToDiscordEvent) {
|
|
|
|
$channels[] = DiscordChannel::class;
|
|
|
|
}
|
2023-10-05 12:37:16 +00:00
|
|
|
if ($isEmailEnabled && $isSubscribedToEmailEvent) {
|
2023-09-06 12:31:38 +00:00
|
|
|
$channels[] = EmailChannel::class;
|
|
|
|
}
|
|
|
|
if ($isTelegramEnabled && $isSubscribedToTelegramEvent) {
|
|
|
|
$channels[] = TelegramChannel::class;
|
|
|
|
}
|
|
|
|
return $channels;
|
|
|
|
}
|
2023-09-15 13:34:25 +00:00
|
|
|
function parseEnvFormatToArray($env_file_contents)
|
|
|
|
{
|
2023-09-08 14:16:59 +00:00
|
|
|
$env_array = array();
|
|
|
|
$lines = explode("\n", $env_file_contents);
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
if ($line === '' || substr($line, 0, 1) === '#') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$equals_pos = strpos($line, '=');
|
|
|
|
if ($equals_pos !== false) {
|
|
|
|
$key = substr($line, 0, $equals_pos);
|
|
|
|
$value = substr($line, $equals_pos + 1);
|
|
|
|
if (substr($value, 0, 1) === '"' && substr($value, -1) === '"') {
|
|
|
|
$value = substr($value, 1, -1);
|
2023-09-15 13:34:25 +00:00
|
|
|
} elseif (substr($value, 0, 1) === "'" && substr($value, -1) === "'") {
|
2023-09-08 14:16:59 +00:00
|
|
|
$value = substr($value, 1, -1);
|
|
|
|
}
|
|
|
|
$env_array[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $env_array;
|
|
|
|
}
|
2023-09-27 10:45:53 +00:00
|
|
|
|
|
|
|
function data_get_str($data, $key, $default = null): Stringable
|
|
|
|
{
|
|
|
|
$str = data_get($data, $key, $default) ?? $default;
|
|
|
|
return Str::of($str);
|
|
|
|
}
|
2023-09-28 08:53:00 +00:00
|
|
|
|
2023-09-30 13:39:40 +00:00
|
|
|
function generateFqdn(Server $server, string $random)
|
|
|
|
{
|
|
|
|
$wildcard = data_get($server, 'settings.wildcard_domain');
|
|
|
|
if (is_null($wildcard) || $wildcard === '') {
|
|
|
|
$wildcard = sslip($server);
|
|
|
|
}
|
|
|
|
$url = Url::fromString($wildcard);
|
|
|
|
$host = $url->getHost();
|
|
|
|
$path = $url->getPath() === '/' ? '' : $url->getPath();
|
|
|
|
$scheme = $url->getScheme();
|
2023-10-05 12:37:16 +00:00
|
|
|
$finalFqdn = "$scheme://{$random}.$host$path";
|
2023-09-30 13:39:40 +00:00
|
|
|
return $finalFqdn;
|
|
|
|
}
|
2023-09-28 08:53:00 +00:00
|
|
|
function sslip(Server $server)
|
|
|
|
{
|
|
|
|
if (isDev()) {
|
2023-09-30 13:39:40 +00:00
|
|
|
return "http://127.0.0.1.sslip.io";
|
2023-09-28 08:53:00 +00:00
|
|
|
}
|
|
|
|
if ($server->ip === 'host.docker.internal') {
|
|
|
|
$baseIp = base_ip();
|
2023-09-30 13:39:40 +00:00
|
|
|
return "http://$baseIp.sslip.io";
|
2023-09-28 08:53:00 +00:00
|
|
|
}
|
2023-09-30 13:39:40 +00:00
|
|
|
return "http://{$server->ip}.sslip.io";
|
2023-09-28 08:53:00 +00:00
|
|
|
}
|
2023-09-28 20:20:49 +00:00
|
|
|
|
|
|
|
function getServiceTemplates()
|
|
|
|
{
|
|
|
|
if (isDev()) {
|
|
|
|
$services = File::get(base_path('templates/service-templates.json'));
|
|
|
|
$services = collect(json_decode($services))->sortKeys();
|
|
|
|
} else {
|
2023-11-13 11:59:59 +00:00
|
|
|
try {
|
|
|
|
$response = Http::retry(3, 50)->get(config('constants.services.official'));
|
|
|
|
if ($response->failed()) {
|
|
|
|
return collect([]);
|
|
|
|
}
|
|
|
|
$services = $response->json();
|
|
|
|
$services = collect($services)->sortKeys();
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
$services = collect([]);
|
2023-09-28 20:20:49 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-13 11:59:59 +00:00
|
|
|
// $version = config('version');
|
|
|
|
// $services = $services->map(function ($service) use ($version) {
|
|
|
|
// if (version_compare($version, data_get($service, 'minVersion', '0.0.0'), '<')) {
|
|
|
|
// $service->disabled = true;
|
|
|
|
// }
|
|
|
|
// return $service;
|
|
|
|
// });
|
2023-09-28 20:20:49 +00:00
|
|
|
return $services;
|
|
|
|
}
|
2023-10-20 12:51:01 +00:00
|
|
|
|
|
|
|
function getResourceByUuid(string $uuid, ?int $teamId = null)
|
|
|
|
{
|
|
|
|
$resource = queryResourcesByUuid($uuid);
|
|
|
|
if (!is_null($teamId)) {
|
2023-10-20 12:58:00 +00:00
|
|
|
if (!is_null($resource) && $resource->environment->project->team_id === $teamId) {
|
2023-10-20 12:51:01 +00:00
|
|
|
return $resource;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return $resource;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function queryResourcesByUuid(string $uuid)
|
|
|
|
{
|
|
|
|
$resource = null;
|
|
|
|
$application = Application::whereUuid($uuid)->first();
|
|
|
|
if ($application) return $application;
|
|
|
|
$service = Service::whereUuid($uuid)->first();
|
|
|
|
if ($service) return $service;
|
|
|
|
$postgresql = StandalonePostgresql::whereUuid($uuid)->first();
|
|
|
|
if ($postgresql) return $postgresql;
|
|
|
|
$redis = StandaloneRedis::whereUuid($uuid)->first();
|
|
|
|
if ($redis) return $redis;
|
|
|
|
$mongodb = StandaloneMongodb::whereUuid($uuid)->first();
|
|
|
|
if ($mongodb) return $mongodb;
|
2023-10-25 08:43:07 +00:00
|
|
|
$mysql = StandaloneMysql::whereUuid($uuid)->first();
|
|
|
|
if ($mysql) return $mysql;
|
|
|
|
$mariadb = StandaloneMariadb::whereUuid($uuid)->first();
|
|
|
|
if ($mariadb) return $mariadb;
|
2023-10-20 12:51:01 +00:00
|
|
|
return $resource;
|
|
|
|
}
|
2023-10-25 08:43:07 +00:00
|
|
|
|
2023-11-13 10:44:13 +00:00
|
|
|
function generateDeployWebhook($resource)
|
|
|
|
{
|
2023-10-25 08:43:07 +00:00
|
|
|
$baseUrl = base_url();
|
|
|
|
$api = Url::fromString($baseUrl) . '/api/v1';
|
|
|
|
$endpoint = '/deploy';
|
|
|
|
$uuid = data_get($resource, 'uuid');
|
|
|
|
$url = $api . $endpoint . "?uuid=$uuid&force=false";
|
|
|
|
return $url;
|
|
|
|
}
|
2023-11-24 14:48:23 +00:00
|
|
|
function generateGitManualWebhook($resource, $type)
|
|
|
|
{
|
2023-11-15 08:15:49 +00:00
|
|
|
if ($resource->source_id !== 0 && !is_null($resource->source_id)) {
|
2023-11-14 13:14:21 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-11-14 12:26:14 +00:00
|
|
|
if ($resource->getMorphClass() === 'App\Models\Application') {
|
|
|
|
$baseUrl = base_url();
|
|
|
|
$api = Url::fromString($baseUrl) . "/webhooks/source/$type/events/manual";
|
|
|
|
return $api;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2023-11-13 10:44:13 +00:00
|
|
|
function removeAnsiColors($text)
|
|
|
|
{
|
2023-11-07 13:40:58 +00:00
|
|
|
return preg_replace('/\e[[][A-Za-z0-9];?[0-9]*m?/', '', $text);
|
|
|
|
}
|
2023-11-24 14:48:23 +00:00
|
|
|
|
2023-11-27 08:58:31 +00:00
|
|
|
function getTopLevelNetworks(Service|Application $resource)
|
|
|
|
{
|
|
|
|
if ($resource->getMorphClass() === 'App\Models\Service') {
|
|
|
|
if ($resource->docker_compose_raw) {
|
|
|
|
try {
|
|
|
|
$yaml = Yaml::parse($resource->docker_compose_raw);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new \Exception($e->getMessage());
|
|
|
|
}
|
|
|
|
$services = data_get($yaml, 'services');
|
|
|
|
$topLevelNetworks = collect(data_get($yaml, 'networks', []));
|
|
|
|
$definedNetwork = collect([$resource->uuid]);
|
|
|
|
$services = collect($services)->map(function ($service, $_) use ($topLevelNetworks, $definedNetwork) {
|
|
|
|
$serviceNetworks = collect(data_get($service, 'networks', []));
|
|
|
|
|
|
|
|
// Collect/create/update networks
|
|
|
|
if ($serviceNetworks->count() > 0) {
|
|
|
|
foreach ($serviceNetworks as $networkName => $networkDetails) {
|
|
|
|
$networkExists = $topLevelNetworks->contains(function ($value, $key) use ($networkName) {
|
|
|
|
return $value == $networkName || $key == $networkName;
|
|
|
|
});
|
|
|
|
if (!$networkExists) {
|
|
|
|
$topLevelNetworks->put($networkDetails, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$definedNetworkExists = $topLevelNetworks->contains(function ($value, $_) use ($definedNetwork) {
|
|
|
|
return $value == $definedNetwork;
|
|
|
|
});
|
|
|
|
if (!$definedNetworkExists) {
|
|
|
|
foreach ($definedNetwork as $network) {
|
|
|
|
$topLevelNetworks->put($network, [
|
|
|
|
'name' => $network,
|
|
|
|
'external' => true
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $service;
|
|
|
|
});
|
|
|
|
return $topLevelNetworks->keys();
|
|
|
|
}
|
|
|
|
} else if ($resource->getMorphClass() === 'App\Models\Application') {
|
|
|
|
try {
|
|
|
|
$yaml = Yaml::parse($resource->docker_compose_raw);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new \Exception($e->getMessage());
|
|
|
|
}
|
|
|
|
$server = $resource->destination->server;
|
|
|
|
$topLevelNetworks = collect(data_get($yaml, 'networks', []));
|
|
|
|
$services = data_get($yaml, 'services');
|
|
|
|
$definedNetwork = collect([$resource->uuid]);
|
|
|
|
$services = collect($services)->map(function ($service, $_) use ($topLevelNetworks, $definedNetwork) {
|
|
|
|
$serviceNetworks = collect(data_get($service, 'networks', []));
|
|
|
|
|
|
|
|
// Collect/create/update networks
|
|
|
|
if ($serviceNetworks->count() > 0) {
|
|
|
|
foreach ($serviceNetworks as $networkName => $networkDetails) {
|
|
|
|
$networkExists = $topLevelNetworks->contains(function ($value, $key) use ($networkName) {
|
|
|
|
return $value == $networkName || $key == $networkName;
|
|
|
|
});
|
|
|
|
if (!$networkExists) {
|
|
|
|
$topLevelNetworks->put($networkDetails, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$definedNetworkExists = $topLevelNetworks->contains(function ($value, $_) use ($definedNetwork) {
|
|
|
|
return $value == $definedNetwork;
|
|
|
|
});
|
|
|
|
if (!$definedNetworkExists) {
|
|
|
|
foreach ($definedNetwork as $network) {
|
|
|
|
$topLevelNetworks->put($network, [
|
|
|
|
'name' => $network,
|
|
|
|
'external' => true
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $service;
|
|
|
|
});
|
|
|
|
return $topLevelNetworks->keys();
|
|
|
|
}
|
|
|
|
}
|
2023-11-28 11:05:04 +00:00
|
|
|
function parseDockerComposeFile(Service|Application $resource, bool $isNew = false, int $pull_request_id = 0, bool $is_pr = false)
|
2023-11-24 14:48:23 +00:00
|
|
|
{
|
2023-11-27 10:54:55 +00:00
|
|
|
// ray()->clearAll();
|
2023-11-24 14:48:23 +00:00
|
|
|
if ($resource->getMorphClass() === 'App\Models\Service') {
|
|
|
|
if ($resource->docker_compose_raw) {
|
|
|
|
try {
|
|
|
|
$yaml = Yaml::parse($resource->docker_compose_raw);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new \Exception($e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
$topLevelVolumes = collect(data_get($yaml, 'volumes', []));
|
|
|
|
$topLevelNetworks = collect(data_get($yaml, 'networks', []));
|
|
|
|
$dockerComposeVersion = data_get($yaml, 'version') ?? '3.8';
|
|
|
|
$services = data_get($yaml, 'services');
|
|
|
|
|
|
|
|
$generatedServiceFQDNS = collect([]);
|
|
|
|
if (is_null($resource->destination)) {
|
|
|
|
$destination = $resource->server->destinations()->first();
|
|
|
|
if ($destination) {
|
|
|
|
$resource->destination()->associate($destination);
|
|
|
|
$resource->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$definedNetwork = collect([$resource->uuid]);
|
|
|
|
|
|
|
|
$services = collect($services)->map(function ($service, $serviceName) use ($topLevelVolumes, $topLevelNetworks, $definedNetwork, $isNew, $generatedServiceFQDNS, $resource) {
|
|
|
|
$serviceVolumes = collect(data_get($service, 'volumes', []));
|
|
|
|
$servicePorts = collect(data_get($service, 'ports', []));
|
|
|
|
$serviceNetworks = collect(data_get($service, 'networks', []));
|
|
|
|
$serviceVariables = collect(data_get($service, 'environment', []));
|
|
|
|
$serviceLabels = collect(data_get($service, 'labels', []));
|
|
|
|
if ($serviceLabels->count() > 0) {
|
|
|
|
$removedLabels = collect([]);
|
|
|
|
$serviceLabels = $serviceLabels->filter(function ($serviceLabel, $serviceLabelName) use ($removedLabels) {
|
|
|
|
if (!str($serviceLabel)->contains('=')) {
|
|
|
|
$removedLabels->put($serviceLabelName, $serviceLabel);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $serviceLabel;
|
|
|
|
});
|
|
|
|
foreach ($removedLabels as $removedLabelName => $removedLabel) {
|
|
|
|
$serviceLabels->push("$removedLabelName=$removedLabel");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$containerName = "$serviceName-{$resource->uuid}";
|
|
|
|
|
|
|
|
// Decide if the service is a database
|
2023-11-27 10:54:55 +00:00
|
|
|
$isDatabase = isDatabaseImage(data_get_str($service, 'image'));
|
2023-11-24 14:48:23 +00:00
|
|
|
$image = data_get_str($service, 'image');
|
|
|
|
data_set($service, 'is_database', $isDatabase);
|
|
|
|
|
|
|
|
// Create new serviceApplication or serviceDatabase
|
|
|
|
if ($isDatabase) {
|
|
|
|
if ($isNew) {
|
|
|
|
$savedService = ServiceDatabase::create([
|
|
|
|
'name' => $serviceName,
|
|
|
|
'image' => $image,
|
|
|
|
'service_id' => $resource->id
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
$savedService = ServiceDatabase::where([
|
|
|
|
'name' => $serviceName,
|
|
|
|
'service_id' => $resource->id
|
|
|
|
])->first();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($isNew) {
|
|
|
|
$savedService = ServiceApplication::create([
|
|
|
|
'name' => $serviceName,
|
|
|
|
'image' => $image,
|
|
|
|
'service_id' => $resource->id
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
$savedService = ServiceApplication::where([
|
|
|
|
'name' => $serviceName,
|
|
|
|
'service_id' => $resource->id
|
|
|
|
])->first();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_null($savedService)) {
|
|
|
|
if ($isDatabase) {
|
|
|
|
$savedService = ServiceDatabase::create([
|
|
|
|
'name' => $serviceName,
|
|
|
|
'image' => $image,
|
|
|
|
'service_id' => $resource->id
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
$savedService = ServiceApplication::create([
|
|
|
|
'name' => $serviceName,
|
|
|
|
'image' => $image,
|
|
|
|
'service_id' => $resource->id
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if image changed
|
|
|
|
if ($savedService->image !== $image) {
|
|
|
|
$savedService->image = $image;
|
|
|
|
$savedService->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect/create/update networks
|
|
|
|
if ($serviceNetworks->count() > 0) {
|
|
|
|
foreach ($serviceNetworks as $networkName => $networkDetails) {
|
|
|
|
$networkExists = $topLevelNetworks->contains(function ($value, $key) use ($networkName) {
|
|
|
|
return $value == $networkName || $key == $networkName;
|
|
|
|
});
|
|
|
|
if (!$networkExists) {
|
|
|
|
$topLevelNetworks->put($networkDetails, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect/create/update ports
|
|
|
|
$collectedPorts = collect([]);
|
|
|
|
if ($servicePorts->count() > 0) {
|
|
|
|
foreach ($servicePorts as $sport) {
|
|
|
|
if (is_string($sport) || is_numeric($sport)) {
|
|
|
|
$collectedPorts->push($sport);
|
|
|
|
}
|
|
|
|
if (is_array($sport)) {
|
|
|
|
$target = data_get($sport, 'target');
|
|
|
|
$published = data_get($sport, 'published');
|
|
|
|
$protocol = data_get($sport, 'protocol');
|
|
|
|
$collectedPorts->push("$target:$published/$protocol");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$savedService->ports = $collectedPorts->implode(',');
|
|
|
|
$savedService->save();
|
|
|
|
|
|
|
|
// Add Coolify specific networks
|
|
|
|
$definedNetworkExists = $topLevelNetworks->contains(function ($value, $_) use ($definedNetwork) {
|
|
|
|
return $value == $definedNetwork;
|
|
|
|
});
|
|
|
|
if (!$definedNetworkExists) {
|
|
|
|
foreach ($definedNetwork as $network) {
|
|
|
|
$topLevelNetworks->put($network, [
|
|
|
|
'name' => $network,
|
|
|
|
'external' => true
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$networks = collect();
|
|
|
|
foreach ($serviceNetworks as $key => $serviceNetwork) {
|
|
|
|
if (gettype($serviceNetwork) === 'string') {
|
|
|
|
// networks:
|
|
|
|
// - appwrite
|
|
|
|
$networks->put($serviceNetwork, null);
|
|
|
|
} else if (gettype($serviceNetwork) === 'array') {
|
|
|
|
// networks:
|
|
|
|
// default:
|
|
|
|
// ipv4_address: 192.168.203.254
|
|
|
|
// $networks->put($serviceNetwork, null);
|
|
|
|
ray($key);
|
|
|
|
$networks->put($key, $serviceNetwork);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($definedNetwork as $key => $network) {
|
|
|
|
$networks->put($network, null);
|
|
|
|
}
|
|
|
|
data_set($service, 'networks', $networks->toArray());
|
|
|
|
|
|
|
|
// Collect/create/update volumes
|
|
|
|
if ($serviceVolumes->count() > 0) {
|
|
|
|
$serviceVolumes = $serviceVolumes->map(function ($volume) use ($savedService, $topLevelVolumes) {
|
|
|
|
$type = null;
|
|
|
|
$source = null;
|
|
|
|
$target = null;
|
|
|
|
$content = null;
|
|
|
|
$isDirectory = false;
|
|
|
|
if (is_string($volume)) {
|
|
|
|
$source = Str::of($volume)->before(':');
|
|
|
|
$target = Str::of($volume)->after(':')->beforeLast(':');
|
|
|
|
if ($source->startsWith('./') || $source->startsWith('/') || $source->startsWith('~')) {
|
|
|
|
$type = Str::of('bind');
|
|
|
|
} else {
|
|
|
|
$type = Str::of('volume');
|
|
|
|
}
|
|
|
|
} else if (is_array($volume)) {
|
|
|
|
$type = data_get_str($volume, 'type');
|
|
|
|
$source = data_get_str($volume, 'source');
|
|
|
|
$target = data_get_str($volume, 'target');
|
|
|
|
$content = data_get($volume, 'content');
|
|
|
|
$isDirectory = (bool) data_get($volume, 'isDirectory', false);
|
|
|
|
$foundConfig = $savedService->fileStorages()->whereMountPath($target)->first();
|
|
|
|
if ($foundConfig) {
|
|
|
|
$contentNotNull = data_get($foundConfig, 'content');
|
|
|
|
if ($contentNotNull) {
|
|
|
|
$content = $contentNotNull;
|
|
|
|
}
|
|
|
|
$isDirectory = (bool) data_get($foundConfig, 'is_directory');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($type->value() === 'bind') {
|
|
|
|
if ($source->value() === "/var/run/docker.sock") {
|
|
|
|
return $volume;
|
|
|
|
}
|
|
|
|
if ($source->value() === '/tmp' || $source->value() === '/tmp/') {
|
|
|
|
return $volume;
|
|
|
|
}
|
|
|
|
LocalFileVolume::updateOrCreate(
|
|
|
|
[
|
|
|
|
'mount_path' => $target,
|
|
|
|
'resource_id' => $savedService->id,
|
|
|
|
'resource_type' => get_class($savedService)
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'fs_path' => $source,
|
|
|
|
'mount_path' => $target,
|
|
|
|
'content' => $content,
|
|
|
|
'is_directory' => $isDirectory,
|
|
|
|
'resource_id' => $savedService->id,
|
|
|
|
'resource_type' => get_class($savedService)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} else if ($type->value() === 'volume') {
|
|
|
|
$slugWithoutUuid = Str::slug($source, '-');
|
|
|
|
$name = "{$savedService->service->uuid}_{$slugWithoutUuid}";
|
|
|
|
if (is_string($volume)) {
|
|
|
|
$source = Str::of($volume)->before(':');
|
|
|
|
$target = Str::of($volume)->after(':')->beforeLast(':');
|
|
|
|
$source = $name;
|
|
|
|
$volume = "$source:$target";
|
|
|
|
} else if (is_array($volume)) {
|
|
|
|
data_set($volume, 'source', $name);
|
|
|
|
}
|
|
|
|
$topLevelVolumes->put($name, [
|
|
|
|
'name' => $name,
|
|
|
|
]);
|
|
|
|
LocalPersistentVolume::updateOrCreate(
|
|
|
|
[
|
|
|
|
'mount_path' => $target,
|
|
|
|
'resource_id' => $savedService->id,
|
|
|
|
'resource_type' => get_class($savedService)
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => $name,
|
|
|
|
'mount_path' => $target,
|
|
|
|
'resource_id' => $savedService->id,
|
|
|
|
'resource_type' => get_class($savedService)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$savedService->getFilesFromServer(isInit: true);
|
|
|
|
return $volume;
|
|
|
|
});
|
|
|
|
data_set($service, 'volumes', $serviceVolumes->toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add env_file with at least .env to the service
|
|
|
|
// $envFile = collect(data_get($service, 'env_file', []));
|
|
|
|
// if ($envFile->count() > 0) {
|
|
|
|
// if (!$envFile->contains('.env')) {
|
|
|
|
// $envFile->push('.env');
|
|
|
|
// }
|
|
|
|
// } else {
|
|
|
|
// $envFile = collect(['.env']);
|
|
|
|
// }
|
|
|
|
// data_set($service, 'env_file', $envFile->toArray());
|
|
|
|
|
|
|
|
|
|
|
|
// Get variables from the service
|
|
|
|
foreach ($serviceVariables as $variableName => $variable) {
|
|
|
|
if (is_numeric($variableName)) {
|
|
|
|
$variable = Str::of($variable);
|
|
|
|
if ($variable->contains('=')) {
|
|
|
|
// - SESSION_SECRET=123
|
|
|
|
// - SESSION_SECRET=
|
|
|
|
$key = $variable->before('=');
|
|
|
|
$value = $variable->after('=');
|
|
|
|
} else {
|
|
|
|
// - SESSION_SECRET
|
|
|
|
$key = $variable;
|
|
|
|
$value = null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// SESSION_SECRET: 123
|
|
|
|
// SESSION_SECRET:
|
|
|
|
$key = Str::of($variableName);
|
|
|
|
$value = Str::of($variable);
|
|
|
|
}
|
|
|
|
if ($key->startsWith('SERVICE_FQDN')) {
|
|
|
|
if ($isNew || $savedService->fqdn === null) {
|
|
|
|
$name = $key->after('SERVICE_FQDN_')->beforeLast('_')->lower();
|
|
|
|
$fqdn = generateFqdn($resource->server, "{$name->value()}-{$resource->uuid}");
|
|
|
|
if (substr_count($key->value(), '_') === 3) {
|
|
|
|
// SERVICE_FQDN_UMAMI_1000
|
|
|
|
$port = $key->afterLast('_');
|
|
|
|
} else {
|
|
|
|
// SERVICE_FQDN_UMAMI
|
|
|
|
$port = null;
|
|
|
|
}
|
|
|
|
if ($port) {
|
|
|
|
$fqdn = "$fqdn:$port";
|
|
|
|
}
|
|
|
|
if (substr_count($key->value(), '_') >= 2) {
|
|
|
|
if (is_null($value)) {
|
|
|
|
$value = Str::of('/');
|
|
|
|
}
|
|
|
|
$path = $value->value();
|
|
|
|
if ($generatedServiceFQDNS->count() > 0) {
|
|
|
|
$alreadyGenerated = $generatedServiceFQDNS->has($key->value());
|
|
|
|
if ($alreadyGenerated) {
|
|
|
|
$fqdn = $generatedServiceFQDNS->get($key->value());
|
|
|
|
} else {
|
|
|
|
$generatedServiceFQDNS->put($key->value(), $fqdn);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$generatedServiceFQDNS->put($key->value(), $fqdn);
|
|
|
|
}
|
|
|
|
$fqdn = "$fqdn$path";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$isDatabase) {
|
|
|
|
if ($savedService->fqdn) {
|
|
|
|
$fqdn = $savedService->fqdn . ',' . $fqdn;
|
|
|
|
} else {
|
|
|
|
$fqdn = $fqdn;
|
|
|
|
}
|
|
|
|
$savedService->fqdn = $fqdn;
|
|
|
|
$savedService->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// data_forget($service, "environment.$variableName");
|
|
|
|
// $yaml = data_forget($yaml, "services.$serviceName.environment.$variableName");
|
|
|
|
// if (count(data_get($yaml, 'services.' . $serviceName . '.environment')) === 0) {
|
|
|
|
// $yaml = data_forget($yaml, "services.$serviceName.environment");
|
|
|
|
// }
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($value?->startsWith('$')) {
|
|
|
|
$foundEnv = EnvironmentVariable::where([
|
|
|
|
'key' => $key,
|
|
|
|
'service_id' => $resource->id,
|
|
|
|
])->first();
|
2023-12-01 11:14:23 +00:00
|
|
|
$value = Str::of(replaceVariables($value));
|
|
|
|
$key = $value;
|
2023-11-24 14:48:23 +00:00
|
|
|
if ($value->startsWith('SERVICE_')) {
|
2023-12-01 11:14:23 +00:00
|
|
|
$foundEnv = EnvironmentVariable::where([
|
|
|
|
'key' => $key,
|
|
|
|
'service_id' => $resource->id,
|
|
|
|
])->first();
|
2023-11-28 11:05:04 +00:00
|
|
|
['command' => $command, 'forService' => $forService, 'generatedValue' => $generatedValue, 'port' => $port] = parseEnvVariable($value);
|
2023-11-24 14:48:23 +00:00
|
|
|
if ($command->value() === 'FQDN' || $command->value() === 'URL') {
|
|
|
|
if (Str::lower($forService) === $serviceName) {
|
|
|
|
$fqdn = generateFqdn($resource->server, $containerName);
|
|
|
|
} else {
|
|
|
|
$fqdn = generateFqdn($resource->server, Str::lower($forService) . '-' . $resource->uuid);
|
|
|
|
}
|
|
|
|
if ($port) {
|
|
|
|
$fqdn = "$fqdn:$port";
|
|
|
|
}
|
|
|
|
if ($foundEnv) {
|
|
|
|
$fqdn = data_get($foundEnv, 'value');
|
|
|
|
} else {
|
|
|
|
if ($command->value() === 'URL') {
|
|
|
|
$fqdn = Str::of($fqdn)->after('://')->value();
|
|
|
|
}
|
|
|
|
EnvironmentVariable::create([
|
|
|
|
'key' => $key,
|
|
|
|
'value' => $fqdn,
|
|
|
|
'is_build_time' => false,
|
|
|
|
'service_id' => $resource->id,
|
|
|
|
'is_preview' => false,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
if (!$isDatabase) {
|
|
|
|
if ($command->value() === 'FQDN' && is_null($savedService->fqdn)) {
|
|
|
|
$savedService->fqdn = $fqdn;
|
|
|
|
$savedService->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-11-28 11:05:04 +00:00
|
|
|
$generatedValue = generateEnvValue($command);
|
2023-11-24 14:48:23 +00:00
|
|
|
if (!$foundEnv) {
|
|
|
|
EnvironmentVariable::create([
|
|
|
|
'key' => $key,
|
|
|
|
'value' => $generatedValue,
|
|
|
|
'is_build_time' => false,
|
|
|
|
'service_id' => $resource->id,
|
|
|
|
'is_preview' => false,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($value->contains(':-')) {
|
|
|
|
$key = $value->before(':');
|
|
|
|
$defaultValue = $value->after(':-');
|
|
|
|
} else if ($value->contains('-')) {
|
|
|
|
$key = $value->before('-');
|
|
|
|
$defaultValue = $value->after('-');
|
|
|
|
} else if ($value->contains(':?')) {
|
|
|
|
$key = $value->before(':');
|
|
|
|
$defaultValue = $value->after(':?');
|
|
|
|
} else if ($value->contains('?')) {
|
|
|
|
$key = $value->before('?');
|
|
|
|
$defaultValue = $value->after('?');
|
|
|
|
} else {
|
|
|
|
$key = $value;
|
|
|
|
$defaultValue = null;
|
|
|
|
}
|
2023-12-12 13:28:11 +00:00
|
|
|
$foundEnv = EnvironmentVariable::where([
|
|
|
|
'key' => $key,
|
|
|
|
'service_id' => $resource->id,
|
|
|
|
])->first();
|
2023-11-24 14:48:23 +00:00
|
|
|
if ($foundEnv) {
|
|
|
|
$defaultValue = data_get($foundEnv, 'value');
|
|
|
|
}
|
|
|
|
EnvironmentVariable::updateOrCreate([
|
|
|
|
'key' => $key,
|
|
|
|
'service_id' => $resource->id,
|
|
|
|
], [
|
|
|
|
'value' => $defaultValue,
|
|
|
|
'is_build_time' => false,
|
|
|
|
'service_id' => $resource->id,
|
|
|
|
'is_preview' => false,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add labels to the service
|
|
|
|
if ($savedService->serviceType()) {
|
|
|
|
$fqdns = generateServiceSpecificFqdns($savedService, forTraefik: true);
|
|
|
|
} else {
|
2023-12-04 10:20:50 +00:00
|
|
|
$fqdns = collect(data_get($savedService, 'fqdns'))->filter();
|
2023-11-24 14:48:23 +00:00
|
|
|
}
|
|
|
|
$defaultLabels = defaultLabels($resource->id, $containerName, type: 'service', subType: $isDatabase ? 'database' : 'application', subId: $savedService->id);
|
|
|
|
$serviceLabels = $serviceLabels->merge($defaultLabels);
|
|
|
|
if (!$isDatabase && $fqdns->count() > 0) {
|
|
|
|
if ($fqdns) {
|
|
|
|
$serviceLabels = $serviceLabels->merge(fqdnLabelsForTraefik($resource->uuid, $fqdns, true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($resource->server->isLogDrainEnabled() && $savedService->isLogDrainEnabled()) {
|
|
|
|
data_set($service, 'logging', [
|
|
|
|
'driver' => 'fluentd',
|
|
|
|
'options' => [
|
|
|
|
'fluentd-address' => "tcp://127.0.0.1:24224",
|
|
|
|
'fluentd-async' => "true",
|
|
|
|
'fluentd-sub-second-precision' => "true",
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
data_set($service, 'labels', $serviceLabels->toArray());
|
|
|
|
data_forget($service, 'is_database');
|
|
|
|
data_set($service, 'restart', RESTART_MODE);
|
|
|
|
data_set($service, 'container_name', $containerName);
|
|
|
|
data_forget($service, 'volumes.*.content');
|
|
|
|
data_forget($service, 'volumes.*.isDirectory');
|
|
|
|
// Remove unnecessary variables from service.environment
|
|
|
|
// $withoutServiceEnvs = collect([]);
|
|
|
|
// collect(data_get($service, 'environment'))->each(function ($value, $key) use ($withoutServiceEnvs) {
|
|
|
|
// ray($key, $value);
|
|
|
|
// if (!Str::of($key)->startsWith('$SERVICE_') && !Str::of($value)->startsWith('SERVICE_')) {
|
|
|
|
// $k = Str::of($value)->before("=");
|
|
|
|
// $v = Str::of($value)->after("=");
|
|
|
|
// $withoutServiceEnvs->put($k->value(), $v->value());
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// ray($withoutServiceEnvs);
|
|
|
|
// data_set($service, 'environment', $withoutServiceEnvs->toArray());
|
|
|
|
return $service;
|
|
|
|
});
|
|
|
|
$finalServices = [
|
|
|
|
'version' => $dockerComposeVersion,
|
|
|
|
'services' => $services->toArray(),
|
|
|
|
'volumes' => $topLevelVolumes->toArray(),
|
|
|
|
'networks' => $topLevelNetworks->toArray(),
|
|
|
|
];
|
|
|
|
$resource->docker_compose_raw = Yaml::dump($yaml, 10, 2);
|
|
|
|
$resource->docker_compose = Yaml::dump($finalServices, 10, 2);
|
|
|
|
$resource->save();
|
|
|
|
$resource->saveComposeConfigs();
|
2023-11-27 10:54:55 +00:00
|
|
|
return collect($finalServices);
|
2023-11-24 14:48:23 +00:00
|
|
|
} else {
|
|
|
|
return collect([]);
|
|
|
|
}
|
|
|
|
} else if ($resource->getMorphClass() === 'App\Models\Application') {
|
2023-11-28 09:11:53 +00:00
|
|
|
$isSameDockerComposeFile = false;
|
|
|
|
if ($resource->dockerComposePrLocation() === $resource->dockerComposeLocation()) {
|
|
|
|
$isSameDockerComposeFile = true;
|
|
|
|
$is_pr = false;
|
|
|
|
}
|
|
|
|
if ($is_pr) {
|
|
|
|
try {
|
|
|
|
$yaml = Yaml::parse($resource->docker_compose_pr_raw);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new \Exception($e->getMessage());
|
|
|
|
}
|
2023-11-27 14:50:22 +00:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
$yaml = Yaml::parse($resource->docker_compose_raw);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new \Exception($e->getMessage());
|
|
|
|
}
|
2023-11-24 14:48:23 +00:00
|
|
|
}
|
|
|
|
$server = $resource->destination->server;
|
|
|
|
$topLevelVolumes = collect(data_get($yaml, 'volumes', []));
|
2023-11-27 13:28:21 +00:00
|
|
|
if ($pull_request_id !== 0) {
|
|
|
|
$topLevelVolumes = collect([]);
|
|
|
|
}
|
2023-11-24 14:48:23 +00:00
|
|
|
$topLevelNetworks = collect(data_get($yaml, 'networks', []));
|
|
|
|
$dockerComposeVersion = data_get($yaml, 'version') ?? '3.8';
|
|
|
|
$services = data_get($yaml, 'services');
|
|
|
|
|
|
|
|
$generatedServiceFQDNS = collect([]);
|
|
|
|
if (is_null($resource->destination)) {
|
|
|
|
$destination = $server->destinations()->first();
|
|
|
|
if ($destination) {
|
|
|
|
$resource->destination()->associate($destination);
|
|
|
|
$resource->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$definedNetwork = collect([$resource->uuid]);
|
2023-11-27 13:28:21 +00:00
|
|
|
if ($pull_request_id !== 0) {
|
|
|
|
$definedNetwork = collect(["{$resource->uuid}-$pull_request_id"]);
|
|
|
|
}
|
|
|
|
$services = collect($services)->map(function ($service, $serviceName) use ($topLevelVolumes, $topLevelNetworks, $definedNetwork, $isNew, $generatedServiceFQDNS, $resource, $server, $pull_request_id) {
|
2023-11-24 14:48:23 +00:00
|
|
|
$serviceVolumes = collect(data_get($service, 'volumes', []));
|
|
|
|
$servicePorts = collect(data_get($service, 'ports', []));
|
|
|
|
$serviceNetworks = collect(data_get($service, 'networks', []));
|
|
|
|
$serviceVariables = collect(data_get($service, 'environment', []));
|
|
|
|
$serviceLabels = collect(data_get($service, 'labels', []));
|
2023-12-04 10:20:50 +00:00
|
|
|
$serviceBuildVariables = collect(data_get($service, 'build.args', []));
|
|
|
|
$serviceVariables = $serviceVariables->merge($serviceBuildVariables);
|
2023-11-24 14:48:23 +00:00
|
|
|
if ($serviceLabels->count() > 0) {
|
|
|
|
$removedLabels = collect([]);
|
|
|
|
$serviceLabels = $serviceLabels->filter(function ($serviceLabel, $serviceLabelName) use ($removedLabels) {
|
|
|
|
if (!str($serviceLabel)->contains('=')) {
|
|
|
|
$removedLabels->put($serviceLabelName, $serviceLabel);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $serviceLabel;
|
|
|
|
});
|
|
|
|
foreach ($removedLabels as $removedLabelName => $removedLabel) {
|
|
|
|
$serviceLabels->push("$removedLabelName=$removedLabel");
|
|
|
|
}
|
|
|
|
}
|
2023-11-27 13:28:21 +00:00
|
|
|
$baseName = generateApplicationContainerName($resource, $pull_request_id);
|
|
|
|
$containerName = "$serviceName-$baseName";
|
|
|
|
if ($pull_request_id !== 0) {
|
|
|
|
if (count($serviceVolumes) > 0) {
|
|
|
|
$serviceVolumes = $serviceVolumes->map(function ($volume) use ($resource, $pull_request_id, $topLevelVolumes) {
|
|
|
|
if (is_string($volume)) {
|
|
|
|
$volume = str($volume);
|
2023-12-06 12:45:43 +00:00
|
|
|
if ($volume->contains(':') && !$volume->startsWith('/')) {
|
2023-11-27 13:28:21 +00:00
|
|
|
$name = $volume->before(':');
|
|
|
|
$mount = $volume->after(':');
|
2023-11-27 14:25:15 +00:00
|
|
|
$newName = $resource->uuid . "-{$name}-pr-$pull_request_id";
|
2023-11-27 13:28:21 +00:00
|
|
|
$volume = str("$newName:$mount");
|
|
|
|
$topLevelVolumes->put($newName, [
|
|
|
|
'name' => $newName,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
} else if (is_array($volume)) {
|
2023-11-27 14:25:15 +00:00
|
|
|
$source = data_get($volume, 'source');
|
|
|
|
if ($source) {
|
|
|
|
$newSource = $resource->uuid . "-{$source}-pr-$pull_request_id";
|
|
|
|
data_set($volume, 'source', $newSource);
|
2023-12-06 12:45:43 +00:00
|
|
|
if (!str($source)->startsWith('/')) {
|
|
|
|
$topLevelVolumes->put($newSource, [
|
|
|
|
'name' => $newSource,
|
|
|
|
]);
|
|
|
|
}
|
2023-11-27 14:25:15 +00:00
|
|
|
}
|
2023-11-27 13:28:21 +00:00
|
|
|
}
|
|
|
|
return $volume->value();
|
|
|
|
});
|
|
|
|
data_set($service, 'volumes', $serviceVolumes->toArray());
|
|
|
|
}
|
2023-11-27 14:25:15 +00:00
|
|
|
} else {
|
2023-12-04 10:20:50 +00:00
|
|
|
if (count($serviceVolumes) > 0) {
|
|
|
|
$serviceVolumes = $serviceVolumes->map(function ($volume) use ($resource, $topLevelVolumes) {
|
|
|
|
if (is_string($volume)) {
|
|
|
|
$volume = str($volume);
|
2023-12-06 12:45:43 +00:00
|
|
|
if ($volume->contains(':') && !$volume->startsWith('/')) {
|
2023-12-04 10:20:50 +00:00
|
|
|
$name = $volume->before(':');
|
|
|
|
$mount = $volume->after(':');
|
|
|
|
if ($name->startsWith('.') || $name->startsWith('~')) {
|
|
|
|
$dir = base_configuration_dir() . '/applications/' . $resource->uuid;
|
|
|
|
if ($name->startsWith('.')) {
|
|
|
|
$name = $name->replaceFirst('.', $dir);
|
|
|
|
}
|
|
|
|
if ($name->startsWith('~')) {
|
|
|
|
$name = $name->replaceFirst('~', $dir);
|
|
|
|
}
|
|
|
|
$volume = str("$name:$mount");
|
|
|
|
} else {
|
|
|
|
$topLevelVolumes->put($name->value(), [
|
|
|
|
'name' => $name->value(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (is_array($volume)) {
|
|
|
|
$source = data_get($volume, 'source');
|
|
|
|
if ($source) {
|
2023-12-06 12:45:43 +00:00
|
|
|
if ((str($source)->startsWith('.') || str($source)->startsWith('~')) && !str($source)->startsWith('/')) {
|
2023-12-04 10:20:50 +00:00
|
|
|
$dir = base_configuration_dir() . '/applications/' . $resource->uuid;
|
|
|
|
if (str($source, '.')) {
|
|
|
|
$source = str('.', $dir, $source);
|
|
|
|
}
|
|
|
|
if (str($source, '~')) {
|
|
|
|
$source = str('~', $dir, $source);
|
|
|
|
}
|
|
|
|
data_set($volume, 'source', $source);
|
|
|
|
} else {
|
|
|
|
data_set($volume, 'source', $source);
|
|
|
|
$topLevelVolumes->put($source, [
|
|
|
|
'name' => $source,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $volume->value();
|
|
|
|
});
|
|
|
|
data_set($service, 'volumes', $serviceVolumes->toArray());
|
|
|
|
}
|
2023-11-27 13:28:21 +00:00
|
|
|
}
|
2023-11-24 14:48:23 +00:00
|
|
|
// Decide if the service is a database
|
2023-11-27 10:54:55 +00:00
|
|
|
$isDatabase = isDatabaseImage(data_get_str($service, 'image'));
|
2023-11-24 14:48:23 +00:00
|
|
|
data_set($service, 'is_database', $isDatabase);
|
|
|
|
|
|
|
|
// Collect/create/update networks
|
|
|
|
if ($serviceNetworks->count() > 0) {
|
|
|
|
foreach ($serviceNetworks as $networkName => $networkDetails) {
|
2023-11-27 13:28:21 +00:00
|
|
|
ray($networkDetails);
|
2023-11-24 14:48:23 +00:00
|
|
|
$networkExists = $topLevelNetworks->contains(function ($value, $key) use ($networkName) {
|
|
|
|
return $value == $networkName || $key == $networkName;
|
|
|
|
});
|
|
|
|
if (!$networkExists) {
|
|
|
|
$topLevelNetworks->put($networkDetails, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Collect/create/update ports
|
|
|
|
$collectedPorts = collect([]);
|
|
|
|
if ($servicePorts->count() > 0) {
|
|
|
|
foreach ($servicePorts as $sport) {
|
|
|
|
if (is_string($sport) || is_numeric($sport)) {
|
|
|
|
$collectedPorts->push($sport);
|
|
|
|
}
|
|
|
|
if (is_array($sport)) {
|
|
|
|
$target = data_get($sport, 'target');
|
|
|
|
$published = data_get($sport, 'published');
|
|
|
|
$protocol = data_get($sport, 'protocol');
|
|
|
|
$collectedPorts->push("$target:$published/$protocol");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($collectedPorts->count() > 0) {
|
|
|
|
// ray($collectedPorts->implode(','));
|
|
|
|
}
|
|
|
|
$definedNetworkExists = $topLevelNetworks->contains(function ($value, $_) use ($definedNetwork) {
|
|
|
|
return $value == $definedNetwork;
|
|
|
|
});
|
|
|
|
if (!$definedNetworkExists) {
|
|
|
|
foreach ($definedNetwork as $network) {
|
2023-11-27 13:28:21 +00:00
|
|
|
if ($pull_request_id !== 0) {
|
|
|
|
$topLevelNetworks->put($network, [
|
|
|
|
'name' => $network,
|
2023-11-27 14:25:15 +00:00
|
|
|
'external' => true
|
2023-11-27 13:28:21 +00:00
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
$topLevelNetworks->put($network, [
|
|
|
|
'name' => $network,
|
|
|
|
'external' => true
|
|
|
|
]);
|
|
|
|
}
|
2023-11-24 14:48:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$networks = collect();
|
|
|
|
foreach ($serviceNetworks as $key => $serviceNetwork) {
|
|
|
|
if (gettype($serviceNetwork) === 'string') {
|
|
|
|
// networks:
|
|
|
|
// - appwrite
|
|
|
|
$networks->put($serviceNetwork, null);
|
|
|
|
} else if (gettype($serviceNetwork) === 'array') {
|
|
|
|
// networks:
|
|
|
|
// default:
|
|
|
|
// ipv4_address: 192.168.203.254
|
|
|
|
// $networks->put($serviceNetwork, null);
|
|
|
|
$networks->put($key, $serviceNetwork);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($definedNetwork as $key => $network) {
|
|
|
|
$networks->put($network, null);
|
|
|
|
}
|
|
|
|
data_set($service, 'networks', $networks->toArray());
|
|
|
|
// Get variables from the service
|
|
|
|
foreach ($serviceVariables as $variableName => $variable) {
|
|
|
|
if (is_numeric($variableName)) {
|
|
|
|
$variable = Str::of($variable);
|
|
|
|
if ($variable->contains('=')) {
|
|
|
|
// - SESSION_SECRET=123
|
|
|
|
// - SESSION_SECRET=
|
|
|
|
$key = $variable->before('=');
|
|
|
|
$value = $variable->after('=');
|
|
|
|
} else {
|
|
|
|
// - SESSION_SECRET
|
|
|
|
$key = $variable;
|
|
|
|
$value = null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// SESSION_SECRET: 123
|
|
|
|
// SESSION_SECRET:
|
|
|
|
$key = Str::of($variableName);
|
|
|
|
$value = Str::of($variable);
|
|
|
|
}
|
|
|
|
if ($key->startsWith('SERVICE_FQDN')) {
|
|
|
|
if ($isNew) {
|
|
|
|
$name = $key->after('SERVICE_FQDN_')->beforeLast('_')->lower();
|
|
|
|
$fqdn = generateFqdn($server, "{$name->value()}-{$resource->uuid}");
|
|
|
|
if (substr_count($key->value(), '_') === 3) {
|
|
|
|
// SERVICE_FQDN_UMAMI_1000
|
|
|
|
$port = $key->afterLast('_');
|
|
|
|
} else {
|
|
|
|
// SERVICE_FQDN_UMAMI
|
|
|
|
$port = null;
|
|
|
|
}
|
|
|
|
if ($port) {
|
|
|
|
$fqdn = "$fqdn:$port";
|
|
|
|
}
|
|
|
|
if (substr_count($key->value(), '_') >= 2) {
|
|
|
|
if (is_null($value)) {
|
|
|
|
$value = Str::of('/');
|
|
|
|
}
|
|
|
|
$path = $value->value();
|
|
|
|
if ($generatedServiceFQDNS->count() > 0) {
|
|
|
|
$alreadyGenerated = $generatedServiceFQDNS->has($key->value());
|
|
|
|
if ($alreadyGenerated) {
|
|
|
|
$fqdn = $generatedServiceFQDNS->get($key->value());
|
|
|
|
} else {
|
|
|
|
$generatedServiceFQDNS->put($key->value(), $fqdn);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$generatedServiceFQDNS->put($key->value(), $fqdn);
|
|
|
|
}
|
|
|
|
$fqdn = "$fqdn$path";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($value?->startsWith('$')) {
|
|
|
|
$foundEnv = EnvironmentVariable::where([
|
|
|
|
'key' => $key,
|
2023-12-01 11:34:23 +00:00
|
|
|
'service_id' => $resource->id,
|
2023-11-24 14:48:23 +00:00
|
|
|
])->first();
|
2023-12-01 11:34:23 +00:00
|
|
|
$value = Str::of(replaceVariables($value));
|
|
|
|
$key = $value;
|
|
|
|
|
2023-11-24 14:48:23 +00:00
|
|
|
if ($value->startsWith('SERVICE_')) {
|
2023-12-01 11:34:23 +00:00
|
|
|
$foundEnv = EnvironmentVariable::where([
|
|
|
|
'key' => $key,
|
|
|
|
'application_id' => $resource->id,
|
|
|
|
])->first();
|
2023-11-28 11:05:04 +00:00
|
|
|
['command' => $command, 'forService' => $forService, 'generatedValue' => $generatedValue, 'port' => $port] = parseEnvVariable($value);
|
2023-11-24 14:48:23 +00:00
|
|
|
if ($command->value() === 'FQDN' || $command->value() === 'URL') {
|
|
|
|
if (Str::lower($forService) === $serviceName) {
|
|
|
|
$fqdn = generateFqdn($server, $containerName);
|
|
|
|
} else {
|
|
|
|
$fqdn = generateFqdn($server, Str::lower($forService) . '-' . $resource->uuid);
|
|
|
|
}
|
|
|
|
if ($port) {
|
|
|
|
$fqdn = "$fqdn:$port";
|
|
|
|
}
|
|
|
|
if ($foundEnv) {
|
|
|
|
$fqdn = data_get($foundEnv, 'value');
|
|
|
|
} else {
|
|
|
|
if ($command->value() === 'URL') {
|
|
|
|
$fqdn = Str::of($fqdn)->after('://')->value();
|
|
|
|
}
|
|
|
|
EnvironmentVariable::create([
|
|
|
|
'key' => $key,
|
|
|
|
'value' => $fqdn,
|
|
|
|
'is_build_time' => false,
|
|
|
|
'application_id' => $resource->id,
|
|
|
|
'is_preview' => false,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
} else {
|
2023-11-28 11:05:04 +00:00
|
|
|
$generatedValue = generateEnvValue($command);
|
2023-11-24 14:48:23 +00:00
|
|
|
if (!$foundEnv) {
|
|
|
|
EnvironmentVariable::create([
|
|
|
|
'key' => $key,
|
|
|
|
'value' => $generatedValue,
|
|
|
|
'is_build_time' => false,
|
|
|
|
'application_id' => $resource->id,
|
|
|
|
'is_preview' => false,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($value->contains(':-')) {
|
|
|
|
$key = $value->before(':');
|
|
|
|
$defaultValue = $value->after(':-');
|
|
|
|
} else if ($value->contains('-')) {
|
|
|
|
$key = $value->before('-');
|
|
|
|
$defaultValue = $value->after('-');
|
|
|
|
} else if ($value->contains(':?')) {
|
|
|
|
$key = $value->before(':');
|
|
|
|
$defaultValue = $value->after(':?');
|
|
|
|
} else if ($value->contains('?')) {
|
|
|
|
$key = $value->before('?');
|
|
|
|
$defaultValue = $value->after('?');
|
|
|
|
} else {
|
|
|
|
$key = $value;
|
|
|
|
$defaultValue = null;
|
|
|
|
}
|
|
|
|
if ($foundEnv) {
|
|
|
|
$defaultValue = data_get($foundEnv, 'value');
|
|
|
|
}
|
2023-11-27 10:54:55 +00:00
|
|
|
$isBuildTime = data_get($foundEnv, 'is_build_time', false);
|
2023-11-24 14:48:23 +00:00
|
|
|
EnvironmentVariable::updateOrCreate([
|
2023-11-27 10:54:55 +00:00
|
|
|
'key' => $key->value(),
|
2023-11-24 14:48:23 +00:00
|
|
|
'application_id' => $resource->id,
|
|
|
|
], [
|
|
|
|
'value' => $defaultValue,
|
2023-11-27 10:54:55 +00:00
|
|
|
'is_build_time' => $isBuildTime,
|
|
|
|
'application_id' => $resource->id,
|
2023-11-24 14:48:23 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add labels to the service
|
|
|
|
if ($resource->serviceType()) {
|
|
|
|
$fqdns = generateServiceSpecificFqdns($resource, forTraefik: true);
|
|
|
|
} else {
|
|
|
|
$domains = collect(json_decode($resource->docker_compose_domains)) ?? [];
|
|
|
|
if ($domains) {
|
|
|
|
$fqdns = data_get($domains, "$serviceName.domain");
|
|
|
|
if ($fqdns) {
|
|
|
|
$fqdns = str($fqdns)->explode(',');
|
2023-11-27 13:28:21 +00:00
|
|
|
$uuid = new Cuid2(7);
|
|
|
|
if ($pull_request_id !== 0) {
|
|
|
|
$fqdns = $fqdns->map(function ($fqdn) use ($pull_request_id, $resource) {
|
|
|
|
$preview = ApplicationPreview::findPreviewByApplicationAndPullId($resource->id, $pull_request_id);
|
|
|
|
$url = Url::fromString($fqdn);
|
|
|
|
$template = $resource->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}}', $pull_request_id, $preview_fqdn);
|
|
|
|
$preview_fqdn = "$schema://$preview_fqdn";
|
|
|
|
$preview->fqdn = $preview_fqdn;
|
|
|
|
$preview->save();
|
|
|
|
return $preview_fqdn;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
$serviceLabels = $serviceLabels->merge(fqdnLabelsForTraefik($uuid, $fqdns));
|
2023-11-24 14:48:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-27 13:28:21 +00:00
|
|
|
$defaultLabels = defaultLabels($resource->id, $containerName, $pull_request_id, type: 'application');
|
2023-11-24 14:48:23 +00:00
|
|
|
$serviceLabels = $serviceLabels->merge($defaultLabels);
|
|
|
|
|
|
|
|
if ($server->isLogDrainEnabled() && $resource->isLogDrainEnabled()) {
|
|
|
|
data_set($service, 'logging', [
|
|
|
|
'driver' => 'fluentd',
|
|
|
|
'options' => [
|
|
|
|
'fluentd-address' => "tcp://127.0.0.1:24224",
|
|
|
|
'fluentd-async' => "true",
|
|
|
|
'fluentd-sub-second-precision' => "true",
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
data_set($service, 'labels', $serviceLabels->toArray());
|
|
|
|
data_forget($service, 'is_database');
|
|
|
|
data_set($service, 'restart', RESTART_MODE);
|
|
|
|
data_set($service, 'container_name', $containerName);
|
|
|
|
data_forget($service, 'volumes.*.content');
|
|
|
|
data_forget($service, 'volumes.*.isDirectory');
|
2023-11-27 13:28:21 +00:00
|
|
|
|
2023-11-24 14:48:23 +00:00
|
|
|
return $service;
|
|
|
|
});
|
|
|
|
$finalServices = [
|
|
|
|
'version' => $dockerComposeVersion,
|
|
|
|
'services' => $services->toArray(),
|
|
|
|
'volumes' => $topLevelVolumes->toArray(),
|
|
|
|
'networks' => $topLevelNetworks->toArray(),
|
|
|
|
];
|
2023-11-28 09:11:53 +00:00
|
|
|
if ($isSameDockerComposeFile) {
|
|
|
|
$resource->docker_compose_pr_raw = Yaml::dump($yaml, 10, 2);
|
|
|
|
$resource->docker_compose_pr = Yaml::dump($finalServices, 10, 2);
|
|
|
|
$resource->docker_compose_raw = Yaml::dump($yaml, 10, 2);
|
|
|
|
$resource->docker_compose = Yaml::dump($finalServices, 10, 2);
|
|
|
|
} else {
|
|
|
|
if ($is_pr) {
|
|
|
|
$resource->docker_compose_pr_raw = Yaml::dump($yaml, 10, 2);
|
|
|
|
$resource->docker_compose_pr = Yaml::dump($finalServices, 10, 2);
|
|
|
|
} else {
|
|
|
|
$resource->docker_compose_raw = Yaml::dump($yaml, 10, 2);
|
|
|
|
$resource->docker_compose = Yaml::dump($finalServices, 10, 2);
|
|
|
|
}
|
|
|
|
}
|
2023-11-24 14:48:23 +00:00
|
|
|
$resource->save();
|
|
|
|
return collect($finalServices);
|
|
|
|
}
|
|
|
|
}
|
2023-11-28 11:05:04 +00:00
|
|
|
|
|
|
|
function parseEnvVariable(Str|string $value)
|
|
|
|
{
|
|
|
|
$value = str($value);
|
|
|
|
$count = substr_count($value->value(), '_');
|
|
|
|
$command = null;
|
|
|
|
$forService = null;
|
|
|
|
$generatedValue = null;
|
|
|
|
$port = null;
|
|
|
|
|
|
|
|
if ($count === 2) {
|
|
|
|
if ($value->startsWith('SERVICE_FQDN') || $value->startsWith('SERVICE_URL')) {
|
|
|
|
// SERVICE_FQDN_UMAMI
|
|
|
|
$command = $value->after('SERVICE_')->beforeLast('_');
|
|
|
|
$forService = $value->afterLast('_');
|
|
|
|
} else {
|
|
|
|
// SERVICE_BASE64_UMAMI
|
|
|
|
$command = $value->after('SERVICE_')->beforeLast('_');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($count === 3) {
|
|
|
|
if ($value->startsWith('SERVICE_FQDN') || $value->startsWith('SERVICE_URL')) {
|
|
|
|
// SERVICE_FQDN_UMAMI_1000
|
|
|
|
$command = $value->after('SERVICE_')->before('_');
|
|
|
|
$forService = $value->after('SERVICE_')->after('_')->before('_');
|
|
|
|
$port = $value->afterLast('_');
|
|
|
|
} else {
|
|
|
|
// SERVICE_BASE64_64_UMAMI
|
|
|
|
$command = $value->after('SERVICE_')->beforeLast('_');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
'command' => $command,
|
|
|
|
'forService' => $forService,
|
|
|
|
'generatedValue' => $generatedValue,
|
|
|
|
'port' => $port,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
function generateEnvValue(string $command)
|
|
|
|
{
|
|
|
|
switch ($command) {
|
|
|
|
case 'PASSWORD':
|
|
|
|
$generatedValue = Str::password(symbols: false);
|
|
|
|
break;
|
|
|
|
case 'PASSWORD_64':
|
|
|
|
$generatedValue = Str::password(length: 64, symbols: false);
|
|
|
|
break;
|
|
|
|
case 'BASE64_64':
|
|
|
|
$generatedValue = Str::random(64);
|
|
|
|
break;
|
|
|
|
case 'BASE64_128':
|
|
|
|
$generatedValue = Str::random(128);
|
|
|
|
break;
|
|
|
|
case 'BASE64':
|
|
|
|
$generatedValue = Str::random(32);
|
|
|
|
break;
|
|
|
|
case 'USER':
|
|
|
|
$generatedValue = Str::random(16);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return $generatedValue;
|
|
|
|
}
|
2023-12-11 19:01:54 +00:00
|
|
|
|
2023-12-11 19:13:41 +00:00
|
|
|
function getRealtime()
|
|
|
|
{
|
2023-12-11 19:01:54 +00:00
|
|
|
$envDefined = env('PUSHER_PORT');
|
2023-12-11 19:22:31 +00:00
|
|
|
if (empty($envDefined)) {
|
2023-12-11 19:13:41 +00:00
|
|
|
$url = Url::fromString(Request::getSchemeAndHttpHost());
|
|
|
|
$port = $url->getPort();
|
|
|
|
if ($port) {
|
|
|
|
return '6001';
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2023-12-11 19:01:54 +00:00
|
|
|
} else {
|
2023-12-11 19:13:41 +00:00
|
|
|
return $envDefined;
|
2023-12-11 19:01:54 +00:00
|
|
|
}
|
|
|
|
}
|