Issues (45)

src/Command/GradeCommand.php (1 issue)

1
<?php
2
3
namespace Startwind\Inventorio\Command;
4
5
use Exception;
6
use GuzzleHttp\Client;
7
use Startwind\Inventorio\Collector\ClientAwareCollector;
8
use Startwind\Inventorio\Collector\InventoryAwareCollector;
9
use Startwind\Inventorio\Exec\Runner;
10
use Startwind\Inventorio\Metrics\Memory\Memory;
11
use Startwind\Inventorio\Reporter\InventorioGradeReporter;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Helper\ProgressBar;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class GradeCommand extends CollectorCommand
19
{
20
    protected static $defaultName = 'grade';
21
    protected static $defaultDescription = 'Grade this server';
22
23
    private const NOT_APPLICABLE = 'not applicable';
24
25
    protected function configure(): void
26
    {
27
        $this->addOption('remote', null, InputOption::VALUE_REQUIRED, 'Remote connection <user>@<ip>');
28
        parent::configure();
29
    }
30
31
    /**
32
     * @inheritDoc
33
     */
34
    protected function execute(InputInterface $input, OutputInterface $output): int
35
    {
36
        $startRun = time();
37
38
        $remote = $input->getOption('remote');
39
40
        if ($remote) {
41
            Runner::getInstance()->setRemote($remote);
42
        }
43
44
        $this->initConfiguration($input->getOption('configFile'));
45
46
        $reporter = new InventorioGradeReporter($output, $this->config->getInventorioServer(), $this->getServerId(), $this->getUserId());
47
48
        $debugMode = $input->getOption('debug');
49
50
        if (!$this->isInitialized()) {
51
            $output->writeln('<error>System was not initialized. Please run inventorio init.</error>');
52
            return Command::FAILURE;
53
        }
54
55
        $this->initCollectors($debugMode);
56
57
        $inventory = [];
58
59
        $client = new \Startwind\Inventorio\Util\Client(new Client());
60
61
        $progressBar = new ProgressBar($output, count($this->collectors));
62
63
        $progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% - %message%');
64
        $progressBar->setMessage('initializing');
65
66
        $progressBar->start();
67
68
        foreach ($this->collectors as $collector) {
69
            if ($debugMode) $start = time();
70
71
            $progressBar->setMessage('collector: ' . $collector->getIdentifier());
72
            $progressBar->display();
73
74
            if ($collector instanceof InventoryAwareCollector) {
75
                $collector->setInventory($inventory);
76
            }
77
            if ($collector instanceof ClientAwareCollector) {
78
                $collector->setClient($client);
79
            }
80
81
            $collected = $collector->collect();
82
83
            if ($collected) {
84
                $inventory[$collector->getIdentifier()] = $collected;
85
            } else {
86
                $inventory[$collector->getIdentifier()] = self::NOT_APPLICABLE;
87
            }
88
89
            $progressBar->advance();
90
91
            if ($debugMode) {
92
                $output->writeln('DEBUG: running ' . $collector->getIdentifier() . ' took ' . time() - $start . ' seconds');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $start does not seem to be defined for all execution paths leading up to this point.
Loading history...
93
            }
94
        }
95
        
96
        $progressBar->setMessage('finished after ' . time() - $startRun . ' seconds');
97
98
        $progressBar->finish();
99
100
        $output->writeln(['', '']);
101
102
        if ($debugMode) {
103
            $output->writeln('DEBUG: collection result:');
104
            $output->writeln(json_encode($inventory, JSON_PRETTY_PRINT));
105
        }
106
107
        Memory::getInstance()->setCollection($inventory);
108
109
        try {
110
            $reporter->report($inventory);
111
        } catch (Exception $exception) {
112
            $output->writeln('<error>                           ');
113
            $output->writeln('  Unable to run reporter.  ');
114
            $output->writeln('                           </error>');
115
            $output->writeln('');
116
            $output->writeln(' <comment>Message: ' . $exception->getMessage() . '</comment>');
117
            return Command::FAILURE;
118
        }
119
120
        return Command::SUCCESS;
121
    }
122
}
123