summaryrefslogtreecommitdiffstats
path: root/application/clicommands/KickstartCommand.php
blob: 80aa183e1a6f8af439b7adff087da38cc56ef728 (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
<?php

namespace Icinga\Module\Director\Clicommands;

use Icinga\Module\Director\Cli\Command;
use Icinga\Module\Director\KickstartHelper;

/**
 * Kickstart a Director installation
 *
 * Once you prepared your DB resource this command retrieves information about
 * unapplied database migration and helps applying them.
 */
class KickstartCommand extends Command
{
    /**
     * Check whether a kickstart run is required
     *
     * This is the case when there is a kickstart.ini in your Directors config
     * directory and no ApiUser in your Director DB.
     *
     * This is mostly for automation, so one could create a Puppet manifest
     * as follows:
     *
     *     exec { 'Icinga Director Kickstart':
     *        path    => '/usr/local/bin:/usr/bin:/bin',
     *        command => 'icingacli director kickstart run',
     *        onlyif  => 'icingacli director kickstart required',
     *        require => Exec['Icinga Director DB migration'],
     *     }
     *
     * Exit code 0 means that a kickstart run is required, code 2 that it is
     * not.
     */
    public function requiredAction()
    {
        if ($this->kickstart()->isConfigured()) {
            if ($this->kickstart()->isRequired()) {
                if ($this->isVerbose) {
                    echo "Kickstart has been configured and should be triggered\n";
                }

                exit(0);
            } else {
                echo "Kickstart configured, execution is not required\n";
                exit(1);
            }
        } else {
            echo "Kickstart has not been configured\n";
            exit(2);
        }
    }

    /**
     * Trigger the kickstart helper
     *
     * This will connect to the endpoint configured in your kickstart.ini,
     * store the given API user and import existing objects like zones,
     * endpoints and commands.
     *
     * /etc/icingaweb2/modules/director/kickstart.ini could look as follows:
     *
     *    [config]
     *    endpoint = "master-node.example.com"
     *
     *    ; Host can be an IP address or a hostname. Equals to endpoint name
     *    ; if not set:
     *    host = "127.0.0.1"
     *
     *    ; Port is 5665 if none given
     *    port = 5665
     *
     *    username = "director"
     *    password = "***"
     *
     */
    public function runAction()
    {
        $this->raiseLimits();
        $this->kickstart()->loadConfigFromFile()->run();
        exit(0);
    }

    protected function kickstart()
    {
        return new KickstartHelper($this->db());
    }
}