transform_exceptions
);
+has apt_lists_dir => (is => 'ro', builder => 1);
+has apt_update_after => (is => 'ro', default => sub { 86400 });
+has apt_update => (is => 'ro');
+
+sub _build_apt_lists_dir { '/var/lib/apt/lists' }
+
sub gather {
my ($self) = @_;
return {
+ update => {
+ last => $self->_last_apt_update,
+ run => transform_exceptions {
+ return { result => $self->_check_apt_state };
+ },
+ },
installed => transform_exceptions {
return { packages => $self->_gather_installed };
},
+ upgradable => transform_exceptions {
+ return { actions => $self->_gather_upgradable };
+ },
};
}
+sub _last_apt_update {
+ my ($self) = @_;
+ return scalar( (stat($self->apt_lists_dir))[9] );
+}
+
+sub _check_apt_state {
+ my ($self) = @_;
+ return 'disabled' unless $self->apt_update;
+ my $threshold = $self->apt_update_after;
+ my $last_change = $self->_last_apt_update;
+ return 'no'if ($last_change + $threshold) > time;
+ handle_from_command 'apt-get update';
+ return 'yes';
+}
+
sub _open_dpkg_query_pipe {
my ($self) = @_;
return handle_from_command 'dpkg-query --show';
}
+sub _open_apt_get_upgrade_simulation_pipe {
+ my ($self) = @_;
+ return handle_from_command 'apt-get -s upgrade';
+}
+
+sub _gather_upgradable {
+ my ($self) = @_;
+ my $pipe = $self->_open_apt_get_upgrade_simulation_pipe;
+ my %action;
+ while (defined( my $line = <$pipe> )) {
+ chomp $line;
+ if ($line =~ m{^(inst|remv)\s+(\S+)\s+(.+)$}i) {
+ $action{ lc($1) }{ $2 } = $3;
+ }
+ }
+ return \%action;
+}
+
sub _gather_installed {
my ($self) = @_;
my $pipe = $self->_open_dpkg_query_pipe;