rvd_front.pl 6.13 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
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
140
141
142
143
144
145
146
147
148
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
180
181
182
183
184
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env perl
use warnings;
use strict;

use Data::Dumper;
use DBIx::Connector;
use Mojolicious::Lite;

use lib 'lib';

use Ravada::Auth::LDAP;

our $CON = DBIx::Connector->new("DBI:mysql:ravada"
                        ,undef,undef,{RaiseError => 1
                        , PrintError=> 0 }) or die "I can't connect";

our $TIMEOUT = 120;

any '/' => sub {
  my $c = shift;
    my $login = $c->param('login');
    my $password = $c->param('password');
    my $id_base = $c->param('id_base');

    my @error =();
    if ($c->param('submit')) {
        push @error,("Empty login name")  if !length $login;
        push @error,("Empty password")  if !length $password;
    }

    if ( $login && $password ) {
        if (Ravada::Auth::LDAP::login($login, $password)) {
            return show_link($c, $id_base, $login);
        }
        push @error,("Access denied");
    }
    $c->render(
                    template => 'bootstrap/start' 
                    ,id_base => $id_base
                      ,login => $login 
                      ,error => \@error
                       ,base => list_bases());
};

get '/ip' => sub {
    my $c = shift;
    $c->render(template => 'bases', base => list_bases());
};

get '/ip/*' => sub {
    my $c = shift;
    my ($base_name) = $c->req->url->to_abs =~ m{/ip/(.*)};
    my $ip = $c->tx->remote_address();
    show_link($c,base_id($base_name),$ip);
};

#######################################################

sub base_id {
    my $name = shift;

    my $sth = $CON->dbh->prepare("SELECT id FROM bases WHERE name=?");
    $sth->execute($name);
    my ($id) =$sth->fetchrow;
    die "CRITICAL: Unknown base $name" if !defined $id;
    return $id;
}

sub find_uri {
    my $host = shift;
    my $url = `virsh domdisplay $host`;
    warn $url;
    chomp $url;
    return $url;
}

sub provisiona {
    my $c = shift;
    my $id_base = shift;
    my $name = shift;

    die "Missing id_base "  if !defined $id_base;
    die "Missing name "     if !defined $name;

    my $dbh = $CON->dbh;
    my $sth = $dbh->prepare("INSERT INTO domains ( id_base,name) VALUES (?,?)");
    $sth->execute($id_base, $name);
    $sth->finish;

    if (wait_node_up($c,$name)) {
        return find_uri($name);
    }

}

sub wait_node_up {
    my ($c, $name) = @_;

    my $dbh = $CON->dbh;
    my $sth = $dbh->prepare(
        "SELECT created, error, uri FROM domains where name=?"
    );

    for (1 .. $TIMEOUT) {
        sleep 1;
        $sth->execute($name);
        my ($created, $error, $uri ) = $sth->fetchrow;
        warn "$_ : ".$created." ".($error or '');
        if ($error) {
            $c->stash(error => $error) if $error;
            last;
        }
        return $uri if $created !~ /n/i;
    }
}

sub raise_node {
    my ($c, $id_base, $name) = @_;

    my $dbh = $CON->dbh;
    my $sth = $dbh->prepare(
        "SELECT id FROM domains WHERE name=? "
        ." AND id_base=?"
    );
    $sth->execute($name, $id_base);
    my ($id) = $sth->fetchrow;
    $sth->finish;
    warn "Found $id " if $id;
    return provisiona(@_) if !$id;

    $sth = $dbh->prepare(
        "INSERT INTO domains_req (id_domain,start,date_req) "
        ." VALUES (?,'y',NOW())"
    );
    $sth->execute($id);

    $sth = $dbh->prepare("SELECT last_insert_id() FROM domains_req");
    $sth->execute;
    my ($id_request) = $sth->fetchrow or die "Missing last insert id";

    return wait_request_done($c,$id_request);
}

sub wait_request_done {
    my ($c, $id) = @_;
    
    my $req;
    my $sth = $CON->dbh->prepare(
        "SELECT r.* , name "
        ." FROM domains_req r, domains d "
        ." WHERE r.id=? AND r.id_domain = d.id "
    );
    for ( 1 .. $TIMEOUT ) {
        $sth->execute($id);
        $req = $sth->fetchrow_hashref;
        $c->stash(error => $req->{error})   if $req->{error};
        last if $req->{id} && $req->{done};
        sleep 1;
    }
    return 0 if $req->{error};
    return find_uri($req->{name});
}

sub base_name {
    my $id_base = shift;

    my $sth = $CON->dbh->prepare("SELECT name FROM bases where id=?");
    $sth->execute($id_base);
    return $sth->fetchrow;
}

sub show_link {
    my $c = shift;
    my ($id_base, $name) = @_;

    my $base_name = base_name($id_base)
        or die "Unkown id_base '$id_base'";

    my $host = $base_name."-".$name;

    my $uri = ( find_uri($host) or raise_node($c, $id_base,$host));
    if (!$uri) {
        $c->render(template => 'fail', name => $host);
        return;
    }
    $c->redirect_to($uri);
    $c->render(template => 'run', url => $uri , name => $name);
}

sub list_bases {
    my $dbh = $CON->dbh();
    my $sth = $dbh->prepare(
        "SELECT id, name FROM bases"
        ." ORDER BY id"
    );
    $sth->execute;
    my %base;
    while ( my ($id,$name) = $sth->fetchrow) {
        $base{$id} = $name;
    }
    $sth->finish;
    return \%base;
}

app->start;
__DATA__

@@ index.html.ep
% layout 'default';
<h1>Welcome to SPICE !</h1>

<form method="post">
    User Name: <input name="login" value ="<%= $login %>" 
            type="text"><br/>
    Password: <input type="password" name="password" value=""><br/>
    Base: <select name="id_base">
%       for my $option (sort keys %$base) {
            <option value="<%= $option %>"><%= $base->{$option} %></option>
%       }
    </select><br/>
    
    <input type="submit" name="submit" value="launch">
</form>
% if (scalar @$error) {
        <ul>
%       for my $i (@$error) {
            <li><%= $i %></li>
%       }
        </ul>
% }

@@ bases.html.ep
% layout 'default';
<h1>Choose a base</h1>

<ul>
% for my $i (sort values %$base) {
    <li><a href="/ip/<%= $i %>"><%= $i %></a></li>
% }
</ul>

@@ run.html.ep
% layout 'default';
<h1>Run</h1>

Hi <%= $name %>, 
<a href="<%= $url %>">click here</a>

@@ fail.html.ep
% layout 'default';
<h1>Fail</h1>

Sorry <%= $name %>, I couldn't make it.
<pre>ERROR: <%= $error %></pre>

@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
  <head><title><%= title %></title></head>
  <body>
    <%= content %>
    <hr>
        <h2>Requirements</h1>
            <ul>
            <li>Linux: virt-viewer</li>
            <li>Windows: <a href="http://bfy.tw/5Nur">Spice plugin for Firefox</a></li>
            </ul>
        </h2>
  </body>
</html>