handle_os.in 5.99 KB
Newer Older
1
2
3
4
5
6
7
8
#!/usr/bin/perl -w

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

# Yet to do:
# Any sort of delta support

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

push(@INC, $IRLIB);
16
17
require libir;

18
19
if ($#ARGV != 0) {
    print STDERR "Syntax: $0 ir_file\n";
20
21
    exit 1;
}
22
$irfile = $ARGV[0];
23
24
25
26

use DBI;

$driver = "mysql";
27
$dbh = DBI->connect("DBI:$driver:database=$DBNAME;host=localhost") 
28
29
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);
}

# Load the node types and grab the default OS from the database
# %nodetype is indexed by virtual node id and contains the type
42
# %ostype is indexed by type and contains the default OS
43
44
# $delayos is the delay os, currently assumed to be os of PC's
# XXX - Should grab this from the DB eventually
45
46
47
$delayos   = "FBSD40-STD";
$delaytype = "pc";

48
49
50
51
52
53
54
55
56
57
58
$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})) {
59
	$sth = $dbh->prepare("SELECT osid from node_types ".
60
			     "where machinetype=\"$type\"");
61
62
63
64
65
66
67
68
69
70
71
72
73
74
	$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
75
# os is indexed by physical node id and contains the osid
76
77
78
79
80
81
82
83
84
$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})) {
85
	# assume to be a delay node. 
86
	$os{$physical} = $delayos;
87
	$nodetype{$virtual} = $delaytype;
88
    } else {
Christopher Alfeld's avatar
Christopher Alfeld committed
89
	$os{$physical} = $ostype{$nodetype{$virtual}};
90
91
    }
}
92
93
94
95
96
97
98
99
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

#
# Read in the osid section. We overwrite the default for any node listed
# in the osid section. 
# 
if (! &ir_exists("/osid")) {
    print STDERR "IR incomplete: No /osid.\n";
    exit(1);
}
foreach (split("\n",&ir_get("/osid"))) {
    my ($node,$osid) = /^([^ ]+)(.*)$/;
    $osid =~ s/^ //;
    $os{$nodemap{$node}} = $osid;
}

#
# Now verify that the OSID is valid and appropriate for the node type.
#
# os_path is indexed by physical node id and contains the path.
#
foreach my $virt (keys(%nodemap)) {
    my $phys = $nodemap{$virt};
    my $osid = $os{$phys};

    $sth = $dbh->prepare("SELECT machinetype,path FROM os_info ".
			 "where osid = \"$osid\"");
    $rr = $sth->execute;
    if ($rr == 0) {
	# Bad OSID
	$sth->finish;
	push(@ERRORS,"Unknown OSID: $osid");
	next;
    }
    @result = $sth->fetchrow_array;
    $sth->finish;
    $valid_type = $result[0];
    
    if ($nodetype{$virt} ne $valid_type) {
	push(@ERRORS, "Node $virt is type $nodetype{$virt}, ".
	     "but $osid is for $valid_type.\n");
	next;
    }
    $os{$phys} = $osid;

    if (defined($result[1])) {
	$os_path{$phys} = $result[1];
    }
    else {
	$os_path{$phys} = "";
    }
142
143
144
145
146
147
148
149
150
151
}

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

152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#
# Now update the nodes table in DB.
#
foreach my $virt (keys(%nodemap)) {
    my $phys = $nodemap{$virt};
    my $osid = $os{$phys};
    my $path = $os_path{$phys};

    $sth = $dbh->prepare("UPDATE nodes set def_boot_osid = \"$osid\", ".
			 "def_boot_path = \"$path\" ".
			 "where node_id = \"$phys\"");
    $sth->execute;
    $sth->finish;
}

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

    @line = split(" ", $dirsandfiles);
    while (@line) {
	$dir = shift(@line);
	$tar = shift(@line);
	if ($tarfiles eq "") {
	    $tarfiles = "$dir $tar";
	}
	else {
	    $tarfiles = "$tarfiles:$dir $tar";
	}
    }
    $sth = $dbh->prepare("UPDATE nodes set tarballs = \"$tarfiles\" ".
			 "where node_id = \"$nodemap{$node}\"");
    $sth->execute;
    $sth->finish;
}
230
231
232
233
234
235
236
237
238
239
240
241
242
if (! &ir_exists("/deltas")) {
    print STDERR "IR incomplete: No /deltas.\n";
    exit(1);
}
foreach (split("\n",&ir_get("/deltas"))) {
    ($node,$deltas) = /^([^ ]+)(.*)$/;
    $deltas =~ s/^ //;
    $deltas =~ s/ /:/g;
    $sth = $dbh->prepare("UPDATE nodes set deltas = \"$deltas\" ".
			 "where node_id = \"$nodemap{$node}\"");
    $sth->execute;
    $sth->finish;
}
243

244
foreach (values(%nodemap)) {
245
246
247
    $node = $_;
    $sth = $dbh->prepare("UPDATE nodes set ready=0,startstatus = \"none\" ".
			 "where node_id = \"$node\"");
Christopher Alfeld's avatar
Added:    
Christopher Alfeld committed
248
249
250
251
    $sth->execute;
    $sth->finish;
}