Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
bobimicroweber committed Sep 13, 2024
1 parent a49ec02 commit 8f2db60
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 28 deletions.
66 changes: 66 additions & 0 deletions web/Modules/Customer/App/Console/GitRepositoryMarkAsPulled.php
Original file line number Diff line number Diff line change
@@ -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],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static function table(Table $table): Table
->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) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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
Expand Down Expand Up @@ -41,7 +42,8 @@ public function register(): void
protected function registerCommands(): void
{
$this->commands([
GitRepositoryMarkAsCloned::class
GitRepositoryMarkAsCloned::class,
GitRepositoryMarkAsPulled::class
]);
}

Expand Down
119 changes: 93 additions & 26 deletions web/app/Models/GitRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class GitRepository extends Model

const STATUS_PULLING = 'pulling';

const STATUS_UP_TO_DATE = 'up_to_date';

protected $fillable = [
'name',
'url',
Expand Down Expand Up @@ -69,16 +71,45 @@ public function domain()
return $this->belongsTo(Domain::class);
}

public function pull()
private function _getSSHKey($findHostingSubscription)
{
$this->status = self::STATUS_PULLING;
$this->save();

$gitSSHKey = GitSshKey::find($this->git_ssh_key_id);
if ($gitSSHKey) {
$sshPath = '/home/'.$findHostingSubscription->system_username .'/.ssh';
$privateKeyFile = $sshPath.'/id_rsa_'. $gitSSHKey->id;
$publicKeyFile = $sshPath.'/id_rsa_'.$gitSSHKey->id.'.pub';

if (!is_dir($sshPath)) {
shell_exec('mkdir -p ' . $sshPath);
shell_exec('chown '.$findHostingSubscription->system_username.':'.$findHostingSubscription->system_username.' -R ' . dirname($sshPath));
shell_exec('chmod 0700 ' . dirname($sshPath));
}

if (!file_exists($privateKeyFile)) {
file_put_contents($privateKeyFile, $gitSSHKey->private_key);

shell_exec('chown '.$findHostingSubscription->system_username.':'.$findHostingSubscription->system_username.' ' . $privateKeyFile);
shell_exec('chmod 0400 ' . $privateKeyFile);

}

if (!file_exists($publicKeyFile)) {
file_put_contents($publicKeyFile, $gitSSHKey->public_key);
shell_exec('chown '.$findHostingSubscription->system_username.':'.$findHostingSubscription->system_username.' ' . $publicKeyFile);
shell_exec('chmod 0400 ' . $publicKeyFile);
}

return [
'privateKeyFile' => $privateKeyFile,
'publicKeyFile' => $publicKeyFile,
];
}
}

public function clone()
public function pull()
{
$this->status = self::STATUS_CLONING;
$this->status = self::STATUS_PULLING;
$this->save();

$findDomain = Domain::find($this->domain_id);
Expand All @@ -97,38 +128,74 @@ public function clone()
return;
}


$projectDir = $findDomain->domain_root . '/' . $this->dir;

$privateKeyFile = null;
$getSSHKey = $this->_getSSHKey($findHostingSubscription);
if (isset($getSSHKey['privateKeyFile'])) {
$privateKeyFile = $getSSHKey['privateKeyFile'];
}

$gitSSHKey = GitSshKey::find($this->git_ssh_key_id);
if ($gitSSHKey) {
$sshPath = '/home/'.$findHostingSubscription->system_username .'/.ssh';
$privateKeyFile = $sshPath.'/id_rsa_'. $gitSSHKey->id;
$publicKeyFile = $sshPath.'/id_rsa_'.$gitSSHKey->id.'.pub';

if (!is_dir($sshPath)) {
shell_exec('mkdir -p ' . $sshPath);
shell_exec('chown '.$findHostingSubscription->system_username.':'.$findHostingSubscription->system_username.' -R ' . dirname($sshPath));
shell_exec('chmod 0700 ' . dirname($sshPath));
}
$gitSSHUrl = GitClient::parseGitUrl($this->url);
if (!isset($gitSSHUrl['provider'])) {
$this->status = self::STATUS_FAILED;
$this->status_message = 'Provider not found';
$this->save();
return;
}

if (!file_exists($privateKeyFile)) {
file_put_contents($privateKeyFile, $gitSSHKey->private_key);
$cloneUrl = 'git@'.$gitSSHUrl['provider'].':'.$gitSSHUrl['owner'].'/'.$gitSSHUrl['name'].'.git';

shell_exec('chown '.$findHostingSubscription->system_username.':'.$findHostingSubscription->system_username.' ' . $privateKeyFile);
shell_exec('chmod 0400 ' . $privateKeyFile);
$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();

if (!file_exists($publicKeyFile)) {
file_put_contents($publicKeyFile, $gitSSHKey->public_key);
shell_exec('chown '.$findHostingSubscription->system_username.':'.$findHostingSubscription->system_username.' ' . $publicKeyFile);
shell_exec('chmod 0400 ' . $publicKeyFile);
}
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'])) {
$this->status = self::STATUS_FAILED;
Expand Down

0 comments on commit 8f2db60

Please sign in to comment.