Skip to content

Commit

Permalink
aii-ks: add kickstart_post_script plugin module to generate the post …
Browse files Browse the repository at this point in the history
…section as a standalone script
  • Loading branch information
stdweird committed Jul 1, 2021
1 parent bd1d314 commit 585f48c
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 16 deletions.
60 changes: 45 additions & 15 deletions aii-ks/src/main/perl/ks.pm
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ our $EC = LC::Exception::Context->new->will_store_all;

our $this_app = $main::this_app;
# Modules that may be interesting for hooks.
our @EXPORT_OK = qw (ksuserhooks ksinstall_rpm get_repos replace_repo_glob);
our @EXPORT_OK = qw (ksuserhooks ksinstall_rpm get_repos replace_repo_glob get_fqdn);

# PAN paths for some of the information needed to generate the
# Kickstart.
Expand Down Expand Up @@ -110,21 +110,36 @@ sub get_anaconda_version
return $version;
}

sub _ks_filename
{
my ($self, $ksdir, $fqdn) = @_;
return "$ksdir/$fqdn.ks";
}

# Opens the kickstart file and sets its handle as the default.
sub ksopen
sub ks_filename
{
my ($self, $cfg) = @_;

my $fqdn = get_fqdn($cfg);

my $ksdir = $this_app->option (KSDIROPT);
$self->debug(3,"Kickstart file directory = $ksdir");
$self->debug(3, "Kickstart file directory = $ksdir");

return $self->_ks_filename($ksdir, $fqdn);
}


# Opens the kickstart file and sets its handle as the default.
sub ksopen
{
my ($self, $cfg) = @_;

my $ks = CAF::FileWriter->open(
$self->ks_filename($cfg),
mode => 0664,
log => $this_app
);

my $ks = CAF::FileWriter->open ("$ksdir/$fqdn.ks",
mode => 0664,
log => $this_app
);
select ($ks);
}

Expand Down Expand Up @@ -1724,7 +1739,9 @@ EOF
# this method.
sub post_install_script
{
my ($self, $config, $packages, $repos) = @_;
my ($self, $config, $packages, $repos, $is_kickstart) = @_;

$is_kickstart = 1 if ! defined($is_kickstart);

my $tree = $config->getElement (KS)->getTree;
my $version = get_anaconda_version($tree);
Expand All @@ -1734,21 +1751,28 @@ sub post_install_script
my $logfile = '/tmp/post-log.log';
my $logaction = log_action($config, $logfile);

print <<EOF;
if ($is_kickstart) {
print <<EOF;
%post --nochroot
test -f /tmp/pre-log.log && cp -a /tmp/pre-log.log /mnt/sysimage/root/
EOF

ksuserhooks ($config, POSTNOCHROOTHOOK);
ksuserhooks ($config, POSTNOCHROOTHOOK);

print <<EOF;
print <<EOF;
%end
%post
EOF

};

print <<EOF;
# %post phase. The base system has already been installed. Let's do
# some minor changes and prepare it for being configured.
$logaction
Expand Down Expand Up @@ -1924,10 +1948,15 @@ echo 'End of post section'
# Drain remote logger (0 if not relevant)
sleep \$drainsleep
EOF

if ($is_kickstart) {
print <<EOF;
%end
EOF

};
}

# Closes the Kickstart file and returns everything to its normal
Expand Down Expand Up @@ -1969,7 +1998,8 @@ sub Unconfigure
return 1;
}

my $ksdir = $main::this_app->option (KSDIROPT);
unlink ("$ksdir/$fqdn.ks");
unlink ($self->ks_filename($config));
return 1;
}

1;
70 changes: 70 additions & 0 deletions aii-ks/src/main/perl/ks_post_script.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#${PMpre} NCM::Component::ks_post_script${PMpost}

# Generate a the ks post section of a node as a standalone script.

use parent qw (NCM::Component::ks);

use CAF::FileWriter;
use NCM::Component::ks qw(get_fqdn);

sub _ks_filename
{
my ($self, $ksdir, $fqdn) = @_;
return "$ksdir/kickstart_post_$fqdn.sh";
}

# Cancels the open filewriter instance on the script
# and returns everything (the select magic/hack) to its normal state.
# Returns the content of the cancelled filewriter instance
sub ksclose
{
my $fh = select;

my $text = "$fh";

select (STDOUT);

$fh->cancel();
$fh->close();

return "$text";
}

sub make_script
{
my ($self, $cfg, $post_script) = @_;

my $fh = CAF::FileWriter->open($self->ks_filename($cfg), mode => 0755, log => $self);
print $fh $post_script;
$fh->close();
}

# Prints the kickstart file.
sub Configure
{
my ($self, $config) = @_;

my $fqdn = get_fqdn($config);
if ($CAF::Object::NoAction) {
$self->info ("Would run " . ref ($self) . " on $fqdn");
return 1;
}

$self->ksopen($config);

# ignore the packages that are treated in install for now
# for now assume, all is there already
# this is not kickstart
# won't generate the POSTNOCHROOTHOOK
# no repos passed
$self->post_install_script ($config, [], {}, 0);

my $post_script = $self->ksclose();

$self->make_script($config, $post_script);

return 1;
}


1;
3 changes: 2 additions & 1 deletion aii-ks/src/test/perl/00-load.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use strict;
use warnings;
use Test::Quattor;
use Test::More tests => 1;
use Test::More tests => 2;

use_ok("NCM::Component::ks");
use_ok("NCM::Component::ks_post_script");
24 changes: 24 additions & 0 deletions aii-ks/src/test/perl/kickstart_post_script.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use strict;
use warnings;
use Test::More;
use Test::Quattor qw(kickstart_post_script);
use NCM::Component::ks_post_script;

our $this_app = $main::this_app;

$this_app->{CONFIG}->define('osinstalldir');
$this_app->{CONFIG}->set('osinstalldir', '/some/path');

my $obj = Test::Quattor::Object->new();

my $ks = NCM::Component::ks_post_script->new('ks_post_script', $obj);

my $cfg = get_config_for_profile('kickstart_post_script');

$ks->Configure($cfg);

my $fh = get_file('/some/path/kickstart_post_x.y.sh');
like("$fh", qr{# %post phase}, "contains the post code");
unlike("$fh", qr{^%post}m, "does not contain the %post tag");

done_testing;
3 changes: 3 additions & 0 deletions aii-ks/src/test/resources/kickstart_post_script.pan
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object template kickstart_post_script;

include 'kickstart';

0 comments on commit 585f48c

Please sign in to comment.