refactor a lot of things

fix: postgres_passwords could be longer
feat: able to define postgresql databases from the ui
This commit is contained in:
Andras Bacsai 2023-08-09 15:57:53 +02:00
parent d18de24cf9
commit a378b5108e
42 changed files with 167 additions and 92 deletions

View File

@ -150,7 +150,7 @@ private function generate_environment_variables()
private function generate_init_scripts()
{
if (count($this->database->init_scripts) === 0) {
if (is_null($this->database->init_scripts) || count($this->database->init_scripts) === 0) {
return;
}
foreach ($this->database->init_scripts as $init_script) {

View File

@ -18,7 +18,7 @@ public function __invoke()
return;
}
$base_url = config('coolify.license_url');
if (isDev()) {
if (is_dev()) {
$base_url = 'http://host.docker.internal:8787';
}
$instance_id = config('app.id');

View File

@ -17,7 +17,7 @@ public function __invoke(bool $force)
$settings = InstanceSettings::get();
ray('Running InstanceAutoUpdateJob');
$localhost_name = 'localhost';
if (isDev()) {
if (is_dev()) {
$localhost_name = 'testing-local-docker-container';
}
$this->server = Server::where('name', $localhost_name)->firstOrFail();
@ -52,7 +52,7 @@ public function __invoke(bool $force)
private function update()
{
if (isDev()) {
if (is_dev()) {
ray("Running update on local docker container. Updating to $this->latest_version");
remote_process([
"sleep 10"

View File

@ -18,7 +18,7 @@ class Kernel extends ConsoleKernel
protected function schedule(Schedule $schedule): void
{
// $schedule->call(fn() => $this->check_scheduled_backups($schedule))->everyTenSeconds();
if (isDev()) {
if (is_dev()) {
$schedule->command('horizon:snapshot')->everyMinute();
$schedule->job(new InstanceApplicationsStatusJob)->everyMinute();
$schedule->job(new ProxyCheckJob)->everyFiveMinutes();

View File

@ -45,7 +45,7 @@ public function register(): void
{
$this->reportable(function (Throwable $e) {
$this->settings = InstanceSettings::get();
if ($this->settings->do_not_track || isDev()) {
if ($this->settings->do_not_track || is_dev()) {
return;
}
Integration::captureUnhandledException($e);

View File

@ -19,7 +19,7 @@ class Controller extends BaseController
public function subscription()
{
if (!isCloud()) {
if (!is_cloud()) {
abort(404);
}
return view('subscription', [
@ -29,7 +29,7 @@ public function subscription()
public function license()
{
if (!isCloud()) {
if (!is_cloud()) {
abort(404);
}
return view('settings.license', [

View File

@ -41,6 +41,9 @@ public function show()
public function new()
{
$type = request()->query('type');
$destination_uuid = request()->query('destination');
$project = session('currentTeam')->load(['projects'])->projects->where('uuid', request()->route('project_uuid'))->first();
if (!$project) {
return redirect()->route('dashboard');
@ -49,9 +52,14 @@ public function new()
if (!$environment) {
return redirect()->route('dashboard');
}
$type = request()->query('type');
if (in_array($type, DATABASE_TYPES)) {
$standalone_postgresql = create_standalone_postgresql($environment->id, $destination_uuid);
return redirect()->route('project.database.configuration', [
'project_uuid' => $project->uuid,
'environment_name' => $environment->name,
'database_uuid' => $standalone_postgresql->uuid,
]);
}
return view('project.new', [
'type' => $type
]);

View File

@ -17,7 +17,7 @@ class Heading extends Component
public function mount()
{
$this->parameters = getRouteParameters();
$this->parameters = get_route_parameters();
}
public function check_status()

View File

@ -20,7 +20,7 @@ class Previews extends Component
public function mount()
{
$this->pull_requests = collect();
$this->parameters = getRouteParameters();
$this->parameters = get_route_parameters();
}
public function loadStatus($pull_request_id)

View File

@ -16,7 +16,7 @@ class Rollback extends Component
public function mount()
{
$this->parameters = getRouteParameters();
$this->parameters = get_route_parameters();
}
public function rollbackImage($commit)

View File

@ -34,7 +34,7 @@ public function check_status()
public function mount()
{
$this->parameters = getRouteParameters();
$this->parameters = get_route_parameters();
}
public function stop()

View File

@ -12,7 +12,7 @@ class DeleteEnvironment extends Component
public function mount()
{
$this->parameters = getRouteParameters();
$this->parameters = get_route_parameters();
}
public function delete()

View File

@ -12,7 +12,7 @@ class DeleteProject extends Component
public function mount()
{
$this->parameters = getRouteParameters();
$this->parameters = get_route_parameters();
}
public function delete()

View File

@ -38,7 +38,7 @@ class GithubPrivateRepository extends Component
public function mount()
{
$this->parameters = getRouteParameters();
$this->parameters = get_route_parameters();
$this->query = request()->query();
$this->repositories = $this->branches = collect();
$this->github_apps = GithubApp::private();

View File

@ -50,10 +50,10 @@ class GithubPrivateRepositoryDeployKey extends Component
public function mount()
{
if (isDev()) {
if (is_dev()) {
$this->repository_url = 'https://github.com/coollabsio/coolify-examples';
}
$this->parameters = getRouteParameters();
$this->parameters = get_route_parameters();
$this->query = request()->query();
$this->private_keys = PrivateKey::where('team_id', session('currentTeam')->id)->where('id', '!=', 0)->get();
}

View File

@ -45,11 +45,11 @@ class PublicGitRepository extends Component
public function mount()
{
if (isDev()) {
if (is_dev()) {
$this->repository_url = 'https://github.com/coollabsio/coolify-examples';
$this->port = 3000;
}
$this->parameters = getRouteParameters();
$this->parameters = get_route_parameters();
$this->query = request()->query();
}

View File

@ -17,7 +17,7 @@ class Select extends Component
public function mount()
{
$this->parameters = getRouteParameters();
$this->parameters = get_route_parameters();
}
public function set_type(string $type)

View File

@ -14,7 +14,7 @@ class Danger extends Component
public function mount()
{
$this->modalId = new Cuid2(7);
$this->parameters = getRouteParameters();
$this->parameters = get_route_parameters();
}
public function delete()

View File

@ -26,7 +26,7 @@ class Add extends Component
public function mount()
{
$this->parameters = getRouteParameters();
$this->parameters = get_route_parameters();
}
public function submit()

View File

@ -25,7 +25,7 @@ class Show extends Component
public function mount()
{
$this->modalId = new Cuid2(7);
$this->parameters = getRouteParameters();
$this->parameters = get_route_parameters();
}
public function submit()

View File

@ -25,7 +25,7 @@ class Add extends Component
public function mount()
{
$this->parameters = getRouteParameters();
$this->parameters = get_route_parameters();
}
public function submit()

View File

@ -39,6 +39,6 @@ public function checkConnection()
public function mount()
{
$this->parameters = getRouteParameters();
$this->parameters = get_route_parameters();
}
}

View File

@ -38,7 +38,7 @@ class Change extends Component
public function mount()
{
$this->webhook_endpoint = $this->ipv4;
$this->parameters = getRouteParameters();
$this->parameters = get_route_parameters();
$this->is_system_wide = $this->github_app->is_system_wide;
}

View File

@ -15,7 +15,7 @@ class InviteLink extends Component
public function mount()
{
$this->email = isDev() ? 'test3@example.com' : '';
$this->email = is_dev() ? 'test3@example.com' : '';
}
public function viaEmail()

View File

@ -36,7 +36,7 @@ class Create extends Component
public function mount()
{
if (isDev()) {
if (is_dev()) {
$this->name = 'Local MinIO';
$this->description = 'Local MinIO';
$this->key = 'minioadmin';

View File

@ -18,7 +18,7 @@ public function checkUpdate()
$this->latestVersion = get_latest_version_of_coolify();
$currentVersion = config('version');
version_compare($currentVersion, $this->latestVersion, '<') ? $this->isUpgradeAvailable = true : $this->isUpgradeAvailable = false;
if (isDev()) {
if (is_dev()) {
$this->isUpgradeAvailable = true;
}
$settings = InstanceSettings::get();

View File

@ -12,7 +12,7 @@ class SubscriptionValid
public function handle(Request $request, Closure $next): Response
{
if (auth()->user()) {
if (isCloud() && !isSubscribed()) {
if (is_cloud() && !isSubscribed()) {
ray('SubscriptionValid Middleware');
$allowed_paths = [

View File

@ -32,7 +32,7 @@ public function handle(): void
try {
$servers = Server::all();
foreach ($servers as $server) {
if (isDev()) {
if (is_dev()) {
$docker_root_filesystem = "/";
} else {
$docker_root_filesystem = instant_remote_process(['stat --printf=%m $(docker info --format "{{json .DockerRootDir}}" |sed \'s/"//g\')'], $server);

View File

@ -0,0 +1,3 @@
<?php
const DATABASE_TYPES = ['postgresql'];

View File

@ -0,0 +1,28 @@
<?php
use App\Models\StandaloneDocker;
use App\Models\StandalonePostgresql;
use Visus\Cuid2\Cuid2;
function generate_database_name(string $type): string
{
$cuid = new Cuid2(7);
return $type . '-database-' . $cuid;
}
function create_standalone_postgresql($environment_id, $destination_uuid): StandalonePostgresql
{
// TODO: If another type of destination is added, this will need to be updated.
$destination = StandaloneDocker::where('uuid', $destination_uuid)->first();
if (!$destination) {
throw new Exception('Destination not found');
}
return StandalonePostgresql::create([
'name' => generate_database_name('postgresql'),
'postgres_password' => \Illuminate\Support\Str::password(),
'environment_id' => $environment_id,
'destination_id' => $destination->id,
'destination_type' => $destination->getMorphClass(),
]);
}

View File

@ -13,7 +13,7 @@ function get_proxy_path()
function generate_default_proxy_configuration(Server $server)
{
$proxy_path = get_proxy_path();
if (isDev()) {
if (is_dev()) {
$proxy_path = config('coolify.dev_config_path') . '/' . $server->name . '/proxy';
}
$networks = collect($server->standaloneDockers)->map(function ($docker) {
@ -85,7 +85,7 @@ function generate_default_proxy_configuration(Server $server)
],
],
];
if (isDev()) {
if (is_dev()) {
$config['services']['traefik']['command'][] = "--log.level=debug";
}
return Yaml::dump($config, 4, 2);

View File

@ -5,37 +5,45 @@
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use Nubs\RandomNameGenerator\All;
use Visus\Cuid2\Cuid2;
function application_configuration_dir() :string {
function application_configuration_dir(): string
{
return '/data/coolify/applications';
}
function database_configuration_dir(): string {
function database_configuration_dir(): string
{
return '/data/coolify/databases';
}
function backup_dir(): string {
function backup_dir(): string
{
return '/data/coolify/backups';
}
function generate_readme_file(string $name, string $updated_at): string {
return "Resource name: {$name}\nLatest Deployment Date: {$updated_at}";
function generate_readme_file(string $name, string $updated_at): string
{
return "Resource name: $name\nLatest Deployment Date: $updated_at";
}
function general_error_handler(\Throwable|null $err = null, $that = null, $isJson = false, $customErrorMessage = null)
function general_error_handler(Throwable|null $err = null, $that = null, $isJson = false, $customErrorMessage = null): mixed
{
try {
ray('ERROR OCCURED: ' . $err->getMessage());
ray('ERROR OCCURRED: ' . $err->getMessage());
if ($err instanceof QueryException) {
if ($err->errorInfo[0] === '23505') {
throw new \Exception($customErrorMessage ?? 'Duplicate entry found.', '23505');
throw new Exception($customErrorMessage ?? 'Duplicate entry found.', '23505');
} else if (count($err->errorInfo) === 4) {
throw new \Exception($customErrorMessage ?? $err->errorInfo[3]);
throw new Exception($customErrorMessage ?? $err->errorInfo[3]);
} else {
throw new \Exception($customErrorMessage ?? $err->errorInfo[2]);
throw new Exception($customErrorMessage ?? $err->errorInfo[2]);
}
} else {
throw new \Exception($customErrorMessage ?? $err->getMessage());
throw new Exception($customErrorMessage ?? $err->getMessage());
}
} catch (\Throwable $error) {
} catch (Throwable $error) {
if ($that) {
return $that->emit('error', $customErrorMessage ?? $error->getMessage());
} elseif ($isJson) {
@ -46,51 +54,54 @@ function general_error_handler(\Throwable|null $err = null, $that = null, $isJso
} else {
ray($customErrorMessage);
ray($error);
return $customErrorMessage ?? $error->getMessage();
}
}
}
function getRouteParameters()
function get_route_parameters(): array
{
return Route::current()->parameters();
}
function get_latest_version_of_coolify()
function get_latest_version_of_coolify(): string
{
try {
$response = Http::get('https://cdn.coollabs.io/coolify/versions.json');
$versions = $response->json();
return data_get($versions, 'coolify.v4.version');
} catch (\Throwable $th) {
} catch (Throwable $th) {
//throw $th;
ray($th->getMessage());
return '0.0.0';
}
}
function generate_random_name()
function generate_random_name(): string
{
$generator = \Nubs\RandomNameGenerator\All::create();
$generator = All::create();
$cuid = new Cuid2(7);
return Str::kebab("{$generator->getName()}-{$cuid}");
return Str::kebab("{$generator->getName()}-$cuid");
}
function generate_application_name(string $git_repository, string $git_branch)
function generate_application_name(string $git_repository, string $git_branch): string
{
$cuid = new Cuid2(7);
return Str::kebab("{$git_repository}:{$git_branch}-{$cuid}");
return Str::kebab("$git_repository:$git_branch-$cuid");
}
function is_transactional_emails_active()
function is_transactional_emails_active(): bool
{
return data_get(InstanceSettings::get(), 'smtp_enabled');
}
function set_transanctional_email_settings()
function set_transanctional_email_settings(): void
{
$settings = InstanceSettings::get();
$password = data_get($settings, 'smtp_password');
if ($password) $password = decrypt($password);
if (isset($password)) {
$password = decrypt($password);
}
config()->set('mail.default', 'smtp');
config()->set('mail.mailers.smtp', [
@ -105,16 +116,19 @@ function set_transanctional_email_settings()
]);
}
function base_ip()
function base_ip(): string
{
if (isDev()) {
if (is_dev()) {
return "http://localhost";
}
$settings = InstanceSettings::get();
return "http://{$settings->public_ipv4}";
return "http://$settings->public_ipv4";
}
function base_url(bool $withPort = true)
/**
* If fqdn is set, return it, otherwise return public ip.
*/
function base_url(bool $withPort = true): string
{
$settings = InstanceSettings::get();
if ($settings->fqdn) {
@ -123,31 +137,32 @@ function base_url(bool $withPort = true)
$port = config('app.port');
if ($settings->public_ipv4) {
if ($withPort) {
if (isDev()) {
return "http://localhost:{$port}";
if (is_dev()) {
return "http://localhost:$port";
}
return "http://{$settings->public_ipv4}:{$port}";
return "http://$settings->public_ipv4:$port";
}
if (isDev()) {
if (is_dev()) {
return "http://localhost";
}
return "http://{$settings->public_ipv4}";
return "http://$settings->public_ipv4";
}
if ($settings->public_ipv6) {
if ($withPort) {
return "http://{$settings->public_ipv6}:{$port}";
return "http://$settings->public_ipv6:$port";
}
return "http://{$settings->public_ipv6}";
return "http://$settings->public_ipv6";
}
return url('/');
}
function isDev()
function is_dev(): bool
{
return config('app.env') === 'local';
}
function isCloud()
function is_cloud(): bool
{
return !config('coolify.self_hosted');
}

View File

@ -0,0 +1,21 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void
{
Schema::table('standalone_postgresqls', function (Blueprint $table) {
$table->text('postgres_password')->change();
});
}
public function down(): void
{
Schema::table('standalone_postgresqls', function (Blueprint $table) {
$table->string('postgres_password')->change();
});
}
};

View File

@ -25,7 +25,7 @@ class="text-xs text-white rounded-none hover:no-underline hover:bg-coollabs hove
<path d="M9 15l6 -6"/>
<path d="M11 6l.463 -.536a5 5 0 0 1 7.071 7.072l-.534 .464"/>
<path
d="M13 18l-.397 .534a5.068 5.068 0 0 1 -7.127 0a4.972 4.972 0 0 1 0 -7.071l.524 -.463"/>
d="M13 18l-.397 .534a5.068 5.068 0 0 1 -7.127 0a4.972 4.972 0 0 1 0 -7.071l.524 -.463"/>
</svg>{{ $fqdn }}
</a>
</li>
@ -44,7 +44,7 @@ class="text-xs text-white rounded-none hover:no-underline hover:bg-coollabs hove
<path d="M9 15l6 -6"/>
<path d="M11 6l.463 -.536a5 5 0 0 1 7.071 7.072l-.534 .464"/>
<path
d="M13 18l-.397 .534a5.068 5.068 0 0 1 -7.127 0a4.972 4.972 0 0 1 0 -7.071l.524 -.463"/>
d="M13 18l-.397 .534a5.068 5.068 0 0 1 -7.127 0a4.972 4.972 0 0 1 0 -7.071l.524 -.463"/>
</svg>
PR{{ data_get($preview, 'pull_request_id') }} |
{{ data_get($preview, 'fqdn') }}
@ -55,7 +55,7 @@ class="text-xs text-white rounded-none hover:no-underline hover:bg-coollabs hove
@endif
@if (data_get($application, 'ports_mappings_array'))
@foreach ($application->ports_mappings_array as $port)
@if (isDev())
@if (is_dev())
<li>
<a class="text-xs text-white rounded-none hover:no-underline hover:bg-coollabs hover:text-white"
target="_blank" href="http://localhost:{{ explode(':', $port)[0] }}">
@ -66,7 +66,7 @@ class="text-xs text-white rounded-none hover:no-underline hover:bg-coollabs hove
<path d="M9 15l6 -6"/>
<path d="M11 6l.463 -.536a5 5 0 0 1 7.071 7.072l-.534 .464"/>
<path
d="M13 18l-.397 .534a5.068 5.068 0 0 1 -7.127 0a4.972 4.972 0 0 1 0 -7.071l.524 -.463"/>
d="M13 18l-.397 .534a5.068 5.068 0 0 1 -7.127 0a4.972 4.972 0 0 1 0 -7.071l.524 -.463"/>
</svg>
Port {{ $port }}
</a>
@ -83,7 +83,7 @@ class="text-xs text-white rounded-none hover:no-underline hover:bg-coollabs hove
<path d="M9 15l6 -6"/>
<path d="M11 6l.463 -.536a5 5 0 0 1 7.071 7.072l-.534 .464"/>
<path
d="M13 18l-.397 .534a5.068 5.068 0 0 1 -7.127 0a4.972 4.972 0 0 1 0 -7.071l.524 -.463"/>
d="M13 18l-.397 .534a5.068 5.068 0 0 1 -7.127 0a4.972 4.972 0 0 1 0 -7.071l.524 -.463"/>
</svg>
Port {{ $port }}
</a>

View File

@ -10,7 +10,7 @@
href="{{ route('settings.emails') }}">
<button>SMTP</button>
</a>
@if (isCloud())
@if (is_cloud())
<a class="{{ request()->routeIs('settings.license') ? 'text-white' : '' }}"
href="{{ route('settings.license') }}">
<button>Resale License</button>

View File

@ -22,7 +22,7 @@
<div class="stat-value">{{ $s3s->count() }}</div>
</div>
</div>
@if (isDev())
@if (is_dev())
<livewire:dev.s3-test/>
<livewire:dev.scheduled-backups/>
@endif

View File

@ -23,7 +23,7 @@
@if (data_get($model, 'discord_enabled'))
<h4 class="mt-4">Subscribe to events</h4>
<div class="w-64 ">
@if (isDev())
@if (is_dev())
<x-forms.checkbox instantSave="saveModel" id="model.discord_notifications_test" label="Test"/>
@endif
<h5 class="mt-4">Applications</h5>

View File

@ -60,7 +60,7 @@ class="text-white normal-case btn btn-xs no-animation btn-primary">
@if (data_get($model, 'smtp_enabled'))
<h4 class="mt-4">Subscribe to events</h4>
<div class="w-64">
@if (isDev())
@if (is_dev())
<x-forms.checkbox instantSave="saveModel" id="model.smtp_notifications_test" label="Test"/>
@endif
<h5 class="mt-4">Applications</h5>

View File

@ -87,7 +87,7 @@
<div class="pb-4">This will remove this server from Coolify. Beware! There is no coming
back!
</div>
@if ($server->id !== 0 || isDev())
@if ($server->id !== 0 || is_dev())
<x-forms.button isError isModal modalId="deleteServer">
Delete
</x-forms.button>

View File

@ -1,7 +1,7 @@
<x-layout>
<x-team.navbar :team="session('currentTeam')"/>
<livewire:team.form/>
@if (isCloud())
@if (is_cloud())
<div class="pb-8">
<h3>Subscription</h3>
@if (data_get(auth()->user()->currentTeam(),

View File

@ -174,7 +174,7 @@
}
});
if (isCloud()) {
if (is_cloud()) {
Route::post('/payments/events', function () {
try {
$secret = config('coolify.lemon_squeezy_webhook_secret');

View File

@ -1,10 +1,10 @@
{
"coolify": {
"main": {
"version": "3.12.33"
},
"v4": {
"version": "4.0.0-beta.18"
}
"coolify": {
"main": {
"version": "3.12.36"
},
"v4": {
"version": "4.0.0-beta.18"
}
}
}
}