This commit is contained in:
Bozhidar 2024-09-13 15:07:17 +03:00
parent a49ec02e55
commit 8f2db60f28
4 changed files with 168 additions and 33 deletions

View file

@ -0,0 +1,66 @@
<?php
namespace Modules\Customer\App\Console;
use App\Models\GitRepository;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class GitRepositoryMarkAsPulled extends Command
{
/**
* The name and signature of the console command.
*/
protected $signature = 'git-repository:mark-as-pulled {id}';
/**
* The console command description.
*/
protected $description = 'Command description.';
/**
* Create a new command instance.
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*/
public function handle()
{
$id = $this->argument('id');
$repository = GitRepository::find($id);
if (!$repository) {
$this->error('Repository not found.');
return;
}
$repository->status = GitRepository::STATUS_UP_TO_DATE;
$repository->save();
}
/**
* Get the console command arguments.
*/
protected function getArguments(): array
{
return [
['id', InputArgument::REQUIRED, 'Git repository ID.'],
];
}
/**
* Get the console command options.
*/
protected function getOptions(): array
{
return [
['example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null],
];
}
}

View file

@ -79,7 +79,7 @@ class GitRepositoryResource extends Resource
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\Action::make('pull')
->hidden(fn (GitRepository $record) => $record->status !== 'cloned')
// ->hidden(fn (GitRepository $record) => $record->status !== 'cloned')
->icon('heroicon-o-arrow-down-tray')
->action(function (GitRepository $record) {

View file

@ -5,6 +5,7 @@ namespace Modules\Customer\App\Providers;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Modules\Customer\App\Console\GitRepositoryMarkAsCloned;
use Modules\Customer\App\Console\GitRepositoryMarkAsPulled;
use Modules\Customer\App\Providers\Filament\CustomerPanelProvider;
class CustomerServiceProvider extends ServiceProvider
@ -41,7 +42,8 @@ class CustomerServiceProvider extends ServiceProvider
protected function registerCommands(): void
{
$this->commands([
GitRepositoryMarkAsCloned::class
GitRepositoryMarkAsCloned::class,
GitRepositoryMarkAsPulled::class
]);
}

View file

@ -24,6 +24,8 @@ class GitRepository extends Model
const STATUS_PULLING = 'pulling';
const STATUS_UP_TO_DATE = 'up_to_date';
protected $fillable = [
'name',
'url',
@ -69,38 +71,8 @@ class GitRepository extends Model
return $this->belongsTo(Domain::class);
}
public function pull()
private function _getSSHKey($findHostingSubscription)
{
$this->status = self::STATUS_PULLING;
$this->save();
}
public function clone()
{
$this->status = self::STATUS_CLONING;
$this->save();
$findDomain = Domain::find($this->domain_id);
if (!$findDomain) {
$this->status = self::STATUS_FAILED;
$this->status_message = 'Domain not found';
$this->save();
return;
}
$findHostingSubscription = HostingSubscription::find($findDomain->hosting_subscription_id);
if (!$findHostingSubscription) {
$this->status = self::STATUS_FAILED;
$this->status_message = 'Hosting Subscription not found';
$this->save();
return;
}
$projectDir = $findDomain->domain_root . '/' . $this->dir;
$privateKeyFile = null;
$gitSSHKey = GitSshKey::find($this->git_ssh_key_id);
if ($gitSSHKey) {
@ -127,7 +99,102 @@ class GitRepository extends Model
shell_exec('chown '.$findHostingSubscription->system_username.':'.$findHostingSubscription->system_username.' ' . $publicKeyFile);
shell_exec('chmod 0400 ' . $publicKeyFile);
}
return [
'privateKeyFile' => $privateKeyFile,
'publicKeyFile' => $publicKeyFile,
];
}
}
public function pull()
{
$this->status = self::STATUS_PULLING;
$this->save();
$findDomain = Domain::find($this->domain_id);
if (!$findDomain) {
$this->status = self::STATUS_FAILED;
$this->status_message = 'Domain not found';
$this->save();
return;
}
$findHostingSubscription = HostingSubscription::find($findDomain->hosting_subscription_id);
if (!$findHostingSubscription) {
$this->status = self::STATUS_FAILED;
$this->status_message = 'Hosting Subscription not found';
$this->save();
return;
}
$projectDir = $findDomain->domain_root . '/' . $this->dir;
$privateKeyFile = null;
$getSSHKey = $this->_getSSHKey($findHostingSubscription);
if (isset($getSSHKey['privateKeyFile'])) {
$privateKeyFile = $getSSHKey['privateKeyFile'];
}
$gitSSHUrl = GitClient::parseGitUrl($this->url);
if (!isset($gitSSHUrl['provider'])) {
$this->status = self::STATUS_FAILED;
$this->status_message = 'Provider not found';
$this->save();
return;
}
$cloneUrl = 'git@'.$gitSSHUrl['provider'].':'.$gitSSHUrl['owner'].'/'.$gitSSHUrl['name'].'.git';
$shellFile = '/tmp/git-pull-' . $this->id . '.sh';
$shellLog = '/tmp/git-pull-' . $this->id . '.log';
$shellContent = view('actions.git.pull-repo', [
'gitProvider' => $gitSSHUrl['provider'],
'systemUsername' => $findHostingSubscription->system_username,
'gitRepositoryId' => $this->id,
'cloneUrl' => $cloneUrl,
'projectDir' => $projectDir,
'privateKeyFile' => $privateKeyFile,
])->render();
file_put_contents($shellFile, $shellContent);
shell_exec('chmod +x ' . $shellFile);
shell_exec('bash '.$shellFile.' >> ' . $shellLog . ' &');
}
public function clone()
{
$this->status = self::STATUS_CLONING;
$this->save();
$findDomain = Domain::find($this->domain_id);
if (!$findDomain) {
$this->status = self::STATUS_FAILED;
$this->status_message = 'Domain not found';
$this->save();
return;
}
$findHostingSubscription = HostingSubscription::find($findDomain->hosting_subscription_id);
if (!$findHostingSubscription) {
$this->status = self::STATUS_FAILED;
$this->status_message = 'Hosting Subscription not found';
$this->save();
return;
}
$projectDir = $findDomain->domain_root . '/' . $this->dir;
$privateKeyFile = null;
$getSSHKey = $this->_getSSHKey($findHostingSubscription);
if (isset($getSSHKey['privateKeyFile'])) {
$privateKeyFile = $getSSHKey['privateKeyFile'];
}
$gitSSHUrl = GitClient::parseGitUrl($this->url);
if (!isset($gitSSHUrl['provider'])) {