Skip to content

Commit

Permalink
aii-core: add ansible playbook, role and task module
Browse files Browse the repository at this point in the history
  • Loading branch information
stdweird committed Oct 20, 2021
1 parent fca85b0 commit 90c968f
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 0 deletions.
83 changes: 83 additions & 0 deletions aii-core/src/main/perl/Playbook.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#${PMpre} AII::Playbook${PMpost}

use LC::Exception qw (SUCCESS);
use parent qw(CAF::Object);
use CAF::TextRender;
use CAF::Path;

use AII::Role;

# hosts: playbook hosts
sub _initialize
{
my ($self, $hosts, %opts) = @_;

%opts = () if !%opts;

$self->{log} = $opts{log} if $opts{log};

$self->{data} = {
hosts => $hosts
};
$self->{roles} = [];

return SUCCESS;
}

sub add_role
{
my ($self, $name) = @_;
my $role = AII::Role->new($name, log => $self);
push @{$self->{roles}}, $role;
return $role;
}


# Generate hashref to render into yaml
sub make_data {
my $self = shift;

# make copy of basic data
my $data = {%{$self->{data}}};

# add roles
$data->{roles} = [map {$_->{name}} @{$self->{roles}}];

return $data;
}

# Generate playbook and roles
# root: base working dir
sub write
{
my ($self, $root) = @_;

# Make roles subdir in root
my $cafpath = CAF::Path::mkcafpath(log => $self);
$cafpath->directory("$root/roles");

# Generate all roles and playbook data
my $files = {
main => $self->make_data()
};

foreach my $role (@{$self->{roles}}) {
$files->{"roles/$role->{name}"} = $role->make_data();
}

# Write
foreach my $filename (sort keys %$files) {
my $trd = CAF::TextRender->new(
'yamlmulti',
{'host' => [$files->{$filename}]}, # use yamlmulti to bypass arrayref issue
log => $self,
);
my $fh = $trd->filewriter(
"$root/$filename.yml",
log => $self,
);
$fh->close();
};
}

1;
46 changes: 46 additions & 0 deletions aii-core/src/main/perl/Role.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#${PMpre} AII::Role${PMpost}

use LC::Exception qw (SUCCESS);
use parent qw(CAF::Object);

use AII::Task;

# name: name of role
sub _initialize
{
my ($self, $name, %opts) = @_;

%opts = () if !%opts;

$self->{log} = $opts{log} if $opts{log};

$self->{name} = $name;
$self->{data} = {
};
$self->{tasks} = [];

return SUCCESS;
}

sub add_task
{
my ($self, $name) = @_;
my $task = AII::Task->new($name, log => $self);
push @{$self->{tasks}}, $task;
return $task;
}

# Generate hashref to render into yaml
sub make_data {
my $self = shift;

# make copy of basic data
my $data = {%{$self->{data}}};

# add tasks
$data->{tasks} = [map {$_->make_data()} @{$self->{tasks}}];

return $data;
}

1;
35 changes: 35 additions & 0 deletions aii-core/src/main/perl/Task.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#${PMpre} AII::Task${PMpost}

use LC::Exception qw (SUCCESS);
use parent qw(CAF::Object);

# name: name of task
sub _initialize
{
my ($self, $name, %opts) = @_;

%opts = () if !%opts;

$self->{log} = $opts{log} if $opts{log};

$self->{name} = $name;
$self->{data} = {
};

return SUCCESS;
}

# Generate hashref to render into yaml
sub make_data {
my $self = shift;

# make copy of basic data
my $data = {%{$self->{data}}};

# add tasks
$data->{name} = $self->{name};

return $data;
}

1;
46 changes: 46 additions & 0 deletions aii-core/src/test/perl/playbook.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use strict;
use warnings;
use Test::More;
use Test::Quattor;
use Test::Quattor::Object;
use Cwd;

use AII::Playbook;

$CAF::Object::NoAction = 1;

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

my $pb = AII::Playbook->new("myhost", log => $obj);

my $root = getcwd . "/target/test/playbook/myhost";

$pb->write($root);

# No roles
my $fh = get_file("$root/main.yml");
is("$fh", "---\n- hosts: myhost\n roles: []\n");

# Add role
my $role = $pb->add_role("first");
isa_ok ($role, "AII::Role", "Correct class after add_role");
my $task = $role->add_task("task1");
isa_ok ($task, "AII::Task", "Correct class after add_task");

$task = $role->add_task("task2");

$role = $pb->add_role("second");
$task = $role->add_task("task3");

$pb->write($root);

# No roles
$fh = get_file("$root/main.yml");
is("$fh", "---\n- hosts: myhost\n roles:\n - first\n - second\n");
$fh = get_file("$root/roles/first.yml");
is("$fh", "---\n- tasks:\n - name: task1\n - name: task2\n");
$fh = get_file("$root/roles/second.yml");
is("$fh", "---\n- tasks:\n - name: task3\n");


done_testing;

0 comments on commit 90c968f

Please sign in to comment.