This commit is contained in:
Bozhidar 2024-09-11 15:41:07 +03:00
parent 17d5b118e1
commit a600030e07
2 changed files with 122 additions and 41 deletions

View file

@ -28,62 +28,63 @@ class GitRepositoryResource extends Resource
{
return $form
->schema([
Forms\Components\Wizard::make([
Forms\Components\Wizard\Step::make('Order')
->schema([
// ...
]),
Forms\Components\Wizard\Step::make('Delivery')
->schema([
// ...
]),
Forms\Components\Wizard\Step::make('Billing')
->schema([
// ...
]),
])->columnSpanFull()
Forms\Components\TextInput::make('dir')
->label('Directory')
->required()
->columnSpanFull(),
]);
}
public static function ___form(Form $form): Form
{
$gitSSHKeys = \App\Models\GitSshKey::all()->pluck('name', 'id');
return $form
->schema([
Forms\Components\Select::make('git_ssh_key_id')
->label('SSH Key')
->options($gitSSHKeys)
->columnSpanFull()
->live()
->required(),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
//
Tables\Columns\TextColumn::make('domain.domain'),
// Tables\Columns\TextColumn::make('name')
// ->searchable()
// ->sortable(),
Tables\Columns\TextColumn::make('url')
->searchable()
->sortable(),
//
// Tables\Columns\TextColumn::make('branch')
// ->searchable()
// ->sortable(),
//
// Tables\Columns\TextColumn::make('tag')
// ->searchable()
// ->sortable(),
//
// Tables\Columns\TextColumn::make('clone_from')
// ->searchable()
// ->sortable(),
Tables\Columns\TextColumn::make('dir')
->searchable()
->sortable(),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\Action::make('pull')
->icon('heroicon-o-arrow-down-tray')
->action(function (GitRepository $record) {
$gitRepository = GitRepository::find($record->id);
$gitRepository->pull();
})
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
// Tables\Actions\BulkActionGroup::make([
// Tables\Actions\DeleteBulkAction::make(),
// ]),
]);
}
@ -98,7 +99,7 @@ class GitRepositoryResource extends Resource
{
return [
'index' => Pages\ListGitRepositories::route('/'),
'edit' => Pages\EditGitRepository::route('/{record}/edit'),
// 'edit' => Pages\EditGitRepository::route('/{record}/edit'),
];
}
}

View file

@ -2,6 +2,7 @@
namespace App\Models;
use App\GitClient;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@ -17,6 +18,8 @@ class GitRepository extends Model
const STATUS_CLONED = 'cloned';
const STATUS_FAILED = 'failed';
const STATUS_PULLING = 'pulling';
protected $fillable = [
'name',
'url',
@ -33,4 +36,81 @@ class GitRepository extends Model
'git_ssh_key_id',
];
public function domain()
{
return $this->belongsTo(Domain::class);
}
public function clone()
{
$this->status = self::STATUS_CLONING;
$this->save();
}
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;
$gitSSHKey = GitSshKey::find($this->git_ssh_key_id);
if ($gitSSHKey) {
$sshPath = '/home/'.$findHostingSubscription->system_username .'/.git-ssh/'.$gitSSHKey->id;
$privateKeyFile = $sshPath.'/id_rsa';
$publicKeyFile = $sshPath.'/id_rsa.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);
chown($privateKeyFile, $findHostingSubscription->system_username);
chmod($privateKeyFile, 0400);
}
if (!file_exists($publicKeyFile)) {
file_put_contents($publicKeyFile, $gitSSHKey->public_key);
chown($publicKeyFile, $findHostingSubscription->system_username);
chmod($publicKeyFile, 0400);
}
}
$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';
// $clone = shell_exec('git -c core.sshCommand="ssh -i '.$privateKeyFile.'" clone ' . $cloneUrl . ' ' . $projectDir);
// dd($clone);
}
}