File Coverage

blib/lib/Metabrik/Server/Kibana.pm
Criterion Covered Total %
statement 9 97 9.2
branch 0 48 0.0
condition 0 8 0.0
subroutine 3 12 25.0
pod 2 8 25.0
total 14 173 8.0


line stmt bran cond sub pod time code
1             #
2             # $Id$
3             #
4             # server::kibana Brik
5             #
6             package Metabrik::Server::Kibana;
7 1     1   756 use strict;
  1         2  
  1         28  
8 1     1   5 use warnings;
  1         2  
  1         27  
9              
10 1     1   5 use base qw(Metabrik::System::Process);
  1         1  
  1         1248  
11              
12             sub brik_properties {
13             return {
14 0     0 1   revision => '$Revision$',
15             tags => [ qw(unstable elk) ],
16             author => 'GomoR ',
17             license => 'http://opensource.org/licenses/BSD-3-Clause',
18             attributes => {
19             datadir => [ qw(datadir) ],
20             listen => [ qw(ip_address) ],
21             port => [ qw(port) ],
22             conf_file => [ qw(file) ],
23             log_file => [ qw(file) ],
24             version => [ qw(4.6.2|5.0.0|5.5.2) ],
25             no_output => [ qw(0|1) ],
26             es_nodes => [ qw(node|node_list) ],
27             binary => [ qw(binary_path) ],
28             },
29             attributes_default => {
30             listen => '127.0.0.1',
31             port => 5601,
32             version => '5.5.2',
33             no_output => 1,
34             es_nodes => 'http://localhost:9200',
35             },
36             commands => {
37             install => [ ],
38             start => [ qw(conf_file) ],
39             stop => [ ],
40             generate_conf => [ qw(conf|OPTIONAL) ],
41             status => [ ],
42             },
43             require_modules => {
44             'Metabrik::File::Text' => [ ],
45             },
46             require_binaries => {
47             tar => [ ],
48             },
49             need_packages => {
50             ubuntu => [ qw(tar openjdk-8-jre-headless) ],
51             debian => [ qw(tar openjdk-8-jre-headless) ],
52             kali => [ qw(tar openjdk-8-jre-headless) ],
53             freebsd => [ qw(openjdk node012 kibana45) ],
54             },
55             };
56             }
57              
58             sub brik_use_properties {
59 0     0 1   my $self = shift;
60              
61 0           my $datadir = $self->datadir;
62 0           my $version = $self->version;
63 0           my $conf_file = $datadir."/kibana-$version.conf";
64 0           my $log_file = $datadir."/kibana-$version.log";
65              
66             return {
67 0           attributes_default => {
68             conf_file => $conf_file,
69             log_file => $log_file,
70             },
71             };
72             }
73              
74             sub get_binary {
75 0     0 0   my $self = shift;
76              
77 0           my $binary = $self->binary;
78 0 0         if (! defined($binary)) {
79 0           my $datadir = $self->datadir;
80 0           my $version = $self->version;
81 0           $binary = $datadir.'/kibana-'.$version.'-linux-x86_64/bin/kibana';
82             }
83              
84 0 0         $self->brik_help_run_file_not_found('get_binary', $binary) or return;
85              
86 0           $self->log->verbose("get_binary: found binary [$binary]");
87              
88 0           return $binary;
89             }
90              
91             sub generate_conf {
92 0     0 0   my $self = shift;
93 0           my ($conf_file) = @_;
94              
95 0   0       $conf_file ||= $self->conf_file;
96              
97 0           my $version = $self->version;
98 0           my $listen = $self->listen;
99 0           my $port = $self->port;
100 0           my $es_nodes = $self->es_nodes;
101              
102 0           my $node = $es_nodes;
103 0 0         if ($es_nodes eq 'ARRAY') {
104 0           $node = $es_nodes->[0];
105             }
106              
107 0           my $conf =<
108             server.port: $port
109             server.host: "$listen"
110             #server.basePath: ""
111             #server.name: "your-hostname"
112             elasticsearch.url: "$node"
113             EOF
114             ;
115              
116 0 0         my $ft = Metabrik::File::Text->new_from_brik_init($self) or return;
117 0           $ft->append(0);
118 0           $ft->overwrite(1);
119              
120 0 0         $ft->write($conf, $conf_file) or return;
121              
122 0           return $conf_file;
123             }
124              
125             sub install {
126 0     0 0   my $self = shift;
127              
128 0           my $datadir = $self->datadir;
129 0           my $version = $self->version;
130              
131 0           my $url = 'https://artifacts.elastic.co/downloads/kibana/kibana-5.5.2-linux-x86_64.tar.gz';
132 0 0         if ($version eq '4.6.2') {
    0          
133 0           $url = 'https://download.elastic.co/kibana/kibana/kibana-4.6.2-linux-x86_64.tar.gz';
134             }
135             elsif ($version eq '5.0.0') {
136 0           $url = 'https://artifacts.elastic.co/downloads/kibana/kibana-5.0.0-linux-x86_64.tar.gz';
137             }
138              
139 0 0         my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return;
140 0 0         $cw->mirror($url, "$datadir/kibana.tar.gz") or return;
141              
142 0   0       my $cwd = defined($self->shell) && $self->shell->pwd || '/tmp';
143              
144 0 0         if (defined($self->shell)) {
145 0 0         $self->shell->run_cd($datadir) or return;
146             }
147             else {
148 0 0         chdir($datadir) or return $self->log->error("install: chdir: $!");
149             }
150              
151 0           my $cmd = "tar zxvf kibana.tar.gz";
152 0 0         my $r = $self->execute($cmd) or return;
153              
154 0 0         if (defined($self->shell)) {
155 0 0         $self->shell->run_cd($cwd) or return;
156             }
157             else {
158 0 0         chdir($cwd) or return $self->log->error("install: chdir: $!");
159             }
160              
161 0           return 1;
162             }
163              
164             sub start {
165 0     0 0   my $self = shift;
166 0           my ($conf_file) = @_;
167              
168 0 0         if ($self->status) {
169 0           return $self->info_process_is_running;
170             }
171              
172 0   0       $conf_file ||= $self->conf_file;
173              
174 0 0         $self->brik_help_run_undef_arg('start', $conf_file) or return;
175 0 0         $self->brik_help_run_file_not_found('start', $conf_file) or return;
176              
177 0           my $log_file = $self->log_file;
178 0           my $no_output = $self->no_output;
179              
180 0 0         my $binary = $self->get_binary or return;
181              
182 0           $self->close_output_on_start($no_output);
183              
184 0           $self->use_pidfile(0);
185              
186             $self->SUPER::start(sub {
187 0     0     $self->log->verbose("Within daemon");
188              
189             # -p port, -l log-file -c config-file -e elasticsearch-uri
190 0           my $cmd = "$binary -Q -l $log_file -c $conf_file";
191              
192 0           $self->system($cmd);
193              
194 0           $self->log->error("start: son failed to start");
195 0           exit(1);
196 0           });
197              
198 0           return 1;
199             }
200              
201             sub stop {
202 0     0 0   my $self = shift;
203              
204 0 0         if (! $self->status) {
205 0           return $self->info_process_is_not_running;
206             }
207              
208 0 0         my $binary = $self->get_binary or return;
209              
210 0           my $log_file = $self->conf_file;
211 0           my $conf_file = $self->conf_file;
212              
213 0           my $string = "-c $conf_file";
214 0 0         my $pid = $self->get_pid_from_string($string) or return;
215              
216 0           return $self->kill($pid);
217             }
218              
219             sub status {
220 0     0 0   my $self = shift;
221              
222 0           my $log_file = $self->log_file;
223 0           my $conf_file = $self->conf_file;
224              
225 0           my $string = "-c $conf_file";
226 0 0         if ($self->is_running_from_string($string)) {
227 0           $self->verbose_process_is_running;
228 0           return 1;
229             }
230              
231 0           $self->verbose_process_is_not_running;
232 0           return 0;
233             }
234              
235             1;
236              
237             __END__