diff --git a/web/Modules/Customer/App/Console/GitRepositoryMarkAsPulled.php b/web/Modules/Customer/App/Console/GitRepositoryMarkAsPulled.php new file mode 100644 index 0000000..ef2e112 --- /dev/null +++ b/web/Modules/Customer/App/Console/GitRepositoryMarkAsPulled.php @@ -0,0 +1,66 @@ +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], + ]; + } +} diff --git a/web/Modules/Customer/App/Filament/Resources/GitRepositoryResource.php b/web/Modules/Customer/App/Filament/Resources/GitRepositoryResource.php index 5f08c33..a1e262d 100644 --- a/web/Modules/Customer/App/Filament/Resources/GitRepositoryResource.php +++ b/web/Modules/Customer/App/Filament/Resources/GitRepositoryResource.php @@ -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) { diff --git a/web/Modules/Customer/App/Providers/CustomerServiceProvider.php b/web/Modules/Customer/App/Providers/CustomerServiceProvider.php index bec78a0..be5d582 100644 --- a/web/Modules/Customer/App/Providers/CustomerServiceProvider.php +++ b/web/Modules/Customer/App/Providers/CustomerServiceProvider.php @@ -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 ]); } diff --git a/web/app/Models/GitRepository.php b/web/app/Models/GitRepository.php index e4108bb..1e3a5ab 100644 --- a/web/app/Models/GitRepository.php +++ b/web/app/Models/GitRepository.php @@ -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'])) {