-
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-core: add ansible playbook, role and task module
- Loading branch information
Showing
4 changed files
with
210 additions
and
0 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
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; |
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,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; |
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,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; |
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,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; |