ns2genilib.in 7.23 KB
Newer Older
1 2 3
#!/usr/bin/perl -w

#
4
# Copyright (c) 2000-2017 University of Utah and the Flux Group.
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 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
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
use strict;
use English;
use Getopt::Std;
use File::Temp qw(tempfile :mktemp :POSIX );
use POSIX qw(:signal_h);
use POSIX ":sys_wait_h";
use File::stat;

#
# Convert an NS file into rspec using geni-lib and some lxml parsing.
#
sub usage()
{
    print STDERR "Usage: ns2genilib [-a | -p pid] [-o filename] nsfile\n";
    print STDERR "       ns2genilib [-a | -p pid] [-o filename] -e pid,eid\n";
    print STDERR "Options:\n";
    print STDERR " -p pid   : Run in context of project (permission checks)\n";
    print STDERR " -e eid   : Get NS file from Classic experiment\n";
    print STDERR " -a       : Run in anon mode (no project checks)\n";
    print STDERR"  -o file  : Specify output file\n";
    exit(-1);
}
my $optlist    = "dao:p:e:";
my $debug      = 0;
my $anonmode   = 0;
my $ofile;
my $pid;
my $experiment;

#
# Configure variables
#
my $TB         = "@prefix@";
my $TBOPS      = "@TBOPSEMAIL@";
my $CONTROL    = "@USERNODE@";
my $PARSENS    = "$TB/libexec/parse-ns";
my $NSGENILIB  = "$TB/bin/nsgenilib.py";
my $RS2GENILIB = "$TB/bin/rspec2genilib";

# Locals
my $nsfile;

# Protos
sub fatal($);

#
# Turn off line buffering on output
#
$| = 1;

#
# Untaint the path
# 
$ENV{'PATH'} = "$TB/bin:$TB/sbin:/bin:/usr/bin:/sbin:/usr/sbin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

if ($UID == 0) {
    die("Please do not run this as root!");
}

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use Experiment;
use Project;
use User;

#
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"a"})) {
    $anonmode = 1;
}
if (defined($options{"o"})) {
    $ofile = $options{"o"};
}
if (defined($options{"p"})) {
    $pid = $options{"p"};
}
if (defined($options{"e"})) {
    $experiment = Experiment->Lookup($options{"e"});
    if (!defined($experiment)) {
	fatal("No such experiment");
    }
}
else {
    if (@ARGV != 1) {
	usage();
    }
    $nsfile = $ARGV[0];

    #
    # Must taint check!
    #
    if ($nsfile =~ /^([-\w\/\.]+)$/) {
	$nsfile = $1;
    }
    else {
	die("Bad data in argument: $nsfile.");
    }
}

my $this_user = User->ThisUser();
if (! defined($this_user)) {
140
    fatal("You ($UID) do not exist!");
141 142 143 144
}
if (defined($pid)) {
    my $project = Project->Lookup($pid);
    if (!defined($project)) {
145
	fatal("No such project");
146 147
    }
    if (! ($this_user->IsAdmin() || defined($project->LookupUser($this_user)))) {
148
	fatal("Not a member of project $pid");
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
    }
}

my $tmpdir   = mktemp("/tmp/ns2genilib.XXXXXX");
my $irfile   = "irfile.xml";
my $xmlfile  = "rspec.xml";
my $pyfile   = "genilib.py";

if (! mkdir("$tmpdir", 0755)) {
    fatal("Could not create temp directory");
}
if (! chdir($tmpdir)) {
    die("Could not chdir to $tmpdir: $!\n");
}

if (defined($experiment)) {
    $nsfile = "nsfile.ns";
    my $ns;
    if ($experiment->GetNSFile(\$ns) || !defined($ns)) {
	fatal("No NS file for experiment");
    }
    #
    # Blech. We want to add an NS statement that sets the description
    # since it is not metadata in a profile, it is part of the source.
    #
    my @lines = split("\n", $ns);
    $ns = "";
    foreach my $line (@lines) {
	$ns .= "$line\n";

	if ($line =~ /^set\s+(.*)\s+\[new Simulator/) {
180 181 182 183 184
	    my $text = $experiment->description();
	    my $nsn  = $1;
	    $text =~ s/\"/\\"/g;
	    
	    $ns .= "\$" . $nsn . " description \"$text\"\n";
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
	}
    }
    open(NS, ">$nsfile")
	or fatal("Could not open $nsfile for writing");
    print NS $ns;
    close(NS);
}

#
# When coming in from the web interface, we run in anonmode to parse without
# a project (since the portal sets the project later). Instead, we will rerun
# the parser again when the user creates the experiment sicne then we know the
# project context.
#
my $parseopts = ($anonmode ? "-n -p -a" : "-n -r $pid $pid");

open ERR, "$PARSENS -c $parseopts $nsfile 2>&1 > $irfile |";

#
# Now read in the results from stderr.
#
my $errs = "";
while (<ERR>) {
    $errs .= $_;
}
close(ERR);
if ($?) {
    my $tmp = $?;
    print STDERR $errs;
    system("/bin/rm -rf $tmpdir");
    # This error is shown to the user.
    if (defined($ofile)) {
	if (open(OFILE, "> $ofile")) {
	    print OFILE $errs;
	    close(OFILE);
	}
    }
    exit($tmp >> 8);
}

#
# Ick, first line is a tag.
#
open(IR, $irfile)
    or fatal("Could not open $irfile");
open(XML, ">$xmlfile")
    or fatal("Could not open $xmlfile");
while (<IR>) {
    next
	if ($_ =~ /^#/);
    print XML $_;
}
close(IR);
close(XML);
system("/bin/mv $xmlfile $irfile") == 0 or
    fatal("Could not rename irfile");

if ($debug) {
    system("/bin/cat $irfile");
}

#
# The next thing is to feed the IR output of the NS parser into
# our geni-lib converter which produces the rspec. 
#
250 251 252
my $opts = ($anonmode ? "" : "-p $pid");

open ERR, "$NSGENILIB $opts $irfile 2>&1 > $xmlfile |";
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304

$errs = "";
while (<ERR>) {
    $errs .= $_;
}
close(ERR);
if ($?) {
    my $tmp = $?;
    print STDERR $errs;
    system("/bin/cat $xmlfile")
	if (-s $xmlfile);
    system("/bin/rm -rf $tmpdir");
    # This error is shown to the user.
    if (defined($ofile)) {
	if (open(OFILE, "> $ofile")) {
	    print OFILE $errs;
	    close(OFILE);
	}
    }
    exit($tmp >> 8);
}

if ($debug) {
    system("/bin/cat $xmlfile");
}

#
# Now feed the rspec into the rspec2genilib converter. 
#
open ERR, "$RS2GENILIB $xmlfile 2>&1 > $pyfile |";

$errs = "";
while (<ERR>) {
    $errs .= $_;
}
close(ERR);
if ($?) {
    my $tmp = $?;
    print STDERR $errs;
    system("/bin/cat $pyfile")
	if (-s $pyfile);
    system("/bin/rm -rf $tmpdir");
    # This error is shown to the user.
    if (defined($ofile)) {
	if (open(OFILE, "> $ofile")) {
	    print OFILE $errs;
	    close(OFILE);
	}
    }
    exit($tmp >> 8);
}

305 306 307 308 309 310 311 312 313
#
# Append original NS file.
#
open(PY, ">>$pyfile") or
    fatal("Could not open $pyfile for appending: $!");
open(NS, "$nsfile") or
    fatal("Could not open $nsfile: $!");
print PY "\n";
print PY "#\n";
314
print PY "# Here is the original NS file from the Emulab experiment.\n";
315 316 317 318 319 320 321 322
print PY "#\n";
while (<NS>) {
    print PY "# $_";
}
print PY "\n";
close(NS);
close(PY);

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
if (defined($ofile)) {
    system("cat $pyfile > $ofile");
}
else {
    system("cat $pyfile");
}
system("/bin/rm -rf $tmpdir");
exit(0);

sub fatal($) {
    my ($mesg) = $_[0];

    print STDERR "*** $0:\n".
	         "    $mesg\n";

    system("/bin/rm -rf $tmpdir")
	if (defined($tmpdir));
    exit(-1);
}