2023-05-24 12:26:50 +00:00
|
|
|
<?php
|
|
|
|
|
2023-10-20 12:51:01 +00:00
|
|
|
use App\Models\Application;
|
2023-06-12 10:00:01 +00:00
|
|
|
use App\Models\InstanceSettings;
|
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-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-08-17 13:19:37 +00:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
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-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)) {
|
|
|
|
return $livewire->emit('error', "Too many requests. Please try again in {$error->secondsUntilAvailable} seconds.");
|
|
|
|
}
|
|
|
|
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) {
|
2023-11-20 09:32:06 +00:00
|
|
|
$error->message = $customErrorMessage . ' ' . $message;
|
2023-09-15 13:34:25 +00:00
|
|
|
$message = $customErrorMessage . ' ' . $message;
|
|
|
|
}
|
2023-11-20 09:32:06 +00:00
|
|
|
|
2023-09-15 13:34:25 +00:00
|
|
|
if (isset($livewire)) {
|
2023-09-25 13:48:43 +00:00
|
|
|
return $livewire->emit('error', $message);
|
2023-09-15 13:34:25 +00:00
|
|
|
}
|
2023-11-20 09:32:06 +00:00
|
|
|
throw $error;
|
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-09-06 12:31:38 +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-14 12:26:14 +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);
|
|
|
|
}
|