handle_os 6.66 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/perl -w

# This handles all the OS TB commands:
# create-os
# set-dnard-os
# set-node-os

# Yet to do:
# Any sort of delta support

push(@INC,"/usr/testbed/lib/tbsetup/ir");
require libir;

if ($#ARGV != 1) {
    print STDERR "Syntax: $0 ir_file ns_file\n";
    exit 1;
}
($irfile,$nsfile) = @ARGV;

use DBI;

$driver = "mysql";
$dbh = DBI->connect("DBI:$driver:database=tbdb;host=localhost") 
    || die "Could not connect to DB.\n";

# List of ERROR messages
@ERRORS = ();

# read IR file
eval {&ir_read($irfile)};
if ($@) {
    print STDERR "Could not read $irfile ($@)\n";
    exit(1);
}

# open the ns file
open(NSFILE,$nsfile) || do {
    print STDERR "Could not open $nsfile.\n";
    exit(1);
};

# Load the node types and grab the default OS from the database
# %nodetype is indexed by virtual node id and contains the type
# %defaultos is indexed by type and contains the default OS
# $delayos is the delay os, currently assumed to be os of PC's
# XXX - Should grab this from the DB eventually
$delayos = "FBSD40-STD";
$raw = eval {&ir_get("/topology/nodes")};
if ($@) {
    print STDERR "Could not load /topology/nodes.\n";
    exit(1);
}
foreach (split("\n",$raw)) {
    @info = split;
    $node = $info[0];
    $type = $info[1];
    $nodetype{$node} = $type;
    if (! defined($ostype{$type})) {
	# Special case - shark shelf
	if ($type eq "shark-shelf") {
	    $subtype = "shark";
	} else {
	    $subtype = $type;
	}
	$sth = $dbh->prepare("SELECT image_id from node_types where type = \"$subtype\"");
	$rr = $sth->execute;
	if ($rr == 0) {
	    push(@ERRORS,"Invalid type $type");
	    $ostype{$type} = "INVALID";
	} else {
	    @result = $sth->fetchrow_array;
	    $ostype{$type} = $result[0];
	}
	$sth->finish;
    }
}

# Load the nodemap and set up an initial os table
# nodemap is indexed by virtual node id and contains the physical node id
# os is indexed by physical node id and contains the image id
$raw = eval {&ir_get("/virtual/nodes")};
if ($@) {
    print STDERR "Could not load /virtual/nodes.  Run assign first.\n";
    exit(1);
}
foreach (split("\n",$raw)) {
    ($virtual,$physical) = split;
    $nodemap{$virtual} = $physical;
    if (!defined($nodetype{$virtual})) {
	# assume to be a delay node
	$os{$physical} = $delayos;
    } else {
93
94
95
96
97
98
99
	if ($nodetype{$virtual} ne "shark-shelf") {
	    $os{$physical} = $ostype{$nodetype{$virtual}};
	} else {
	    for ($i=1;$i<=8;$i++) {
		$os{"$physical-$i"} = $ostype{"shark-shelf"};
	    }
	}
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
    }
}

# Now we parse the NS file
# %images is indexed by label and contains "$path $partition"
while (<NSFILE>) {
    /^\#TB/ || next;    
    chop;
    @line = split;
    $_ = $line[1];
  SWITCH: {
      /^create-os$/ && do {
	  # Should add some checking for partition
	  if ($#line != 4) {
	      push(@ERRORS,"Syntax: create-os label path partition");
	  } else {
	      ($label,$path,$partition) = @line[2..4];
	      $images{$label} = "$path $partition";
	  }
	  last SWITCH;
      };
      /^set-dnard-os$/ && do {
	  if ($#line != 4) {
	      push(@ERRORS,"Syntax: set-dnard-os shelf n os");
	  } else {
	      ($shelf,$n,$label) = @line[2..4];
	      if (! defined($nodemap{$shelf})) {
		  push(@ERRORS,"Unknown node $shelf\n");
		  last SWITCH;
	      }
	      if (! (($n =~ /[0-9]-[0-9]/) || ($n =~ /[0-9]/))) {
		  push(@ERRORS,"n must either be a number or a range start-end\n");
		  last SWITCH;
	      }
	      
	      # The label should either be in the images table or
	      # in the DB.
	      if (! defined($images{$label})) {
		  # Check DB
		  $sth = $dbh->prepare("SELECT image_id from disk_images where image_id = \"$label\"");
		  $rr = $sth->execute;
		  if ($rr == 0) {
		      # Bad OS
		      $sth->finish;
		      push(@ERRORS,"Unknown OS label $label");
		      last SWITCH;
		  }
		  $sth->finish;
	      }
	      $prefix = $nodemap{$shelf};
	      if ($n =~ /[0-9]-[0-9]/) {
		  ($start,$end) = split("-",$n);
		  foreach ($start..$end) {
		      $os{"$prefix-$_"}=$label;
		  }
	      } else {
		  $os{"$prefix-$n"}=$label;
	      }
	  }
	  last SWITCH;
      };
      /^set-node-os$/ && do {
	  if ($#line != 3) {
	      push(@ERRORS,"Syntax: set-node-os nodespec os");
	  } else {
	      ($nodespec,$label) = @line[2..3];
	      # Do initial checking on label
	      # We also get the valid type for this image and store it
	      # in valid_type;
	      if (! defined($images{$label})) {
		  # Check DB
		  $sth = $dbh->prepare("SELECT type from disk_images where image_id = \"$label\"");
		  $rr = $sth->execute;
		  if ($rr == 0) {
		      # Bad OS
		      $sth->finish;
		      push(@ERRORS,"Unknown OS label $label");
		      last SWITCH;
		  } else {
		      @result = $sth->fetchrow_array;
		      $sth->finish;
		      $valid_type = $result[0];
		  }
	      } else {
		  # Custom image - don't know type info
		  $valid_type = "";
	      }
	      foreach (keys(%nodemap)) {
		  if (eval("/^$nodespec" . '$' . "/")) {
		      # Special case, make sure we aren't talking about sharks
		      if ($nodetype{$_} eq "shark-shelf") {
			  push(@ERRORS,"Use set-dnard-os for setting os of shark shelf $_\n");
		      } elsif (($valid_type ne "") &&
			  ($nodetype{$_} ne $valid_type)) {
			  push(@ERRORS,"$_ is of type $nodetype($_), OS $label is of type $valid_type");
			  $os{$nodemap{$_}} = "INVALID";
		      } else {
			  $os{$nodemap{$_}} = $label;
		      }
		  }
	      }
	  }
	      
	  last SWITCH;
      };
      /^set-dnard-deltas$/ && do {
	  # PLACE HOLDER
	  last SWITCH;
      };
      /^set-node-deltas$/ && do {
	  # PLACE HOLDER
	  last SWITCH;
      };
  }
}

# Check if we're good so far
if ($#ERRORS != -1) {
    foreach (@ERRORS) {
	print STDERR $_ . "\n";
    }
    exit(1);
}

# Let's do some output
# We allow running this on an IR file that has already been handle_os'ed
# it simply overwrites in that case.
if (! &ir_exists("/os")) {
    # Fresh file, just append
    open(IRFILE,">> $irfile") || do {
	print STDERR "Could not open $irfile for appending.\n";
	exit(1);
    };
    print IRFILE "START os\n";
    print IRFILE "START images\n";
    foreach (sort(keys(%images))) {
	print IRFILE "$_ $images{$_}\n";
    }
    print IRFILE "END images\n";
    print IRFILE "START nodes\n";
    foreach (sort(keys(%os))) {
	print IRFILE "$_ $os{$_}\n";
    }
    print IRFILE "END nodes\n";
    print IRFILE "END os\n";
    close(IRFILE);
} else {
    # Here we use libir to overwrite the sections.
    $images_raw = "";
    foreach (sort(keys(%images))) {
	$images_raw .= "$_ $images{$_}\n";
    }
    &ir_set("/os/images",$images_raw);
    $nodes_raw = "";
    foreach (sort(keys(%os))) {
	$nodes_raw .= "$_ $os{$_}\n";
    }
    &ir_set("/os/nodes",$nodes_raw);
    eval {&ir_write($irfile)};
    if ($@) {
	print STDERR "Could not write $irfile ($@)\n";
	exit(1);
    }
}