Skip to content

Commit

Permalink
Merge pull request coollabsio#1126 from coollabsio/next
Browse files Browse the repository at this point in the history
v4.0.0-beta.17
  • Loading branch information
andrasbacsai authored Jul 7, 2023
2 parents e4d3ecb + 18d2690 commit 41e8683
Show file tree
Hide file tree
Showing 28 changed files with 424 additions and 1,070 deletions.
12 changes: 6 additions & 6 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace App\Console;

use App\Jobs\InstanceAutoUpdateJob;
use App\Jobs\InstanceProxyCheckJob;
use App\Jobs\InstanceDockerCleanupJob;
use App\Jobs\ProxyCheckJob;
use App\Jobs\DockerCleanupJob;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

Expand All @@ -14,12 +14,12 @@ protected function schedule(Schedule $schedule): void
{
if (isDev()) {
$schedule->command('horizon:snapshot')->everyMinute();
// $schedule->job(new InstanceDockerCleanupJob)->everyMinute();
// $schedule->job(new DockerCleanupJob)->everyOddHour();
// $schedule->job(new InstanceAutoUpdateJob(true))->everyMinute();
} else {
$schedule->command('horizon:snapshot')->everyFiveMinutes();
$schedule->job(new InstanceDockerCleanupJob)->everyFiveMinutes();
$schedule->job(new InstanceProxyCheckJob)->everyFiveMinutes();
$schedule->job(new DockerCleanupJob)->everyTenMinutes();
$schedule->job(new ProxyCheckJob)->everyFiveMinutes();
$schedule->job(new InstanceAutoUpdateJob)->everyTenMinutes();
}
}
Expand All @@ -29,4 +29,4 @@ protected function commands(): void

require base_path('routes/console.php');
}
}
}
22 changes: 14 additions & 8 deletions app/Http/Livewire/Project/Application/DeploymentNavbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Enums\ApplicationDeploymentStatus;
use App\Models\Application;
use App\Models\ApplicationDeploymentQueue;
use App\Models\Server;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Process;
use Livewire\Component;
Expand All @@ -15,29 +16,34 @@ class DeploymentNavbar extends Component
protected $listeners = ['deploymentFinished'];

public ApplicationDeploymentQueue $application_deployment_queue;
public Application $application;
public Server $server;
public bool $is_debug_enabled = false;

public function mount()
{
$this->application = Application::find($this->application_deployment_queue->application_id);
$this->server = $this->application->destination->server;
$this->is_debug_enabled = $this->application->settings->is_debug_enabled;
}
public function deploymentFinished()
{
$this->application_deployment_queue->refresh();
}
public function show_debug()
{
$application = Application::find($this->application_deployment_queue->application_id);
$application->settings->is_debug_enabled = !$application->settings->is_debug_enabled;
$application->settings->save();
$this->is_debug_enabled = $application->settings->is_debug_enabled;
$this->application->settings->is_debug_enabled = !$this->application->settings->is_debug_enabled;
$this->application->settings->save();
$this->is_debug_enabled = $this->application->settings->is_debug_enabled;
$this->emit('refreshQueue');
}
public function cancel()
{
try {
$kill_command = "kill -9 {$this->application_deployment_queue->current_process_id}";
$application = Application::find($this->application_deployment_queue->application_id);
$server = $application->destination->server;
if ($this->application_deployment_queue->current_process_id) {
$process = Process::run("ps -p {$this->application_deployment_queue->current_process_id} -o command --no-headers");
if (Str::of($process->output())->contains([$server->ip, 'EOF-COOLIFY-SSH'])) {
if (Str::of($process->output())->contains([$this->server->ip, 'EOF-COOLIFY-SSH'])) {
Process::run($kill_command);
}
$previous_logs = json_decode($this->application_deployment_queue->logs, associative: true, flags: JSON_THROW_ON_ERROR);
Expand All @@ -60,4 +66,4 @@ public function cancel()
return general_error_handler(err: $e, that: $this);
}
}
}
}
15 changes: 13 additions & 2 deletions app/Http/Livewire/Project/Application/General.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class General extends Component
public bool $is_force_https_enabled;

protected $rules = [
'application.name' => 'required|min:6',
'application.name' => 'required',
'application.fqdn' => 'nullable',
'application.git_repository' => 'required',
'application.git_branch' => 'required',
Expand Down Expand Up @@ -67,13 +67,19 @@ public function instantSave()
{
// @TODO: find another way - if possible
$this->application->settings->is_static = $this->is_static;
if ($this->is_static) {
$this->application->ports_exposes = 80;
} else {
$this->application->ports_exposes = 3000;
}
$this->application->settings->is_git_submodules_enabled = $this->is_git_submodules_enabled;
$this->application->settings->is_git_lfs_enabled = $this->is_git_lfs_enabled;
$this->application->settings->is_debug_enabled = $this->is_debug_enabled;
$this->application->settings->is_preview_deployments_enabled = $this->is_preview_deployments_enabled;
$this->application->settings->is_auto_deploy_enabled = $this->is_auto_deploy_enabled;
$this->application->settings->is_force_https_enabled = $this->is_force_https_enabled;
$this->application->settings->save();
$this->application->save();
$this->application->refresh();
$this->emit('success', 'Application settings updated!');
$this->checkWildCardDomain();
Expand Down Expand Up @@ -126,7 +132,12 @@ public function submit()
$domains = Str::of($this->application->fqdn)->trim()->explode(',')->map(function ($domain) {
return Str::of($domain)->trim()->lower();
});

if ($this->application->base_directory && $this->application->base_directory !== '/') {
$this->application->base_directory = rtrim($this->application->base_directory, '/');
}
if ($this->application->publish_directory && $this->application->publish_directory !== '/') {
$this->application->publish_directory = rtrim($this->application->publish_directory, '/');
}
$this->application->fqdn = $domains->implode(',');
$this->application->save();
$this->emit('success', 'Application settings updated!');
Expand Down
5 changes: 4 additions & 1 deletion app/Http/Livewire/Server/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Form extends Component
public $uptime;
public $dockerVersion;
public string|null $wildcard_domain = null;
public int $cleanup_after_percentage;

protected $rules = [
'server.name' => 'required|min:6',
Expand All @@ -35,6 +36,7 @@ class Form extends Component
public function mount()
{
$this->wildcard_domain = $this->server->settings->wildcard_domain;
$this->cleanup_after_percentage = $this->server->settings->cleanup_after_percentage;
}
public function installDocker()
{
Expand Down Expand Up @@ -91,8 +93,9 @@ public function submit()
// return;
// }
$this->server->settings->wildcard_domain = $this->wildcard_domain;
$this->server->settings->cleanup_after_percentage = $this->cleanup_after_percentage;
$this->server->settings->save();
$this->server->save();
$this->emit('success', 'Server updated successfully.');
}
}
}
4 changes: 2 additions & 2 deletions app/Http/Livewire/Settings/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Http\Livewire\Settings;

use App\Jobs\InstanceProxyCheckJob;
use App\Jobs\ProxyCheckJob;
use App\Models\InstanceSettings as ModelsInstanceSettings;
use App\Models\Server;
use Livewire\Component;
Expand Down Expand Up @@ -138,7 +138,7 @@ public function submit()
$this->server = Server::findOrFail(0);
$this->setup_instance_fqdn();
if ($this->settings->fqdn) {
dispatch(new InstanceProxyCheckJob());
dispatch(new ProxyCheckJob());
}
$this->emit('success', 'Instance settings updated successfully!');
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Livewire/Upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ public function upgrade()
return general_error_handler(err: $e, that: $this);
}
}
}
}
66 changes: 47 additions & 19 deletions app/Jobs/ApplicationDeploymentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;
use Spatie\Url\Url;
use Illuminate\Support\Str;
use Symfony\Component\Yaml\Yaml;
Expand Down Expand Up @@ -48,6 +49,7 @@ class ApplicationDeploymentJob implements ShouldQueue

private string $container_name;
private string $workdir;
private string $build_workdir;
private string $build_image_name;
private string $production_image_name;
private bool $is_debug_enabled;
Expand Down Expand Up @@ -76,6 +78,7 @@ public function __construct(int $application_deployment_queue_id)
$this->private_key_location = save_private_key_for_server($this->server);

$this->workdir = "/artifacts/{$this->deployment_uuid}";
$this->build_workdir = "{$this->workdir}" . rtrim($this->application->base_directory, '/');
$this->is_debug_enabled = $this->application->settings->is_debug_enabled;

$this->container_name = generate_container_name($this->application->uuid, $this->pull_request_id);
Expand Down Expand Up @@ -104,6 +107,7 @@ public function __construct(int $application_deployment_queue_id)

public function handle(): void
{
ray()->measure();
$this->application_deployment_queue->update([
'status' => ApplicationDeploymentStatus::IN_PROGRESS->value,
]);
Expand All @@ -116,31 +120,32 @@ public function handle(): void
$this->next(ApplicationDeploymentStatus::FINISHED->value);
} catch (\Exception $e) {
ray($e);
$this->execute_remote_command([
["echo '\nOops something is not okay, are you okay? 😢'"],
["echo '\n\n{$e->getMessage()}'"]
]);
$this->fail($e->getMessage());
$this->fail($e);
} finally {
// if (isset($this->docker_compose)) {
// Storage::disk('deployments')->put(Str::kebab($this->application->name) . '/docker-compose.yml', $this->docker_compose);
// }
if (isset($this->docker_compose)) {
Storage::disk('deployments')->put(Str::kebab($this->application->name) . '/docker-compose.yml', $this->docker_compose);
}
$this->execute_remote_command(
[
"docker rm -f {$this->deployment_uuid} >/dev/null 2>&1",
"hidden" => true,
]
);
ray()->measure();
}
}
public function failed(Throwable $exception): void
{
ray($exception);
$this->execute_remote_command(
["echo 'Oops something is not okay, are you okay? 😢'"],
["echo '{$exception->getMessage()}'"]
);
$this->next(ApplicationDeploymentStatus::FAILED->value);
}
private function execute_in_builder(string $command)
{
return "docker exec {$this->deployment_uuid} bash -c '{$command} |& tee -a /proc/1/fd/1'";
return "docker exec {$this->deployment_uuid} bash -c '{$command}'";
// return "docker exec {$this->deployment_uuid} bash -c '{$command} |& tee -a /proc/1/fd/1; [ \$PIPESTATUS -eq 0 ] || exit \$PIPESTATUS'";
}
private function deploy()
{
Expand All @@ -153,13 +158,18 @@ private function deploy()
$this->prepare_builder_image();
$this->clone_repository();

$this->build_image_name = "{$this->application->git_repository}:{$this->commit}-build";
$this->production_image_name = "{$this->application->uuid}:{$this->commit}";
$tag = Str::of("{$this->commit}-{$this->application->id}-{$this->pull_request_id}");
if (strlen($tag) > 128) {
$tag = $tag->substr(0, 128);
};

$this->build_image_name = "{$this->application->git_repository}:{$tag}-build";
$this->production_image_name = "{$this->application->uuid}:{$tag}";
ray('Build Image Name: ' . $this->build_image_name . ' & Production Image Name: ' . $this->production_image_name)->green();

if (!$this->force_rebuild) {
$this->execute_remote_command([
"docker images -q {$this->application->uuid}:{$this->commit} 2>/dev/null", "hidden" => true, "save" => "local_image_found"
"docker images -q {$this->production_image_name} 2>/dev/null", "hidden" => true, "save" => "local_image_found"
]);
if (Str::of($this->saved_outputs->get('local_image_found'))->isNotEmpty()) {
$this->execute_remote_command([
Expand Down Expand Up @@ -237,15 +247,34 @@ private function build_image()
$this->execute_in_builder("docker build -f {$this->workdir}/Dockerfile {$this->build_args} --progress plain -t $this->build_image_name {$this->workdir}"), "hidden" => true
]);

$dockerfile = "FROM {$this->application->static_image}
$dockerfile = base64_encode("FROM {$this->application->static_image}
WORKDIR /usr/share/nginx/html/
LABEL coolify.deploymentId={$this->deployment_uuid}
COPY --from=$this->build_image_name /app/{$this->application->publish_directory} .";
$docker_file = base64_encode($dockerfile);

COPY --from=$this->build_image_name /app/{$this->application->publish_directory} .
COPY ./nginx.conf /etc/nginx/conf.d/default.conf");

$nginx_config = base64_encode("server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html;
try_files \$uri \$uri/index.html \$uri/ /index.html =404;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}");
$this->execute_remote_command(
[
$this->execute_in_builder("echo '{$docker_file}' | base64 -d > {$this->workdir}/Dockerfile-prod")
$this->execute_in_builder("echo '{$dockerfile}' | base64 -d > {$this->workdir}/Dockerfile-prod")
],
[
$this->execute_in_builder("echo '{$nginx_config}' | base64 -d > {$this->workdir}/nginx.conf")
],
[
$this->execute_in_builder("docker build -f {$this->workdir}/Dockerfile-prod {$this->build_args} --progress plain -t $this->production_image_name {$this->workdir}"), "hidden" => true
Expand Down Expand Up @@ -360,7 +389,6 @@ private function generate_local_persistent_volumes()
}
$local_persistent_volumes[] = $volume_name . ':' . $persistentStorage->mount_path;
}
ray('local_persistent_volumes', $local_persistent_volumes)->green();
return $local_persistent_volumes;
}
private function generate_local_persistent_volumes_only_volume_names()
Expand Down
Loading

0 comments on commit 41e8683

Please sign in to comment.