1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file is part of the `tvi/monitor-bundle` project. |
||
5 | * |
||
6 | * (c) https://github.com/turnaev/monitor-bundle/graphs/contributors |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE.md |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Tvi\MonitorBundle\Check\etcd\Etcd; |
||
13 | |||
14 | use GuzzleHttp\Client; |
||
15 | use GuzzleHttp\ClientInterface; |
||
16 | use JMS\Serializer\Annotation as JMS; |
||
17 | use Tvi\MonitorBundle\Check\CheckAbstract; |
||
18 | use ZendDiagnostics\Result\Failure; |
||
19 | use ZendDiagnostics\Result\Success; |
||
20 | |||
21 | /** |
||
22 | * @JMS\ExclusionPolicy("all") |
||
23 | * |
||
24 | * @author Vladimir Turnaev <[email protected]> |
||
25 | */ |
||
26 | class Check extends CheckAbstract |
||
27 | { |
||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $uri; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $cert; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $sslKey; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $ca; |
||
47 | |||
48 | /** |
||
49 | * @var bool |
||
50 | */ |
||
51 | protected $verify; |
||
52 | |||
53 | /** |
||
54 | * @var ClientInterface |
||
55 | */ |
||
56 | protected $client; |
||
57 | |||
58 | /** |
||
59 | * @param bool $verify |
||
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
60 | * @param string $cert |
||
0 ignored issues
–
show
|
|||
61 | * @param string $sslKey |
||
0 ignored issues
–
show
|
|||
62 | * @param string $ca |
||
0 ignored issues
–
show
|
|||
63 | * |
||
64 | * @throws \Exception |
||
65 | */ |
||
66 | 2 | public function __construct( |
|
67 | $url = 'https://localhost:2379', |
||
68 | $verify = false, |
||
69 | $cert = '/etc/etcd/cert/client-etcd.crt', |
||
70 | $sslKey = '/etc/etcd/cert/client-etcd.key', |
||
71 | $ca = '/etc/etcd/cert/ca.crt') |
||
72 | { |
||
73 | 2 | $this->url = $url; |
|
74 | 2 | $this->verify = $verify; |
|
75 | 2 | $this->cert = $cert; |
|
76 | 2 | $this->sslKey = $sslKey; |
|
77 | 2 | $this->ca = $ca; |
|
78 | |||
79 | 2 | $this->client = $this->createClient(); |
|
80 | 2 | } |
|
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | 2 | public function check() |
|
86 | { |
||
87 | try { |
||
88 | 2 | $version = $this->getVersion(); |
|
89 | |||
90 | return new Success(null, json_decode($version, true)); |
||
91 | 2 | } catch (\Exception $e) { |
|
92 | 2 | return new Failure($e->getMessage()); |
|
93 | } |
||
94 | } |
||
95 | |||
96 | /** |
||
0 ignored issues
–
show
|
|||
97 | * @throws \Exception |
||
98 | * |
||
99 | * @return ClientInterface |
||
100 | */ |
||
101 | 2 | private function createClient() |
|
102 | { |
||
103 | 2 | return new Client([ |
|
104 | 2 | 'base_uri' => $this->url, |
|
105 | 2 | 'verify' => $this->verify, |
|
106 | 'exceptions' => true, |
||
107 | 2 | 'cert' => $this->cert, |
|
108 | 2 | 'ssl_key' => $this->sslKey, |
|
109 | ]); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @return string |
||
114 | */ |
||
115 | 2 | private function getVersion() |
|
116 | { |
||
117 | 2 | $response = $this->client->get('/version'); |
|
118 | |||
119 | return $response->getBody().''; |
||
120 | } |
||
121 | } |
||
122 |