update.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace App\Console\Commands;
  3. use Closure;
  4. use Illuminate\Console\Command;
  5. use Symfony\Component\Console\Helper\ProgressBar;
  6. use Symfony\Component\Process\Process;
  7. class update extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'update
  15. {--user= : The user that PHP runs under. All files will be owned by this user.}
  16. {--group= : The group that PHP runs under. All files will be owned by this group.}';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'Update your Dashboard to the latest version';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return int
  36. */
  37. public function handle()
  38. {
  39. $this->output->warning('This command does just pull the newest changes from the github repo. Verify the github repo before running this');
  40. if (version_compare(PHP_VERSION, '8.0.0') < 0) {
  41. $this->error('Cannot execute self-upgrade process. The minimum required PHP version required is 8.0.0, you have ['.PHP_VERSION.'].');
  42. }
  43. $user = 'www-data';
  44. $group = 'www-data';
  45. if ($this->input->isInteractive()) {
  46. if (is_null($this->option('user'))) {
  47. $userDetails = posix_getpwuid(fileowner('public'));
  48. $user = $userDetails['name'] ?? 'www-data';
  49. if (! $this->confirm("Your webserver user has been detected as [{$user}]: is this correct?", true)) {
  50. $user = $this->anticipate(
  51. 'Please enter the name of the user running your webserver process. This varies from system to system, but is generally "www-data", "nginx", or "apache".',
  52. [
  53. 'www-data',
  54. 'nginx',
  55. 'apache',
  56. ]
  57. );
  58. }
  59. }
  60. if (is_null($this->option('group'))) {
  61. $groupDetails = posix_getgrgid(filegroup('public'));
  62. $group = $groupDetails['name'] ?? 'www-data';
  63. if (! $this->confirm("Your webserver group has been detected as [{$group}]: is this correct?", true)) {
  64. $group = $this->anticipate(
  65. 'Please enter the name of the group running your webserver process. Normally this is the same as your user.',
  66. [
  67. 'www-data',
  68. 'nginx',
  69. 'apache',
  70. ]
  71. );
  72. }
  73. }
  74. ini_set('output_buffering', 0);
  75. if (! $this->confirm('Are you sure you want to run the upgrade process for your Dashboard?')) {
  76. return false;
  77. }
  78. $bar = $this->output->createProgressBar(9);
  79. $bar->start();
  80. $this->withProgress($bar, function () {
  81. $this->line('$upgrader> git pull');
  82. $process = Process::fromShellCommandline('git pull');
  83. $process->run(function ($type, $buffer) {
  84. $this->{$type === Process::ERR ? 'error' : 'line'}($buffer);
  85. });
  86. });
  87. $this->withProgress($bar, function () {
  88. $this->line('$upgrader> php artisan down');
  89. $this->call('down');
  90. });
  91. $this->withProgress($bar, function () {
  92. $this->line('$upgrader> chmod -R 755 storage bootstrap/cache');
  93. $process = new Process(['chmod', '-R', '755', 'storage', 'bootstrap/cache']);
  94. $process->run(function ($type, $buffer) {
  95. $this->{$type === Process::ERR ? 'error' : 'line'}($buffer);
  96. });
  97. });
  98. $this->withProgress($bar, function () {
  99. $command = ['composer', 'install', '--no-ansi'];
  100. if (config('app.env') === 'production' && ! config('app.debug')) {
  101. $command[] = '--optimize-autoloader';
  102. $command[] = '--no-dev';
  103. }
  104. $this->line('$upgrader> '.implode(' ', $command));
  105. $process = new Process($command);
  106. $process->setTimeout(10 * 60);
  107. $process->run(function ($type, $buffer) {
  108. $this->line($buffer);
  109. });
  110. });
  111. $this->withProgress($bar, function () {
  112. $this->line('$upgrader> php artisan view:clear');
  113. $this->call('view:clear');
  114. });
  115. $this->withProgress($bar, function () {
  116. $this->line('$upgrader> php artisan config:clear');
  117. $this->call('config:clear');
  118. });
  119. $this->withProgress($bar, function () {
  120. $this->line('$upgrader> php artisan migrate --force');
  121. $this->call('migrate', ['--force' => '']);
  122. });
  123. $this->withProgress($bar, function () use ($user, $group) {
  124. $this->line("\$upgrader> chown -R {$user}:{$group} *");
  125. $process = Process::fromShellCommandline("chown -R {$user}:{$group} *", $this->getLaravel()->basePath());
  126. $process->setTimeout(10 * 60);
  127. $process->run(function ($type, $buffer) {
  128. $this->{$type === Process::ERR ? 'error' : 'line'}($buffer);
  129. });
  130. });
  131. $this->withProgress($bar, function () {
  132. $this->line('$upgrader> php artisan up');
  133. $this->call('up');
  134. });
  135. $this->newLine();
  136. $this->info('Finished running upgrade.');
  137. }
  138. }
  139. protected function withProgress(ProgressBar $bar, Closure $callback)
  140. {
  141. $bar->clear();
  142. $callback();
  143. $bar->advance();
  144. $bar->display();
  145. }
  146. }