From 90c968fd46935c8548d165b7a6904f79f0a6834f Mon Sep 17 00:00:00 2001 From: stdweird Date: Wed, 20 Oct 2021 13:12:30 +0200 Subject: [PATCH] aii-core: add ansible playbook, role and task module --- aii-core/src/main/perl/Playbook.pm | 83 ++++++++++++++++++++++++++++++ aii-core/src/main/perl/Role.pm | 46 +++++++++++++++++ aii-core/src/main/perl/Task.pm | 35 +++++++++++++ aii-core/src/test/perl/playbook.t | 46 +++++++++++++++++ 4 files changed, 210 insertions(+) create mode 100644 aii-core/src/main/perl/Playbook.pm create mode 100644 aii-core/src/main/perl/Role.pm create mode 100644 aii-core/src/main/perl/Task.pm create mode 100644 aii-core/src/test/perl/playbook.t diff --git a/aii-core/src/main/perl/Playbook.pm b/aii-core/src/main/perl/Playbook.pm new file mode 100644 index 00000000..4f775303 --- /dev/null +++ b/aii-core/src/main/perl/Playbook.pm @@ -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; diff --git a/aii-core/src/main/perl/Role.pm b/aii-core/src/main/perl/Role.pm new file mode 100644 index 00000000..4a0fa8e2 --- /dev/null +++ b/aii-core/src/main/perl/Role.pm @@ -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; diff --git a/aii-core/src/main/perl/Task.pm b/aii-core/src/main/perl/Task.pm new file mode 100644 index 00000000..18d6e780 --- /dev/null +++ b/aii-core/src/main/perl/Task.pm @@ -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; diff --git a/aii-core/src/test/perl/playbook.t b/aii-core/src/test/perl/playbook.t new file mode 100644 index 00000000..e79f3bf1 --- /dev/null +++ b/aii-core/src/test/perl/playbook.t @@ -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;