Skip to content

Commit b417565

Browse files
authored
Merge branch 'next' into next
2 parents 58dcf76 + 0e54ed1 commit b417565

File tree

18 files changed

+760
-305
lines changed

18 files changed

+760
-305
lines changed

app/Jobs/ApplicationDeploymentJob.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
157157

158158
private ?string $coolify_variables = null;
159159

160-
private bool $preserveRepository = true;
160+
private bool $preserveRepository = false;
161161

162162
public $tries = 1;
163163

@@ -480,6 +480,7 @@ private function deploy_docker_compose_buildpack()
480480
}
481481

482482
// Start compose file
483+
$server_workdir = $this->application->workdir();
483484
if ($this->application->settings->is_raw_compose_deployment_enabled) {
484485
if ($this->docker_compose_custom_start_command) {
485486
$this->write_deployment_configurations();
@@ -488,7 +489,6 @@ private function deploy_docker_compose_buildpack()
488489
);
489490
} else {
490491
$this->write_deployment_configurations();
491-
$server_workdir = $this->application->workdir();
492492
$this->docker_compose_location = '/docker-compose.yaml';
493493

494494
$command = "{$this->coolify_variables} docker compose";
@@ -508,15 +508,26 @@ private function deploy_docker_compose_buildpack()
508508
);
509509
} else {
510510
$command = "{$this->coolify_variables} docker compose";
511-
if ($this->env_filename) {
512-
$command .= " --env-file {$this->workdir}/{$this->env_filename}";
511+
if ($this->preserveRepository) {
512+
if ($this->env_filename) {
513+
$command .= " --env-file {$server_workdir}/{$this->env_filename}";
514+
}
515+
$command .= " --project-name {$this->application->uuid} --project-directory {$server_workdir} -f {$server_workdir}{$this->docker_compose_location} up -d";
516+
$this->write_deployment_configurations();
517+
518+
$this->execute_remote_command(
519+
['command' => $command, 'hidden' => true],
520+
);
521+
} else {
522+
if ($this->env_filename) {
523+
$command .= " --env-file {$this->workdir}/{$this->env_filename}";
524+
}
525+
$command .= " --project-name {$this->application->uuid} --project-directory {$this->workdir} -f {$this->workdir}{$this->docker_compose_location} up -d";
526+
$this->execute_remote_command(
527+
[executeInDocker($this->deployment_uuid, $command), 'hidden' => true],
528+
);
513529
}
514-
$command .= " --project-name {$this->application->uuid} --project-directory {$this->workdir} -f {$this->workdir}{$this->docker_compose_location} up -d";
515530

516-
$this->write_deployment_configurations();
517-
$this->execute_remote_command(
518-
[executeInDocker($this->deployment_uuid, $command), 'hidden' => true],
519-
);
520531
}
521532
}
522533

@@ -619,6 +630,11 @@ private function write_deployment_configurations()
619630
],
620631
);
621632
}
633+
$this->application->fileStorages()->each(function ($fileStorage) {
634+
if (! $fileStorage->is_based_on_git && ! $fileStorage->is_directory) {
635+
$fileStorage->saveStorageOnServer();
636+
}
637+
});
622638
if ($this->use_build_server) {
623639
$this->server = $this->build_server;
624640
}

app/Livewire/Project/Application/General.php

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace App\Livewire\Project\Application;
44

55
use App\Models\Application;
6-
use App\Models\LocalFileVolume;
76
use Illuminate\Support\Collection;
87
use Livewire\Component;
98
use Visus\Cuid2\Cuid2;
@@ -30,6 +29,8 @@ class General extends Component
3029

3130
public ?string $ports_exposes = null;
3231

32+
public bool $is_preserve_repository_enabled = false;
33+
3334
public bool $is_container_label_escape_enabled = true;
3435

3536
public $customLabels;
@@ -145,6 +146,7 @@ public function mount()
145146
}
146147
$this->parsedServiceDomains = $this->application->docker_compose_domains ? json_decode($this->application->docker_compose_domains, true) : [];
147148
$this->ports_exposes = $this->application->ports_exposes;
149+
$this->is_preserve_repository_enabled = $this->application->settings->is_preserve_repository_enabled;
148150
$this->is_container_label_escape_enabled = $this->application->settings->is_container_label_escape_enabled;
149151
$this->customLabels = $this->application->parseContainerLabels();
150152
if (! $this->customLabels && $this->application->destination->server->proxyType() !== 'NONE' && ! $this->application->settings->is_container_label_readonly_enabled) {
@@ -168,9 +170,21 @@ public function instantSave()
168170
$this->application->settings->save();
169171
$this->dispatch('success', 'Settings saved.');
170172
$this->application->refresh();
173+
174+
// If port_exposes changed, reset default labels
171175
if ($this->ports_exposes !== $this->application->ports_exposes || $this->is_container_label_escape_enabled !== $this->application->settings->is_container_label_escape_enabled) {
172176
$this->resetDefaultLabels(false);
173177
}
178+
if ($this->is_preserve_repository_enabled !== $this->application->settings->is_preserve_repository_enabled) {
179+
if ($this->application->settings->is_preserve_repository_enabled === false) {
180+
$this->application->fileStorages->each(function ($storage) {
181+
$storage->is_based_on_git = $this->application->settings->is_preserve_repository_enabled;
182+
$storage->save();
183+
});
184+
}
185+
186+
}
187+
174188
}
175189

176190
public function loadComposeFile($isInit = false)
@@ -179,39 +193,18 @@ public function loadComposeFile($isInit = false)
179193
if ($isInit && $this->application->docker_compose_raw) {
180194
return;
181195
}
196+
197+
// Must reload the application to get the latest database changes
198+
// Why? Not sure, but it works.
199+
$this->application->refresh();
200+
182201
['parsedServices' => $this->parsedServices, 'initialDockerComposeLocation' => $this->initialDockerComposeLocation] = $this->application->loadComposeFile($isInit);
183202
if (is_null($this->parsedServices)) {
184203
$this->dispatch('error', 'Failed to parse your docker-compose file. Please check the syntax and try again.');
185204

186205
return;
187206
}
188207
$compose = $this->application->parseCompose();
189-
$services = data_get($compose, 'services');
190-
if ($services) {
191-
$volumes = collect($services)->map(function ($service) {
192-
return data_get($service, 'volumes');
193-
})->flatten()->filter(function ($volume) {
194-
return str($volume)->startsWith('/data/coolify');
195-
})->unique()->values();
196-
foreach ($volumes as $volume) {
197-
$source = str($volume)->before(':');
198-
$target = str($volume)->after(':')->beforeLast(':');
199-
200-
LocalFileVolume::updateOrCreate(
201-
[
202-
'mount_path' => $target,
203-
'resource_id' => $this->application->id,
204-
'resource_type' => get_class($this->application),
205-
],
206-
[
207-
'fs_path' => $source,
208-
'mount_path' => $target,
209-
'resource_id' => $this->application->id,
210-
'resource_type' => get_class($this->application),
211-
]
212-
);
213-
}
214-
}
215208
$this->dispatch('success', 'Docker compose file loaded.');
216209
$this->dispatch('compose_loaded');
217210
$this->dispatch('refreshStorages');

app/Livewire/Project/Service/FileStorage.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class FileStorage extends Component
3333
'fileStorage.fs_path' => 'required',
3434
'fileStorage.mount_path' => 'required',
3535
'fileStorage.content' => 'nullable',
36+
'fileStorage.is_based_on_git' => 'required|boolean',
3637
];
3738

3839
public function mount()
@@ -45,6 +46,7 @@ public function mount()
4546
$this->workdir = null;
4647
$this->fs_path = $this->fileStorage->fs_path;
4748
}
49+
$this->fileStorage->loadStorageOnServer();
4850
}
4951

5052
public function convertToDirectory()
@@ -68,6 +70,9 @@ public function convertToFile()
6870
$this->fileStorage->deleteStorageOnServer();
6971
$this->fileStorage->is_directory = false;
7072
$this->fileStorage->content = null;
73+
if (data_get($this->resource, 'settings.is_preserve_repository_enabled')) {
74+
$this->fileStorage->is_based_on_git = true;
75+
}
7176
$this->fileStorage->save();
7277
$this->fileStorage->saveStorageOnServer();
7378
} catch (\Throwable $e) {

app/Livewire/Project/Shared/EnvironmentVariable/Show.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class Show extends Component
2424
public string $type;
2525

2626
protected $listeners = [
27-
'refresh' => 'refresh',
27+
'refreshEnvs' => 'refresh',
28+
'refresh',
2829
'compose_loaded' => '$refresh',
2930
];
3031

app/Livewire/SharedVariables/Environment/Show.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Show extends Component
1616

1717
public array $parameters;
1818

19-
protected $listeners = ['refreshEnvs' => '$refresh', 'saveKey' => 'saveKey'];
19+
protected $listeners = ['refreshEnvs' => '$refresh', 'saveKey'];
2020

2121
public function saveKey($data)
2222
{

app/Models/LocalFileVolume.php

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,32 @@ public function service()
2424
return $this->morphTo('resource');
2525
}
2626

27+
public function loadStorageOnServer()
28+
{
29+
$this->load(['service']);
30+
$isService = data_get($this->resource, 'service');
31+
if ($isService) {
32+
$workdir = $this->resource->service->workdir();
33+
$server = $this->resource->service->server;
34+
} else {
35+
$workdir = $this->resource->workdir();
36+
$server = $this->resource->destination->server;
37+
}
38+
$commands = collect([]);
39+
$path = data_get_str($this, 'fs_path');
40+
if ($path->startsWith('.')) {
41+
$path = $path->after('.');
42+
$path = $workdir.$path;
43+
}
44+
$isFile = instant_remote_process(["test -f $path && echo OK || echo NOK"], $server);
45+
if ($isFile === 'OK') {
46+
$content = instant_remote_process(["cat $path"], $server, false);
47+
$this->content = $content;
48+
$this->is_directory = false;
49+
$this->save();
50+
}
51+
}
52+
2753
public function deleteStorageOnServer()
2854
{
2955
$isService = data_get($this->resource, 'service');
@@ -35,17 +61,20 @@ public function deleteStorageOnServer()
3561
$server = $this->resource->destination->server;
3662
}
3763
$commands = collect([]);
38-
$fs_path = data_get($this, 'fs_path');
39-
$isFile = instant_remote_process(["test -f $fs_path && echo OK || echo NOK"], $server);
40-
$isDir = instant_remote_process(["test -d $fs_path && echo OK || echo NOK"], $server);
41-
if ($fs_path && $fs_path != '/' && $fs_path != '.' && $fs_path != '..') {
42-
ray($isFile, $isDir);
64+
$path = data_get_str($this, 'fs_path');
65+
if ($path->startsWith('.')) {
66+
$path = $path->after('.');
67+
$path = $workdir.$path;
68+
}
69+
$isFile = instant_remote_process(["test -f $path && echo OK || echo NOK"], $server);
70+
$isDir = instant_remote_process(["test -d $path && echo OK || echo NOK"], $server);
71+
if ($path && $path != '/' && $path != '.' && $path != '..') {
4372
if ($isFile === 'OK') {
44-
$commands->push("rm -rf $fs_path > /dev/null 2>&1 || true");
73+
$commands->push("rm -rf $path > /dev/null 2>&1 || true");
4574

4675
} elseif ($isDir === 'OK') {
47-
$commands->push("rm -rf $fs_path > /dev/null 2>&1 || true");
48-
$commands->push("rmdir $fs_path > /dev/null 2>&1 || true");
76+
$commands->push("rm -rf $path > /dev/null 2>&1 || true");
77+
$commands->push("rmdir $path > /dev/null 2>&1 || true");
4978
}
5079
}
5180
if ($commands->count() > 0) {
@@ -55,6 +84,7 @@ public function deleteStorageOnServer()
5584

5685
public function saveStorageOnServer()
5786
{
87+
$this->load(['service']);
5888
$isService = data_get($this->resource, 'service');
5989
if ($isService) {
6090
$workdir = $this->resource->service->workdir();
@@ -74,30 +104,36 @@ public function saveStorageOnServer()
74104
$commands->push("mkdir -p $parent_dir > /dev/null 2>&1 || true");
75105
}
76106
}
77-
$fileVolume = $this;
78-
$path = str(data_get($fileVolume, 'fs_path'));
79-
$content = data_get($fileVolume, 'content');
107+
$path = data_get_str($this, 'fs_path');
108+
$content = data_get($this, 'content');
80109
if ($path->startsWith('.')) {
81110
$path = $path->after('.');
82111
$path = $workdir.$path;
83112
}
84113
$isFile = instant_remote_process(["test -f $path && echo OK || echo NOK"], $server);
85114
$isDir = instant_remote_process(["test -d $path && echo OK || echo NOK"], $server);
86-
if ($isFile == 'OK' && $fileVolume->is_directory) {
115+
if ($isFile == 'OK' && $this->is_directory) {
87116
$content = instant_remote_process(["cat $path"], $server, false);
88-
$fileVolume->is_directory = false;
89-
$fileVolume->content = $content;
90-
$fileVolume->save();
117+
$this->is_directory = false;
118+
$this->content = $content;
119+
$this->save();
91120
FileStorageChanged::dispatch(data_get($server, 'team_id'));
92121
throw new \Exception('The following file is a file on the server, but you are trying to mark it as a directory. Please delete the file on the server or mark it as directory.');
93-
} elseif ($isDir == 'OK' && ! $fileVolume->is_directory) {
94-
$fileVolume->is_directory = true;
95-
$fileVolume->save();
96-
throw new \Exception('The following file is a directory on the server, but you are trying to mark it as a file. <br><br>Please delete the directory on the server or mark it as directory.');
122+
} elseif ($isDir == 'OK' && ! $this->is_directory) {
123+
if ($path == '/' || $path == '.' || $path == '..' || $path == '' || str($path)->isEmpty() || is_null($path)) {
124+
$this->is_directory = true;
125+
$this->save();
126+
throw new \Exception('The following file is a directory on the server, but you are trying to mark it as a file. <br><br>Please delete the directory on the server or mark it as directory.');
127+
}
128+
instant_remote_process([
129+
"rm -fr $path",
130+
"touch $path",
131+
], $server, false);
132+
FileStorageChanged::dispatch(data_get($server, 'team_id'));
97133
}
98-
if ($isDir == 'NOK' && ! $fileVolume->is_directory) {
99-
$chmod = data_get($fileVolume, 'chmod');
100-
$chown = data_get($fileVolume, 'chown');
134+
if ($isDir == 'NOK' && ! $this->is_directory) {
135+
$chmod = data_get($this, 'chmod');
136+
$chown = data_get($this, 'chown');
101137
if ($content) {
102138
$content = base64_encode($content);
103139
$commands->push("echo '$content' | base64 -d | tee $path > /dev/null");
@@ -111,7 +147,7 @@ public function saveStorageOnServer()
111147
if ($chmod) {
112148
$commands->push("chmod $chmod $path");
113149
}
114-
} elseif ($isDir == 'NOK' && $fileVolume->is_directory) {
150+
} elseif ($isDir == 'NOK' && $this->is_directory) {
115151
$commands->push("mkdir -p $path > /dev/null 2>&1 || true");
116152
}
117153

0 commit comments

Comments
 (0)