/
InstallManager.pm
226 lines (155 loc) · 5.05 KB
/
InstallManager.pm
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
package App::BCVI::InstallManager;
use warnings;
use strict;
use Digest::MD5;
use Fcntl;
use SDBM_File;
our $VERSION = '1.03';
my($source_signature, %sig_db, $db_file);
sub update_all_installs {
my($self) = @_;
if(my $arg = $self->opt('update-all')) { # usual value '' is false
if($arg eq 'list') {
return $self->list_all_installs
}
elsif($arg =~ m{^-(.+)$}) {
return $self->untrack_host($1);
}
die "Unrecognised argument ('$arg') to --update-all\n";
}
my $sig = $self->install_source_signature();
my $host_sigs = $self->all_install_signatures();
my @hosts = grep { $host_sigs->{$_} ne $sig } sort keys %$host_sigs;
if(@hosts) {
$self->install_to_hosts(@hosts);
}
else {
print "All known targets are up to date\n";
exit(0);
}
}
sub list_all_installs {
my($self) = @_;
my $valid = $self->install_source_signature();
$self->tie_install_sig_db();
foreach my $key (sort keys %sig_db) {
my $sig = $sig_db{$key};
if($sig eq $valid) {
printf("%-30s OK\n", $key);
}
else {
printf("%-30s Needs update\n", $key);
}
}
$self->untie_install_sig_db();
}
sub untrack_host {
my($self, $host) = @_;
print "Discarding install info for $host\n";
$self->tie_install_sig_db();
delete $sig_db{$host};
$self->untie_install_sig_db();
}
sub install_to_host {
my($self, $host) = @_;
my $sig = $self->install_source_signature();
if( $self->SUPER::install_to_host($host) ) {
$self->set_install_signature($host=> $sig);
return 1;
}
return;
}
sub tie_install_sig_db {
my($self) = @_;
$db_file ||= File::Spec->catfile($self->conf_directory, 'install.db');
tie(%sig_db, 'SDBM_File', $db_file, O_RDWR|O_CREAT, 0666)
or die "Couldn't tie SDBM file $db_file: $!; aborting";
}
sub untie_install_sig_db {
my($self) = @_;
untie %sig_db;
}
sub set_install_signature {
my($self, $host, $sig) = @_;
$self->tie_install_sig_db();
$sig_db{$host} = $sig;
$self->untie_install_sig_db();
}
sub get_install_signature {
my($self, $host) = @_;
$self->tie_install_sig_db();
my $sig = $sig_db{$host};
$self->untie_install_sig_db();
return $sig;
}
sub all_install_signatures {
my($self) = @_;
$self->tie_install_sig_db();
my $all = { %sig_db };
$self->untie_install_sig_db();
return $all;
}
sub install_source_signature {
my($self) = @_;
return $source_signature if $source_signature;
my $md5 = Digest::MD5->new;
open my $fh, '<', $0 or die "open($0): $!";
$md5->addfile($fh);
close($fh);
foreach my $path ( $self->installable_files ) {
open my $fh, '<', $path or die "open($path): $!";
$md5->addfile($fh);
close($fh);
}
$source_signature = substr($md5->hexdigest(), 0, 8);
}
App::BCVI->register_option(
name => 'update-all',
arg_spec => ':s',
dispatch_to => 'update_all_installs',
summary => 'update bcvi on all tracked hosts',
description => <<'END_POD'
This option is provided by the InstallManager plugin. It causes C<--install>
to be run against each host where bcvi has been installed but is now out of
date.
Two sub options are available: You can list all known hosts where bcvi has been
installed, with: C<< --update-all=list >>. You can also discard a hostname from
being tracked with: C<< --update-all=-hostname >>.
END_POD
);
App::BCVI->hook_client_class();
1;
__END__
=head1 NAME
App::BCVI::InstallManager - Track where bcvi is installed, to manage updates
=head1 DESCRIPTION
This module is a plugin for C<bcvi> (see: L<App::BCVI>). It tracks the names
of servers where bcvi has been installed using the C<< bcvi --install <host> >>
command.
The plugin also adds the C<--update-all> option to the C<bcvi> command. This
option identifies hosts where an old version of bcvi is installed and re-runs
the C<--install> against those hosts to update the script and aliases.
To get a list of all the hosts where C<bcvi> is installed, you can run:
bcvi --update-all=list
To remove a host from the list, use:
bcvi --update-all=-hostname
Note: Although this plugin does hook into the C<--install> process, it does not
change the behaviour of that process. In particular it will B<not> block an
attempt to install the same version of bcvi as is already installed on a host.
=head1 SUPPORT
You can look for information at:
=over 4
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=App-BCVI-InstallManager>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/App-BCVI-InstallManager>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/App-BCVI-InstallManager>
=item * Search CPAN
L<http://search.cpan.org/dist/App-BCVI-InstallManager>
=back
=head1 COPYRIGHT & LICENSE
Copyright 2010 Grant McLean E<lt>grantm@cpan.orgE<gt>
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut