summaryrefslogtreecommitdiffstats
path: root/application/clicommands/ConvertCommand.php
blob: a05538b1944a9a75b8598e59ceaebf6324785a0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/* TopLevelView module for Icingaweb2 - Copyright (c) 2017 Icinga Development Team <info@icinga.com> */

namespace Icinga\Module\Toplevelview\Clicommands;

use Icinga\Exception\ConfigurationError;
use Icinga\Module\Toplevelview\Command;
use Icinga\Module\Toplevelview\Config\ConfigEmitter;
use Icinga\Module\Toplevelview\Legacy\LegacyDbHelper;
use Icinga\Module\Toplevelview\ViewConfig;
use Zend_Db_Adapter_Pdo_Sqlite;

/**
 * Converts a TopLevelView database into the new YAML configuration format
 */
class ConvertCommand extends Command
{
    protected $dbConnections = array();

    public function init()
    {
        parent::init();

        if (! extension_loaded('pdo_sqlite')) {
            throw new ConfigurationError('You need the PHP extension "pdo_sqlite" in order to convert TopLevelView');
        }
    }

    /**
     * List all hierarchies in the database
     *
     * Arguments:
     *   --db <file>  SQLite3 data from from old TopLevelView module
     */
    public function listAction()
    {
        $dbFile = $this->params->getRequired('db');
        $db = $this->sqlite($dbFile);

        $helper = new LegacyDbHelper($db);
        foreach ($helper->fetchHierarchies() as $root) {
            printf("[%d] %s\n", $root['id'], $root['name']);
        }
    }

    /**
     * Generate a YAML config file for Icinga Web 2 module
     *
     * Arguments:
     *   --db <file>      SQLite3 data from from old TopLevelView module
     *   --id <id>        Database id to export (see list)
     *   --output <file>  Write to file (default '-' for stdout)
     *   --name <name>    If name is specified instead of file,
     *                    config is saved under that name
     */
    public function convertAction()
    {
        $dbFile = $this->params->getRequired('db');
        $db = $this->sqlite($dbFile);

        $id = $this->params->getRequired('id');

        $output = $this->params->get('output', null);
        $name = $this->params->get('name', null);
        $format = $this->params->get('format', 'yaml');

        $helper = new LegacyDbHelper($db, $this->monitoringBackend());
        $tree = $helper->fetchTree($id);

        $emitter = ConfigEmitter::fromLegacyTree($tree);

        if ($name !== null and $output === null) {
            $viewConfig = new ViewConfig();
            $viewConfig->setConfigDir();
            $viewConfig->setFormat(ViewConfig::FORMAT_YAML);
            $viewConfig->setName($name);
            $viewConfig->setText($emitter->emitYAML($format));
            $viewConfig->store();
            printf("Saved as config %s\n", $name);
            exit(0);
        }

        $text = $emitter->emit($format);
        if ($output === null || $output === '-') {
            echo $text;
        } else {
            file_put_contents($output, $text);
        }
    }

    /**
     * Sets up the Zend PDO resource for SQLite
     *
     * @param string $file
     *
     * @return Zend_Db_Adapter_Pdo_Sqlite
     */
    protected function sqlite($file)
    {
        return new Zend_Db_Adapter_Pdo_Sqlite(array(
            'dbname' => $file,
        ));
    }
}