2020-08-31 10:11:06 +08:00
|
|
|
<?php
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
declare(strict_types=1);
|
2020-08-31 10:11:06 +08:00
|
|
|
|
2021-07-04 15:45:30 +08:00
|
|
|
namespace ZM\Command\Generate;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2021-07-04 15:45:30 +08:00
|
|
|
use ZM\Config\ZMConfig;
|
|
|
|
|
use ZM\Utils\DataProvider;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
2021-07-04 15:45:30 +08:00
|
|
|
class SystemdGenerateCommand extends Command
|
2020-08-31 10:11:06 +08:00
|
|
|
{
|
|
|
|
|
// the name of the command (the part after "bin/console")
|
2021-07-04 15:45:30 +08:00
|
|
|
protected static $defaultName = 'generate:systemd';
|
2020-08-31 10:11:06 +08:00
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
protected function configure()
|
|
|
|
|
{
|
|
|
|
|
$this->setDescription('生成框架的 systemd 配置文件');
|
2021-03-25 16:18:09 +08:00
|
|
|
}
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
|
|
|
{
|
2021-07-09 12:59:07 +08:00
|
|
|
ZMConfig::setDirectory(DataProvider::getSourceRootDir() . '/config');
|
2021-03-25 16:18:09 +08:00
|
|
|
$path = $this->generate();
|
2022-03-15 18:05:33 +08:00
|
|
|
$output->writeln('<info>成功生成 systemd 文件,位置:' . $path . '</info>');
|
|
|
|
|
$output->writeln('<info>有关如何使用 systemd 配置文件,请访问 `https://github.com/zhamao-robot/zhamao-framework/issues/36`</info>');
|
2021-04-06 01:19:56 +08:00
|
|
|
return 0;
|
2020-08-31 10:11:06 +08:00
|
|
|
}
|
2021-03-25 16:18:09 +08:00
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
private function generate()
|
|
|
|
|
{
|
2021-03-25 16:18:09 +08:00
|
|
|
$s = "[Unit]\nDescription=zhamao-framework Daemon\nAfter=rc-local.service\n\n[Service]\nType=simple";
|
2022-03-15 18:05:33 +08:00
|
|
|
$s .= "\nUser=" . exec('whoami');
|
2021-03-25 16:18:09 +08:00
|
|
|
$s .= "\nGroup=" . exec("groups | awk '{print $1}'");
|
|
|
|
|
$s .= "\nWorkingDirectory=" . getcwd();
|
2021-06-16 00:17:30 +08:00
|
|
|
global $argv;
|
2021-07-04 15:45:30 +08:00
|
|
|
$s .= "\nExecStart=" . PHP_BINARY . " {$argv[0]} server";
|
2021-03-25 16:18:09 +08:00
|
|
|
$s .= "\nRestart=always\n\n[Install]\nWantedBy=multi-user.target\n";
|
2022-03-15 18:05:33 +08:00
|
|
|
@mkdir(getcwd() . '/resources/');
|
|
|
|
|
file_put_contents(ZMConfig::get('global', 'zm_data') . 'zhamao.service', $s);
|
|
|
|
|
return ZMConfig::get('global', 'zm_data') . 'zhamao.service';
|
2021-03-25 16:18:09 +08:00
|
|
|
}
|
2020-08-31 10:11:06 +08:00
|
|
|
}
|