1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
package Hostfile::Manager;
use strict;
use warnings;
use Moose;
use File::Find;
use File::Slurp;
use File::Basename qw/dirname/;
our $VERSION = '0.09';
=head1 NAME
Hostfile::Manager - Manage a hostfile by composing multiple fragments into a whole.
=head1 SYNOPSIS
use Hostfile::Manager;
$manager = Hostfile::Manager->new;
$manager->enable_fragment($fragment_name);
$manager->write_hostfile;
=head1 ACCESSORS
=over 6
=item B<< Str path_prefix( [Str $prefix] ) >>
Defines the prefix that will be searched for hostfile fragments. Defaults to '/etc/hostfiles/'.
=cut
has path_prefix => (
is => 'rw',
isa => 'Str',
default => '/etc/hostfiles/',
);
=item B<< Str hostfile_path( [Str $path] ) >>
Defines the path to the hostfile to manage. Defaults to '/etc/hosts'.
=cut
has hostfile_path => (
is => 'rw',
isa => 'Str',
default => '/etc/hosts',
);
=item B<< Str hostfile >>
The contents of the hostfile under management.
=cut
has hostfile => (
is => 'ro',
isa => 'Str',
writer => '_set_hostfile',
lazy => 1,
builder => 'load_hostfile',
init_arg => undef,
);
has blocks => (
is => 'ro',
isa => 'HashRef',
default => sub { {} },
init_arg => undef,
);
=item B<< HashRef fragments >>
The available hostfile fragments.
=item B<< Array fragment_list >>
A list of the names of available fragments.
=cut
sub fragment_list {
my ($self) = @_;
return sort { $a cmp $b } keys %{$self->fragments};
}
=item B<< Str get_fragment( Str $fragment_name ) >>
The contents of an individual hostfile fragment.
=back
=cut
has fragments => (
is => 'ro',
isa => 'HashRef[Str]',
traits => ['Hash'],
lazy => 1,
builder => '_load_fragments',
handles => {
get_fragment => 'get',
},
init_arg => undef,
);
=head1 METHODS
=over 6
=item B<< Hostfile::Manager->new( [\%options] ) >>
Create a new manager instance. Available options are B<path_prefix> and B<hostfile_path>, listed in the L<ACCESSORS|/"ACCESSORS"> section.
=cut
sub load_hostfile {
my ( $self, $filename ) = @_;
$filename = $self->hostfile_path unless defined $filename;
unless ( -e $filename ) {
Carp::croak("Hostfile must exist. File not found at $filename");
}
my $file = read_file($filename);
$self->_set_hostfile($file);
}
=item B<< Bool write_hostfile >>
Write the contents of the hostfile to disk.
=cut
sub write_hostfile {
my $self = shift;
my $filename = $self->hostfile_path;
unless ( ( !-e $filename && -w dirname($filename) ) || -w $filename ) {
Carp::croak("Unable to write hostfile to $filename");
}
write_file( $filename, $self->hostfile );
}
=item B<< Bool fragment_enabled( Str $fragment_name ) >>
Test whether a named fragment is enabled in the hostfile under management.
=cut
sub fragment_enabled {
my ( $self, $fragment_name ) = @_;
$self->hostfile =~ $self->block($fragment_name);
}
=item B<< enable_fragment( Str $fragment_name ) >>
Enable a named fragment. If the fragment is currently enabled, it will be disabled first, removing any modifications that may have been made out-of-band.
=cut
sub enable_fragment {
my ( $self, $fragment_name ) = @_;
my $fragment = $self->get_fragment($fragment_name) or return;
$self->disable_fragment($fragment_name)
if $self->fragment_enabled($fragment_name);
$self->_set_hostfile( $self->hostfile
. "# BEGIN: $fragment_name\n$fragment# END: $fragment_name\n" );
}
=item B<< disable_fragment( Str $fragment_name ) >>
Disable a named fragment.
=cut
sub disable_fragment {
my ( $self, $fragment_name ) = @_;
my $hostfile = $self->hostfile;
$hostfile =~ s/@{[$self->block($fragment_name)]}//g;
$self->_set_hostfile($hostfile);
}
=item B<< toggle_fragment( Str $fragment_name ) >>
Enable a fragment if it is disabled, disable it otherwise.
=cut
sub toggle_fragment {
my ( $self, $fragment_name ) = @_;
if ( $self->fragment_enabled($fragment_name) ) {
$self->disable_fragment($fragment_name);
}
else {
$self->enable_fragment($fragment_name);
}
}
sub block {
my ( $self, $block_name ) = @_;
$self->blocks->{$block_name} ||=
qr/#+\s*BEGIN: $block_name[\r\n](.*)#+\s*END: $block_name[\r\n]/ms;
return $self->blocks->{$block_name};
}
sub _load_fragments {
my $self = shift;
my $fragments = {};
my $prefix = $self->path_prefix;
find(
{
wanted => sub {
return if -d $_;
$_ =~ s{^$prefix}{};
$fragments->{$_} = $self->_load_fragment($_);
},
no_chdir => 1
},
$prefix
);
$fragments;
}
sub _load_fragment {
my ( $self, $fragment_name ) = @_;
my $filename = $self->path_prefix . $fragment_name;
unless ( -e $filename ) {
Carp::carp("Fragment not found at $filename");
return;
}
read_file($filename);
}
=item B<< Str fragment_status_flag( Str $fragment_name ) >>
Returns a string indicating the current status of a named fragment.
=over 2
=item B<"+">
The named fragment is enabled.
=item B<"*">
The named fragment is enabled and has been modified in the sourced hostfile.
=item B<" ">
The named fragment is not enabled.
=back
=back
=cut
sub fragment_status_flag {
my ( $self, $fragment_name ) = @_;
my $fragment_contents = $self->get_fragment($fragment_name);
my ($found) = $self->hostfile =~ /@{[$self->block($fragment_name)]}/g;
return $found ? ( $found eq $fragment_contents ? "+" : "*" ) : " ";
}
no Moose;
__PACKAGE__->meta->make_immutable;
__END__
=head1 LICENSE
Copyright (c) 2010-11,2018 Anthony J. Mirabella. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=head1 AUTHOR
Anthony J. Mirabella <mirabeaj AT gmail DOT com>
|