|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Startwind\Inventorio\Command; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use GuzzleHttp\RequestOptions; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
use Symfony\Component\Process\Process; |
|
10
|
|
|
|
|
11
|
|
|
class RemoteCommand extends InventorioCommand |
|
12
|
|
|
{ |
|
13
|
|
|
protected static $defaultName = 'remote'; |
|
14
|
|
|
protected static $defaultDescription = 'Start remote mode'; |
|
15
|
|
|
|
|
16
|
|
|
private const URL_HAS_COMMAND = '/inventorio/command/queued/{serverId}'; |
|
17
|
|
|
private const URL_POP_COMMAND = '/inventorio/command/pop/{serverId}'; |
|
18
|
|
|
private const URL_SEND_OUTPUT = '/inventorio/command/result/{commandId}'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @inheritDoc |
|
22
|
|
|
*/ |
|
23
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
24
|
|
|
{ |
|
25
|
|
|
$this->initConfiguration($input->getOption('configFile')); |
|
26
|
|
|
|
|
27
|
|
|
$client = new Client(); |
|
28
|
|
|
|
|
29
|
|
|
$popUrl = str_replace('{serverId}', $this->getServerId(), self::URL_POP_COMMAND); |
|
30
|
|
|
$hasUrl = str_replace('{serverId}', $this->getServerId(), self::URL_HAS_COMMAND); |
|
31
|
|
|
|
|
32
|
|
|
while (true) { |
|
33
|
|
|
$response = $client->get($this->config->getInventorioServer() . $hasUrl); |
|
34
|
|
|
$result = json_decode($response->getBody(), true); |
|
35
|
|
|
|
|
36
|
|
|
if ($result['data']['hasQueued']) { |
|
37
|
|
|
$commandResponse = $client->get($this->config->getInventorioServer() . $popUrl); |
|
38
|
|
|
$commandResult = json_decode($commandResponse->getBody(), true); |
|
39
|
|
|
|
|
40
|
|
|
$command = $commandResult['data']['command']['command']; |
|
41
|
|
|
$identifier = $commandResult['data']['command']['id']; |
|
42
|
|
|
|
|
43
|
|
|
$output->writeln('Running: ' . $command); |
|
44
|
|
|
|
|
45
|
|
|
$commandOutput = $this->runCommand($command); |
|
46
|
|
|
|
|
47
|
|
|
$sendUrl = str_replace('{commandId}', $identifier, self::URL_SEND_OUTPUT); |
|
48
|
|
|
|
|
49
|
|
|
$client->post($this->config->getInventorioServer() . $sendUrl, [ |
|
50
|
|
|
RequestOptions::JSON => ['output' => $commandOutput] |
|
51
|
|
|
]); |
|
52
|
|
|
} |
|
53
|
|
|
sleep(10); |
|
54
|
|
|
} |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function runCommand($command): array |
|
58
|
|
|
{ |
|
59
|
|
|
$commands = $this->config->getCommands(); |
|
60
|
|
|
|
|
61
|
|
|
if (!array_key_exists($command, $commands)) { |
|
62
|
|
|
return [ |
|
63
|
|
|
"output" => "No command with identifier '" . $command . "' found." |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$actualCommand = $commands[$command]; |
|
68
|
|
|
|
|
69
|
|
|
$process = Process::fromShellCommandline($actualCommand['command']); |
|
70
|
|
|
|
|
71
|
|
|
$process->run(); |
|
72
|
|
|
|
|
73
|
|
|
return [ |
|
74
|
|
|
'output' => $process->getOutput(), |
|
75
|
|
|
'error' => $process->getErrorOutput(), |
|
76
|
|
|
'actualCommand' => $actualCommand['command'], |
|
77
|
|
|
'exitCode' => $process->getExitCode() |
|
78
|
|
|
]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: