|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Command; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Import classes |
|
7
|
|
|
*/ |
|
8
|
|
|
use Symfony\Component\Console\Command\Command; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Question\Question; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Import functions |
|
15
|
|
|
*/ |
|
16
|
|
|
use function sprintf; |
|
17
|
|
|
use function strtr; |
|
18
|
|
|
use function trim; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* GenerateRoadRunnerSystemdUnitCommand |
|
22
|
|
|
* |
|
23
|
|
|
* @link https://wiki.debian.org/systemd/Services |
|
24
|
|
|
* @link https://manpages.debian.org/buster/systemd/systemd.service.5.en.html |
|
25
|
|
|
*/ |
|
26
|
|
|
final class GenerateRoadRunnerSystemdUnitCommand extends Command |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private const TEMPLATE = <<<'EOT' |
|
33
|
|
|
[Unit] |
|
34
|
|
|
After=network.target |
|
35
|
|
|
|
|
36
|
|
|
[Service] |
|
37
|
|
|
Type=simple |
|
38
|
|
|
User={user} |
|
39
|
|
|
Group={group} |
|
40
|
|
|
Restart=always |
|
41
|
|
|
WorkingDirectory={cwd} |
|
42
|
|
|
ExecStart={rr} serve -dv |
|
43
|
|
|
ExecReload={rr} http:reset |
|
44
|
|
|
ExecStop=/bin/kill -s TERM $MAINPID |
|
45
|
|
|
|
|
46
|
|
|
[Install] |
|
47
|
|
|
WantedBy=multi-user.target |
|
48
|
|
|
EOT; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritDoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function configure() : void |
|
54
|
|
|
{ |
|
55
|
|
|
$this->setName('app:roadrunner:generate-systemd-unit'); |
|
56
|
|
|
$this->setDescription('Generates the systemd unit for RoadRunner'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritDoc} |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) : int |
|
63
|
|
|
{ |
|
64
|
|
|
$rr = trim(`which rr`); |
|
65
|
|
|
$cwd = trim(`pwd`); |
|
66
|
|
|
$user = trim(`id -u -n`); |
|
67
|
|
|
$group = trim(`id -g -n`); |
|
68
|
|
|
$questioner = $this->getHelper('question'); |
|
69
|
|
|
$replacements = []; |
|
70
|
|
|
|
|
71
|
|
|
$format = 'The RoadRunner path [<fg=yellow>%s</>]: '; |
|
72
|
|
|
$question = new Question(sprintf($format, $rr), $rr); |
|
73
|
|
|
$replacements['{rr}'] = $questioner->ask($input, $output, $question); |
|
74
|
|
|
|
|
75
|
|
|
$format = 'The directory in which the service will be started [<fg=yellow>%s</>]: '; |
|
76
|
|
|
$question = new Question(sprintf($format, $cwd), $cwd); |
|
77
|
|
|
$replacements['{cwd}'] = $questioner->ask($input, $output, $question); |
|
78
|
|
|
|
|
79
|
|
|
$format = 'The user on whose behalf the service will be started [<fg=yellow>%s</>]: '; |
|
80
|
|
|
$question = new Question(sprintf($format, $user), $user); |
|
81
|
|
|
$replacements['{user}'] = $questioner->ask($input, $output, $question); |
|
82
|
|
|
|
|
83
|
|
|
$format = 'The group on whose behalf the service will be started [<fg=yellow>%s</>]: '; |
|
84
|
|
|
$question = new Question(sprintf($format, $group), $group); |
|
85
|
|
|
$replacements['{group}'] = $questioner->ask($input, $output, $question); |
|
86
|
|
|
|
|
87
|
|
|
$output->writeln(strtr(static::TEMPLATE, $replacements)); |
|
88
|
|
|
|
|
89
|
|
|
return 0; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|