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