Skip to content

Commit 51eea19

Browse files
committed
refactor: move template handling from Provision command to OpenTofu wrapper
- Move _copy_templates method from TorrustDeploy::App::Command::Provision to TorrustDeploy::Provision::OpenTofu::copy_templates - Add Path::Tiny import to OpenTofu.pm for path handling - Update Provision command to use OpenTofu wrapper for template copying - Update unit tests to reflect the new responsibility distribution - Both OpenTofu and Ansible wrappers now have consistent responsibilities: * Template handling and resolution * Command execution This improves code organization and maintains consistency between provision wrappers.
1 parent eb362af commit 51eea19

File tree

4 files changed

+98
-51
lines changed

4 files changed

+98
-51
lines changed

lib/TorrustDeploy/App/Command/Provision.pm

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,13 @@ sub execute {
3434

3535
say "Starting Torrust Tracker provisioning...";
3636

37-
# Set up working directories
37+
# Set up build directory
3838
my $work_dir = path('build');
39-
my $templates_dir = path('templates/provision');
40-
my $tofu_dir = $work_dir->child('tofu');
41-
42-
# Ensure tofu directory exists
43-
$tofu_dir->mkpath unless $tofu_dir->exists;
4439

45-
# Copy templates to working directory
46-
$self->_copy_templates($templates_dir, $tofu_dir);
47-
48-
# Create OpenTofu instance
40+
# Set up OpenTofu working directory and copy resolved templates
41+
my $tofu_dir = $work_dir->child('tofu');
4942
my $tofu = TorrustDeploy::Provision::OpenTofu->new();
43+
$tofu->copy_templates($tofu_dir);
5044

5145
# Initialize OpenTofu
5246
$tofu->init($tofu_dir);
@@ -58,7 +52,7 @@ sub execute {
5852
my $vm_ip = $tofu->get_vm_ip($tofu_dir);
5953
STDOUT->flush();
6054

61-
# Set up Ansible working directory and copy templates
55+
# Set up Ansible working directory and copy resolved templates
6256
my $ansible_dir = $work_dir->child('ansible');
6357
my $ansible = TorrustDeploy::Provision::Ansible->new();
6458
$ansible->copy_templates_and_generate_inventory($vm_ip, $ansible_dir);
@@ -81,41 +75,6 @@ sub execute {
8175
STDOUT->flush();
8276
}
8377

84-
sub _copy_templates {
85-
my ($self, $templates_dir, $tofu_dir) = @_;
86-
87-
say "Copying OpenTofu templates...";
88-
89-
# Check if templates directory exists
90-
unless ($templates_dir->exists) {
91-
die "Templates directory not found: $templates_dir";
92-
}
93-
94-
# Copy main.tf template
95-
my $main_tf_template = $templates_dir->child('tofu/providers/libvirt/main.tf');
96-
my $main_tf_dest = $tofu_dir->child('main.tf');
97-
98-
unless ($main_tf_template->exists) {
99-
die "Template file not found: $main_tf_template";
100-
}
101-
102-
$main_tf_template->copy($main_tf_dest);
103-
say "Copied: $main_tf_template -> $main_tf_dest";
104-
105-
# Copy cloud-init.yml template
106-
my $cloud_init_template = $templates_dir->child('cloud-init.yml');
107-
my $cloud_init_dest = $tofu_dir->child('cloud-init.yml');
108-
109-
unless ($cloud_init_template->exists) {
110-
die "Template file not found: $cloud_init_template";
111-
}
112-
113-
$cloud_init_template->copy($cloud_init_dest);
114-
say "Copied: $cloud_init_template -> $cloud_init_dest";
115-
116-
say "Templates copied successfully.";
117-
}
118-
11978
1;
12079

12180
__END__

lib/TorrustDeploy/Provision/OpenTofu.pm

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package TorrustDeploy::Provision::OpenTofu;
33
use v5.38;
44

55
use JSON;
6+
use Path::Tiny qw(path);
67

78
=head1 NAME
89
@@ -29,6 +30,54 @@ sub new {
2930
return bless {}, $class;
3031
}
3132

33+
=head2 copy_templates
34+
35+
Copy OpenTofu templates to the working directory.
36+
37+
$tofu->copy_templates($tofu_dir);
38+
39+
=cut
40+
41+
sub copy_templates {
42+
my ($self, $tofu_dir) = @_;
43+
44+
say "Copying OpenTofu templates...";
45+
46+
# Ensure tofu directory exists
47+
$tofu_dir->mkpath unless $tofu_dir->exists;
48+
49+
my $templates_dir = path('templates/provision');
50+
51+
# Check if templates directory exists
52+
unless ($templates_dir->exists) {
53+
die "Templates directory not found: $templates_dir";
54+
}
55+
56+
# Copy main.tf template
57+
my $main_tf_template = $templates_dir->child('tofu/providers/libvirt/main.tf');
58+
my $main_tf_dest = $tofu_dir->child('main.tf');
59+
60+
unless ($main_tf_template->exists) {
61+
die "Template file not found: $main_tf_template";
62+
}
63+
64+
$main_tf_template->copy($main_tf_dest);
65+
say "Copied: $main_tf_template -> $main_tf_dest";
66+
67+
# Copy cloud-init.yml template
68+
my $cloud_init_template = $templates_dir->child('cloud-init.yml');
69+
my $cloud_init_dest = $tofu_dir->child('cloud-init.yml');
70+
71+
unless ($cloud_init_template->exists) {
72+
die "Template file not found: $cloud_init_template";
73+
}
74+
75+
$cloud_init_template->copy($cloud_init_dest);
76+
say "Copied: $cloud_init_template -> $cloud_init_dest";
77+
78+
say "Templates copied successfully.";
79+
}
80+
3281
=head2 init
3382
3483
Initialize OpenTofu in the specified directory.

t/unit/command/provision.t

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,12 @@ subtest 'Provision command template copying functionality' => sub {
8686
$test_tofu_dir->mkpath;
8787
ok($test_tofu_dir->exists, 'Test tofu directory created');
8888

89-
# Test the actual _copy_templates method with both required parameters
89+
# Test the OpenTofu copy_templates method since that's where it moved
90+
require TorrustDeploy::Provision::OpenTofu;
91+
my $tofu = TorrustDeploy::Provision::OpenTofu->new();
92+
9093
eval {
91-
$provision_cmd->_copy_templates($templates_dir, $test_tofu_dir);
94+
$tofu->copy_templates($test_tofu_dir);
9295
};
9396
ok(!$@, 'Template copying method executes without error') or diag("Error: $@");
9497

@@ -113,12 +116,10 @@ subtest 'Provision command internal methods' => sub {
113116
app => $app,
114117
});
115118

116-
# Test that the command has the expected private methods
117-
ok($provision_cmd->can('_copy_templates'), 'Provision command has _copy_templates method');
118-
119119
# Test that OpenTofu functionality is available via the OpenTofu package
120120
require TorrustDeploy::Provision::OpenTofu;
121121
my $tofu = TorrustDeploy::Provision::OpenTofu->new();
122+
ok($tofu->can('copy_templates'), 'OpenTofu package has copy_templates method');
122123
ok($tofu->can('init'), 'OpenTofu package has init method');
123124
ok($tofu->can('apply'), 'OpenTofu package has apply method');
124125
ok($tofu->can('get_vm_ip'), 'OpenTofu package has get_vm_ip method');

t/unit/provision/opentofu.t

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
use Test2::V0;
22
use FindBin qw($Bin);
33
use lib "$Bin/../../../lib";
4+
use Path::Tiny qw(path);
5+
use File::Temp qw(tempdir);
46

57
use TorrustDeploy::Provision::OpenTofu;
68

79
subtest 'OpenTofu module loads correctly' => sub {
810
ok(TorrustDeploy::Provision::OpenTofu->can('new'), 'OpenTofu has new method');
11+
ok(TorrustDeploy::Provision::OpenTofu->can('copy_templates'), 'OpenTofu has copy_templates method');
912
ok(TorrustDeploy::Provision::OpenTofu->can('init'), 'OpenTofu has init method');
1013
ok(TorrustDeploy::Provision::OpenTofu->can('apply'), 'OpenTofu has apply method');
1114
ok(TorrustDeploy::Provision::OpenTofu->can('get_vm_ip'), 'OpenTofu has get_vm_ip method');
@@ -17,4 +20,39 @@ subtest 'OpenTofu instance creation' => sub {
1720
isa_ok($tofu, 'TorrustDeploy::Provision::OpenTofu');
1821
};
1922

23+
subtest 'OpenTofu template copying functionality' => sub {
24+
my $tofu = TorrustDeploy::Provision::OpenTofu->new();
25+
26+
# Create temporary directory for testing
27+
my $temp_dir = tempdir(CLEANUP => 1);
28+
my $test_tofu_dir = path($temp_dir)->child('test-tofu');
29+
$test_tofu_dir->mkpath;
30+
31+
# Verify templates directory exists before testing
32+
my $templates_dir = path('templates/provision');
33+
skip_all "Templates directory not found: $templates_dir" unless $templates_dir->exists;
34+
35+
# Test the copy_templates method
36+
eval {
37+
$tofu->copy_templates($test_tofu_dir);
38+
};
39+
ok(!$@, 'copy_templates method executes without error') or diag("Error: $@");
40+
41+
# Verify files were copied correctly
42+
my $target_main_tf = $test_tofu_dir->child('main.tf');
43+
my $target_cloud_init = $test_tofu_dir->child('cloud-init.yml');
44+
45+
ok($target_main_tf->exists, 'main.tf copied to target directory');
46+
ok($target_cloud_init->exists, 'cloud-init.yml copied to target directory');
47+
48+
# Verify content matches the templates if templates exist
49+
my $main_tf_template = $templates_dir->child('tofu/providers/libvirt/main.tf');
50+
my $cloud_init_template = $templates_dir->child('cloud-init.yml');
51+
52+
if ($main_tf_template->exists && $cloud_init_template->exists) {
53+
is($target_main_tf->slurp_utf8, $main_tf_template->slurp_utf8, 'main.tf content matches template');
54+
is($target_cloud_init->slurp_utf8, $cloud_init_template->slurp_utf8, 'cloud-init.yml content matches template');
55+
}
56+
};
57+
2058
done_testing;

0 commit comments

Comments
 (0)