<commit_before>package Tapper::Installer::Precondition::Kernelbuild;


use Moose;
use IO::Handle; # needed to set pipe nonblocking
extends 'Tapper::Installer::Precondition';

use strict;
use warnings;

=head1 NAME

Tapper::Installer::Precondition::Kernelbuild - Build and install a kernel from git

=head1 SYNOPSIS

 my $kernel_precondition = '
precondition_type: kernelbuild
git_url: git://osrc.amd.com/linux-2.6.git
changeset: HEAD
patchdir: /patches
';

 use Tapper::Installer::Precondition::Kernelbuild;
 $kernel = Tapper::Installer::Precondition::Kernelbuild->new($config);
 $kernel->install(YAML::Load($kernel_precondition));


=head1 FUNCTIONS

=cut

=head2 fix_git_url

URL rewrite.

@param string git_url

@return string - fixed git url

=cut

sub fix_git_url
{
	my ($self, $git_url) = @_;
        $self->log->info("Git URL before rewrite: $git_url");
	$git_url =~ s|^git://osrc((\.osrc)?\.amd\.com)?/|git://wotan.amd.com/|;
        $self->log->info("Git URL after  rewrite: $git_url");
	return $git_url;
}

=head2 git_get

This function encapsulates getting a kernel source directory out of a git
repository. It changes the current directory into the the repository.

@param string - repository URL
@param string - revision in this repository


@return success - 0
@return error   - error string

=cut

sub git_get
{
	my ($self, $git_url, $git_rev)=@_;

        # git may generate more output than log_and_exec can handle, thus keep the system()
        chdir $self->cfg->{paths}{base_dir};
	$git_url = $self->fix_git_url($git_url);
	system("git","clone","-q",$git_url,"linux") == 0
          or return("unable to clone git repository $git_url");
	chdir ("linux");
        system("git","checkout",$git_rev) == 0
          or return("unable to check out $git_rev from git repository $git_url");
	return(0);
}

=head2 get_config

Get the kernel config.

@return success - 0
@return error   - error string

=cut

sub get_config
{
        my ($self, $config_file) = @_;
        $config_file ||= $self->cfg->{paths}{config_path}."/kernelconfigs/config_x86_64";
        $self->log->debug("Getting config $config_file");

        return "Can not get config $config_file because the file does not exist"
          if not -e $config_file;

        system("cp $config_file .config") == 0
          or return "Can not get config $config_file";
        return 0;
}

=head2 make_kernel

Build and install a kernel and write all log messages to STDOUT/STDERR.

@return success - 0
@return error   - error string

=cut

sub make_kernel
{
        my ($self) = @_;
        system("make","clean") == 0
          or return("Making mrproper failed: $!");

        system('yes ""|make oldconfig') == 0
          or return("Making oldconfig failed: $!");

        system('make','-j8') == 0
          or return("Build the kernel failed: $!");

        system('make','install') == 0
          or return("Installing the kernel failed: $!");

        system('make','modules_install') == 0
          or return("Installing the kernel failed: $!");

        return 0;
}

=head2 younger

Sort function, sort files based on modification time.

=cut

sub younger { stat($a)->mtime() <=> stat($b)->mtime() }

=head2 make_initrd

Build and install an initrd and write all log messages to STDOUT/STDERR.

@return success - 0
@return error   - error string

=cut

sub make_initrd
{
        my ($self) = @_;
        my ($error, $kernelversion) = $self->log_and_exec("make","kernelversion");
        my $kernel_file = "vmlinuz-$kernelversion";
        
        # double block, the outermost belongs to if, the innermost can be left with last;
        # great stuff, isn't it?
        if (not -e "/boot/$kernel_file") {{
                if (-e "/boot/vmlinuz-${kernelversion}+"){
                        $kernelversion .='+';
                        $kernel_file = "vmlinuz-$kernelversion";
                        last;
                }
                if (-e "/boot/bzImage") {
                        $kernel_file = "bzImage";
                        last;
                }
                if (-e "/boot/bzImage-$kernelversion") {
                        $kernel_file = "bzImage-$kernelversion";
                        last;
                }
                if (-e "/boot/bzImage-$kernelversion+") {
                        $kernel_file = "bzImage-$kernelversion";
                        last;
                }

                my @files = sort younger </boot/vmlinuz-*>;
                if (@files and $files[0] =~/vmlinuz-(.*)/) {
                        $kernel_file = "vmlinuz-$1";
                        last;
                }
                my $filename;
                $filename = join("/tmp/bootdir-content");
                system("ls -l /boot/ > $filename");
                return "kernel install failed, can not find new kernel";
        }}

        system("depmod $kernelversion") == 0
          or return("Can not create initrd file, see log file");

        my $modules = "ixgbe forcedeth r8169 libata sata-sil scsi-mod atiixp ide-disk";
        $modules   .= " ide-core 3c59x tg3 mii amd8111e e1000e bnx2 bnx2x ixgb";
        my $mkinitrd_command = "mkinitrd -k /boot/$kernel_file -i /boot/initrd-$kernelversion ";
        $mkinitrd_command   .= qq(-m "$modules");
        
        $self->log->debug($mkinitrd_command);
        system($mkinitrd_command) == 0
          or return("Can not create initrd file, see log file");

        # prepare_boot called at the end of the install process will generate
        # a grub entry for vmlinuz/initrd with no version string attached
        $error = $self->log_and_exec("ln -sf","/boot/$kernel_file", "/boot/vmlinuz");
        return $error if $error;
        $error = $self->log_and_exec("ln -sf","/boot/initrd-$kernelversion", "/boot/initrd");
        return $error if $error;

        return 0;
}<commit_msg>More elaborated way to get kernel version<commit_after>package Tapper::Installer::Precondition::Kernelbuild;


use Moose;
use IO::Handle; # needed to set pipe nonblocking
extends 'Tapper::Installer::Precondition';

use strict;
use warnings;

=head1 NAME

Tapper::Installer::Precondition::Kernelbuild - Build and install a kernel from git

=head1 SYNOPSIS

 my $kernel_precondition = '
precondition_type: kernelbuild
git_url: git://osrc.amd.com/linux-2.6.git
changeset: HEAD
patchdir: /patches
';

 use Tapper::Installer::Precondition::Kernelbuild;
 $kernel = Tapper::Installer::Precondition::Kernelbuild->new($config);
 $kernel->install(YAML::Load($kernel_precondition));


=head1 FUNCTIONS

=cut

=head2 fix_git_url

URL rewrite.

@param string git_url

@return string - fixed git url

=cut

sub fix_git_url
{
	my ($self, $git_url) = @_;
        $self->log->info("Git URL before rewrite: $git_url");
	$git_url =~ s|^git://osrc((\.osrc)?\.amd\.com)?/|git://wotan.amd.com/|;
        $self->log->info("Git URL after  rewrite: $git_url");
	return $git_url;
}

=head2 git_get

This function encapsulates getting a kernel source directory out of a git
repository. It changes the current directory into the the repository.

@param string - repository URL
@param string - revision in this repository


@return success - 0
@return error   - error string

=cut

sub git_get
{
	my ($self, $git_url, $git_rev)=@_;

        # git may generate more output than log_and_exec can handle, thus keep the system()
        chdir $self->cfg->{paths}{base_dir};
	$git_url = $self->fix_git_url($git_url);
	system("git","clone","-q",$git_url,"linux") == 0
          or return("unable to clone git repository $git_url");
	chdir ("linux");
        system("git","checkout",$git_rev) == 0
          or return("unable to check out $git_rev from git repository $git_url");
	return(0);
}

=head2 get_config

Get the kernel config.

@return success - 0
@return error   - error string

=cut

sub get_config
{
        my ($self, $config_file) = @_;
        $config_file ||= $self->cfg->{paths}{config_path}."/kernelconfigs/config_x86_64";
        $self->log->debug("Getting config $config_file");

        return "Can not get config $config_file because the file does not exist"
          if not -e $config_file;

        system("cp $config_file .config") == 0
          or return "Can not get config $config_file";
        return 0;
}

=head2 make_kernel

Build and install a kernel and write all log messages to STDOUT/STDERR.

@return success - 0
@return error   - error string

=cut

sub make_kernel
{
        my ($self) = @_;
        system("make","clean") == 0
          or return("Making mrproper failed: $!");

        system('yes ""|make oldconfig') == 0
          or return("Making oldconfig failed: $!");

        system('make','-j8') == 0
          or return("Build the kernel failed: $!");

        system('make','install') == 0
          or return("Installing the kernel failed: $!");

        system('make','modules_install') == 0
          or return("Installing the kernel failed: $!");

        return 0;
}

=head2 younger

Sort function, sort files based on modification time.

=cut

sub younger { stat($a)->mtime() <=> stat($b)->mtime() }

=head2 make_initrd

Build and install an initrd and write all log messages to STDOUT/STDERR.

@return success - 0
@return error   - error string

=cut

sub make_initrd
{
        my ($self) = @_;
        my ($error, $kernelversion) = $self->log_and_exec("make","kernelversion");
        my $kernel_file = "vmlinuz-$kernelversion";
        
        # double block, the outermost belongs to if, the innermost can be left with last;
        # great stuff, isn't it?
        if (not -e "/boot/$kernel_file") {{
                if (-e "/boot/vmlinuz-${kernelversion}+"){
                        $kernelversion .='+';
                        $kernel_file = "vmlinuz-$kernelversion";
                        last;
                }
                if (-e "/boot/bzImage") {
                        $kernel_file = "bzImage";
                        last;
                }
                if (-e "/boot/bzImage-$kernelversion") {
                        $kernel_file = "bzImage-$kernelversion";
                        last;
                }
                if (-e "/boot/bzImage-$kernelversion+") {
                        $kernel_file = "bzImage-$kernelversion";
                        last;
                }

                my @files = sort younger </boot/vmlinuz-*>;
                if (@files) {
                        $kernel_file   = $files[0];
                        $kernelversion = $1;
                        last;
                }
                my $filename;
                $filename = "/tmp/bootdir-content";
                system("ls -l /boot/ > $filename");
                return "kernel install failed, can not find new kernel";
        }}

        system("depmod $kernelversion") == 0
          or return("Can not create initrd file, see log file");

        my $modules = "ixgbe forcedeth r8169 libata sata-sil scsi-mod atiixp ide-disk";
        $modules   .= " ide-core 3c59x tg3 mii amd8111e e1000e bnx2 bnx2x ixgb";
        my $mkinitrd_command = "mkinitrd -k /boot/$kernel_file -i /boot/initrd-$kernelversion ";
        $mkinitrd_command   .= qq(-m "$modules");
        
        $self->log->debug($mkinitrd_command);
        system($mkinitrd_command) == 0
          or return("Can not create initrd file, see log file");

        # prepare_boot called at the end of the install process will generate
        # a grub entry for vmlinuz/initrd with no version string attached
        $error = $self->log_and_exec("ln -sf","/boot/$kernel_file", "/boot/vmlinuz");
        return $error if $error;
        $error = $self->log_and_exec("ln -sf","/boot/initrd-$kernelversion", "/boot/initrd");
        return $error if $error;

        return 0;
}
{'fraction_non_alphanumeric': 0.13135268579211715, 'fraction_numerical': 0.009193836724270026, 'mean_word_length': 2.328045325779037, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3306807', 'n_tokens_mistral': 3719, 'n_tokens_neox': 3486, 'n_words': 1009}