ns2rspec.in 5.11 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
#!/usr/bin/perl -w

#
# Copyright (c) 2000-2016 University of Utah and the Flux Group.
# 
# {{{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()
{
38 39 40 41 42
    print STDERR "Usage: ns2rspec [-a | -p pid] [-o filename] nsfile\n";
    print STDERR "Options:\n";
    print STDERR " -p pid   : Run in context of project (permission checks)\n";
    print STDERR " -a       : Run in anon mode (no project checks)\n";
    print STDERR"  -o file  : Specify output file\n";
43 44
    exit(-1);
}
45
my $optlist    = "dao:p:";
46
my $debug      = 0;
47
my $anonmode   = 0;
48
my $ofile;
49
my $pid;
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

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

# 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;
87
use Project;
88 89 90 91 92 93 94 95 96 97 98 99 100
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;
}
101 102 103
if (defined($options{"a"})) {
    $anonmode = 1;
}
104 105 106
if (defined($options{"o"})) {
    $ofile = $options{"o"};
}
107 108 109
if (defined($options{"p"})) {
    $pid = $options{"p"};
}
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
if (@ARGV != 1) {
    usage();
}
$nsfile = $ARGV[0];

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

125 126 127 128 129 130 131 132 133 134 135 136 137 138
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    Fatal("You ($UID) do not exist!");
}
if (defined($pid)) {
    my $project = Project->Lookup($pid);
    if (!defined($project)) {
	Fatal("No such project");
    }
    if (! ($this_user->IsAdmin() || defined($project->LookupUser($this_user)))) {
	Fatal("No a member of project $pid");
    }
}

139 140 141 142 143 144 145 146 147 148 149 150
my $tmpdir   = mktemp("/tmp/ns2rspec.XXXXXX");
my $irfile   = "irfile.xml";
my $xmlfile  = "rspec.xml";

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

#
151 152 153 154
# 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.
155
#
156 157 158
my $parseopts = ($anonmode ? "-n -p -a" : "-n -r $pid $pid");

open ERR, "$PARSENS -c $parseopts $nsfile 2>&1 > $irfile |";
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

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

199 200 201 202
if ($debug) {
    system("/bin/cat $irfile");
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216
#
# The next thing is to feed the IR output of the NS parser into
# our geni-lib converter. 
#
open ERR, "$NSGENILIB $irfile 2>&1 > $xmlfile |";

$errs = "";
while (<ERR>) {
    $errs .= $_;
}
close(ERR);
if ($?) {
    my $tmp = $?;
    print STDERR $errs;
217 218
    system("/bin/cat $xmlfile")
	if (-s $xmlfile);
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
    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 (defined($ofile)) {
    system("cat $xmlfile > $ofile");
}
else {
    system("cat $xmlfile");
}
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);
}