X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;ds=sidebyside;f=rt%2Fbin%2Frt-crontool.in;h=a8fc30d83d674ee7b731bd8e368ee70673907dcc;hb=9aee669886202be7035e6c6049fc71bc99dd3013;hp=8401acab3895dba83fe6739d85a912588dff1790;hpb=b4b0c7e72d7eaee2fbfc7022022c9698323203dd;p=freeside.git diff --git a/rt/bin/rt-crontool.in b/rt/bin/rt-crontool.in index 8401acab3..a8fc30d83 100644 --- a/rt/bin/rt-crontool.in +++ b/rt/bin/rt-crontool.in @@ -1,41 +1,41 @@ #!@PERL@ # BEGIN BPS TAGGED BLOCK {{{ -# +# # COPYRIGHT: -# -# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC -# -# +# +# This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC +# +# # (Except where explicitly superseded by other copyright notices) -# -# +# +# # LICENSE: -# +# # This work is made available to you under the terms of Version 2 of # the GNU General Public License. A copy of that license should have # been provided with this software, but in any event can be snarfed # from www.gnu.org. -# +# # This work is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. -# +# # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # 02110-1301 or visit their web page on the internet at # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html. -# -# +# +# # CONTRIBUTION SUBMISSION POLICY: -# +# # (The following paragraph is not intended to limit the rights granted # to you to modify and distribute this software under the terms of # the GNU General Public License and is only of importance to you if # you choose to contribute your changes and enhancements to the # community by submitting them to Best Practical Solutions, LLC.) -# +# # By intentionally submitting any modifications, corrections or # derivatives to this work, or any other work intended for use with # Request Tracker, to Best Practical Solutions, LLC, you confirm that @@ -44,29 +44,22 @@ # royalty-free, perpetual, license to use, copy, create derivative # works based on those contributions, and sublicense and distribute # those contributions and any derivatives thereof. -# +# # END BPS TAGGED BLOCK }}} use strict; +use warnings; use Carp; # fix lib paths, some may be relative -BEGIN { +BEGIN { # BEGIN RT CMD BOILERPLATE require File::Spec; + require Cwd; my @libs = ("@RT_LIB_PATH@", "@LOCAL_LIB_PATH@"); my $bin_path; for my $lib (@libs) { unless ( File::Spec->file_name_is_absolute($lib) ) { - unless ($bin_path) { - if ( File::Spec->file_name_is_absolute(__FILE__) ) { - $bin_path = ( File::Spec->splitpath(__FILE__) )[1]; - } - else { - require FindBin; - no warnings "once"; - $bin_path = $FindBin::Bin; - } - } + $bin_path ||= ( File::Spec->splitpath(Cwd::abs_path(__FILE__)) )[1]; $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib ); } unshift @INC, $lib; @@ -78,12 +71,7 @@ use RT; use Getopt::Long; -use RT::Interface::CLI qw(CleanEnv GetCurrentUser GetMessageContent loc); -use RT::Tickets; -use RT::Template; - -#Clean out all the nasties from the environment -CleanEnv(); +use RT::Interface::CLI qw(GetCurrentUser loc); my ( $search, $condition, $action, $search_arg, $condition_arg, $action_arg, $template, $template_id, $transaction, $transaction_type, $help, $log, $verbose ); @@ -107,11 +95,14 @@ GetOptions( RT::LoadConfig(); # adjust logging to the screen according to options -RT->Config->Set( LogToScreen => $log ) if $log; +RT->Config->Set( LogToSTDERR => $log ) if $log; #Connect to the database and get RT::SystemUser and RT::Nobody loaded RT::Init(); +require RT::Tickets; +require RT::Template; + #Get the current user all loaded my $CurrentUser = GetCurrentUser(); @@ -119,7 +110,7 @@ my $CurrentUser = GetCurrentUser(); help() if $help; unless ( $CurrentUser->Id ) { - print loc("No RT user found. Please consult your RT administrator.\n"); + print loc("No RT user found. Please consult your RT administrator.") . "\n"; exit(1); } @@ -152,20 +143,16 @@ my $void_scrip_action = RT::ScripAction->new( $CurrentUser ); #find a bunch of tickets my $tickets = RT::Tickets->new($CurrentUser); -my $search = $search->new( +$search = $search->new( TicketsObj => $tickets, Argument => $search_arg, CurrentUser => $CurrentUser ); - $search->Prepare(); -# TicketsFound is an RT::Tickets object -my $tickets = $search->TicketsObj; - #for each ticket we've found while ( my $ticket = $tickets->Next() ) { - print $ticket->Id() . ": " if ($verbose); + print $ticket->Id() . ":\n" if ($verbose); my $template_obj = get_template( $ticket ); @@ -173,15 +160,15 @@ while ( my $ticket = $tickets->Next() ) { my $txns = get_transactions($ticket); my $found = 0; while ( my $txn = $txns->Next ) { - print loc("Using transaction #[_1]...", $txn->id) + print "\t".loc("Using transaction #[_1]...", $txn->id)."\n" if $verbose; process($ticket, $txn, $template_obj); $found = 1; } - print loc("Couldn't find suitable transaction, skipping") + print "\t".loc("Couldn't find suitable transaction, skipping")."\n" if $verbose && !$found; } else { - print loc("Processing without transaction, some conditions and actions may fail. Consider using --transaction argument") + print "\t".loc("Processing without transaction, some conditions and actions may fail. Consider using --transaction argument")."\n" if $verbose; process($ticket, undef, $template_obj); @@ -207,7 +194,7 @@ sub process { # if the condition doesn't apply, get out of here return unless $condition_obj->IsApplicable; - print loc("Condition matches...") if $verbose; + print "\t".loc("Condition matches...")."\n" if $verbose; } #prepare our action @@ -223,20 +210,20 @@ sub process { #if our preparation, move onto the next ticket return unless $action_obj->Prepare; - print loc("Action prepared...") if $verbose; + print "\t".loc("Action prepared...")."\n" if $verbose; #commit our action. return unless $action_obj->Commit; - print loc("Action committed.\n") if $verbose; + print "\t".loc("Action committed.")."\n" if $verbose; } -=head2 get_transactions - -Takes ticket and returns L object with transactions -of the ticket according to command line arguments C<--transaction> -and <--transaction-type>. - -=cut +# =head2 get_transactions +# +# Takes ticket and returns L object with transactions +# of the ticket according to command line arguments C<--transaction> +# and <--transaction-type>. +# +# =cut sub get_transactions { my $ticket = shift; @@ -257,73 +244,53 @@ sub get_transactions { return $txns; } -=head2 get_template - -Takes a ticket and returns a template according to command line options. - -=cut +# =head2 get_template +# +# Takes a ticket and returns a template according to command line options. +# +# =cut -{ my $cache = undef; sub get_template { my $ticket = shift; return undef unless $template; unless ( $template =~ /\D/ ) { # by id - return $cache if $cache; - - my $cache = RT::Template->new( $RT::SystemUser ); - $cache->Load( $template ); + my $template_obj = RT::Template->new( RT->SystemUser ); + $template_obj->Load( $template ); die "Failed to load template '$template'" - unless $cache->id; - return $cache; + unless $template_obj->id; + return $template_obj; } my $queue = $ticket->Queue; - return $cache->{ $queue } if $cache->{ $queue }; - my $res = RT::Template->new( $RT::SystemUser ); + my $res = RT::Template->new( RT->SystemUser ); $res->LoadQueueTemplate( Queue => $queue, Name => $template ); unless ( $res->id ) { $res->LoadGlobalTemplate( $template ); die "Failed to load template '$template', either for queue #$queue or global" unless $res->id; } - return $cache->{ $queue } = $res; -} } - -# {{{ load_module - -=head2 load_module + return $res; +} -Loads a perl module, dying nicely if it can't find it. -=cut +# =head2 load_module +# +# Loads a perl module, dying nicely if it can't find it. +# +# =cut sub load_module { my $modname = shift; - eval "require $modname"; - if ($@) { - die loc( "Failed to load module [_1]. ([_2])", $modname, $@ ); + unless ($modname->require) { + my $error = $@; + die loc( "Failed to load module [_1]. ([_2])", $modname, $error ); } } -# }}} - -# {{{ loc - -=head2 loc LIST - -Localize this string, with the current user's currentuser object - -=cut - -sub loc { - $CurrentUser->loc(@_); -} - -# }}} sub help { @@ -331,37 +298,37 @@ sub help { . "\n"; print loc("It takes several arguments:") . "\n\n"; - print " " + print " " . loc( "[_1] - Specify the search module you want to use", "--search" ) . "\n"; - print " " + print " " . loc( "[_1] - An argument to pass to [_2]", "--search-arg", "--search" ) . "\n"; - print " " + print " " . loc( "[_1] - Specify the condition module you want to use", "--condition" ) . "\n"; - print " " + print " " . loc( "[_1] - An argument to pass to [_2]", "--condition-arg", "--condition" ) . "\n"; - print " " + print " " . loc( "[_1] - Specify the action module you want to use", "--action" ) . "\n"; - print " " + print " " . loc( "[_1] - An argument to pass to [_2]", "--action-arg", "--action" ) . "\n"; - print " " + print " " . loc( "[_1] - Specify name or id of template(s) you want to use", "--template" ) . "\n"; - print " " + print " " . loc( "[_1] - Specify if you want to use either 'first', 'last' or 'all' transactions", "--transaction" ) . "\n"; - print " " + print " " . loc( "[_1] - Specify the comma separated list of transactions' types you want to use", "--transaction-type" ) . "\n"; - print " " - . loc( "[_1] - Adjust LogToScreen config option", "--log" ) . "\n"; - print " " + print " " + . loc( "[_1] - Adjust LogToSTDERR config option", "--log" ) . "\n"; + print " " . loc( "[_1] - Output status updates to STDOUT", "--verbose" ) . "\n"; print "\n"; print "\n"; @@ -374,13 +341,13 @@ sub help { print loc("Example:"); print "\n"; print " " - . loc( "The following command will find all active tickets in the queue 'general' and set their priority to 99 if they haven't been touched in 4 hours:" + . loc( "The following command will find all active tickets in the queue 'general' and set their priority to 99 if they are overdue:" ) . "\n\n"; print " bin/rt-crontool \\\n"; print " --search RT::Search::ActiveTicketsInQueue --search-arg general \\\n"; - print " --condition RT::Condition::UntouchedInHours --condition-arg 4 \\\n"; + print " --condition RT::Condition::Overdue \\\n"; print " --action RT::Action::SetPriority --action-arg 99 \\\n"; print " --verbose\n"; @@ -388,7 +355,7 @@ sub help { print loc("Escalate tickets"). "\n"; print " bin/rt-crontool \\\n"; print " --search RT::Search::ActiveTicketsInQueue --search-arg general \\\n"; - print " --action RT::Action::EscalatePriority\n"; + print" --action RT::Action::EscalatePriority\n"; @@ -397,3 +364,90 @@ sub help { exit(0); } + +__END__ + +=head1 NAME + +rt-crontool - a tool to act on tickets from an external scheduling tool + +=head1 SYNOPSIS + + # find all active tickets in the queue 'general' and set their priority to 99 if they are overdue: + rt-crontool \ + --search RT::Search::ActiveTicketsInQueue --search-arg general \ + --condition RT::Condition::Overdue \ + --action RT::Action::SetPriority --action-arg 99 \ + --verbose + + # Escalate tickets + rt-crontool \ + --search RT::Search::ActiveTicketsInQueue --search-arg general \ + --action RT::Action::EscalatePriority + +=head1 DESCRIPTION + +This script is a tool to act on tickets from an external scheduling tool, such +as cron. + +Security: + +This tool allows the user to run arbitrary perl modules from within RT. If +this tool were setgid, a hostile local user could use this tool to gain +administrative access to RT. It is incredibly important that nonprivileged +users not be allowed to run this tool. It is suggested that you create a +non-privileged unix user with the correct group membership and RT access to +run this tool. + + +=head1 OPTIONS + +=over + +=item search + +Specify the search module you want to use + +=item search-arg + +An argument to pass to --search + +=item condition + +Specify the condition module you want to use + +=item condition-arg + +An argument to pass to --condition + +=item action + +Specify the action module you want to use + +=item action-arg + +An argument to pass to --action + +=item template + +Specify name or id of template(s) you want to use + +=item transaction + +Specify if you want to use either 'first', 'last' or 'all' transactions + + +=item transaction-type + +Specify the comma separated list of transactions' types you want to use + +=item log + +Adjust LogToSTDERR config option + +=item verbose + +Output status updates to STDOUT + +=back +