-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
aii-ks: introduce ks_ansible to generate a playbook with the post script
- Loading branch information
Showing
5 changed files
with
184 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#${PMpre} NCM::Component::ks_ansible${PMpost} | ||
|
||
# Generate a playbook to run the ks post section on a node. | ||
|
||
use parent qw (NCM::Component::ks); | ||
|
||
use CAF::TextRender; | ||
use NCM::Component::ks qw(get_fqdn); | ||
|
||
sub _ks_filename | ||
{ | ||
my ($self, $ksdir, $fqdn) = @_; | ||
return "$ksdir/kickstart_post_$fqdn.yml"; | ||
} | ||
|
||
# Cancels the open filewriter instance on the playbook | ||
# 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_playbook | ||
{ | ||
my ($self, $cfg, $post_script) = @_; | ||
|
||
# make playbook yaml | ||
my @playbook; | ||
|
||
push(@playbook, { | ||
'name' => 'Execute AII KS post section script', | ||
'ansible.builtin.shell' => $post_script, | ||
'chdir' => '/', | ||
'args' => { | ||
'executable' => '/bin/bash', | ||
} | ||
}); | ||
|
||
# textrender the yaml | ||
# bypass issue in CAF::TextRender wrt not allowing arrays | ||
my $trd = CAF::TextRender->new('yaml', {}, log => $self); | ||
$trd->{contents} = \@playbook; | ||
|
||
my $fh = $trd->filewriter($self->ks_filename($cfg)); | ||
if (defined($fh)) { | ||
$fh->close(); | ||
} else { | ||
$self->error("Problem rendering the playbook"); | ||
}; | ||
} | ||
|
||
# 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_playbook($config, $post_script); | ||
|
||
return 1; | ||
} | ||
|
||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use strict; | ||
use warnings; | ||
use Test::More; | ||
use Test::Quattor qw(kickstart_ansible); | ||
use NCM::Component::ks_ansible; | ||
use YAML::XS; | ||
|
||
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_ansible->new('ks_ansible', $obj); | ||
|
||
my $cfg = get_config_for_profile('kickstart_ansible'); | ||
|
||
$ks->Configure($cfg); | ||
|
||
my $fh = get_file('/some/path/kickstart_post_x.y.yml'); | ||
my $data = Load("$fh"); | ||
|
||
my $post = $data->[0]->{"ansible.builtin.shell"}; | ||
$data->[0]->{"ansible.builtin.shell"} = 'POSTSCRIPT'; | ||
|
||
like($post, qr{# %post phase}, "contains the post code"); | ||
unlike($post, qr{^%post}m, "does not contain the %post tag"); | ||
|
||
|
||
is_deeply( | ||
$data, [ | ||
{ | ||
"ansible.builtin.shell" => 'POSTSCRIPT', | ||
"name" => "", | ||
"args" => { | ||
"executable" => "/bin/bash", | ||
}, | ||
"chdir" => "/", | ||
"name" => "Execute AII KS post section script", | ||
}, | ||
], "Converted playbook data"); | ||
|
||
done_testing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
object template kickstart_ansible; | ||
|
||
include 'kickstart'; |