innmind / server-control
允许从PHP控制服务器
5.2.3
2024-09-18 10:12 UTC
Requires
- php: ~8.2
- innmind/filesystem: ~7.0
- innmind/immutable: ~4.15|~5.0
- innmind/stream: ~4.0
- innmind/time-continuum: ^3.1,<3.3|^3.4.1
- innmind/time-warp: ^3.0
- innmind/url: ~4.0
- psr/log: ~3.0
Requires (Dev)
- innmind/black-box: ~5.5
- innmind/coding-standard: ~2.0
- phpunit/phpunit: ~10.2
- vimeo/psalm: ~5.12
This package is auto-updated.
Last update: 2024-09-18 10:13:00 UTC
README
提供对向服务器发送指令的访问权限。
重要:要正确使用此库,您必须使用 vimeo/psalm
验证您的代码
安装
composer require innmind/server-control
使用方法
use Innmind\Server\Control\{ ServerFactory, Server\Command, Server\Process\Output\Type, Server\Process\Pid, Server\Signal, Server\Volumes\Name, }; use Innmind\TimeContinuum\Earth\Clock; use Innmind\TimeWarp\Halt\Usleep; use Innmind\Stream\Streams; use Innmind\Url\Path; use Innmind\Immutable\Str; $server = ServerFactory::build( new Clock, Streams::fromAmbientAuthority(), new Usleep, ); $server ->processes() ->execute( Command::foreground('bin/console') ->withArgument('debug:router') ) ->output() ->foreach(static function(Str $data, Type $type): void { $type = match ($type) { Type::error => 'ERR : ', Type::output => 'OUT : ', }; echo $type.$data->toString(); }); $server ->processes() ->kill( new Pid(42), Signal::kill, ); $server->volumes()->mount(new Name('/dev/disk2s1'), Path::of('/somewhere')); // the path is only interpreted for linux $server->volumes()->unmount(new Name('/dev/disk2s1')); $server->reboot(); $server->shutdown();
脚本
有时您可能想在服务器上运行一组命令。您可以轻松地这样做,如下所示
use Innmind\Server\Control\Server\Script; $script = Script::of( 'apt-get install php-fpm -y', 'service nginx start', ); $script($server);
如果任何命令失败,它将停止脚本并抛出异常。
远程服务器控制
use Innmind\Server\Control\Servers\Remote; use Innmind\Url\Authority\{ Host, Port, UserInformation\User, }; $server = new Remote( $server, User::of('john'), Host::of('example.com'), Port::of(42), ); $server->processes()->execute(Command::foreground('ls'));
这将运行 ssh -p 42 john@example.com ls
。
重要:在远程服务器上,指定环境变量或输入流将不被考虑。
日志记录
use Innmind\Server\Control\Servers\Logger; use Psr\Log\LoggerInterface; $server = Logger::psr($server, /** an instance of LoggerInterface */);