#!/usr/bin/env perl

# Copyright (C) 2008  Joshua Hoblitt
#
# $Id: neb-voladm,v 1.4 2008-10-15 01:56:00 jhoblitt Exp $

use strict;
use warnings FATAL => qw( all );

use vars qw( $VERSION );
$VERSION = '0.01';

use DBI;
use Nebulous::Server::SQL;
use URI::file;
use SQL::Interp qw( sql_interp );
use URI;

use Getopt::Long qw( GetOptions :config auto_help auto_version );
use Pod::Usage qw( pod2usage );

my (
    $allocate,
    $available,
    $db,
    $dbhost,
    $dbpass,
    $dbuser,
    $debug,
    $vhost,
    $vname,
    $xattr,
);

$db     = $ENV{'NEB_DB'} unless $db;
$dbhost = $ENV{'NEB_DBHOST'} || 'localhost';
$dbuser = $ENV{'NEB_USER'} unless $dbuser;
$dbpass = $ENV{'NEB_PASS'} unless $dbpass;

GetOptions(
    'allocate=i'        => \$allocate,
    'available=i'       => \$available,
    'db|d=s'            => \$db,
    'debug'             => \$debug,
    'host=s'            => \$dbhost,
    'pass|p=s'          => \$dbpass,
    'user|u=s'          => \$dbuser,
    'vhost=s'           => \$vhost,
    'vname|n=s'         => \$vname,
    'xattr=i'           => \$xattr,
) || pod2usage( 2 );

pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
pod2usage( -msg => "Required options: --db --user --pass", -exitval => 2 )
    unless defined $db && defined $dbuser && defined $dbpass;

# are we listing the volumes or updating?
if (defined $allocate or defined $available or defined $xattr) {
    pod2usage( -msg => "Options: --allocate, --avaiable, and --xattr Require options --vhost or --vname", -exitval => 2 )
        unless defined $vhost or defined $vname;
    pod2usage( -msg => "Options: --allocate, --avaiable, and --xattr Must be 0 or 1", -exitval => 2)
        if (defined $allocate and $allocate !~ m/^[01]$/)
        or (defined $available and $available !~ m/^[01]$/)
        or (defined $xattr and $xattr !~ m/^[01]$/);
}

my $dbh = DBI->connect(
    "DBI:mysql:database=$db:host=$dbhost",
    $dbuser,
    $dbpass,
    {
        RaiseError => 1,
        PrintError => 0,
        AutoCommit => 1,
    },
);

my %constraint;
$constraint{'v.host'} = $vhost if defined $vhost;
$constraint{'v.name'} = $vname if defined $vname;

my %set;
$set{'v.allocate'} = $allocate if defined $allocate;
$set{'v.available'} = $available if defined $available;
$set{'v.xattr'} = $xattr if defined $xattr;

if (%set) {
    my ($q, @bind) = sql_interp("UPDATE volume AS v SET", \%set, "WHERE", \%constraint);
    warn "$q\n" if $debug;
    my $query = $dbh->prepare($q);
    $query->execute(@bind);
} 

my $sql = Nebulous::Server::SQL->new();
my ($q, @bind);
if (%constraint) {
    ($q, @bind) = sql_interp($sql->get_volumes . "WHERE", \%constraint);
} else {
    $q = $sql->get_volumes;
}

warn "$q\n" if $debug;
my $query = $dbh->prepare($q);
$query->execute(@bind);

my $format  = "%-15s %-15s %-10s %-10s %-10s %-10s\n";
my @columns = qw(host name mounted allocate available xattr);
printf($format, @columns);

while (my $row = $query->fetchrow_hashref) {
    printf($format, @$row{@columns});
}

__END__

=pod

=head1 NAME

neb-voladm - manage Nebulous storage volumes

=head1 SYNOPSIS

    neb-addvol [--vname <volume name>] [--vhost <volume host>]
    [--allocate <0|1>] [--available <0|1>] [--xattr <0|1>]
    [--host <db host> ] [--db <db name>] [--user <username>] [--pass <password>]

=head1 DESCRIPTION

This program manages a Nebulous server's list of known storage volumes.  the
appropriate set of tables.  Any pre-existing tables with conflicting names are
first removed.

=head1 OPTIONS

=over 4

=item * --vname <volume name>

Symbolic name of the storage volume.

Optional.

=item * --vhost <volume host>

Name of the host on which the storage volume(s) reside.

Optional.

=item * --allocate <0|1>

=item * --available <0|1>

=item * --xattr <0|1>

Enables/Disables these flags on the storage volume.

Requires either the C<--vname> or c<--vhost> options.

=item * --db|-d <db name>

Name of database (C<namespace>) to create tables in.

Optional if the appropriate environment variable is set.

=item * --host <db host>

Host name where the database resides.

Defaults to C<localhost>.

=item * --user|-u <username>

Username to authenticate with.

Optional if the appropriate environment variable is set.

=item * --pass|-p <password>

Password to authenticate with.

Optional if the appropriate environment variable is set.

=back

=head1 EXAMPLE USAGE

=head2 Listing all volumes

    $ neb-voladm
    host            name            mounted    allocate   available  xattr     
    ipp004          ipp004.0        1          0          1          0         
    ipp005          ipp005.0        1          0          1          0         
    ipp006          ipp006.0        1          1          1          0
    ...

=head2 Listing just the volumes on host C<ipp025>

    $ neb-voladm --vhost ipp025
    host            name            mounted    allocate   available  xattr     
    ipp025          ipp025.0        1          1          0          0     

=head2 Listing just the volume symbolicly named C<ipp006.0>

    $ neb-voladm -vname ipp006.0
    host            name            mounted    allocate   available  xattr     
    ipp006          ipp006.0        1          1          1          0    

=head2 Turning the C<available> flag off for all volumes on the host named C<ipp012>

    $ neb-voladm --vhost ipp012 --available 0
    host            name            mounted    allocate   available  xattr     
    ipp012          ipp012.0        0          1          0          

=head1 ENVIRONMENT

These environment variables may be used in place of the specified command line
options.  All command line option will override the corresponding environment
value.

=over 4

=item * C<NEB_DB>

Equivalent to --db|-d

=item * C<NEB_HOST>

Equivalent to --host

=item * C<NEB_USER>

Equivalent to --user|-u

=item * C<NEB_PASS>

Equivalent to --pass|-p 

=back

=head1 CREDITS

Just me, myself, and I.

=head1 SUPPORT

Please contact the author directly via e-mail.

=head1 AUTHOR

Joshua Hoblitt <jhoblitt@cpan.org>

=head1 COPYRIGHT

Copyright (C) 2008  Joshua Hoblitt.  All rights reserved.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA  02111-1307, USA.

The full text of the license can be found in the L<perlgpl> Pod as supplied
with Perl 5.8.1 and later.

=head1 SEE ALSO

L<Nebulous::Server>, L<neb-initdb>, L<neb-voladd>, L<nebdiskd>

=cut
