### ### Copyright 2000-2007 University of Illinois Board of Trustees ### All rights reserved. ### ### cron.pm - cron module for psgconf ### ### Campus Information Technologies and Educational Services ### University of Illinois at Urbana-Champaign ### package PSGConf::Control::cron; use strict; use PSGConf::Action::Crontab; use PSGConf::Action::GenerateFile::Literal; use PSGConf::Action::Remove; use PSGConf::Action::RemoveCrontab; use PSGConf::Data::Boolean; use PSGConf::Data::List; use PSGConf::Data::String; use PSGConf::Data::Hash; use PSGConf::Control::Packages qw(_add_pkgs); use File::Basename; ############################################################################### ### policies ############################################################################### sub _enable_rc_scripts { my ($self, $psgconf) = @_; $psgconf->data_obj('rc_scripts')->insert( { 'cron' => { 'state' => 'enable' }} ) if ( $psgconf->data_obj('cron_enable')->equals('true') ); } ############################################################################### ### decide() method ############################################################################### sub decide { my ($self, $psgconf) = @_; my ($crontabs, $user, $entry, $tab); $crontabs = $psgconf->data_obj('crontabs')->get(); ### ### Remove all crontabs for users that do not exist. ### Use the crontab -r routine to make sure cron is ### aware that we removed the file, and do not do a ### backup because the cron program will still pick up ### the file if it is backed up. Change this when we ### do backups to a seperate subtree. ### map { $tab = basename $_; $psgconf->register_actions( PSGConf::Action::RemoveCrontab->new( name => $tab, fullpath => $_ ) ) if ( ! defined $psgconf->data_obj('user_info')->find($tab) ); } glob ($psgconf->data_obj('crontab_dir')->get() . "/*"); $psgconf->register_actions( (map { PSGConf::Action::Crontab->new( user => $_, uid => $psgconf->data_obj('user_info')->find($_)->{uid}, entries => $crontabs->{$_} ) } keys %$crontabs) ); if ( ! $psgconf->data_obj('cron_use_vixie')->get() ) { $psgconf->register_actions( PSGConf::Action::Remove->new( name => '/etc/crontab' ) ); } else { $tab = $psgconf->data_obj('cron_vixie_header')->get(); foreach $user ( keys %$crontabs ) { foreach $entry ( @{$crontabs->{$user}} ) { if ( defined $entry->{type} && $entry->{type} eq 'vixie' ) { $tab .= join(' ', (map { (defined($entry->{$_})? $entry->{$_}: '*'); } qw(minute hour dom month dow))); $tab .= "\t" . $user . "\t" . $entry->{command} . "\n"; } } } $psgconf->register_actions( PSGConf::Action::GenerateFile::Literal->new( name => '/etc/crontab', content => $tab ) ); } } ############################################################################### ### Constructor ############################################################################### sub new { my ($class, $psgconf) = @_; my ($self); $self = {}; bless($self, $class); ### So that _add_pkgs knows which directives to look at $self->{name} = 'cron'; $self->{enable} = $self->{name} . '_enable'; $self->{packages} = $self->{name} . '_packages'; $psgconf->register_data( cron_enable => PSGConf::Data::Boolean->new( value => 'true' ), cron_use_vixie => PSGConf::Data::Boolean->new( value => 'false' ), cron_vixie_header => PSGConf::Data::String->new(), crontab_dir => PSGConf::Data::String->new( 'value_abspath' => 1, value => "/var/spool/cron/crontabs" ), cron_packages => PSGConf::Data::List->new(), crontabs => PSGConf::Data::Hash->new( value_type => 'ARRAY' ) ); $psgconf->register_policy($self, cron_add_packages => '_add_pkgs', cron_enable_rc_scripts => '_enable_rc_scripts' ); return $self; } ############################################################################### ### documentation ############################################################################### 1; __END__ =head1 NAME PSGConf::Control::cron - psgconf control class for cron configuration =head1 SYNOPSIS In F: Control PSGConf::Control::cron =head1 DESCRIPTION The B module provides a B control object for configuring C files. It supports the following methods: =over 4 =item new() The constructor. Its parameter is a reference to the B object. It registers the following Data objects: =over 4 =item I A B object that indicates whether C should be configured. =item I A B object that indicates whether to support Vixie Cron, including F file. =item I A B object that defines the header of the F file. Treated as a literal. =item I A B object that indicates where the crontab files are stored. Defaults to F. =item I A B object that lists all packages to install. =item I A B object that contains the entries for each user's crontab. The hash key is the name of the user. The hash value is an anonymous array containing an anonymous hash for each entry in the user's crontab. The anonymous hash supports the following keys: =over 4 =item I If this is a vixie crontab entry (ie needs to be put into F, then set type to C. =item I The minute of the entry (range 0-59). If not set, defaults to C<*>. =item I The hour of the entry (range 0-23). If not set, defaults to C<*>. =item I The day of month of the entry (range 1-31). If not set, defaults to C<*>. =item I The month of the entry (range 1-12). If not set, defaults to C<*>. =item I The day of week of the entry (range 0-6, with 0 being Sunday). If not set, defaults to C<*>. =item I The command to execute. This field is required. =back The constructor also registers the following policy methods: =over 4 =item I Requests that the C package be installed. This is done using the I object, which is provided by the B module. =back =item decide() Registers a B Action object to generate the crontab for each user in the I Data object. Also registers a B Action object to remove the F file as well as B object for each and every crontab file that no longer has a login on the system. =back =back =head1 BUGS This module should provide a mechanism for generating the F and F files. =head1 SEE ALSO L crontab(5) L L L L L L L L L L L L =cut