|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Modules\Microweber\Jobs; |
| 4 | + |
| 5 | +use App\Models\HostingSubscription; |
| 6 | +use App\ShellApi; |
| 7 | +use Illuminate\Bus\Queueable; |
| 8 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 9 | +use Illuminate\Foundation\Bus\Dispatchable; |
| 10 | +use Illuminate\Queue\InteractsWithQueue; |
| 11 | +use Illuminate\Queue\SerializesModels; |
| 12 | +use Modules\Microweber\App\Models\MicroweberInstallation; |
| 13 | + |
| 14 | +class RunInstallationCommands implements ShouldQueue |
| 15 | +{ |
| 16 | + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
| 17 | + |
| 18 | + public static $displayName = 'Run Installation Commands'; |
| 19 | + public static $displayDescription = 'Execute commands on Microweber installations'; |
| 20 | + |
| 21 | + protected $command; |
| 22 | + protected $installationId; |
| 23 | + |
| 24 | + public function __construct(string $command, ?int $installationId = null) |
| 25 | + { |
| 26 | + $this->command = $command; |
| 27 | + $this->installationId = $installationId; |
| 28 | + } |
| 29 | + |
| 30 | + public function handle(): void |
| 31 | + { |
| 32 | + set_time_limit(0); |
| 33 | + |
| 34 | + if ($this->installationId) { |
| 35 | + $installations = MicroweberInstallation::where('id', $this->installationId)->get(); |
| 36 | + } else { |
| 37 | + $installations = MicroweberInstallation::all(); |
| 38 | + } |
| 39 | + |
| 40 | + if ($installations->isEmpty()) { |
| 41 | + \Log::info('No installations found to run command: ' . $this->command); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + foreach ($installations as $installation) { |
| 46 | + |
| 47 | + $this->runCommandForInstallation($installation, $this->command); |
| 48 | + |
| 49 | + |
| 50 | +// try { |
| 51 | +// } catch (\Exception $e) { |
| 52 | +// \Log::error('Error running command "' . $this->command . '" on installation: ' . $installation->installation_path . ' - ' . $e->getMessage()); |
| 53 | +// } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private function runCommandForInstallation(MicroweberInstallation $installation, string $command): void |
| 58 | + { |
| 59 | + $domain = $installation->domain; |
| 60 | + if (!$domain) { |
| 61 | + \Log::warning('Domain not found for installation: ' . $installation->id); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + $hostingSubscription = HostingSubscription::where('id', $domain->hosting_subscription_id)->first(); |
| 66 | + if (!$hostingSubscription) { |
| 67 | + \Log::warning('Hosting subscription not found for domain: ' . $domain->id); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + $installationPath = $installation->installation_path; |
| 72 | + if (!is_dir($installationPath)) { |
| 73 | + \Log::warning('Installation path does not exist: ' . $installationPath); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + $username = $hostingSubscription->system_username; |
| 78 | + |
| 79 | + switch ($command) { |
| 80 | + case 'cache:clear': |
| 81 | + $this->runArtisanCommand($installationPath, 'cache:clear', $username); |
| 82 | + break; |
| 83 | + |
| 84 | + case 'composer:dump': |
| 85 | + $this->runComposerCommand($installationPath, 'dump-autoload', $username); |
| 86 | + break; |
| 87 | + |
| 88 | + case 'composer:publish-assets': |
| 89 | + $this->runComposerCommand($installationPath, 'publish-assets', $username); |
| 90 | + break; |
| 91 | + |
| 92 | + default: |
| 93 | + // For custom artisan commands |
| 94 | + $this->runArtisanCommand($installationPath, $command, $username); |
| 95 | + break; |
| 96 | + } |
| 97 | + |
| 98 | + \Log::info("Command '{$command}' executed successfully for installation: {$installationPath}"); |
| 99 | + } |
| 100 | + |
| 101 | + private function runArtisanCommand(string $installationPath, string $command, string $username): void |
| 102 | + { |
| 103 | + $artisanPath = $installationPath . '/artisan'; |
| 104 | + |
| 105 | + if (!file_exists($artisanPath)) { |
| 106 | + throw new \Exception('Artisan file not found at: ' . $artisanPath); |
| 107 | + } |
| 108 | + |
| 109 | + $fullCommand = "cd {$installationPath} && sudo -u {$username} php {$artisanPath} {$command}"; |
| 110 | + \Log::info("Executing artisan command: {$fullCommand}"); |
| 111 | + |
| 112 | + $result = ShellApi::exec($fullCommand); |
| 113 | + |
| 114 | + if ($result['exit_code'] !== 0) { |
| 115 | + throw new \Exception('Artisan command failed: ' . $result['output']); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private function runComposerCommand(string $installationPath, string $command, string $username): void |
| 120 | + { |
| 121 | + $composerPath = $installationPath . '/composer.json'; |
| 122 | + |
| 123 | + if (!file_exists($composerPath)) { |
| 124 | + throw new \Exception('Composer.json file not found at: ' . $composerPath); |
| 125 | + } |
| 126 | + |
| 127 | + $fullCommand = "cd {$installationPath} && sudo -u {$username} composer {$command}"; |
| 128 | + |
| 129 | + |
| 130 | + \Log::info("Executing composer command: {$fullCommand}"); |
| 131 | + |
| 132 | + $result = ShellApi::exec($fullCommand); |
| 133 | + |
| 134 | +// if(!isset( $result['exit_code'])){ |
| 135 | +// dd($result); |
| 136 | +// } |
| 137 | +// |
| 138 | +// if ($result['exit_code'] !== 0) { |
| 139 | +// throw new \Exception('Composer command failed: ' . $result['output']); |
| 140 | +// } |
| 141 | + } |
| 142 | +} |
0 commit comments