handle_os.in 6.42 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#!/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

11
12
13
14
15
16
17
#
# Configure variables
#
my $IRLIB  = "@prefix@/lib/ir";
my $DBNAME = "@TBDBNAME@";

push(@INC, $IRLIB);
18
19
20
require libir;

if ($#ARGV != 1) {
21
    print STDERR "Syntax: $0 ir_file tbcmdfile\n";
22
23
    exit 1;
}
24
($irfile,$tbcmdfile) = @ARGV;
25
26
27
28

use DBI;

$driver = "mysql";
29
$dbh = DBI->connect("DBI:$driver:database=$DBNAME;host=localhost") 
30
31
32
33
34
35
36
37
38
39
40
41
    || 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);
}

42
43
44
# open the tbcmd file
open(TBCMD,$tbcmdfile) || do {
    print STDERR "Could not open $tbcmdfile.\n";
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    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})) {
Christopher Alfeld's avatar
Christopher Alfeld committed
65
	$sth = $dbh->prepare("SELECT image_id from node_types where type = \"$type\"");
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
	$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 {
Christopher Alfeld's avatar
Christopher Alfeld committed
93
	$os{$physical} = $ostype{$nodetype{$virtual}};
94
95
    }
}
96
# Now we parse the tbcmd file
97
# %images is indexed by label and contains "$path $partition"
98
while (<TBCMD>) {
99
100
    chop;
    @line = split;
101
    $_ = $line[0];
102
  SWITCH: {
103
      /^tb-create-os$/ && do {
104
	  # Should add some checking for partition
105
106
	  if ($#line != 3) {
	      push(@ERRORS,"Syntax: tb-create-os label path partition");
107
	  } else {
108
	      ($label,$path,$partition) = @line[1..3];
109
110
111
112
	      $images{$label} = "$path $partition";
	  }
	  last SWITCH;
      };
113
114
115
      /^tb-set-node-os$/ && do {
	  if ($#line != 2) {
	      push(@ERRORS,"Syntax: tb-set-node-os nodespec os");
116
	  } else {
117
	      ($nodespec,$label) = @line[1..2];
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
	      # 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" . '$' . "/")) {
Christopher Alfeld's avatar
Christopher Alfeld committed
141
		      if (($valid_type ne "") &&
142
143
144
145
146
147
148
149
150
151
152
153
			  ($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;
      };
154
      /^tb-set-node-deltas$/ && do {
155
156
157
158
159
	  # PLACE HOLDER
	  last SWITCH;
      };
  }
}
160
161
 
close(TBCMD);
162
163
164
165
166
167
168
169
170

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

Christopher Alfeld's avatar
Added:    
Christopher Alfeld committed
171
172
173
174
175
176
177
178
## Handle cmdline/startup/etc.
if (! &ir_exists("/cmdline")) {
    print STDERR "IR incomplete: No /cmdline.\n";
    exit(1);
}
foreach (split("\n",&ir_get("/cmdline"))) {
    ($node,$cmdline) = /^([^ ]+)(.*)$/;
    $cmdline =~ s/^ //;
179
180
    $sth = $dbh->prepare("UPDATE nodes set def_boot_cmd_line = \"$cmdline\" ".
			 "where node_id = \"$nodemap{$node}\"");
Christopher Alfeld's avatar
Added:    
Christopher Alfeld committed
181
182
183
184
185
186
187
188
189
190
    $sth->execute;
    $sth->finish;
}
if (! &ir_exists("/startup")) {
    print STDERR "IR incomplete: No /startup.\n";
    exit(1);
}
foreach (split("\n",&ir_get("/startup"))) {
    ($node,$cmdline) = /^([^ ]+)(.*)$/;
    $cmdline =~ s/^ //;
191
192
    $sth = $dbh->prepare("UPDATE nodes set startupcmd = \"$cmdline\" ".
			 "where node_id = \"$nodemap{$node}\"");
Christopher Alfeld's avatar
Added:    
Christopher Alfeld committed
193
194
195
196
197
198
199
200
201
202
203
    $sth->execute;
    $sth->finish;
}
if (! &ir_exists("/rpms")) {
    print STDERR "IR incomplete: No /rpms.\n";
    exit(1);
}
foreach (split("\n",&ir_get("/rpms"))) {
    ($node,$rpms) = /^([^ ]+)(.*)$/;
    $rpms =~ s/^ //;
    $rpms =~ s/ /:/g;
204
205
206
207
208
209
    $sth = $dbh->prepare("UPDATE nodes set rpms = \"$rpms\" ".
			 "where node_id = \"$nodemap{$node}\"");
    $sth->execute;
    $sth->finish;
}

210
foreach (values(%nodemap)) {
211
212
213
    $node = $_;
    $sth = $dbh->prepare("UPDATE nodes set ready=0,startstatus = \"none\" ".
			 "where node_id = \"$node\"");
Christopher Alfeld's avatar
Added:    
Christopher Alfeld committed
214
215
216
217
    $sth->execute;
    $sth->finish;
}

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
# 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);
    }
}