2023-04-25 12:43:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Livewire\Project\New;
|
|
|
|
|
|
|
|
use App\Models\Application;
|
|
|
|
use App\Models\GithubApp;
|
|
|
|
use App\Models\GitlabApp;
|
|
|
|
use App\Models\Project;
|
|
|
|
use App\Models\StandaloneDocker;
|
|
|
|
use App\Models\SwarmDocker;
|
2023-04-26 11:25:41 +00:00
|
|
|
use Illuminate\Support\Facades\Route;
|
2023-04-25 12:43:35 +00:00
|
|
|
use Livewire\Component;
|
|
|
|
use Spatie\Url\Url;
|
|
|
|
|
|
|
|
class PublicGitRepository extends Component
|
|
|
|
{
|
|
|
|
public string $public_repository_url;
|
|
|
|
public int $port;
|
2023-04-26 11:25:41 +00:00
|
|
|
public string $type;
|
|
|
|
public $parameters;
|
2023-04-25 12:43:35 +00:00
|
|
|
|
|
|
|
public $servers;
|
|
|
|
public $standalone_docker;
|
|
|
|
public $swarm_docker;
|
|
|
|
public $chosenServer;
|
|
|
|
public $chosenDestination;
|
|
|
|
public $github_apps;
|
|
|
|
public $gitlab_apps;
|
|
|
|
|
2023-04-26 11:01:09 +00:00
|
|
|
public bool $is_static = false;
|
2023-04-27 10:25:32 +00:00
|
|
|
public null|string $publish_directory = null;
|
2023-04-26 11:01:09 +00:00
|
|
|
|
2023-04-25 12:43:35 +00:00
|
|
|
protected $rules = [
|
|
|
|
'public_repository_url' => 'required|url',
|
2023-04-26 08:02:19 +00:00
|
|
|
'port' => 'required|numeric',
|
|
|
|
'is_static' => 'required|boolean',
|
2023-04-27 10:25:32 +00:00
|
|
|
'publish_directory' => 'nullable|string',
|
2023-04-25 12:43:35 +00:00
|
|
|
];
|
|
|
|
public function mount()
|
|
|
|
{
|
2023-05-03 06:55:03 +00:00
|
|
|
if (config('app.env') === 'local') {
|
2023-04-25 12:43:35 +00:00
|
|
|
$this->public_repository_url = 'https://github.com/coollabsio/coolify-examples/tree/nodejs-fastify';
|
|
|
|
$this->port = 3000;
|
|
|
|
}
|
2023-04-26 11:25:41 +00:00
|
|
|
$this->parameters = Route::current()->parameters();
|
2023-04-25 12:43:35 +00:00
|
|
|
$this->servers = session('currentTeam')->load(['servers'])->servers;
|
|
|
|
}
|
|
|
|
public function chooseServer($server_id)
|
|
|
|
{
|
|
|
|
$this->chosenServer = $server_id;
|
|
|
|
$this->standalone_docker = StandaloneDocker::where('server_id', $server_id)->get();
|
|
|
|
$this->swarm_docker = SwarmDocker::where('server_id', $server_id)->get();
|
|
|
|
}
|
|
|
|
public function setDestination($destination_uuid, $destination_type)
|
|
|
|
{
|
|
|
|
$class = "App\Models\\{$destination_type}";
|
|
|
|
$instance = new $class;
|
|
|
|
$this->chosenDestination = $instance::where('uuid', $destination_uuid)->first();
|
|
|
|
}
|
|
|
|
|
2023-04-26 11:01:09 +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-04-25 12:43:35 +00:00
|
|
|
public function submit()
|
|
|
|
{
|
|
|
|
$this->validate();
|
|
|
|
$url = Url::fromString($this->public_repository_url);
|
|
|
|
$git_host = $url->getHost();
|
|
|
|
$git_repository = $url->getSegment(1) . '/' . $url->getSegment(2);
|
|
|
|
$git_branch = $url->getSegment(4) ?? 'main';
|
|
|
|
|
2023-04-26 11:25:41 +00:00
|
|
|
if ($this->type === 'project') {
|
|
|
|
$project = Project::create([
|
2023-05-02 07:11:22 +00:00
|
|
|
'name' => generateRandomName(),
|
2023-04-26 11:25:41 +00:00
|
|
|
'team_id' => session('currentTeam')->id,
|
|
|
|
]);
|
|
|
|
$environment = $project->environments->first();
|
|
|
|
} else {
|
|
|
|
$project = Project::where('uuid', $this->parameters['project_uuid'])->firstOrFail();
|
|
|
|
$environment = $project->environments->where('name', $this->parameters['environment_name'])->firstOrFail();
|
|
|
|
}
|
2023-04-25 12:43:35 +00:00
|
|
|
$application_init = [
|
2023-05-02 07:11:22 +00:00
|
|
|
'name' => generateRandomName(),
|
2023-04-25 12:43:35 +00:00
|
|
|
'git_repository' => $git_repository,
|
|
|
|
'git_branch' => $git_branch,
|
|
|
|
'build_pack' => 'nixpacks',
|
|
|
|
'ports_exposes' => $this->port,
|
2023-04-26 11:01:09 +00:00
|
|
|
'publish_directory' => $this->publish_directory,
|
2023-04-26 08:02:19 +00:00
|
|
|
'environment_id' => $environment->id,
|
2023-04-25 12:43:35 +00:00
|
|
|
'destination_id' => $this->chosenDestination->id,
|
|
|
|
'destination_type' => $this->chosenDestination->getMorphClass(),
|
|
|
|
];
|
|
|
|
if ($git_host == 'github.com') {
|
|
|
|
$application_init['source_id'] = GithubApp::where('name', 'Public GitHub')->first()->id;
|
|
|
|
$application_init['source_type'] = GithubApp::class;
|
|
|
|
} elseif ($git_host == 'gitlab.com') {
|
|
|
|
$application_init['source_id'] = GitlabApp::where('name', 'Public GitLab')->first()->id;
|
|
|
|
$application_init['source_type'] = GitlabApp::class;
|
|
|
|
} elseif ($git_host == 'bitbucket.org') {
|
|
|
|
}
|
2023-04-26 08:02:19 +00:00
|
|
|
$application = Application::create($application_init);
|
2023-04-26 11:01:09 +00:00
|
|
|
$application->settings->is_static = $this->is_static;
|
|
|
|
$application->settings->save();
|
2023-04-26 08:02:19 +00:00
|
|
|
|
|
|
|
return redirect()->route('project.application.configuration', [
|
|
|
|
'project_uuid' => $project->uuid,
|
|
|
|
'environment_name' => $environment->name,
|
|
|
|
'application_uuid' => $application->uuid,
|
|
|
|
]);
|
2023-04-25 12:43:35 +00:00
|
|
|
}
|
|
|
|
}
|