Server/Elasticsearch.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Server/Dns.pm ↑ Summary Server/Http.pm »
1 #
2 # $Id$
3 #
4 # server::elasticsearch Brik
5 #
6 package Metabrik::Server::Elasticsearch;
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 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 my $self = shift;
68  
69 my $datadir = $self->datadir;
70 my $version = $self->version;
71 my $pidfile = $datadir."/elasticsearch-$version.pid";
72 my $conf_file = $datadir."/elasticsearch-$version/config/elasticsearch.yml";
73 my $db_dir = $datadir."/db-$version";
74 my $log_dir = $datadir."/log-$version";
75  
76 return {
77 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 my $self = shift;
88  
89 my $binary = $self->binary;
90 if (! defined($binary)) {
91 my $datadir = $self->datadir;
92 my $version = $self->version;
93 $binary = $datadir.'/elasticsearch-'.$version.'/bin/elasticsearch';
94 }
95  
96 $self->brik_help_run_file_not_found('get_binary', $binary) or return;
97  
98 $self->log->verbose("get_binary: found binary [$binary]");
99  
100 return $binary;
101 }
102  
103 sub generate_conf {
104 my $self = shift;
105 my ($conf_file) = @_;
106  
107 $conf_file ||= $self->conf_file;
108  
109 my $version = $self->version;
110 my $cluster_name = $self->cluster_name;
111 my $node_name = $self->node_name;
112 my $listen = $self->listen;
113 my $port = $self->port;
114 my $db_dir = $self->db_dir;
115 my $log_dir = $self->log_dir;
116  
117 $self->log->debug("mkdir db_dir [$db_dir]");
118 $self->log->debug("mkdir log_dir [$log_dir]");
119  
120 my $sf = Metabrik::System::File->new_from_brik_init($self) or return;
121 $sf->mkdir($db_dir) or return;
122 $sf->mkdir($log_dir) or return;
123  
124 (my $path_conf = $conf_file) =~ s{/elasticsearch.yml$}{};
125  
126 my $conf =<<EOF
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 my $ft = Metabrik::File::Text->new_from_brik_init($self) or return;
145 $ft->append(0);
146 $ft->overwrite(1);
147  
148 $ft->write($conf, $conf_file) or return;
149  
150 return $conf_file;
151 }
152  
153 sub install {
154 my $self = shift;
155  
156 my $datadir = $self->datadir;
157 my $version = $self->version;
158  
159 my $url = 'https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.2.tar.gz';
160 if ($version eq '2.4.1') {
161 $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 $url = 'https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.0.0.tar.gz';
167 }
168  
169 my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return;
170 $cw->mirror($url, "$datadir/es.tar.gz") or return;
171  
172 my $cwd = defined($self->shell) && $self->shell->pwd || '/tmp';
173  
174 if (defined($self->shell)) {
175 $self->shell->run_cd($datadir) or return;
176 }
177 else {
178 chdir($datadir) or return $self->log->error("install: chdir: $!");
179 }
180  
181 my $cmd = "tar zxvf es.tar.gz";
182 my $r = $self->execute($cmd) or return;
183  
184 if (defined($self->shell)) {
185 $self->shell->run_cd($cwd) or return;
186 }
187 else {
188 chdir($cwd) or return $self->log->error("install: chdir: $!");
189 }
190  
191 return 1;
192 }
193  
194 sub start {
195 my $self = shift;
196  
197 if ($self->status) {
198 return $self->info_process_is_running;
199 }
200  
201 my $conf_file = $self->conf_file;
202 $self->brik_help_run_file_not_found('start', $conf_file) or return;
203  
204 my $no_output = $self->no_output;
205  
206 my $binary = $self->get_binary or return;
207  
208 $self->close_output_on_start($no_output);
209  
210 my $datadir = $self->datadir;
211 my $pidfile = $self->pidfile;
212  
213 $self->use_pidfile(0);
214  
215 $self->SUPER::start(sub {
216 $self->log->verbose("Within daemon");
217  
218 my $cmd = "$binary -p $pidfile";
219  
220 $self->system($cmd);
221  
222 $self->log->error("start: son failed to start");
223 exit(1);
224 });
225  
226 return $pidfile;
227 }
228  
229 sub stop {
230 my $self = shift;
231  
232 if (! $self->status) {
233 return $self->info_process_is_not_running;
234 }
235  
236 my $pidfile = $self->pidfile;
237  
238 return $self->kill_from_pidfile($pidfile);
239 }
240  
241 sub status {
242 my $self = shift;
243  
244 my $pidfile = $self->pidfile;
245  
246 if ($self->is_running_from_pidfile($pidfile)) {
247 $self->verbose_process_is_running;
248 return 1;
249 }
250  
251 $self->verbose_process_is_not_running;
252 return 0;
253 }
254  
255 1;
256  
257 __END__
« Server/Dns.pm ↑ Summary Server/Http.pm »