OCS-Client

 view release on metacpan or  search on metacpan

examples/ocssvn.pl  view on Meta::CPAN

	! exists $metas{$computer->{NAME}}
	    or warn "! Duplicate NAME found ($computer->{NAME}) in meta. Keeping just the first one found...\n"
		and next COMPUTER;
	$metas{$computer->{NAME}} = $computer;
    }
    warn "* There is a total of ", scalar(keys %metas), " inventoried computers in OCS.\n" if $Verbose;

    # Remove from the working copy any computer not found in OCS.
    opendir HOSTS, '.';
    foreach my $file (grep {-f $_} readdir HOSTS) {
	if ($file =~ /^(?<name>.*)\.js$/ && ! exists $metas{$+{name}}) {
	    warn "* Remove file $file from working copy since there's no corresponding computer in OCS.\n" if $Verbose;
	    shell("svn rm -q '$file'");
	}
    }
    closedir HOSTS;

    # Collect the IDs of the ones recently inventoried or never gotten before
    map {$_->{DATABASEID} => $_->{NAME}}
	grep { $All || ! exists $_->{LASTDATE} || $_->{LASTDATE} gt $last_tag_time || ! -e "$_->{NAME}.js" }
	    values %metas;
};
warn "* ", scalar(keys %ids), " of which have been inventoried since $last_tag_time or neven gotten before.\n" if $Verbose;

# FIXME: The OCS documentation says that I can ask for a bunch of
# computers in a single get_computers_V1 invokation. It would be
# interesting to ask for computers in batches of 10, for instance, to
# make it faster.

{
    my $i = 0;
    my $total = keys %ids;
    while (my ($id, $name) = each %ids) {
	++$i;
	warn "* [$i/$total] get_computers_V1(ID => $id, checksum => $Checksum)\n" if $Verbose;
	my @computers = $ocs->get_computers_V1(id => $id, checksum => $Checksum);
	@computers > 0
	    or warn "! Can't get any computer for id $id.\n"
		and next;
	@computers == 1
	    or warn "! There are ", scalar(@computers), " computers for id $id. I'm considering just the first one.\n";

	my $file = "$name.js";
	my $existed = -e $file;

	warn '* ', ($existed ? 'Update' : 'Add   '), " $file\n" if $Verbose;
	unless ($Dont) {
	    my $computer = OCS::Client::prune($computers[0]);

	    # Some computers don't have this important field in their
	    # INVENTORY's ACCOUNTINFO. Hence, we insert it from the
	    # META we grokked earlier.
	    foreach my $accountinfo (values %{$computer->{ACCOUNTINFO}}) {
		$accountinfo->{DATABASEID} //= $id;
	    }

	    open my $fd, '>:raw', $file;
	    $fd->print($json->utf8->encode($computer));
	}

	shell("svn add -q '$file' && svn propset -q svn:mime-type 'text/javascript; charset=UTF-8' '$file'")
	    unless $existed;
    }
}

warn "* Commit changes...\n";
shell("svn ci -q -m 'ocssvn commit'");

my $new_tag = strftime("%F%T", localtime);
$new_tag =~ tr/0-9//cd;
warn "* Previous tag: $svn_url/tags/$last_tag\n";
warn "* New      tag: $svn_url/tags/$new_tag\n";
shell("svn cp -m \"ocssvn tag\" ^/trunk ^/tags/$new_tag");

shell('svn up -q');

warn "* TIME: ", scalar(localtime), "\n" if $Verbose;


__END__
=head1 NAME

ocssvn.pl - keep OCS computer data versioned in Subversion.

=head1 SYNOPSIS

ocssvn.pl --svn=WCPATH --ocsurl=URL [--checksum=NUM] [--all] [--dont] [--verbose]

=head1 DESCRIPTION

Use this script to keep the information from a OCS server
(L<http://www.ocsinventory-ng.org/>) versioned in a Subversion
repository.

The information about each computer in OCS is kept in a text file in
JSON format (http://json.org/), which is a structured and readable
format.

First you need to create a dedicated Subversion repository to keep OCS
information. Also, you need to create a C<trunk> and a C<tags>
directory at the root of the repository. You can do this with these
commands:

    $ svnadmin create /path/to/ocssvnrepo
    $ svn mkdir -m'Create standard root directories' file:///path/to/ocssvnrepo/{trunk,branches,tags}

Then, checkout the trunk directory creating a working copy. The
working copy can be in the same computer as the repository or in a
remote machine. You need to adjust the URL accordingly.

    $ svn checkout file:///path/to/ocssvnrepo/trunk /path/to/ocssvn

Now you can run the script. The two required options are the path to
the Subversion working copy that you just created (C<--svn>) and the
URL to the OCS server.

    $ ocssvn.pl --svn=/path/to/ocssvn --ocsurl=http://ocs.domain.com/

On its first run, the script will fetch information about all
computers registered in OCS and create a file called C<name.js> in the
working copy. The C<name> part is taken from the NAME of the computer



( run in 1.715 second using v1.01-cache-2.11-cpan-71847e10f99 )