2023-05-10 11:05:32 +00:00
|
|
|
<?php
|
|
|
|
|
2023-12-07 18:06:32 +00:00
|
|
|
namespace App\Livewire\Project\New;
|
2023-05-10 11:05:32 +00:00
|
|
|
|
|
|
|
use App\Models\Application;
|
2023-06-12 12:38:32 +00:00
|
|
|
use App\Models\GithubApp;
|
|
|
|
use App\Models\GitlabApp;
|
2023-05-10 11:05:32 +00:00
|
|
|
use App\Models\PrivateKey;
|
|
|
|
use App\Models\Project;
|
|
|
|
use App\Models\StandaloneDocker;
|
|
|
|
use App\Models\SwarmDocker;
|
2024-02-04 12:50:24 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2023-05-10 11:05:32 +00:00
|
|
|
use Livewire\Component;
|
|
|
|
use Spatie\Url\Url;
|
2023-10-06 12:39:30 +00:00
|
|
|
use Illuminate\Support\Str;
|
2023-05-10 11:05:32 +00:00
|
|
|
|
|
|
|
class GithubPrivateRepositoryDeployKey extends Component
|
|
|
|
{
|
2023-07-26 11:23:47 +00:00
|
|
|
public $current_step = 'private_keys';
|
2023-05-10 11:05:32 +00:00
|
|
|
public $parameters;
|
2023-05-11 13:20:02 +00:00
|
|
|
public $query;
|
2024-02-04 12:50:24 +00:00
|
|
|
public $private_keys =[];
|
2023-05-10 11:05:32 +00:00
|
|
|
public int $private_key_id;
|
|
|
|
|
|
|
|
public int $port = 3000;
|
|
|
|
public string $type;
|
|
|
|
|
|
|
|
public bool $is_static = false;
|
|
|
|
public null|string $publish_directory = null;
|
2023-06-12 12:38:32 +00:00
|
|
|
|
|
|
|
public string $repository_url;
|
|
|
|
public string $branch;
|
2024-01-25 14:57:04 +00:00
|
|
|
|
|
|
|
public $build_pack = 'nixpacks';
|
|
|
|
public bool $show_is_static = true;
|
|
|
|
|
2024-02-04 12:50:24 +00:00
|
|
|
private object $repository_url_parsed;
|
|
|
|
private GithubApp|GitlabApp|string $git_source = 'other';
|
|
|
|
private ?string $git_host = null;
|
|
|
|
private string $git_repository;
|
|
|
|
|
2023-05-10 11:05:32 +00:00
|
|
|
protected $rules = [
|
2023-10-06 12:39:30 +00:00
|
|
|
'repository_url' => 'required',
|
2023-06-12 12:38:32 +00:00
|
|
|
'branch' => 'required|string',
|
2023-05-10 11:05:32 +00:00
|
|
|
'port' => 'required|numeric',
|
|
|
|
'is_static' => 'required|boolean',
|
|
|
|
'publish_directory' => 'nullable|string',
|
2024-01-25 14:57:04 +00:00
|
|
|
'build_pack' => 'required|string',
|
2023-05-10 11:05:32 +00:00
|
|
|
];
|
2023-06-12 12:38:32 +00:00
|
|
|
protected $validationAttributes = [
|
|
|
|
'repository_url' => 'Repository',
|
|
|
|
'branch' => 'Branch',
|
|
|
|
'port' => 'Port',
|
|
|
|
'is_static' => 'Is static',
|
|
|
|
'publish_directory' => 'Publish directory',
|
2024-01-25 14:57:04 +00:00
|
|
|
'build_pack' => 'Build pack',
|
2023-06-12 12:38:32 +00:00
|
|
|
];
|
2024-02-04 12:50:24 +00:00
|
|
|
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-05-10 11:05:32 +00:00
|
|
|
public function mount()
|
|
|
|
{
|
2023-08-27 13:23:47 +00:00
|
|
|
if (isDev()) {
|
2023-06-12 12:38:32 +00:00
|
|
|
$this->repository_url = 'https://github.com/coollabsio/coolify-examples';
|
2023-05-10 11:05:32 +00:00
|
|
|
}
|
2023-08-09 13:57:53 +00:00
|
|
|
$this->parameters = get_route_parameters();
|
2023-05-11 13:20:02 +00:00
|
|
|
$this->query = request()->query();
|
2023-12-30 13:20:02 +00:00
|
|
|
if (isDev()) {
|
|
|
|
$this->private_keys = PrivateKey::where('team_id', currentTeam()->id)->get();
|
|
|
|
} else {
|
|
|
|
$this->private_keys = PrivateKey::where('team_id', currentTeam()->id)->where('id', '!=', 0)->get();
|
|
|
|
}
|
2023-05-10 11:05:32 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2024-01-25 14:57:04 +00:00
|
|
|
public function updatedBuildPack()
|
|
|
|
{
|
|
|
|
if ($this->build_pack === 'nixpacks') {
|
|
|
|
$this->show_is_static = true;
|
|
|
|
$this->port = 3000;
|
|
|
|
} else if ($this->build_pack === 'static') {
|
|
|
|
$this->show_is_static = false;
|
|
|
|
$this->is_static = false;
|
|
|
|
$this->port = 80;
|
|
|
|
} else {
|
|
|
|
$this->show_is_static = false;
|
|
|
|
$this->is_static = false;
|
|
|
|
}
|
|
|
|
}
|
2023-05-10 11:05:32 +00:00
|
|
|
public function instantSave()
|
|
|
|
{
|
|
|
|
if ($this->is_static) {
|
|
|
|
$this->port = 80;
|
|
|
|
$this->publish_directory = '/dist';
|
|
|
|
} else {
|
|
|
|
$this->port = 3000;
|
|
|
|
$this->publish_directory = null;
|
|
|
|
}
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-05-10 11:05:32 +00:00
|
|
|
public function setPrivateKey($private_key_id)
|
|
|
|
{
|
|
|
|
$this->private_key_id = $private_key_id;
|
2023-07-26 11:23:47 +00:00
|
|
|
$this->current_step = 'repository';
|
2023-05-10 11:05:32 +00:00
|
|
|
}
|
2023-06-12 12:38:32 +00:00
|
|
|
|
2023-05-10 11:05:32 +00:00
|
|
|
public function submit()
|
|
|
|
{
|
2023-06-12 12:38:32 +00:00
|
|
|
$this->validate();
|
2023-05-11 13:20:02 +00:00
|
|
|
try {
|
|
|
|
$destination_uuid = $this->query['destination'];
|
|
|
|
$destination = StandaloneDocker::where('uuid', $destination_uuid)->first();
|
|
|
|
if (!$destination) {
|
|
|
|
$destination = SwarmDocker::where('uuid', $destination_uuid)->first();
|
|
|
|
}
|
|
|
|
if (!$destination) {
|
|
|
|
throw new \Exception('Destination not found. What?!');
|
|
|
|
}
|
|
|
|
$destination_class = $destination->getMorphClass();
|
|
|
|
|
2023-06-12 12:38:32 +00:00
|
|
|
$this->get_git_source();
|
|
|
|
|
2023-05-11 13:20:02 +00:00
|
|
|
$project = Project::where('uuid', $this->parameters['project_uuid'])->first();
|
|
|
|
$environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first();
|
2023-10-06 12:39:30 +00:00
|
|
|
if ($this->git_source === 'other') {
|
|
|
|
$application_init = [
|
|
|
|
'name' => generate_random_name(),
|
|
|
|
'git_repository' => $this->git_repository,
|
|
|
|
'git_branch' => $this->branch,
|
|
|
|
'build_pack' => 'nixpacks',
|
|
|
|
'ports_exposes' => $this->port,
|
|
|
|
'publish_directory' => $this->publish_directory,
|
|
|
|
'environment_id' => $environment->id,
|
|
|
|
'destination_id' => $destination->id,
|
|
|
|
'destination_type' => $destination_class,
|
|
|
|
'private_key_id' => $this->private_key_id,
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$application_init = [
|
|
|
|
'name' => generate_random_name(),
|
|
|
|
'git_repository' => $this->git_repository,
|
|
|
|
'git_branch' => $this->branch,
|
|
|
|
'build_pack' => 'nixpacks',
|
|
|
|
'ports_exposes' => $this->port,
|
|
|
|
'publish_directory' => $this->publish_directory,
|
|
|
|
'environment_id' => $environment->id,
|
|
|
|
'destination_id' => $destination->id,
|
|
|
|
'destination_type' => $destination_class,
|
|
|
|
'private_key_id' => $this->private_key_id,
|
|
|
|
'source_id' => $this->git_source->id,
|
|
|
|
'source_type' => $this->git_source->getMorphClass()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-05-11 13:20:02 +00:00
|
|
|
$application = Application::create($application_init);
|
|
|
|
$application->settings->is_static = $this->is_static;
|
|
|
|
$application->settings->save();
|
|
|
|
|
2023-09-30 13:39:40 +00:00
|
|
|
$fqdn = generateFqdn($destination->server, $application->uuid);
|
|
|
|
$application->fqdn = $fqdn;
|
2023-09-19 13:51:13 +00:00
|
|
|
$application->name = generate_random_name($application->uuid);
|
|
|
|
$application->save();
|
|
|
|
|
2023-12-27 15:45:01 +00:00
|
|
|
return redirect()->route('project.application.configuration', [
|
2023-05-11 13:20:02 +00:00
|
|
|
'application_uuid' => $application->uuid,
|
2023-12-07 21:56:55 +00:00
|
|
|
'environment_name' => $environment->name,
|
|
|
|
'project_uuid' => $project->uuid,
|
2023-12-27 15:45:01 +00:00
|
|
|
]);
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-15 13:34:25 +00:00
|
|
|
return handleError($e, $this);
|
2023-05-10 11:05:32 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
|
|
|
private function get_git_source()
|
|
|
|
{
|
2023-10-06 12:50:49 +00:00
|
|
|
$this->repository_url_parsed = Url::fromString($this->repository_url);
|
|
|
|
$this->git_host = $this->repository_url_parsed->getHost();
|
|
|
|
$this->git_repository = $this->repository_url_parsed->getSegment(1) . '/' . $this->repository_url_parsed->getSegment(2);
|
2023-08-08 09:51:36 +00:00
|
|
|
|
|
|
|
if ($this->git_host == 'github.com') {
|
|
|
|
$this->git_source = GithubApp::where('name', 'Public GitHub')->first();
|
2023-10-06 12:39:30 +00:00
|
|
|
return;
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|
2023-10-06 12:50:49 +00:00
|
|
|
if (Str::of($this->repository_url)->startsWith('http')) {
|
|
|
|
$this->git_host = $this->repository_url_parsed->getHost();
|
|
|
|
$this->git_repository = $this->repository_url_parsed->getSegment(1) . '/' . $this->repository_url_parsed->getSegment(2);
|
|
|
|
$this->git_repository = Str::finish("git@$this->git_host:$this->git_repository", '.git');
|
2023-10-13 19:39:49 +00:00
|
|
|
} else {
|
|
|
|
$this->git_repository = $this->repository_url;
|
2023-10-06 12:50:49 +00:00
|
|
|
}
|
2023-10-06 12:39:30 +00:00
|
|
|
$this->git_source = 'other';
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|
2023-05-10 11:05:32 +00:00
|
|
|
}
|