find upgradable packages and optionally update apt index
[scpubgit/System-Introspector.git] / bin / system-introspector
CommitLineData
b549a663 1#!/usr/bin/env perl
2use strictures 1;
3
4use Getopt::Long;
5use Pod::Usage;
6use System::Introspector::State;
7use File::Tree::Snapshot;
8use System::Introspector::Config;
9
10GetOptions(
11 'c|config=s' => \my $config_file,
12 's|storage=s' => \my $storage_dir,
13 'H|host=s' => \my $hostname,
a5e1e1c6 14 'U|user=s' => \my $username,
b549a663 15 'allow-empty' => \my $allow_empty,
16 'a|all' => \my $update_all,
17 'g|group=s' => \my @update_groups,
18 'h|help' => sub { pod2usage(0) },
19) or pod2usage(2);
20
21die "Requires --all or --group option\n"
22 unless $update_all or @update_groups;
23
a5e1e1c6 24die "The --user option also requires a --host to be set\n"
25 if defined($username) and not defined($hostname);
26
b549a663 27my $config = System::Introspector::Config->new(
28 config_file => (defined($config_file)
29 ? $config_file
30 : "$storage_dir/main.conf"),
31);
32
33$config->has_group($_) or die "Unknown group '$_'\n"
34 for @update_groups;
35
36@update_groups = $config->groups
37 if $update_all;
a5e1e1c6 38
39my $sudo_user = $config->sudo_user;
b549a663 40
41for my $group (@update_groups) {
42 my $group_dir = "$storage_dir/$group";
43 print "Group $group at $group_dir\n";
44 my $storage = File::Tree::Snapshot->new(
45 storage_path => $group_dir,
46 allow_empty => $allow_empty,
47 );
48 $storage->create
49 unless $storage->exists;
50 my $state = System::Introspector::State->new(
a5e1e1c6 51 defined($hostname) ? (host => $hostname) : (),
52 defined($username) ? (user => $username) : (),
53 defined($sudo_user) ? (sudo_user => $sudo_user) : (),
b549a663 54 storage => $storage,
55 config => $config->config_for_group($group),
56 );
57 eval { $state->fetch_and_store };
58 if (my $error = $@) {
a5e1e1c6 59 warn "Error: $error\n";
b549a663 60 $storage->reset;
61 }
62 else {
63 $storage->commit;
64 }
65}
66
67__END__
68
69=head1 NAME
70
71system-introspector - Generate System Introspection Data
72
73=head1 SYNOPSIS
74
75 system-introspector --storage <path> [OPTIONS]
76
77=head1 OPTIONS
78
79=head2 -s <path>, --storage <path>
80
81Path to storage. Always required.
82
83=head2 -H <host>, --host <host>
84
85Remote host gathering
86
87=head2 --allow-empty
88
89Allow empty commits to storage.
90
91=head2 -h, --help
92
93Display help.
94
95=cut