Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / rt / sbin / rt-shredder.in
1 #!@PERL@
2 # BEGIN BPS TAGGED BLOCK {{{
3 #
4 # COPYRIGHT:
5 #
6 # This software is Copyright (c) 1996-2015 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
50 =head1 NAME
51
52 rt-shredder - Script which wipe out tickets from RT DB
53
54 =head1 SYNOPSIS
55
56   rt-shredder --plugin list
57   rt-shredder --plugin help-Tickets
58   rt-shredder --plugin 'Tickets=query,Queue="general" and Status="deleted"'
59
60   rt-shredder --sqldump unshred.sql --plugin ...
61   rt-shredder --force --plugin ...
62
63 =head1 DESCRIPTION
64
65 rt-shredder - is script that allow you to wipe out objects
66 from RT DB. This script uses API that L<RT::Shredder> module adds to RT.
67 Script can be used as example of usage of the shredder API.
68
69 =head1 USAGE
70
71 You can use several options to control which objects script
72 should wipeout.
73
74 =head1 OPTIONS
75
76 =head2 --sqldump <filename>
77
78 Outputs INSERT queries into file. This dump can be used to restore data
79 after wiping out.
80
81 By default creates files named F<< <ISO_date>-XXXX.sql >> in the current
82 directory.
83
84 =head2 --object (DEPRECATED)
85
86 Option has been deprecated, use plugin C<Objects> instead.
87
88 =head2 --plugin '<plugin name>[=<arg>,<val>[;<arg>,<val>]...]'
89
90 You can use plugins to select RT objects with various conditions.
91 See also --plugin list and --plugin help options.
92
93 =head2 --plugin list
94
95 Output list of the available plugins.
96
97 =head2 --plugin help-<plugin name>
98
99 Outputs help for specified plugin.
100
101 =head2 --force
102
103 Script doesn't ask any questions.
104
105 =head1 SEE ALSO
106
107 L<RT::Shredder>
108
109 =cut
110
111 use strict;
112 use warnings FATAL => 'all';
113
114 # fix lib paths, some may be relative
115 BEGIN { # BEGIN RT CMD BOILERPLATE
116     require File::Spec;
117     require Cwd;
118     my @libs = ("@RT_LIB_PATH@", "@LOCAL_LIB_PATH@");
119     my $bin_path;
120
121     for my $lib (@libs) {
122         unless ( File::Spec->file_name_is_absolute($lib) ) {
123             $bin_path ||= ( File::Spec->splitpath(Cwd::abs_path(__FILE__)) )[1];
124             $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
125         }
126         unshift @INC, $lib;
127     }
128
129 }
130
131 use RT -init;
132
133 require RT::Shredder;
134
135 use Getopt::Long qw(GetOptions);
136 use File::Spec ();
137
138 use RT::Shredder::Plugin ();
139 # prefetch list of plugins
140 our %plugins = RT::Shredder::Plugin->List;
141
142 our %opt;
143 parse_args();
144
145 my $shredder = RT::Shredder->new;
146
147 {
148     my $plugin = eval { $shredder->AddDumpPlugin( Arguments => {
149         file_name    => $opt{'sqldump'},
150         from_storage => 0,
151     } ) };
152         if( $@ ) {
153         print STDERR "ERROR: Couldn't open SQL dump file: $@\n";
154         exit 1 if $opt{'sqldump'};
155
156         print STDERR "WARNING: It's strongly recommended to use '--sqldump <filename>' option\n";
157         unless( $opt{'force'} ) {
158             exit 0 unless prompt_yN( "Do you want to proceed?" );
159         }
160         } else {
161         print "SQL dump file is '". $plugin->FileName ."'\n";
162     }
163 }
164
165 my @objs = process_plugins( $shredder );
166 prompt_delete_objs( \@objs ) unless $opt{'force'};
167
168 $shredder->PutObjects( Objects => $_ ) foreach @objs;
169 eval { $shredder->WipeoutAll };
170 if( $@ ) {
171     require RT::Shredder::Exceptions;
172     if( my $e = RT::Shredder::Exception::Info->caught ) {
173         print "\nERROR: $e\n\n";
174         exit 1;
175     }
176     die $@;
177 }
178
179 sub prompt_delete_objs
180 {
181     my( $objs ) = @_;
182     unless( @$objs ) {
183         print "Objects list is empty, try refine search options\n";
184         exit 0;
185     }
186     my $list = "Next ". scalar( @$objs ) ." objects would be deleted:\n";
187     foreach my $o( @$objs ) {
188         $list .= "\t". $o->UID ." object\n";
189     }
190     print $list;
191     exit(0) unless prompt_yN( "Do you want to proceed?" );
192 }
193
194 sub prompt_yN
195 {
196     my $text = shift;
197     print "$text [y/N] ";
198     unless( <STDIN> =~ /^(?:y|yes)$/i ) {
199         return 0;
200     }
201     return 1;
202 }
203
204 sub usage
205 {
206     require RT::Shredder::POD;
207     RT::Shredder::POD::shredder_cli( $0, \*STDOUT );
208     exit 1;
209 }
210
211 sub parse_args
212 {
213     my $tmp;
214     Getopt::Long::Configure( "pass_through" );
215     my @objs = ();
216     if( GetOptions( 'object=s' => \@objs ) && @objs ) {
217         print STDERR "Option --object had been deprecated, use plugin 'Objects' instead\n";
218         exit(1);
219     }
220
221     my @plugins = ();
222     if( GetOptions( 'plugin=s' => \@plugins ) && @plugins ) {
223         $opt{'plugin'} = \@plugins;
224         foreach my $str( @plugins ) {
225             if( $str =~ /^\s*list\s*$/ ) {
226                 show_plugin_list();
227             } elsif( $str =~ /^\s*help-(\w+)\s*$/ ) {
228                 show_plugin_help( $1 );
229             } elsif( $str =~ /^(\w+)(=.*)?$/ && !$plugins{$1} ) {
230                 print "Couldn't find plugin '$1'\n";
231                 show_plugin_list();
232             }
233         }
234     }
235
236     # other options make no sense without previouse
237     usage() unless keys %opt;
238
239     if( GetOptions( 'force' => \$tmp ) && $tmp ) {
240         $opt{'force'}++;
241     }
242     $tmp = undef;
243     if( GetOptions( 'sqldump=s' => \$tmp ) && $tmp ) {
244         $opt{'sqldump'} = $tmp;
245     }
246     return;
247 }
248
249 sub process_plugins
250 {
251     my $shredder = shift;
252
253     my @res;
254     foreach my $str( @{ $opt{'plugin'} } ) {
255         my $plugin = RT::Shredder::Plugin->new;
256         my( $status, $msg ) = $plugin->LoadByString( $str );
257         unless( $status ) {
258             print STDERR "Couldn't load plugin\n";
259             print STDERR "Error: $msg\n";
260             exit(1);
261         }
262         if ( lc $plugin->Type eq 'search' ) {
263             push @res, _process_search_plugin( $shredder, $plugin );
264         }
265         elsif ( lc $plugin->Type eq 'dump' ) {
266             _process_dump_plugin( $shredder, $plugin );
267         }
268     }
269     return RT::Shredder->CastObjectsToRecords( Objects => \@res );
270 }
271
272 sub _process_search_plugin {
273     my ($shredder, $plugin) = @_;
274     my ($status, @objs) = $plugin->Run;
275     unless( $status ) {
276         print STDERR "Couldn't run plugin\n";
277         print STDERR "Error: $objs[1]\n";
278         exit(1);
279     }
280
281     my $msg;
282     ($status, $msg) = $plugin->SetResolvers( Shredder => $shredder );
283     unless( $status ) {
284         print STDERR "Couldn't set conflicts resolver\n";
285         print STDERR "Error: $msg\n";
286         exit(1);
287     }
288     return @objs;
289 }
290
291 sub _process_dump_plugin {
292     my ($shredder, $plugin) = @_;
293     $shredder->AddDumpPlugin(
294         Object => $plugin,
295     );
296 }
297
298 sub show_plugin_list
299 {
300     print "Plugins list:\n";
301     print "\t$_\n" foreach( grep !/^Base$/, keys %plugins );
302     exit(1);
303 }
304
305 sub show_plugin_help
306 {
307     my( $name ) = @_;
308     require RT::Shredder::POD;
309     unless( $plugins{ $name } ) {
310         print "Couldn't find plugin '$name'\n";
311         show_plugin_list();
312     }
313     RT::Shredder::POD::plugin_cli( $plugins{'Base'}, \*STDOUT, 1 );
314     RT::Shredder::POD::plugin_cli( $plugins{ $name }, \*STDOUT );
315     exit(1);
316 }
317
318 exit(0);