summaryrefslogtreecommitdiffstats
path: root/vendor/gipfl/icinga-cli-daemon/src/IcingaCli.php
blob: 8aad4c633a60ec8ff25f172fbf751498901c8347 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php

namespace gipfl\IcingaCliDaemon;

use Exception;
use Evenement\EventEmitterTrait;
use gipfl\Protocol\JsonRpc\Connection;
use React\EventLoop\LoopInterface;
use React\Promise\Deferred;
use React\Promise\Stream;

class IcingaCli
{
    use EventEmitterTrait;

    /** @var IcingaCliRunner */
    protected $runner;

    /** @var Connection|null */
    protected $rpc;

    protected $arguments = [];

    /** @var  \React\Stream\WritableStreamInterface|null */
    protected $stdin;

    /** @var Deferred|null */
    protected $deferredStdin;

    /** @var \React\Stream\ReadableStreamInterface|null */
    protected $stdout;

    /** @var Deferred|null */
    protected $deferredStdout;

    /** @var \React\Stream\ReadableStreamInterface|null */
    protected $stderr;

    /** @var Deferred|null */
    protected $deferredStderr;

    public function __construct(IcingaCliRunner $runner = null)
    {
        if ($runner === null) {
            $runner = new IcingaCliRunner();
        }
        $this->runner = $runner;
        $this->init();
    }

    protected function init()
    {
        // Override this if you want.
    }

    public function setArguments($arguments)
    {
        $this->arguments = $arguments;

        return $this;
    }

    public function getArguments()
    {
        return $this->arguments;
    }

    public function run(LoopInterface $loop)
    {
        $process = $this->runner->command($this->getArguments());
        $canceller = function () use ($process) {
            // TODO: first soft, then hard
            $process->terminate();
        };
        $deferred = new Deferred($canceller);
        $process->on('exit', function ($exitCode, $termSignal) use ($deferred) {
            $state = new FinishedProcessState($exitCode, $termSignal);
            if ($state->succeeded()) {
                $deferred->resolve();
            } else {
                $deferred->reject(new Exception($state->getReason()));
            }
        });

        $process->start($loop);
        if ($this->deferredStdin instanceof Deferred) {
            $this->deferredStdin->resolve($process->stdin);
        } else {
            $this->stdin = $process->stdin;
        }
        if ($this->deferredStdout instanceof Deferred) {
            $this->deferredStdout->resolve($process->stdout);
        } else {
            $this->stdout = $process->stdout;
        }
        if ($this->deferredStderr instanceof Deferred) {
            $this->deferredStderr->resolve($process->stderr);
        } else {
            $this->stderr = $process->stderr;
        }
        $this->emit('start', [$process]);

        return $deferred->promise();
    }

    /**
     * @return \React\Stream\WritableStreamInterface
     */
    public function stdin()
    {
        if ($this->stdin === null) {
            $this->deferredStdin = new Deferred();
            $this->stdin = Stream\unwrapWritable($this->deferredStdin->promise());
        }

        return $this->stdin;
    }

    /**
     * @return \React\Stream\ReadableStreamInterface
     */
    public function stdout()
    {
        if ($this->stdout === null) {
            $this->deferredStdout = new Deferred();
            $this->stdout = Stream\unwrapReadable($this->deferredStdout->promise());
        }

        return $this->stdout;
    }

    /**
     * @return \React\Stream\ReadableStreamInterface
     */
    public function stderr()
    {
        if ($this->stderr === null) {
            $this->deferredStderr = new Deferred();
            $this->stderr = Stream\unwrapReadable($this->deferredStderr->promise());
        }

        return $this->stderr;
    }
}