###
###  Copyright 2000-2007 University of Illinois Board of Trustees
###  All rights reserved. 
###
###  RedHat.pm - RedHat module for psgconf
###
###  Campus Information Technologies and Educational Services
###  University of Illinois at Urbana-Champaign
###


package PSGConf::Control::RedHat;

use strict;

use PSGConf::Action::GenerateFile::AppConfig;
use PSGConf::Action::GenerateFile::EnvFile;
use PSGConf::Action::RPMImportGPGKey;
use PSGConf::Action::ModifyFile;
use PSGConf::Data::Boolean;
use PSGConf::Data::Hash;
use PSGConf::Data::List;
use PSGConf::Data::String;

use PSGConf::Control::Packages qw(_add_pkgs);


###############################################################################
###  decide() method
###############################################################################

sub decide
{
	my ($self, $psgconf) = @_;
	my ($key);

	return
		if (!$psgconf->data_obj('platform')->match('(-rhel-)|(-rhl-)|(-fc-)'));

	$psgconf->register_actions(
		PSGConf::Action::GenerateFile::EnvFile->new(
			'name'			=> '/etc/sysconfig/autofsck',
			'description'		=> 'Auto fsck config file',
			'requires_reboot'	=> 1,
			'vars'			=> { AUTOFSCK_DEF_CHECK => 'yes' }
		)
	);

	if ($psgconf->data_obj('rhn_enable')->equals('true')) {
		$psgconf->register_actions(
			PSGConf::Action::GenerateFile::EnvFile->new(
				name			=> '/etc/sysconfig/rhn/up2date',
				description	=> 'RHN configuration file',
				mode			=> 0600,
				vars			=> $psgconf->data_obj('rhn_config')->get()
			),
			PSGConf::Action::ModifyFile->new(
				'name'         => '/etc/sysconfig/rhn/systemid',
				'modify_func'  => \&_modify_systemid,
				'uid'          => 0,
				'gid'          => 0,
				'mode'         => 0600
			)
		);
	}

	if ($psgconf->data_obj('yum_enable')->equals('true')) {
		$psgconf->register_actions(
			PSGConf::Action::GenerateFile::AppConfig->new(
				name			=> '/etc/yum.conf',
				description	=> 'Yum configuration file',
				mode			=> 0644,
				stanza		=> 1,
				vars			=> $psgconf->data_obj('yum_config')->get()
			)
		);
	}

	if ($psgconf->data_obj('rhn_enable')->equals('true') ||
		$psgconf->data_obj('yum_enable')->equals('true')) {
		foreach $key ( @{$psgconf->data_obj('gpg_keys')->get()} ) {
			$psgconf->register_actions(
				PSGConf::Action::RPMImportGPGKey->new(
					name		=> "GPG KeyID $key",
					key		=> $key,
					rpm_cmd	=> $psgconf->data_obj('pkg_rpm_cmd')->get(),
					gpg_cmd	=> $psgconf->data_obj('gpg_cmd')->get(),
					keyserver	=> $psgconf->data_obj('gpg_keyserver')->get()
				)
			);
		}
	}
}


###############################################################################
###  ModfileFile plugin to edit /etc/sysconfig/rhn/systemid
###############################################################################

sub _modify_systemid {
	my ($action, $infh, $fh, $psgconf) = @_;
	my ($line, $in_profile_name);

	while ($line = <$infh>) {
		if ($line =~ m|^<name>profile_name</name>| ) {
			$in_profile_name = 1;
		} elsif ( $in_profile_name &&
			$line =~ m|^<value><string>.*</string></value>| ) {
			$line = '<value><string>' 
				. $psgconf->data_obj('hostname')->get()
				. "</string></value>\n";
			$in_profile_name = 0;
		}

		print $fh $line;
	}

	return 1;
}


###############################################################################
###  policy methods
###############################################################################

### create RC script
sub _policy_add_rc_scripts
{
	my ($self, $psgconf) = @_;
          
	if ( $psgconf->data_obj('yum_enable')->equals('true') ) {
		$psgconf->data_obj('rc_scripts')->insert(
			{ 'yum' => { 'state' => 'enable' }}
		);
	} elsif ( $psgconf->data_obj('rhn_enable')->equals('true') ) {
		$psgconf->data_obj('rc_scripts')->insert(
			{ 'rhnsd' => { 'state' => 'enable' }}
		);
	} 
}

sub _policy_add_packages
{
	my ($self, $psgconf) = @_;

	if ( $psgconf->data_obj('yum_enable')->equals('true')) {
		$self->{enable} = 'yum_enable';
		$self->{packages} = 'yum_packages';
	} else {
		$self->{enable} = 'rhn_enable';
		$self->{packages} = 'rhn_packages';
	}

	$self->_add_pkgs($psgconf);
}


###############################################################################
###  Constructor
###############################################################################

sub new
{
	my ($class, $psgconf) = @_;
	my ($self);

	$self = {};
	bless($self, $class);

	$self->{name} = 'RedHat';

	$psgconf->register_data(
		rhn_enable		=> PSGConf::Data::Boolean->new(
							value => 'false'
						),
		yum_enable		=> PSGConf::Data::Boolean->new(
							value => 'false'
						),
		rhn_config		=> PSGConf::Data::Hash->new(),
		yum_config		=> PSGConf::Data::Hash->new(),
		pkg_up2date_cmd	=> PSGConf::Data::String->new(
							'value_abspath' => 1,
							value => "/usr/sbin/up2date"
						),
		pkg_yum_cmd		=> PSGConf::Data::String->new(
							'value_abspath' => 1,
							value => "/usr/bin/yum"
						),
		gpg_cmd			=> PSGConf::Data::String->new(
							'value_abspath' => 1,
							value => "/usr/bin/gpg"
						),
		gpg_keys			=> PSGConf::Data::List->new(),
		gpg_keyserver		=> PSGConf::Data::String->new(
							value => "hkp://subkeys.pgp.net"
						),
		rhn_packages		=> PSGConf::Data::List->new(),
		yum_packages		=> PSGConf::Data::List->new()
	);

	$psgconf->register_policy($self,
		redhat_add_packages		=> '_policy_add_packages',
		redhat_add_rc_scripts	=> '_policy_add_rc_scripts'
	);

	return $self;
}


###############################################################################
###  documentation
###############################################################################

1;

__END__

=head1 NAME

PSGConf::Control::RedHat - psgconf control class for RedHat-specific configuration

=head1 SYNOPSIS

In F<psgconf_modules>:

  Control PSGConf::Control::RedHat

=head1 DESCRIPTION

The B<PSGConf::Control::RedHat> module provides a B<psgconf> control object
for RedHat-specific configuration.  It supports the following methods:

=over 4

=item new()

The constructor.  Its parameter is a reference to the B<PSGConf>
object.  It registers the following data objects:

=over 4

=item I<rhn_enable>

A B<PSGConf::Data::Boolean> object indicating whether RHN should be
configured.  Defaults to no.

=item I<yum_enable>

A B<PSGConf::Data::Boolean> object indicating whether Yum should be
configured.  Defaults to no.

=item I<rhn_config>

A B<PSGConf::Data::Hash> object containing the contents of the 
F</etc/sysconfig/rhn/up2date> file.

=item I<yum_config>

A B<PSGConf::Data::Hash> object containing the contents of the 
F</etc/yum.conf> file.

=item I<gpg_cmd>

A B<PSGConf::Data::String> object that determines the location of the
I<GnuPG> command.  Defaults to F</usr/bin/gpg>.

=item I<gpg_keyserver>

A B<PSGConf::Data::String> object that determines what GPG Key server
to fetch the keys from.  Defaults to F<hkp://subkeys.pgp.net>.

=item I<gpg_keys>

A B<PSGConf::Data::List> object containing the GPG keys to trust when
installing packages.

=item I<rhn_packages>

A B<PSGConf::Data::List> object containing the packages to install
in support of RHN.

=item I<yum_packages>

A B<PSGConf::Data::List> object containing the packages to install
in support of Yum.

=item I<pkg_up2date_cmd>

A B<PSGConf::Data::String> object that determines the location of the
I<up2date> command.  The default is F</usr/sbin/up2date>.

=item I<pkg_yum_cmd>

A B<PSGConf::Data::String> object that determines the location of the
I<yum> command.  The default is F</usr/bin/yum>.

=back

The constructor also registers the following policy methods:

=over 4

=item I<redhat_add_packages>

If I<rhn_enable> is set, adds the I<rhn_packages> to the 
I<pkg_install_list>.  If I<yum_enable> is set, adds the
I<yum_packages> to the I<pkg_install_list>.

=item I<redhat_add_rc_scripts>

If I<rhn_enable> is set, enables the I<rhnd> startup script.
If I<yum_enable> is set, enables the I<yum> startup script.

=back

=item decide()

=over 4

=item *

Under systems other than RedHat, returns without doing anything.
Otherwise, registers B<PSGConf::Action::GenerateFile::EnvFile> action
objects to create the F</etc/sysconfig/autofsck> file.

=item *

Registers a B<PSGConf::Action::GenerateFile::EnvFile> object to create
F</etc/sysconfig/rhn/up2date> if I<rhn_enable> is set.

=item *

Registers a B<PSGConf::Action::GenerateFile::AppConfig> object to create
F</etc/yum.conf> if I<yum_enable> is set.

=item *

Registers a B<PSGConf::Action::RPMImportGPGKey> object to download and
install all the keys listsed in F<gpg_keys> if either I<rhn_enable> or
I<yum_enable> is set.

=item *

Registers a B<PSGConf::Action::ModifyFile> object to change the C<profile_name>
in F</etc/sysconfig/rhn/systemid> if I<rhn_enable> is set.

=back

=back

=head1 SEE ALSO

C<CE<lt>up2date(8)E<gt>>

C<CE<lt>yum(8)E<gt>>

L<perl>

L<PSGConf>

L<PSGConf::Action::GenerateFile::AppConfig>

L<PSGConf::Action::GenerateFile::EnvFile>

L<PSGConf::Action::ModifyFile>

L<PSGConf::Action::RPMImportGPGKey>

L<PSGConf::Control::Packages>

L<PSGConf::Data::Boolean>

L<PSGConf::Data::Hash>

L<PSGConf::Data::List>

L<PSGConf::Data::String>

L<psgconf-intro>

=cut


syntax highlighted by Code2HTML, v. 0.9.1