2021-01-23 23:52:57 +00:00
< ? php
namespace Pterodactyl\Console\Commands ;
use Closure ;
use Illuminate\Console\Command ;
use Pterodactyl\Console\Kernel ;
use Symfony\Component\Process\Process ;
use Symfony\Component\Console\Helper\ProgressBar ;
class UpgradeCommand extends Command
{
2021-01-24 00:12:13 +00:00
protected const DEFAULT_URL = 'https://github.com/pterodactyl/panel/releases/%s/panel.tar.gz' ;
2021-01-24 00:07:49 +00:00
2021-01-23 23:52:57 +00:00
/** @var string */
2021-01-24 00:12:13 +00:00
protected $signature = ' p : upgrade
2021-04-20 15:39:34 +00:00
{ -- user = : The user that PHP runs under . All files will be owned by this user . }
{ -- group = : The group that PHP runs under . All files will be owned by this group . }
2021-01-24 00:12:13 +00:00
{ -- url = : The specific archive to download . }
{ -- release = : A specific Pterodactyl version to download from GitHub . Leave blank to use latest . }
{ -- skip - download : If set no archive will be downloaded . } ' ;
2021-01-23 23:52:57 +00:00
/** @var string */
2021-01-24 00:07:49 +00:00
protected $description = 'Downloads a new archive for Pterodactyl from GitHub and then executes the normal upgrade commands.' ;
2021-01-23 23:52:57 +00:00
/**
* Executes an upgrade command which will run through all of our standard
* commands for Pterodactyl and enable users to basically just download
* the archive and execute this and be done .
*
* This places the application in maintenance mode as well while the commands
* are being executed .
*
* @ throws \Exception
*/
public function handle ()
{
2021-01-24 00:07:49 +00:00
$skipDownload = $this -> option ( 'skip-download' );
if ( ! $skipDownload ) {
$this -> output -> warning ( 'This command does not verify the integrity of downloaded assets. Please ensure that you trust the download source before continuing. If you do not wish to download an archive, please indicate that using the --skip-download flag, or answering "no" to the question below.' );
$this -> output -> comment ( 'Download Source (set with --url=):' );
2021-01-24 00:12:13 +00:00
$this -> line ( $this -> getUrl ());
2021-01-24 00:07:49 +00:00
}
2021-01-24 00:32:43 +00:00
if ( version_compare ( PHP_VERSION , '7.4.0' ) < 0 ) {
$this -> error ( 'Cannot execute self-upgrade process. The minimum required PHP version required is 7.4.0, you have [' . PHP_VERSION . '].' );
}
2021-01-24 00:27:23 +00:00
$user = 'www-data' ;
2021-04-20 08:06:19 +00:00
$group = 'www-data' ;
2021-01-23 23:52:57 +00:00
if ( $this -> input -> isInteractive ()) {
2021-01-24 00:07:49 +00:00
if ( ! $skipDownload ) {
$skipDownload = ! $this -> confirm ( 'Would you like to download and unpack the archive files for the latest version?' , true );
}
2021-01-24 00:27:23 +00:00
if ( is_null ( $this -> option ( 'user' ))) {
2021-04-20 15:39:34 +00:00
$userDetails = posix_getpwuid ( fileowner ( 'public' ));
$user = $userDetails [ 'name' ] ? ? 'www-data' ;
2021-01-24 00:27:23 +00:00
2021-04-20 15:39:34 +00:00
if ( ! $this -> confirm ( " Your webserver user has been detected as [ { $user } ]: is this correct? " , true )) {
2021-01-24 00:27:23 +00:00
$user = $this -> anticipate (
2021-04-20 15:39:34 +00:00
'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".' ,
2021-01-24 00:27:23 +00:00
[
2021-04-20 15:39:34 +00:00
'www-data' ,
'nginx' ,
'apache' ,
]
);
}
}
if ( is_null ( $this -> option ( 'group' ))) {
$groupDetails = posix_getgrgid ( filegroup ( 'public' ));
$group = $groupDetails [ 'name' ] ? ? 'www-data' ;
if ( ! $this -> confirm ( " Your webserver group has been detected as [ { $group } ]: is this correct? " , true )) {
$group = $this -> anticipate (
'Please enter the name of the group running your webserver process. Normally this is the same as your user.' ,
[
'www-data' ,
'nginx' ,
'apache' ,
2021-01-24 00:27:23 +00:00
]
);
}
}
2021-01-23 23:52:57 +00:00
if ( ! $this -> confirm ( 'Are you sure you want to run the upgrade process for your Panel?' )) {
return ;
}
}
ini_set ( 'output_buffering' , 0 );
2021-01-24 00:27:23 +00:00
$bar = $this -> output -> createProgressBar ( $skipDownload ? 9 : 10 );
2021-01-23 23:52:57 +00:00
$bar -> start ();
2021-01-24 00:07:49 +00:00
if ( ! $skipDownload ) {
$this -> withProgress ( $bar , function () {
2021-01-24 00:12:13 +00:00
$this -> line ( " \$ upgrader> curl -L \" { $this -> getUrl () } \" | tar -xzv " );
2021-03-08 10:19:20 +00:00
$process = Process :: fromShellCommandline ( " curl -L \" { $this -> getUrl () } \" | tar -xzv " );
2021-01-24 00:07:49 +00:00
$process -> run ( function ( $type , $buffer ) {
$this -> { $type === Process :: ERR ? 'error' : 'line' }( $buffer );
});
});
}
2021-01-23 23:52:57 +00:00
$this -> withProgress ( $bar , function () {
$this -> line ( '$upgrader> php artisan down' );
$this -> call ( 'down' );
});
$this -> withProgress ( $bar , function () {
$this -> line ( '$upgrader> chmod -R 755 storage bootstrap/cache' );
$process = new Process ([ 'chmod' , '-R' , '755' , 'storage' , 'bootstrap/cache' ]);
$process -> run ( function ( $type , $buffer ) {
$this -> { $type === Process :: ERR ? 'error' : 'line' }( $buffer );
});
});
$this -> withProgress ( $bar , function () {
$command = [ 'composer' , 'install' , '--no-ansi' ];
if ( config ( 'app.env' ) === 'production' && ! config ( 'app.debug' )) {
$command [] = '--optimize-autoloader' ;
$command [] = '--no-dev' ;
}
$this -> line ( '$upgrader> ' . implode ( ' ' , $command ));
$process = new Process ( $command );
2021-01-24 00:29:18 +00:00
$process -> setTimeout ( 10 * 60 );
2021-01-23 23:52:57 +00:00
$process -> run ( function ( $type , $buffer ) {
$this -> line ( $buffer );
});
});
/** @var \Illuminate\Foundation\Application $app */
$app = require __DIR__ . '/../../../bootstrap/app.php' ;
/** @var \Pterodactyl\Console\Kernel $kernel */
$kernel = $app -> make ( Kernel :: class );
$kernel -> bootstrap ();
$this -> setLaravel ( $app );
$this -> withProgress ( $bar , function () {
$this -> line ( '$upgrader> php artisan view:clear' );
$this -> call ( 'view:clear' );
});
$this -> withProgress ( $bar , function () {
$this -> line ( '$upgrader> php artisan config:clear' );
$this -> call ( 'config:clear' );
});
$this -> withProgress ( $bar , function () {
$this -> line ( '$upgrader> php artisan migrate --seed --force' );
$this -> call ( 'migrate' , [ '--seed' => '' , '--force' => '' ]);
});
2021-04-20 08:06:19 +00:00
$this -> withProgress ( $bar , function () use ( $user , $group ) {
$this -> line ( " \$ upgrader> chown -R { $user } : { $group } * " );
$process = Process :: fromShellCommandline ( " chown -R { $user } : { $group } * " , $this -> getLaravel () -> basePath ());
2021-01-24 00:29:18 +00:00
$process -> setTimeout ( 10 * 60 );
2021-01-24 00:27:23 +00:00
$process -> run ( function ( $type , $buffer ) {
$this -> { $type === Process :: ERR ? 'error' : 'line' }( $buffer );
});
});
2021-01-23 23:52:57 +00:00
$this -> withProgress ( $bar , function () {
$this -> line ( '$upgrader> php artisan queue:restart' );
$this -> call ( 'queue:restart' );
});
$this -> withProgress ( $bar , function () {
$this -> line ( '$upgrader> php artisan up' );
$this -> call ( 'up' );
});
$this -> newLine ();
$this -> info ( 'Finished running upgrade.' );
}
protected function withProgress ( ProgressBar $bar , Closure $callback )
{
$bar -> clear ();
$callback ();
$bar -> advance ();
$bar -> display ();
}
2021-01-24 00:12:13 +00:00
protected function getUrl () : string
{
if ( $this -> option ( 'url' )) {
return $this -> option ( 'url' );
}
return sprintf ( self :: DEFAULT_URL , $this -> option ( 'release' ) ? 'download/v' . $this -> option ( 'release' ) : 'latest/download' );
}
2021-01-23 23:52:57 +00:00
}