forked from kohler/hotcrp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstopvm
132 lines (119 loc) · 2.91 KB
/
stopvm
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
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
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
$|=1;
sub randompass()
{
my @alphanumeric = ('a'..'z', 'A'..'Z', 0..9,'!','_','-');
my @numeric = (0..9);
my $randpassword = '';
while ( length($randpassword) < 8 ) {
$randpassword = $randpassword . join '', map $alphanumeric[rand @alphanumeric], 0..(rand @numeric);
}
$randpassword .= '#';
return $randpassword;
}
# We will just rebuild experiment name and
# delete the experiment
my $SSH = "ssh -o StrictHostKeyChecking=no";
# Take paper ID, type of VM to stop
# and build a name
# like stopvm 2 small
my $usage="$0 paper-ID vm-type\n";
my %opt = ();
if ($#ARGV < 1)
{
print $usage;
print "DONE";
exit 1;
}
# Read info from conf/options.php
# to get orgName, dbUser, etc.
my $fh = new IO::File("conf/options.php");
while (<$fh>)
{
if ($_ =~ /\$Opt/ && $_ =~ /\=/)
{
my @arr = split /[\[\]\"\=\s+\;]/, $_;
if ($#arr > 3)
{
my $intake = 0;
for my $a (@arr)
{
if ($a !~ /[\[\]\"\=\s+]/ && $a =~ /[a-zA-Z0-9]/)
{
if ($a =~ /^\$Opt/)
{
$intake = 1;
next;
}
if ($intake =~ /^[0-9]$/ && $intake == 1)
{
$opt{$a} = 0;
$intake = $a;
}
else
{
$opt{$intake} = $a;
last;
}
}
}
}
}
}
close($fh);
my $dsn = "DBI:mysql:database=$opt{'dbName'}";
my $dbh = DBI->connect($dsn, $opt{'dbUser'}, $opt{'dbPassword'});
my $sth;
my %tocreate=();
my $paperID = $ARGV[0];
my $vmtype = $ARGV[1];
# Set server
my $path=$opt{'webpath'} . "/secret";
$ENV{HOME}=$path;
my $cmd="mrg config set server grpc.sphere-testbed.net";
system($cmd);
# Call stopexp
my $logincmd = "mrg login " . $opt{'clusterUser'} . " -p \"" . $opt{'clusterPass'} . "\"";
my $proj = $opt{'clusterOrg'} . "p" . $paperID;
my $vmid = $vmtype . "." . $proj;
# Log in as conf admin and as www-data user
$cmd = $logincmd;
my $result=system($cmd);
$cmd = "mrg delete experiment $vmtype.$proj";
my $result = system($cmd);
if ($result != 0)
{
print "Experiment could not be stopped";
print "DONE";
exit 1;
}
# Also delete XDC
$cmd = "mrg delete xdc xdc.$proj";
$result = system($cmd);
if ($result != 0)
{
print "XDC could not be deleted";
print "DONE";
exit 1;
}
# Remove user access
$sth = $dbh->prepare(
'DELETE FROM VMs where vmid = ?')
or die "prepare statement failed: $dbh->errstr()";
$sth->execute($vmid) or die "execution failed: $dbh->errstr()";
$sth = $dbh->prepare(
'DELETE FROM VMaccess where vmid = ?')
or die "prepare statement failed: $dbh->errstr()";
$sth->execute($vmid) or die "execution failed: $dbh->errstr()";
$sth = $dbh->prepare(
'DELETE FROM VMnodes where vmid = ?')
or die "prepare statement failed: $dbh->errstr()";
$sth->execute($vmid) or die "execution failed: $dbh->errstr()";
$sth = $dbh->prepare(
'DELETE FROM Ports where vmid = ?')
or die "prepare statement failed: $dbh->errstr()";
$sth->execute($vmid) or die "execution failed: $dbh->errstr()";
print "DONE";