Removed beanstalkd stuff for now as it's not necessary; updated the server creation function to actually download the specified server JAR version

This commit is contained in:
Gregory Ballantine 2022-10-08 00:02:29 -04:00
parent fdb31eeb00
commit 5c35389ae3
7 changed files with 62 additions and 134 deletions

View File

@ -20,6 +20,6 @@
"slim/twig-view": "^3.3", "slim/twig-view": "^3.3",
"hassankhan/config": "^3.0", "hassankhan/config": "^3.0",
"php-di/php-di": "^6.4", "php-di/php-di": "^6.4",
"pda/pheanstalk": "~4.0" "imangazaliev/didom": "^1.13"
} }
} }

107
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "6ac1bc7f15734931ede8901fed3ed3eb", "content-hash": "895fe7eeb340eea7f4d603b1042f1634",
"packages": [ "packages": [
{ {
"name": "fig/http-message-util", "name": "fig/http-message-util",
@ -125,6 +125,56 @@
}, },
"time": "2022-03-26T19:37:50+00:00" "time": "2022-03-26T19:37:50+00:00"
}, },
{
"name": "imangazaliev/didom",
"version": "1.13",
"source": {
"type": "git",
"url": "https://github.com/Imangazaliev/DiDOM.git",
"reference": "10c4033d56e599f09183959ed90bf17519b9e38b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Imangazaliev/DiDOM/zipball/10c4033d56e599f09183959ed90bf17519b9e38b",
"reference": "10c4033d56e599f09183959ed90bf17519b9e38b",
"shasum": ""
},
"require": {
"php": ">=5.4"
},
"require-dev": {
"phpunit/phpunit": "^4.8"
},
"type": "library",
"autoload": {
"psr-4": {
"DiDom\\": "src/DiDom/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Imangazaliev Muhammad",
"email": "imangazalievm@gmail.com"
}
],
"description": "Simple and fast HTML parser",
"homepage": "https://github.com/Imangazaliev/DiDOM",
"keywords": [
"didom",
"html",
"parser",
"xml"
],
"support": {
"issues": "https://github.com/Imangazaliev/DiDOM/issues",
"source": "https://github.com/Imangazaliev/DiDOM/tree/develop"
},
"time": "2017-12-08T15:20:07+00:00"
},
{ {
"name": "laravel/serializable-closure", "name": "laravel/serializable-closure",
"version": "v1.2.2", "version": "v1.2.2",
@ -235,61 +285,6 @@
}, },
"time": "2018-02-13T20:26:39+00:00" "time": "2018-02-13T20:26:39+00:00"
}, },
{
"name": "pda/pheanstalk",
"version": "v4.0.4",
"source": {
"type": "git",
"url": "https://github.com/pheanstalk/pheanstalk.git",
"reference": "1a43eb97a53144a2e692bce2ea2be721cc9913a4"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/pheanstalk/pheanstalk/zipball/1a43eb97a53144a2e692bce2ea2be721cc9913a4",
"reference": "1a43eb97a53144a2e692bce2ea2be721cc9913a4",
"shasum": ""
},
"require": {
"ext-mbstring": "*",
"php": ">=7.1.0"
},
"require-dev": {
"phpunit/phpunit": "^7"
},
"type": "library",
"autoload": {
"psr-4": {
"Pheanstalk\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Paul Annesley",
"email": "paul@annesley.cc",
"homepage": "http://paul.annesley.cc/",
"role": "Developer"
},
{
"name": "Sam Mousa",
"email": "sam@mousa.nl",
"role": "Maintainer"
}
],
"description": "PHP client for beanstalkd queue",
"homepage": "https://github.com/pheanstalk/pheanstalk",
"keywords": [
"beanstalkd"
],
"support": {
"issues": "https://github.com/pheanstalk/pheanstalk/issues",
"source": "https://github.com/pheanstalk/pheanstalk/tree/v4.0.4"
},
"time": "2021-11-19T15:00:20+00:00"
},
{ {
"name": "php-di/invoker", "name": "php-di/invoker",
"version": "2.3.3", "version": "2.3.3",

View File

@ -1,8 +1,3 @@
{ {
"server_directory": "/opt/minecraft", "server_directory": "/opt/minecraft"
"beanstalkd": {
"host": "localhost",
"port": 11300
}
} }

View File

@ -2,7 +2,6 @@
namespace BitGoblin\MCST\Controllers; namespace BitGoblin\MCST\Controllers;
use Pheanstalk\Pheanstalk;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Views\Twig; use Slim\Views\Twig;
@ -22,12 +21,6 @@ class HomeController extends Controller {
array_push($minecraftServers, new Server($m)); array_push($minecraftServers, new Server($m));
} }
// create pheanstalk object and create a job
$pheanstalk = Pheanstalk::create($config->get('beanstalkd.host'), $config->get('beanstalkd.port'));
$pheanstalk
->useTube('mcst')
->put("refresh-server-downloads", Pheanstalk::DEFAULT_PRIORITY);
$view = Twig::fromRequest($request); $view = Twig::fromRequest($request);
return $view->render($response, 'index.twig', [ return $view->render($response, 'index.twig', [
'servers' => $minecraftServers, 'servers' => $minecraftServers,

View File

@ -2,6 +2,7 @@
namespace BitGoblin\MCST\Controllers; namespace BitGoblin\MCST\Controllers;
use DiDom\Document;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Views\Twig; use Slim\Views\Twig;
@ -26,8 +27,14 @@ class ServerController extends Controller {
$serverDir = join('/', array($config->get('server_directory'), $params['serverName'])); $serverDir = join('/', array($config->get('server_directory'), $params['serverName']));
mkdir($serverDir); mkdir($serverDir);
// find the server JAR URL for the version
$versionPageLink = 'https://minecraft.fandom.com/wiki/Java_Edition_' . $params['serverVersion'];
$dom = new Document($versionPageLink, true);
$infobox = $dom->find('.notaninfobox')[0];
$serverLinkElem = $infobox->find("a:contains('Server')");
$serverJarUrl = $serverLinkElem[0]->attr('href');
// grab the server JAR file // grab the server JAR file
$serverJarUrl = "https://piston-data.mojang.com/v1/objects/f69c284232d7c7580bd89a5a4931c3581eae1378/server.jar";
$serverJarName = "server_" . $params['serverVersion'] . ".jar"; $serverJarName = "server_" . $params['serverVersion'] . ".jar";
$serverJarPath = join('/', array($serverDir, $serverJarName)); $serverJarPath = join('/', array($serverDir, $serverJarName));
file_put_contents($serverJarPath, file_get_contents($serverJarUrl)); file_put_contents($serverJarPath, file_get_contents($serverJarUrl));
@ -35,7 +42,7 @@ class ServerController extends Controller {
// create the start.sh shell script // create the start.sh shell script
$scriptFilePath = join('/', array($serverDir, 'start.sh')); $scriptFilePath = join('/', array($serverDir, 'start.sh'));
$scriptFile = fopen($scriptFilePath, 'w'); $scriptFile = fopen($scriptFilePath, 'w');
$scriptContent = "#!/bin/sh\n\ncd " . $serverDir . "\njava -Xmx2048M -Xms2048M -jar server_1.19.2.jar nogui"; $scriptContent = "#!/bin/sh\n\ncd " . $serverDir . "\njava -Xmx2048M -Xms2048M -jar server_" . $params['serverVersion'] . ".jar nogui";
fwrite($scriptFile, $scriptContent); fwrite($scriptFile, $scriptContent);
fclose($scriptFile); fclose($scriptFile);
chmod($scriptFilePath, 0755); chmod($scriptFilePath, 0755);

View File

@ -1,17 +0,0 @@
<?php
namespace BitGoblin\MCST\Runners;
class RefreshServerDownloads {
protected $config;
public function __construct($config) {
$this->config = $config;
}
public function start() {
echo "Refreshing server download URLs...\n";
}
}

View File

@ -1,45 +0,0 @@
<?php
use Noodlehaus\Config;
use Noodlehaus\Parser\Json;
use Pheanstalk\Pheanstalk;
use BitGoblin\MCST\Runners\RefreshServerDownloads;
require __DIR__ . '/../vendor/autoload.php';
// Load app configuration
$config = Config::load(__DIR__ . '/../conf/defaults.json');
// create pheanstalk object
$pheanstalk = Pheanstalk::create($config->get('beanstalkd.host'), $config->get('beanstalkd.port'));
// we want jobs from 'mcst' tube only.
$pheanstalk->watch('mcst');
while (true) {
echo "Waiting for a new job...\n";
// try to reserve a job
$job = $pheanstalk->reserve();
// check if we actually got a job before trying to do work
if (isset($job)) {
$jobPayload = $job->getData();
switch ($jobPayload) {
case 'refresh-server-downloads':
$rsd = new RefreshServerDownloads($config);
$rsd->start();
break;
default:
echo "Runner command " . $jobPayload . " isn't supported.\n";
break;
}
// remove job
$pheanstalk->delete($job);
}
sleep(60);
}