Merge lp:~percona-toolkit-dev/percona-toolkit/remove-pt-agent into lp:~percona-toolkit-dev/percona-toolkit/release-2.2.8
- remove-pt-agent
- Merge into release-2.2.8
Proposed by
Daniel Nichter
Status: | Merged |
---|---|
Approved by: | Daniel Nichter |
Approved revision: | 599 |
Merged at revision: | 600 |
Proposed branch: | lp:~percona-toolkit-dev/percona-toolkit/remove-pt-agent |
Merge into: | lp:~percona-toolkit-dev/percona-toolkit/release-2.2.8 |
Diff against target: |
14753 lines (+2/-14551) 37 files modified
Changelog (+2/-0) bin/pt-agent (+0/-9802) lib/Percona/Agent/Logger.pm (+0/-341) lib/Percona/Test/Mock/AgentLogger.pm (+0/-129) lib/Percona/Test/Mock/UserAgent.pm (+0/-71) lib/Percona/WebAPI/Client.pm (+0/-321) lib/Percona/WebAPI/Exception/Request.pm (+0/-69) lib/Percona/WebAPI/Exception/Resource.pm (+0/-66) lib/Percona/WebAPI/Representation.pm (+0/-86) lib/Percona/WebAPI/Resource/Agent.pm (+0/-77) lib/Percona/WebAPI/Resource/Config.pm (+0/-55) lib/Percona/WebAPI/Resource/LogEntry.pm (+0/-66) lib/Percona/WebAPI/Resource/Service.pm (+0/-94) lib/Percona/WebAPI/Resource/Task.pm (+0/-62) t/lib/Percona/WebAPI/Client.t (+0/-236) t/lib/Percona/WebAPI/Representation.t (+0/-51) t/pt-agent/basics.t (+0/-101) t/pt-agent/get_services.t (+0/-423) t/pt-agent/init_agent.t (+0/-333) t/pt-agent/make_new_crontab.t (+0/-151) t/pt-agent/replace_special_vars.t (+0/-73) t/pt-agent/run_agent.t (+0/-527) t/pt-agent/run_service.t (+0/-503) t/pt-agent/samples/crontab001.out (+0/-2) t/pt-agent/samples/crontab002.in (+0/-1) t/pt-agent/samples/crontab002.out (+0/-3) t/pt-agent/samples/crontab003.in (+0/-3) t/pt-agent/samples/crontab003.out (+0/-3) t/pt-agent/samples/crontab004.in (+0/-2) t/pt-agent/samples/crontab004.out (+0/-2) t/pt-agent/samples/query-history/data001.json (+0/-152) t/pt-agent/samples/query-history/data001.send (+0/-166) t/pt-agent/samples/service001 (+0/-19) t/pt-agent/samples/write_services001 (+0/-19) t/pt-agent/schedule_services.t (+0/-200) t/pt-agent/send_data.t (+0/-234) t/pt-agent/write_services.t (+0/-108) |
To merge this branch: | bzr merge lp:~percona-toolkit-dev/percona-toolkit/remove-pt-agent |
Related bugs: | |
Related blueprints: |
Remove pt-agent
(Medium)
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Daniel Nichter | Approve | ||
Review via email: mp+217325@code.launchpad.net |
Commit message
Description of the change
To post a comment you must log in.
Revision history for this message
Daniel Nichter (daniel-nichter) : | # |
review:
Approve
- 600. By Daniel Nichter
-
Remove pt-agent.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'Changelog' | |||
2 | --- Changelog 2014-02-20 08:10:16 +0000 | |||
3 | +++ Changelog 2014-04-26 04:29:24 +0000 | |||
4 | @@ -1,5 +1,7 @@ | |||
5 | 1 | Changelog for Percona Toolkit | 1 | Changelog for Percona Toolkit |
6 | 2 | 2 | ||
7 | 3 | * Removed pt-agent | ||
8 | 4 | |||
9 | 3 | v2.2.7 released 2014-02-20 | 5 | v2.2.7 released 2014-02-20 |
10 | 4 | 6 | ||
11 | 5 | * Fixed bug 1279502: --version-check behaves like spyware | 7 | * Fixed bug 1279502: --version-check behaves like spyware |
12 | 6 | 8 | ||
13 | === removed file 'bin/pt-agent' | |||
14 | --- bin/pt-agent 2014-02-20 08:10:16 +0000 | |||
15 | +++ bin/pt-agent 1970-01-01 00:00:00 +0000 | |||
16 | @@ -1,9802 +0,0 @@ | |||
17 | 1 | #!/usr/bin/env perl | ||
18 | 2 | |||
19 | 3 | # This program is part of Percona Toolkit: http://www.percona.com/software/ | ||
20 | 4 | # See "COPYRIGHT, LICENSE, AND WARRANTY" at the end of this file for legal | ||
21 | 5 | # notices and disclaimers. | ||
22 | 6 | |||
23 | 7 | use strict; | ||
24 | 8 | use warnings FATAL => 'all'; | ||
25 | 9 | |||
26 | 10 | # This tool is "fat-packed": most of its dependent modules are embedded | ||
27 | 11 | # in this file. Setting %INC to this file for each module makes Perl aware | ||
28 | 12 | # of this so it will not try to load the module from @INC. See the tool's | ||
29 | 13 | # documentation for a full list of dependencies. | ||
30 | 14 | BEGIN { | ||
31 | 15 | $INC{$_} = __FILE__ for map { (my $pkg = "$_.pm") =~ s!::!/!g; $pkg } (qw( | ||
32 | 16 | Percona::Toolkit | ||
33 | 17 | Lmo::Utils | ||
34 | 18 | Lmo::Meta | ||
35 | 19 | Lmo::Object | ||
36 | 20 | Lmo::Types | ||
37 | 21 | Lmo | ||
38 | 22 | Percona::WebAPI::Representation | ||
39 | 23 | Percona::WebAPI::Client | ||
40 | 24 | Percona::WebAPI::Exception::Request | ||
41 | 25 | Percona::WebAPI::Exception::Resource | ||
42 | 26 | Percona::WebAPI::Resource::Agent | ||
43 | 27 | Percona::WebAPI::Resource::Config | ||
44 | 28 | Percona::WebAPI::Resource::Service | ||
45 | 29 | Percona::WebAPI::Resource::Task | ||
46 | 30 | Percona::WebAPI::Resource::LogEntry | ||
47 | 31 | VersionCheck | ||
48 | 32 | DSNParser | ||
49 | 33 | OptionParser | ||
50 | 34 | Cxn | ||
51 | 35 | Quoter | ||
52 | 36 | VersionParser | ||
53 | 37 | Daemon | ||
54 | 38 | Transformers | ||
55 | 39 | Safeguards | ||
56 | 40 | Percona::Agent::Logger | ||
57 | 41 | )); | ||
58 | 42 | } | ||
59 | 43 | |||
60 | 44 | # ########################################################################### | ||
61 | 45 | # Percona::Toolkit package | ||
62 | 46 | # This package is a copy without comments from the original. The original | ||
63 | 47 | # with comments and its test file can be found in the Bazaar repository at, | ||
64 | 48 | # lib/Percona/Toolkit.pm | ||
65 | 49 | # t/lib/Percona/Toolkit.t | ||
66 | 50 | # See https://launchpad.net/percona-toolkit for more information. | ||
67 | 51 | # ########################################################################### | ||
68 | 52 | { | ||
69 | 53 | package Percona::Toolkit; | ||
70 | 54 | |||
71 | 55 | our $VERSION = '2.2.7'; | ||
72 | 56 | |||
73 | 57 | use strict; | ||
74 | 58 | use warnings FATAL => 'all'; | ||
75 | 59 | use English qw(-no_match_vars); | ||
76 | 60 | use constant PTDEBUG => $ENV{PTDEBUG} || 0; | ||
77 | 61 | |||
78 | 62 | use Carp qw(carp cluck); | ||
79 | 63 | use Data::Dumper qw(); | ||
80 | 64 | |||
81 | 65 | require Exporter; | ||
82 | 66 | our @ISA = qw(Exporter); | ||
83 | 67 | our @EXPORT_OK = qw( | ||
84 | 68 | have_required_args | ||
85 | 69 | Dumper | ||
86 | 70 | _d | ||
87 | 71 | ); | ||
88 | 72 | |||
89 | 73 | sub have_required_args { | ||
90 | 74 | my ($args, @required_args) = @_; | ||
91 | 75 | my $have_required_args = 1; | ||
92 | 76 | foreach my $arg ( @required_args ) { | ||
93 | 77 | if ( !defined $args->{$arg} ) { | ||
94 | 78 | $have_required_args = 0; | ||
95 | 79 | carp "Argument $arg is not defined"; | ||
96 | 80 | } | ||
97 | 81 | } | ||
98 | 82 | cluck unless $have_required_args; # print backtrace | ||
99 | 83 | return $have_required_args; | ||
100 | 84 | } | ||
101 | 85 | |||
102 | 86 | sub Dumper { | ||
103 | 87 | local $Data::Dumper::Indent = 1; | ||
104 | 88 | local $Data::Dumper::Sortkeys = 1; | ||
105 | 89 | local $Data::Dumper::Quotekeys = 0; | ||
106 | 90 | Data::Dumper::Dumper(@_); | ||
107 | 91 | } | ||
108 | 92 | |||
109 | 93 | sub _d { | ||
110 | 94 | my ($package, undef, $line) = caller 0; | ||
111 | 95 | @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } | ||
112 | 96 | map { defined $_ ? $_ : 'undef' } | ||
113 | 97 | @_; | ||
114 | 98 | print STDERR "# $package:$line $PID ", join(' ', @_), "\n"; | ||
115 | 99 | } | ||
116 | 100 | |||
117 | 101 | 1; | ||
118 | 102 | } | ||
119 | 103 | # ########################################################################### | ||
120 | 104 | # End Percona::Toolkit package | ||
121 | 105 | # ########################################################################### | ||
122 | 106 | |||
123 | 107 | # ########################################################################### | ||
124 | 108 | # Lmo::Utils package | ||
125 | 109 | # This package is a copy without comments from the original. The original | ||
126 | 110 | # with comments and its test file can be found in the Bazaar repository at, | ||
127 | 111 | # lib/Lmo/Utils.pm | ||
128 | 112 | # t/lib/Lmo/Utils.t | ||
129 | 113 | # See https://launchpad.net/percona-toolkit for more information. | ||
130 | 114 | # ########################################################################### | ||
131 | 115 | { | ||
132 | 116 | package Lmo::Utils; | ||
133 | 117 | |||
134 | 118 | use strict; | ||
135 | 119 | use warnings qw( FATAL all ); | ||
136 | 120 | require Exporter; | ||
137 | 121 | our (@ISA, @EXPORT, @EXPORT_OK); | ||
138 | 122 | |||
139 | 123 | BEGIN { | ||
140 | 124 | @ISA = qw(Exporter); | ||
141 | 125 | @EXPORT = @EXPORT_OK = qw( | ||
142 | 126 | _install_coderef | ||
143 | 127 | _unimport_coderefs | ||
144 | 128 | _glob_for | ||
145 | 129 | _stash_for | ||
146 | 130 | ); | ||
147 | 131 | } | ||
148 | 132 | |||
149 | 133 | { | ||
150 | 134 | no strict 'refs'; | ||
151 | 135 | sub _glob_for { | ||
152 | 136 | return \*{shift()} | ||
153 | 137 | } | ||
154 | 138 | |||
155 | 139 | sub _stash_for { | ||
156 | 140 | return \%{ shift() . "::" }; | ||
157 | 141 | } | ||
158 | 142 | } | ||
159 | 143 | |||
160 | 144 | sub _install_coderef { | ||
161 | 145 | my ($to, $code) = @_; | ||
162 | 146 | |||
163 | 147 | return *{ _glob_for $to } = $code; | ||
164 | 148 | } | ||
165 | 149 | |||
166 | 150 | sub _unimport_coderefs { | ||
167 | 151 | my ($target, @names) = @_; | ||
168 | 152 | return unless @names; | ||
169 | 153 | my $stash = _stash_for($target); | ||
170 | 154 | foreach my $name (@names) { | ||
171 | 155 | if ($stash->{$name} and defined(&{$stash->{$name}})) { | ||
172 | 156 | delete $stash->{$name}; | ||
173 | 157 | } | ||
174 | 158 | } | ||
175 | 159 | } | ||
176 | 160 | |||
177 | 161 | 1; | ||
178 | 162 | } | ||
179 | 163 | # ########################################################################### | ||
180 | 164 | # End Lmo::Utils package | ||
181 | 165 | # ########################################################################### | ||
182 | 166 | |||
183 | 167 | # ########################################################################### | ||
184 | 168 | # Lmo::Meta package | ||
185 | 169 | # This package is a copy without comments from the original. The original | ||
186 | 170 | # with comments and its test file can be found in the Bazaar repository at, | ||
187 | 171 | # lib/Lmo/Meta.pm | ||
188 | 172 | # t/lib/Lmo/Meta.t | ||
189 | 173 | # See https://launchpad.net/percona-toolkit for more information. | ||
190 | 174 | # ########################################################################### | ||
191 | 175 | { | ||
192 | 176 | package Lmo::Meta; | ||
193 | 177 | use strict; | ||
194 | 178 | use warnings qw( FATAL all ); | ||
195 | 179 | |||
196 | 180 | my %metadata_for; | ||
197 | 181 | |||
198 | 182 | sub new { | ||
199 | 183 | my $class = shift; | ||
200 | 184 | return bless { @_ }, $class | ||
201 | 185 | } | ||
202 | 186 | |||
203 | 187 | sub metadata_for { | ||
204 | 188 | my $self = shift; | ||
205 | 189 | my ($class) = @_; | ||
206 | 190 | |||
207 | 191 | return $metadata_for{$class} ||= {}; | ||
208 | 192 | } | ||
209 | 193 | |||
210 | 194 | sub class { shift->{class} } | ||
211 | 195 | |||
212 | 196 | sub attributes { | ||
213 | 197 | my $self = shift; | ||
214 | 198 | return keys %{$self->metadata_for($self->class)} | ||
215 | 199 | } | ||
216 | 200 | |||
217 | 201 | sub attributes_for_new { | ||
218 | 202 | my $self = shift; | ||
219 | 203 | my @attributes; | ||
220 | 204 | |||
221 | 205 | my $class_metadata = $self->metadata_for($self->class); | ||
222 | 206 | while ( my ($attr, $meta) = each %$class_metadata ) { | ||
223 | 207 | if ( exists $meta->{init_arg} ) { | ||
224 | 208 | push @attributes, $meta->{init_arg} | ||
225 | 209 | if defined $meta->{init_arg}; | ||
226 | 210 | } | ||
227 | 211 | else { | ||
228 | 212 | push @attributes, $attr; | ||
229 | 213 | } | ||
230 | 214 | } | ||
231 | 215 | return @attributes; | ||
232 | 216 | } | ||
233 | 217 | |||
234 | 218 | 1; | ||
235 | 219 | } | ||
236 | 220 | # ########################################################################### | ||
237 | 221 | # End Lmo::Meta package | ||
238 | 222 | # ########################################################################### | ||
239 | 223 | |||
240 | 224 | # ########################################################################### | ||
241 | 225 | # Lmo::Object package | ||
242 | 226 | # This package is a copy without comments from the original. The original | ||
243 | 227 | # with comments and its test file can be found in the Bazaar repository at, | ||
244 | 228 | # lib/Lmo/Object.pm | ||
245 | 229 | # t/lib/Lmo/Object.t | ||
246 | 230 | # See https://launchpad.net/percona-toolkit for more information. | ||
247 | 231 | # ########################################################################### | ||
248 | 232 | { | ||
249 | 233 | package Lmo::Object; | ||
250 | 234 | |||
251 | 235 | use strict; | ||
252 | 236 | use warnings qw( FATAL all ); | ||
253 | 237 | |||
254 | 238 | use Carp (); | ||
255 | 239 | use Scalar::Util qw(blessed); | ||
256 | 240 | |||
257 | 241 | use Lmo::Meta; | ||
258 | 242 | use Lmo::Utils qw(_glob_for); | ||
259 | 243 | |||
260 | 244 | sub new { | ||
261 | 245 | my $class = shift; | ||
262 | 246 | my $args = $class->BUILDARGS(@_); | ||
263 | 247 | |||
264 | 248 | my $class_metadata = Lmo::Meta->metadata_for($class); | ||
265 | 249 | |||
266 | 250 | my @args_to_delete; | ||
267 | 251 | while ( my ($attr, $meta) = each %$class_metadata ) { | ||
268 | 252 | next unless exists $meta->{init_arg}; | ||
269 | 253 | my $init_arg = $meta->{init_arg}; | ||
270 | 254 | |||
271 | 255 | if ( defined $init_arg ) { | ||
272 | 256 | $args->{$attr} = delete $args->{$init_arg}; | ||
273 | 257 | } | ||
274 | 258 | else { | ||
275 | 259 | push @args_to_delete, $attr; | ||
276 | 260 | } | ||
277 | 261 | } | ||
278 | 262 | |||
279 | 263 | delete $args->{$_} for @args_to_delete; | ||
280 | 264 | |||
281 | 265 | for my $attribute ( keys %$args ) { | ||
282 | 266 | if ( my $coerce = $class_metadata->{$attribute}{coerce} ) { | ||
283 | 267 | $args->{$attribute} = $coerce->($args->{$attribute}); | ||
284 | 268 | } | ||
285 | 269 | if ( my $isa_check = $class_metadata->{$attribute}{isa} ) { | ||
286 | 270 | my ($check_name, $check_sub) = @$isa_check; | ||
287 | 271 | $check_sub->($args->{$attribute}); | ||
288 | 272 | } | ||
289 | 273 | } | ||
290 | 274 | |||
291 | 275 | while ( my ($attribute, $meta) = each %$class_metadata ) { | ||
292 | 276 | next unless $meta->{required}; | ||
293 | 277 | Carp::confess("Attribute ($attribute) is required for $class") | ||
294 | 278 | if ! exists $args->{$attribute} | ||
295 | 279 | } | ||
296 | 280 | |||
297 | 281 | my $self = bless $args, $class; | ||
298 | 282 | |||
299 | 283 | my @build_subs; | ||
300 | 284 | my $linearized_isa = mro::get_linear_isa($class); | ||
301 | 285 | |||
302 | 286 | for my $isa_class ( @$linearized_isa ) { | ||
303 | 287 | unshift @build_subs, *{ _glob_for "${isa_class}::BUILD" }{CODE}; | ||
304 | 288 | } | ||
305 | 289 | my @args = %$args; | ||
306 | 290 | for my $sub (grep { defined($_) && exists &$_ } @build_subs) { | ||
307 | 291 | $sub->( $self, @args); | ||
308 | 292 | } | ||
309 | 293 | return $self; | ||
310 | 294 | } | ||
311 | 295 | |||
312 | 296 | sub BUILDARGS { | ||
313 | 297 | shift; # No need for the classname | ||
314 | 298 | if ( @_ == 1 && ref($_[0]) ) { | ||
315 | 299 | Carp::confess("Single parameters to new() must be a HASH ref, not $_[0]") | ||
316 | 300 | unless ref($_[0]) eq ref({}); | ||
317 | 301 | return {%{$_[0]}} # We want a new reference, always | ||
318 | 302 | } | ||
319 | 303 | else { | ||
320 | 304 | return { @_ }; | ||
321 | 305 | } | ||
322 | 306 | } | ||
323 | 307 | |||
324 | 308 | sub meta { | ||
325 | 309 | my $class = shift; | ||
326 | 310 | $class = Scalar::Util::blessed($class) || $class; | ||
327 | 311 | return Lmo::Meta->new(class => $class); | ||
328 | 312 | } | ||
329 | 313 | |||
330 | 314 | 1; | ||
331 | 315 | } | ||
332 | 316 | # ########################################################################### | ||
333 | 317 | # End Lmo::Object package | ||
334 | 318 | # ########################################################################### | ||
335 | 319 | |||
336 | 320 | # ########################################################################### | ||
337 | 321 | # Lmo::Types package | ||
338 | 322 | # This package is a copy without comments from the original. The original | ||
339 | 323 | # with comments and its test file can be found in the Bazaar repository at, | ||
340 | 324 | # lib/Lmo/Types.pm | ||
341 | 325 | # t/lib/Lmo/Types.t | ||
342 | 326 | # See https://launchpad.net/percona-toolkit for more information. | ||
343 | 327 | # ########################################################################### | ||
344 | 328 | { | ||
345 | 329 | package Lmo::Types; | ||
346 | 330 | |||
347 | 331 | use strict; | ||
348 | 332 | use warnings qw( FATAL all ); | ||
349 | 333 | |||
350 | 334 | use Carp (); | ||
351 | 335 | use Scalar::Util qw(looks_like_number blessed); | ||
352 | 336 | |||
353 | 337 | |||
354 | 338 | our %TYPES = ( | ||
355 | 339 | Bool => sub { !$_[0] || (defined $_[0] && looks_like_number($_[0]) && $_[0] == 1) }, | ||
356 | 340 | Num => sub { defined $_[0] && looks_like_number($_[0]) }, | ||
357 | 341 | Int => sub { defined $_[0] && looks_like_number($_[0]) && $_[0] == int($_[0]) }, | ||
358 | 342 | Str => sub { defined $_[0] }, | ||
359 | 343 | Object => sub { defined $_[0] && blessed($_[0]) }, | ||
360 | 344 | FileHandle => sub { local $@; require IO::Handle; fileno($_[0]) && $_[0]->opened }, | ||
361 | 345 | |||
362 | 346 | map { | ||
363 | 347 | my $type = /R/ ? $_ : uc $_; | ||
364 | 348 | $_ . "Ref" => sub { ref $_[0] eq $type } | ||
365 | 349 | } qw(Array Code Hash Regexp Glob Scalar) | ||
366 | 350 | ); | ||
367 | 351 | |||
368 | 352 | sub check_type_constaints { | ||
369 | 353 | my ($attribute, $type_check, $check_name, $val) = @_; | ||
370 | 354 | ( ref($type_check) eq 'CODE' | ||
371 | 355 | ? $type_check->($val) | ||
372 | 356 | : (ref $val eq $type_check | ||
373 | 357 | || ($val && $val eq $type_check) | ||
374 | 358 | || (exists $TYPES{$type_check} && $TYPES{$type_check}->($val))) | ||
375 | 359 | ) | ||
376 | 360 | || Carp::confess( | ||
377 | 361 | qq<Attribute ($attribute) does not pass the type constraint because: > | ||
378 | 362 | . qq<Validation failed for '$check_name' with value > | ||
379 | 363 | . (defined $val ? Lmo::Dumper($val) : 'undef') ) | ||
380 | 364 | } | ||
381 | 365 | |||
382 | 366 | sub _nested_constraints { | ||
383 | 367 | my ($attribute, $aggregate_type, $type) = @_; | ||
384 | 368 | |||
385 | 369 | my $inner_types; | ||
386 | 370 | if ( $type =~ /\A(ArrayRef|Maybe)\[(.*)\]\z/ ) { | ||
387 | 371 | $inner_types = _nested_constraints($1, $2); | ||
388 | 372 | } | ||
389 | 373 | else { | ||
390 | 374 | $inner_types = $TYPES{$type}; | ||
391 | 375 | } | ||
392 | 376 | |||
393 | 377 | if ( $aggregate_type eq 'ArrayRef' ) { | ||
394 | 378 | return sub { | ||
395 | 379 | my ($val) = @_; | ||
396 | 380 | return unless ref($val) eq ref([]); | ||
397 | 381 | |||
398 | 382 | if ($inner_types) { | ||
399 | 383 | for my $value ( @{$val} ) { | ||
400 | 384 | return unless $inner_types->($value) | ||
401 | 385 | } | ||
402 | 386 | } | ||
403 | 387 | else { | ||
404 | 388 | for my $value ( @{$val} ) { | ||
405 | 389 | return unless $value && ($value eq $type | ||
406 | 390 | || (Scalar::Util::blessed($value) && $value->isa($type))); | ||
407 | 391 | } | ||
408 | 392 | } | ||
409 | 393 | return 1; | ||
410 | 394 | }; | ||
411 | 395 | } | ||
412 | 396 | elsif ( $aggregate_type eq 'Maybe' ) { | ||
413 | 397 | return sub { | ||
414 | 398 | my ($value) = @_; | ||
415 | 399 | return 1 if ! defined($value); | ||
416 | 400 | if ($inner_types) { | ||
417 | 401 | return unless $inner_types->($value) | ||
418 | 402 | } | ||
419 | 403 | else { | ||
420 | 404 | return unless $value eq $type | ||
421 | 405 | || (Scalar::Util::blessed($value) && $value->isa($type)); | ||
422 | 406 | } | ||
423 | 407 | return 1; | ||
424 | 408 | } | ||
425 | 409 | } | ||
426 | 410 | else { | ||
427 | 411 | Carp::confess("Nested aggregate types are only implemented for ArrayRefs and Maybe"); | ||
428 | 412 | } | ||
429 | 413 | } | ||
430 | 414 | |||
431 | 415 | 1; | ||
432 | 416 | } | ||
433 | 417 | # ########################################################################### | ||
434 | 418 | # End Lmo::Types package | ||
435 | 419 | # ########################################################################### | ||
436 | 420 | |||
437 | 421 | # ########################################################################### | ||
438 | 422 | # Lmo package | ||
439 | 423 | # This package is a copy without comments from the original. The original | ||
440 | 424 | # with comments and its test file can be found in the Bazaar repository at, | ||
441 | 425 | # lib/Lmo.pm | ||
442 | 426 | # t/lib/Lmo.t | ||
443 | 427 | # See https://launchpad.net/percona-toolkit for more information. | ||
444 | 428 | # ########################################################################### | ||
445 | 429 | { | ||
446 | 430 | BEGIN { | ||
447 | 431 | $INC{"Lmo.pm"} = __FILE__; | ||
448 | 432 | package Lmo; | ||
449 | 433 | our $VERSION = '0.30_Percona'; # Forked from 0.30 of Mo. | ||
450 | 434 | |||
451 | 435 | |||
452 | 436 | use strict; | ||
453 | 437 | use warnings qw( FATAL all ); | ||
454 | 438 | |||
455 | 439 | use Carp (); | ||
456 | 440 | use Scalar::Util qw(looks_like_number blessed); | ||
457 | 441 | |||
458 | 442 | use Lmo::Meta; | ||
459 | 443 | use Lmo::Object; | ||
460 | 444 | use Lmo::Types; | ||
461 | 445 | |||
462 | 446 | use Lmo::Utils; | ||
463 | 447 | |||
464 | 448 | my %export_for; | ||
465 | 449 | sub import { | ||
466 | 450 | warnings->import(qw(FATAL all)); | ||
467 | 451 | strict->import(); | ||
468 | 452 | |||
469 | 453 | my $caller = scalar caller(); # Caller's package | ||
470 | 454 | my %exports = ( | ||
471 | 455 | extends => \&extends, | ||
472 | 456 | has => \&has, | ||
473 | 457 | with => \&with, | ||
474 | 458 | override => \&override, | ||
475 | 459 | confess => \&Carp::confess, | ||
476 | 460 | ); | ||
477 | 461 | |||
478 | 462 | $export_for{$caller} = \%exports; | ||
479 | 463 | |||
480 | 464 | for my $keyword ( keys %exports ) { | ||
481 | 465 | _install_coderef "${caller}::$keyword" => $exports{$keyword}; | ||
482 | 466 | } | ||
483 | 467 | |||
484 | 468 | if ( !@{ *{ _glob_for "${caller}::ISA" }{ARRAY} || [] } ) { | ||
485 | 469 | @_ = "Lmo::Object"; | ||
486 | 470 | goto *{ _glob_for "${caller}::extends" }{CODE}; | ||
487 | 471 | } | ||
488 | 472 | } | ||
489 | 473 | |||
490 | 474 | sub extends { | ||
491 | 475 | my $caller = scalar caller(); | ||
492 | 476 | for my $class ( @_ ) { | ||
493 | 477 | _load_module($class); | ||
494 | 478 | } | ||
495 | 479 | _set_package_isa($caller, @_); | ||
496 | 480 | _set_inherited_metadata($caller); | ||
497 | 481 | } | ||
498 | 482 | |||
499 | 483 | sub _load_module { | ||
500 | 484 | my ($class) = @_; | ||
501 | 485 | |||
502 | 486 | (my $file = $class) =~ s{::|'}{/}g; | ||
503 | 487 | $file .= '.pm'; | ||
504 | 488 | { local $@; eval { require "$file" } } # or warn $@; | ||
505 | 489 | return; | ||
506 | 490 | } | ||
507 | 491 | |||
508 | 492 | sub with { | ||
509 | 493 | my $package = scalar caller(); | ||
510 | 494 | require Role::Tiny; | ||
511 | 495 | for my $role ( @_ ) { | ||
512 | 496 | _load_module($role); | ||
513 | 497 | _role_attribute_metadata($package, $role); | ||
514 | 498 | } | ||
515 | 499 | Role::Tiny->apply_roles_to_package($package, @_); | ||
516 | 500 | } | ||
517 | 501 | |||
518 | 502 | sub _role_attribute_metadata { | ||
519 | 503 | my ($package, $role) = @_; | ||
520 | 504 | |||
521 | 505 | my $package_meta = Lmo::Meta->metadata_for($package); | ||
522 | 506 | my $role_meta = Lmo::Meta->metadata_for($role); | ||
523 | 507 | |||
524 | 508 | %$package_meta = (%$role_meta, %$package_meta); | ||
525 | 509 | } | ||
526 | 510 | |||
527 | 511 | sub has { | ||
528 | 512 | my $names = shift; | ||
529 | 513 | my $caller = scalar caller(); | ||
530 | 514 | |||
531 | 515 | my $class_metadata = Lmo::Meta->metadata_for($caller); | ||
532 | 516 | |||
533 | 517 | for my $attribute ( ref $names ? @$names : $names ) { | ||
534 | 518 | my %args = @_; | ||
535 | 519 | my $method = ($args{is} || '') eq 'ro' | ||
536 | 520 | ? sub { | ||
537 | 521 | Carp::confess("Cannot assign a value to a read-only accessor at reader ${caller}::${attribute}") | ||
538 | 522 | if $#_; | ||
539 | 523 | return $_[0]{$attribute}; | ||
540 | 524 | } | ||
541 | 525 | : sub { | ||
542 | 526 | return $#_ | ||
543 | 527 | ? $_[0]{$attribute} = $_[1] | ||
544 | 528 | : $_[0]{$attribute}; | ||
545 | 529 | }; | ||
546 | 530 | |||
547 | 531 | $class_metadata->{$attribute} = (); | ||
548 | 532 | |||
549 | 533 | if ( my $type_check = $args{isa} ) { | ||
550 | 534 | my $check_name = $type_check; | ||
551 | 535 | |||
552 | 536 | if ( my ($aggregate_type, $inner_type) = $type_check =~ /\A(ArrayRef|Maybe)\[(.*)\]\z/ ) { | ||
553 | 537 | $type_check = Lmo::Types::_nested_constraints($attribute, $aggregate_type, $inner_type); | ||
554 | 538 | } | ||
555 | 539 | |||
556 | 540 | my $check_sub = sub { | ||
557 | 541 | my ($new_val) = @_; | ||
558 | 542 | Lmo::Types::check_type_constaints($attribute, $type_check, $check_name, $new_val); | ||
559 | 543 | }; | ||
560 | 544 | |||
561 | 545 | $class_metadata->{$attribute}{isa} = [$check_name, $check_sub]; | ||
562 | 546 | my $orig_method = $method; | ||
563 | 547 | $method = sub { | ||
564 | 548 | $check_sub->($_[1]) if $#_; | ||
565 | 549 | goto &$orig_method; | ||
566 | 550 | }; | ||
567 | 551 | } | ||
568 | 552 | |||
569 | 553 | if ( my $builder = $args{builder} ) { | ||
570 | 554 | my $original_method = $method; | ||
571 | 555 | $method = sub { | ||
572 | 556 | $#_ | ||
573 | 557 | ? goto &$original_method | ||
574 | 558 | : ! exists $_[0]{$attribute} | ||
575 | 559 | ? $_[0]{$attribute} = $_[0]->$builder | ||
576 | 560 | : goto &$original_method | ||
577 | 561 | }; | ||
578 | 562 | } | ||
579 | 563 | |||
580 | 564 | if ( my $code = $args{default} ) { | ||
581 | 565 | Carp::confess("${caller}::${attribute}'s default is $code, but should be a coderef") | ||
582 | 566 | unless ref($code) eq 'CODE'; | ||
583 | 567 | my $original_method = $method; | ||
584 | 568 | $method = sub { | ||
585 | 569 | $#_ | ||
586 | 570 | ? goto &$original_method | ||
587 | 571 | : ! exists $_[0]{$attribute} | ||
588 | 572 | ? $_[0]{$attribute} = $_[0]->$code | ||
589 | 573 | : goto &$original_method | ||
590 | 574 | }; | ||
591 | 575 | } | ||
592 | 576 | |||
593 | 577 | if ( my $role = $args{does} ) { | ||
594 | 578 | my $original_method = $method; | ||
595 | 579 | $method = sub { | ||
596 | 580 | if ( $#_ ) { | ||
597 | 581 | Carp::confess(qq<Attribute ($attribute) doesn't consume a '$role' role">) | ||
598 | 582 | unless Scalar::Util::blessed($_[1]) && eval { $_[1]->does($role) } | ||
599 | 583 | } | ||
600 | 584 | goto &$original_method | ||
601 | 585 | }; | ||
602 | 586 | } | ||
603 | 587 | |||
604 | 588 | if ( my $coercion = $args{coerce} ) { | ||
605 | 589 | $class_metadata->{$attribute}{coerce} = $coercion; | ||
606 | 590 | my $original_method = $method; | ||
607 | 591 | $method = sub { | ||
608 | 592 | if ( $#_ ) { | ||
609 | 593 | return $original_method->($_[0], $coercion->($_[1])) | ||
610 | 594 | } | ||
611 | 595 | goto &$original_method; | ||
612 | 596 | } | ||
613 | 597 | } | ||
614 | 598 | |||
615 | 599 | _install_coderef "${caller}::$attribute" => $method; | ||
616 | 600 | |||
617 | 601 | if ( $args{required} ) { | ||
618 | 602 | $class_metadata->{$attribute}{required} = 1; | ||
619 | 603 | } | ||
620 | 604 | |||
621 | 605 | if ($args{clearer}) { | ||
622 | 606 | _install_coderef "${caller}::$args{clearer}" | ||
623 | 607 | => sub { delete shift->{$attribute} } | ||
624 | 608 | } | ||
625 | 609 | |||
626 | 610 | if ($args{predicate}) { | ||
627 | 611 | _install_coderef "${caller}::$args{predicate}" | ||
628 | 612 | => sub { exists shift->{$attribute} } | ||
629 | 613 | } | ||
630 | 614 | |||
631 | 615 | if ($args{handles}) { | ||
632 | 616 | _has_handles($caller, $attribute, \%args); | ||
633 | 617 | } | ||
634 | 618 | |||
635 | 619 | if (exists $args{init_arg}) { | ||
636 | 620 | $class_metadata->{$attribute}{init_arg} = $args{init_arg}; | ||
637 | 621 | } | ||
638 | 622 | } | ||
639 | 623 | } | ||
640 | 624 | |||
641 | 625 | sub _has_handles { | ||
642 | 626 | my ($caller, $attribute, $args) = @_; | ||
643 | 627 | my $handles = $args->{handles}; | ||
644 | 628 | |||
645 | 629 | my $ref = ref $handles; | ||
646 | 630 | my $kv; | ||
647 | 631 | if ( $ref eq ref [] ) { | ||
648 | 632 | $kv = { map { $_,$_ } @{$handles} }; | ||
649 | 633 | } | ||
650 | 634 | elsif ( $ref eq ref {} ) { | ||
651 | 635 | $kv = $handles; | ||
652 | 636 | } | ||
653 | 637 | elsif ( $ref eq ref qr// ) { | ||
654 | 638 | Carp::confess("Cannot delegate methods based on a Regexp without a type constraint (isa)") | ||
655 | 639 | unless $args->{isa}; | ||
656 | 640 | my $target_class = $args->{isa}; | ||
657 | 641 | $kv = { | ||
658 | 642 | map { $_, $_ } | ||
659 | 643 | grep { $_ =~ $handles } | ||
660 | 644 | grep { !exists $Lmo::Object::{$_} && $target_class->can($_) } | ||
661 | 645 | grep { !$export_for{$target_class}->{$_} } | ||
662 | 646 | keys %{ _stash_for $target_class } | ||
663 | 647 | }; | ||
664 | 648 | } | ||
665 | 649 | else { | ||
666 | 650 | Carp::confess("handles for $ref not yet implemented"); | ||
667 | 651 | } | ||
668 | 652 | |||
669 | 653 | while ( my ($method, $target) = each %{$kv} ) { | ||
670 | 654 | my $name = _glob_for "${caller}::$method"; | ||
671 | 655 | Carp::confess("You cannot overwrite a locally defined method ($method) with a delegation") | ||
672 | 656 | if defined &$name; | ||
673 | 657 | |||
674 | 658 | my ($target, @curried_args) = ref($target) ? @$target : $target; | ||
675 | 659 | *$name = sub { | ||
676 | 660 | my $self = shift; | ||
677 | 661 | my $delegate_to = $self->$attribute(); | ||
678 | 662 | my $error = "Cannot delegate $method to $target because the value of $attribute"; | ||
679 | 663 | Carp::confess("$error is not defined") unless $delegate_to; | ||
680 | 664 | Carp::confess("$error is not an object (got '$delegate_to')") | ||
681 | 665 | unless Scalar::Util::blessed($delegate_to) || (!ref($delegate_to) && $delegate_to->can($target)); | ||
682 | 666 | return $delegate_to->$target(@curried_args, @_); | ||
683 | 667 | } | ||
684 | 668 | } | ||
685 | 669 | } | ||
686 | 670 | |||
687 | 671 | sub _set_package_isa { | ||
688 | 672 | my ($package, @new_isa) = @_; | ||
689 | 673 | my $package_isa = \*{ _glob_for "${package}::ISA" }; | ||
690 | 674 | @{*$package_isa} = @new_isa; | ||
691 | 675 | } | ||
692 | 676 | |||
693 | 677 | sub _set_inherited_metadata { | ||
694 | 678 | my $class = shift; | ||
695 | 679 | my $class_metadata = Lmo::Meta->metadata_for($class); | ||
696 | 680 | my $linearized_isa = mro::get_linear_isa($class); | ||
697 | 681 | my %new_metadata; | ||
698 | 682 | |||
699 | 683 | for my $isa_class (reverse @$linearized_isa) { | ||
700 | 684 | my $isa_metadata = Lmo::Meta->metadata_for($isa_class); | ||
701 | 685 | %new_metadata = ( | ||
702 | 686 | %new_metadata, | ||
703 | 687 | %$isa_metadata, | ||
704 | 688 | ); | ||
705 | 689 | } | ||
706 | 690 | %$class_metadata = %new_metadata; | ||
707 | 691 | } | ||
708 | 692 | |||
709 | 693 | sub unimport { | ||
710 | 694 | my $caller = scalar caller(); | ||
711 | 695 | my $target = caller; | ||
712 | 696 | _unimport_coderefs($target, keys %{$export_for{$caller}}); | ||
713 | 697 | } | ||
714 | 698 | |||
715 | 699 | sub Dumper { | ||
716 | 700 | require Data::Dumper; | ||
717 | 701 | local $Data::Dumper::Indent = 0; | ||
718 | 702 | local $Data::Dumper::Sortkeys = 0; | ||
719 | 703 | local $Data::Dumper::Quotekeys = 0; | ||
720 | 704 | local $Data::Dumper::Terse = 1; | ||
721 | 705 | |||
722 | 706 | Data::Dumper::Dumper(@_) | ||
723 | 707 | } | ||
724 | 708 | |||
725 | 709 | BEGIN { | ||
726 | 710 | if ($] >= 5.010) { | ||
727 | 711 | { local $@; require mro; } | ||
728 | 712 | } | ||
729 | 713 | else { | ||
730 | 714 | local $@; | ||
731 | 715 | eval { | ||
732 | 716 | require MRO::Compat; | ||
733 | 717 | } or do { | ||
734 | 718 | *mro::get_linear_isa = *mro::get_linear_isa_dfs = sub { | ||
735 | 719 | no strict 'refs'; | ||
736 | 720 | |||
737 | 721 | my $classname = shift; | ||
738 | 722 | |||
739 | 723 | my @lin = ($classname); | ||
740 | 724 | my %stored; | ||
741 | 725 | foreach my $parent (@{"$classname\::ISA"}) { | ||
742 | 726 | my $plin = mro::get_linear_isa_dfs($parent); | ||
743 | 727 | foreach (@$plin) { | ||
744 | 728 | next if exists $stored{$_}; | ||
745 | 729 | push(@lin, $_); | ||
746 | 730 | $stored{$_} = 1; | ||
747 | 731 | } | ||
748 | 732 | } | ||
749 | 733 | return \@lin; | ||
750 | 734 | }; | ||
751 | 735 | } | ||
752 | 736 | } | ||
753 | 737 | } | ||
754 | 738 | |||
755 | 739 | sub override { | ||
756 | 740 | my ($methods, $code) = @_; | ||
757 | 741 | my $caller = scalar caller; | ||
758 | 742 | |||
759 | 743 | for my $method ( ref($methods) ? @$methods : $methods ) { | ||
760 | 744 | my $full_method = "${caller}::${method}"; | ||
761 | 745 | *{_glob_for $full_method} = $code; | ||
762 | 746 | } | ||
763 | 747 | } | ||
764 | 748 | |||
765 | 749 | } | ||
766 | 750 | 1; | ||
767 | 751 | } | ||
768 | 752 | # ########################################################################### | ||
769 | 753 | # End Lmo package | ||
770 | 754 | # ########################################################################### | ||
771 | 755 | |||
772 | 756 | # ########################################################################### | ||
773 | 757 | # Percona::WebAPI::Representation package | ||
774 | 758 | # This package is a copy without comments from the original. The original | ||
775 | 759 | # with comments and its test file can be found in the Bazaar repository at, | ||
776 | 760 | # lib/Percona/WebAPI/Representation.pm | ||
777 | 761 | # t/lib/Percona/WebAPI/Representation.t | ||
778 | 762 | # See https://launchpad.net/percona-toolkit for more information. | ||
779 | 763 | # ########################################################################### | ||
780 | 764 | { | ||
781 | 765 | package Percona::WebAPI::Representation; | ||
782 | 766 | |||
783 | 767 | eval { | ||
784 | 768 | require JSON; | ||
785 | 769 | }; | ||
786 | 770 | |||
787 | 771 | require Exporter; | ||
788 | 772 | our @ISA = qw(Exporter); | ||
789 | 773 | our @EXPORT_OK = qw( | ||
790 | 774 | as_hashref | ||
791 | 775 | as_json | ||
792 | 776 | as_config | ||
793 | 777 | ); | ||
794 | 778 | |||
795 | 779 | sub as_hashref { | ||
796 | 780 | my ($resource, %args) = @_; | ||
797 | 781 | |||
798 | 782 | my $as_hashref = { %$resource }; | ||
799 | 783 | |||
800 | 784 | if ( !defined $args{with_links} || !$args{with_links} ) { | ||
801 | 785 | delete $as_hashref->{links}; | ||
802 | 786 | } | ||
803 | 787 | |||
804 | 788 | return $as_hashref; | ||
805 | 789 | } | ||
806 | 790 | |||
807 | 791 | sub as_json { | ||
808 | 792 | my ($resource, %args) = @_; | ||
809 | 793 | |||
810 | 794 | my $json = $args{json} || JSON->new; | ||
811 | 795 | $json->allow_blessed([]); | ||
812 | 796 | $json->convert_blessed([]); | ||
813 | 797 | |||
814 | 798 | my $text = $json->encode( | ||
815 | 799 | ref $resource eq 'ARRAY' ? $resource : as_hashref($resource, %args) | ||
816 | 800 | ); | ||
817 | 801 | if ( $args{json} && $text ) { # for testing | ||
818 | 802 | chomp($text); | ||
819 | 803 | $text .= "\n"; | ||
820 | 804 | } | ||
821 | 805 | return $text; | ||
822 | 806 | } | ||
823 | 807 | |||
824 | 808 | sub as_config { | ||
825 | 809 | my $resource = shift; | ||
826 | 810 | if ( !$resource->isa('Percona::WebAPI::Resource::Config') ) { | ||
827 | 811 | die "Only Config resources can be represented as config.\n"; | ||
828 | 812 | } | ||
829 | 813 | my $as_hashref = as_hashref($resource); | ||
830 | 814 | my $options = $as_hashref->{options}; | ||
831 | 815 | my $config = join("\n", | ||
832 | 816 | map { defined $options->{$_} ? "$_=$options->{$_}" : "$_" } | ||
833 | 817 | sort keys %$options | ||
834 | 818 | ) . "\n"; | ||
835 | 819 | return $config; | ||
836 | 820 | } | ||
837 | 821 | |||
838 | 822 | 1; | ||
839 | 823 | } | ||
840 | 824 | # ########################################################################### | ||
841 | 825 | # End Percona::WebAPI::Representation package | ||
842 | 826 | # ########################################################################### | ||
843 | 827 | |||
844 | 828 | # ########################################################################### | ||
845 | 829 | # Percona::WebAPI::Client package | ||
846 | 830 | # This package is a copy without comments from the original. The original | ||
847 | 831 | # with comments and its test file can be found in the Bazaar repository at, | ||
848 | 832 | # lib/Percona/WebAPI/Client.pm | ||
849 | 833 | # t/lib/Percona/WebAPI/Client.t | ||
850 | 834 | # See https://launchpad.net/percona-toolkit for more information. | ||
851 | 835 | # ########################################################################### | ||
852 | 836 | { | ||
853 | 837 | package Percona::WebAPI::Client; | ||
854 | 838 | |||
855 | 839 | our $VERSION = '0.01'; | ||
856 | 840 | |||
857 | 841 | use strict; | ||
858 | 842 | use warnings FATAL => 'all'; | ||
859 | 843 | use English qw(-no_match_vars); | ||
860 | 844 | use constant PTDEBUG => $ENV{PTDEBUG} || 0; | ||
861 | 845 | |||
862 | 846 | eval { | ||
863 | 847 | require LWP; | ||
864 | 848 | require JSON; | ||
865 | 849 | }; | ||
866 | 850 | |||
867 | 851 | use Scalar::Util qw(blessed); | ||
868 | 852 | |||
869 | 853 | use Lmo; | ||
870 | 854 | use Percona::Toolkit; | ||
871 | 855 | use Percona::WebAPI::Representation; | ||
872 | 856 | use Percona::WebAPI::Exception::Request; | ||
873 | 857 | use Percona::WebAPI::Exception::Resource; | ||
874 | 858 | |||
875 | 859 | Percona::WebAPI::Representation->import(qw(as_json)); | ||
876 | 860 | Percona::Toolkit->import(qw(_d Dumper have_required_args)); | ||
877 | 861 | |||
878 | 862 | has 'api_key' => ( | ||
879 | 863 | is => 'ro', | ||
880 | 864 | isa => 'Str', | ||
881 | 865 | required => 1, | ||
882 | 866 | ); | ||
883 | 867 | |||
884 | 868 | has 'entry_link' => ( | ||
885 | 869 | is => 'rw', | ||
886 | 870 | isa => 'Str', | ||
887 | 871 | required => 0, | ||
888 | 872 | default => sub { return 'https://cloud-api.percona.com' }, | ||
889 | 873 | ); | ||
890 | 874 | |||
891 | 875 | has 'ua' => ( | ||
892 | 876 | is => 'rw', | ||
893 | 877 | isa => 'Object', | ||
894 | 878 | lazy => 1, | ||
895 | 879 | required => 0, | ||
896 | 880 | builder => '_build_ua', | ||
897 | 881 | ); | ||
898 | 882 | |||
899 | 883 | has 'response' => ( | ||
900 | 884 | is => 'rw', | ||
901 | 885 | isa => 'Object', | ||
902 | 886 | required => 0, | ||
903 | 887 | default => undef, | ||
904 | 888 | ); | ||
905 | 889 | |||
906 | 890 | sub _build_ua { | ||
907 | 891 | my $self = shift; | ||
908 | 892 | my $ua = LWP::UserAgent->new; | ||
909 | 893 | $ua->agent("Percona::WebAPI::Client/$Percona::WebAPI::Client::VERSION"); | ||
910 | 894 | $ua->default_header('Content-Type', 'application/json'); | ||
911 | 895 | $ua->default_header('X-Percona-API-Key', $self->api_key); | ||
912 | 896 | return $ua; | ||
913 | 897 | } | ||
914 | 898 | |||
915 | 899 | sub get { | ||
916 | 900 | my ($self, %args) = @_; | ||
917 | 901 | |||
918 | 902 | have_required_args(\%args, qw( | ||
919 | 903 | link | ||
920 | 904 | )) or die; | ||
921 | 905 | my ($link) = $args{link}; | ||
922 | 906 | |||
923 | 907 | eval { | ||
924 | 908 | $self->_request( | ||
925 | 909 | method => 'GET', | ||
926 | 910 | link => $link, | ||
927 | 911 | ); | ||
928 | 912 | }; | ||
929 | 913 | if ( my $e = $EVAL_ERROR ) { | ||
930 | 914 | if (blessed($e) && $e->isa('Percona::WebAPI::Exception::Request')) { | ||
931 | 915 | die $e; | ||
932 | 916 | } | ||
933 | 917 | else { | ||
934 | 918 | die "Unknown error: $e"; | ||
935 | 919 | } | ||
936 | 920 | } | ||
937 | 921 | |||
938 | 922 | my $resource = eval { | ||
939 | 923 | JSON::decode_json($self->response->content); | ||
940 | 924 | }; | ||
941 | 925 | if ( $EVAL_ERROR ) { | ||
942 | 926 | warn sprintf "Error decoding resource: %s: %s", | ||
943 | 927 | $self->response->content, | ||
944 | 928 | $EVAL_ERROR; | ||
945 | 929 | return; | ||
946 | 930 | } | ||
947 | 931 | |||
948 | 932 | my $resource_objects; | ||
949 | 933 | if ( my $type = $self->response->headers->{'x-percona-resource-type'} ) { | ||
950 | 934 | eval { | ||
951 | 935 | $type = "Percona::WebAPI::Resource::$type"; | ||
952 | 936 | if ( ref $resource eq 'ARRAY' ) { | ||
953 | 937 | PTDEBUG && _d('Got a list of', $type, 'resources'); | ||
954 | 938 | $resource_objects = []; | ||
955 | 939 | foreach my $attribs ( @$resource ) { | ||
956 | 940 | my $obj = $type->new(%$attribs); | ||
957 | 941 | push @$resource_objects, $obj; | ||
958 | 942 | } | ||
959 | 943 | } | ||
960 | 944 | else { | ||
961 | 945 | PTDEBUG && _d('Got a', $type, 'resource', Dumper($resource)); | ||
962 | 946 | $resource_objects = $type->new(%$resource); | ||
963 | 947 | } | ||
964 | 948 | }; | ||
965 | 949 | if ( my $e = $EVAL_ERROR ) { | ||
966 | 950 | die Percona::WebAPI::Exception::Resource->new( | ||
967 | 951 | type => $type, | ||
968 | 952 | link => $link, | ||
969 | 953 | data => (ref $resource eq 'ARRAY' ? $resource : [ $resource ]), | ||
970 | 954 | error => $e, | ||
971 | 955 | ); | ||
972 | 956 | } | ||
973 | 957 | } | ||
974 | 958 | elsif ( exists $resource->{links} ) { | ||
975 | 959 | $resource_objects = $resource->{links}; | ||
976 | 960 | } | ||
977 | 961 | elsif ( exists $resource->{pong} ) { | ||
978 | 962 | PTDEBUG && _d("Ping pong!"); | ||
979 | 963 | } | ||
980 | 964 | else { | ||
981 | 965 | warn "Did not get X-Percona-Resource-Type or links from $link\n"; | ||
982 | 966 | } | ||
983 | 967 | |||
984 | 968 | return $resource_objects; | ||
985 | 969 | } | ||
986 | 970 | |||
987 | 971 | sub post { | ||
988 | 972 | my $self = shift; | ||
989 | 973 | $self->_set( | ||
990 | 974 | @_, | ||
991 | 975 | method => 'POST', | ||
992 | 976 | ); | ||
993 | 977 | return $self->response->header('Location'); | ||
994 | 978 | } | ||
995 | 979 | |||
996 | 980 | sub put { | ||
997 | 981 | my $self = shift; | ||
998 | 982 | $self->_set( | ||
999 | 983 | @_, | ||
1000 | 984 | method => 'PUT', | ||
1001 | 985 | ); | ||
1002 | 986 | return $self->response->header('Location'); | ||
1003 | 987 | } | ||
1004 | 988 | |||
1005 | 989 | sub delete { | ||
1006 | 990 | my ($self, %args) = @_; | ||
1007 | 991 | have_required_args(\%args, qw( | ||
1008 | 992 | link | ||
1009 | 993 | )) or die; | ||
1010 | 994 | my ($link) = $args{link}; | ||
1011 | 995 | |||
1012 | 996 | eval { | ||
1013 | 997 | $self->_request( | ||
1014 | 998 | method => 'DELETE', | ||
1015 | 999 | link => $link, | ||
1016 | 1000 | headers => { 'Content-Length' => 0 }, | ||
1017 | 1001 | ); | ||
1018 | 1002 | }; | ||
1019 | 1003 | if ( my $e = $EVAL_ERROR ) { | ||
1020 | 1004 | if (blessed($e) && $e->isa('Percona::WebAPI::Exception::Request')) { | ||
1021 | 1005 | die $e; | ||
1022 | 1006 | } | ||
1023 | 1007 | else { | ||
1024 | 1008 | die "Unknown error: $e"; | ||
1025 | 1009 | } | ||
1026 | 1010 | } | ||
1027 | 1011 | |||
1028 | 1012 | return; | ||
1029 | 1013 | } | ||
1030 | 1014 | |||
1031 | 1015 | sub _set { | ||
1032 | 1016 | my ($self, %args) = @_; | ||
1033 | 1017 | have_required_args(\%args, qw( | ||
1034 | 1018 | method | ||
1035 | 1019 | resources | ||
1036 | 1020 | link | ||
1037 | 1021 | )) or die; | ||
1038 | 1022 | my $method = $args{method}; | ||
1039 | 1023 | my $res = $args{resources}; | ||
1040 | 1024 | my $link = $args{link}; | ||
1041 | 1025 | |||
1042 | 1026 | my $headers = $args{headers}; | ||
1043 | 1027 | |||
1044 | 1028 | my $content = ''; | ||
1045 | 1029 | if ( ref($res) eq 'ARRAY' ) { | ||
1046 | 1030 | PTDEBUG && _d('List of resources'); | ||
1047 | 1031 | $content = '[' . join(",\n", map { as_json($_) } @$res) . ']'; | ||
1048 | 1032 | } | ||
1049 | 1033 | elsif ( ref($res) ) { | ||
1050 | 1034 | PTDEBUG && _d('Resource object'); | ||
1051 | 1035 | $content = as_json($res); | ||
1052 | 1036 | } | ||
1053 | 1037 | elsif ( $res !~ m/\n/ && -f $res ) { | ||
1054 | 1038 | PTDEBUG && _d('List of resources in file', $res); | ||
1055 | 1039 | $content = '['; | ||
1056 | 1040 | my $data = do { | ||
1057 | 1041 | local $INPUT_RECORD_SEPARATOR = undef; | ||
1058 | 1042 | open my $fh, '<', $res | ||
1059 | 1043 | or die "Error opening $res: $OS_ERROR"; | ||
1060 | 1044 | <$fh>; | ||
1061 | 1045 | }; | ||
1062 | 1046 | $data =~ s/,?\s*$/]/; | ||
1063 | 1047 | $content .= $data; | ||
1064 | 1048 | } | ||
1065 | 1049 | else { | ||
1066 | 1050 | PTDEBUG && _d('Resource text'); | ||
1067 | 1051 | $content = $res; | ||
1068 | 1052 | } | ||
1069 | 1053 | |||
1070 | 1054 | eval { | ||
1071 | 1055 | $self->_request( | ||
1072 | 1056 | method => $method, | ||
1073 | 1057 | link => $link, | ||
1074 | 1058 | content => $content, | ||
1075 | 1059 | headers => $headers, | ||
1076 | 1060 | ); | ||
1077 | 1061 | }; | ||
1078 | 1062 | if ( my $e = $EVAL_ERROR ) { | ||
1079 | 1063 | if (blessed($e) && $e->isa('Percona::WebAPI::Exception::Request')) { | ||
1080 | 1064 | die $e; | ||
1081 | 1065 | } | ||
1082 | 1066 | else { | ||
1083 | 1067 | die "Unknown error: $e"; | ||
1084 | 1068 | } | ||
1085 | 1069 | } | ||
1086 | 1070 | |||
1087 | 1071 | return; | ||
1088 | 1072 | } | ||
1089 | 1073 | |||
1090 | 1074 | sub _request { | ||
1091 | 1075 | my ($self, %args) = @_; | ||
1092 | 1076 | |||
1093 | 1077 | have_required_args(\%args, qw( | ||
1094 | 1078 | method | ||
1095 | 1079 | link | ||
1096 | 1080 | )) or die; | ||
1097 | 1081 | my $method = $args{method}; | ||
1098 | 1082 | my $link = $args{link}; | ||
1099 | 1083 | |||
1100 | 1084 | my $content = $args{content}; | ||
1101 | 1085 | my $headers = $args{headers}; | ||
1102 | 1086 | |||
1103 | 1087 | my $req = HTTP::Request->new($method => $link); | ||
1104 | 1088 | if ( $content ) { | ||
1105 | 1089 | $req->content($content); | ||
1106 | 1090 | } | ||
1107 | 1091 | if ( $headers ) { | ||
1108 | 1092 | map { $req->header($_ => $headers->{$_}) } keys %$headers; | ||
1109 | 1093 | } | ||
1110 | 1094 | PTDEBUG && _d('Request', $method, $link, Dumper($req)); | ||
1111 | 1095 | |||
1112 | 1096 | my $response = $self->ua->request($req); | ||
1113 | 1097 | PTDEBUG && _d('Response', Dumper($response)); | ||
1114 | 1098 | |||
1115 | 1099 | $self->response($response); | ||
1116 | 1100 | |||
1117 | 1101 | if ( !($response->code >= 200 && $response->code < 400) ) { | ||
1118 | 1102 | die Percona::WebAPI::Exception::Request->new( | ||
1119 | 1103 | method => $method, | ||
1120 | 1104 | url => $link, | ||
1121 | 1105 | content => $content, | ||
1122 | 1106 | status => $response->code, | ||
1123 | 1107 | error => "Failed to $method $link", | ||
1124 | 1108 | ); | ||
1125 | 1109 | } | ||
1126 | 1110 | |||
1127 | 1111 | return; | ||
1128 | 1112 | } | ||
1129 | 1113 | |||
1130 | 1114 | no Lmo; | ||
1131 | 1115 | 1; | ||
1132 | 1116 | } | ||
1133 | 1117 | # ########################################################################### | ||
1134 | 1118 | # End Percona::WebAPI::Client package | ||
1135 | 1119 | # ########################################################################### | ||
1136 | 1120 | |||
1137 | 1121 | # ########################################################################### | ||
1138 | 1122 | # Percona::WebAPI::Exception::Request package | ||
1139 | 1123 | # This package is a copy without comments from the original. The original | ||
1140 | 1124 | # with comments and its test file can be found in the Bazaar repository at, | ||
1141 | 1125 | # lib/Percona/WebAPI/Exception/Request.pm | ||
1142 | 1126 | # t/lib/Percona/WebAPI/Exception/Request.t | ||
1143 | 1127 | # See https://launchpad.net/percona-toolkit for more information. | ||
1144 | 1128 | # ########################################################################### | ||
1145 | 1129 | { | ||
1146 | 1130 | package Percona::WebAPI::Exception::Request; | ||
1147 | 1131 | |||
1148 | 1132 | use Lmo; | ||
1149 | 1133 | use overload '""' => \&as_string; | ||
1150 | 1134 | |||
1151 | 1135 | has 'method' => ( | ||
1152 | 1136 | is => 'ro', | ||
1153 | 1137 | isa => 'Str', | ||
1154 | 1138 | required => 1, | ||
1155 | 1139 | ); | ||
1156 | 1140 | |||
1157 | 1141 | has 'url' => ( | ||
1158 | 1142 | is => 'ro', | ||
1159 | 1143 | isa => 'Str', | ||
1160 | 1144 | required => 1, | ||
1161 | 1145 | ); | ||
1162 | 1146 | |||
1163 | 1147 | has 'content' => ( | ||
1164 | 1148 | is => 'ro', | ||
1165 | 1149 | isa => 'Maybe[Str]', | ||
1166 | 1150 | required => 0, | ||
1167 | 1151 | ); | ||
1168 | 1152 | |||
1169 | 1153 | has 'status' => ( | ||
1170 | 1154 | is => 'ro', | ||
1171 | 1155 | isa => 'Int', | ||
1172 | 1156 | required => 1, | ||
1173 | 1157 | ); | ||
1174 | 1158 | |||
1175 | 1159 | has 'error' => ( | ||
1176 | 1160 | is => 'ro', | ||
1177 | 1161 | isa => 'Str', | ||
1178 | 1162 | required => 1, | ||
1179 | 1163 | ); | ||
1180 | 1164 | |||
1181 | 1165 | sub as_string { | ||
1182 | 1166 | my $self = shift; | ||
1183 | 1167 | chomp(my $error = $self->error); | ||
1184 | 1168 | $error =~ s/\n/ /g; | ||
1185 | 1169 | return sprintf "%s\nRequest: %s %s %s\nStatus: %d\n", | ||
1186 | 1170 | $error, $self->method, $self->url, $self->content || '', $self->status; | ||
1187 | 1171 | } | ||
1188 | 1172 | |||
1189 | 1173 | no Lmo; | ||
1190 | 1174 | 1; | ||
1191 | 1175 | } | ||
1192 | 1176 | # ########################################################################### | ||
1193 | 1177 | # End Percona::WebAPI::Exception::Request package | ||
1194 | 1178 | # ########################################################################### | ||
1195 | 1179 | |||
1196 | 1180 | # ########################################################################### | ||
1197 | 1181 | # Percona::WebAPI::Exception::Resource package | ||
1198 | 1182 | # This package is a copy without comments from the original. The original | ||
1199 | 1183 | # with comments and its test file can be found in the Bazaar repository at, | ||
1200 | 1184 | # lib/Percona/WebAPI/Exception/Resource.pm | ||
1201 | 1185 | # t/lib/Percona/WebAPI/Exception/Resource.t | ||
1202 | 1186 | # See https://launchpad.net/percona-toolkit for more information. | ||
1203 | 1187 | # ########################################################################### | ||
1204 | 1188 | { | ||
1205 | 1189 | package Percona::WebAPI::Exception::Resource; | ||
1206 | 1190 | |||
1207 | 1191 | use Lmo; | ||
1208 | 1192 | use overload '""' => \&as_string; | ||
1209 | 1193 | use Data::Dumper; | ||
1210 | 1194 | |||
1211 | 1195 | has 'type' => ( | ||
1212 | 1196 | is => 'ro', | ||
1213 | 1197 | isa => 'Str', | ||
1214 | 1198 | required => 1, | ||
1215 | 1199 | ); | ||
1216 | 1200 | |||
1217 | 1201 | has 'link' => ( | ||
1218 | 1202 | is => 'ro', | ||
1219 | 1203 | isa => 'Str', | ||
1220 | 1204 | required => 1, | ||
1221 | 1205 | ); | ||
1222 | 1206 | |||
1223 | 1207 | has 'data' => ( | ||
1224 | 1208 | is => 'ro', | ||
1225 | 1209 | isa => 'ArrayRef', | ||
1226 | 1210 | required => 1, | ||
1227 | 1211 | ); | ||
1228 | 1212 | |||
1229 | 1213 | has 'error' => ( | ||
1230 | 1214 | is => 'ro', | ||
1231 | 1215 | isa => 'Str', | ||
1232 | 1216 | required => 1, | ||
1233 | 1217 | ); | ||
1234 | 1218 | |||
1235 | 1219 | sub as_string { | ||
1236 | 1220 | my $self = shift; | ||
1237 | 1221 | chomp(my $error = $self->error); | ||
1238 | 1222 | local $Data::Dumper::Indent = 1; | ||
1239 | 1223 | local $Data::Dumper::Sortkeys = 1; | ||
1240 | 1224 | local $Data::Dumper::Quotekeys = 0; | ||
1241 | 1225 | return sprintf "Invalid %s resource from %s:\n\n%s\nError: %s\n\n", | ||
1242 | 1226 | $self->type, $self->link, Dumper($self->data), $error; | ||
1243 | 1227 | } | ||
1244 | 1228 | |||
1245 | 1229 | no Lmo; | ||
1246 | 1230 | 1; | ||
1247 | 1231 | } | ||
1248 | 1232 | # ########################################################################### | ||
1249 | 1233 | # End Percona::WebAPI::Exception::Resource package | ||
1250 | 1234 | # ########################################################################### | ||
1251 | 1235 | |||
1252 | 1236 | # ########################################################################### | ||
1253 | 1237 | # Percona::WebAPI::Resource::Agent package | ||
1254 | 1238 | # This package is a copy without comments from the original. The original | ||
1255 | 1239 | # with comments and its test file can be found in the Bazaar repository at, | ||
1256 | 1240 | # lib/Percona/WebAPI/Resource/Agent.pm | ||
1257 | 1241 | # t/lib/Percona/WebAPI/Resource/Agent.t | ||
1258 | 1242 | # See https://launchpad.net/percona-toolkit for more information. | ||
1259 | 1243 | # ########################################################################### | ||
1260 | 1244 | { | ||
1261 | 1245 | package Percona::WebAPI::Resource::Agent; | ||
1262 | 1246 | |||
1263 | 1247 | use Lmo; | ||
1264 | 1248 | |||
1265 | 1249 | has 'uuid' => ( | ||
1266 | 1250 | is => 'ro', | ||
1267 | 1251 | isa => 'Str', | ||
1268 | 1252 | required => 0, | ||
1269 | 1253 | ); | ||
1270 | 1254 | |||
1271 | 1255 | has 'username' => ( | ||
1272 | 1256 | is => 'rw', | ||
1273 | 1257 | isa => 'Str', | ||
1274 | 1258 | required => 0, | ||
1275 | 1259 | default => sub { return $ENV{USER} || $ENV{LOGNAME} }, | ||
1276 | 1260 | ); | ||
1277 | 1261 | |||
1278 | 1262 | has 'hostname' => ( | ||
1279 | 1263 | is => 'rw', | ||
1280 | 1264 | isa => 'Str', | ||
1281 | 1265 | required => 0, | ||
1282 | 1266 | default => sub { | ||
1283 | 1267 | chomp(my $hostname = `hostname`); | ||
1284 | 1268 | return $hostname; | ||
1285 | 1269 | }, | ||
1286 | 1270 | ); | ||
1287 | 1271 | |||
1288 | 1272 | has 'alias' => ( | ||
1289 | 1273 | is => 'rw', | ||
1290 | 1274 | isa => 'Str', | ||
1291 | 1275 | required => 0, | ||
1292 | 1276 | ); | ||
1293 | 1277 | |||
1294 | 1278 | has 'versions' => ( | ||
1295 | 1279 | is => 'rw', | ||
1296 | 1280 | isa => 'Maybe[HashRef]', | ||
1297 | 1281 | required => 0, | ||
1298 | 1282 | ); | ||
1299 | 1283 | |||
1300 | 1284 | has 'links' => ( | ||
1301 | 1285 | is => 'rw', | ||
1302 | 1286 | isa => 'Maybe[HashRef]', | ||
1303 | 1287 | required => 0, | ||
1304 | 1288 | default => sub { return {} }, | ||
1305 | 1289 | ); | ||
1306 | 1290 | |||
1307 | 1291 | sub name { | ||
1308 | 1292 | my ($self) = @_; | ||
1309 | 1293 | return $self->alias || $self->hostname || $self->uuid || 'Unknown'; | ||
1310 | 1294 | } | ||
1311 | 1295 | |||
1312 | 1296 | no Lmo; | ||
1313 | 1297 | 1; | ||
1314 | 1298 | } | ||
1315 | 1299 | # ########################################################################### | ||
1316 | 1300 | # End Percona::WebAPI::Resource::Agent package | ||
1317 | 1301 | # ########################################################################### | ||
1318 | 1302 | |||
1319 | 1303 | # ########################################################################### | ||
1320 | 1304 | # Percona::WebAPI::Resource::Config package | ||
1321 | 1305 | # This package is a copy without comments from the original. The original | ||
1322 | 1306 | # with comments and its test file can be found in the Bazaar repository at, | ||
1323 | 1307 | # lib/Percona/WebAPI/Resource/Config.pm | ||
1324 | 1308 | # t/lib/Percona/WebAPI/Resource/Config.t | ||
1325 | 1309 | # See https://launchpad.net/percona-toolkit for more information. | ||
1326 | 1310 | # ########################################################################### | ||
1327 | 1311 | { | ||
1328 | 1312 | package Percona::WebAPI::Resource::Config; | ||
1329 | 1313 | |||
1330 | 1314 | use Lmo; | ||
1331 | 1315 | |||
1332 | 1316 | has 'ts' => ( | ||
1333 | 1317 | is => 'ro', | ||
1334 | 1318 | isa => 'Int', | ||
1335 | 1319 | required => 1, | ||
1336 | 1320 | ); | ||
1337 | 1321 | |||
1338 | 1322 | has 'name' => ( | ||
1339 | 1323 | is => 'ro', | ||
1340 | 1324 | isa => 'Str', | ||
1341 | 1325 | required => 1, | ||
1342 | 1326 | ); | ||
1343 | 1327 | |||
1344 | 1328 | has 'options' => ( | ||
1345 | 1329 | is => 'ro', | ||
1346 | 1330 | isa => 'HashRef', | ||
1347 | 1331 | required => 1, | ||
1348 | 1332 | ); | ||
1349 | 1333 | |||
1350 | 1334 | has 'links' => ( | ||
1351 | 1335 | is => 'rw', | ||
1352 | 1336 | isa => 'Maybe[HashRef]', | ||
1353 | 1337 | required => 0, | ||
1354 | 1338 | default => sub { return {} }, | ||
1355 | 1339 | ); | ||
1356 | 1340 | |||
1357 | 1341 | no Lmo; | ||
1358 | 1342 | 1; | ||
1359 | 1343 | } | ||
1360 | 1344 | # ########################################################################### | ||
1361 | 1345 | # End Percona::WebAPI::Resource::Config package | ||
1362 | 1346 | # ########################################################################### | ||
1363 | 1347 | |||
1364 | 1348 | # ########################################################################### | ||
1365 | 1349 | # Percona::WebAPI::Resource::Service package | ||
1366 | 1350 | # This package is a copy without comments from the original. The original | ||
1367 | 1351 | # with comments and its test file can be found in the Bazaar repository at, | ||
1368 | 1352 | # lib/Percona/WebAPI/Resource/Service.pm | ||
1369 | 1353 | # t/lib/Percona/WebAPI/Resource/Service.t | ||
1370 | 1354 | # See https://launchpad.net/percona-toolkit for more information. | ||
1371 | 1355 | # ########################################################################### | ||
1372 | 1356 | { | ||
1373 | 1357 | package Percona::WebAPI::Resource::Service; | ||
1374 | 1358 | |||
1375 | 1359 | use Lmo; | ||
1376 | 1360 | |||
1377 | 1361 | has 'ts' => ( | ||
1378 | 1362 | is => 'ro', | ||
1379 | 1363 | isa => 'Int', | ||
1380 | 1364 | required => 1, | ||
1381 | 1365 | ); | ||
1382 | 1366 | |||
1383 | 1367 | has 'name' => ( | ||
1384 | 1368 | is => 'ro', | ||
1385 | 1369 | isa => 'Str', | ||
1386 | 1370 | required => 1, | ||
1387 | 1371 | ); | ||
1388 | 1372 | |||
1389 | 1373 | has 'tasks' => ( | ||
1390 | 1374 | is => 'ro', | ||
1391 | 1375 | isa => 'ArrayRef[Percona::WebAPI::Resource::Task]', | ||
1392 | 1376 | required => 1, | ||
1393 | 1377 | ); | ||
1394 | 1378 | |||
1395 | 1379 | has 'run_schedule' => ( | ||
1396 | 1380 | is => 'ro', | ||
1397 | 1381 | isa => 'Str', | ||
1398 | 1382 | required => 0, | ||
1399 | 1383 | ); | ||
1400 | 1384 | |||
1401 | 1385 | has 'spool_schedule' => ( | ||
1402 | 1386 | is => 'ro', | ||
1403 | 1387 | isa => 'Str', | ||
1404 | 1388 | required => 0, | ||
1405 | 1389 | ); | ||
1406 | 1390 | |||
1407 | 1391 | has 'meta' => ( | ||
1408 | 1392 | is => 'ro', | ||
1409 | 1393 | isa => 'Bool', | ||
1410 | 1394 | required => 0, | ||
1411 | 1395 | default => sub { return 0 }, | ||
1412 | 1396 | ); | ||
1413 | 1397 | |||
1414 | 1398 | has 'run_once' => ( | ||
1415 | 1399 | is => 'ro', | ||
1416 | 1400 | isa => 'Bool', | ||
1417 | 1401 | required => 0, | ||
1418 | 1402 | default => sub { return 0 }, | ||
1419 | 1403 | ); | ||
1420 | 1404 | |||
1421 | 1405 | has 'links' => ( | ||
1422 | 1406 | is => 'rw', | ||
1423 | 1407 | isa => 'Maybe[HashRef]', | ||
1424 | 1408 | required => 0, | ||
1425 | 1409 | default => sub { return {} }, | ||
1426 | 1410 | ); | ||
1427 | 1411 | |||
1428 | 1412 | sub BUILDARGS { | ||
1429 | 1413 | my ($class, %args) = @_; | ||
1430 | 1414 | if ( ref $args{tasks} eq 'ARRAY' ) { | ||
1431 | 1415 | my @tasks; | ||
1432 | 1416 | foreach my $run_hashref ( @{$args{tasks}} ) { | ||
1433 | 1417 | my $task = Percona::WebAPI::Resource::Task->new(%$run_hashref); | ||
1434 | 1418 | push @tasks, $task; | ||
1435 | 1419 | } | ||
1436 | 1420 | $args{tasks} = \@tasks; | ||
1437 | 1421 | } | ||
1438 | 1422 | return $class->SUPER::BUILDARGS(%args); | ||
1439 | 1423 | } | ||
1440 | 1424 | |||
1441 | 1425 | no Lmo; | ||
1442 | 1426 | 1; | ||
1443 | 1427 | } | ||
1444 | 1428 | # ########################################################################### | ||
1445 | 1429 | # End Percona::WebAPI::Resource::Service package | ||
1446 | 1430 | # ########################################################################### | ||
1447 | 1431 | |||
1448 | 1432 | # ########################################################################### | ||
1449 | 1433 | # Percona::WebAPI::Resource::Task package | ||
1450 | 1434 | # This package is a copy without comments from the original. The original | ||
1451 | 1435 | # with comments and its test file can be found in the Bazaar repository at, | ||
1452 | 1436 | # lib/Percona/WebAPI/Resource/Task.pm | ||
1453 | 1437 | # t/lib/Percona/WebAPI/Resource/Task.t | ||
1454 | 1438 | # See https://launchpad.net/percona-toolkit for more information. | ||
1455 | 1439 | # ########################################################################### | ||
1456 | 1440 | { | ||
1457 | 1441 | package Percona::WebAPI::Resource::Task; | ||
1458 | 1442 | |||
1459 | 1443 | use Lmo; | ||
1460 | 1444 | |||
1461 | 1445 | has 'name' => ( | ||
1462 | 1446 | is => 'ro', | ||
1463 | 1447 | isa => 'Str', | ||
1464 | 1448 | required => 1, | ||
1465 | 1449 | ); | ||
1466 | 1450 | |||
1467 | 1451 | has 'number' => ( | ||
1468 | 1452 | is => 'ro', | ||
1469 | 1453 | isa => 'Int', | ||
1470 | 1454 | required => 1, | ||
1471 | 1455 | ); | ||
1472 | 1456 | |||
1473 | 1457 | has 'program' => ( | ||
1474 | 1458 | is => 'ro', | ||
1475 | 1459 | isa => 'Maybe[Str]', | ||
1476 | 1460 | required => 0, | ||
1477 | 1461 | ); | ||
1478 | 1462 | |||
1479 | 1463 | has 'query' => ( | ||
1480 | 1464 | is => 'ro', | ||
1481 | 1465 | isa => 'Maybe[Str]', | ||
1482 | 1466 | required => 0, | ||
1483 | 1467 | ); | ||
1484 | 1468 | |||
1485 | 1469 | has 'output' => ( | ||
1486 | 1470 | is => 'ro', | ||
1487 | 1471 | isa => 'Maybe[Str]', | ||
1488 | 1472 | required => 0, | ||
1489 | 1473 | ); | ||
1490 | 1474 | |||
1491 | 1475 | sub TO_JSON { return { %{ shift() } }; } | ||
1492 | 1476 | |||
1493 | 1477 | no Lmo; | ||
1494 | 1478 | 1; | ||
1495 | 1479 | } | ||
1496 | 1480 | # ########################################################################### | ||
1497 | 1481 | # End Percona::WebAPI::Resource::Task package | ||
1498 | 1482 | # ########################################################################### | ||
1499 | 1483 | |||
1500 | 1484 | # ########################################################################### | ||
1501 | 1485 | # Percona::WebAPI::Resource::LogEntry package | ||
1502 | 1486 | # This package is a copy without comments from the original. The original | ||
1503 | 1487 | # with comments and its test file can be found in the Bazaar repository at, | ||
1504 | 1488 | # lib/Percona/WebAPI/Resource/LogEntry.pm | ||
1505 | 1489 | # t/lib/Percona/WebAPI/Resource/LogEntry.t | ||
1506 | 1490 | # See https://launchpad.net/percona-toolkit for more information. | ||
1507 | 1491 | # ########################################################################### | ||
1508 | 1492 | { | ||
1509 | 1493 | package Percona::WebAPI::Resource::LogEntry; | ||
1510 | 1494 | |||
1511 | 1495 | use Lmo; | ||
1512 | 1496 | |||
1513 | 1497 | has 'pid' => ( | ||
1514 | 1498 | is => 'ro', | ||
1515 | 1499 | isa => 'Int', | ||
1516 | 1500 | required => 1, | ||
1517 | 1501 | ); | ||
1518 | 1502 | |||
1519 | 1503 | has 'service' => ( | ||
1520 | 1504 | is => 'ro', | ||
1521 | 1505 | isa => 'Str', | ||
1522 | 1506 | required => 0, | ||
1523 | 1507 | ); | ||
1524 | 1508 | |||
1525 | 1509 | has 'data_ts' => ( | ||
1526 | 1510 | is => 'ro', | ||
1527 | 1511 | isa => 'Int', | ||
1528 | 1512 | required => 0, | ||
1529 | 1513 | ); | ||
1530 | 1514 | |||
1531 | 1515 | has 'entry_ts' => ( | ||
1532 | 1516 | is => 'ro', | ||
1533 | 1517 | isa => 'Str', | ||
1534 | 1518 | required => 1, | ||
1535 | 1519 | ); | ||
1536 | 1520 | |||
1537 | 1521 | has 'log_level' => ( | ||
1538 | 1522 | is => 'ro', | ||
1539 | 1523 | isa => 'Int', | ||
1540 | 1524 | required => 1, | ||
1541 | 1525 | ); | ||
1542 | 1526 | |||
1543 | 1527 | has 'message' => ( | ||
1544 | 1528 | is => 'ro', | ||
1545 | 1529 | isa => 'Str', | ||
1546 | 1530 | required => 1, | ||
1547 | 1531 | ); | ||
1548 | 1532 | |||
1549 | 1533 | no Lmo; | ||
1550 | 1534 | 1; | ||
1551 | 1535 | } | ||
1552 | 1536 | # ########################################################################### | ||
1553 | 1537 | # End Percona::WebAPI::Resource::LogEntry package | ||
1554 | 1538 | # ########################################################################### | ||
1555 | 1539 | |||
1556 | 1540 | # ########################################################################### | ||
1557 | 1541 | # VersionCheck package | ||
1558 | 1542 | # This package is a copy without comments from the original. The original | ||
1559 | 1543 | # with comments and its test file can be found in the Bazaar repository at, | ||
1560 | 1544 | # lib/VersionCheck.pm | ||
1561 | 1545 | # t/lib/VersionCheck.t | ||
1562 | 1546 | # See https://launchpad.net/percona-toolkit for more information. | ||
1563 | 1547 | # ########################################################################### | ||
1564 | 1548 | { | ||
1565 | 1549 | package VersionCheck; | ||
1566 | 1550 | |||
1567 | 1551 | |||
1568 | 1552 | use strict; | ||
1569 | 1553 | use warnings FATAL => 'all'; | ||
1570 | 1554 | use English qw(-no_match_vars); | ||
1571 | 1555 | |||
1572 | 1556 | use constant PTDEBUG => $ENV{PTDEBUG} || 0; | ||
1573 | 1557 | |||
1574 | 1558 | use Data::Dumper; | ||
1575 | 1559 | local $Data::Dumper::Indent = 1; | ||
1576 | 1560 | local $Data::Dumper::Sortkeys = 1; | ||
1577 | 1561 | local $Data::Dumper::Quotekeys = 0; | ||
1578 | 1562 | |||
1579 | 1563 | use Digest::MD5 qw(md5_hex); | ||
1580 | 1564 | use Sys::Hostname qw(hostname); | ||
1581 | 1565 | use File::Basename qw(); | ||
1582 | 1566 | use File::Spec; | ||
1583 | 1567 | use FindBin qw(); | ||
1584 | 1568 | |||
1585 | 1569 | eval { | ||
1586 | 1570 | require Percona::Toolkit; | ||
1587 | 1571 | require HTTP::Micro; | ||
1588 | 1572 | }; | ||
1589 | 1573 | |||
1590 | 1574 | { | ||
1591 | 1575 | my $file = 'percona-version-check'; | ||
1592 | 1576 | my $home = $ENV{HOME} || $ENV{HOMEPATH} || $ENV{USERPROFILE} || '.'; | ||
1593 | 1577 | my @vc_dirs = ( | ||
1594 | 1578 | '/etc/percona', | ||
1595 | 1579 | '/etc/percona-toolkit', | ||
1596 | 1580 | '/tmp', | ||
1597 | 1581 | "$home", | ||
1598 | 1582 | ); | ||
1599 | 1583 | |||
1600 | 1584 | sub version_check_file { | ||
1601 | 1585 | foreach my $dir ( @vc_dirs ) { | ||
1602 | 1586 | if ( -d $dir && -w $dir ) { | ||
1603 | 1587 | PTDEBUG && _d('Version check file', $file, 'in', $dir); | ||
1604 | 1588 | return $dir . '/' . $file; | ||
1605 | 1589 | } | ||
1606 | 1590 | } | ||
1607 | 1591 | PTDEBUG && _d('Version check file', $file, 'in', $ENV{PWD}); | ||
1608 | 1592 | return $file; # in the CWD | ||
1609 | 1593 | } | ||
1610 | 1594 | } | ||
1611 | 1595 | |||
1612 | 1596 | sub version_check_time_limit { | ||
1613 | 1597 | return 60 * 60 * 24; # one day | ||
1614 | 1598 | } | ||
1615 | 1599 | |||
1616 | 1600 | |||
1617 | 1601 | sub version_check { | ||
1618 | 1602 | my (%args) = @_; | ||
1619 | 1603 | |||
1620 | 1604 | my $instances = $args{instances} || []; | ||
1621 | 1605 | my $instances_to_check; | ||
1622 | 1606 | |||
1623 | 1607 | PTDEBUG && _d('FindBin::Bin:', $FindBin::Bin); | ||
1624 | 1608 | if ( !$args{force} ) { | ||
1625 | 1609 | if ( $FindBin::Bin | ||
1626 | 1610 | && (-d "$FindBin::Bin/../.bzr" || -d "$FindBin::Bin/../../.bzr") ) { | ||
1627 | 1611 | PTDEBUG && _d("$FindBin::Bin/../.bzr disables --version-check"); | ||
1628 | 1612 | return; | ||
1629 | 1613 | } | ||
1630 | 1614 | } | ||
1631 | 1615 | |||
1632 | 1616 | eval { | ||
1633 | 1617 | foreach my $instance ( @$instances ) { | ||
1634 | 1618 | my ($name, $id) = get_instance_id($instance); | ||
1635 | 1619 | $instance->{name} = $name; | ||
1636 | 1620 | $instance->{id} = $id; | ||
1637 | 1621 | } | ||
1638 | 1622 | |||
1639 | 1623 | push @$instances, { name => 'system', id => 0 }; | ||
1640 | 1624 | |||
1641 | 1625 | $instances_to_check = get_instances_to_check( | ||
1642 | 1626 | instances => $instances, | ||
1643 | 1627 | vc_file => $args{vc_file}, # testing | ||
1644 | 1628 | now => $args{now}, # testing | ||
1645 | 1629 | ); | ||
1646 | 1630 | PTDEBUG && _d(scalar @$instances_to_check, 'instances to check'); | ||
1647 | 1631 | return unless @$instances_to_check; | ||
1648 | 1632 | |||
1649 | 1633 | my $protocol = 'https'; # optimistic, but... | ||
1650 | 1634 | eval { require IO::Socket::SSL; }; | ||
1651 | 1635 | if ( $EVAL_ERROR ) { | ||
1652 | 1636 | PTDEBUG && _d($EVAL_ERROR); | ||
1653 | 1637 | $protocol = 'http'; | ||
1654 | 1638 | } | ||
1655 | 1639 | PTDEBUG && _d('Using', $protocol); | ||
1656 | 1640 | |||
1657 | 1641 | my $advice = pingback( | ||
1658 | 1642 | instances => $instances_to_check, | ||
1659 | 1643 | protocol => $protocol, | ||
1660 | 1644 | url => $args{url} # testing | ||
1661 | 1645 | || $ENV{PERCONA_VERSION_CHECK_URL} # testing | ||
1662 | 1646 | || "$protocol://v.percona.com", | ||
1663 | 1647 | ); | ||
1664 | 1648 | if ( $advice ) { | ||
1665 | 1649 | PTDEBUG && _d('Advice:', Dumper($advice)); | ||
1666 | 1650 | if ( scalar @$advice > 1) { | ||
1667 | 1651 | print "\n# " . scalar @$advice . " software updates are " | ||
1668 | 1652 | . "available:\n"; | ||
1669 | 1653 | } | ||
1670 | 1654 | else { | ||
1671 | 1655 | print "\n# A software update is available:\n"; | ||
1672 | 1656 | } | ||
1673 | 1657 | print join("\n", map { "# * $_" } @$advice), "\n\n"; | ||
1674 | 1658 | } | ||
1675 | 1659 | }; | ||
1676 | 1660 | if ( $EVAL_ERROR ) { | ||
1677 | 1661 | PTDEBUG && _d('Version check failed:', $EVAL_ERROR); | ||
1678 | 1662 | } | ||
1679 | 1663 | |||
1680 | 1664 | if ( @$instances_to_check ) { | ||
1681 | 1665 | eval { | ||
1682 | 1666 | update_check_times( | ||
1683 | 1667 | instances => $instances_to_check, | ||
1684 | 1668 | vc_file => $args{vc_file}, # testing | ||
1685 | 1669 | now => $args{now}, # testing | ||
1686 | 1670 | ); | ||
1687 | 1671 | }; | ||
1688 | 1672 | if ( $EVAL_ERROR ) { | ||
1689 | 1673 | PTDEBUG && _d('Error updating version check file:', $EVAL_ERROR); | ||
1690 | 1674 | } | ||
1691 | 1675 | } | ||
1692 | 1676 | |||
1693 | 1677 | if ( $ENV{PTDEBUG_VERSION_CHECK} ) { | ||
1694 | 1678 | warn "Exiting because the PTDEBUG_VERSION_CHECK " | ||
1695 | 1679 | . "environment variable is defined.\n"; | ||
1696 | 1680 | exit 255; | ||
1697 | 1681 | } | ||
1698 | 1682 | |||
1699 | 1683 | return; | ||
1700 | 1684 | } | ||
1701 | 1685 | |||
1702 | 1686 | sub get_instances_to_check { | ||
1703 | 1687 | my (%args) = @_; | ||
1704 | 1688 | |||
1705 | 1689 | my $instances = $args{instances}; | ||
1706 | 1690 | my $now = $args{now} || int(time); | ||
1707 | 1691 | my $vc_file = $args{vc_file} || version_check_file(); | ||
1708 | 1692 | |||
1709 | 1693 | if ( !-f $vc_file ) { | ||
1710 | 1694 | PTDEBUG && _d('Version check file', $vc_file, 'does not exist;', | ||
1711 | 1695 | 'version checking all instances'); | ||
1712 | 1696 | return $instances; | ||
1713 | 1697 | } | ||
1714 | 1698 | |||
1715 | 1699 | open my $fh, '<', $vc_file or die "Cannot open $vc_file: $OS_ERROR"; | ||
1716 | 1700 | chomp(my $file_contents = do { local $/ = undef; <$fh> }); | ||
1717 | 1701 | PTDEBUG && _d('Version check file', $vc_file, 'contents:', $file_contents); | ||
1718 | 1702 | close $fh; | ||
1719 | 1703 | my %last_check_time_for = $file_contents =~ /^([^,]+),(.+)$/mg; | ||
1720 | 1704 | |||
1721 | 1705 | my $check_time_limit = version_check_time_limit(); | ||
1722 | 1706 | my @instances_to_check; | ||
1723 | 1707 | foreach my $instance ( @$instances ) { | ||
1724 | 1708 | my $last_check_time = $last_check_time_for{ $instance->{id} }; | ||
1725 | 1709 | PTDEBUG && _d('Intsance', $instance->{id}, 'last checked', | ||
1726 | 1710 | $last_check_time, 'now', $now, 'diff', $now - ($last_check_time || 0), | ||
1727 | 1711 | 'hours until next check', | ||
1728 | 1712 | sprintf '%.2f', | ||
1729 | 1713 | ($check_time_limit - ($now - ($last_check_time || 0))) / 3600); | ||
1730 | 1714 | if ( !defined $last_check_time | ||
1731 | 1715 | || ($now - $last_check_time) >= $check_time_limit ) { | ||
1732 | 1716 | PTDEBUG && _d('Time to check', Dumper($instance)); | ||
1733 | 1717 | push @instances_to_check, $instance; | ||
1734 | 1718 | } | ||
1735 | 1719 | } | ||
1736 | 1720 | |||
1737 | 1721 | return \@instances_to_check; | ||
1738 | 1722 | } | ||
1739 | 1723 | |||
1740 | 1724 | sub update_check_times { | ||
1741 | 1725 | my (%args) = @_; | ||
1742 | 1726 | |||
1743 | 1727 | my $instances = $args{instances}; | ||
1744 | 1728 | my $now = $args{now} || int(time); | ||
1745 | 1729 | my $vc_file = $args{vc_file} || version_check_file(); | ||
1746 | 1730 | PTDEBUG && _d('Updating last check time:', $now); | ||
1747 | 1731 | |||
1748 | 1732 | my %all_instances = map { | ||
1749 | 1733 | $_->{id} => { name => $_->{name}, ts => $now } | ||
1750 | 1734 | } @$instances; | ||
1751 | 1735 | |||
1752 | 1736 | if ( -f $vc_file ) { | ||
1753 | 1737 | open my $fh, '<', $vc_file or die "Cannot read $vc_file: $OS_ERROR"; | ||
1754 | 1738 | my $contents = do { local $/ = undef; <$fh> }; | ||
1755 | 1739 | close $fh; | ||
1756 | 1740 | |||
1757 | 1741 | foreach my $line ( split("\n", ($contents || '')) ) { | ||
1758 | 1742 | my ($id, $ts) = split(',', $line); | ||
1759 | 1743 | if ( !exists $all_instances{$id} ) { | ||
1760 | 1744 | $all_instances{$id} = { ts => $ts }; # original ts, not updated | ||
1761 | 1745 | } | ||
1762 | 1746 | } | ||
1763 | 1747 | } | ||
1764 | 1748 | |||
1765 | 1749 | open my $fh, '>', $vc_file or die "Cannot write to $vc_file: $OS_ERROR"; | ||
1766 | 1750 | foreach my $id ( sort keys %all_instances ) { | ||
1767 | 1751 | PTDEBUG && _d('Updated:', $id, Dumper($all_instances{$id})); | ||
1768 | 1752 | print { $fh } $id . ',' . $all_instances{$id}->{ts} . "\n"; | ||
1769 | 1753 | } | ||
1770 | 1754 | close $fh; | ||
1771 | 1755 | |||
1772 | 1756 | return; | ||
1773 | 1757 | } | ||
1774 | 1758 | |||
1775 | 1759 | sub get_instance_id { | ||
1776 | 1760 | my ($instance) = @_; | ||
1777 | 1761 | |||
1778 | 1762 | my $dbh = $instance->{dbh}; | ||
1779 | 1763 | my $dsn = $instance->{dsn}; | ||
1780 | 1764 | |||
1781 | 1765 | my $sql = q{SELECT CONCAT(@@hostname, @@port)}; | ||
1782 | 1766 | PTDEBUG && _d($sql); | ||
1783 | 1767 | my ($name) = eval { $dbh->selectrow_array($sql) }; | ||
1784 | 1768 | if ( $EVAL_ERROR ) { | ||
1785 | 1769 | PTDEBUG && _d($EVAL_ERROR); | ||
1786 | 1770 | $sql = q{SELECT @@hostname}; | ||
1787 | 1771 | PTDEBUG && _d($sql); | ||
1788 | 1772 | ($name) = eval { $dbh->selectrow_array($sql) }; | ||
1789 | 1773 | if ( $EVAL_ERROR ) { | ||
1790 | 1774 | PTDEBUG && _d($EVAL_ERROR); | ||
1791 | 1775 | $name = ($dsn->{h} || 'localhost') . ($dsn->{P} || 3306); | ||
1792 | 1776 | } | ||
1793 | 1777 | else { | ||
1794 | 1778 | $sql = q{SHOW VARIABLES LIKE 'port'}; | ||
1795 | 1779 | PTDEBUG && _d($sql); | ||
1796 | 1780 | my (undef, $port) = eval { $dbh->selectrow_array($sql) }; | ||
1797 | 1781 | PTDEBUG && _d('port:', $port); | ||
1798 | 1782 | $name .= $port || ''; | ||
1799 | 1783 | } | ||
1800 | 1784 | } | ||
1801 | 1785 | my $id = md5_hex($name); | ||
1802 | 1786 | |||
1803 | 1787 | PTDEBUG && _d('MySQL instance:', $id, $name, Dumper($dsn)); | ||
1804 | 1788 | |||
1805 | 1789 | return $name, $id; | ||
1806 | 1790 | } | ||
1807 | 1791 | |||
1808 | 1792 | |||
1809 | 1793 | sub pingback { | ||
1810 | 1794 | my (%args) = @_; | ||
1811 | 1795 | my @required_args = qw(url instances); | ||
1812 | 1796 | foreach my $arg ( @required_args ) { | ||
1813 | 1797 | die "I need a $arg arugment" unless $args{$arg}; | ||
1814 | 1798 | } | ||
1815 | 1799 | my $url = $args{url}; | ||
1816 | 1800 | my $instances = $args{instances}; | ||
1817 | 1801 | |||
1818 | 1802 | my $ua = $args{ua} || HTTP::Micro->new( timeout => 3 ); | ||
1819 | 1803 | |||
1820 | 1804 | my $response = $ua->request('GET', $url); | ||
1821 | 1805 | PTDEBUG && _d('Server response:', Dumper($response)); | ||
1822 | 1806 | die "No response from GET $url" | ||
1823 | 1807 | if !$response; | ||
1824 | 1808 | die("GET on $url returned HTTP status $response->{status}; expected 200\n", | ||
1825 | 1809 | ($response->{content} || '')) if $response->{status} != 200; | ||
1826 | 1810 | die("GET on $url did not return any programs to check") | ||
1827 | 1811 | if !$response->{content}; | ||
1828 | 1812 | |||
1829 | 1813 | my $items = parse_server_response( | ||
1830 | 1814 | response => $response->{content} | ||
1831 | 1815 | ); | ||
1832 | 1816 | die "Failed to parse server requested programs: $response->{content}" | ||
1833 | 1817 | if !scalar keys %$items; | ||
1834 | 1818 | |||
1835 | 1819 | my $versions = get_versions( | ||
1836 | 1820 | items => $items, | ||
1837 | 1821 | instances => $instances, | ||
1838 | 1822 | ); | ||
1839 | 1823 | die "Failed to get any program versions; should have at least gotten Perl" | ||
1840 | 1824 | if !scalar keys %$versions; | ||
1841 | 1825 | |||
1842 | 1826 | my $client_content = encode_client_response( | ||
1843 | 1827 | items => $items, | ||
1844 | 1828 | versions => $versions, | ||
1845 | 1829 | general_id => md5_hex( hostname() ), | ||
1846 | 1830 | ); | ||
1847 | 1831 | |||
1848 | 1832 | my $client_response = { | ||
1849 | 1833 | headers => { "X-Percona-Toolkit-Tool" => File::Basename::basename($0) }, | ||
1850 | 1834 | content => $client_content, | ||
1851 | 1835 | }; | ||
1852 | 1836 | PTDEBUG && _d('Client response:', Dumper($client_response)); | ||
1853 | 1837 | |||
1854 | 1838 | $response = $ua->request('POST', $url, $client_response); | ||
1855 | 1839 | PTDEBUG && _d('Server suggestions:', Dumper($response)); | ||
1856 | 1840 | die "No response from POST $url $client_response" | ||
1857 | 1841 | if !$response; | ||
1858 | 1842 | die "POST $url returned HTTP status $response->{status}; expected 200" | ||
1859 | 1843 | if $response->{status} != 200; | ||
1860 | 1844 | |||
1861 | 1845 | return unless $response->{content}; | ||
1862 | 1846 | |||
1863 | 1847 | $items = parse_server_response( | ||
1864 | 1848 | response => $response->{content}, | ||
1865 | 1849 | split_vars => 0, | ||
1866 | 1850 | ); | ||
1867 | 1851 | die "Failed to parse server suggestions: $response->{content}" | ||
1868 | 1852 | if !scalar keys %$items; | ||
1869 | 1853 | my @suggestions = map { $_->{vars} } | ||
1870 | 1854 | sort { $a->{item} cmp $b->{item} } | ||
1871 | 1855 | values %$items; | ||
1872 | 1856 | |||
1873 | 1857 | return \@suggestions; | ||
1874 | 1858 | } | ||
1875 | 1859 | |||
1876 | 1860 | sub encode_client_response { | ||
1877 | 1861 | my (%args) = @_; | ||
1878 | 1862 | my @required_args = qw(items versions general_id); | ||
1879 | 1863 | foreach my $arg ( @required_args ) { | ||
1880 | 1864 | die "I need a $arg arugment" unless $args{$arg}; | ||
1881 | 1865 | } | ||
1882 | 1866 | my ($items, $versions, $general_id) = @args{@required_args}; | ||
1883 | 1867 | |||
1884 | 1868 | my @lines; | ||
1885 | 1869 | foreach my $item ( sort keys %$items ) { | ||
1886 | 1870 | next unless exists $versions->{$item}; | ||
1887 | 1871 | if ( ref($versions->{$item}) eq 'HASH' ) { | ||
1888 | 1872 | my $mysql_versions = $versions->{$item}; | ||
1889 | 1873 | for my $id ( sort keys %$mysql_versions ) { | ||
1890 | 1874 | push @lines, join(';', $id, $item, $mysql_versions->{$id}); | ||
1891 | 1875 | } | ||
1892 | 1876 | } | ||
1893 | 1877 | else { | ||
1894 | 1878 | push @lines, join(';', $general_id, $item, $versions->{$item}); | ||
1895 | 1879 | } | ||
1896 | 1880 | } | ||
1897 | 1881 | |||
1898 | 1882 | my $client_response = join("\n", @lines) . "\n"; | ||
1899 | 1883 | return $client_response; | ||
1900 | 1884 | } | ||
1901 | 1885 | |||
1902 | 1886 | sub parse_server_response { | ||
1903 | 1887 | my (%args) = @_; | ||
1904 | 1888 | my @required_args = qw(response); | ||
1905 | 1889 | foreach my $arg ( @required_args ) { | ||
1906 | 1890 | die "I need a $arg arugment" unless $args{$arg}; | ||
1907 | 1891 | } | ||
1908 | 1892 | my ($response) = @args{@required_args}; | ||
1909 | 1893 | |||
1910 | 1894 | my %items = map { | ||
1911 | 1895 | my ($item, $type, $vars) = split(";", $_); | ||
1912 | 1896 | if ( !defined $args{split_vars} || $args{split_vars} ) { | ||
1913 | 1897 | $vars = [ split(",", ($vars || '')) ]; | ||
1914 | 1898 | } | ||
1915 | 1899 | $item => { | ||
1916 | 1900 | item => $item, | ||
1917 | 1901 | type => $type, | ||
1918 | 1902 | vars => $vars, | ||
1919 | 1903 | }; | ||
1920 | 1904 | } split("\n", $response); | ||
1921 | 1905 | |||
1922 | 1906 | PTDEBUG && _d('Items:', Dumper(\%items)); | ||
1923 | 1907 | |||
1924 | 1908 | return \%items; | ||
1925 | 1909 | } | ||
1926 | 1910 | |||
1927 | 1911 | my %sub_for_type = ( | ||
1928 | 1912 | os_version => \&get_os_version, | ||
1929 | 1913 | perl_version => \&get_perl_version, | ||
1930 | 1914 | perl_module_version => \&get_perl_module_version, | ||
1931 | 1915 | mysql_variable => \&get_mysql_variable, | ||
1932 | 1916 | ); | ||
1933 | 1917 | |||
1934 | 1918 | sub valid_item { | ||
1935 | 1919 | my ($item) = @_; | ||
1936 | 1920 | return unless $item; | ||
1937 | 1921 | if ( !exists $sub_for_type{ $item->{type} } ) { | ||
1938 | 1922 | PTDEBUG && _d('Invalid type:', $item->{type}); | ||
1939 | 1923 | return 0; | ||
1940 | 1924 | } | ||
1941 | 1925 | return 1; | ||
1942 | 1926 | } | ||
1943 | 1927 | |||
1944 | 1928 | sub get_versions { | ||
1945 | 1929 | my (%args) = @_; | ||
1946 | 1930 | my @required_args = qw(items); | ||
1947 | 1931 | foreach my $arg ( @required_args ) { | ||
1948 | 1932 | die "I need a $arg arugment" unless $args{$arg}; | ||
1949 | 1933 | } | ||
1950 | 1934 | my ($items) = @args{@required_args}; | ||
1951 | 1935 | |||
1952 | 1936 | my %versions; | ||
1953 | 1937 | foreach my $item ( values %$items ) { | ||
1954 | 1938 | next unless valid_item($item); | ||
1955 | 1939 | eval { | ||
1956 | 1940 | my $version = $sub_for_type{ $item->{type} }->( | ||
1957 | 1941 | item => $item, | ||
1958 | 1942 | instances => $args{instances}, | ||
1959 | 1943 | ); | ||
1960 | 1944 | if ( $version ) { | ||
1961 | 1945 | chomp $version unless ref($version); | ||
1962 | 1946 | $versions{$item->{item}} = $version; | ||
1963 | 1947 | } | ||
1964 | 1948 | }; | ||
1965 | 1949 | if ( $EVAL_ERROR ) { | ||
1966 | 1950 | PTDEBUG && _d('Error getting version for', Dumper($item), $EVAL_ERROR); | ||
1967 | 1951 | } | ||
1968 | 1952 | } | ||
1969 | 1953 | |||
1970 | 1954 | return \%versions; | ||
1971 | 1955 | } | ||
1972 | 1956 | |||
1973 | 1957 | |||
1974 | 1958 | sub get_os_version { | ||
1975 | 1959 | if ( $OSNAME eq 'MSWin32' ) { | ||
1976 | 1960 | require Win32; | ||
1977 | 1961 | return Win32::GetOSDisplayName(); | ||
1978 | 1962 | } | ||
1979 | 1963 | |||
1980 | 1964 | chomp(my $platform = `uname -s`); | ||
1981 | 1965 | PTDEBUG && _d('platform:', $platform); | ||
1982 | 1966 | return $OSNAME unless $platform; | ||
1983 | 1967 | |||
1984 | 1968 | chomp(my $lsb_release | ||
1985 | 1969 | = `which lsb_release 2>/dev/null | awk '{print \$1}'` || ''); | ||
1986 | 1970 | PTDEBUG && _d('lsb_release:', $lsb_release); | ||
1987 | 1971 | |||
1988 | 1972 | my $release = ""; | ||
1989 | 1973 | |||
1990 | 1974 | if ( $platform eq 'Linux' ) { | ||
1991 | 1975 | if ( -f "/etc/fedora-release" ) { | ||
1992 | 1976 | $release = `cat /etc/fedora-release`; | ||
1993 | 1977 | } | ||
1994 | 1978 | elsif ( -f "/etc/redhat-release" ) { | ||
1995 | 1979 | $release = `cat /etc/redhat-release`; | ||
1996 | 1980 | } | ||
1997 | 1981 | elsif ( -f "/etc/system-release" ) { | ||
1998 | 1982 | $release = `cat /etc/system-release`; | ||
1999 | 1983 | } | ||
2000 | 1984 | elsif ( $lsb_release ) { | ||
2001 | 1985 | $release = `$lsb_release -ds`; | ||
2002 | 1986 | } | ||
2003 | 1987 | elsif ( -f "/etc/lsb-release" ) { | ||
2004 | 1988 | $release = `grep DISTRIB_DESCRIPTION /etc/lsb-release`; | ||
2005 | 1989 | $release =~ s/^\w+="([^"]+)".+/$1/; | ||
2006 | 1990 | } | ||
2007 | 1991 | elsif ( -f "/etc/debian_version" ) { | ||
2008 | 1992 | chomp(my $rel = `cat /etc/debian_version`); | ||
2009 | 1993 | $release = "Debian $rel"; | ||
2010 | 1994 | if ( -f "/etc/apt/sources.list" ) { | ||
2011 | 1995 | chomp(my $code_name = `awk '/^deb/ {print \$3}' /etc/apt/sources.list | awk -F/ '{print \$1}'| awk 'BEGIN {FS="|"} {print \$1}' | sort | uniq -c | sort -rn | head -n1 | awk '{print \$2}'`); | ||
2012 | 1996 | $release .= " ($code_name)" if $code_name; | ||
2013 | 1997 | } | ||
2014 | 1998 | } | ||
2015 | 1999 | elsif ( -f "/etc/os-release" ) { # openSUSE | ||
2016 | 2000 | chomp($release = `grep PRETTY_NAME /etc/os-release`); | ||
2017 | 2001 | $release =~ s/^PRETTY_NAME="(.+)"$/$1/; | ||
2018 | 2002 | } | ||
2019 | 2003 | elsif ( `ls /etc/*release 2>/dev/null` ) { | ||
2020 | 2004 | if ( `grep DISTRIB_DESCRIPTION /etc/*release 2>/dev/null` ) { | ||
2021 | 2005 | $release = `grep DISTRIB_DESCRIPTION /etc/*release | head -n1`; | ||
2022 | 2006 | } | ||
2023 | 2007 | else { | ||
2024 | 2008 | $release = `cat /etc/*release | head -n1`; | ||
2025 | 2009 | } | ||
2026 | 2010 | } | ||
2027 | 2011 | } | ||
2028 | 2012 | elsif ( $platform =~ m/(?:BSD|^Darwin)$/ ) { | ||
2029 | 2013 | my $rel = `uname -r`; | ||
2030 | 2014 | $release = "$platform $rel"; | ||
2031 | 2015 | } | ||
2032 | 2016 | elsif ( $platform eq "SunOS" ) { | ||
2033 | 2017 | my $rel = `head -n1 /etc/release` || `uname -r`; | ||
2034 | 2018 | $release = "$platform $rel"; | ||
2035 | 2019 | } | ||
2036 | 2020 | |||
2037 | 2021 | if ( !$release ) { | ||
2038 | 2022 | PTDEBUG && _d('Failed to get the release, using platform'); | ||
2039 | 2023 | $release = $platform; | ||
2040 | 2024 | } | ||
2041 | 2025 | chomp($release); | ||
2042 | 2026 | |||
2043 | 2027 | $release =~ s/^"|"$//g; | ||
2044 | 2028 | |||
2045 | 2029 | PTDEBUG && _d('OS version =', $release); | ||
2046 | 2030 | return $release; | ||
2047 | 2031 | } | ||
2048 | 2032 | |||
2049 | 2033 | sub get_perl_version { | ||
2050 | 2034 | my (%args) = @_; | ||
2051 | 2035 | my $item = $args{item}; | ||
2052 | 2036 | return unless $item; | ||
2053 | 2037 | |||
2054 | 2038 | my $version = sprintf '%vd', $PERL_VERSION; | ||
2055 | 2039 | PTDEBUG && _d('Perl version', $version); | ||
2056 | 2040 | return $version; | ||
2057 | 2041 | } | ||
2058 | 2042 | |||
2059 | 2043 | sub get_perl_module_version { | ||
2060 | 2044 | my (%args) = @_; | ||
2061 | 2045 | my $item = $args{item}; | ||
2062 | 2046 | return unless $item; | ||
2063 | 2047 | |||
2064 | 2048 | my $var = '$' . $item->{item} . '::VERSION'; | ||
2065 | 2049 | my $version = eval "use $item->{item}; $var;"; | ||
2066 | 2050 | PTDEBUG && _d('Perl version for', $var, '=', $version); | ||
2067 | 2051 | return $version; | ||
2068 | 2052 | } | ||
2069 | 2053 | |||
2070 | 2054 | sub get_mysql_variable { | ||
2071 | 2055 | return get_from_mysql( | ||
2072 | 2056 | show => 'VARIABLES', | ||
2073 | 2057 | @_, | ||
2074 | 2058 | ); | ||
2075 | 2059 | } | ||
2076 | 2060 | |||
2077 | 2061 | sub get_from_mysql { | ||
2078 | 2062 | my (%args) = @_; | ||
2079 | 2063 | my $show = $args{show}; | ||
2080 | 2064 | my $item = $args{item}; | ||
2081 | 2065 | my $instances = $args{instances}; | ||
2082 | 2066 | return unless $show && $item; | ||
2083 | 2067 | |||
2084 | 2068 | if ( !$instances || !@$instances ) { | ||
2085 | 2069 | PTDEBUG && _d('Cannot check', $item, | ||
2086 | 2070 | 'because there are no MySQL instances'); | ||
2087 | 2071 | return; | ||
2088 | 2072 | } | ||
2089 | 2073 | |||
2090 | 2074 | my @versions; | ||
2091 | 2075 | my %version_for; | ||
2092 | 2076 | foreach my $instance ( @$instances ) { | ||
2093 | 2077 | next unless $instance->{id}; # special system instance has id=0 | ||
2094 | 2078 | my $dbh = $instance->{dbh}; | ||
2095 | 2079 | local $dbh->{FetchHashKeyName} = 'NAME_lc'; | ||
2096 | 2080 | my $sql = qq/SHOW $show/; | ||
2097 | 2081 | PTDEBUG && _d($sql); | ||
2098 | 2082 | my $rows = $dbh->selectall_hashref($sql, 'variable_name'); | ||
2099 | 2083 | |||
2100 | 2084 | my @versions; | ||
2101 | 2085 | foreach my $var ( @{$item->{vars}} ) { | ||
2102 | 2086 | $var = lc($var); | ||
2103 | 2087 | my $version = $rows->{$var}->{value}; | ||
2104 | 2088 | PTDEBUG && _d('MySQL version for', $item->{item}, '=', $version, | ||
2105 | 2089 | 'on', $instance->{name}); | ||
2106 | 2090 | push @versions, $version; | ||
2107 | 2091 | } | ||
2108 | 2092 | $version_for{ $instance->{id} } = join(' ', @versions); | ||
2109 | 2093 | } | ||
2110 | 2094 | |||
2111 | 2095 | return \%version_for; | ||
2112 | 2096 | } | ||
2113 | 2097 | |||
2114 | 2098 | sub _d { | ||
2115 | 2099 | my ($package, undef, $line) = caller 0; | ||
2116 | 2100 | @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } | ||
2117 | 2101 | map { defined $_ ? $_ : 'undef' } | ||
2118 | 2102 | @_; | ||
2119 | 2103 | print STDERR "# $package:$line $PID ", join(' ', @_), "\n"; | ||
2120 | 2104 | } | ||
2121 | 2105 | |||
2122 | 2106 | 1; | ||
2123 | 2107 | } | ||
2124 | 2108 | # ########################################################################### | ||
2125 | 2109 | # End VersionCheck package | ||
2126 | 2110 | # ########################################################################### | ||
2127 | 2111 | |||
2128 | 2112 | # ########################################################################### | ||
2129 | 2113 | # DSNParser package | ||
2130 | 2114 | # This package is a copy without comments from the original. The original | ||
2131 | 2115 | # with comments and its test file can be found in the Bazaar repository at, | ||
2132 | 2116 | # lib/DSNParser.pm | ||
2133 | 2117 | # t/lib/DSNParser.t | ||
2134 | 2118 | # See https://launchpad.net/percona-toolkit for more information. | ||
2135 | 2119 | # ########################################################################### | ||
2136 | 2120 | { | ||
2137 | 2121 | package DSNParser; | ||
2138 | 2122 | |||
2139 | 2123 | use strict; | ||
2140 | 2124 | use warnings FATAL => 'all'; | ||
2141 | 2125 | use English qw(-no_match_vars); | ||
2142 | 2126 | use constant PTDEBUG => $ENV{PTDEBUG} || 0; | ||
2143 | 2127 | |||
2144 | 2128 | use Data::Dumper; | ||
2145 | 2129 | $Data::Dumper::Indent = 0; | ||
2146 | 2130 | $Data::Dumper::Quotekeys = 0; | ||
2147 | 2131 | |||
2148 | 2132 | my $dsn_sep = qr/(?<!\\),/; | ||
2149 | 2133 | |||
2150 | 2134 | eval { | ||
2151 | 2135 | require DBI; | ||
2152 | 2136 | }; | ||
2153 | 2137 | my $have_dbi = $EVAL_ERROR ? 0 : 1; | ||
2154 | 2138 | |||
2155 | 2139 | sub new { | ||
2156 | 2140 | my ( $class, %args ) = @_; | ||
2157 | 2141 | foreach my $arg ( qw(opts) ) { | ||
2158 | 2142 | die "I need a $arg argument" unless $args{$arg}; | ||
2159 | 2143 | } | ||
2160 | 2144 | my $self = { | ||
2161 | 2145 | opts => {} # h, P, u, etc. Should come from DSN OPTIONS section in POD. | ||
2162 | 2146 | }; | ||
2163 | 2147 | foreach my $opt ( @{$args{opts}} ) { | ||
2164 | 2148 | if ( !$opt->{key} || !$opt->{desc} ) { | ||
2165 | 2149 | die "Invalid DSN option: ", Dumper($opt); | ||
2166 | 2150 | } | ||
2167 | 2151 | PTDEBUG && _d('DSN option:', | ||
2168 | 2152 | join(', ', | ||
2169 | 2153 | map { "$_=" . (defined $opt->{$_} ? ($opt->{$_} || '') : 'undef') } | ||
2170 | 2154 | keys %$opt | ||
2171 | 2155 | ) | ||
2172 | 2156 | ); | ||
2173 | 2157 | $self->{opts}->{$opt->{key}} = { | ||
2174 | 2158 | dsn => $opt->{dsn}, | ||
2175 | 2159 | desc => $opt->{desc}, | ||
2176 | 2160 | copy => $opt->{copy} || 0, | ||
2177 | 2161 | }; | ||
2178 | 2162 | } | ||
2179 | 2163 | return bless $self, $class; | ||
2180 | 2164 | } | ||
2181 | 2165 | |||
2182 | 2166 | sub prop { | ||
2183 | 2167 | my ( $self, $prop, $value ) = @_; | ||
2184 | 2168 | if ( @_ > 2 ) { | ||
2185 | 2169 | PTDEBUG && _d('Setting', $prop, 'property'); | ||
2186 | 2170 | $self->{$prop} = $value; | ||
2187 | 2171 | } | ||
2188 | 2172 | return $self->{$prop}; | ||
2189 | 2173 | } | ||
2190 | 2174 | |||
2191 | 2175 | sub parse { | ||
2192 | 2176 | my ( $self, $dsn, $prev, $defaults ) = @_; | ||
2193 | 2177 | if ( !$dsn ) { | ||
2194 | 2178 | PTDEBUG && _d('No DSN to parse'); | ||
2195 | 2179 | return; | ||
2196 | 2180 | } | ||
2197 | 2181 | PTDEBUG && _d('Parsing', $dsn); | ||
2198 | 2182 | $prev ||= {}; | ||
2199 | 2183 | $defaults ||= {}; | ||
2200 | 2184 | my %given_props; | ||
2201 | 2185 | my %final_props; | ||
2202 | 2186 | my $opts = $self->{opts}; | ||
2203 | 2187 | |||
2204 | 2188 | foreach my $dsn_part ( split($dsn_sep, $dsn) ) { | ||
2205 | 2189 | $dsn_part =~ s/\\,/,/g; | ||
2206 | 2190 | if ( my ($prop_key, $prop_val) = $dsn_part =~ m/^(.)=(.*)$/ ) { | ||
2207 | 2191 | $given_props{$prop_key} = $prop_val; | ||
2208 | 2192 | } | ||
2209 | 2193 | else { | ||
2210 | 2194 | PTDEBUG && _d('Interpreting', $dsn_part, 'as h=', $dsn_part); | ||
2211 | 2195 | $given_props{h} = $dsn_part; | ||
2212 | 2196 | } | ||
2213 | 2197 | } | ||
2214 | 2198 | |||
2215 | 2199 | foreach my $key ( keys %$opts ) { | ||
2216 | 2200 | PTDEBUG && _d('Finding value for', $key); | ||
2217 | 2201 | $final_props{$key} = $given_props{$key}; | ||
2218 | 2202 | if ( !defined $final_props{$key} | ||
2219 | 2203 | && defined $prev->{$key} && $opts->{$key}->{copy} ) | ||
2220 | 2204 | { | ||
2221 | 2205 | $final_props{$key} = $prev->{$key}; | ||
2222 | 2206 | PTDEBUG && _d('Copying value for', $key, 'from previous DSN'); | ||
2223 | 2207 | } | ||
2224 | 2208 | if ( !defined $final_props{$key} ) { | ||
2225 | 2209 | $final_props{$key} = $defaults->{$key}; | ||
2226 | 2210 | PTDEBUG && _d('Copying value for', $key, 'from defaults'); | ||
2227 | 2211 | } | ||
2228 | 2212 | } | ||
2229 | 2213 | |||
2230 | 2214 | foreach my $key ( keys %given_props ) { | ||
2231 | 2215 | die "Unknown DSN option '$key' in '$dsn'. For more details, " | ||
2232 | 2216 | . "please use the --help option, or try 'perldoc $PROGRAM_NAME' " | ||
2233 | 2217 | . "for complete documentation." | ||
2234 | 2218 | unless exists $opts->{$key}; | ||
2235 | 2219 | } | ||
2236 | 2220 | if ( (my $required = $self->prop('required')) ) { | ||
2237 | 2221 | foreach my $key ( keys %$required ) { | ||
2238 | 2222 | die "Missing required DSN option '$key' in '$dsn'. For more details, " | ||
2239 | 2223 | . "please use the --help option, or try 'perldoc $PROGRAM_NAME' " | ||
2240 | 2224 | . "for complete documentation." | ||
2241 | 2225 | unless $final_props{$key}; | ||
2242 | 2226 | } | ||
2243 | 2227 | } | ||
2244 | 2228 | |||
2245 | 2229 | return \%final_props; | ||
2246 | 2230 | } | ||
2247 | 2231 | |||
2248 | 2232 | sub parse_options { | ||
2249 | 2233 | my ( $self, $o ) = @_; | ||
2250 | 2234 | die 'I need an OptionParser object' unless ref $o eq 'OptionParser'; | ||
2251 | 2235 | my $dsn_string | ||
2252 | 2236 | = join(',', | ||
2253 | 2237 | map { "$_=".$o->get($_); } | ||
2254 | 2238 | grep { $o->has($_) && $o->get($_) } | ||
2255 | 2239 | keys %{$self->{opts}} | ||
2256 | 2240 | ); | ||
2257 | 2241 | PTDEBUG && _d('DSN string made from options:', $dsn_string); | ||
2258 | 2242 | return $self->parse($dsn_string); | ||
2259 | 2243 | } | ||
2260 | 2244 | |||
2261 | 2245 | sub as_string { | ||
2262 | 2246 | my ( $self, $dsn, $props ) = @_; | ||
2263 | 2247 | return $dsn unless ref $dsn; | ||
2264 | 2248 | my @keys = $props ? @$props : sort keys %$dsn; | ||
2265 | 2249 | return join(',', | ||
2266 | 2250 | map { "$_=" . ($_ eq 'p' ? '...' : $dsn->{$_}) } | ||
2267 | 2251 | grep { | ||
2268 | 2252 | exists $self->{opts}->{$_} | ||
2269 | 2253 | && exists $dsn->{$_} | ||
2270 | 2254 | && defined $dsn->{$_} | ||
2271 | 2255 | } @keys); | ||
2272 | 2256 | } | ||
2273 | 2257 | |||
2274 | 2258 | sub usage { | ||
2275 | 2259 | my ( $self ) = @_; | ||
2276 | 2260 | my $usage | ||
2277 | 2261 | = "DSN syntax is key=value[,key=value...] Allowable DSN keys:\n\n" | ||
2278 | 2262 | . " KEY COPY MEANING\n" | ||
2279 | 2263 | . " === ==== =============================================\n"; | ||
2280 | 2264 | my %opts = %{$self->{opts}}; | ||
2281 | 2265 | foreach my $key ( sort keys %opts ) { | ||
2282 | 2266 | $usage .= " $key " | ||
2283 | 2267 | . ($opts{$key}->{copy} ? 'yes ' : 'no ') | ||
2284 | 2268 | . ($opts{$key}->{desc} || '[No description]') | ||
2285 | 2269 | . "\n"; | ||
2286 | 2270 | } | ||
2287 | 2271 | $usage .= "\n If the DSN is a bareword, the word is treated as the 'h' key.\n"; | ||
2288 | 2272 | return $usage; | ||
2289 | 2273 | } | ||
2290 | 2274 | |||
2291 | 2275 | sub get_cxn_params { | ||
2292 | 2276 | my ( $self, $info ) = @_; | ||
2293 | 2277 | my $dsn; | ||
2294 | 2278 | my %opts = %{$self->{opts}}; | ||
2295 | 2279 | my $driver = $self->prop('dbidriver') || ''; | ||
2296 | 2280 | if ( $driver eq 'Pg' ) { | ||
2297 | 2281 | $dsn = 'DBI:Pg:dbname=' . ( $info->{D} || '' ) . ';' | ||
2298 | 2282 | . join(';', map { "$opts{$_}->{dsn}=$info->{$_}" } | ||
2299 | 2283 | grep { defined $info->{$_} } | ||
2300 | 2284 | qw(h P)); | ||
2301 | 2285 | } | ||
2302 | 2286 | else { | ||
2303 | 2287 | $dsn = 'DBI:mysql:' . ( $info->{D} || '' ) . ';' | ||
2304 | 2288 | . join(';', map { "$opts{$_}->{dsn}=$info->{$_}" } | ||
2305 | 2289 | grep { defined $info->{$_} } | ||
2306 | 2290 | qw(F h P S A)) | ||
2307 | 2291 | . ';mysql_read_default_group=client' | ||
2308 | 2292 | . ($info->{L} ? ';mysql_local_infile=1' : ''); | ||
2309 | 2293 | } | ||
2310 | 2294 | PTDEBUG && _d($dsn); | ||
2311 | 2295 | return ($dsn, $info->{u}, $info->{p}); | ||
2312 | 2296 | } | ||
2313 | 2297 | |||
2314 | 2298 | sub fill_in_dsn { | ||
2315 | 2299 | my ( $self, $dbh, $dsn ) = @_; | ||
2316 | 2300 | my $vars = $dbh->selectall_hashref('SHOW VARIABLES', 'Variable_name'); | ||
2317 | 2301 | my ($user, $db) = $dbh->selectrow_array('SELECT USER(), DATABASE()'); | ||
2318 | 2302 | $user =~ s/@.*//; | ||
2319 | 2303 | $dsn->{h} ||= $vars->{hostname}->{Value}; | ||
2320 | 2304 | $dsn->{S} ||= $vars->{'socket'}->{Value}; | ||
2321 | 2305 | $dsn->{P} ||= $vars->{port}->{Value}; | ||
2322 | 2306 | $dsn->{u} ||= $user; | ||
2323 | 2307 | $dsn->{D} ||= $db; | ||
2324 | 2308 | } | ||
2325 | 2309 | |||
2326 | 2310 | sub get_dbh { | ||
2327 | 2311 | my ( $self, $cxn_string, $user, $pass, $opts ) = @_; | ||
2328 | 2312 | $opts ||= {}; | ||
2329 | 2313 | my $defaults = { | ||
2330 | 2314 | AutoCommit => 0, | ||
2331 | 2315 | RaiseError => 1, | ||
2332 | 2316 | PrintError => 0, | ||
2333 | 2317 | ShowErrorStatement => 1, | ||
2334 | 2318 | mysql_enable_utf8 => ($cxn_string =~ m/charset=utf8/i ? 1 : 0), | ||
2335 | 2319 | }; | ||
2336 | 2320 | @{$defaults}{ keys %$opts } = values %$opts; | ||
2337 | 2321 | if (delete $defaults->{L}) { # L for LOAD DATA LOCAL INFILE, our own extension | ||
2338 | 2322 | $defaults->{mysql_local_infile} = 1; | ||
2339 | 2323 | } | ||
2340 | 2324 | |||
2341 | 2325 | if ( $opts->{mysql_use_result} ) { | ||
2342 | 2326 | $defaults->{mysql_use_result} = 1; | ||
2343 | 2327 | } | ||
2344 | 2328 | |||
2345 | 2329 | if ( !$have_dbi ) { | ||
2346 | 2330 | die "Cannot connect to MySQL because the Perl DBI module is not " | ||
2347 | 2331 | . "installed or not found. Run 'perl -MDBI' to see the directories " | ||
2348 | 2332 | . "that Perl searches for DBI. If DBI is not installed, try:\n" | ||
2349 | 2333 | . " Debian/Ubuntu apt-get install libdbi-perl\n" | ||
2350 | 2334 | . " RHEL/CentOS yum install perl-DBI\n" | ||
2351 | 2335 | . " OpenSolaris pkg install pkg:/SUNWpmdbi\n"; | ||
2352 | 2336 | |||
2353 | 2337 | } | ||
2354 | 2338 | |||
2355 | 2339 | my $dbh; | ||
2356 | 2340 | my $tries = 2; | ||
2357 | 2341 | while ( !$dbh && $tries-- ) { | ||
2358 | 2342 | PTDEBUG && _d($cxn_string, ' ', $user, ' ', $pass, | ||
2359 | 2343 | join(', ', map { "$_=>$defaults->{$_}" } keys %$defaults )); | ||
2360 | 2344 | |||
2361 | 2345 | $dbh = eval { DBI->connect($cxn_string, $user, $pass, $defaults) }; | ||
2362 | 2346 | |||
2363 | 2347 | if ( !$dbh && $EVAL_ERROR ) { | ||
2364 | 2348 | if ( $EVAL_ERROR =~ m/locate DBD\/mysql/i ) { | ||
2365 | 2349 | die "Cannot connect to MySQL because the Perl DBD::mysql module is " | ||
2366 | 2350 | . "not installed or not found. Run 'perl -MDBD::mysql' to see " | ||
2367 | 2351 | . "the directories that Perl searches for DBD::mysql. If " | ||
2368 | 2352 | . "DBD::mysql is not installed, try:\n" | ||
2369 | 2353 | . " Debian/Ubuntu apt-get install libdbd-mysql-perl\n" | ||
2370 | 2354 | . " RHEL/CentOS yum install perl-DBD-MySQL\n" | ||
2371 | 2355 | . " OpenSolaris pgk install pkg:/SUNWapu13dbd-mysql\n"; | ||
2372 | 2356 | } | ||
2373 | 2357 | elsif ( $EVAL_ERROR =~ m/not a compiled character set|character set utf8/ ) { | ||
2374 | 2358 | PTDEBUG && _d('Going to try again without utf8 support'); | ||
2375 | 2359 | delete $defaults->{mysql_enable_utf8}; | ||
2376 | 2360 | } | ||
2377 | 2361 | if ( !$tries ) { | ||
2378 | 2362 | die $EVAL_ERROR; | ||
2379 | 2363 | } | ||
2380 | 2364 | } | ||
2381 | 2365 | } | ||
2382 | 2366 | |||
2383 | 2367 | if ( $cxn_string =~ m/mysql/i ) { | ||
2384 | 2368 | my $sql; | ||
2385 | 2369 | |||
2386 | 2370 | $sql = 'SELECT @@SQL_MODE'; | ||
2387 | 2371 | PTDEBUG && _d($dbh, $sql); | ||
2388 | 2372 | my ($sql_mode) = eval { $dbh->selectrow_array($sql) }; | ||
2389 | 2373 | if ( $EVAL_ERROR ) { | ||
2390 | 2374 | die "Error getting the current SQL_MODE: $EVAL_ERROR"; | ||
2391 | 2375 | } | ||
2392 | 2376 | |||
2393 | 2377 | if ( my ($charset) = $cxn_string =~ m/charset=([\w]+)/ ) { | ||
2394 | 2378 | $sql = qq{/*!40101 SET NAMES "$charset"*/}; | ||
2395 | 2379 | PTDEBUG && _d($dbh, $sql); | ||
2396 | 2380 | eval { $dbh->do($sql) }; | ||
2397 | 2381 | if ( $EVAL_ERROR ) { | ||
2398 | 2382 | die "Error setting NAMES to $charset: $EVAL_ERROR"; | ||
2399 | 2383 | } | ||
2400 | 2384 | PTDEBUG && _d('Enabling charset for STDOUT'); | ||
2401 | 2385 | if ( $charset eq 'utf8' ) { | ||
2402 | 2386 | binmode(STDOUT, ':utf8') | ||
2403 | 2387 | or die "Can't binmode(STDOUT, ':utf8'): $OS_ERROR"; | ||
2404 | 2388 | } | ||
2405 | 2389 | else { | ||
2406 | 2390 | binmode(STDOUT) or die "Can't binmode(STDOUT): $OS_ERROR"; | ||
2407 | 2391 | } | ||
2408 | 2392 | } | ||
2409 | 2393 | |||
2410 | 2394 | if ( my $vars = $self->prop('set-vars') ) { | ||
2411 | 2395 | $self->set_vars($dbh, $vars); | ||
2412 | 2396 | } | ||
2413 | 2397 | |||
2414 | 2398 | $sql = 'SET @@SQL_QUOTE_SHOW_CREATE = 1' | ||
2415 | 2399 | . '/*!40101, @@SQL_MODE=\'NO_AUTO_VALUE_ON_ZERO' | ||
2416 | 2400 | . ($sql_mode ? ",$sql_mode" : '') | ||
2417 | 2401 | . '\'*/'; | ||
2418 | 2402 | PTDEBUG && _d($dbh, $sql); | ||
2419 | 2403 | eval { $dbh->do($sql) }; | ||
2420 | 2404 | if ( $EVAL_ERROR ) { | ||
2421 | 2405 | die "Error setting SQL_QUOTE_SHOW_CREATE, SQL_MODE" | ||
2422 | 2406 | . ($sql_mode ? " and $sql_mode" : '') | ||
2423 | 2407 | . ": $EVAL_ERROR"; | ||
2424 | 2408 | } | ||
2425 | 2409 | } | ||
2426 | 2410 | |||
2427 | 2411 | PTDEBUG && _d('DBH info: ', | ||
2428 | 2412 | $dbh, | ||
2429 | 2413 | Dumper($dbh->selectrow_hashref( | ||
2430 | 2414 | 'SELECT DATABASE(), CONNECTION_ID(), VERSION()/*!50038 , @@hostname*/')), | ||
2431 | 2415 | 'Connection info:', $dbh->{mysql_hostinfo}, | ||
2432 | 2416 | 'Character set info:', Dumper($dbh->selectall_arrayref( | ||
2433 | 2417 | "SHOW VARIABLES LIKE 'character_set%'", { Slice => {}})), | ||
2434 | 2418 | '$DBD::mysql::VERSION:', $DBD::mysql::VERSION, | ||
2435 | 2419 | '$DBI::VERSION:', $DBI::VERSION, | ||
2436 | 2420 | ); | ||
2437 | 2421 | |||
2438 | 2422 | return $dbh; | ||
2439 | 2423 | } | ||
2440 | 2424 | |||
2441 | 2425 | sub get_hostname { | ||
2442 | 2426 | my ( $self, $dbh ) = @_; | ||
2443 | 2427 | if ( my ($host) = ($dbh->{mysql_hostinfo} || '') =~ m/^(\w+) via/ ) { | ||
2444 | 2428 | return $host; | ||
2445 | 2429 | } | ||
2446 | 2430 | my ( $hostname, $one ) = $dbh->selectrow_array( | ||
2447 | 2431 | 'SELECT /*!50038 @@hostname, */ 1'); | ||
2448 | 2432 | return $hostname; | ||
2449 | 2433 | } | ||
2450 | 2434 | |||
2451 | 2435 | sub disconnect { | ||
2452 | 2436 | my ( $self, $dbh ) = @_; | ||
2453 | 2437 | PTDEBUG && $self->print_active_handles($dbh); | ||
2454 | 2438 | $dbh->disconnect; | ||
2455 | 2439 | } | ||
2456 | 2440 | |||
2457 | 2441 | sub print_active_handles { | ||
2458 | 2442 | my ( $self, $thing, $level ) = @_; | ||
2459 | 2443 | $level ||= 0; | ||
2460 | 2444 | printf("# Active %sh: %s %s %s\n", ($thing->{Type} || 'undef'), "\t" x $level, | ||
2461 | 2445 | $thing, (($thing->{Type} || '') eq 'st' ? $thing->{Statement} || '' : '')) | ||
2462 | 2446 | or die "Cannot print: $OS_ERROR"; | ||
2463 | 2447 | foreach my $handle ( grep {defined} @{ $thing->{ChildHandles} } ) { | ||
2464 | 2448 | $self->print_active_handles( $handle, $level + 1 ); | ||
2465 | 2449 | } | ||
2466 | 2450 | } | ||
2467 | 2451 | |||
2468 | 2452 | sub copy { | ||
2469 | 2453 | my ( $self, $dsn_1, $dsn_2, %args ) = @_; | ||
2470 | 2454 | die 'I need a dsn_1 argument' unless $dsn_1; | ||
2471 | 2455 | die 'I need a dsn_2 argument' unless $dsn_2; | ||
2472 | 2456 | my %new_dsn = map { | ||
2473 | 2457 | my $key = $_; | ||
2474 | 2458 | my $val; | ||
2475 | 2459 | if ( $args{overwrite} ) { | ||
2476 | 2460 | $val = defined $dsn_1->{$key} ? $dsn_1->{$key} : $dsn_2->{$key}; | ||
2477 | 2461 | } | ||
2478 | 2462 | else { | ||
2479 | 2463 | $val = defined $dsn_2->{$key} ? $dsn_2->{$key} : $dsn_1->{$key}; | ||
2480 | 2464 | } | ||
2481 | 2465 | $key => $val; | ||
2482 | 2466 | } keys %{$self->{opts}}; | ||
2483 | 2467 | return \%new_dsn; | ||
2484 | 2468 | } | ||
2485 | 2469 | |||
2486 | 2470 | sub set_vars { | ||
2487 | 2471 | my ($self, $dbh, $vars) = @_; | ||
2488 | 2472 | |||
2489 | 2473 | return unless $vars; | ||
2490 | 2474 | |||
2491 | 2475 | foreach my $var ( sort keys %$vars ) { | ||
2492 | 2476 | my $val = $vars->{$var}->{val}; | ||
2493 | 2477 | |||
2494 | 2478 | (my $quoted_var = $var) =~ s/_/\\_/; | ||
2495 | 2479 | my ($var_exists, $current_val); | ||
2496 | 2480 | eval { | ||
2497 | 2481 | ($var_exists, $current_val) = $dbh->selectrow_array( | ||
2498 | 2482 | "SHOW VARIABLES LIKE '$quoted_var'"); | ||
2499 | 2483 | }; | ||
2500 | 2484 | my $e = $EVAL_ERROR; | ||
2501 | 2485 | if ( $e ) { | ||
2502 | 2486 | PTDEBUG && _d($e); | ||
2503 | 2487 | } | ||
2504 | 2488 | |||
2505 | 2489 | if ( $vars->{$var}->{default} && !$var_exists ) { | ||
2506 | 2490 | PTDEBUG && _d('Not setting default var', $var, | ||
2507 | 2491 | 'because it does not exist'); | ||
2508 | 2492 | next; | ||
2509 | 2493 | } | ||
2510 | 2494 | |||
2511 | 2495 | if ( $current_val && $current_val eq $val ) { | ||
2512 | 2496 | PTDEBUG && _d('Not setting var', $var, 'because its value', | ||
2513 | 2497 | 'is already', $val); | ||
2514 | 2498 | next; | ||
2515 | 2499 | } | ||
2516 | 2500 | |||
2517 | 2501 | my $sql = "SET SESSION $var=$val"; | ||
2518 | 2502 | PTDEBUG && _d($dbh, $sql); | ||
2519 | 2503 | eval { $dbh->do($sql) }; | ||
2520 | 2504 | if ( my $set_error = $EVAL_ERROR ) { | ||
2521 | 2505 | chomp($set_error); | ||
2522 | 2506 | $set_error =~ s/ at \S+ line \d+//; | ||
2523 | 2507 | my $msg = "Error setting $var: $set_error"; | ||
2524 | 2508 | if ( $current_val ) { | ||
2525 | 2509 | $msg .= " The current value for $var is $current_val. " | ||
2526 | 2510 | . "If the variable is read only (not dynamic), specify " | ||
2527 | 2511 | . "--set-vars $var=$current_val to avoid this warning, " | ||
2528 | 2512 | . "else manually set the variable and restart MySQL."; | ||
2529 | 2513 | } | ||
2530 | 2514 | warn $msg . "\n\n"; | ||
2531 | 2515 | } | ||
2532 | 2516 | } | ||
2533 | 2517 | |||
2534 | 2518 | return; | ||
2535 | 2519 | } | ||
2536 | 2520 | |||
2537 | 2521 | sub _d { | ||
2538 | 2522 | my ($package, undef, $line) = caller 0; | ||
2539 | 2523 | @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } | ||
2540 | 2524 | map { defined $_ ? $_ : 'undef' } | ||
2541 | 2525 | @_; | ||
2542 | 2526 | print STDERR "# $package:$line $PID ", join(' ', @_), "\n"; | ||
2543 | 2527 | } | ||
2544 | 2528 | |||
2545 | 2529 | 1; | ||
2546 | 2530 | } | ||
2547 | 2531 | # ########################################################################### | ||
2548 | 2532 | # End DSNParser package | ||
2549 | 2533 | # ########################################################################### | ||
2550 | 2534 | |||
2551 | 2535 | # ########################################################################### | ||
2552 | 2536 | # OptionParser package | ||
2553 | 2537 | # This package is a copy without comments from the original. The original | ||
2554 | 2538 | # with comments and its test file can be found in the Bazaar repository at, | ||
2555 | 2539 | # lib/OptionParser.pm | ||
2556 | 2540 | # t/lib/OptionParser.t | ||
2557 | 2541 | # See https://launchpad.net/percona-toolkit for more information. | ||
2558 | 2542 | # ########################################################################### | ||
2559 | 2543 | { | ||
2560 | 2544 | package OptionParser; | ||
2561 | 2545 | |||
2562 | 2546 | use strict; | ||
2563 | 2547 | use warnings FATAL => 'all'; | ||
2564 | 2548 | use English qw(-no_match_vars); | ||
2565 | 2549 | use constant PTDEBUG => $ENV{PTDEBUG} || 0; | ||
2566 | 2550 | |||
2567 | 2551 | use List::Util qw(max); | ||
2568 | 2552 | use Getopt::Long; | ||
2569 | 2553 | use Data::Dumper; | ||
2570 | 2554 | |||
2571 | 2555 | my $POD_link_re = '[LC]<"?([^">]+)"?>'; | ||
2572 | 2556 | |||
2573 | 2557 | sub new { | ||
2574 | 2558 | my ( $class, %args ) = @_; | ||
2575 | 2559 | my @required_args = qw(); | ||
2576 | 2560 | foreach my $arg ( @required_args ) { | ||
2577 | 2561 | die "I need a $arg argument" unless $args{$arg}; | ||
2578 | 2562 | } | ||
2579 | 2563 | |||
2580 | 2564 | my ($program_name) = $PROGRAM_NAME =~ m/([.A-Za-z-]+)$/; | ||
2581 | 2565 | $program_name ||= $PROGRAM_NAME; | ||
2582 | 2566 | my $home = $ENV{HOME} || $ENV{HOMEPATH} || $ENV{USERPROFILE} || '.'; | ||
2583 | 2567 | |||
2584 | 2568 | my %attributes = ( | ||
2585 | 2569 | 'type' => 1, | ||
2586 | 2570 | 'short form' => 1, | ||
2587 | 2571 | 'group' => 1, | ||
2588 | 2572 | 'default' => 1, | ||
2589 | 2573 | 'cumulative' => 1, | ||
2590 | 2574 | 'negatable' => 1, | ||
2591 | 2575 | ); | ||
2592 | 2576 | |||
2593 | 2577 | my $self = { | ||
2594 | 2578 | head1 => 'OPTIONS', # These args are used internally | ||
2595 | 2579 | skip_rules => 0, # to instantiate another Option- | ||
2596 | 2580 | item => '--(.*)', # Parser obj that parses the | ||
2597 | 2581 | attributes => \%attributes, # DSN OPTIONS section. Tools | ||
2598 | 2582 | parse_attributes => \&_parse_attribs, # don't tinker with these args. | ||
2599 | 2583 | |||
2600 | 2584 | %args, | ||
2601 | 2585 | |||
2602 | 2586 | strict => 1, # disabled by a special rule | ||
2603 | 2587 | program_name => $program_name, | ||
2604 | 2588 | opts => {}, | ||
2605 | 2589 | got_opts => 0, | ||
2606 | 2590 | short_opts => {}, | ||
2607 | 2591 | defaults => {}, | ||
2608 | 2592 | groups => {}, | ||
2609 | 2593 | allowed_groups => {}, | ||
2610 | 2594 | errors => [], | ||
2611 | 2595 | rules => [], # desc of rules for --help | ||
2612 | 2596 | mutex => [], # rule: opts are mutually exclusive | ||
2613 | 2597 | atleast1 => [], # rule: at least one opt is required | ||
2614 | 2598 | disables => {}, # rule: opt disables other opts | ||
2615 | 2599 | defaults_to => {}, # rule: opt defaults to value of other opt | ||
2616 | 2600 | DSNParser => undef, | ||
2617 | 2601 | default_files => [ | ||
2618 | 2602 | "/etc/percona-toolkit/percona-toolkit.conf", | ||
2619 | 2603 | "/etc/percona-toolkit/$program_name.conf", | ||
2620 | 2604 | "$home/.percona-toolkit.conf", | ||
2621 | 2605 | "$home/.$program_name.conf", | ||
2622 | 2606 | ], | ||
2623 | 2607 | types => { | ||
2624 | 2608 | string => 's', # standard Getopt type | ||
2625 | 2609 | int => 'i', # standard Getopt type | ||
2626 | 2610 | float => 'f', # standard Getopt type | ||
2627 | 2611 | Hash => 'H', # hash, formed from a comma-separated list | ||
2628 | 2612 | hash => 'h', # hash as above, but only if a value is given | ||
2629 | 2613 | Array => 'A', # array, similar to Hash | ||
2630 | 2614 | array => 'a', # array, similar to hash | ||
2631 | 2615 | DSN => 'd', # DSN | ||
2632 | 2616 | size => 'z', # size with kMG suffix (powers of 2^10) | ||
2633 | 2617 | time => 'm', # time, with an optional suffix of s/h/m/d | ||
2634 | 2618 | }, | ||
2635 | 2619 | }; | ||
2636 | 2620 | |||
2637 | 2621 | return bless $self, $class; | ||
2638 | 2622 | } | ||
2639 | 2623 | |||
2640 | 2624 | sub get_specs { | ||
2641 | 2625 | my ( $self, $file ) = @_; | ||
2642 | 2626 | $file ||= $self->{file} || __FILE__; | ||
2643 | 2627 | my @specs = $self->_pod_to_specs($file); | ||
2644 | 2628 | $self->_parse_specs(@specs); | ||
2645 | 2629 | |||
2646 | 2630 | open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; | ||
2647 | 2631 | my $contents = do { local $/ = undef; <$fh> }; | ||
2648 | 2632 | close $fh; | ||
2649 | 2633 | if ( $contents =~ m/^=head1 DSN OPTIONS/m ) { | ||
2650 | 2634 | PTDEBUG && _d('Parsing DSN OPTIONS'); | ||
2651 | 2635 | my $dsn_attribs = { | ||
2652 | 2636 | dsn => 1, | ||
2653 | 2637 | copy => 1, | ||
2654 | 2638 | }; | ||
2655 | 2639 | my $parse_dsn_attribs = sub { | ||
2656 | 2640 | my ( $self, $option, $attribs ) = @_; | ||
2657 | 2641 | map { | ||
2658 | 2642 | my $val = $attribs->{$_}; | ||
2659 | 2643 | if ( $val ) { | ||
2660 | 2644 | $val = $val eq 'yes' ? 1 | ||
2661 | 2645 | : $val eq 'no' ? 0 | ||
2662 | 2646 | : $val; | ||
2663 | 2647 | $attribs->{$_} = $val; | ||
2664 | 2648 | } | ||
2665 | 2649 | } keys %$attribs; | ||
2666 | 2650 | return { | ||
2667 | 2651 | key => $option, | ||
2668 | 2652 | %$attribs, | ||
2669 | 2653 | }; | ||
2670 | 2654 | }; | ||
2671 | 2655 | my $dsn_o = new OptionParser( | ||
2672 | 2656 | description => 'DSN OPTIONS', | ||
2673 | 2657 | head1 => 'DSN OPTIONS', | ||
2674 | 2658 | dsn => 0, # XXX don't infinitely recurse! | ||
2675 | 2659 | item => '\* (.)', # key opts are a single character | ||
2676 | 2660 | skip_rules => 1, # no rules before opts | ||
2677 | 2661 | attributes => $dsn_attribs, | ||
2678 | 2662 | parse_attributes => $parse_dsn_attribs, | ||
2679 | 2663 | ); | ||
2680 | 2664 | my @dsn_opts = map { | ||
2681 | 2665 | my $opts = { | ||
2682 | 2666 | key => $_->{spec}->{key}, | ||
2683 | 2667 | dsn => $_->{spec}->{dsn}, | ||
2684 | 2668 | copy => $_->{spec}->{copy}, | ||
2685 | 2669 | desc => $_->{desc}, | ||
2686 | 2670 | }; | ||
2687 | 2671 | $opts; | ||
2688 | 2672 | } $dsn_o->_pod_to_specs($file); | ||
2689 | 2673 | $self->{DSNParser} = DSNParser->new(opts => \@dsn_opts); | ||
2690 | 2674 | } | ||
2691 | 2675 | |||
2692 | 2676 | if ( $contents =~ m/^=head1 VERSION\n\n^(.+)$/m ) { | ||
2693 | 2677 | $self->{version} = $1; | ||
2694 | 2678 | PTDEBUG && _d($self->{version}); | ||
2695 | 2679 | } | ||
2696 | 2680 | |||
2697 | 2681 | return; | ||
2698 | 2682 | } | ||
2699 | 2683 | |||
2700 | 2684 | sub DSNParser { | ||
2701 | 2685 | my ( $self ) = @_; | ||
2702 | 2686 | return $self->{DSNParser}; | ||
2703 | 2687 | }; | ||
2704 | 2688 | |||
2705 | 2689 | sub get_defaults_files { | ||
2706 | 2690 | my ( $self ) = @_; | ||
2707 | 2691 | return @{$self->{default_files}}; | ||
2708 | 2692 | } | ||
2709 | 2693 | |||
2710 | 2694 | sub _pod_to_specs { | ||
2711 | 2695 | my ( $self, $file ) = @_; | ||
2712 | 2696 | $file ||= $self->{file} || __FILE__; | ||
2713 | 2697 | open my $fh, '<', $file or die "Cannot open $file: $OS_ERROR"; | ||
2714 | 2698 | |||
2715 | 2699 | my @specs = (); | ||
2716 | 2700 | my @rules = (); | ||
2717 | 2701 | my $para; | ||
2718 | 2702 | |||
2719 | 2703 | local $INPUT_RECORD_SEPARATOR = ''; | ||
2720 | 2704 | while ( $para = <$fh> ) { | ||
2721 | 2705 | next unless $para =~ m/^=head1 $self->{head1}/; | ||
2722 | 2706 | last; | ||
2723 | 2707 | } | ||
2724 | 2708 | |||
2725 | 2709 | while ( $para = <$fh> ) { | ||
2726 | 2710 | last if $para =~ m/^=over/; | ||
2727 | 2711 | next if $self->{skip_rules}; | ||
2728 | 2712 | chomp $para; | ||
2729 | 2713 | $para =~ s/\s+/ /g; | ||
2730 | 2714 | $para =~ s/$POD_link_re/$1/go; | ||
2731 | 2715 | PTDEBUG && _d('Option rule:', $para); | ||
2732 | 2716 | push @rules, $para; | ||
2733 | 2717 | } | ||
2734 | 2718 | |||
2735 | 2719 | die "POD has no $self->{head1} section" unless $para; | ||
2736 | 2720 | |||
2737 | 2721 | do { | ||
2738 | 2722 | if ( my ($option) = $para =~ m/^=item $self->{item}/ ) { | ||
2739 | 2723 | chomp $para; | ||
2740 | 2724 | PTDEBUG && _d($para); | ||
2741 | 2725 | my %attribs; | ||
2742 | 2726 | |||
2743 | 2727 | $para = <$fh>; # read next paragraph, possibly attributes | ||
2744 | 2728 | |||
2745 | 2729 | if ( $para =~ m/: / ) { # attributes | ||
2746 | 2730 | $para =~ s/\s+\Z//g; | ||
2747 | 2731 | %attribs = map { | ||
2748 | 2732 | my ( $attrib, $val) = split(/: /, $_); | ||
2749 | 2733 | die "Unrecognized attribute for --$option: $attrib" | ||
2750 | 2734 | unless $self->{attributes}->{$attrib}; | ||
2751 | 2735 | ($attrib, $val); | ||
2752 | 2736 | } split(/; /, $para); | ||
2753 | 2737 | if ( $attribs{'short form'} ) { | ||
2754 | 2738 | $attribs{'short form'} =~ s/-//; | ||
2755 | 2739 | } | ||
2756 | 2740 | $para = <$fh>; # read next paragraph, probably short help desc | ||
2757 | 2741 | } | ||
2758 | 2742 | else { | ||
2759 | 2743 | PTDEBUG && _d('Option has no attributes'); | ||
2760 | 2744 | } | ||
2761 | 2745 | |||
2762 | 2746 | $para =~ s/\s+\Z//g; | ||
2763 | 2747 | $para =~ s/\s+/ /g; | ||
2764 | 2748 | $para =~ s/$POD_link_re/$1/go; | ||
2765 | 2749 | |||
2766 | 2750 | $para =~ s/\.(?:\n.*| [A-Z].*|\Z)//s; | ||
2767 | 2751 | PTDEBUG && _d('Short help:', $para); | ||
2768 | 2752 | |||
2769 | 2753 | die "No description after option spec $option" if $para =~ m/^=item/; | ||
2770 | 2754 | |||
2771 | 2755 | if ( my ($base_option) = $option =~ m/^\[no\](.*)/ ) { | ||
2772 | 2756 | $option = $base_option; | ||
2773 | 2757 | $attribs{'negatable'} = 1; | ||
2774 | 2758 | } | ||
2775 | 2759 | |||
2776 | 2760 | push @specs, { | ||
2777 | 2761 | spec => $self->{parse_attributes}->($self, $option, \%attribs), | ||
2778 | 2762 | desc => $para | ||
2779 | 2763 | . (defined $attribs{default} ? " (default $attribs{default})" : ''), | ||
2780 | 2764 | group => ($attribs{'group'} ? $attribs{'group'} : 'default'), | ||
2781 | 2765 | }; | ||
2782 | 2766 | } | ||
2783 | 2767 | while ( $para = <$fh> ) { | ||
2784 | 2768 | last unless $para; | ||
2785 | 2769 | if ( $para =~ m/^=head1/ ) { | ||
2786 | 2770 | $para = undef; # Can't 'last' out of a do {} block. | ||
2787 | 2771 | last; | ||
2788 | 2772 | } | ||
2789 | 2773 | last if $para =~ m/^=item /; | ||
2790 | 2774 | } | ||
2791 | 2775 | } while ( $para ); | ||
2792 | 2776 | |||
2793 | 2777 | die "No valid specs in $self->{head1}" unless @specs; | ||
2794 | 2778 | |||
2795 | 2779 | close $fh; | ||
2796 | 2780 | return @specs, @rules; | ||
2797 | 2781 | } | ||
2798 | 2782 | |||
2799 | 2783 | sub _parse_specs { | ||
2800 | 2784 | my ( $self, @specs ) = @_; | ||
2801 | 2785 | my %disables; # special rule that requires deferred checking | ||
2802 | 2786 | |||
2803 | 2787 | foreach my $opt ( @specs ) { | ||
2804 | 2788 | if ( ref $opt ) { # It's an option spec, not a rule. | ||
2805 | 2789 | PTDEBUG && _d('Parsing opt spec:', | ||
2806 | 2790 | map { ($_, '=>', $opt->{$_}) } keys %$opt); | ||
2807 | 2791 | |||
2808 | 2792 | my ( $long, $short ) = $opt->{spec} =~ m/^([\w-]+)(?:\|([^!+=]*))?/; | ||
2809 | 2793 | if ( !$long ) { | ||
2810 | 2794 | die "Cannot parse long option from spec $opt->{spec}"; | ||
2811 | 2795 | } | ||
2812 | 2796 | $opt->{long} = $long; | ||
2813 | 2797 | |||
2814 | 2798 | die "Duplicate long option --$long" if exists $self->{opts}->{$long}; | ||
2815 | 2799 | $self->{opts}->{$long} = $opt; | ||
2816 | 2800 | |||
2817 | 2801 | if ( length $long == 1 ) { | ||
2818 | 2802 | PTDEBUG && _d('Long opt', $long, 'looks like short opt'); | ||
2819 | 2803 | $self->{short_opts}->{$long} = $long; | ||
2820 | 2804 | } | ||
2821 | 2805 | |||
2822 | 2806 | if ( $short ) { | ||
2823 | 2807 | die "Duplicate short option -$short" | ||
2824 | 2808 | if exists $self->{short_opts}->{$short}; | ||
2825 | 2809 | $self->{short_opts}->{$short} = $long; | ||
2826 | 2810 | $opt->{short} = $short; | ||
2827 | 2811 | } | ||
2828 | 2812 | else { | ||
2829 | 2813 | $opt->{short} = undef; | ||
2830 | 2814 | } | ||
2831 | 2815 | |||
2832 | 2816 | $opt->{is_negatable} = $opt->{spec} =~ m/!/ ? 1 : 0; | ||
2833 | 2817 | $opt->{is_cumulative} = $opt->{spec} =~ m/\+/ ? 1 : 0; | ||
2834 | 2818 | $opt->{is_required} = $opt->{desc} =~ m/required/ ? 1 : 0; | ||
2835 | 2819 | |||
2836 | 2820 | $opt->{group} ||= 'default'; | ||
2837 | 2821 | $self->{groups}->{ $opt->{group} }->{$long} = 1; | ||
2838 | 2822 | |||
2839 | 2823 | $opt->{value} = undef; | ||
2840 | 2824 | $opt->{got} = 0; | ||
2841 | 2825 | |||
2842 | 2826 | my ( $type ) = $opt->{spec} =~ m/=(.)/; | ||
2843 | 2827 | $opt->{type} = $type; | ||
2844 | 2828 | PTDEBUG && _d($long, 'type:', $type); | ||
2845 | 2829 | |||
2846 | 2830 | |||
2847 | 2831 | $opt->{spec} =~ s/=./=s/ if ( $type && $type =~ m/[HhAadzm]/ ); | ||
2848 | 2832 | |||
2849 | 2833 | if ( (my ($def) = $opt->{desc} =~ m/default\b(?: ([^)]+))?/) ) { | ||
2850 | 2834 | $self->{defaults}->{$long} = defined $def ? $def : 1; | ||
2851 | 2835 | PTDEBUG && _d($long, 'default:', $def); | ||
2852 | 2836 | } | ||
2853 | 2837 | |||
2854 | 2838 | if ( $long eq 'config' ) { | ||
2855 | 2839 | $self->{defaults}->{$long} = join(',', $self->get_defaults_files()); | ||
2856 | 2840 | } | ||
2857 | 2841 | |||
2858 | 2842 | if ( (my ($dis) = $opt->{desc} =~ m/(disables .*)/) ) { | ||
2859 | 2843 | $disables{$long} = $dis; | ||
2860 | 2844 | PTDEBUG && _d('Deferring check of disables rule for', $opt, $dis); | ||
2861 | 2845 | } | ||
2862 | 2846 | |||
2863 | 2847 | $self->{opts}->{$long} = $opt; | ||
2864 | 2848 | } | ||
2865 | 2849 | else { # It's an option rule, not a spec. | ||
2866 | 2850 | PTDEBUG && _d('Parsing rule:', $opt); | ||
2867 | 2851 | push @{$self->{rules}}, $opt; | ||
2868 | 2852 | my @participants = $self->_get_participants($opt); | ||
2869 | 2853 | my $rule_ok = 0; | ||
2870 | 2854 | |||
2871 | 2855 | if ( $opt =~ m/mutually exclusive|one and only one/ ) { | ||
2872 | 2856 | $rule_ok = 1; | ||
2873 | 2857 | push @{$self->{mutex}}, \@participants; | ||
2874 | 2858 | PTDEBUG && _d(@participants, 'are mutually exclusive'); | ||
2875 | 2859 | } | ||
2876 | 2860 | if ( $opt =~ m/at least one|one and only one/ ) { | ||
2877 | 2861 | $rule_ok = 1; | ||
2878 | 2862 | push @{$self->{atleast1}}, \@participants; | ||
2879 | 2863 | PTDEBUG && _d(@participants, 'require at least one'); | ||
2880 | 2864 | } | ||
2881 | 2865 | if ( $opt =~ m/default to/ ) { | ||
2882 | 2866 | $rule_ok = 1; | ||
2883 | 2867 | $self->{defaults_to}->{$participants[0]} = $participants[1]; | ||
2884 | 2868 | PTDEBUG && _d($participants[0], 'defaults to', $participants[1]); | ||
2885 | 2869 | } | ||
2886 | 2870 | if ( $opt =~ m/restricted to option groups/ ) { | ||
2887 | 2871 | $rule_ok = 1; | ||
2888 | 2872 | my ($groups) = $opt =~ m/groups ([\w\s\,]+)/; | ||
2889 | 2873 | my @groups = split(',', $groups); | ||
2890 | 2874 | %{$self->{allowed_groups}->{$participants[0]}} = map { | ||
2891 | 2875 | s/\s+//; | ||
2892 | 2876 | $_ => 1; | ||
2893 | 2877 | } @groups; | ||
2894 | 2878 | } | ||
2895 | 2879 | if( $opt =~ m/accepts additional command-line arguments/ ) { | ||
2896 | 2880 | $rule_ok = 1; | ||
2897 | 2881 | $self->{strict} = 0; | ||
2898 | 2882 | PTDEBUG && _d("Strict mode disabled by rule"); | ||
2899 | 2883 | } | ||
2900 | 2884 | |||
2901 | 2885 | die "Unrecognized option rule: $opt" unless $rule_ok; | ||
2902 | 2886 | } | ||
2903 | 2887 | } | ||
2904 | 2888 | |||
2905 | 2889 | foreach my $long ( keys %disables ) { | ||
2906 | 2890 | my @participants = $self->_get_participants($disables{$long}); | ||
2907 | 2891 | $self->{disables}->{$long} = \@participants; | ||
2908 | 2892 | PTDEBUG && _d('Option', $long, 'disables', @participants); | ||
2909 | 2893 | } | ||
2910 | 2894 | |||
2911 | 2895 | return; | ||
2912 | 2896 | } | ||
2913 | 2897 | |||
2914 | 2898 | sub _get_participants { | ||
2915 | 2899 | my ( $self, $str ) = @_; | ||
2916 | 2900 | my @participants; | ||
2917 | 2901 | foreach my $long ( $str =~ m/--(?:\[no\])?([\w-]+)/g ) { | ||
2918 | 2902 | die "Option --$long does not exist while processing rule $str" | ||
2919 | 2903 | unless exists $self->{opts}->{$long}; | ||
2920 | 2904 | push @participants, $long; | ||
2921 | 2905 | } | ||
2922 | 2906 | PTDEBUG && _d('Participants for', $str, ':', @participants); | ||
2923 | 2907 | return @participants; | ||
2924 | 2908 | } | ||
2925 | 2909 | |||
2926 | 2910 | sub opts { | ||
2927 | 2911 | my ( $self ) = @_; | ||
2928 | 2912 | my %opts = %{$self->{opts}}; | ||
2929 | 2913 | return %opts; | ||
2930 | 2914 | } | ||
2931 | 2915 | |||
2932 | 2916 | sub short_opts { | ||
2933 | 2917 | my ( $self ) = @_; | ||
2934 | 2918 | my %short_opts = %{$self->{short_opts}}; | ||
2935 | 2919 | return %short_opts; | ||
2936 | 2920 | } | ||
2937 | 2921 | |||
2938 | 2922 | sub set_defaults { | ||
2939 | 2923 | my ( $self, %defaults ) = @_; | ||
2940 | 2924 | $self->{defaults} = {}; | ||
2941 | 2925 | foreach my $long ( keys %defaults ) { | ||
2942 | 2926 | die "Cannot set default for nonexistent option $long" | ||
2943 | 2927 | unless exists $self->{opts}->{$long}; | ||
2944 | 2928 | $self->{defaults}->{$long} = $defaults{$long}; | ||
2945 | 2929 | PTDEBUG && _d('Default val for', $long, ':', $defaults{$long}); | ||
2946 | 2930 | } | ||
2947 | 2931 | return; | ||
2948 | 2932 | } | ||
2949 | 2933 | |||
2950 | 2934 | sub get_defaults { | ||
2951 | 2935 | my ( $self ) = @_; | ||
2952 | 2936 | return $self->{defaults}; | ||
2953 | 2937 | } | ||
2954 | 2938 | |||
2955 | 2939 | sub get_groups { | ||
2956 | 2940 | my ( $self ) = @_; | ||
2957 | 2941 | return $self->{groups}; | ||
2958 | 2942 | } | ||
2959 | 2943 | |||
2960 | 2944 | sub _set_option { | ||
2961 | 2945 | my ( $self, $opt, $val ) = @_; | ||
2962 | 2946 | my $long = exists $self->{opts}->{$opt} ? $opt | ||
2963 | 2947 | : exists $self->{short_opts}->{$opt} ? $self->{short_opts}->{$opt} | ||
2964 | 2948 | : die "Getopt::Long gave a nonexistent option: $opt"; | ||
2965 | 2949 | $opt = $self->{opts}->{$long}; | ||
2966 | 2950 | if ( $opt->{is_cumulative} ) { | ||
2967 | 2951 | $opt->{value}++; | ||
2968 | 2952 | } | ||
2969 | 2953 | elsif ( ($opt->{type} || '') eq 's' && $val =~ m/^--?(.+)/ ) { | ||
2970 | 2954 | my $next_opt = $1; | ||
2971 | 2955 | if ( exists $self->{opts}->{$next_opt} | ||
2972 | 2956 | || exists $self->{short_opts}->{$next_opt} ) { | ||
2973 | 2957 | $self->save_error("--$long requires a string value"); | ||
2974 | 2958 | return; | ||
2975 | 2959 | } | ||
2976 | 2960 | else { | ||
2977 | 2961 | $opt->{value} = $val; | ||
2978 | 2962 | } | ||
2979 | 2963 | } | ||
2980 | 2964 | else { | ||
2981 | 2965 | $opt->{value} = $val; | ||
2982 | 2966 | } | ||
2983 | 2967 | $opt->{got} = 1; | ||
2984 | 2968 | PTDEBUG && _d('Got option', $long, '=', $val); | ||
2985 | 2969 | } | ||
2986 | 2970 | |||
2987 | 2971 | sub get_opts { | ||
2988 | 2972 | my ( $self ) = @_; | ||
2989 | 2973 | |||
2990 | 2974 | foreach my $long ( keys %{$self->{opts}} ) { | ||
2991 | 2975 | $self->{opts}->{$long}->{got} = 0; | ||
2992 | 2976 | $self->{opts}->{$long}->{value} | ||
2993 | 2977 | = exists $self->{defaults}->{$long} ? $self->{defaults}->{$long} | ||
2994 | 2978 | : $self->{opts}->{$long}->{is_cumulative} ? 0 | ||
2995 | 2979 | : undef; | ||
2996 | 2980 | } | ||
2997 | 2981 | $self->{got_opts} = 0; | ||
2998 | 2982 | |||
2999 | 2983 | $self->{errors} = []; | ||
3000 | 2984 | |||
3001 | 2985 | if ( @ARGV && $ARGV[0] eq "--config" ) { | ||
3002 | 2986 | shift @ARGV; | ||
3003 | 2987 | $self->_set_option('config', shift @ARGV); | ||
3004 | 2988 | } | ||
3005 | 2989 | if ( $self->has('config') ) { | ||
3006 | 2990 | my @extra_args; | ||
3007 | 2991 | foreach my $filename ( split(',', $self->get('config')) ) { | ||
3008 | 2992 | eval { | ||
3009 | 2993 | push @extra_args, $self->_read_config_file($filename); | ||
3010 | 2994 | }; | ||
3011 | 2995 | if ( $EVAL_ERROR ) { | ||
3012 | 2996 | if ( $self->got('config') ) { | ||
3013 | 2997 | die $EVAL_ERROR; | ||
3014 | 2998 | } | ||
3015 | 2999 | elsif ( PTDEBUG ) { | ||
3016 | 3000 | _d($EVAL_ERROR); | ||
3017 | 3001 | } | ||
3018 | 3002 | } | ||
3019 | 3003 | } | ||
3020 | 3004 | unshift @ARGV, @extra_args; | ||
3021 | 3005 | } | ||
3022 | 3006 | |||
3023 | 3007 | Getopt::Long::Configure('no_ignore_case', 'bundling'); | ||
3024 | 3008 | GetOptions( | ||
3025 | 3009 | map { $_->{spec} => sub { $self->_set_option(@_); } } | ||
3026 | 3010 | grep { $_->{long} ne 'config' } # --config is handled specially above. | ||
3027 | 3011 | values %{$self->{opts}} | ||
3028 | 3012 | ) or $self->save_error('Error parsing options'); | ||
3029 | 3013 | |||
3030 | 3014 | if ( exists $self->{opts}->{version} && $self->{opts}->{version}->{got} ) { | ||
3031 | 3015 | if ( $self->{version} ) { | ||
3032 | 3016 | print $self->{version}, "\n"; | ||
3033 | 3017 | } | ||
3034 | 3018 | else { | ||
3035 | 3019 | print "Error parsing version. See the VERSION section of the tool's documentation.\n"; | ||
3036 | 3020 | } | ||
3037 | 3021 | exit 1; | ||
3038 | 3022 | } | ||
3039 | 3023 | |||
3040 | 3024 | if ( @ARGV && $self->{strict} ) { | ||
3041 | 3025 | $self->save_error("Unrecognized command-line options @ARGV"); | ||
3042 | 3026 | } | ||
3043 | 3027 | |||
3044 | 3028 | foreach my $mutex ( @{$self->{mutex}} ) { | ||
3045 | 3029 | my @set = grep { $self->{opts}->{$_}->{got} } @$mutex; | ||
3046 | 3030 | if ( @set > 1 ) { | ||
3047 | 3031 | my $err = join(', ', map { "--$self->{opts}->{$_}->{long}" } | ||
3048 | 3032 | @{$mutex}[ 0 .. scalar(@$mutex) - 2] ) | ||
3049 | 3033 | . ' and --'.$self->{opts}->{$mutex->[-1]}->{long} | ||
3050 | 3034 | . ' are mutually exclusive.'; | ||
3051 | 3035 | $self->save_error($err); | ||
3052 | 3036 | } | ||
3053 | 3037 | } | ||
3054 | 3038 | |||
3055 | 3039 | foreach my $required ( @{$self->{atleast1}} ) { | ||
3056 | 3040 | my @set = grep { $self->{opts}->{$_}->{got} } @$required; | ||
3057 | 3041 | if ( @set == 0 ) { | ||
3058 | 3042 | my $err = join(', ', map { "--$self->{opts}->{$_}->{long}" } | ||
3059 | 3043 | @{$required}[ 0 .. scalar(@$required) - 2] ) | ||
3060 | 3044 | .' or --'.$self->{opts}->{$required->[-1]}->{long}; | ||
3061 | 3045 | $self->save_error("Specify at least one of $err"); | ||
3062 | 3046 | } | ||
3063 | 3047 | } | ||
3064 | 3048 | |||
3065 | 3049 | $self->_check_opts( keys %{$self->{opts}} ); | ||
3066 | 3050 | $self->{got_opts} = 1; | ||
3067 | 3051 | return; | ||
3068 | 3052 | } | ||
3069 | 3053 | |||
3070 | 3054 | sub _check_opts { | ||
3071 | 3055 | my ( $self, @long ) = @_; | ||
3072 | 3056 | my $long_last = scalar @long; | ||
3073 | 3057 | while ( @long ) { | ||
3074 | 3058 | foreach my $i ( 0..$#long ) { | ||
3075 | 3059 | my $long = $long[$i]; | ||
3076 | 3060 | next unless $long; | ||
3077 | 3061 | my $opt = $self->{opts}->{$long}; | ||
3078 | 3062 | if ( $opt->{got} ) { | ||
3079 | 3063 | if ( exists $self->{disables}->{$long} ) { | ||
3080 | 3064 | my @disable_opts = @{$self->{disables}->{$long}}; | ||
3081 | 3065 | map { $self->{opts}->{$_}->{value} = undef; } @disable_opts; | ||
3082 | 3066 | PTDEBUG && _d('Unset options', @disable_opts, | ||
3083 | 3067 | 'because', $long,'disables them'); | ||
3084 | 3068 | } | ||
3085 | 3069 | |||
3086 | 3070 | if ( exists $self->{allowed_groups}->{$long} ) { | ||
3087 | 3071 | |||
3088 | 3072 | my @restricted_groups = grep { | ||
3089 | 3073 | !exists $self->{allowed_groups}->{$long}->{$_} | ||
3090 | 3074 | } keys %{$self->{groups}}; | ||
3091 | 3075 | |||
3092 | 3076 | my @restricted_opts; | ||
3093 | 3077 | foreach my $restricted_group ( @restricted_groups ) { | ||
3094 | 3078 | RESTRICTED_OPT: | ||
3095 | 3079 | foreach my $restricted_opt ( | ||
3096 | 3080 | keys %{$self->{groups}->{$restricted_group}} ) | ||
3097 | 3081 | { | ||
3098 | 3082 | next RESTRICTED_OPT if $restricted_opt eq $long; | ||
3099 | 3083 | push @restricted_opts, $restricted_opt | ||
3100 | 3084 | if $self->{opts}->{$restricted_opt}->{got}; | ||
3101 | 3085 | } | ||
3102 | 3086 | } | ||
3103 | 3087 | |||
3104 | 3088 | if ( @restricted_opts ) { | ||
3105 | 3089 | my $err; | ||
3106 | 3090 | if ( @restricted_opts == 1 ) { | ||
3107 | 3091 | $err = "--$restricted_opts[0]"; | ||
3108 | 3092 | } | ||
3109 | 3093 | else { | ||
3110 | 3094 | $err = join(', ', | ||
3111 | 3095 | map { "--$self->{opts}->{$_}->{long}" } | ||
3112 | 3096 | grep { $_ } | ||
3113 | 3097 | @restricted_opts[0..scalar(@restricted_opts) - 2] | ||
3114 | 3098 | ) | ||
3115 | 3099 | . ' or --'.$self->{opts}->{$restricted_opts[-1]}->{long}; | ||
3116 | 3100 | } | ||
3117 | 3101 | $self->save_error("--$long is not allowed with $err"); | ||
3118 | 3102 | } | ||
3119 | 3103 | } | ||
3120 | 3104 | |||
3121 | 3105 | } | ||
3122 | 3106 | elsif ( $opt->{is_required} ) { | ||
3123 | 3107 | $self->save_error("Required option --$long must be specified"); | ||
3124 | 3108 | } | ||
3125 | 3109 | |||
3126 | 3110 | $self->_validate_type($opt); | ||
3127 | 3111 | if ( $opt->{parsed} ) { | ||
3128 | 3112 | delete $long[$i]; | ||
3129 | 3113 | } | ||
3130 | 3114 | else { | ||
3131 | 3115 | PTDEBUG && _d('Temporarily failed to parse', $long); | ||
3132 | 3116 | } | ||
3133 | 3117 | } | ||
3134 | 3118 | |||
3135 | 3119 | die "Failed to parse options, possibly due to circular dependencies" | ||
3136 | 3120 | if @long == $long_last; | ||
3137 | 3121 | $long_last = @long; | ||
3138 | 3122 | } | ||
3139 | 3123 | |||
3140 | 3124 | return; | ||
3141 | 3125 | } | ||
3142 | 3126 | |||
3143 | 3127 | sub _validate_type { | ||
3144 | 3128 | my ( $self, $opt ) = @_; | ||
3145 | 3129 | return unless $opt; | ||
3146 | 3130 | |||
3147 | 3131 | if ( !$opt->{type} ) { | ||
3148 | 3132 | $opt->{parsed} = 1; | ||
3149 | 3133 | return; | ||
3150 | 3134 | } | ||
3151 | 3135 | |||
3152 | 3136 | my $val = $opt->{value}; | ||
3153 | 3137 | |||
3154 | 3138 | if ( $val && $opt->{type} eq 'm' ) { # type time | ||
3155 | 3139 | PTDEBUG && _d('Parsing option', $opt->{long}, 'as a time value'); | ||
3156 | 3140 | my ( $prefix, $num, $suffix ) = $val =~ m/([+-]?)(\d+)([a-z])?$/; | ||
3157 | 3141 | if ( !$suffix ) { | ||
3158 | 3142 | my ( $s ) = $opt->{desc} =~ m/\(suffix (.)\)/; | ||
3159 | 3143 | $suffix = $s || 's'; | ||
3160 | 3144 | PTDEBUG && _d('No suffix given; using', $suffix, 'for', | ||
3161 | 3145 | $opt->{long}, '(value:', $val, ')'); | ||
3162 | 3146 | } | ||
3163 | 3147 | if ( $suffix =~ m/[smhd]/ ) { | ||
3164 | 3148 | $val = $suffix eq 's' ? $num # Seconds | ||
3165 | 3149 | : $suffix eq 'm' ? $num * 60 # Minutes | ||
3166 | 3150 | : $suffix eq 'h' ? $num * 3600 # Hours | ||
3167 | 3151 | : $num * 86400; # Days | ||
3168 | 3152 | $opt->{value} = ($prefix || '') . $val; | ||
3169 | 3153 | PTDEBUG && _d('Setting option', $opt->{long}, 'to', $val); | ||
3170 | 3154 | } | ||
3171 | 3155 | else { | ||
3172 | 3156 | $self->save_error("Invalid time suffix for --$opt->{long}"); | ||
3173 | 3157 | } | ||
3174 | 3158 | } | ||
3175 | 3159 | elsif ( $val && $opt->{type} eq 'd' ) { # type DSN | ||
3176 | 3160 | PTDEBUG && _d('Parsing option', $opt->{long}, 'as a DSN'); | ||
3177 | 3161 | my $prev = {}; | ||
3178 | 3162 | my $from_key = $self->{defaults_to}->{ $opt->{long} }; | ||
3179 | 3163 | if ( $from_key ) { | ||
3180 | 3164 | PTDEBUG && _d($opt->{long}, 'DSN copies from', $from_key, 'DSN'); | ||
3181 | 3165 | if ( $self->{opts}->{$from_key}->{parsed} ) { | ||
3182 | 3166 | $prev = $self->{opts}->{$from_key}->{value}; | ||
3183 | 3167 | } | ||
3184 | 3168 | else { | ||
3185 | 3169 | PTDEBUG && _d('Cannot parse', $opt->{long}, 'until', | ||
3186 | 3170 | $from_key, 'parsed'); | ||
3187 | 3171 | return; | ||
3188 | 3172 | } | ||
3189 | 3173 | } | ||
3190 | 3174 | my $defaults = $self->{DSNParser}->parse_options($self); | ||
3191 | 3175 | $opt->{value} = $self->{DSNParser}->parse($val, $prev, $defaults); | ||
3192 | 3176 | } | ||
3193 | 3177 | elsif ( $val && $opt->{type} eq 'z' ) { # type size | ||
3194 | 3178 | PTDEBUG && _d('Parsing option', $opt->{long}, 'as a size value'); | ||
3195 | 3179 | $self->_parse_size($opt, $val); | ||
3196 | 3180 | } | ||
3197 | 3181 | elsif ( $opt->{type} eq 'H' || (defined $val && $opt->{type} eq 'h') ) { | ||
3198 | 3182 | $opt->{value} = { map { $_ => 1 } split(/(?<!\\),\s*/, ($val || '')) }; | ||
3199 | 3183 | } | ||
3200 | 3184 | elsif ( $opt->{type} eq 'A' || (defined $val && $opt->{type} eq 'a') ) { | ||
3201 | 3185 | $opt->{value} = [ split(/(?<!\\),\s*/, ($val || '')) ]; | ||
3202 | 3186 | } | ||
3203 | 3187 | else { | ||
3204 | 3188 | PTDEBUG && _d('Nothing to validate for option', | ||
3205 | 3189 | $opt->{long}, 'type', $opt->{type}, 'value', $val); | ||
3206 | 3190 | } | ||
3207 | 3191 | |||
3208 | 3192 | $opt->{parsed} = 1; | ||
3209 | 3193 | return; | ||
3210 | 3194 | } | ||
3211 | 3195 | |||
3212 | 3196 | sub get { | ||
3213 | 3197 | my ( $self, $opt ) = @_; | ||
3214 | 3198 | my $long = (length $opt == 1 ? $self->{short_opts}->{$opt} : $opt); | ||
3215 | 3199 | die "Option $opt does not exist" | ||
3216 | 3200 | unless $long && exists $self->{opts}->{$long}; | ||
3217 | 3201 | return $self->{opts}->{$long}->{value}; | ||
3218 | 3202 | } | ||
3219 | 3203 | |||
3220 | 3204 | sub got { | ||
3221 | 3205 | my ( $self, $opt ) = @_; | ||
3222 | 3206 | my $long = (length $opt == 1 ? $self->{short_opts}->{$opt} : $opt); | ||
3223 | 3207 | die "Option $opt does not exist" | ||
3224 | 3208 | unless $long && exists $self->{opts}->{$long}; | ||
3225 | 3209 | return $self->{opts}->{$long}->{got}; | ||
3226 | 3210 | } | ||
3227 | 3211 | |||
3228 | 3212 | sub has { | ||
3229 | 3213 | my ( $self, $opt ) = @_; | ||
3230 | 3214 | my $long = (length $opt == 1 ? $self->{short_opts}->{$opt} : $opt); | ||
3231 | 3215 | return defined $long ? exists $self->{opts}->{$long} : 0; | ||
3232 | 3216 | } | ||
3233 | 3217 | |||
3234 | 3218 | sub set { | ||
3235 | 3219 | my ( $self, $opt, $val ) = @_; | ||
3236 | 3220 | my $long = (length $opt == 1 ? $self->{short_opts}->{$opt} : $opt); | ||
3237 | 3221 | die "Option $opt does not exist" | ||
3238 | 3222 | unless $long && exists $self->{opts}->{$long}; | ||
3239 | 3223 | $self->{opts}->{$long}->{value} = $val; | ||
3240 | 3224 | return; | ||
3241 | 3225 | } | ||
3242 | 3226 | |||
3243 | 3227 | sub save_error { | ||
3244 | 3228 | my ( $self, $error ) = @_; | ||
3245 | 3229 | push @{$self->{errors}}, $error; | ||
3246 | 3230 | return; | ||
3247 | 3231 | } | ||
3248 | 3232 | |||
3249 | 3233 | sub errors { | ||
3250 | 3234 | my ( $self ) = @_; | ||
3251 | 3235 | return $self->{errors}; | ||
3252 | 3236 | } | ||
3253 | 3237 | |||
3254 | 3238 | sub usage { | ||
3255 | 3239 | my ( $self ) = @_; | ||
3256 | 3240 | warn "No usage string is set" unless $self->{usage}; # XXX | ||
3257 | 3241 | return "Usage: " . ($self->{usage} || '') . "\n"; | ||
3258 | 3242 | } | ||
3259 | 3243 | |||
3260 | 3244 | sub descr { | ||
3261 | 3245 | my ( $self ) = @_; | ||
3262 | 3246 | warn "No description string is set" unless $self->{description}; # XXX | ||
3263 | 3247 | my $descr = ($self->{description} || $self->{program_name} || '') | ||
3264 | 3248 | . " For more details, please use the --help option, " | ||
3265 | 3249 | . "or try 'perldoc $PROGRAM_NAME' " | ||
3266 | 3250 | . "for complete documentation."; | ||
3267 | 3251 | $descr = join("\n", $descr =~ m/(.{0,80})(?:\s+|$)/g) | ||
3268 | 3252 | unless $ENV{DONT_BREAK_LINES}; | ||
3269 | 3253 | $descr =~ s/ +$//mg; | ||
3270 | 3254 | return $descr; | ||
3271 | 3255 | } | ||
3272 | 3256 | |||
3273 | 3257 | sub usage_or_errors { | ||
3274 | 3258 | my ( $self, $file, $return ) = @_; | ||
3275 | 3259 | $file ||= $self->{file} || __FILE__; | ||
3276 | 3260 | |||
3277 | 3261 | if ( !$self->{description} || !$self->{usage} ) { | ||
3278 | 3262 | PTDEBUG && _d("Getting description and usage from SYNOPSIS in", $file); | ||
3279 | 3263 | my %synop = $self->_parse_synopsis($file); | ||
3280 | 3264 | $self->{description} ||= $synop{description}; | ||
3281 | 3265 | $self->{usage} ||= $synop{usage}; | ||
3282 | 3266 | PTDEBUG && _d("Description:", $self->{description}, | ||
3283 | 3267 | "\nUsage:", $self->{usage}); | ||
3284 | 3268 | } | ||
3285 | 3269 | |||
3286 | 3270 | if ( $self->{opts}->{help}->{got} ) { | ||
3287 | 3271 | print $self->print_usage() or die "Cannot print usage: $OS_ERROR"; | ||
3288 | 3272 | exit 0 unless $return; | ||
3289 | 3273 | } | ||
3290 | 3274 | elsif ( scalar @{$self->{errors}} ) { | ||
3291 | 3275 | print $self->print_errors() or die "Cannot print errors: $OS_ERROR"; | ||
3292 | 3276 | exit 1 unless $return; | ||
3293 | 3277 | } | ||
3294 | 3278 | |||
3295 | 3279 | return; | ||
3296 | 3280 | } | ||
3297 | 3281 | |||
3298 | 3282 | sub print_errors { | ||
3299 | 3283 | my ( $self ) = @_; | ||
3300 | 3284 | my $usage = $self->usage() . "\n"; | ||
3301 | 3285 | if ( (my @errors = @{$self->{errors}}) ) { | ||
3302 | 3286 | $usage .= join("\n * ", 'Errors in command-line arguments:', @errors) | ||
3303 | 3287 | . "\n"; | ||
3304 | 3288 | } | ||
3305 | 3289 | return $usage . "\n" . $self->descr(); | ||
3306 | 3290 | } | ||
3307 | 3291 | |||
3308 | 3292 | sub print_usage { | ||
3309 | 3293 | my ( $self ) = @_; | ||
3310 | 3294 | die "Run get_opts() before print_usage()" unless $self->{got_opts}; | ||
3311 | 3295 | my @opts = values %{$self->{opts}}; | ||
3312 | 3296 | |||
3313 | 3297 | my $maxl = max( | ||
3314 | 3298 | map { | ||
3315 | 3299 | length($_->{long}) # option long name | ||
3316 | 3300 | + ($_->{is_negatable} ? 4 : 0) # "[no]" if opt is negatable | ||
3317 | 3301 | + ($_->{type} ? 2 : 0) # "=x" where x is the opt type | ||
3318 | 3302 | } | ||
3319 | 3303 | @opts); | ||
3320 | 3304 | |||
3321 | 3305 | my $maxs = max(0, | ||
3322 | 3306 | map { | ||
3323 | 3307 | length($_) | ||
3324 | 3308 | + ($self->{opts}->{$_}->{is_negatable} ? 4 : 0) | ||
3325 | 3309 | + ($self->{opts}->{$_}->{type} ? 2 : 0) | ||
3326 | 3310 | } | ||
3327 | 3311 | values %{$self->{short_opts}}); | ||
3328 | 3312 | |||
3329 | 3313 | my $lcol = max($maxl, ($maxs + 3)); | ||
3330 | 3314 | my $rcol = 80 - $lcol - 6; | ||
3331 | 3315 | my $rpad = ' ' x ( 80 - $rcol ); | ||
3332 | 3316 | |||
3333 | 3317 | $maxs = max($lcol - 3, $maxs); | ||
3334 | 3318 | |||
3335 | 3319 | my $usage = $self->descr() . "\n" . $self->usage(); | ||
3336 | 3320 | |||
3337 | 3321 | my @groups = reverse sort grep { $_ ne 'default'; } keys %{$self->{groups}}; | ||
3338 | 3322 | push @groups, 'default'; | ||
3339 | 3323 | |||
3340 | 3324 | foreach my $group ( reverse @groups ) { | ||
3341 | 3325 | $usage .= "\n".($group eq 'default' ? 'Options' : $group).":\n\n"; | ||
3342 | 3326 | foreach my $opt ( | ||
3343 | 3327 | sort { $a->{long} cmp $b->{long} } | ||
3344 | 3328 | grep { $_->{group} eq $group } | ||
3345 | 3329 | @opts ) | ||
3346 | 3330 | { | ||
3347 | 3331 | my $long = $opt->{is_negatable} ? "[no]$opt->{long}" : $opt->{long}; | ||
3348 | 3332 | my $short = $opt->{short}; | ||
3349 | 3333 | my $desc = $opt->{desc}; | ||
3350 | 3334 | |||
3351 | 3335 | $long .= $opt->{type} ? "=$opt->{type}" : ""; | ||
3352 | 3336 | |||
3353 | 3337 | if ( $opt->{type} && $opt->{type} eq 'm' ) { | ||
3354 | 3338 | my ($s) = $desc =~ m/\(suffix (.)\)/; | ||
3355 | 3339 | $s ||= 's'; | ||
3356 | 3340 | $desc =~ s/\s+\(suffix .\)//; | ||
3357 | 3341 | $desc .= ". Optional suffix s=seconds, m=minutes, h=hours, " | ||
3358 | 3342 | . "d=days; if no suffix, $s is used."; | ||
3359 | 3343 | } | ||
3360 | 3344 | $desc = join("\n$rpad", grep { $_ } $desc =~ m/(.{0,$rcol}(?!\W))(?:\s+|(?<=\W)|$)/g); | ||
3361 | 3345 | $desc =~ s/ +$//mg; | ||
3362 | 3346 | if ( $short ) { | ||
3363 | 3347 | $usage .= sprintf(" --%-${maxs}s -%s %s\n", $long, $short, $desc); | ||
3364 | 3348 | } | ||
3365 | 3349 | else { | ||
3366 | 3350 | $usage .= sprintf(" --%-${lcol}s %s\n", $long, $desc); | ||
3367 | 3351 | } | ||
3368 | 3352 | } | ||
3369 | 3353 | } | ||
3370 | 3354 | |||
3371 | 3355 | $usage .= "\nOption types: s=string, i=integer, f=float, h/H/a/A=comma-separated list, d=DSN, z=size, m=time\n"; | ||
3372 | 3356 | |||
3373 | 3357 | if ( (my @rules = @{$self->{rules}}) ) { | ||
3374 | 3358 | $usage .= "\nRules:\n\n"; | ||
3375 | 3359 | $usage .= join("\n", map { " $_" } @rules) . "\n"; | ||
3376 | 3360 | } | ||
3377 | 3361 | if ( $self->{DSNParser} ) { | ||
3378 | 3362 | $usage .= "\n" . $self->{DSNParser}->usage(); | ||
3379 | 3363 | } | ||
3380 | 3364 | $usage .= "\nOptions and values after processing arguments:\n\n"; | ||
3381 | 3365 | foreach my $opt ( sort { $a->{long} cmp $b->{long} } @opts ) { | ||
3382 | 3366 | my $val = $opt->{value}; | ||
3383 | 3367 | my $type = $opt->{type} || ''; | ||
3384 | 3368 | my $bool = $opt->{spec} =~ m/^[\w-]+(?:\|[\w-])?!?$/; | ||
3385 | 3369 | $val = $bool ? ( $val ? 'TRUE' : 'FALSE' ) | ||
3386 | 3370 | : !defined $val ? '(No value)' | ||
3387 | 3371 | : $type eq 'd' ? $self->{DSNParser}->as_string($val) | ||
3388 | 3372 | : $type =~ m/H|h/ ? join(',', sort keys %$val) | ||
3389 | 3373 | : $type =~ m/A|a/ ? join(',', @$val) | ||
3390 | 3374 | : $val; | ||
3391 | 3375 | $usage .= sprintf(" --%-${lcol}s %s\n", $opt->{long}, $val); | ||
3392 | 3376 | } | ||
3393 | 3377 | return $usage; | ||
3394 | 3378 | } | ||
3395 | 3379 | |||
3396 | 3380 | sub prompt_noecho { | ||
3397 | 3381 | shift @_ if ref $_[0] eq __PACKAGE__; | ||
3398 | 3382 | my ( $prompt ) = @_; | ||
3399 | 3383 | local $OUTPUT_AUTOFLUSH = 1; | ||
3400 | 3384 | print $prompt | ||
3401 | 3385 | or die "Cannot print: $OS_ERROR"; | ||
3402 | 3386 | my $response; | ||
3403 | 3387 | eval { | ||
3404 | 3388 | require Term::ReadKey; | ||
3405 | 3389 | Term::ReadKey::ReadMode('noecho'); | ||
3406 | 3390 | chomp($response = <STDIN>); | ||
3407 | 3391 | Term::ReadKey::ReadMode('normal'); | ||
3408 | 3392 | print "\n" | ||
3409 | 3393 | or die "Cannot print: $OS_ERROR"; | ||
3410 | 3394 | }; | ||
3411 | 3395 | if ( $EVAL_ERROR ) { | ||
3412 | 3396 | die "Cannot read response; is Term::ReadKey installed? $EVAL_ERROR"; | ||
3413 | 3397 | } | ||
3414 | 3398 | return $response; | ||
3415 | 3399 | } | ||
3416 | 3400 | |||
3417 | 3401 | sub _read_config_file { | ||
3418 | 3402 | my ( $self, $filename ) = @_; | ||
3419 | 3403 | open my $fh, "<", $filename or die "Cannot open $filename: $OS_ERROR\n"; | ||
3420 | 3404 | my @args; | ||
3421 | 3405 | my $prefix = '--'; | ||
3422 | 3406 | my $parse = 1; | ||
3423 | 3407 | |||
3424 | 3408 | LINE: | ||
3425 | 3409 | while ( my $line = <$fh> ) { | ||
3426 | 3410 | chomp $line; | ||
3427 | 3411 | next LINE if $line =~ m/^\s*(?:\#|\;|$)/; | ||
3428 | 3412 | $line =~ s/\s+#.*$//g; | ||
3429 | 3413 | $line =~ s/^\s+|\s+$//g; | ||
3430 | 3414 | if ( $line eq '--' ) { | ||
3431 | 3415 | $prefix = ''; | ||
3432 | 3416 | $parse = 0; | ||
3433 | 3417 | next LINE; | ||
3434 | 3418 | } | ||
3435 | 3419 | if ( $parse | ||
3436 | 3420 | && (my($opt, $arg) = $line =~ m/^\s*([^=\s]+?)(?:\s*=\s*(.*?)\s*)?$/) | ||
3437 | 3421 | ) { | ||
3438 | 3422 | push @args, grep { defined $_ } ("$prefix$opt", $arg); | ||
3439 | 3423 | } | ||
3440 | 3424 | elsif ( $line =~ m/./ ) { | ||
3441 | 3425 | push @args, $line; | ||
3442 | 3426 | } | ||
3443 | 3427 | else { | ||
3444 | 3428 | die "Syntax error in file $filename at line $INPUT_LINE_NUMBER"; | ||
3445 | 3429 | } | ||
3446 | 3430 | } | ||
3447 | 3431 | close $fh; | ||
3448 | 3432 | return @args; | ||
3449 | 3433 | } | ||
3450 | 3434 | |||
3451 | 3435 | sub read_para_after { | ||
3452 | 3436 | my ( $self, $file, $regex ) = @_; | ||
3453 | 3437 | open my $fh, "<", $file or die "Can't open $file: $OS_ERROR"; | ||
3454 | 3438 | local $INPUT_RECORD_SEPARATOR = ''; | ||
3455 | 3439 | my $para; | ||
3456 | 3440 | while ( $para = <$fh> ) { | ||
3457 | 3441 | next unless $para =~ m/^=pod$/m; | ||
3458 | 3442 | last; | ||
3459 | 3443 | } | ||
3460 | 3444 | while ( $para = <$fh> ) { | ||
3461 | 3445 | next unless $para =~ m/$regex/; | ||
3462 | 3446 | last; | ||
3463 | 3447 | } | ||
3464 | 3448 | $para = <$fh>; | ||
3465 | 3449 | chomp($para); | ||
3466 | 3450 | close $fh or die "Can't close $file: $OS_ERROR"; | ||
3467 | 3451 | return $para; | ||
3468 | 3452 | } | ||
3469 | 3453 | |||
3470 | 3454 | sub clone { | ||
3471 | 3455 | my ( $self ) = @_; | ||
3472 | 3456 | |||
3473 | 3457 | my %clone = map { | ||
3474 | 3458 | my $hashref = $self->{$_}; | ||
3475 | 3459 | my $val_copy = {}; | ||
3476 | 3460 | foreach my $key ( keys %$hashref ) { | ||
3477 | 3461 | my $ref = ref $hashref->{$key}; | ||
3478 | 3462 | $val_copy->{$key} = !$ref ? $hashref->{$key} | ||
3479 | 3463 | : $ref eq 'HASH' ? { %{$hashref->{$key}} } | ||
3480 | 3464 | : $ref eq 'ARRAY' ? [ @{$hashref->{$key}} ] | ||
3481 | 3465 | : $hashref->{$key}; | ||
3482 | 3466 | } | ||
3483 | 3467 | $_ => $val_copy; | ||
3484 | 3468 | } qw(opts short_opts defaults); | ||
3485 | 3469 | |||
3486 | 3470 | foreach my $scalar ( qw(got_opts) ) { | ||
3487 | 3471 | $clone{$scalar} = $self->{$scalar}; | ||
3488 | 3472 | } | ||
3489 | 3473 | |||
3490 | 3474 | return bless \%clone; | ||
3491 | 3475 | } | ||
3492 | 3476 | |||
3493 | 3477 | sub _parse_size { | ||
3494 | 3478 | my ( $self, $opt, $val ) = @_; | ||
3495 | 3479 | |||
3496 | 3480 | if ( lc($val || '') eq 'null' ) { | ||
3497 | 3481 | PTDEBUG && _d('NULL size for', $opt->{long}); | ||
3498 | 3482 | $opt->{value} = 'null'; | ||
3499 | 3483 | return; | ||
3500 | 3484 | } | ||
3501 | 3485 | |||
3502 | 3486 | my %factor_for = (k => 1_024, M => 1_048_576, G => 1_073_741_824); | ||
3503 | 3487 | my ($pre, $num, $factor) = $val =~ m/^([+-])?(\d+)([kMG])?$/; | ||
3504 | 3488 | if ( defined $num ) { | ||
3505 | 3489 | if ( $factor ) { | ||
3506 | 3490 | $num *= $factor_for{$factor}; | ||
3507 | 3491 | PTDEBUG && _d('Setting option', $opt->{y}, | ||
3508 | 3492 | 'to num', $num, '* factor', $factor); | ||
3509 | 3493 | } | ||
3510 | 3494 | $opt->{value} = ($pre || '') . $num; | ||
3511 | 3495 | } | ||
3512 | 3496 | else { | ||
3513 | 3497 | $self->save_error("Invalid size for --$opt->{long}: $val"); | ||
3514 | 3498 | } | ||
3515 | 3499 | return; | ||
3516 | 3500 | } | ||
3517 | 3501 | |||
3518 | 3502 | sub _parse_attribs { | ||
3519 | 3503 | my ( $self, $option, $attribs ) = @_; | ||
3520 | 3504 | my $types = $self->{types}; | ||
3521 | 3505 | return $option | ||
3522 | 3506 | . ($attribs->{'short form'} ? '|' . $attribs->{'short form'} : '' ) | ||
3523 | 3507 | . ($attribs->{'negatable'} ? '!' : '' ) | ||
3524 | 3508 | . ($attribs->{'cumulative'} ? '+' : '' ) | ||
3525 | 3509 | . ($attribs->{'type'} ? '=' . $types->{$attribs->{type}} : '' ); | ||
3526 | 3510 | } | ||
3527 | 3511 | |||
3528 | 3512 | sub _parse_synopsis { | ||
3529 | 3513 | my ( $self, $file ) = @_; | ||
3530 | 3514 | $file ||= $self->{file} || __FILE__; | ||
3531 | 3515 | PTDEBUG && _d("Parsing SYNOPSIS in", $file); | ||
3532 | 3516 | |||
3533 | 3517 | local $INPUT_RECORD_SEPARATOR = ''; # read paragraphs | ||
3534 | 3518 | open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; | ||
3535 | 3519 | my $para; | ||
3536 | 3520 | 1 while defined($para = <$fh>) && $para !~ m/^=head1 SYNOPSIS/; | ||
3537 | 3521 | die "$file does not contain a SYNOPSIS section" unless $para; | ||
3538 | 3522 | my @synop; | ||
3539 | 3523 | for ( 1..2 ) { # 1 for the usage, 2 for the description | ||
3540 | 3524 | my $para = <$fh>; | ||
3541 | 3525 | push @synop, $para; | ||
3542 | 3526 | } | ||
3543 | 3527 | close $fh; | ||
3544 | 3528 | PTDEBUG && _d("Raw SYNOPSIS text:", @synop); | ||
3545 | 3529 | my ($usage, $desc) = @synop; | ||
3546 | 3530 | die "The SYNOPSIS section in $file is not formatted properly" | ||
3547 | 3531 | unless $usage && $desc; | ||
3548 | 3532 | |||
3549 | 3533 | $usage =~ s/^\s*Usage:\s+(.+)/$1/; | ||
3550 | 3534 | chomp $usage; | ||
3551 | 3535 | |||
3552 | 3536 | $desc =~ s/\n/ /g; | ||
3553 | 3537 | $desc =~ s/\s{2,}/ /g; | ||
3554 | 3538 | $desc =~ s/\. ([A-Z][a-z])/. $1/g; | ||
3555 | 3539 | $desc =~ s/\s+$//; | ||
3556 | 3540 | |||
3557 | 3541 | return ( | ||
3558 | 3542 | description => $desc, | ||
3559 | 3543 | usage => $usage, | ||
3560 | 3544 | ); | ||
3561 | 3545 | }; | ||
3562 | 3546 | |||
3563 | 3547 | sub set_vars { | ||
3564 | 3548 | my ($self, $file) = @_; | ||
3565 | 3549 | $file ||= $self->{file} || __FILE__; | ||
3566 | 3550 | |||
3567 | 3551 | my %user_vars; | ||
3568 | 3552 | my $user_vars = $self->has('set-vars') ? $self->get('set-vars') : undef; | ||
3569 | 3553 | if ( $user_vars ) { | ||
3570 | 3554 | foreach my $var_val ( @$user_vars ) { | ||
3571 | 3555 | my ($var, $val) = $var_val =~ m/([^\s=]+)=(\S+)/; | ||
3572 | 3556 | die "Invalid --set-vars value: $var_val\n" unless $var && defined $val; | ||
3573 | 3557 | $user_vars{$var} = { | ||
3574 | 3558 | val => $val, | ||
3575 | 3559 | default => 0, | ||
3576 | 3560 | }; | ||
3577 | 3561 | } | ||
3578 | 3562 | } | ||
3579 | 3563 | |||
3580 | 3564 | my %default_vars; | ||
3581 | 3565 | my $default_vars = $self->read_para_after($file, qr/MAGIC_set_vars/); | ||
3582 | 3566 | if ( $default_vars ) { | ||
3583 | 3567 | %default_vars = map { | ||
3584 | 3568 | my $var_val = $_; | ||
3585 | 3569 | my ($var, $val) = $var_val =~ m/([^\s=]+)=(\S+)/; | ||
3586 | 3570 | die "Invalid --set-vars value: $var_val\n" unless $var && defined $val; | ||
3587 | 3571 | $var => { | ||
3588 | 3572 | val => $val, | ||
3589 | 3573 | default => 1, | ||
3590 | 3574 | }; | ||
3591 | 3575 | } split("\n", $default_vars); | ||
3592 | 3576 | } | ||
3593 | 3577 | |||
3594 | 3578 | my %vars = ( | ||
3595 | 3579 | %default_vars, # first the tool's defaults | ||
3596 | 3580 | %user_vars, # then the user's which overwrite the defaults | ||
3597 | 3581 | ); | ||
3598 | 3582 | PTDEBUG && _d('--set-vars:', Dumper(\%vars)); | ||
3599 | 3583 | return \%vars; | ||
3600 | 3584 | } | ||
3601 | 3585 | |||
3602 | 3586 | sub _d { | ||
3603 | 3587 | my ($package, undef, $line) = caller 0; | ||
3604 | 3588 | @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } | ||
3605 | 3589 | map { defined $_ ? $_ : 'undef' } | ||
3606 | 3590 | @_; | ||
3607 | 3591 | print STDERR "# $package:$line $PID ", join(' ', @_), "\n"; | ||
3608 | 3592 | } | ||
3609 | 3593 | |||
3610 | 3594 | if ( PTDEBUG ) { | ||
3611 | 3595 | print STDERR '# ', $^X, ' ', $], "\n"; | ||
3612 | 3596 | if ( my $uname = `uname -a` ) { | ||
3613 | 3597 | $uname =~ s/\s+/ /g; | ||
3614 | 3598 | print STDERR "# $uname\n"; | ||
3615 | 3599 | } | ||
3616 | 3600 | print STDERR '# Arguments: ', | ||
3617 | 3601 | join(' ', map { my $a = "_[$_]_"; $a =~ s/\n/\n# /g; $a; } @ARGV), "\n"; | ||
3618 | 3602 | } | ||
3619 | 3603 | |||
3620 | 3604 | 1; | ||
3621 | 3605 | } | ||
3622 | 3606 | # ########################################################################### | ||
3623 | 3607 | # End OptionParser package | ||
3624 | 3608 | # ########################################################################### | ||
3625 | 3609 | |||
3626 | 3610 | # ########################################################################### | ||
3627 | 3611 | # Cxn package | ||
3628 | 3612 | # This package is a copy without comments from the original. The original | ||
3629 | 3613 | # with comments and its test file can be found in the Bazaar repository at, | ||
3630 | 3614 | # lib/Cxn.pm | ||
3631 | 3615 | # t/lib/Cxn.t | ||
3632 | 3616 | # See https://launchpad.net/percona-toolkit for more information. | ||
3633 | 3617 | # ########################################################################### | ||
3634 | 3618 | { | ||
3635 | 3619 | package Cxn; | ||
3636 | 3620 | |||
3637 | 3621 | use strict; | ||
3638 | 3622 | use warnings FATAL => 'all'; | ||
3639 | 3623 | use English qw(-no_match_vars); | ||
3640 | 3624 | use Scalar::Util qw(blessed); | ||
3641 | 3625 | use constant { | ||
3642 | 3626 | PTDEBUG => $ENV{PTDEBUG} || 0, | ||
3643 | 3627 | PERCONA_TOOLKIT_TEST_USE_DSN_NAMES => $ENV{PERCONA_TOOLKIT_TEST_USE_DSN_NAMES} || 0, | ||
3644 | 3628 | }; | ||
3645 | 3629 | |||
3646 | 3630 | sub new { | ||
3647 | 3631 | my ( $class, %args ) = @_; | ||
3648 | 3632 | my @required_args = qw(DSNParser OptionParser); | ||
3649 | 3633 | foreach my $arg ( @required_args ) { | ||
3650 | 3634 | die "I need a $arg argument" unless $args{$arg}; | ||
3651 | 3635 | }; | ||
3652 | 3636 | my ($dp, $o) = @args{@required_args}; | ||
3653 | 3637 | |||
3654 | 3638 | my $dsn_defaults = $dp->parse_options($o); | ||
3655 | 3639 | my $prev_dsn = $args{prev_dsn}; | ||
3656 | 3640 | my $dsn = $args{dsn}; | ||
3657 | 3641 | if ( !$dsn ) { | ||
3658 | 3642 | $args{dsn_string} ||= 'h=' . ($dsn_defaults->{h} || 'localhost'); | ||
3659 | 3643 | |||
3660 | 3644 | $dsn = $dp->parse( | ||
3661 | 3645 | $args{dsn_string}, $prev_dsn, $dsn_defaults); | ||
3662 | 3646 | } | ||
3663 | 3647 | elsif ( $prev_dsn ) { | ||
3664 | 3648 | $dsn = $dp->copy($prev_dsn, $dsn); | ||
3665 | 3649 | } | ||
3666 | 3650 | |||
3667 | 3651 | my $dsn_name = $dp->as_string($dsn, [qw(h P S)]) | ||
3668 | 3652 | || $dp->as_string($dsn, [qw(F)]) | ||
3669 | 3653 | || ''; | ||
3670 | 3654 | |||
3671 | 3655 | my $self = { | ||
3672 | 3656 | dsn => $dsn, | ||
3673 | 3657 | dbh => $args{dbh}, | ||
3674 | 3658 | dsn_name => $dsn_name, | ||
3675 | 3659 | hostname => '', | ||
3676 | 3660 | set => $args{set}, | ||
3677 | 3661 | NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1, | ||
3678 | 3662 | dbh_set => 0, | ||
3679 | 3663 | ask_pass => $args{ask_pass}, | ||
3680 | 3664 | DSNParser => $dp, | ||
3681 | 3665 | is_cluster_node => undef, | ||
3682 | 3666 | parent => $args{parent}, | ||
3683 | 3667 | }; | ||
3684 | 3668 | |||
3685 | 3669 | return bless $self, $class; | ||
3686 | 3670 | } | ||
3687 | 3671 | |||
3688 | 3672 | sub connect { | ||
3689 | 3673 | my ( $self, %opts ) = @_; | ||
3690 | 3674 | my $dsn = $opts{dsn} || $self->{dsn}; | ||
3691 | 3675 | my $dp = $self->{DSNParser}; | ||
3692 | 3676 | |||
3693 | 3677 | my $dbh = $self->{dbh}; | ||
3694 | 3678 | if ( !$dbh || !$dbh->ping() ) { | ||
3695 | 3679 | if ( $self->{ask_pass} && !$self->{asked_for_pass} ) { | ||
3696 | 3680 | $dsn->{p} = OptionParser::prompt_noecho("Enter MySQL password: "); | ||
3697 | 3681 | $self->{asked_for_pass} = 1; | ||
3698 | 3682 | } | ||
3699 | 3683 | $dbh = $dp->get_dbh( | ||
3700 | 3684 | $dp->get_cxn_params($dsn), | ||
3701 | 3685 | { | ||
3702 | 3686 | AutoCommit => 1, | ||
3703 | 3687 | %opts, | ||
3704 | 3688 | }, | ||
3705 | 3689 | ); | ||
3706 | 3690 | } | ||
3707 | 3691 | |||
3708 | 3692 | $dbh = $self->set_dbh($dbh); | ||
3709 | 3693 | if ( $opts{dsn} ) { | ||
3710 | 3694 | $self->{dsn} = $dsn; | ||
3711 | 3695 | $self->{dsn_name} = $dp->as_string($dsn, [qw(h P S)]) | ||
3712 | 3696 | || $dp->as_string($dsn, [qw(F)]) | ||
3713 | 3697 | || ''; | ||
3714 | 3698 | |||
3715 | 3699 | } | ||
3716 | 3700 | PTDEBUG && _d($dbh, 'Connected dbh to', $self->{hostname},$self->{dsn_name}); | ||
3717 | 3701 | return $dbh; | ||
3718 | 3702 | } | ||
3719 | 3703 | |||
3720 | 3704 | sub set_dbh { | ||
3721 | 3705 | my ($self, $dbh) = @_; | ||
3722 | 3706 | |||
3723 | 3707 | if ( $self->{dbh} && $self->{dbh} == $dbh && $self->{dbh_set} ) { | ||
3724 | 3708 | PTDEBUG && _d($dbh, 'Already set dbh'); | ||
3725 | 3709 | return $dbh; | ||
3726 | 3710 | } | ||
3727 | 3711 | |||
3728 | 3712 | PTDEBUG && _d($dbh, 'Setting dbh'); | ||
3729 | 3713 | |||
3730 | 3714 | $dbh->{FetchHashKeyName} = 'NAME_lc' if $self->{NAME_lc}; | ||
3731 | 3715 | |||
3732 | 3716 | my $sql = 'SELECT @@server_id /*!50038 , @@hostname*/'; | ||
3733 | 3717 | PTDEBUG && _d($dbh, $sql); | ||
3734 | 3718 | my ($server_id, $hostname) = $dbh->selectrow_array($sql); | ||
3735 | 3719 | PTDEBUG && _d($dbh, 'hostname:', $hostname, $server_id); | ||
3736 | 3720 | if ( $hostname ) { | ||
3737 | 3721 | $self->{hostname} = $hostname; | ||
3738 | 3722 | } | ||
3739 | 3723 | |||
3740 | 3724 | if ( $self->{parent} ) { | ||
3741 | 3725 | PTDEBUG && _d($dbh, 'Setting InactiveDestroy=1 in parent'); | ||
3742 | 3726 | $dbh->{InactiveDestroy} = 1; | ||
3743 | 3727 | } | ||
3744 | 3728 | |||
3745 | 3729 | if ( my $set = $self->{set}) { | ||
3746 | 3730 | $set->($dbh); | ||
3747 | 3731 | } | ||
3748 | 3732 | |||
3749 | 3733 | $self->{dbh} = $dbh; | ||
3750 | 3734 | $self->{dbh_set} = 1; | ||
3751 | 3735 | return $dbh; | ||
3752 | 3736 | } | ||
3753 | 3737 | |||
3754 | 3738 | sub lost_connection { | ||
3755 | 3739 | my ($self, $e) = @_; | ||
3756 | 3740 | return 0 unless $e; | ||
3757 | 3741 | return $e =~ m/MySQL server has gone away/ | ||
3758 | 3742 | || $e =~ m/Lost connection to MySQL server/; | ||
3759 | 3743 | } | ||
3760 | 3744 | |||
3761 | 3745 | sub dbh { | ||
3762 | 3746 | my ($self) = @_; | ||
3763 | 3747 | return $self->{dbh}; | ||
3764 | 3748 | } | ||
3765 | 3749 | |||
3766 | 3750 | sub dsn { | ||
3767 | 3751 | my ($self) = @_; | ||
3768 | 3752 | return $self->{dsn}; | ||
3769 | 3753 | } | ||
3770 | 3754 | |||
3771 | 3755 | sub name { | ||
3772 | 3756 | my ($self) = @_; | ||
3773 | 3757 | return $self->{dsn_name} if PERCONA_TOOLKIT_TEST_USE_DSN_NAMES; | ||
3774 | 3758 | return $self->{hostname} || $self->{dsn_name} || 'unknown host'; | ||
3775 | 3759 | } | ||
3776 | 3760 | |||
3777 | 3761 | sub remove_duplicate_cxns { | ||
3778 | 3762 | my ($self, %args) = @_; | ||
3779 | 3763 | my @cxns = @{$args{cxns}}; | ||
3780 | 3764 | my $seen_ids = $args{seen_ids} || {}; | ||
3781 | 3765 | PTDEBUG && _d("Removing duplicates from ", join(" ", map { $_->name } @cxns)); | ||
3782 | 3766 | my @trimmed_cxns; | ||
3783 | 3767 | |||
3784 | 3768 | for my $cxn ( @cxns ) { | ||
3785 | 3769 | my $dbh = $cxn->dbh(); | ||
3786 | 3770 | my $sql = q{SELECT @@server_id}; | ||
3787 | 3771 | PTDEBUG && _d($sql); | ||
3788 | 3772 | my ($id) = $dbh->selectrow_array($sql); | ||
3789 | 3773 | PTDEBUG && _d('Server ID for ', $cxn->name, ': ', $id); | ||
3790 | 3774 | |||
3791 | 3775 | if ( ! $seen_ids->{$id}++ ) { | ||
3792 | 3776 | push @trimmed_cxns, $cxn | ||
3793 | 3777 | } | ||
3794 | 3778 | else { | ||
3795 | 3779 | PTDEBUG && _d("Removing ", $cxn->name, | ||
3796 | 3780 | ", ID ", $id, ", because we've already seen it"); | ||
3797 | 3781 | } | ||
3798 | 3782 | } | ||
3799 | 3783 | |||
3800 | 3784 | return \@trimmed_cxns; | ||
3801 | 3785 | } | ||
3802 | 3786 | |||
3803 | 3787 | sub DESTROY { | ||
3804 | 3788 | my ($self) = @_; | ||
3805 | 3789 | |||
3806 | 3790 | PTDEBUG && _d('Destroying cxn'); | ||
3807 | 3791 | |||
3808 | 3792 | if ( $self->{parent} ) { | ||
3809 | 3793 | PTDEBUG && _d($self->{dbh}, 'Not disconnecting dbh in parent'); | ||
3810 | 3794 | } | ||
3811 | 3795 | elsif ( $self->{dbh} | ||
3812 | 3796 | && blessed($self->{dbh}) | ||
3813 | 3797 | && $self->{dbh}->can("disconnect") ) | ||
3814 | 3798 | { | ||
3815 | 3799 | PTDEBUG && _d($self->{dbh}, 'Disconnecting dbh on', $self->{hostname}, | ||
3816 | 3800 | $self->{dsn_name}); | ||
3817 | 3801 | $self->{dbh}->disconnect(); | ||
3818 | 3802 | } | ||
3819 | 3803 | |||
3820 | 3804 | return; | ||
3821 | 3805 | } | ||
3822 | 3806 | |||
3823 | 3807 | sub _d { | ||
3824 | 3808 | my ($package, undef, $line) = caller 0; | ||
3825 | 3809 | @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } | ||
3826 | 3810 | map { defined $_ ? $_ : 'undef' } | ||
3827 | 3811 | @_; | ||
3828 | 3812 | print STDERR "# $package:$line $PID ", join(' ', @_), "\n"; | ||
3829 | 3813 | } | ||
3830 | 3814 | |||
3831 | 3815 | 1; | ||
3832 | 3816 | } | ||
3833 | 3817 | # ########################################################################### | ||
3834 | 3818 | # End Cxn package | ||
3835 | 3819 | # ########################################################################### | ||
3836 | 3820 | |||
3837 | 3821 | # ########################################################################### | ||
3838 | 3822 | # Quoter package | ||
3839 | 3823 | # This package is a copy without comments from the original. The original | ||
3840 | 3824 | # with comments and its test file can be found in the Bazaar repository at, | ||
3841 | 3825 | # lib/Quoter.pm | ||
3842 | 3826 | # t/lib/Quoter.t | ||
3843 | 3827 | # See https://launchpad.net/percona-toolkit for more information. | ||
3844 | 3828 | # ########################################################################### | ||
3845 | 3829 | { | ||
3846 | 3830 | package Quoter; | ||
3847 | 3831 | |||
3848 | 3832 | use strict; | ||
3849 | 3833 | use warnings FATAL => 'all'; | ||
3850 | 3834 | use English qw(-no_match_vars); | ||
3851 | 3835 | use constant PTDEBUG => $ENV{PTDEBUG} || 0; | ||
3852 | 3836 | |||
3853 | 3837 | use Data::Dumper; | ||
3854 | 3838 | $Data::Dumper::Indent = 1; | ||
3855 | 3839 | $Data::Dumper::Sortkeys = 1; | ||
3856 | 3840 | $Data::Dumper::Quotekeys = 0; | ||
3857 | 3841 | |||
3858 | 3842 | sub new { | ||
3859 | 3843 | my ( $class, %args ) = @_; | ||
3860 | 3844 | return bless {}, $class; | ||
3861 | 3845 | } | ||
3862 | 3846 | |||
3863 | 3847 | sub quote { | ||
3864 | 3848 | my ( $self, @vals ) = @_; | ||
3865 | 3849 | foreach my $val ( @vals ) { | ||
3866 | 3850 | $val =~ s/`/``/g; | ||
3867 | 3851 | } | ||
3868 | 3852 | return join('.', map { '`' . $_ . '`' } @vals); | ||
3869 | 3853 | } | ||
3870 | 3854 | |||
3871 | 3855 | sub quote_val { | ||
3872 | 3856 | my ( $self, $val, %args ) = @_; | ||
3873 | 3857 | |||
3874 | 3858 | return 'NULL' unless defined $val; # undef = NULL | ||
3875 | 3859 | return "''" if $val eq ''; # blank string = '' | ||
3876 | 3860 | return $val if $val =~ m/^0x[0-9a-fA-F]+$/ # quote hex data | ||
3877 | 3861 | && !$args{is_char}; # unless is_char is true | ||
3878 | 3862 | |||
3879 | 3863 | return $val if $args{is_float}; | ||
3880 | 3864 | |||
3881 | 3865 | $val =~ s/(['\\])/\\$1/g; | ||
3882 | 3866 | return "'$val'"; | ||
3883 | 3867 | } | ||
3884 | 3868 | |||
3885 | 3869 | sub split_unquote { | ||
3886 | 3870 | my ( $self, $db_tbl, $default_db ) = @_; | ||
3887 | 3871 | my ( $db, $tbl ) = split(/[.]/, $db_tbl); | ||
3888 | 3872 | if ( !$tbl ) { | ||
3889 | 3873 | $tbl = $db; | ||
3890 | 3874 | $db = $default_db; | ||
3891 | 3875 | } | ||
3892 | 3876 | for ($db, $tbl) { | ||
3893 | 3877 | next unless $_; | ||
3894 | 3878 | s/\A`//; | ||
3895 | 3879 | s/`\z//; | ||
3896 | 3880 | s/``/`/g; | ||
3897 | 3881 | } | ||
3898 | 3882 | |||
3899 | 3883 | return ($db, $tbl); | ||
3900 | 3884 | } | ||
3901 | 3885 | |||
3902 | 3886 | sub literal_like { | ||
3903 | 3887 | my ( $self, $like ) = @_; | ||
3904 | 3888 | return unless $like; | ||
3905 | 3889 | $like =~ s/([%_])/\\$1/g; | ||
3906 | 3890 | return "'$like'"; | ||
3907 | 3891 | } | ||
3908 | 3892 | |||
3909 | 3893 | sub join_quote { | ||
3910 | 3894 | my ( $self, $default_db, $db_tbl ) = @_; | ||
3911 | 3895 | return unless $db_tbl; | ||
3912 | 3896 | my ($db, $tbl) = split(/[.]/, $db_tbl); | ||
3913 | 3897 | if ( !$tbl ) { | ||
3914 | 3898 | $tbl = $db; | ||
3915 | 3899 | $db = $default_db; | ||
3916 | 3900 | } | ||
3917 | 3901 | $db = "`$db`" if $db && $db !~ m/^`/; | ||
3918 | 3902 | $tbl = "`$tbl`" if $tbl && $tbl !~ m/^`/; | ||
3919 | 3903 | return $db ? "$db.$tbl" : $tbl; | ||
3920 | 3904 | } | ||
3921 | 3905 | |||
3922 | 3906 | sub serialize_list { | ||
3923 | 3907 | my ( $self, @args ) = @_; | ||
3924 | 3908 | PTDEBUG && _d('Serializing', Dumper(\@args)); | ||
3925 | 3909 | return unless @args; | ||
3926 | 3910 | |||
3927 | 3911 | my @parts; | ||
3928 | 3912 | foreach my $arg ( @args ) { | ||
3929 | 3913 | if ( defined $arg ) { | ||
3930 | 3914 | $arg =~ s/,/\\,/g; # escape commas | ||
3931 | 3915 | $arg =~ s/\\N/\\\\N/g; # escape literal \N | ||
3932 | 3916 | push @parts, $arg; | ||
3933 | 3917 | } | ||
3934 | 3918 | else { | ||
3935 | 3919 | push @parts, '\N'; | ||
3936 | 3920 | } | ||
3937 | 3921 | } | ||
3938 | 3922 | |||
3939 | 3923 | my $string = join(',', @parts); | ||
3940 | 3924 | PTDEBUG && _d('Serialized: <', $string, '>'); | ||
3941 | 3925 | return $string; | ||
3942 | 3926 | } | ||
3943 | 3927 | |||
3944 | 3928 | sub deserialize_list { | ||
3945 | 3929 | my ( $self, $string ) = @_; | ||
3946 | 3930 | PTDEBUG && _d('Deserializing <', $string, '>'); | ||
3947 | 3931 | die "Cannot deserialize an undefined string" unless defined $string; | ||
3948 | 3932 | |||
3949 | 3933 | my @parts; | ||
3950 | 3934 | foreach my $arg ( split(/(?<!\\),/, $string) ) { | ||
3951 | 3935 | if ( $arg eq '\N' ) { | ||
3952 | 3936 | $arg = undef; | ||
3953 | 3937 | } | ||
3954 | 3938 | else { | ||
3955 | 3939 | $arg =~ s/\\,/,/g; | ||
3956 | 3940 | $arg =~ s/\\\\N/\\N/g; | ||
3957 | 3941 | } | ||
3958 | 3942 | push @parts, $arg; | ||
3959 | 3943 | } | ||
3960 | 3944 | |||
3961 | 3945 | if ( !@parts ) { | ||
3962 | 3946 | my $n_empty_strings = $string =~ tr/,//; | ||
3963 | 3947 | $n_empty_strings++; | ||
3964 | 3948 | PTDEBUG && _d($n_empty_strings, 'empty strings'); | ||
3965 | 3949 | map { push @parts, '' } 1..$n_empty_strings; | ||
3966 | 3950 | } | ||
3967 | 3951 | elsif ( $string =~ m/(?<!\\),$/ ) { | ||
3968 | 3952 | PTDEBUG && _d('Last value is an empty string'); | ||
3969 | 3953 | push @parts, ''; | ||
3970 | 3954 | } | ||
3971 | 3955 | |||
3972 | 3956 | PTDEBUG && _d('Deserialized', Dumper(\@parts)); | ||
3973 | 3957 | return @parts; | ||
3974 | 3958 | } | ||
3975 | 3959 | |||
3976 | 3960 | sub _d { | ||
3977 | 3961 | my ($package, undef, $line) = caller 0; | ||
3978 | 3962 | @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } | ||
3979 | 3963 | map { defined $_ ? $_ : 'undef' } | ||
3980 | 3964 | @_; | ||
3981 | 3965 | print STDERR "# $package:$line $PID ", join(' ', @_), "\n"; | ||
3982 | 3966 | } | ||
3983 | 3967 | |||
3984 | 3968 | 1; | ||
3985 | 3969 | } | ||
3986 | 3970 | # ########################################################################### | ||
3987 | 3971 | # End Quoter package | ||
3988 | 3972 | # ########################################################################### | ||
3989 | 3973 | |||
3990 | 3974 | # ########################################################################### | ||
3991 | 3975 | # VersionParser package | ||
3992 | 3976 | # This package is a copy without comments from the original. The original | ||
3993 | 3977 | # with comments and its test file can be found in the Bazaar repository at, | ||
3994 | 3978 | # lib/VersionParser.pm | ||
3995 | 3979 | # t/lib/VersionParser.t | ||
3996 | 3980 | # See https://launchpad.net/percona-toolkit for more information. | ||
3997 | 3981 | # ########################################################################### | ||
3998 | 3982 | { | ||
3999 | 3983 | package VersionParser; | ||
4000 | 3984 | |||
4001 | 3985 | use Lmo; | ||
4002 | 3986 | use Scalar::Util qw(blessed); | ||
4003 | 3987 | use English qw(-no_match_vars); | ||
4004 | 3988 | use constant PTDEBUG => $ENV{PTDEBUG} || 0; | ||
4005 | 3989 | |||
4006 | 3990 | use overload ( | ||
4007 | 3991 | '""' => "version", | ||
4008 | 3992 | '<=>' => "cmp", | ||
4009 | 3993 | 'cmp' => "cmp", | ||
4010 | 3994 | fallback => 1, | ||
4011 | 3995 | ); | ||
4012 | 3996 | |||
4013 | 3997 | use Carp (); | ||
4014 | 3998 | |||
4015 | 3999 | has major => ( | ||
4016 | 4000 | is => 'ro', | ||
4017 | 4001 | isa => 'Int', | ||
4018 | 4002 | required => 1, | ||
4019 | 4003 | ); | ||
4020 | 4004 | |||
4021 | 4005 | has [qw( minor revision )] => ( | ||
4022 | 4006 | is => 'ro', | ||
4023 | 4007 | isa => 'Num', | ||
4024 | 4008 | ); | ||
4025 | 4009 | |||
4026 | 4010 | has flavor => ( | ||
4027 | 4011 | is => 'ro', | ||
4028 | 4012 | isa => 'Str', | ||
4029 | 4013 | default => sub { 'Unknown' }, | ||
4030 | 4014 | ); | ||
4031 | 4015 | |||
4032 | 4016 | has innodb_version => ( | ||
4033 | 4017 | is => 'ro', | ||
4034 | 4018 | isa => 'Str', | ||
4035 | 4019 | default => sub { 'NO' }, | ||
4036 | 4020 | ); | ||
4037 | 4021 | |||
4038 | 4022 | sub series { | ||
4039 | 4023 | my $self = shift; | ||
4040 | 4024 | return $self->_join_version($self->major, $self->minor); | ||
4041 | 4025 | } | ||
4042 | 4026 | |||
4043 | 4027 | sub version { | ||
4044 | 4028 | my $self = shift; | ||
4045 | 4029 | return $self->_join_version($self->major, $self->minor, $self->revision); | ||
4046 | 4030 | } | ||
4047 | 4031 | |||
4048 | 4032 | sub is_in { | ||
4049 | 4033 | my ($self, $target) = @_; | ||
4050 | 4034 | |||
4051 | 4035 | return $self eq $target; | ||
4052 | 4036 | } | ||
4053 | 4037 | |||
4054 | 4038 | sub _join_version { | ||
4055 | 4039 | my ($self, @parts) = @_; | ||
4056 | 4040 | |||
4057 | 4041 | return join ".", map { my $c = $_; $c =~ s/^0\./0/; $c } grep defined, @parts; | ||
4058 | 4042 | } | ||
4059 | 4043 | sub _split_version { | ||
4060 | 4044 | my ($self, $str) = @_; | ||
4061 | 4045 | my @version_parts = map { s/^0(?=\d)/0./; $_ } $str =~ m/(\d+)/g; | ||
4062 | 4046 | return @version_parts[0..2]; | ||
4063 | 4047 | } | ||
4064 | 4048 | |||
4065 | 4049 | sub normalized_version { | ||
4066 | 4050 | my ( $self ) = @_; | ||
4067 | 4051 | my $result = sprintf('%d%02d%02d', map { $_ || 0 } $self->major, | ||
4068 | 4052 | $self->minor, | ||
4069 | 4053 | $self->revision); | ||
4070 | 4054 | PTDEBUG && _d($self->version, 'normalizes to', $result); | ||
4071 | 4055 | return $result; | ||
4072 | 4056 | } | ||
4073 | 4057 | |||
4074 | 4058 | sub comment { | ||
4075 | 4059 | my ( $self, $cmd ) = @_; | ||
4076 | 4060 | my $v = $self->normalized_version(); | ||
4077 | 4061 | |||
4078 | 4062 | return "/*!$v $cmd */" | ||
4079 | 4063 | } | ||
4080 | 4064 | |||
4081 | 4065 | my @methods = qw(major minor revision); | ||
4082 | 4066 | sub cmp { | ||
4083 | 4067 | my ($left, $right) = @_; | ||
4084 | 4068 | my $right_obj = (blessed($right) && $right->isa(ref($left))) | ||
4085 | 4069 | ? $right | ||
4086 | 4070 | : ref($left)->new($right); | ||
4087 | 4071 | |||
4088 | 4072 | my $retval = 0; | ||
4089 | 4073 | for my $m ( @methods ) { | ||
4090 | 4074 | last unless defined($left->$m) && defined($right_obj->$m); | ||
4091 | 4075 | $retval = $left->$m <=> $right_obj->$m; | ||
4092 | 4076 | last if $retval; | ||
4093 | 4077 | } | ||
4094 | 4078 | return $retval; | ||
4095 | 4079 | } | ||
4096 | 4080 | |||
4097 | 4081 | sub BUILDARGS { | ||
4098 | 4082 | my $self = shift; | ||
4099 | 4083 | |||
4100 | 4084 | if ( @_ == 1 ) { | ||
4101 | 4085 | my %args; | ||
4102 | 4086 | if ( blessed($_[0]) && $_[0]->can("selectrow_hashref") ) { | ||
4103 | 4087 | PTDEBUG && _d("VersionParser got a dbh, trying to get the version"); | ||
4104 | 4088 | my $dbh = $_[0]; | ||
4105 | 4089 | local $dbh->{FetchHashKeyName} = 'NAME_lc'; | ||
4106 | 4090 | my $query = eval { | ||
4107 | 4091 | $dbh->selectall_arrayref(q/SHOW VARIABLES LIKE 'version%'/, { Slice => {} }) | ||
4108 | 4092 | }; | ||
4109 | 4093 | if ( $query ) { | ||
4110 | 4094 | $query = { map { $_->{variable_name} => $_->{value} } @$query }; | ||
4111 | 4095 | @args{@methods} = $self->_split_version($query->{version}); | ||
4112 | 4096 | $args{flavor} = delete $query->{version_comment} | ||
4113 | 4097 | if $query->{version_comment}; | ||
4114 | 4098 | } | ||
4115 | 4099 | elsif ( eval { ($query) = $dbh->selectrow_array(q/SELECT VERSION()/) } ) { | ||
4116 | 4100 | @args{@methods} = $self->_split_version($query); | ||
4117 | 4101 | } | ||
4118 | 4102 | else { | ||
4119 | 4103 | Carp::confess("Couldn't get the version from the dbh while " | ||
4120 | 4104 | . "creating a VersionParser object: $@"); | ||
4121 | 4105 | } | ||
4122 | 4106 | $args{innodb_version} = eval { $self->_innodb_version($dbh) }; | ||
4123 | 4107 | } | ||
4124 | 4108 | elsif ( !ref($_[0]) ) { | ||
4125 | 4109 | @args{@methods} = $self->_split_version($_[0]); | ||
4126 | 4110 | } | ||
4127 | 4111 | |||
4128 | 4112 | for my $method (@methods) { | ||
4129 | 4113 | delete $args{$method} unless defined $args{$method}; | ||
4130 | 4114 | } | ||
4131 | 4115 | @_ = %args if %args; | ||
4132 | 4116 | } | ||
4133 | 4117 | |||
4134 | 4118 | return $self->SUPER::BUILDARGS(@_); | ||
4135 | 4119 | } | ||
4136 | 4120 | |||
4137 | 4121 | sub _innodb_version { | ||
4138 | 4122 | my ( $self, $dbh ) = @_; | ||
4139 | 4123 | return unless $dbh; | ||
4140 | 4124 | my $innodb_version = "NO"; | ||
4141 | 4125 | |||
4142 | 4126 | my ($innodb) = | ||
4143 | 4127 | grep { $_->{engine} =~ m/InnoDB/i } | ||
4144 | 4128 | map { | ||
4145 | 4129 | my %hash; | ||
4146 | 4130 | @hash{ map { lc $_ } keys %$_ } = values %$_; | ||
4147 | 4131 | \%hash; | ||
4148 | 4132 | } | ||
4149 | 4133 | @{ $dbh->selectall_arrayref("SHOW ENGINES", {Slice=>{}}) }; | ||
4150 | 4134 | if ( $innodb ) { | ||
4151 | 4135 | PTDEBUG && _d("InnoDB support:", $innodb->{support}); | ||
4152 | 4136 | if ( $innodb->{support} =~ m/YES|DEFAULT/i ) { | ||
4153 | 4137 | my $vars = $dbh->selectrow_hashref( | ||
4154 | 4138 | "SHOW VARIABLES LIKE 'innodb_version'"); | ||
4155 | 4139 | $innodb_version = !$vars ? "BUILTIN" | ||
4156 | 4140 | : ($vars->{Value} || $vars->{value}); | ||
4157 | 4141 | } | ||
4158 | 4142 | else { | ||
4159 | 4143 | $innodb_version = $innodb->{support}; # probably DISABLED or NO | ||
4160 | 4144 | } | ||
4161 | 4145 | } | ||
4162 | 4146 | |||
4163 | 4147 | PTDEBUG && _d("InnoDB version:", $innodb_version); | ||
4164 | 4148 | return $innodb_version; | ||
4165 | 4149 | } | ||
4166 | 4150 | |||
4167 | 4151 | sub _d { | ||
4168 | 4152 | my ($package, undef, $line) = caller 0; | ||
4169 | 4153 | @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } | ||
4170 | 4154 | map { defined $_ ? $_ : 'undef' } | ||
4171 | 4155 | @_; | ||
4172 | 4156 | print STDERR "# $package:$line $PID ", join(' ', @_), "\n"; | ||
4173 | 4157 | } | ||
4174 | 4158 | |||
4175 | 4159 | no Lmo; | ||
4176 | 4160 | 1; | ||
4177 | 4161 | } | ||
4178 | 4162 | # ########################################################################### | ||
4179 | 4163 | # End VersionParser package | ||
4180 | 4164 | # ########################################################################### | ||
4181 | 4165 | |||
4182 | 4166 | # ########################################################################### | ||
4183 | 4167 | # Daemon package | ||
4184 | 4168 | # This package is a copy without comments from the original. The original | ||
4185 | 4169 | # with comments and its test file can be found in the Bazaar repository at, | ||
4186 | 4170 | # lib/Daemon.pm | ||
4187 | 4171 | # t/lib/Daemon.t | ||
4188 | 4172 | # See https://launchpad.net/percona-toolkit for more information. | ||
4189 | 4173 | # ########################################################################### | ||
4190 | 4174 | { | ||
4191 | 4175 | package Daemon; | ||
4192 | 4176 | |||
4193 | 4177 | use strict; | ||
4194 | 4178 | use warnings FATAL => 'all'; | ||
4195 | 4179 | use English qw(-no_match_vars); | ||
4196 | 4180 | |||
4197 | 4181 | use constant PTDEBUG => $ENV{PTDEBUG} || 0; | ||
4198 | 4182 | |||
4199 | 4183 | use POSIX qw(setsid); | ||
4200 | 4184 | use Fcntl qw(:DEFAULT); | ||
4201 | 4185 | |||
4202 | 4186 | sub new { | ||
4203 | 4187 | my ($class, %args) = @_; | ||
4204 | 4188 | my $self = { | ||
4205 | 4189 | log_file => $args{log_file}, | ||
4206 | 4190 | pid_file => $args{pid_file}, | ||
4207 | 4191 | daemonize => $args{daemonize}, | ||
4208 | 4192 | force_log_file => $args{force_log_file}, | ||
4209 | 4193 | parent_exit => $args{parent_exit}, | ||
4210 | 4194 | pid_file_owner => 0, | ||
4211 | 4195 | }; | ||
4212 | 4196 | return bless $self, $class; | ||
4213 | 4197 | } | ||
4214 | 4198 | |||
4215 | 4199 | sub run { | ||
4216 | 4200 | my ($self) = @_; | ||
4217 | 4201 | |||
4218 | 4202 | my $daemonize = $self->{daemonize}; | ||
4219 | 4203 | my $pid_file = $self->{pid_file}; | ||
4220 | 4204 | my $log_file = $self->{log_file}; | ||
4221 | 4205 | my $force_log_file = $self->{force_log_file}; | ||
4222 | 4206 | my $parent_exit = $self->{parent_exit}; | ||
4223 | 4207 | |||
4224 | 4208 | PTDEBUG && _d('Starting daemon'); | ||
4225 | 4209 | |||
4226 | 4210 | if ( $pid_file ) { | ||
4227 | 4211 | eval { | ||
4228 | 4212 | $self->_make_pid_file( | ||
4229 | 4213 | pid => $PID, # parent's pid | ||
4230 | 4214 | pid_file => $pid_file, | ||
4231 | 4215 | ); | ||
4232 | 4216 | }; | ||
4233 | 4217 | die "$EVAL_ERROR\n" if $EVAL_ERROR; | ||
4234 | 4218 | if ( !$daemonize ) { | ||
4235 | 4219 | $self->{pid_file_owner} = $PID; # parent's pid | ||
4236 | 4220 | } | ||
4237 | 4221 | } | ||
4238 | 4222 | |||
4239 | 4223 | if ( $daemonize ) { | ||
4240 | 4224 | defined (my $child_pid = fork()) or die "Cannot fork: $OS_ERROR"; | ||
4241 | 4225 | if ( $child_pid ) { | ||
4242 | 4226 | PTDEBUG && _d('Forked child', $child_pid); | ||
4243 | 4227 | $parent_exit->($child_pid) if $parent_exit; | ||
4244 | 4228 | exit 0; | ||
4245 | 4229 | } | ||
4246 | 4230 | |||
4247 | 4231 | POSIX::setsid() or die "Cannot start a new session: $OS_ERROR"; | ||
4248 | 4232 | chdir '/' or die "Cannot chdir to /: $OS_ERROR"; | ||
4249 | 4233 | |||
4250 | 4234 | if ( $pid_file ) { | ||
4251 | 4235 | $self->_update_pid_file( | ||
4252 | 4236 | pid => $PID, # child's pid | ||
4253 | 4237 | pid_file => $pid_file, | ||
4254 | 4238 | ); | ||
4255 | 4239 | $self->{pid_file_owner} = $PID; | ||
4256 | 4240 | } | ||
4257 | 4241 | } | ||
4258 | 4242 | |||
4259 | 4243 | if ( $daemonize || $force_log_file ) { | ||
4260 | 4244 | PTDEBUG && _d('Redirecting STDIN to /dev/null'); | ||
4261 | 4245 | close STDIN; | ||
4262 | 4246 | open STDIN, '/dev/null' | ||
4263 | 4247 | or die "Cannot reopen STDIN to /dev/null: $OS_ERROR"; | ||
4264 | 4248 | if ( $log_file ) { | ||
4265 | 4249 | PTDEBUG && _d('Redirecting STDOUT and STDERR to', $log_file); | ||
4266 | 4250 | close STDOUT; | ||
4267 | 4251 | open STDOUT, '>>', $log_file | ||
4268 | 4252 | or die "Cannot open log file $log_file: $OS_ERROR"; | ||
4269 | 4253 | |||
4270 | 4254 | close STDERR; | ||
4271 | 4255 | open STDERR, ">&STDOUT" | ||
4272 | 4256 | or die "Cannot dupe STDERR to STDOUT: $OS_ERROR"; | ||
4273 | 4257 | } | ||
4274 | 4258 | else { | ||
4275 | 4259 | if ( -t STDOUT ) { | ||
4276 | 4260 | PTDEBUG && _d('No log file and STDOUT is a terminal;', | ||
4277 | 4261 | 'redirecting to /dev/null'); | ||
4278 | 4262 | close STDOUT; | ||
4279 | 4263 | open STDOUT, '>', '/dev/null' | ||
4280 | 4264 | or die "Cannot reopen STDOUT to /dev/null: $OS_ERROR"; | ||
4281 | 4265 | } | ||
4282 | 4266 | if ( -t STDERR ) { | ||
4283 | 4267 | PTDEBUG && _d('No log file and STDERR is a terminal;', | ||
4284 | 4268 | 'redirecting to /dev/null'); | ||
4285 | 4269 | close STDERR; | ||
4286 | 4270 | open STDERR, '>', '/dev/null' | ||
4287 | 4271 | or die "Cannot reopen STDERR to /dev/null: $OS_ERROR"; | ||
4288 | 4272 | } | ||
4289 | 4273 | } | ||
4290 | 4274 | |||
4291 | 4275 | $OUTPUT_AUTOFLUSH = 1; | ||
4292 | 4276 | } | ||
4293 | 4277 | |||
4294 | 4278 | PTDEBUG && _d('Daemon running'); | ||
4295 | 4279 | return; | ||
4296 | 4280 | } | ||
4297 | 4281 | |||
4298 | 4282 | sub _make_pid_file { | ||
4299 | 4283 | my ($self, %args) = @_; | ||
4300 | 4284 | my @required_args = qw(pid pid_file); | ||
4301 | 4285 | foreach my $arg ( @required_args ) { | ||
4302 | 4286 | die "I need a $arg argument" unless $args{$arg}; | ||
4303 | 4287 | }; | ||
4304 | 4288 | my $pid = $args{pid}; | ||
4305 | 4289 | my $pid_file = $args{pid_file}; | ||
4306 | 4290 | |||
4307 | 4291 | eval { | ||
4308 | 4292 | sysopen(PID_FH, $pid_file, O_RDWR|O_CREAT|O_EXCL) or die $OS_ERROR; | ||
4309 | 4293 | print PID_FH $PID, "\n"; | ||
4310 | 4294 | close PID_FH; | ||
4311 | 4295 | }; | ||
4312 | 4296 | if ( my $e = $EVAL_ERROR ) { | ||
4313 | 4297 | if ( $e =~ m/file exists/i ) { | ||
4314 | 4298 | my $old_pid = $self->_check_pid_file( | ||
4315 | 4299 | pid_file => $pid_file, | ||
4316 | 4300 | pid => $PID, | ||
4317 | 4301 | ); | ||
4318 | 4302 | if ( $old_pid ) { | ||
4319 | 4303 | warn "Overwriting PID file $pid_file because PID $old_pid " | ||
4320 | 4304 | . "is not running.\n"; | ||
4321 | 4305 | } | ||
4322 | 4306 | $self->_update_pid_file( | ||
4323 | 4307 | pid => $PID, | ||
4324 | 4308 | pid_file => $pid_file | ||
4325 | 4309 | ); | ||
4326 | 4310 | } | ||
4327 | 4311 | else { | ||
4328 | 4312 | die "Error creating PID file $pid_file: $e\n"; | ||
4329 | 4313 | } | ||
4330 | 4314 | } | ||
4331 | 4315 | |||
4332 | 4316 | return; | ||
4333 | 4317 | } | ||
4334 | 4318 | |||
4335 | 4319 | sub _check_pid_file { | ||
4336 | 4320 | my ($self, %args) = @_; | ||
4337 | 4321 | my @required_args = qw(pid_file pid); | ||
4338 | 4322 | foreach my $arg ( @required_args ) { | ||
4339 | 4323 | die "I need a $arg argument" unless $args{$arg}; | ||
4340 | 4324 | }; | ||
4341 | 4325 | my $pid_file = $args{pid_file}; | ||
4342 | 4326 | my $pid = $args{pid}; | ||
4343 | 4327 | |||
4344 | 4328 | PTDEBUG && _d('Checking if PID in', $pid_file, 'is running'); | ||
4345 | 4329 | |||
4346 | 4330 | if ( ! -f $pid_file ) { | ||
4347 | 4331 | PTDEBUG && _d('PID file', $pid_file, 'does not exist'); | ||
4348 | 4332 | return; | ||
4349 | 4333 | } | ||
4350 | 4334 | |||
4351 | 4335 | open my $fh, '<', $pid_file | ||
4352 | 4336 | or die "Error opening $pid_file: $OS_ERROR"; | ||
4353 | 4337 | my $existing_pid = do { local $/; <$fh> }; | ||
4354 | 4338 | chomp($existing_pid) if $existing_pid; | ||
4355 | 4339 | close $fh | ||
4356 | 4340 | or die "Error closing $pid_file: $OS_ERROR"; | ||
4357 | 4341 | |||
4358 | 4342 | if ( $existing_pid ) { | ||
4359 | 4343 | if ( $existing_pid == $pid ) { | ||
4360 | 4344 | warn "The current PID $pid already holds the PID file $pid_file\n"; | ||
4361 | 4345 | return; | ||
4362 | 4346 | } | ||
4363 | 4347 | else { | ||
4364 | 4348 | PTDEBUG && _d('Checking if PID', $existing_pid, 'is running'); | ||
4365 | 4349 | my $pid_is_alive = kill 0, $existing_pid; | ||
4366 | 4350 | if ( $pid_is_alive ) { | ||
4367 | 4351 | die "PID file $pid_file exists and PID $existing_pid is running\n"; | ||
4368 | 4352 | } | ||
4369 | 4353 | } | ||
4370 | 4354 | } | ||
4371 | 4355 | else { | ||
4372 | 4356 | die "PID file $pid_file exists but it is empty. Remove the file " | ||
4373 | 4357 | . "if the process is no longer running.\n"; | ||
4374 | 4358 | } | ||
4375 | 4359 | |||
4376 | 4360 | return $existing_pid; | ||
4377 | 4361 | } | ||
4378 | 4362 | |||
4379 | 4363 | sub _update_pid_file { | ||
4380 | 4364 | my ($self, %args) = @_; | ||
4381 | 4365 | my @required_args = qw(pid pid_file); | ||
4382 | 4366 | foreach my $arg ( @required_args ) { | ||
4383 | 4367 | die "I need a $arg argument" unless $args{$arg}; | ||
4384 | 4368 | }; | ||
4385 | 4369 | my $pid = $args{pid}; | ||
4386 | 4370 | my $pid_file = $args{pid_file}; | ||
4387 | 4371 | |||
4388 | 4372 | open my $fh, '>', $pid_file | ||
4389 | 4373 | or die "Cannot open $pid_file: $OS_ERROR"; | ||
4390 | 4374 | print { $fh } $pid, "\n" | ||
4391 | 4375 | or die "Cannot print to $pid_file: $OS_ERROR"; | ||
4392 | 4376 | close $fh | ||
4393 | 4377 | or warn "Cannot close $pid_file: $OS_ERROR"; | ||
4394 | 4378 | |||
4395 | 4379 | return; | ||
4396 | 4380 | } | ||
4397 | 4381 | |||
4398 | 4382 | sub remove_pid_file { | ||
4399 | 4383 | my ($self, $pid_file) = @_; | ||
4400 | 4384 | $pid_file ||= $self->{pid_file}; | ||
4401 | 4385 | if ( $pid_file && -f $pid_file ) { | ||
4402 | 4386 | unlink $self->{pid_file} | ||
4403 | 4387 | or warn "Cannot remove PID file $pid_file: $OS_ERROR"; | ||
4404 | 4388 | PTDEBUG && _d('Removed PID file'); | ||
4405 | 4389 | } | ||
4406 | 4390 | else { | ||
4407 | 4391 | PTDEBUG && _d('No PID to remove'); | ||
4408 | 4392 | } | ||
4409 | 4393 | return; | ||
4410 | 4394 | } | ||
4411 | 4395 | |||
4412 | 4396 | sub DESTROY { | ||
4413 | 4397 | my ($self) = @_; | ||
4414 | 4398 | |||
4415 | 4399 | if ( $self->{pid_file_owner} == $PID ) { | ||
4416 | 4400 | $self->remove_pid_file(); | ||
4417 | 4401 | } | ||
4418 | 4402 | |||
4419 | 4403 | return; | ||
4420 | 4404 | } | ||
4421 | 4405 | |||
4422 | 4406 | sub _d { | ||
4423 | 4407 | my ($package, undef, $line) = caller 0; | ||
4424 | 4408 | @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } | ||
4425 | 4409 | map { defined $_ ? $_ : 'undef' } | ||
4426 | 4410 | @_; | ||
4427 | 4411 | print STDERR "# $package:$line $PID ", join(' ', @_), "\n"; | ||
4428 | 4412 | } | ||
4429 | 4413 | |||
4430 | 4414 | 1; | ||
4431 | 4415 | } | ||
4432 | 4416 | # ########################################################################### | ||
4433 | 4417 | # End Daemon package | ||
4434 | 4418 | # ########################################################################### | ||
4435 | 4419 | |||
4436 | 4420 | # ########################################################################### | ||
4437 | 4421 | # Transformers package | ||
4438 | 4422 | # This package is a copy without comments from the original. The original | ||
4439 | 4423 | # with comments and its test file can be found in the Bazaar repository at, | ||
4440 | 4424 | # lib/Transformers.pm | ||
4441 | 4425 | # t/lib/Transformers.t | ||
4442 | 4426 | # See https://launchpad.net/percona-toolkit for more information. | ||
4443 | 4427 | # ########################################################################### | ||
4444 | 4428 | { | ||
4445 | 4429 | package Transformers; | ||
4446 | 4430 | |||
4447 | 4431 | use strict; | ||
4448 | 4432 | use warnings FATAL => 'all'; | ||
4449 | 4433 | use English qw(-no_match_vars); | ||
4450 | 4434 | use constant PTDEBUG => $ENV{PTDEBUG} || 0; | ||
4451 | 4435 | |||
4452 | 4436 | use Time::Local qw(timegm timelocal); | ||
4453 | 4437 | use Digest::MD5 qw(md5_hex); | ||
4454 | 4438 | use B qw(); | ||
4455 | 4439 | |||
4456 | 4440 | BEGIN { | ||
4457 | 4441 | require Exporter; | ||
4458 | 4442 | our @ISA = qw(Exporter); | ||
4459 | 4443 | our %EXPORT_TAGS = (); | ||
4460 | 4444 | our @EXPORT = (); | ||
4461 | 4445 | our @EXPORT_OK = qw( | ||
4462 | 4446 | micro_t | ||
4463 | 4447 | percentage_of | ||
4464 | 4448 | secs_to_time | ||
4465 | 4449 | time_to_secs | ||
4466 | 4450 | shorten | ||
4467 | 4451 | ts | ||
4468 | 4452 | parse_timestamp | ||
4469 | 4453 | unix_timestamp | ||
4470 | 4454 | any_unix_timestamp | ||
4471 | 4455 | make_checksum | ||
4472 | 4456 | crc32 | ||
4473 | 4457 | encode_json | ||
4474 | 4458 | ); | ||
4475 | 4459 | } | ||
4476 | 4460 | |||
4477 | 4461 | our $mysql_ts = qr/(\d\d)(\d\d)(\d\d) +(\d+):(\d+):(\d+)(\.\d+)?/; | ||
4478 | 4462 | our $proper_ts = qr/(\d\d\d\d)-(\d\d)-(\d\d)[T ](\d\d):(\d\d):(\d\d)(\.\d+)?/; | ||
4479 | 4463 | our $n_ts = qr/(\d{1,5})([shmd]?)/; # Limit \d{1,5} because \d{6} looks | ||
4480 | 4464 | |||
4481 | 4465 | sub micro_t { | ||
4482 | 4466 | my ( $t, %args ) = @_; | ||
4483 | 4467 | my $p_ms = defined $args{p_ms} ? $args{p_ms} : 0; # precision for ms vals | ||
4484 | 4468 | my $p_s = defined $args{p_s} ? $args{p_s} : 0; # precision for s vals | ||
4485 | 4469 | my $f; | ||
4486 | 4470 | |||
4487 | 4471 | $t = 0 if $t < 0; | ||
4488 | 4472 | |||
4489 | 4473 | $t = sprintf('%.17f', $t) if $t =~ /e/; | ||
4490 | 4474 | |||
4491 | 4475 | $t =~ s/\.(\d{1,6})\d*/\.$1/; | ||
4492 | 4476 | |||
4493 | 4477 | if ($t > 0 && $t <= 0.000999) { | ||
4494 | 4478 | $f = ($t * 1000000) . 'us'; | ||
4495 | 4479 | } | ||
4496 | 4480 | elsif ($t >= 0.001000 && $t <= 0.999999) { | ||
4497 | 4481 | $f = sprintf("%.${p_ms}f", $t * 1000); | ||
4498 | 4482 | $f = ($f * 1) . 'ms'; # * 1 to remove insignificant zeros | ||
4499 | 4483 | } | ||
4500 | 4484 | elsif ($t >= 1) { | ||
4501 | 4485 | $f = sprintf("%.${p_s}f", $t); | ||
4502 | 4486 | $f = ($f * 1) . 's'; # * 1 to remove insignificant zeros | ||
4503 | 4487 | } | ||
4504 | 4488 | else { | ||
4505 | 4489 | $f = 0; # $t should = 0 at this point | ||
4506 | 4490 | } | ||
4507 | 4491 | |||
4508 | 4492 | return $f; | ||
4509 | 4493 | } | ||
4510 | 4494 | |||
4511 | 4495 | sub percentage_of { | ||
4512 | 4496 | my ( $is, $of, %args ) = @_; | ||
4513 | 4497 | my $p = $args{p} || 0; # float precision | ||
4514 | 4498 | my $fmt = $p ? "%.${p}f" : "%d"; | ||
4515 | 4499 | return sprintf $fmt, ($is * 100) / ($of ||= 1); | ||
4516 | 4500 | } | ||
4517 | 4501 | |||
4518 | 4502 | sub secs_to_time { | ||
4519 | 4503 | my ( $secs, $fmt ) = @_; | ||
4520 | 4504 | $secs ||= 0; | ||
4521 | 4505 | return '00:00' unless $secs; | ||
4522 | 4506 | |||
4523 | 4507 | $fmt ||= $secs >= 86_400 ? 'd' | ||
4524 | 4508 | : $secs >= 3_600 ? 'h' | ||
4525 | 4509 | : 'm'; | ||
4526 | 4510 | |||
4527 | 4511 | return | ||
4528 | 4512 | $fmt eq 'd' ? sprintf( | ||
4529 | 4513 | "%d+%02d:%02d:%02d", | ||
4530 | 4514 | int($secs / 86_400), | ||
4531 | 4515 | int(($secs % 86_400) / 3_600), | ||
4532 | 4516 | int(($secs % 3_600) / 60), | ||
4533 | 4517 | $secs % 60) | ||
4534 | 4518 | : $fmt eq 'h' ? sprintf( | ||
4535 | 4519 | "%02d:%02d:%02d", | ||
4536 | 4520 | int(($secs % 86_400) / 3_600), | ||
4537 | 4521 | int(($secs % 3_600) / 60), | ||
4538 | 4522 | $secs % 60) | ||
4539 | 4523 | : sprintf( | ||
4540 | 4524 | "%02d:%02d", | ||
4541 | 4525 | int(($secs % 3_600) / 60), | ||
4542 | 4526 | $secs % 60); | ||
4543 | 4527 | } | ||
4544 | 4528 | |||
4545 | 4529 | sub time_to_secs { | ||
4546 | 4530 | my ( $val, $default_suffix ) = @_; | ||
4547 | 4531 | die "I need a val argument" unless defined $val; | ||
4548 | 4532 | my $t = 0; | ||
4549 | 4533 | my ( $prefix, $num, $suffix ) = $val =~ m/([+-]?)(\d+)([a-z])?$/; | ||
4550 | 4534 | $suffix = $suffix || $default_suffix || 's'; | ||
4551 | 4535 | if ( $suffix =~ m/[smhd]/ ) { | ||
4552 | 4536 | $t = $suffix eq 's' ? $num * 1 # Seconds | ||
4553 | 4537 | : $suffix eq 'm' ? $num * 60 # Minutes | ||
4554 | 4538 | : $suffix eq 'h' ? $num * 3600 # Hours | ||
4555 | 4539 | : $num * 86400; # Days | ||
4556 | 4540 | |||
4557 | 4541 | $t *= -1 if $prefix && $prefix eq '-'; | ||
4558 | 4542 | } | ||
4559 | 4543 | else { | ||
4560 | 4544 | die "Invalid suffix for $val: $suffix"; | ||
4561 | 4545 | } | ||
4562 | 4546 | return $t; | ||
4563 | 4547 | } | ||
4564 | 4548 | |||
4565 | 4549 | sub shorten { | ||
4566 | 4550 | my ( $num, %args ) = @_; | ||
4567 | 4551 | my $p = defined $args{p} ? $args{p} : 2; # float precision | ||
4568 | 4552 | my $d = defined $args{d} ? $args{d} : 1_024; # divisor | ||
4569 | 4553 | my $n = 0; | ||
4570 | 4554 | my @units = ('', qw(k M G T P E Z Y)); | ||
4571 | 4555 | while ( $num >= $d && $n < @units - 1 ) { | ||
4572 | 4556 | $num /= $d; | ||
4573 | 4557 | ++$n; | ||
4574 | 4558 | } | ||
4575 | 4559 | return sprintf( | ||
4576 | 4560 | $num =~ m/\./ || $n | ||
4577 | 4561 | ? "%.${p}f%s" | ||
4578 | 4562 | : '%d', | ||
4579 | 4563 | $num, $units[$n]); | ||
4580 | 4564 | } | ||
4581 | 4565 | |||
4582 | 4566 | sub ts { | ||
4583 | 4567 | my ( $time, $gmt ) = @_; | ||
4584 | 4568 | my ( $sec, $min, $hour, $mday, $mon, $year ) | ||
4585 | 4569 | = $gmt ? gmtime($time) : localtime($time); | ||
4586 | 4570 | $mon += 1; | ||
4587 | 4571 | $year += 1900; | ||
4588 | 4572 | my $val = sprintf("%d-%02d-%02dT%02d:%02d:%02d", | ||
4589 | 4573 | $year, $mon, $mday, $hour, $min, $sec); | ||
4590 | 4574 | if ( my ($us) = $time =~ m/(\.\d+)$/ ) { | ||
4591 | 4575 | $us = sprintf("%.6f", $us); | ||
4592 | 4576 | $us =~ s/^0\././; | ||
4593 | 4577 | $val .= $us; | ||
4594 | 4578 | } | ||
4595 | 4579 | return $val; | ||
4596 | 4580 | } | ||
4597 | 4581 | |||
4598 | 4582 | sub parse_timestamp { | ||
4599 | 4583 | my ( $val ) = @_; | ||
4600 | 4584 | if ( my($y, $m, $d, $h, $i, $s, $f) | ||
4601 | 4585 | = $val =~ m/^$mysql_ts$/ ) | ||
4602 | 4586 | { | ||
4603 | 4587 | return sprintf "%d-%02d-%02d %02d:%02d:" | ||
4604 | 4588 | . (defined $f ? '%09.6f' : '%02d'), | ||
4605 | 4589 | $y + 2000, $m, $d, $h, $i, (defined $f ? $s + $f : $s); | ||
4606 | 4590 | } | ||
4607 | 4591 | elsif ( $val =~ m/^$proper_ts$/ ) { | ||
4608 | 4592 | return $val; | ||
4609 | 4593 | } | ||
4610 | 4594 | return $val; | ||
4611 | 4595 | } | ||
4612 | 4596 | |||
4613 | 4597 | sub unix_timestamp { | ||
4614 | 4598 | my ( $val, $gmt ) = @_; | ||
4615 | 4599 | if ( my($y, $m, $d, $h, $i, $s, $us) = $val =~ m/^$proper_ts$/ ) { | ||
4616 | 4600 | $val = $gmt | ||
4617 | 4601 | ? timegm($s, $i, $h, $d, $m - 1, $y) | ||
4618 | 4602 | : timelocal($s, $i, $h, $d, $m - 1, $y); | ||
4619 | 4603 | if ( defined $us ) { | ||
4620 | 4604 | $us = sprintf('%.6f', $us); | ||
4621 | 4605 | $us =~ s/^0\././; | ||
4622 | 4606 | $val .= $us; | ||
4623 | 4607 | } | ||
4624 | 4608 | } | ||
4625 | 4609 | return $val; | ||
4626 | 4610 | } | ||
4627 | 4611 | |||
4628 | 4612 | sub any_unix_timestamp { | ||
4629 | 4613 | my ( $val, $callback ) = @_; | ||
4630 | 4614 | |||
4631 | 4615 | if ( my ($n, $suffix) = $val =~ m/^$n_ts$/ ) { | ||
4632 | 4616 | $n = $suffix eq 's' ? $n # Seconds | ||
4633 | 4617 | : $suffix eq 'm' ? $n * 60 # Minutes | ||
4634 | 4618 | : $suffix eq 'h' ? $n * 3600 # Hours | ||
4635 | 4619 | : $suffix eq 'd' ? $n * 86400 # Days | ||
4636 | 4620 | : $n; # default: Seconds | ||
4637 | 4621 | PTDEBUG && _d('ts is now - N[shmd]:', $n); | ||
4638 | 4622 | return time - $n; | ||
4639 | 4623 | } | ||
4640 | 4624 | elsif ( $val =~ m/^\d{9,}/ ) { | ||
4641 | 4625 | PTDEBUG && _d('ts is already a unix timestamp'); | ||
4642 | 4626 | return $val; | ||
4643 | 4627 | } | ||
4644 | 4628 | elsif ( my ($ymd, $hms) = $val =~ m/^(\d{6})(?:\s+(\d+:\d+:\d+))?/ ) { | ||
4645 | 4629 | PTDEBUG && _d('ts is MySQL slow log timestamp'); | ||
4646 | 4630 | $val .= ' 00:00:00' unless $hms; | ||
4647 | 4631 | return unix_timestamp(parse_timestamp($val)); | ||
4648 | 4632 | } | ||
4649 | 4633 | elsif ( ($ymd, $hms) = $val =~ m/^(\d{4}-\d\d-\d\d)(?:[T ](\d+:\d+:\d+))?/) { | ||
4650 | 4634 | PTDEBUG && _d('ts is properly formatted timestamp'); | ||
4651 | 4635 | $val .= ' 00:00:00' unless $hms; | ||
4652 | 4636 | return unix_timestamp($val); | ||
4653 | 4637 | } | ||
4654 | 4638 | else { | ||
4655 | 4639 | PTDEBUG && _d('ts is MySQL expression'); | ||
4656 | 4640 | return $callback->($val) if $callback && ref $callback eq 'CODE'; | ||
4657 | 4641 | } | ||
4658 | 4642 | |||
4659 | 4643 | PTDEBUG && _d('Unknown ts type:', $val); | ||
4660 | 4644 | return; | ||
4661 | 4645 | } | ||
4662 | 4646 | |||
4663 | 4647 | sub make_checksum { | ||
4664 | 4648 | my ( $val ) = @_; | ||
4665 | 4649 | my $checksum = uc substr(md5_hex($val), -16); | ||
4666 | 4650 | PTDEBUG && _d($checksum, 'checksum for', $val); | ||
4667 | 4651 | return $checksum; | ||
4668 | 4652 | } | ||
4669 | 4653 | |||
4670 | 4654 | sub crc32 { | ||
4671 | 4655 | my ( $string ) = @_; | ||
4672 | 4656 | return unless $string; | ||
4673 | 4657 | my $poly = 0xEDB88320; | ||
4674 | 4658 | my $crc = 0xFFFFFFFF; | ||
4675 | 4659 | foreach my $char ( split(//, $string) ) { | ||
4676 | 4660 | my $comp = ($crc ^ ord($char)) & 0xFF; | ||
4677 | 4661 | for ( 1 .. 8 ) { | ||
4678 | 4662 | $comp = $comp & 1 ? $poly ^ ($comp >> 1) : $comp >> 1; | ||
4679 | 4663 | } | ||
4680 | 4664 | $crc = (($crc >> 8) & 0x00FFFFFF) ^ $comp; | ||
4681 | 4665 | } | ||
4682 | 4666 | return $crc ^ 0xFFFFFFFF; | ||
4683 | 4667 | } | ||
4684 | 4668 | |||
4685 | 4669 | my $got_json = eval { require JSON }; | ||
4686 | 4670 | sub encode_json { | ||
4687 | 4671 | return JSON::encode_json(@_) if $got_json; | ||
4688 | 4672 | my ( $data ) = @_; | ||
4689 | 4673 | return (object_to_json($data) || ''); | ||
4690 | 4674 | } | ||
4691 | 4675 | |||
4692 | 4676 | |||
4693 | 4677 | sub object_to_json { | ||
4694 | 4678 | my ($obj) = @_; | ||
4695 | 4679 | my $type = ref($obj); | ||
4696 | 4680 | |||
4697 | 4681 | if($type eq 'HASH'){ | ||
4698 | 4682 | return hash_to_json($obj); | ||
4699 | 4683 | } | ||
4700 | 4684 | elsif($type eq 'ARRAY'){ | ||
4701 | 4685 | return array_to_json($obj); | ||
4702 | 4686 | } | ||
4703 | 4687 | else { | ||
4704 | 4688 | return value_to_json($obj); | ||
4705 | 4689 | } | ||
4706 | 4690 | } | ||
4707 | 4691 | |||
4708 | 4692 | sub hash_to_json { | ||
4709 | 4693 | my ($obj) = @_; | ||
4710 | 4694 | my @res; | ||
4711 | 4695 | for my $k ( sort { $a cmp $b } keys %$obj ) { | ||
4712 | 4696 | push @res, string_to_json( $k ) | ||
4713 | 4697 | . ":" | ||
4714 | 4698 | . ( object_to_json( $obj->{$k} ) || value_to_json( $obj->{$k} ) ); | ||
4715 | 4699 | } | ||
4716 | 4700 | return '{' . ( @res ? join( ",", @res ) : '' ) . '}'; | ||
4717 | 4701 | } | ||
4718 | 4702 | |||
4719 | 4703 | sub array_to_json { | ||
4720 | 4704 | my ($obj) = @_; | ||
4721 | 4705 | my @res; | ||
4722 | 4706 | |||
4723 | 4707 | for my $v (@$obj) { | ||
4724 | 4708 | push @res, object_to_json($v) || value_to_json($v); | ||
4725 | 4709 | } | ||
4726 | 4710 | |||
4727 | 4711 | return '[' . ( @res ? join( ",", @res ) : '' ) . ']'; | ||
4728 | 4712 | } | ||
4729 | 4713 | |||
4730 | 4714 | sub value_to_json { | ||
4731 | 4715 | my ($value) = @_; | ||
4732 | 4716 | |||
4733 | 4717 | return 'null' if(!defined $value); | ||
4734 | 4718 | |||
4735 | 4719 | my $b_obj = B::svref_2object(\$value); # for round trip problem | ||
4736 | 4720 | my $flags = $b_obj->FLAGS; | ||
4737 | 4721 | return $value # as is | ||
4738 | 4722 | if $flags & ( B::SVp_IOK | B::SVp_NOK ) and !( $flags & B::SVp_POK ); # SvTYPE is IV or NV? | ||
4739 | 4723 | |||
4740 | 4724 | my $type = ref($value); | ||
4741 | 4725 | |||
4742 | 4726 | if( !$type ) { | ||
4743 | 4727 | return string_to_json($value); | ||
4744 | 4728 | } | ||
4745 | 4729 | else { | ||
4746 | 4730 | return 'null'; | ||
4747 | 4731 | } | ||
4748 | 4732 | |||
4749 | 4733 | } | ||
4750 | 4734 | |||
4751 | 4735 | my %esc = ( | ||
4752 | 4736 | "\n" => '\n', | ||
4753 | 4737 | "\r" => '\r', | ||
4754 | 4738 | "\t" => '\t', | ||
4755 | 4739 | "\f" => '\f', | ||
4756 | 4740 | "\b" => '\b', | ||
4757 | 4741 | "\"" => '\"', | ||
4758 | 4742 | "\\" => '\\\\', | ||
4759 | 4743 | "\'" => '\\\'', | ||
4760 | 4744 | ); | ||
4761 | 4745 | |||
4762 | 4746 | sub string_to_json { | ||
4763 | 4747 | my ($arg) = @_; | ||
4764 | 4748 | |||
4765 | 4749 | $arg =~ s/([\x22\x5c\n\r\t\f\b])/$esc{$1}/g; | ||
4766 | 4750 | $arg =~ s/\//\\\//g; | ||
4767 | 4751 | $arg =~ s/([\x00-\x08\x0b\x0e-\x1f])/'\\u00' . unpack('H2', $1)/eg; | ||
4768 | 4752 | |||
4769 | 4753 | utf8::upgrade($arg); | ||
4770 | 4754 | utf8::encode($arg); | ||
4771 | 4755 | |||
4772 | 4756 | return '"' . $arg . '"'; | ||
4773 | 4757 | } | ||
4774 | 4758 | |||
4775 | 4759 | sub _d { | ||
4776 | 4760 | my ($package, undef, $line) = caller 0; | ||
4777 | 4761 | @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } | ||
4778 | 4762 | map { defined $_ ? $_ : 'undef' } | ||
4779 | 4763 | @_; | ||
4780 | 4764 | print STDERR "# $package:$line $PID ", join(' ', @_), "\n"; | ||
4781 | 4765 | } | ||
4782 | 4766 | |||
4783 | 4767 | 1; | ||
4784 | 4768 | } | ||
4785 | 4769 | # ########################################################################### | ||
4786 | 4770 | # End Transformers package | ||
4787 | 4771 | # ########################################################################### | ||
4788 | 4772 | |||
4789 | 4773 | # ########################################################################### | ||
4790 | 4774 | # Safeguards package | ||
4791 | 4775 | # This package is a copy without comments from the original. The original | ||
4792 | 4776 | # with comments and its test file can be found in the Bazaar repository at, | ||
4793 | 4777 | # lib/Safeguards.pm | ||
4794 | 4778 | # t/lib/Safeguards.t | ||
4795 | 4779 | # See https://launchpad.net/percona-toolkit for more information. | ||
4796 | 4780 | # ########################################################################### | ||
4797 | 4781 | { | ||
4798 | 4782 | package Safeguards; | ||
4799 | 4783 | |||
4800 | 4784 | use strict; | ||
4801 | 4785 | use warnings FATAL => 'all'; | ||
4802 | 4786 | use English qw(-no_match_vars); | ||
4803 | 4787 | |||
4804 | 4788 | use constant PTDEBUG => $ENV{PTDEBUG} || 0; | ||
4805 | 4789 | |||
4806 | 4790 | sub new { | ||
4807 | 4791 | my ($class, %args) = @_; | ||
4808 | 4792 | my $self = { | ||
4809 | 4793 | disk_bytes_free => $args{disk_bytes_free} || 104857600, # 100 MiB | ||
4810 | 4794 | disk_pct_free => $args{disk_pct_free} || 5, | ||
4811 | 4795 | }; | ||
4812 | 4796 | return bless $self, $class; | ||
4813 | 4797 | } | ||
4814 | 4798 | |||
4815 | 4799 | sub get_disk_space { | ||
4816 | 4800 | my ($self, %args) = @_; | ||
4817 | 4801 | my $filesystem = $args{filesystem} || $ENV{PWD}; | ||
4818 | 4802 | |||
4819 | 4803 | my $disk_space = `df -P -k "$filesystem"`; | ||
4820 | 4804 | chop($disk_space) if $disk_space; | ||
4821 | 4805 | PTDEBUG && _d('Disk space on', $filesystem, $disk_space); | ||
4822 | 4806 | |||
4823 | 4807 | return $disk_space; | ||
4824 | 4808 | } | ||
4825 | 4809 | |||
4826 | 4810 | sub check_disk_space() { | ||
4827 | 4811 | my ($self, %args) = @_; | ||
4828 | 4812 | my $disk_space = $args{disk_space}; | ||
4829 | 4813 | PTDEBUG && _d("Checking disk space:\n", $disk_space); | ||
4830 | 4814 | |||
4831 | 4815 | my ($partition) = $disk_space =~ m/^\s*(\/.+)/m; | ||
4832 | 4816 | PTDEBUG && _d('Partition:', $partition); | ||
4833 | 4817 | die "Failed to parse partition from disk space:\n$disk_space" | ||
4834 | 4818 | unless $partition; | ||
4835 | 4819 | |||
4836 | 4820 | my (undef, undef, $bytes_used, $bytes_free, $pct_used, undef) | ||
4837 | 4821 | = $partition =~ m/(\S+)/g; | ||
4838 | 4822 | PTDEBUG && _d('Bytes used:', $bytes_used, 'free:', $bytes_free, | ||
4839 | 4823 | 'Percentage used:', $pct_used); | ||
4840 | 4824 | |||
4841 | 4825 | $bytes_used = ($bytes_used || 0) * 1024; | ||
4842 | 4826 | $bytes_free = ($bytes_free || 0) * 1024; | ||
4843 | 4827 | |||
4844 | 4828 | $pct_used =~ s/%//; | ||
4845 | 4829 | my $pct_free = 100 - ($pct_used || 0); | ||
4846 | 4830 | |||
4847 | 4831 | return $bytes_free >= $self->{disk_bytes_free} | ||
4848 | 4832 | && $pct_free >= $self->{disk_pct_free}; | ||
4849 | 4833 | } | ||
4850 | 4834 | |||
4851 | 4835 | sub _d { | ||
4852 | 4836 | my ($package, undef, $line) = caller 0; | ||
4853 | 4837 | @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } | ||
4854 | 4838 | map { defined $_ ? $_ : 'undef' } | ||
4855 | 4839 | @_; | ||
4856 | 4840 | print STDERR "# $package:$line $PID ", join(' ', @_), "\n"; | ||
4857 | 4841 | } | ||
4858 | 4842 | |||
4859 | 4843 | 1; | ||
4860 | 4844 | } | ||
4861 | 4845 | # ########################################################################### | ||
4862 | 4846 | # End Safeguards package | ||
4863 | 4847 | # ########################################################################### | ||
4864 | 4848 | |||
4865 | 4849 | # ########################################################################### | ||
4866 | 4850 | # Percona::Agent::Logger package | ||
4867 | 4851 | # This package is a copy without comments from the original. The original | ||
4868 | 4852 | # with comments and its test file can be found in the Bazaar repository at, | ||
4869 | 4853 | # lib/Percona/Agent/Logger.pm | ||
4870 | 4854 | # t/lib/Percona/Agent/Logger.t | ||
4871 | 4855 | # See https://launchpad.net/percona-toolkit for more information. | ||
4872 | 4856 | # ########################################################################### | ||
4873 | 4857 | { | ||
4874 | 4858 | package Percona::Agent::Logger; | ||
4875 | 4859 | |||
4876 | 4860 | use strict; | ||
4877 | 4861 | use warnings FATAL => 'all'; | ||
4878 | 4862 | use English qw(-no_match_vars); | ||
4879 | 4863 | |||
4880 | 4864 | use constant PTDEBUG => $ENV{PTDEBUG} || 0; | ||
4881 | 4865 | |||
4882 | 4866 | use POSIX qw(SIGALRM); | ||
4883 | 4867 | |||
4884 | 4868 | use Lmo; | ||
4885 | 4869 | use Transformers; | ||
4886 | 4870 | use Percona::WebAPI::Resource::LogEntry; | ||
4887 | 4871 | |||
4888 | 4872 | Transformers->import(qw(ts)); | ||
4889 | 4873 | |||
4890 | 4874 | has 'exit_status' => ( | ||
4891 | 4875 | is => 'rw', | ||
4892 | 4876 | isa => 'ScalarRef', | ||
4893 | 4877 | required => 1, | ||
4894 | 4878 | ); | ||
4895 | 4879 | |||
4896 | 4880 | has 'pid' => ( | ||
4897 | 4881 | is => 'ro', | ||
4898 | 4882 | isa => 'Int', | ||
4899 | 4883 | required => 1, | ||
4900 | 4884 | ); | ||
4901 | 4885 | |||
4902 | 4886 | has 'service' => ( | ||
4903 | 4887 | is => 'rw', | ||
4904 | 4888 | isa => 'Maybe[Str]', | ||
4905 | 4889 | required => 0, | ||
4906 | 4890 | default => sub { return; }, | ||
4907 | 4891 | ); | ||
4908 | 4892 | |||
4909 | 4893 | has 'data_ts' => ( | ||
4910 | 4894 | is => 'rw', | ||
4911 | 4895 | isa => 'Maybe[Int]', | ||
4912 | 4896 | required => 0, | ||
4913 | 4897 | default => sub { return; }, | ||
4914 | 4898 | ); | ||
4915 | 4899 | |||
4916 | 4900 | has 'online_logging' => ( | ||
4917 | 4901 | is => 'ro', | ||
4918 | 4902 | isa => 'Bool', | ||
4919 | 4903 | required => 0, | ||
4920 | 4904 | default => sub { return 1 }, | ||
4921 | 4905 | ); | ||
4922 | 4906 | |||
4923 | 4907 | has 'online_logging_enabled' => ( | ||
4924 | 4908 | is => 'rw', | ||
4925 | 4909 | isa => 'Bool', | ||
4926 | 4910 | required => 0, | ||
4927 | 4911 | default => sub { return 0 }, | ||
4928 | 4912 | ); | ||
4929 | 4913 | |||
4930 | 4914 | has 'quiet' => ( | ||
4931 | 4915 | is => 'rw', | ||
4932 | 4916 | isa => 'Int', | ||
4933 | 4917 | required => 0, | ||
4934 | 4918 | default => sub { return 0 }, | ||
4935 | 4919 | ); | ||
4936 | 4920 | |||
4937 | 4921 | has '_buffer' => ( | ||
4938 | 4922 | is => 'rw', | ||
4939 | 4923 | isa => 'ArrayRef', | ||
4940 | 4924 | required => 0, | ||
4941 | 4925 | default => sub { return []; }, | ||
4942 | 4926 | ); | ||
4943 | 4927 | |||
4944 | 4928 | has '_pipe_write' => ( | ||
4945 | 4929 | is => 'rw', | ||
4946 | 4930 | isa => 'Maybe[FileHandle]', | ||
4947 | 4931 | required => 0, | ||
4948 | 4932 | ); | ||
4949 | 4933 | |||
4950 | 4934 | sub read_stdin { | ||
4951 | 4935 | my ( $t ) = @_; | ||
4952 | 4936 | |||
4953 | 4937 | POSIX::sigaction( | ||
4954 | 4938 | SIGALRM, | ||
4955 | 4939 | POSIX::SigAction->new(sub { die 'read timeout'; }), | ||
4956 | 4940 | ) or die "Error setting SIGALRM handler: $OS_ERROR"; | ||
4957 | 4941 | |||
4958 | 4942 | my $timeout = 0; | ||
4959 | 4943 | my @lines; | ||
4960 | 4944 | eval { | ||
4961 | 4945 | alarm $t; | ||
4962 | 4946 | while(defined(my $line = <STDIN>)) { | ||
4963 | 4947 | push @lines, $line; | ||
4964 | 4948 | } | ||
4965 | 4949 | alarm 0; | ||
4966 | 4950 | }; | ||
4967 | 4951 | if ( $EVAL_ERROR ) { | ||
4968 | 4952 | PTDEBUG && _d('Read error:', $EVAL_ERROR); | ||
4969 | 4953 | die $EVAL_ERROR unless $EVAL_ERROR =~ m/read timeout/; | ||
4970 | 4954 | $timeout = 1; | ||
4971 | 4955 | } | ||
4972 | 4956 | return unless scalar @lines || $timeout; | ||
4973 | 4957 | return \@lines; | ||
4974 | 4958 | } | ||
4975 | 4959 | |||
4976 | 4960 | sub start_online_logging { | ||
4977 | 4961 | my ($self, %args) = @_; | ||
4978 | 4962 | my $client = $args{client}; | ||
4979 | 4963 | my $log_link = $args{log_link}; | ||
4980 | 4964 | my $read_timeout = $args{read_timeout} || 3; | ||
4981 | 4965 | |||
4982 | 4966 | return unless $self->online_logging; | ||
4983 | 4967 | |||
4984 | 4968 | my $pid = open(my $pipe_write, "|-"); | ||
4985 | 4969 | |||
4986 | 4970 | if ($pid) { | ||
4987 | 4971 | select $pipe_write; | ||
4988 | 4972 | $OUTPUT_AUTOFLUSH = 1; | ||
4989 | 4973 | $self->_pipe_write($pipe_write); | ||
4990 | 4974 | $self->online_logging_enabled(1); | ||
4991 | 4975 | } | ||
4992 | 4976 | else { | ||
4993 | 4977 | my @log_entries; | ||
4994 | 4978 | my $n_errors = 0; | ||
4995 | 4979 | my $oktorun = 1; | ||
4996 | 4980 | QUEUE: | ||
4997 | 4981 | while ($oktorun) { | ||
4998 | 4982 | my $lines = read_stdin($read_timeout); | ||
4999 | 4983 | last QUEUE unless $lines; | ||
5000 | 4984 | LINE: |
The diff has been truncated for viewing.