line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Ubic::Service::MongoDB; |
2
|
|
|
|
|
|
|
BEGIN { |
3
|
1
|
|
|
1
|
|
733
|
$Ubic::Service::MongoDB::VERSION = '0.01'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
8
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
7
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# ABSTRACT: running MongoDB as Ubic service |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
818
|
use parent qw(Ubic::Service::Common); |
|
1
|
|
|
|
|
13980
|
|
|
1
|
|
|
|
|
9
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
use File::Copy qw(move); |
15
|
|
|
|
|
|
|
use File::Spec::Functions qw(catfile); |
16
|
|
|
|
|
|
|
use MongoDB; |
17
|
|
|
|
|
|
|
use Params::Validate qw(:all); |
18
|
|
|
|
|
|
|
use Ubic::Daemon qw(:all); |
19
|
|
|
|
|
|
|
use Ubic::Result qw(:all); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub new { |
24
|
|
|
|
|
|
|
my $class = shift; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
my $opt_str = { type => SCALAR, optional => 1 }; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
my $params = validate(@_, { |
29
|
|
|
|
|
|
|
config => { type => HASHREF }, |
30
|
|
|
|
|
|
|
daemon => { type => SCALAR, |
31
|
|
|
|
|
|
|
regex => qr/^mongo(d|s)$/, |
32
|
|
|
|
|
|
|
default => 'mongod' }, |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
status => { type => CODEREF, optional => 1 }, |
35
|
|
|
|
|
|
|
user => $opt_str, |
36
|
|
|
|
|
|
|
ubic_log => $opt_str, |
37
|
|
|
|
|
|
|
stdout => $opt_str, |
38
|
|
|
|
|
|
|
stderr => $opt_str, |
39
|
|
|
|
|
|
|
pidfile => $opt_str, |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
gen_cfg => $opt_str, |
42
|
|
|
|
|
|
|
}); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
my $self = $params; |
45
|
|
|
|
|
|
|
bless $self, $class; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
$self->{port} = $self->{config}->{port} || 27017; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
if (!$params->{pidfile}) { |
50
|
|
|
|
|
|
|
$params->{pidfile} = "/tmp/$self->{daemon}." . $self->{port} . '.pid'; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
if (!$params->{gen_cfg}) { |
53
|
|
|
|
|
|
|
$params->{gen_cfg} = "/tmp/$self->{daemon}." . $self->{port} . '.conf'; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
return bless $params => $class; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub bin { |
61
|
|
|
|
|
|
|
my $self = shift; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
my @cmd = ($self->{daemon}, '--config', $self->{gen_cfg}); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
return \@cmd; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub create_cfg_file { |
69
|
|
|
|
|
|
|
my $self = shift; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
my $fname = $self->{gen_cfg}; |
72
|
|
|
|
|
|
|
my $tmp_fname = $fname . ".tmp"; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
open(my $tmp_fh, '>', $tmp_fname) or die "Can't open file [$tmp_fname]: $!"; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
foreach my $k (keys %{$self->{config}}) { |
77
|
|
|
|
|
|
|
my $v = $self->{config}->{$k}; |
78
|
|
|
|
|
|
|
print $tmp_fh "$k=$v\n"; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
close($tmp_fh) or die "Can't close file [$tmp_fname]: $!"; |
82
|
|
|
|
|
|
|
move($tmp_fname, $fname) or die "Can't mova file [${tmp_fname}] to [$fname]: $!"; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub pidfile { |
86
|
|
|
|
|
|
|
my $self = shift; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
return $self->{pidfile}; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub user { |
92
|
|
|
|
|
|
|
my $self = shift; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
return $self->{user} if defined $self->{user}; |
95
|
|
|
|
|
|
|
return $self->SUPER::user; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub timeout_options { |
99
|
|
|
|
|
|
|
return { |
100
|
|
|
|
|
|
|
start => { trials => 15, step => 0.1 }, |
101
|
|
|
|
|
|
|
stop => { trials => 15, step => 0.1 } |
102
|
|
|
|
|
|
|
}; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub start_impl { |
106
|
|
|
|
|
|
|
my $self = shift; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
$self->create_cfg_file; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
my $daemon_opts = { |
111
|
|
|
|
|
|
|
bin => $self->bin, |
112
|
|
|
|
|
|
|
term_timeout => 5 |
113
|
|
|
|
|
|
|
}; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
for (qw/ubic_log stdout stderr pidfile/) { |
116
|
|
|
|
|
|
|
$daemon_opts->{$_} = $self->{$_} if defined $self->{$_}; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
start_daemon($daemon_opts); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub stop_impl { |
122
|
|
|
|
|
|
|
my $self = shift; |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
return stop_daemon($self->pidfile, { timeout => 7 }); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub status_impl { |
128
|
|
|
|
|
|
|
my $self = shift; |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
my $running = check_daemon($self->pidfile); |
131
|
|
|
|
|
|
|
return result('not running') unless ($running); |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
my $status; |
134
|
|
|
|
|
|
|
eval { |
135
|
|
|
|
|
|
|
my $conn = MongoDB::Connection->new( |
136
|
|
|
|
|
|
|
host => "mongodb://localhost:$self->{port}", |
137
|
|
|
|
|
|
|
timeout => 2, |
138
|
|
|
|
|
|
|
query_timeout => 2, |
139
|
|
|
|
|
|
|
); |
140
|
|
|
|
|
|
|
my $db = $conn->admin; |
141
|
|
|
|
|
|
|
$status = $db->run_command({ serverStatus => 1 }); |
142
|
|
|
|
|
|
|
}; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
if ($@) { |
145
|
|
|
|
|
|
|
return result('broken', $@); |
146
|
|
|
|
|
|
|
} elsif (!$status || !$status->{ok} || $status->{ok} != 1) { |
147
|
|
|
|
|
|
|
return result('broken'); |
148
|
|
|
|
|
|
|
} else { |
149
|
|
|
|
|
|
|
return result('running'); |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
1; |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
__END__ |
157
|
|
|
|
|
|
|
=pod |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head1 NAME |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Ubic::Service::MongoDB - running MongoDB as Ubic service |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head1 VERSION |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
version 0.01 |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head1 SYNOPSIS |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
# in your ubic service (/etc/ubic/service/mymongo, for example) |
170
|
|
|
|
|
|
|
use Ubic::Service::MongoDB; |
171
|
|
|
|
|
|
|
return Ubic::Service::MongoDB->new({ |
172
|
|
|
|
|
|
|
config => { |
173
|
|
|
|
|
|
|
dbpath => '/var/lib/mongodb', |
174
|
|
|
|
|
|
|
logpath => "/var/log/mongodb/mongodb.log", |
175
|
|
|
|
|
|
|
logappend => "true", |
176
|
|
|
|
|
|
|
}, |
177
|
|
|
|
|
|
|
daemon => 'mongod', |
178
|
|
|
|
|
|
|
user => 'mongodb', |
179
|
|
|
|
|
|
|
ubic_log => '/var/log/mongodb/ubic.log', |
180
|
|
|
|
|
|
|
stdout => '/var/log/mongodb/stdout.log', |
181
|
|
|
|
|
|
|
stderr => '/var/log/mongodb/stderr.log', |
182
|
|
|
|
|
|
|
}); |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 DESCRIPTION |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
This is a L<Ubic> service for MongoDB. You can start/stop C<mongod> and C<mongos> using this module. |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head1 METHODS |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=over |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=item C<new($params)> |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
Creates new MongoDB service. C<$params> is a hashref with the following parameters: |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=over |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=item I<config> |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
Hashref with keys and values for MongoDB .conf file. This conf file regenerates every time at start. |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=item I<daemon> (optional) |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
What you want to start: C<mongod> or C<mongos>. Default is C<mongod>. |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=item I<user> (optional) |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
User name that will be used as real and effective user identifier during exec of MongoDB. |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=item I<status> (optional) |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
Coderef for checking MongoDB status. Takes current instance of C<Ubic::Service::MongoDB> as a first param. |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
Default implemetation uses C<serverStatus()> MongoDB command. |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=item I<ubic_log> (optional) |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
Path to ubic log. |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=item I<stdout> (optional) |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
Path to stdout log. |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=item I<stderr> (optional) |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
Path to stderr log. |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=item I<pidfile> (optional) |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
Pidfile for C<Ubic::Daemon> module. |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
If not specified it is a /tmp/mongo(d|s).<port>.pid. |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=item I<gen_cfg> (optional) |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
Generated MongoDB config file name. |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
If not specified it is a /tmp/mongo(d|s).<port>.conf. |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=back |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=back |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=head1 SEE ALSO |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
L<http://www.mongodb.org/display/DOCS/File+Based+Configuration> |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
L<Ubic> |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=head1 AUTHOR |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
Yury Zavarin <yury.zavarin@gmail.com> |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
This software is copyright (c) 2011 by Yury Zavarin. |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
259
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=cut |
262
|
|
|
|
|
|
|
|