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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
3
4
#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
6
7
8
# All rights reserved.
#

9
10
11
12
13
14
15
16
17
#
# avail - prints a list of all available nodes
#

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

19
my $d = 0; #debug mode
Mac Newbold's avatar
Mac Newbold committed
20
21
22

if ($#ARGV >= 0 && $ARGV[0] =~ /^-?h/i ) { 
  die("Usage:\navail [help] [type[=<type>]] [OS[=<OS>]] [ver[=<OS_ver>]]\n".
23
24
      "              [[includevirt] or [virtonly[=<phys>]]] [rand] [limit=N]".
      "\n".
Mac Newbold's avatar
Mac Newbold committed
25
      "\n".
Mac Newbold's avatar
Mac Newbold committed
26
      "type \t= pc | shark\n"."OS \t= Linux | FreeBSD | NetBSD | ...\n".
27
      "ver \t= 6.2 | 3.4 | 4.0 | 5.2 | ...\n".#"deltas \t= anetd | ...\n".
28
      "includevirt: Include virtual nodes\n".
29
      "rand: Randomize result order\n".
30
      "limit: Limit count to N nodes\n".
Mac Newbold's avatar
Mac Newbold committed
31
      "A field name alone will show that field in the display of available\n".
32
      "nodes. A field name with '=' followed by a string will match nodes\n".
Mac Newbold's avatar
Mac Newbold committed
33
34
35
36
37
38
39
40
41
      "that have that string in that field.\n"
     );
}

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

my %args = ();
while ($#ARGV >= 0) {
  $_ = shift;
Mac Newbold's avatar
Mac Newbold committed
42
  if ( ! /=/ ) { $args{"\L$_"}='='; } else {
43
44
45
    @_ = split('=',$_);
    my ($opt,$val) = @_ if ($#_ >= 0);
    if (!defined($val)) { $val=""; }
46
    $args{"\L$opt"}=$val if (defined($opt));
Mac Newbold's avatar
Mac Newbold committed
47
48
49
50
51
52
53
54
55
  }
}
if ( $d ) {
  print "Parsed args to:\n";
  foreach my $opt (keys %args) {
    print "$opt:\t$args{$opt}\n";
  }
}

56
57
58
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";
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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' ";
    }
75
    $cond .= " ) ";
76
}
77

78
79
80
81
82
83
if ($args{"rand"}) {
    $order  = "rand()";
} else {
    $order = "priority";
}

84
85
86
87
88
89
if ($args{"limit"}) {
    $limit = "limit " . $args{"limit"};
} else {
    $limit = "";
}

Mac Newbold's avatar
Mac Newbold committed
90
91
my $os = 0;
my $delta = 0;
Mac Newbold's avatar
Mac Newbold committed
92

93
94
if (defined($args{"type"})) {
  # Make it a like, so if they type 'pc' they get pc850 and pc600...
95
96
  $cond .= " and (a.type='".$args{"type"}."' or nt.class='".$args{"type"}."')" 
    if $args{"type"} ne '=';
97
}
Mac Newbold's avatar
Mac Newbold committed
98
99
if (defined($args{"os"})) { 
  $os = 1;
Mac Newbold's avatar
Mac Newbold committed
100
  $cols .= ",d.OS";
Mac Newbold's avatar
Mac Newbold committed
101
  $cond .= " and d.OS='".$args{"os"}."'" if $args{"os"} ne '='; 
Mac Newbold's avatar
Mac Newbold committed
102
103
}
if (defined($args{"ver"})) { 
Mac Newbold's avatar
Mac Newbold committed
104
  $os = 1;
105
106
  $cols .= ",d.version";
  $cond .= " and d.version='".$args{"ver"}."'" if $args{"ver"} ne '='; 
Mac Newbold's avatar
Mac Newbold committed
107
}
108
109
110
111
112
113
# if (defined($args{"deltas"})) { 
#   $os = 1;
#   $delta=1;
#   $cols .= ",e.delta_id";
#   $cond .= " and e.delta_id='".$args{"deltas"}."'" if $args{"deltas"} ne '='; 
# }
Mac Newbold's avatar
Mac Newbold committed
114

Mac Newbold's avatar
Mac Newbold committed
115
116
if ($os) {
  $join .= " left join partitions as c on a.node_id=c.node_id";
117
  $join .= " left join os_info as d on c.osid=d.osid";
Mac Newbold's avatar
Mac Newbold committed
118
119
}

120
121
122
123
# if ($delta) {
#   $join .= " left join delta_inst as e on c.node_id=e.node_id ".
#     "and c.partition=e.partition";
# }
Mac Newbold's avatar
Mac Newbold committed
124
125


126
my $cmd = "select $cols from $join where $cond order by $order $limit";
Mac Newbold's avatar
Mac Newbold committed
127
128
129

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

130
my $result = DBQueryFatal($cmd);
Mac Newbold's avatar
Mac Newbold committed
131

132
print $result->as_string();
Mac Newbold's avatar
Mac Newbold committed
133

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