Skip to content

Commit

Permalink
add a few commands and improve some stuff
Browse files Browse the repository at this point in the history
**New Stuff**
* server:console -- see the server console
* server:say -- send a message to the console

**Fixes**
* server:list -- now checks status of server process
* server:create -- shut git the fu*k up
* server:path -- now tells you the path if it is set and a paht isn't provided
* server:stop -- now sends message to server, disablable
  • Loading branch information
Novalis committed Nov 16, 2017
1 parent ca419a7 commit c9f4a0c
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 17 deletions.
105 changes: 105 additions & 0 deletions app/Commands/Server/ConsoleCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace App\Commands\Server;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Facades\Storage;
use LaravelZero\Framework\Commands\Command;

class ConsoleCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'server:console {name? : The name of the server} {--no-warning : Don\'t show the warning}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'See the server console';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle(): void
{
try {
$servers = json_decode(Storage::get('servers.json'), true);
} catch (FileNotFoundException $e) {
$this->error('FiveM is not installed! Please run server:install');
exit;
}

$serverName = $this->argument('name');

if (empty($serverName)) {
$serverName = $this->ask('Which server');
}

$serverName = str_slug($serverName);

if (! isset($servers[$serverName])) {
$this->error('That server does not exist!');
exit;
}

$server = $servers[$serverName];

$status = [];
exec("ps auxw | grep -i fivem- | grep -v grep | awk '{print $13}'", $status);
$status = str_replace('fivem-', '', $status);

if (! $server['status'] && ! in_array($serverName, $status)) {
$this->error('That server is not up!');
exit;
}

if ($server['status'] && ! in_array($serverName, $status)) {
$this->warn("$serverName may have crashed!");
if ($this->confirm("Do you want to put it back up?")) {
$this->call('server:start', ['name' => $serverName, '-q' => true]);
} else {
$server['status'] = false;
$servers[$serverName] = $server;
Storage::put('servers.json', json_encode($servers));
}
exit;
}

if(!$this->option('no-warning')) {
$this->warn('Remember to do [CTRL+A, D] to close the console or you will crash the server!');
sleep(2);
}

system("screen -r fivem-$serverName");
}

/**
* Define the command's schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
*
* @return void
*/
public function schedule(Schedule $schedule): void
{
// $schedule->command(static::class)->everyMinute();
}
}
2 changes: 1 addition & 1 deletion app/Commands/Server/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected function checkDirectory()

protected function downloadFiles()
{
exec("cd $this->path; git clone https://github.com/citizenfx/cfx-server-data.git $this->serverName --depth=1; cd $this->serverName; rm -rf .git");
exec("cd $this->path; git clone https://github.com/citizenfx/cfx-server-data.git -q $this->serverName --depth=1; cd $this->serverName; rm -rf .git");

copy(__DIR__.'/'.'../stubs/server.cfg.stub', "$this->path/$this->serverName/server.cfg");
}
Expand Down
17 changes: 15 additions & 2 deletions app/Commands/Server/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public function handle(): void
$this->error('FiveM is not installed! Please run server:install');
exit;
}
$status = [];
exec("ps auxw | grep -i fivem- | grep -v grep | awk '{print $13}'", $status);
$status = str_replace('fivem-', '', $status);

$includePath = $this->option('path');

Expand All @@ -60,16 +63,26 @@ public function handle(): void
foreach ($servers as $name => $sData) {
$data[$name] = [];
$data[$name]['Server'] = $name;
if($sData['status']) {
if ($sData['status'] && !in_array($name, $status)) {
$this->warn("$name may have crashed!");
if ($this->confirm("Do you want to put it back up?")) {
$this->call('server:start', ['name' => $name, '-q' => true]);
}
}
$sData['status'] = in_array($name, $status);
if ($sData['status']) {
$data[$name]['Status'] = '<info>UP</info>';
} else {
$data[$name]['Status'] = '<comment>DOWN</comment>';
}
if($includePath) {
if ($includePath) {
$data[$name]['Path'] = $sData['path'];
}
$servers[$name] = $sData;
}

Storage::put('servers.json', json_encode($servers));

$this->table($headers, $data);
}

Expand Down
90 changes: 90 additions & 0 deletions app/Commands/Server/SayCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace App\Commands\Server;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Facades\Storage;
use LaravelZero\Framework\Commands\Command;

class SayCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'server:say {name?} {message?}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Send a message to the server';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle(): void
{
try {
$servers = json_decode(Storage::get('servers.json'), true);
} catch (FileNotFoundException $e) {
$this->error('FiveM is not installed! Please run server:install');
exit;
}

$serverName = $this->argument('name');

if (empty($serverName)) {
$serverName = $this->ask('Which server');
}

$message = $this->argument('message');

if (empty($message)) {
$message = $this->ask('What is your question');
}

$serverName = str_slug($serverName);

$server = $servers[$serverName];

if (empty($server)) {
$this->error('That server does not exist!');
exit;
}

if (! $server['status']) {
$this->error('That server is not up!');
exit;
}

exec("screen -S fivem-$serverName -X stuff 'say $message'$(echo -ne '\015')");
}

/**
* Define the command's schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
*
* @return void
*/
public function schedule(Schedule $schedule): void
{
// $schedule->command(static::class)->everyMinute();
}
}
7 changes: 6 additions & 1 deletion app/Commands/Server/SetPathCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,15 @@ public function handle(): void

$path = $this->argument('path');

if (empty($path)) {
if (empty($path) && ! isset($settings['server-path'])) {
$path = $this->ask('Path');
}

if (isset($settings['server-path'])) {
$this->info('Current Path: ' . $settings['server-path']);
exit;
}

$this->path = realpath(str_replace('~', $_SERVER['HOME'], $path));

$this->checkDirectory();
Expand Down
14 changes: 12 additions & 2 deletions app/Commands/Server/StopCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class StopCommand extends Command
*
* @var string
*/
protected $signature = 'server:stop {name? : The server name}';
protected $signature = 'server:stop {name? : The server name} {--no-warning : Don\'t send the warning}';

/**
* The console command description.
Expand Down Expand Up @@ -63,11 +63,21 @@ public function handle(): void

$server = $servers[$serverName];

if (empty($server)) {
if (! isset($server)) {
$this->error('That server does not exist!');
exit;
}

if (! $server['status']) {
$this->error('That server is not up!');
exit;
}

if (! $this->option('no-warning')) {
exec("screen -S fivem-$serverName -X stuff 'say The server shutting down!'$(echo -ne '\015')");
sleep(3);
}

exec("screen -XS fivem-$serverName quit");

$server['status'] = false;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"scripts": {
"post-install-cmd": [
"php fsm storage:create"
"php fsm files:create"
]
},
"bin": ["fsm"]
Expand Down
2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/*
* Here goes the application version.
*/
'version' => '0.0.8',
'version' => '0.0.9',

/*
* Here goes the application default command. By default
Expand Down
7 changes: 0 additions & 7 deletions fsm
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
#!/usr/bin/env php
<?php

/**
* Laravel Zero - An elegant starting point for your Laravel Console Application
*
* @package Laravel Zero
* @author Nuno Maduro <enunomaduro@gmail.com>
*/

require_once __DIR__ . '/bootstrap/init.php';
2 changes: 0 additions & 2 deletions storage/app/.gitignore

This file was deleted.

0 comments on commit c9f4a0c

Please sign in to comment.