summaryrefslogtreecommitdiffstats
path: root/application/clicommands/MigrateCommand.php
blob: cb4e3898172fd7c9d78703c2449ed3da70cb1662 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php

/* Icinga Web 2 X.509 Module | (c) 2023 Icinga GmbH | GPLv2 */

namespace Icinga\Module\X509\Clicommands;

use DateTime;
use Icinga\Application\Logger;
use Icinga\Authentication\Auth;
use Icinga\Module\X509\Command;
use Icinga\Module\X509\Common\Database;
use Icinga\Module\X509\Job;
use Icinga\Repository\IniRepository;
use Icinga\User;
use Icinga\Util\Json;
use ipl\Scheduler\Cron;
use ipl\Sql\Connection;
use ipl\Sql\Expression;
use stdClass;

use function ipl\Stdlib\get_php_type;

class MigrateCommand extends Command
{
    /**
     * Migrate the jobs config rom INI to the database
     *
     * USAGE
     *
     *     icingacli x509 migrate jobs --author=<name>
     *
     * OPTIONS
     *
     * --author=<name>
     *     An Icinga Web 2 user used to mark as an author for all the migrated jobs.
     */
    public function jobsAction(): void
    {
        /** @var string $author */
        $author = $this->params->getRequired('author');
        /** @var User $user */
        $user = Auth::getInstance()->getUser();
        $user->setUsername($author);

        $this->migrateJobs();

        Logger::info('Successfully applied all pending migrations');
    }

    protected function migrateJobs(): void
    {
        $repo = new class () extends IniRepository {
            /** @var array<string, array<int, string>> */
            protected $queryColumns = [
                'jobs' => ['name', 'cidrs', 'ports', 'exclude_targets', 'schedule', 'frequencyType']
            ];

            /** @var array<string, array<string, string>> */
            protected $configs = [
                'jobs' => [
                    'module'    => 'x509',
                    'name'      => 'jobs',
                    'keyColumn' => 'name'
                ]
            ];
        };

        $conn = Database::get();
        $conn->transaction(function (Connection $conn) use ($repo) {
            /** @var User $user */
            $user = Auth::getInstance()->getUser();
            /** @var stdClass $data */
            foreach ($repo->select() as $data) {
                $config = [];
                if (! isset($data->frequencyType) && ! empty($data->schedule)) {
                    $frequency = new Cron($data->schedule);
                    $config = [
                        'type'      => get_php_type($frequency),
                        'frequency' => Json::encode($frequency)
                    ];
                } elseif (! empty($data->schedule)) {
                    $config = [
                        'type'      => $data->frequencyType,
                        'frequency' => $data->schedule // Is already json encoded
                    ];
                }

                $excludes = $data->exclude_targets;
                if (empty($excludes)) {
                    $excludes = new Expression('NULL');
                }

                $conn->insert('x509_job', [
                    'name'            => $data->name,
                    'author'          => $user->getUsername(),
                    'cidrs'           => $data->cidrs,
                    'ports'           => $data->ports,
                    'exclude_targets' => $excludes,
                    'ctime'           => (new DateTime())->getTimestamp() * 1000,
                    'mtime'           => (new DateTime())->getTimestamp() * 1000
                ]);

                $jobId = (int) $conn->lastInsertId();
                if (! empty($config)) {
                    $config['rescan'] = 'n';
                    $config['full_scan'] = 'n';
                    $config['since_last_scan'] = Job::DEFAULT_SINCE_LAST_SCAN;

                    $conn->insert('x509_schedule', [
                        'job_id' => $jobId,
                        'name'   => $data->name . ' Schedule',
                        'author' => $user->getUsername(),
                        'config' => Json::encode($config),
                        'ctime'  => (new DateTime())->getTimestamp() * 1000,
                        'mtime'  => (new DateTime())->getTimestamp() * 1000,
                    ]);
                }
            }
        });
    }
}