avail.in 3.96 KB
Newer Older
1
#!/usr/bin/perl -w
2

Leigh Stoller's avatar
Leigh Stoller committed
3
#
4
# Copyright (c) 2000-2017 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file 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 Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
Leigh Stoller's avatar
Leigh Stoller committed
24 25
#

26 27 28 29 30 31 32 33 34
#
# avail - prints a list of all available nodes
#

#
# Configure variables
#
use lib '@prefix@/lib';
use libdb;
Mac Newbold's avatar
Mac Newbold committed
35

36
my $d = 0; #debug mode
Mac Newbold's avatar
Mac Newbold committed
37 38 39

if ($#ARGV >= 0 && $ARGV[0] =~ /^-?h/i ) { 
  die("Usage:\navail [help] [type[=<type>]] [OS[=<OS>]] [ver[=<OS_ver>]]\n".
40
      "              [[includevirt] or [virtonly[=<phys>]]] [rand]\n".
41
      "              [limit=N] [aslist] [notprereserved]\n".
Mac Newbold's avatar
Mac Newbold committed
42
      "\n".
43
      "type \t= pc | shark\n"."OS \t= Linux | FreeBSD | NetBSD | ...\n".
44
      "ver \t= 6.2 | 3.4 | 4.0 | 5.2 | ...\n".#"deltas \t= anetd | ...\n".
45
      "includevirt: Include virtual nodes\n".
46
      "rand: Randomize result order\n".
47
      "limit: Limit count to N nodes\n".
Mac Newbold's avatar
Mac Newbold committed
48
      "A field name alone will show that field in the display of available\n".
49
      "nodes. A field name with '=' followed by a string will match nodes\n".
Mac Newbold's avatar
Mac Newbold committed
50 51 52 53 54 55 56 57 58
      "that have that string in that field.\n"
     );
}

print "Got ARGS = ",join(" ",@ARGV),"\n" if $d;

my %args = ();
while ($#ARGV >= 0) {
  $_ = shift;
59
  if ( ! /=/ ) { $args{"\L$_"}='='; } else {
60 61 62
    @_ = split('=',$_);
    my ($opt,$val) = @_ if ($#_ >= 0);
    if (!defined($val)) { $val=""; }
63
    $args{"\L$opt"}=$val if (defined($opt));
Mac Newbold's avatar
Mac Newbold committed
64 65 66 67 68 69 70 71 72
  }
}
if ( $d ) {
  print "Parsed args to:\n";
  foreach my $opt (keys %args) {
    print "$opt:\t$args{$opt}\n";
  }
}

73 74 75
my $join= "nodes as a left join reserved as b on a.node_id=b.node_id".
  " left join node_types as nt on a.type=nt.type";
my $cols= "a.node_id,a.type,nt.class";
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
my $cond= "b.node_id is null ";

if ($args{"virtonly"}) {
    $cond .= " and ( a.role='virtnode' ";

    if ($args{"virtonly"} ne '=') {
	$cond .= " and a.phys_nodeid='" . $args{"virtonly"} . "' ";
    }
    $cond .= " ) ";
}
else {
    $cond .= " and ( a.role='testnode' ";

    if ($args{"includevirt"}) {
	$cond .= " or a.role='virtnode' ";
    }
92
    $cond .= " ) ";
93
}
94

95 96 97 98 99 100
if ($args{"rand"}) {
    $order  = "rand()";
} else {
    $order = "priority";
}

101 102 103 104 105 106
if ($args{"limit"}) {
    $limit = "limit " . $args{"limit"};
} else {
    $limit = "";
}

107 108
my $os = 0;
my $delta = 0;
Mac Newbold's avatar
Mac Newbold committed
109

110 111
if (defined($args{"type"})) {
  # Make it a like, so if they type 'pc' they get pc850 and pc600...
112 113
  $cond .= " and (a.type='".$args{"type"}."' or nt.class='".$args{"type"}."')" 
    if $args{"type"} ne '=';
114
}
115 116
if (defined($args{"os"})) { 
  $os = 1;
Mac Newbold's avatar
Mac Newbold committed
117
  $cols .= ",d.OS";
118
  $cond .= " and d.OS='".$args{"os"}."'" if $args{"os"} ne '='; 
Mac Newbold's avatar
Mac Newbold committed
119 120
}
if (defined($args{"ver"})) { 
121
  $os = 1;
122 123
  $cols .= ",d.version";
  $cond .= " and d.version='".$args{"ver"}."'" if $args{"ver"} ne '='; 
Mac Newbold's avatar
Mac Newbold committed
124
}
125
if ($os) {
126
  $join .= " left join `partitions` as c on a.node_id=c.node_id";
127
  $join .= " left join os_info as d on c.osid=d.osid";
Mac Newbold's avatar
Mac Newbold committed
128
}
129 130 131 132
if (defined($args{"prereserve"})) {
    $pid = $args{"prereserve"};
    $cond .= " and a.reserved_pid='$pid' ";
}
133

134 135 136 137
if (defined($args{"notprereserved"})) {
    $cond .= " and a.reserved_pid is null";
}

138
my $cmd = "select $cols from $join where $cond order by $order $limit";
Mac Newbold's avatar
Mac Newbold committed
139 140 141

print "Sending cmd:\n$cmd\n" if $d;

142
my $result = DBQueryFatal($cmd);
Mac Newbold's avatar
Mac Newbold committed
143

144 145 146 147 148 149 150 151
if ($args{"aslist"}) {
    while (my ($node_id) = $result->fetchrow_array()) {
	print "$node_id\n";
    }
}
else { 
    print $result->as_string();
}
Mac Newbold's avatar
Mac Newbold committed
152

153
# Hacky. Batch daemon depends on this exit code.
154
exit($result->numrows);