]>
Commit | Line | Data |
---|---|---|
26e72bdc MKG |
1 | #!/usr/bin/perl |
2 | # BEGIN BPS TAGGED BLOCK {{{ | |
3 | # | |
4 | # COPYRIGHT: | |
5 | # | |
6 | # This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC | |
7 | # <sales@bestpractical.com> | |
8 | # | |
9 | # (Except where explicitly superseded by other copyright notices) | |
10 | # | |
11 | # | |
12 | # LICENSE: | |
13 | # | |
14 | # This work is made available to you under the terms of Version 2 of | |
15 | # the GNU General Public License. A copy of that license should have | |
16 | # been provided with this software, but in any event can be snarfed | |
17 | # from www.gnu.org. | |
18 | # | |
19 | # This work is distributed in the hope that it will be useful, but | |
20 | # WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
22 | # General Public License for more details. | |
23 | # | |
24 | # You should have received a copy of the GNU General Public License | |
25 | # along with this program; if not, write to the Free Software | |
26 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
27 | # 02110-1301 or visit their web page on the internet at | |
28 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html. | |
29 | # | |
30 | # | |
31 | # CONTRIBUTION SUBMISSION POLICY: | |
32 | # | |
33 | # (The following paragraph is not intended to limit the rights granted | |
34 | # to you to modify and distribute this software under the terms of | |
35 | # the GNU General Public License and is only of importance to you if | |
36 | # you choose to contribute your changes and enhancements to the | |
37 | # community by submitting them to Best Practical Solutions, LLC.) | |
38 | # | |
39 | # By intentionally submitting any modifications, corrections or | |
40 | # derivatives to this work, or any other work intended for use with | |
41 | # Request Tracker, to Best Practical Solutions, LLC, you confirm that | |
42 | # you are the copyright holder for those contributions and you grant | |
43 | # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable, | |
44 | # royalty-free, perpetual, license to use, copy, create derivative | |
45 | # works based on those contributions, and sublicense and distribute | |
46 | # those contributions and any derivatives thereof. | |
47 | # | |
48 | # END BPS TAGGED BLOCK }}} | |
49 | =head1 NAME | |
50 | ||
51 | rt-shredder - Script which wipe out tickets from RT DB | |
52 | ||
53 | =head1 SYNOPSIS | |
54 | ||
55 | rt-shredder --plugin list | |
56 | rt-shredder --plugin help-Tickets | |
57 | rt-shredder --plugin 'Tickets=query,Queue="general" and Status="deleted"' | |
58 | ||
59 | rt-shredder --sqldump unshred.sql --plugin ... | |
60 | rt-shredder --force --plugin ... | |
61 | ||
62 | =head1 DESCRIPTION | |
63 | ||
64 | rt-shredder - is script that allow you to wipe out objects | |
65 | from RT DB. This script uses API that L<RT::Shredder> module adds to RT. | |
66 | Script can be used as example of usage of the shredder API. | |
67 | ||
68 | =head1 USAGE | |
69 | ||
70 | You can use several options to control which objects script | |
71 | should wipeout. | |
72 | ||
73 | =head1 OPTIONS | |
74 | ||
75 | =head2 --sqldump <filename> | |
76 | ||
77 | Outputs INSERT queries into file. This dump can be used to restore data | |
78 | after wiping out. | |
79 | ||
80 | By default creates files | |
81 | F<< <RT_home>/var/data/RT-Shredder/<ISO_date>-XXXX.sql >> | |
82 | ||
83 | =head2 --object (DEPRECATED) | |
84 | ||
85 | Option has been deprecated, use plugin C<Objects> instead. | |
86 | ||
87 | =head2 --plugin '<plugin name>[=<arg>,<val>[;<arg>,<val>]...]' | |
88 | ||
89 | You can use plugins to select RT objects with various conditions. | |
90 | See also --plugin list and --plugin help options. | |
91 | ||
92 | =head2 --plugin list | |
93 | ||
94 | Output list of the available plugins. | |
95 | ||
96 | =head2 --plugin help-<plugin name> | |
97 | ||
98 | Outputs help for specified plugin. | |
99 | ||
100 | =head2 --force | |
101 | ||
102 | Script doesn't ask any questions. | |
103 | ||
104 | =head1 SEE ALSO | |
105 | ||
106 | L<RT::Shredder> | |
107 | ||
108 | =cut | |
109 | ||
110 | use lib '/www/data/rt/rt-perl/current-perl10/share/perl5'; | |
111 | use lib '/www/data/rt/rt-perl/current-perl10/lib/perl5'; | |
112 | use lib '/www/data/rt/rt-perl/current-perl10/lib64/perl5'; | |
113 | ||
114 | use strict; | |
115 | use warnings FATAL => 'all'; | |
116 | ||
117 | # fix lib paths, some may be relative | |
118 | BEGIN { | |
119 | require File::Spec; | |
120 | my @libs = ("lib", "local/lib"); | |
121 | my $bin_path; | |
122 | ||
123 | for my $lib (@libs) { | |
124 | unless ( File::Spec->file_name_is_absolute($lib) ) { | |
125 | unless ($bin_path) { | |
126 | if ( File::Spec->file_name_is_absolute(__FILE__) ) { | |
127 | $bin_path = ( File::Spec->splitpath(__FILE__) )[1]; | |
128 | } | |
129 | else { | |
130 | require FindBin; | |
131 | no warnings "once"; | |
132 | $bin_path = $FindBin::Bin; | |
133 | } | |
134 | } | |
135 | $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib ); | |
136 | } | |
137 | unshift @INC, $lib; | |
138 | } | |
139 | ||
140 | } | |
141 | ||
142 | use RT::Shredder (); | |
143 | use Getopt::Long qw(GetOptions); | |
144 | use File::Spec (); | |
145 | ||
146 | use RT::Shredder::Plugin (); | |
147 | # prefetch list of plugins | |
148 | our %plugins = RT::Shredder::Plugin->List; | |
149 | ||
150 | our %opt; | |
151 | parse_args(); | |
152 | ||
153 | RT::Shredder::Init( %opt ); | |
154 | my $shredder = RT::Shredder->new; | |
155 | ||
156 | { | |
157 | my $plugin = eval { $shredder->AddDumpPlugin( Arguments => { | |
158 | file_name => $opt{'sqldump'}, | |
159 | from_storage => 0, | |
160 | } ) }; | |
161 | if( $@ ) { | |
162 | print STDERR "ERROR: Couldn't open SQL dump file: $@\n"; | |
163 | exit 1 if $opt{'sqldump'}; | |
164 | ||
165 | print STDERR "WARNING: It's strongly recommended to use '--sqldump <filename>' option\n"; | |
166 | unless( $opt{'force'} ) { | |
167 | exit 0 unless prompt_yN( "Do you want to proceed?" ); | |
168 | } | |
169 | } else { | |
170 | print "SQL dump file is '". $plugin->FileName ."'\n"; | |
171 | } | |
172 | } | |
173 | ||
174 | my @objs = process_plugins( $shredder ); | |
175 | prompt_delete_objs( \@objs ) unless $opt{'force'}; | |
176 | ||
177 | $shredder->PutObjects( Objects => $_ ) foreach @objs; | |
178 | eval { $shredder->WipeoutAll }; | |
179 | if( $@ ) { | |
180 | require RT::Shredder::Exceptions; | |
181 | if( my $e = RT::Shredder::Exception::Info->caught ) { | |
182 | print "\nERROR: $e\n\n"; | |
183 | exit 1; | |
184 | } | |
185 | die $@; | |
186 | } | |
187 | ||
188 | sub prompt_delete_objs | |
189 | { | |
190 | my( $objs ) = @_; | |
191 | unless( @$objs ) { | |
192 | print "Objects list is empty, try refine search options\n"; | |
193 | exit 0; | |
194 | } | |
195 | my $list = "Next ". scalar( @$objs ) ." objects would be deleted:\n"; | |
196 | foreach my $o( @$objs ) { | |
197 | $list .= "\t". $o->_AsString ." object\n"; | |
198 | } | |
199 | print $list; | |
200 | exit(0) unless prompt_yN( "Do you want to proceed?" ); | |
201 | } | |
202 | ||
203 | sub prompt_yN | |
204 | { | |
205 | my $text = shift; | |
206 | print "$text [y/N] "; | |
207 | unless( <STDIN> =~ /^(?:y|yes)$/i ) { | |
208 | return 0; | |
209 | } | |
210 | return 1; | |
211 | } | |
212 | ||
213 | sub usage | |
214 | { | |
215 | require RT::Shredder::POD; | |
216 | RT::Shredder::POD::shredder_cli( $0, \*STDOUT ); | |
217 | exit 1; | |
218 | } | |
219 | ||
220 | sub parse_args | |
221 | { | |
222 | my $tmp; | |
223 | Getopt::Long::Configure( "pass_through" ); | |
224 | my @objs = (); | |
225 | if( GetOptions( 'object=s' => \@objs ) && @objs ) { | |
226 | print STDERR "Option --object had been deprecated, use plugin 'Objects' instead\n"; | |
227 | exit(1); | |
228 | } | |
229 | ||
230 | my @plugins = (); | |
231 | if( GetOptions( 'plugin=s' => \@plugins ) && @plugins ) { | |
232 | $opt{'plugin'} = \@plugins; | |
233 | foreach my $str( @plugins ) { | |
234 | if( $str =~ /^\s*list\s*$/ ) { | |
235 | show_plugin_list(); | |
236 | } elsif( $str =~ /^\s*help-(\w+)\s*$/ ) { | |
237 | show_plugin_help( $1 ); | |
238 | } elsif( $str =~ /^(\w+)(=.*)?$/ && !$plugins{$1} ) { | |
239 | print "Couldn't find plugin '$1'\n"; | |
240 | show_plugin_list(); | |
241 | } | |
242 | } | |
243 | } | |
244 | ||
245 | # other options make no sense without previouse | |
246 | usage() unless keys %opt; | |
247 | ||
248 | if( GetOptions( 'force' => \$tmp ) && $tmp ) { | |
249 | $opt{'force'}++; | |
250 | } | |
251 | $tmp = undef; | |
252 | if( GetOptions( 'sqldump=s' => \$tmp ) && $tmp ) { | |
253 | $opt{'sqldump'} = $tmp; | |
254 | } | |
255 | return; | |
256 | } | |
257 | ||
258 | sub process_plugins | |
259 | { | |
260 | my $shredder = shift; | |
261 | ||
262 | my @res; | |
263 | foreach my $str( @{ $opt{'plugin'} } ) { | |
264 | my $plugin = RT::Shredder::Plugin->new; | |
265 | my( $status, $msg ) = $plugin->LoadByString( $str ); | |
266 | unless( $status ) { | |
267 | print STDERR "Couldn't load plugin\n"; | |
268 | print STDERR "Error: $msg\n"; | |
269 | exit(1); | |
270 | } | |
271 | if ( lc $plugin->Type eq 'search' ) { | |
272 | push @res, _process_search_plugin( $shredder, $plugin ); | |
273 | } | |
274 | elsif ( lc $plugin->Type eq 'dump' ) { | |
275 | _process_dump_plugin( $shredder, $plugin ); | |
276 | } | |
277 | } | |
278 | return RT::Shredder->CastObjectsToRecords( Objects => \@res ); | |
279 | } | |
280 | ||
281 | sub _process_search_plugin { | |
282 | my ($shredder, $plugin) = @_; | |
283 | my ($status, @objs) = $plugin->Run; | |
284 | unless( $status ) { | |
285 | print STDERR "Couldn't run plugin\n"; | |
286 | print STDERR "Error: $objs[1]\n"; | |
287 | exit(1); | |
288 | } | |
289 | ||
290 | my $msg; | |
291 | ($status, $msg) = $plugin->SetResolvers( Shredder => $shredder ); | |
292 | unless( $status ) { | |
293 | print STDERR "Couldn't set conflicts resolver\n"; | |
294 | print STDERR "Error: $msg\n"; | |
295 | exit(1); | |
296 | } | |
297 | return @objs; | |
298 | } | |
299 | ||
300 | sub _process_dump_plugin { | |
301 | my ($shredder, $plugin) = @_; | |
302 | $shredder->AddDumpPlugin( | |
303 | Object => $plugin, | |
304 | ); | |
305 | } | |
306 | ||
307 | sub show_plugin_list | |
308 | { | |
309 | print "Plugins list:\n"; | |
310 | print "\t$_\n" foreach( grep !/^Base$/, keys %plugins ); | |
311 | exit(1); | |
312 | } | |
313 | ||
314 | sub show_plugin_help | |
315 | { | |
316 | my( $name ) = @_; | |
317 | require RT::Shredder::POD; | |
318 | unless( $plugins{ $name } ) { | |
319 | print "Couldn't find plugin '$name'\n"; | |
320 | show_plugin_list(); | |
321 | } | |
322 | RT::Shredder::POD::plugin_cli( $plugins{'Base'}, \*STDOUT, 1 ); | |
323 | RT::Shredder::POD::plugin_cli( $plugins{ $name }, \*STDOUT ); | |
324 | exit(1); | |
325 | } | |
326 | ||
327 | exit(0); |