import rt 3.8.10
[freeside.git] / rt / sbin / rt-shredder
1 #!/usr/bin/perl
2 # BEGIN BPS TAGGED BLOCK {{{
3 #
4 # COPYRIGHT:
5 #
6 # This software is Copyright (c) 1996-2011 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 queiries 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 strict;
111 use warnings FATAL => 'all';
112
113 # fix lib paths, some may be relative
114 BEGIN {
115     require File::Spec;
116     my @libs = ("lib", "local/lib");
117     my $bin_path;
118
119     for my $lib (@libs) {
120         unless ( File::Spec->file_name_is_absolute($lib) ) {
121             unless ($bin_path) {
122                 if ( File::Spec->file_name_is_absolute(__FILE__) ) {
123                     $bin_path = ( File::Spec->splitpath(__FILE__) )[1];
124                 }
125                 else {
126                     require FindBin;
127                     no warnings "once";
128                     $bin_path = $FindBin::Bin;
129                 }
130             }
131             $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
132         }
133         unshift @INC, $lib;
134     }
135
136 }
137
138 use RT::Shredder ();
139 use Getopt::Long qw(GetOptions);
140 use File::Spec ();
141
142 use RT::Shredder::Plugin ();
143 # prefetch list of plugins
144 our %plugins = RT::Shredder::Plugin->List;
145
146 our %opt;
147 parse_args();
148
149 RT::Shredder::Init( %opt );
150 my $shredder = new RT::Shredder;
151
152 {
153     my $plugin = eval { $shredder->AddDumpPlugin( Arguments => {
154         file_name    => $opt{'sqldump'},
155         from_storage => 0,
156     } ) };
157         if( $@ ) {
158         print STDERR "ERROR: Couldn't open SQL dump file: $@\n";
159         exit 1 if $opt{'sqldump'};
160
161         print STDERR "WARNING: It's strongly recommended to use '--sqldump <filename>' option\n";
162         unless( $opt{'force'} ) {
163             exit 0 unless prompt_yN( "Do you want to proceed?" );
164         }
165         } else {
166         print "SQL dump file is '". $plugin->FileName ."'\n";
167     }
168 }
169
170 my @objs = process_plugins( $shredder );
171 prompt_delete_objs( \@objs ) unless $opt{'force'};
172
173 $shredder->PutObjects( Objects => $_ ) foreach @objs;
174 eval { $shredder->WipeoutAll };
175 if( $@ ) {
176     require RT::Shredder::Exceptions;
177     if( my $e = RT::Shredder::Exception::Info->caught ) {
178         print "\nERROR: $e\n\n";
179         exit 1;
180     }
181     die $@;
182 }
183
184 sub prompt_delete_objs
185 {
186         my( $objs ) = @_;
187         unless( @$objs ) {
188                 print "Objects list is empty, try refine search options\n";
189                 exit 0;
190         }
191         my $list = "Next ". scalar( @$objs ) ." objects would be deleted:\n";
192         foreach my $o( @$objs ) {
193                 $list .= "\t". $o->_AsString ." object\n";
194         }
195         print $list;
196         exit(0) unless prompt_yN( "Do you want to proceed?" );
197 }
198
199 sub prompt_yN
200 {
201         my $text = shift;
202         print "$text [y/N] ";
203         unless( <STDIN> =~ /^(?:y|yes)$/i ) {
204                 return 0;
205         }
206         return 1;
207 }
208
209 sub usage
210 {
211         require RT::Shredder::POD;
212         RT::Shredder::POD::shredder_cli( $0, \*STDOUT );
213         exit 1;
214 }
215
216 sub parse_args
217 {
218         my $tmp;
219         Getopt::Long::Configure( "pass_through" );
220         my @objs = ();
221         if( GetOptions( 'object=s' => \@objs ) && @objs ) {
222                 print STDERR "Option --object had been deprecated, use plugin 'Objects' instead\n";
223         exit(1);
224         }
225
226         my @plugins = ();
227         if( GetOptions( 'plugin=s' => \@plugins ) && @plugins ) {
228                 $opt{'plugin'} = \@plugins;
229                 foreach my $str( @plugins ) {
230                         if( $str =~ /^\s*list\s*$/ ) {
231                                 show_plugin_list();
232                         } elsif( $str =~ /^\s*help-(\w+)\s*$/ ) {
233                                 show_plugin_help( $1 );
234                         } elsif( $str =~ /^(\w+)(=.*)?$/ && !$plugins{$1} ) {
235                                 print "Couldn't find plugin '$1'\n";
236                                 show_plugin_list();
237                         }
238                 }
239         }
240
241         # other options make no sense without previouse
242         usage() unless keys %opt;
243
244         if( GetOptions( 'force' => \$tmp ) && $tmp ) {
245                 $opt{'force'}++;
246         }
247         $tmp = undef;
248         if( GetOptions( 'sqldump=s' => \$tmp ) && $tmp ) {
249                 $opt{'sqldump'} = $tmp;
250         }
251         return;
252 }
253
254 sub process_plugins
255 {
256         my $shredder = shift;
257
258         my @res;
259         foreach my $str( @{ $opt{'plugin'} } ) {
260                 my $plugin = new RT::Shredder::Plugin;
261                 my( $status, $msg ) = $plugin->LoadByString( $str );
262                 unless( $status ) {
263                         print STDERR "Couldn't load plugin\n";
264                         print STDERR "Error: $msg\n";
265                         exit(1);
266                 }
267         if ( lc $plugin->Type eq 'search' ) {
268             push @res, _process_search_plugin( $shredder, $plugin );
269         }
270         elsif ( lc $plugin->Type eq 'dump' ) {
271             _process_dump_plugin( $shredder, $plugin );
272         }
273         }
274         return RT::Shredder->CastObjectsToRecords( Objects => \@res );
275 }
276
277 sub _process_search_plugin {
278     my ($shredder, $plugin) = @_;
279     my ($status, @objs) = $plugin->Run;
280     unless( $status ) {
281         print STDERR "Couldn't run plugin\n";
282         print STDERR "Error: $objs[1]\n";
283         exit(1);
284     }
285
286     my $msg;
287     ($status, $msg) = $plugin->SetResolvers( Shredder => $shredder );
288     unless( $status ) {
289         print STDERR "Couldn't set conflicts resolver\n";
290         print STDERR "Error: $msg\n";
291         exit(1);
292     }
293     return @objs;
294 }
295
296 sub _process_dump_plugin {
297     my ($shredder, $plugin) = @_;
298     $shredder->AddDumpPlugin(
299         Object => $plugin,
300     );
301 }
302
303 sub show_plugin_list
304 {
305         print "Plugins list:\n";
306         print "\t$_\n" foreach( grep !/^Base$/, keys %plugins );
307         exit(1);
308 }
309
310 sub show_plugin_help
311 {
312         my( $name ) = @_;
313         require RT::Shredder::POD;
314         unless( $plugins{ $name } ) {
315                 print "Couldn't find plugin '$name'\n";
316                 show_plugin_list();
317         }
318         RT::Shredder::POD::plugin_cli( $plugins{'Base'}, \*STDOUT, 1 );
319         RT::Shredder::POD::plugin_cli( $plugins{ $name }, \*STDOUT );
320         exit(1);
321 }
322
323 exit(0);