File Coverage

blib/lib/Metabrik/Server/Elasticsearch.pm
Criterion Covered Total %
statement 9 98 9.1
branch 0 46 0.0
condition 0 5 0.0
subroutine 3 12 25.0
pod 2 8 25.0
total 14 169 8.2


line stmt bran cond sub pod time code
1             #
2             # $Id$
3             #
4             # server::elasticsearch Brik
5             #
6             package Metabrik::Server::Elasticsearch;
7 1     1   1020 use strict;
  1         3  
  1         28  
8 1     1   10 use warnings;
  1         2  
  1         27  
9              
10 1     1   5 use base qw(Metabrik::System::Process);
  1         3  
  1         542  
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             pidfile => [ qw(file) ],
24             version => [ qw(2.4.1|5.0.0|5.5.2) ],
25             no_output => [ qw(0|1) ],
26             cluster_name => [ qw(name) ],
27             node_name => [ qw(name) ],
28             db_dir => [ qw(directory) ],
29             log_dir => [ qw(directory) ],
30             binary => [ qw(binary_path) ],
31             },
32             attributes_default => {
33             listen => '127.0.0.1',
34             port => 9200,
35             version => '5.5.2',
36             no_output => 1,
37             cluster_name => 'metabrik',
38             node_name => 'metabrik-1',
39             },
40             commands => {
41             install => [ ],
42             get_binary => [ ],
43             start => [ ],
44             stop => [ ],
45             generate_conf => [ qw(conf_file|OPTIONAL) ],
46             # XXX: ./bin/plugin -install lmenezes/elasticsearch-kopf
47             #install_plugin => [ qw(plugin) ],
48             status => [ ],
49             },
50             require_modules => {
51             'Metabrik::File::Text' => [ ],
52             'Metabrik::System::File' => [ ],
53             },
54             require_binaries => {
55             tar => [ ],
56             },
57             need_packages => {
58             ubuntu => [ qw(tar openjdk-8-jre-headless) ],
59             debian => [ qw(tar openjdk-8-jre-headless) ],
60             kali => [ qw(tar openjdk-8-jre-headless) ],
61             freebsd => [ qw(openjdk elasticsearch2) ],
62             },
63             };
64             }
65              
66             sub brik_use_properties {
67 0     0 1   my $self = shift;
68              
69 0           my $datadir = $self->datadir;
70 0           my $version = $self->version;
71 0           my $pidfile = $datadir."/elasticsearch-$version.pid";
72 0           my $conf_file = $datadir."/elasticsearch-$version/config/elasticsearch.yml";
73 0           my $db_dir = $datadir."/db-$version";
74 0           my $log_dir = $datadir."/log-$version";
75              
76             return {
77 0           attributes_default => {
78             conf_file => $conf_file,
79             pidfile => $pidfile,
80             db_dir => $db_dir,
81             log_dir => $log_dir,
82             },
83             };
84             }
85              
86             sub get_binary {
87 0     0 0   my $self = shift;
88              
89 0           my $binary = $self->binary;
90 0 0         if (! defined($binary)) {
91 0           my $datadir = $self->datadir;
92 0           my $version = $self->version;
93 0           $binary = $datadir.'/elasticsearch-'.$version.'/bin/elasticsearch';
94             }
95              
96 0 0         $self->brik_help_run_file_not_found('get_binary', $binary) or return;
97              
98 0           $self->log->verbose("get_binary: found binary [$binary]");
99              
100 0           return $binary;
101             }
102              
103             sub generate_conf {
104 0     0 0   my $self = shift;
105 0           my ($conf_file) = @_;
106              
107 0   0       $conf_file ||= $self->conf_file;
108              
109 0           my $version = $self->version;
110 0           my $cluster_name = $self->cluster_name;
111 0           my $node_name = $self->node_name;
112 0           my $listen = $self->listen;
113 0           my $port = $self->port;
114 0           my $db_dir = $self->db_dir;
115 0           my $log_dir = $self->log_dir;
116              
117 0           $self->log->debug("mkdir db_dir [$db_dir]");
118 0           $self->log->debug("mkdir log_dir [$log_dir]");
119              
120 0 0         my $sf = Metabrik::System::File->new_from_brik_init($self) or return;
121 0 0         $sf->mkdir($db_dir) or return;
122 0 0         $sf->mkdir($log_dir) or return;
123              
124 0           (my $path_conf = $conf_file) =~ s{/elasticsearch.yml$}{};
125              
126 0           my $conf =<
127             #bootstrap.memory_lock: true
128             node.master: true
129             node.data: true
130             node.ingest: true
131             cluster.name: $cluster_name
132             node.name: $node_name
133             path.conf: $path_conf
134             path.data: $db_dir
135             path.logs: $log_dir
136             discovery.zen.minimum_master_nodes: 1
137             discovery.zen.ping.unicast.hosts: ["$listen"]
138             network.bind_host: ["$listen"]
139             network.publish_host: $listen
140             http.port: $port
141             EOF
142             ;
143              
144 0 0         my $ft = Metabrik::File::Text->new_from_brik_init($self) or return;
145 0           $ft->append(0);
146 0           $ft->overwrite(1);
147              
148 0 0         $ft->write($conf, $conf_file) or return;
149              
150 0           return $conf_file;
151             }
152              
153             sub install {
154 0     0 0   my $self = shift;
155              
156 0           my $datadir = $self->datadir;
157 0           my $version = $self->version;
158              
159 0           my $url = 'https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.2.tar.gz';
160 0 0         if ($version eq '2.4.1') {
    0          
161 0           $url = 'https://download.elastic.co/elasticsearch/release/org/'.
162             'elasticsearch/distribution/tar/elasticsearch/2.4.1/'.
163             'elasticsearch-2.4.1.tar.gz';
164             }
165             elsif ($version eq '5.0.0') {
166 0           $url = 'https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.0.0.tar.gz';
167             }
168              
169 0 0         my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return;
170 0 0         $cw->mirror($url, "$datadir/es.tar.gz") or return;
171              
172 0   0       my $cwd = defined($self->shell) && $self->shell->pwd || '/tmp';
173              
174 0 0         if (defined($self->shell)) {
175 0 0         $self->shell->run_cd($datadir) or return;
176             }
177             else {
178 0 0         chdir($datadir) or return $self->log->error("install: chdir: $!");
179             }
180              
181 0           my $cmd = "tar zxvf es.tar.gz";
182 0 0         my $r = $self->execute($cmd) or return;
183              
184 0 0         if (defined($self->shell)) {
185 0 0         $self->shell->run_cd($cwd) or return;
186             }
187             else {
188 0 0         chdir($cwd) or return $self->log->error("install: chdir: $!");
189             }
190              
191 0           return 1;
192             }
193              
194             sub start {
195 0     0 0   my $self = shift;
196              
197 0 0         if ($self->status) {
198 0           return $self->info_process_is_running;
199             }
200              
201 0           my $conf_file = $self->conf_file;
202 0 0         $self->brik_help_run_file_not_found('start', $conf_file) or return;
203              
204 0           my $no_output = $self->no_output;
205              
206 0 0         my $binary = $self->get_binary or return;
207              
208 0           $self->close_output_on_start($no_output);
209              
210 0           my $datadir = $self->datadir;
211 0           my $pidfile = $self->pidfile;
212              
213 0           $self->use_pidfile(0);
214              
215             $self->SUPER::start(sub {
216 0     0     $self->log->verbose("Within daemon");
217              
218 0           my $cmd = "$binary -p $pidfile";
219              
220 0           $self->system($cmd);
221              
222 0           $self->log->error("start: son failed to start");
223 0           exit(1);
224 0           });
225              
226 0           return $pidfile;
227             }
228              
229             sub stop {
230 0     0 0   my $self = shift;
231              
232 0 0         if (! $self->status) {
233 0           return $self->info_process_is_not_running;
234             }
235              
236 0           my $pidfile = $self->pidfile;
237              
238 0           return $self->kill_from_pidfile($pidfile);
239             }
240              
241             sub status {
242 0     0 0   my $self = shift;
243              
244 0           my $pidfile = $self->pidfile;
245              
246 0 0         if ($self->is_running_from_pidfile($pidfile)) {
247 0           $self->verbose_process_is_running;
248 0           return 1;
249             }
250              
251 0           $self->verbose_process_is_not_running;
252 0           return 0;
253             }
254              
255             1;
256              
257             __END__