Skip to content

Instantly share code, notes, and snippets.

@iksaku
Last active June 1, 2016 03:55
Show Gist options
  • Select an option

  • Save iksaku/2047a6f98659a20e5581 to your computer and use it in GitHub Desktop.

Select an option

Save iksaku/2047a6f98659a20e5581 to your computer and use it in GitHub Desktop.
Update all the servers located in a "Parent directory" (Need custom "src/" folder method for accessing PocketMine API)
<?php
/**
* @name ServerUpdater
* @main ServerUpdater\Loader
* @version 1.0.0
* @api 1.12.0
* @description Updates all server's PHAR file
* @author iksaku
*/
namespace ServerUpdater{
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\command\PluginIdentifiableCommand;
use pocketmine\plugin\PluginBase;
use pocketmine\scheduler\AsyncTask;
use pocketmine\Server;
use pocketmine\utils\TextFormat;
use pocketmine\utils\Utils;
class Loader extends PluginBase{
public function onEnable(){
/** Just register the new command ;) */
$this->getServer()->getCommandMap()->register("ServersUpdater", new UpdateCommand($this));
}
/** @var AsyncTask|null */
private $updating;
/**
* @return bool
*/
public function callAsyncUpdater(){
if($this->updating !== null && $this->updating->isRunning()){
$this->getLogger()->notice(TextFormat::RED . "\"Updating Script\" is already in process... Please be patient ;)");
return false;
}
$this->getServer()->getLogger()->alert(TextFormat::YELLOW . "Starting \"Updater Script\"...");
$this->getServer()->getScheduler()->scheduleAsyncTask($this->updating = new Updater($this));
return true;
}
}
class UpdateCommand extends Command implements PluginIdentifiableCommand{
/**
* Update command...
* Call a new "Update Async Task" when executed!
*/
/** @var Loader */
private $plugin;
public function __construct(Loader $plugin){
parent::__construct("update", "Update all the servers", "/update");
$this->plugin = $plugin;
}
/**
* @return Loader
*/
public function getPlugin(){
return $this->plugin;
}
/**
* @param CommandSender $sender
* @param string $alias
* @param array $args
* @return bool
*/
public function execute(CommandSender $sender, $alias, array $args){
if(!$this->getPlugin()->callAsyncUpdater()){
return false;
}
return true;
}
}
class Updater extends AsyncTask{
/** @var string */
private $parent;
public function __construct(Loader $plugin){
$this->parent = dirname($plugin->getServer()->getDataPath()) . DIRECTORY_SEPARATOR;
}
public function onRun(){
// Remove any piece of old files... If present...
if(file_exists($file = $this->parent . "archive.zip")){
unlink($file);
}
if(is_dir($dir = $this->parent . "archive" . DIRECTORY_SEPARATOR)){
rmdir($dir);
}
// Create a new file called "archive.zip" that will contain the zipped update...
$file = fopen($this->parent . "archive.zip", "w+");
// Download the zipped update from Jenkins...
fwrite($file, Utils::getURL("http://jenkins.pocketmine.net/job/PocketMine-MP/Development/artifact/*zip*/archive.zip"));
// After download is complete, close the file...
fclose($file);
// Open the zipped update...
$zip = new \ZipArchive();
$zip->open($this->parent . "archive.zip");
// Extract the update from the zip to the parent directory (The directory where all servers are located)...
$zip->extractTo($this->parent);
// Close the zip file...
$zip->close();
// Remove the zip file...
if(file_exists($file = $this->parent . "archive.zip")){
unlink($file);
}
/*
* Scan the extracted zip directory to find the update file (This is because the name changes each time there's an update),
* rename the update file to "PocketMine-MP" and move it to the parent directory for faster reference in next steps...
*/
foreach(scandir($dir = $this->parent . "archive" . DIRECTORY_SEPARATOR) as $f){
if(substr($f, -5) === ".phar"){
rename($dir . $f, $this->parent . "PocketMine-MP.phar");
}
}
// Remove the (now) unnecessary extracted (From the zip) directory...
if(is_dir($dir = $this->parent . "archive" . DIRECTORY_SEPARATOR)){
rmdir($dir);
}
// Scan all the directories and validate that they're servers and copy the update file...
foreach(scandir($this->parent) as $dir){
if(is_dir($d = $this->parent . $dir . DIRECTORY_SEPARATOR)){
if(is_dir($d . "src" . DIRECTORY_SEPARATOR)){
// Copy to root folder of the server, allowing another trick at server's initialization
copy($this->parent . "PocketMine-MP.phar", $d . "update.phar");
}
}
}
// Remove the now unnecessary update file from the parent directory...
if(file_exists($file = $this->parent . "PocketMine-MP.phar")){
unlink($file);
}
}
public function onCompletion(Server $server){
$server->getLogger()->alert(TextFormat::GREEN . "All servers have been updated! Please restart your servers...");
}
}
}
@echo off
TITLE PocketMine-MP server software for Minecraft: Pocket Edition
cd /d %~dp0
if exist bin\php\php.exe (
set PHP_BINARY=bin\php\php.exe
) else (
set PHP_BINARY=php
)
if exist src\PocketMine-MP.phar (
set POCKETMINE_FILE=src\PocketMine-MP.phar
) else (
if exist PocketMine-MP.phar (
set POCKETMINE_FILE=PocketMine-MP.phar
) else (
if exist src\pocketmine\PocketMine.php (
set POCKETMINE_FILE=src\pocketmine\PocketMine.php
) else (
echo "Couldn't find a valid PocketMine-MP installation"
pause
exit 1
)
)
)
if exist update.phar (
move /y update.phar %POCKETMINE_FILE%
)
REM if exist bin\php\php_wxwidgets.dll (
REM %PHP_BINARY% %POCKETMINE_FILE% --enable-gui %*
REM ) else (
if exist bin\mintty.exe (
start "" bin\mintty.exe -o Columns=88 -o Rows=32 -o AllowBlinking=0 -o FontQuality=3 -o Font="DejaVu Sans Mono" -o FontHeight=10 -o CursorType=0 -o CursorBlinks=1 -h error -t "PocketMine-MP" -i bin/pocketmine.ico -w max %PHP_BINARY% %POCKETMINE_FILE% --enable-ansi %*
) else (
%PHP_BINARY% -c bin\php %POCKETMINE_FILE% %*
)
REM )
#!/bin/bash
DIR="$(cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
cd "$DIR"
DO_LOOP="no"
while getopts "p:f:l" OPTION 2> /dev/null; do
case ${OPTION} in
p)
PHP_BINARY="$OPTARG"
;;
f)
POCKETMINE_FILE="$OPTARG"
;;
l)
DO_LOOP="yes"
;;
\?)
break
;;
esac
done
if [ "$PHP_BINARY" == "" ]; then
if [ -f ./bin/php5/bin/php ]; then
export PHPRC=""
PHP_BINARY="./bin/php5/bin/php"
elif [ type php 2>/dev/null ]; then
PHP_BINARY=$(type -p php)
else
echo "Couldn't find a working PHP binary, please use the installer."
exit 1
fi
fi
if [ "$POCKETMINE_FILE" == "" ]; then
if [ -f ./src/PocketMine-MP.phar ]; then
POCKETMINE_FILE="./src/PocketMine-MP.phar"
elif [ -f ./PocketMine-MP.phar ]; then
POCKETMINE_FILE="./PocketMine-MP.phar"
elif [ -f ./src/pocketmine/PocketMine.php ]; then
POCKETMINE_FILE="./src/pocketmine/PocketMine.php"
else
echo "Couldn't find a valid PocketMine-MP installation"
exit 1
fi
fi
if [ -f ./update.phar ]; then
mv -f ./update.phar "$POCKETMINE_FILE"
fi
LOOPS=0
set +e
while [ "$LOOPS" -eq 0 ] || [ "$DO_LOOP" == "yes" ]; do
if [ "$DO_LOOP" == "yes" ]; then
"$PHP_BINARY" "$POCKETMINE_FILE" $@
else
exec "$PHP_BINARY" "$POCKETMINE_FILE" $@
fi
((LOOPS++))
done
if [ ${LOOPS} -gt 1 ]; then
echo "Restarted $LOOPS times"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment