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