tbprerun.tcl 5.19 KB
Newer Older
1
2
#!/usr/local/bin/tclsh

Christopher Alfeld's avatar
Christopher Alfeld committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
proc outs {args} {
    global logFp
    if {[llength $args] == 1} {
	set out stdout
	set s [lindex $args 0]
    } else {
	set out [lindex $args 0]
	set s [lindex $args 1]
    }
    
    puts $out $s
    puts $logFp $s
}

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
set wait {15 30 60 600 600}
proc lock {} {
    global wait lockfile
    outs "Locking the world!"
    
    set waiti 0
    while {[catch "open $lockfile {WRONLY CREAT EXCL}" lockfp]} {
	if {$wait == [llength $waiti]} {
	    outs stderr "Giving up on locking.  If no other tbprerun is running then remove $lockfile manually."
	    exit 1
	}
	set delay [lindex $wait $waiti]
	incr waiti
	outs "World is locked.  Waiting $delay seconds"
	after [expr $delay * 1000]
    }
    close $lockfp
}
35
36
37
38
39
40
41
42
43
44
45
46
proc unlock {} {
    global lockfile
    outs "Unlocking the world!"
    if {! [file exists $lockfile]} {
	outs stderr "Error: World already unlocked - DB may be corrupted."
    } else {
	if {[catch "file delete -force $lockfile" err]} {
	    outs stderr "Error unlocking world ($err)"
	}
    }
}

47
48
49
50
51
52
53
54
55
56
proc cleanup {} {
    global pid eid nfree logFp
    outs "Cleaning up and freeing resources!"
    
    if {[catch "exec $nfree $pid $eid >@ $logFp 2>@ $logFp" err]} {
        outs stderr "Error freeing resources. ($err)"
	exit 1
    }
}

57
58
59
60
61
### Bootstrapping code.  The whole purpose of this is to find the
# directory containing the script.
set file [info script]
while {![catch "file readlink $file" newfile]} {
    set file $newfile
62
}
63
64
65
set scriptdir [file dirname $file]
if {$scriptdir == "."} {set scriptdir [pwd]}
###
66
67
68
if {[file exists $scriptdir/ns2ir]} {
    # development tree
    set updir [file dirname $scriptdir]
69
    set sqldir $updir/lib
70
71
72
73
} else {
    # install tree
    set updir [file dirname $scriptdir]/lib
    set scriptdir [file dirname $scriptdir]/lib/tbsetup
74
    set sqldir $updir
75
}
76

77
78
load $sqldir/sql.so

Christopher Alfeld's avatar
Christopher Alfeld committed
79
set lockfile "/usr/testbed/locks/tblock"
80
set ns2ir "$scriptdir/ns2ir/parse.tcl"
81
set postparse "$scriptdir/ns2ir/postparse"
82
83
set assign "$scriptdir/ir/assign.tcl"
set handle_ip "$scriptdir/ir/handle_ip.tcl"
84
set handle_os "$scriptdir/ir/handle_os"
85
86
set avail "$updir/db/avail"
set ptopgen "$updir/db/ptopgen"
87
set ptopfile "/tmp/testbed[pid].ptop"
88
set reserve "$updir/db/nalloc"
89
set nfree "$updir/db/nfree"
90
set libir "$scriptdir/ir/libir.tcl"
91
set maxtries 5
92
93
94

source $libir
namespace import TB_LIBIR::ir
95

Christopher Alfeld's avatar
Christopher Alfeld committed
96
97
if {$argc != 3} {
    puts stderr "Syntax: $argv0 <pid> <eid> <ns-file>"
98
99
100
    exit 1
}

101
102
103
set DB [sql connect]
sql selectdb $DB tbdb

Christopher Alfeld's avatar
Christopher Alfeld committed
104
set nsFile [lindex $argv 2]
105
set t [split $nsFile .]
Christopher Alfeld's avatar
Christopher Alfeld committed
106
107
108
set pid [lindex $argv 0]
set eid [lindex $argv 1]
set id "$pid-$eid"
109
set prefix [join [lrange $t 0 [expr [llength $t] - 2]] .]
110
111
112
113
114
115
116
117
set irFile "$prefix.ir"
set logFile "$prefix.log"

if {[catch "open $logFile w" logFp]} {
    puts stderr "Could not open $logFile for writing."
    exit 1
}

Christopher Alfeld's avatar
Christopher Alfeld committed
118
119
120
121
122
outs "Input: $nsFile"
outs "Output: $irFile"
outs "Log: $logFile"
outs ""
outs "Beginning Testbed pre run for $nsFile. [clock format [clock seconds]]"
123
124

if {! [file exists $nsFile]} {
Christopher Alfeld's avatar
Christopher Alfeld committed
125
    outs stderr "$nsFile does not exist"
126
127
128
    exit 1
}

Christopher Alfeld's avatar
Christopher Alfeld committed
129
outs "Parsing ns input."
130
if {[catch "exec $ns2ir $id $nsFile $irFile >@ $logFp 2>@ $logFp" err]} {
Christopher Alfeld's avatar
Christopher Alfeld committed
131
    outs stderr "Error parsing ns input. ($err)"
132
133
134
    exit 1
}
if {! [file exists $irFile]} {
Christopher Alfeld's avatar
Christopher Alfeld committed
135
    outs stderr "$irFile not generated.  Make sure you have a 'run' command in your ns file."
136
137
138
    exit 1
}

139
140
141
142
143
144
outs "Post Parsing ns input."
if {[catch "exec $postparse $nsFile $irFile >@ $logFp 2>@ $logFp" err]} {
    outs stderr "Error post parsing ns input. ($err)"
    exit 1
}

145
146
147
148
149
150
151
152
153
set done 0
set tries 0
while {$done == 0} {
    incr tries
    lock
    outs "Determining available resources."
    if {[catch "exec $avail | $ptopgen > $ptopfile 2>@ $logFp" err]} {
	outs stderr "Error determining available resources. ($err)"
	unlock
154
155
156
	exit 1
    }
    unlock
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
    
    outs "Allocating resources - This may take a while."
    if {[catch "exec $assign $irFile $ptopfile >@ $logFp 2>@ $logFp" err]} {
	outs stderr "Error allocating resources.  See $logFile and assign.log for more info."
	unlock
	exit 1
    }
    
    ir read $irFile
    set nodemap [ir get /virtual/nodes]
    set machines {}
    foreach pair $nodemap {
	lappend machines [lindex $pair 1]
    }
    
    lock
    outs "Checking if resources still available."
    set done 1
    foreach machine $machines {
	sql query $DB "select node_id from reserved where node_id=\"$machine\""
	if {[sql fetchrow $DB] != {}} {
	    set done 0
	    sql endquery $DB
	    break
	}
	sql endquery $DB
    }
    
    if {$done == 1} {
	outs "Reserving resources."
	if {[catch "exec $reserve $pid $eid $machines >@ $logFp 2>@ $logFp" err]} {
	    outs stderr "Error reserving resources. ($err)"
	    unlock
	    exit 1
	}
    } else {
	if {$tries >= $maxtries} {
	    outs "Resources unavailable.  Giving up!"
	    exit 1
	} else {
	    outs "Resources unavailable.  Retrying."
	}
    }
200
    unlock
201
}
202

203
204
205
outs "Allocating IP addresses."
if {[catch "exec $handle_ip $irFile $nsFile >@ $logFp 2>@ $logFp" err]} {
    outs stderr "Error allocating IP addresses. ($err)"
206
    cleanup
207
208
    exit 1
}
209

Christopher Alfeld's avatar
Christopher Alfeld committed
210
211
212
outs "Parsing OS information."
if {[catch "exec $handle_os $irFile $nsFile >@ $logFp 2>@ $logFp" err]} {
    outs stderr "Error parsing OS information. ($err)"
213
    cleanup
Christopher Alfeld's avatar
Christopher Alfeld committed
214
215
    exit 1
}
216

Christopher Alfeld's avatar
Christopher Alfeld committed
217
outs "Setup finished - $irFile generated."
218
219