| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # server::kibana Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Server::Kibana; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik::System::Process); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable elk) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 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 | my $self = shift; | ||
| 60 | |||
| 61 | my $datadir = $self->datadir; | ||
| 62 | my $version = $self->version; | ||
| 63 | my $conf_file = $datadir."/kibana-$version.conf"; | ||
| 64 | my $log_file = $datadir."/kibana-$version.log"; | ||
| 65 | |||
| 66 | return { | ||
| 67 | attributes_default => { | ||
| 68 | conf_file => $conf_file, | ||
| 69 | log_file => $log_file, | ||
| 70 | }, | ||
| 71 | }; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub get_binary { | ||
| 75 | my $self = shift; | ||
| 76 | |||
| 77 | my $binary = $self->binary; | ||
| 78 | if (! defined($binary)) { | ||
| 79 | my $datadir = $self->datadir; | ||
| 80 | my $version = $self->version; | ||
| 81 | $binary = $datadir.'/kibana-'.$version.'-linux-x86_64/bin/kibana'; | ||
| 82 | } | ||
| 83 | |||
| 84 | $self->brik_help_run_file_not_found('get_binary', $binary) or return; | ||
| 85 | |||
| 86 | $self->log->verbose("get_binary: found binary [$binary]"); | ||
| 87 | |||
| 88 | return $binary; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub generate_conf { | ||
| 92 | my $self = shift; | ||
| 93 | my ($conf_file) = @_; | ||
| 94 | |||
| 95 | $conf_file ||= $self->conf_file; | ||
| 96 | |||
| 97 | my $version = $self->version; | ||
| 98 | my $listen = $self->listen; | ||
| 99 | my $port = $self->port; | ||
| 100 | my $es_nodes = $self->es_nodes; | ||
| 101 | |||
| 102 | my $node = $es_nodes; | ||
| 103 | if ($es_nodes eq 'ARRAY') { | ||
| 104 | $node = $es_nodes->[0]; | ||
| 105 | } | ||
| 106 | |||
| 107 | my $conf =<<EOF | ||
| 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 | my $ft = Metabrik::File::Text->new_from_brik_init($self) or return; | ||
| 117 | $ft->append(0); | ||
| 118 | $ft->overwrite(1); | ||
| 119 | |||
| 120 | $ft->write($conf, $conf_file) or return; | ||
| 121 | |||
| 122 | return $conf_file; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub install { | ||
| 126 | my $self = shift; | ||
| 127 | |||
| 128 | my $datadir = $self->datadir; | ||
| 129 | my $version = $self->version; | ||
| 130 | |||
| 131 | my $url = 'https://artifacts.elastic.co/downloads/kibana/kibana-5.5.2-linux-x86_64.tar.gz'; | ||
| 132 | if ($version eq '4.6.2') { | ||
| 133 | $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 | $url = 'https://artifacts.elastic.co/downloads/kibana/kibana-5.0.0-linux-x86_64.tar.gz'; | ||
| 137 | } | ||
| 138 | |||
| 139 | my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return; | ||
| 140 | $cw->mirror($url, "$datadir/kibana.tar.gz") or return; | ||
| 141 | |||
| 142 | my $cwd = defined($self->shell) && $self->shell->pwd || '/tmp'; | ||
| 143 | |||
| 144 | if (defined($self->shell)) { | ||
| 145 | $self->shell->run_cd($datadir) or return; | ||
| 146 | } | ||
| 147 | else { | ||
| 148 | chdir($datadir) or return $self->log->error("install: chdir: $!"); | ||
| 149 | } | ||
| 150 | |||
| 151 | my $cmd = "tar zxvf kibana.tar.gz"; | ||
| 152 | my $r = $self->execute($cmd) or return; | ||
| 153 | |||
| 154 | if (defined($self->shell)) { | ||
| 155 | $self->shell->run_cd($cwd) or return; | ||
| 156 | } | ||
| 157 | else { | ||
| 158 | chdir($cwd) or return $self->log->error("install: chdir: $!"); | ||
| 159 | } | ||
| 160 | |||
| 161 | return 1; | ||
| 162 | } | ||
| 163 | |||
| 164 | sub start { | ||
| 165 | my $self = shift; | ||
| 166 | my ($conf_file) = @_; | ||
| 167 | |||
| 168 | if ($self->status) { | ||
| 169 | return $self->info_process_is_running; | ||
| 170 | } | ||
| 171 | |||
| 172 | $conf_file ||= $self->conf_file; | ||
| 173 | |||
| 174 | $self->brik_help_run_undef_arg('start', $conf_file) or return; | ||
| 175 | $self->brik_help_run_file_not_found('start', $conf_file) or return; | ||
| 176 | |||
| 177 | my $log_file = $self->log_file; | ||
| 178 | my $no_output = $self->no_output; | ||
| 179 | |||
| 180 | my $binary = $self->get_binary or return; | ||
| 181 | |||
| 182 | $self->close_output_on_start($no_output); | ||
| 183 | |||
| 184 | $self->use_pidfile(0); | ||
| 185 | |||
| 186 | $self->SUPER::start(sub { | ||
| 187 | $self->log->verbose("Within daemon"); | ||
| 188 | |||
| 189 | # -p port, -l log-file -c config-file -e elasticsearch-uri | ||
| 190 | my $cmd = "$binary -Q -l $log_file -c $conf_file"; | ||
| 191 | |||
| 192 | $self->system($cmd); | ||
| 193 | |||
| 194 | $self->log->error("start: son failed to start"); | ||
| 195 | exit(1); | ||
| 196 | }); | ||
| 197 | |||
| 198 | return 1; | ||
| 199 | } | ||
| 200 | |||
| 201 | sub stop { | ||
| 202 | my $self = shift; | ||
| 203 | |||
| 204 | if (! $self->status) { | ||
| 205 | return $self->info_process_is_not_running; | ||
| 206 | } | ||
| 207 | |||
| 208 | my $binary = $self->get_binary or return; | ||
| 209 | |||
| 210 | my $log_file = $self->conf_file; | ||
| 211 | my $conf_file = $self->conf_file; | ||
| 212 | |||
| 213 | my $string = "-c $conf_file"; | ||
| 214 | my $pid = $self->get_pid_from_string($string) or return; | ||
| 215 | |||
| 216 | return $self->kill($pid); | ||
| 217 | } | ||
| 218 | |||
| 219 | sub status { | ||
| 220 | my $self = shift; | ||
| 221 | |||
| 222 | my $log_file = $self->log_file; | ||
| 223 | my $conf_file = $self->conf_file; | ||
| 224 | |||
| 225 | my $string = "-c $conf_file"; | ||
| 226 | if ($self->is_running_from_string($string)) { | ||
| 227 | $self->verbose_process_is_running; | ||
| 228 | return 1; | ||
| 229 | } | ||
| 230 | |||
| 231 | $self->verbose_process_is_not_running; | ||
| 232 | return 0; | ||
| 233 | } | ||
| 234 | |||
| 235 | 1; | ||
| 236 | |||
| 237 | __END__ | ||