IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 42879


Ignore:
Timestamp:
May 20, 2025, 3:38:12 PM (14 months ago)
Author:
tdeboer
Message:

update to neb-host to del properly with xattr=3

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tags/ipp-ops-20220906/Nebulous-Server/bin/neb-host

    r41655 r42879  
    1616use Pod::Usage qw( pod2usage );
    1717
    18 my ($host, $state, $note,
    19     $db, $dbhost, $dbpass, $dbuser,
    20     $allocate, $available, $debug,$volume);
     18my ($host, $state, $xattr, $note, $db, $dbhost, $dbpass, $dbuser, $debug, $volume);
    2119
    2220# $db     = $ENV{'NEB_DB'} unless $db;
     
    2523# $dbpass = $ENV{'NEB_PASS'} unless $dbpass;
    2624
    27 # my $conffile = "/data/ippc64.1/ippitc/ippconfig/nebulous.config";
    2825# my $conffile = "/home/panstarrs/ipp/ippconfig/nebulous.config";
    2926my $conffile = "/home/panstarrs/ippps1/ippconfig/nebulous.config";
    3027
    3128if (-e $conffile) {
    32 #    printf STDERR "found config\n";
     29    # printf STDERR "found config\n";
    3330    open(IN,$conffile);
    3431    while(<IN>) {
    3532        chomp;
    36         if ($_ =~ /NEB_DB\s+/) {
    37             $db = (split /\s+/,$_)[2];
    38         }
    39         elsif ($_ =~ /NEB_DBHOST/) {
    40             $dbhost = (split /\s+/,$_)[2];
    41         }
    42         elsif ($_ =~ /NEB_USER/) {
    43             $dbuser = (split /\s+/,$_)[2];
    44         }
    45         elsif ($_ =~ /NEB_PASS/) {
    46             $dbpass = (split /\s+/,$_)[2];
    47         }
     33        if ($_ =~ /NEB_DB\s+/)  { $db     = (split /\s+/,$_)[2]; next; }
     34        if ($_ =~ /NEB_DBHOST/) { $dbhost = (split /\s+/,$_)[2]; next; }
     35        if ($_ =~ /NEB_USER/)   { $dbuser = (split /\s+/,$_)[2]; next; }
     36        if ($_ =~ /NEB_PASS/)   { $dbpass = (split /\s+/,$_)[2]; next; }
    4837    }
    4938    close(IN);
    50 #    print STDERR  "$db $dbhost $dbuser $dbpass\n";
     39    # print STDERR  "$db $dbhost $dbuser $dbpass\n";
    5140}
    5241
     
    5443    'host=s'         => \$host,
    5544    'state=s'        => \$state,
     45    'xattr=s'        => \$xattr,
    5646    'note=s'         => \$note,
    5747    'db=s'           => \$db,
     
    6050    'dbpass=s'       => \$dbpass,
    6151    'volume'         => \$volume,
    62    
    6352    ) || pod2usage( 2 );
    6453
    65 if (($#ARGV == 1)&&(!defined($host))&&(!defined($state))) {
     54# if --host and --state are not supplied, look for positional arguments
     55my $Narg = @ARGV;
     56
     57if (($Narg == 1) && (!defined($host)) && (!defined($state))) {
     58    # neb-host (host) : return volume into for specified host
     59    $host = shift(@ARGV);
     60}
     61if (($Narg == 2) && (!defined($host)) && (!defined($state))) {
    6662    $host = shift(@ARGV);
    6763    $state = shift(@ARGV);
     
    8379    );
    8480
    85 if ((defined($host))&&(defined($state))) {
    86     if ($state eq 'up') {
    87         $allocate = 1;
    88         $available = 1;
    89     }
    90     elsif ($state eq 'down') {
    91         $allocate = 0;
    92         $available = 0;
    93     }
    94     elsif ($state eq 'repair') {
    95         $allocate = 0;
    96         $available = 1;
    97     }
    98     else {
    99         die "Unknown state '$state'";
    100     }
     81if (defined($host) && defined($state)) {
     82
     83    # strip dangerous characters
     84    if (defined($note)) { $note =~ s/'/ /g; $note =~ s/;/ /g; $note =~ s/"/ /g; }
     85    if (defined($host)) { $host =~ s/'/ /g; $host =~ s/;/ /g; $host =~ s/"/ /g; }
     86
     87    if (($state ne 'up') && ($state ne 'down') && ($state ne 'repair')) { die "Unknown state '$state'"; }
    10188   
    102     my %constraint;
     89    # for 'up', we want to exclude set volumes with xattr = 3, and possible issue an equivalent of 'repair' for xattr = 3
     90    # for 'up' and 'repair', we should exclude volumes with xattr = 5
     91    # for 'down', all volumes are ok
     92   
     93    # up:
     94    # UPDATE volume SET (allocate = 1, available = 1, note = '$note') WHERE (name = '$host') AND (xattr != 3) AND (xattr != 5)
     95    # UPDATE volume SET (allocate = 0, available = 1, note = '$note') WHERE (name = '$host') AND (xattr = 3)
     96
     97    # repair:
     98    # UPDATE volume SET (allocate = 0, available = 1, note = '$note') WHERE (name = '$host') AND (xattr != 5)
     99
     100    # down:
     101    # UPDATE volume SET (allocate = 0, available = 0, note = '$note') WHERE (name = '$host')
     102
     103    # $volume is a boolean which means 'use' the supplied host as the 'name' field
     104    my $hostWhere;
    103105    if (defined($volume)) {
    104         $constraint{'v.name'} = $host;
    105     }
    106     else {
    107         $constraint{'v.host'} = $host;
    108     }
    109     my %set;
    110     $set{'v.allocate'} = $allocate if defined $allocate;
    111     $set{'v.available'} = $available if defined $available;
    112     $set{'v.note'} = $note if defined $note;
    113 
    114     eval {
    115         my ($q, @bind) = sql_interp("UPDATE volume AS v SET", \%set, "WHERE", \%constraint);
    116         warn "$q\n" if $debug;
    117         my $query = $dbh->prepare($q);
    118         $query->execute(@bind);
    119         $dbh->commit;
    120     };
    121     if ($@) {
    122         $dbh->rollback;
    123         die $@;
    124     }
    125     eval {
    126         my ($q, @bind) = sql_interp("UPDATE mountedvol AS v SET", \%set, "WHERE", \%constraint);
    127         warn "$q\n" if $debug;
    128         my $query = $dbh->prepare($q);
    129         $query->execute(@bind);
    130         $dbh->commit;
    131     };
    132     if ($@) {
    133         $dbh->rollback;
    134         die $@;
    135     }
     106        $hostWhere = "(name = '$host')";
     107    } else {
     108        $hostWhere = "(host = '$host')";
     109    }
     110
     111    my ($selections, $constraint);
     112
     113    if ($state eq 'up')     {
     114        $selections = "allocate = 1, available = 1";
     115        $constraint = "$hostWhere AND (xattr != 3) AND (xattr != 5)";
     116    }
     117
     118    if ($state eq 'repair')     {
     119        $selections = "allocate = 0, available = 1";
     120        $constraint = "$hostWhere AND (xattr != 5)";
     121    }
     122
     123    if ($state eq 'down')     {
     124        $selections = "allocate = 0, available = 0";
     125        $constraint = "$hostWhere";
     126    }
     127
     128    &modify_hosts ($selections, $constraint, $note);
     129
     130    ## for 'up', make a second call to set hosts with (xattr = 3) to 'repair':
     131    if ($state eq 'up')     {
     132        $selections = "allocate = 0, available = 1";
     133        $constraint = "$hostWhere AND (xattr = 3)";
     134
     135        &modify_hosts ($selections, $constraint, $note);
     136    }
     137    exit 0;
    136138}
    137139
    138140my $sql = Nebulous::Server::SQL->new();
    139141my ($q, @bind);
    140 $q = $sql->get_volumes . " ORDER BY v.host, v.name";
     142
     143if (defined($host)) {
     144    if (defined($xattr)) {
     145        $q = $sql->get_volumes . " WHERE v.host = '$host' AND v.xattr = $xattr ORDER BY v.host, v.name";
     146    } else {
     147        $q = $sql->get_volumes . " WHERE v.host = '$host' ORDER BY v.host, v.name";
     148    }
     149} else {
     150    if (defined($xattr)) {
     151        $q = $sql->get_volumes . " WHERE v.xattr = $xattr ORDER BY v.host, v.name";
     152    } else {
     153        $q = $sql->get_volumes . " ORDER BY v.host, v.name";
     154    }
     155}
     156
    141157warn "$q\n" if $debug;
    142158my $query = $dbh->prepare($q);
     
    152168        $$row{ 'note' } = '-';
    153169    }
    154 
    155170    printf($format, @$row{@columns});
    156171}
    157172
     173sub modify_hosts {
     174
     175    my $selections = $_[0];
     176    my $constraint = $_[1];
     177
     178    my $note = $_[2];
     179    # print "NOTE: $note\n";
     180
     181    if (defined $note) { $selections = $selections . ", note = '$note'"; }
     182
     183    {
     184        my $sqlLine = "UPDATE volume SET $selections WHERE $constraint";
     185        # print "SQL: $sqlLine\n";
     186        my $query = $dbh->prepare($sqlLine);
     187        $query->execute() or die "error from Nebulous database\n";
     188        $dbh->commit;
     189    }
     190   
     191    {
     192        my $sqlLine = "UPDATE mountedvol SET $selections WHERE $constraint";
     193        # print "SQL: $sqlLine\n";
     194        my $query = $dbh->prepare($sqlLine);
     195        $query->execute() or die "error from Nebulous database\n";
     196        $dbh->commit;
     197    }
     198}
     199
     200
    158201__END__
     202
     203
    159204
    160205=pod
     
    169214    [--note '<status note>']
    170215    [--host <nebulous host>] [--state <up|down|repair>]
     216    [--xattr N]
    171217    [--volume]
    172218    [--dbhost <database host>] [--db <database name>]
     
    180226given, the host and state are read directly from the command line.  If nothing
    181227is specified, prints out the table of currently mounted volumes in nebulous.
     228If the state is not specified, but a host is supplied, the table of values is restricted
     229to the specified host.
    182230
    183231=head1 OPTIONS
     
    200248    repair Allocate = 0, Available = 1
    201249
     250=item * --xattr <N>
     251
     252Limit the listed volumes to the specified xattr value
     253
    202254=item * --volume
    203255
Note: See TracChangeset for help on using the changeset viewer.