forked from UPC/ravada
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rvd_front.pl
1872 lines (1463 loc) · 49.5 KB
/
rvd_front.pl
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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl
use warnings;
use strict;
#####
use locale ':not_characters';
#####
use Carp qw(confess);
use Data::Dumper;
use Digest::SHA qw(sha256_hex);
use Hash::Util qw(lock_hash);
use Mojolicious::Lite 'Ravada::I18N';
use Time::Piece;
#use Mojolicious::Plugin::I18N;
use Mojo::Home;
#####
#my $self->plugin('I18N');
#package Ravada::I18N:en;
#####
use lib 'lib';
use Ravada::Front;
use Ravada::Auth;
use POSIX qw(locale_h);
my $help;
my $FILE_CONFIG = "/etc/rvd_front.conf";
my $error_file_duplicated = 0;
for my $file ( "/etc/rvd_front.conf" , ($ENV{HOME} or '')."/rvd_front.conf") {
warn "WARNING: Found config file at $file and at $FILE_CONFIG\n"
if -e $file && $FILE_CONFIG;
$FILE_CONFIG = $file if -e $file;
$error_file_duplicated++;
}
warn "WARNING: using $FILE_CONFIG\n" if$error_file_duplicated>2;
my $FILE_CONFIG_RAVADA;
for my $file ( "/etc/ravada.conf" , ($ENV{HOME} or '')."/ravada.conf") {
warn "WARNING: Found config file at $file and at $FILE_CONFIG_RAVADA\n"
if -e $file && $FILE_CONFIG_RAVADA;
$FILE_CONFIG_RAVADA = $file if -e $file;
}
my $CONFIG_FRONT = plugin Config => { default => {
hypnotoad => {
pid_file => 'log/rvd_front.pid'
,listen => ['http://*:8081']
}
,login_bg_file => '/img/intro-bg.jpg'
,login_header => 'Welcome'
,login_message => ''
,secrets => ['changeme0']
,login_custom => ''
,admin => {
hide_clones => 15
}
,config => $FILE_CONFIG_RAVADA
}
,file => $FILE_CONFIG
};
#####
#####
#####
# Import locale-handling tool set from POSIX module.
# This example uses: setlocale -- the function call
# LC_CTYPE -- explained below
# query and save the old locale
my $old_locale = setlocale(LC_CTYPE);
setlocale(LC_CTYPE, "en_US.ISO8859-1");
# LC_CTYPE now in locale "English, US, codeset ISO 8859-1"
setlocale(LC_CTYPE, "");
# LC_CTYPE now reset to default defined by LC_ALL/LC_CTYPE/LANG
# environment variables. See below for documentation.
# restore the old locale
setlocale(LC_CTYPE, $old_locale);
#####
#####
#####
plugin I18N => {namespace => 'Ravada::I18N', default => 'en'};
plugin 'RenderFile';
my %config;
%config = (config => $CONFIG_FRONT->{config}) if $CONFIG_FRONT->{config};
our $RAVADA = Ravada::Front->new(%config);
our $USER;
# TODO: get those from the config file
our $DOCUMENT_ROOT = "/var/www";
# session times out in 5 minutes
our $SESSION_TIMEOUT = ($CONFIG_FRONT->{session_timeout} or 5 * 60);
# session times out in 15 minutes for admin users
our $SESSION_TIMEOUT_ADMIN = ($CONFIG_FRONT->{session_timeout_admin} or 15 * 60);
init();
############################################################################3
hook before_routes => sub {
my $c = shift;
$c->stash(version => $RAVADA->version);
my $url = $c->req->url->to_abs->path;
$c->stash(css=>['/css/sb-admin.css']
,js=>[
'/js/ravada.js'
]
,csssnippets => []
,navbar_custom => 0
,url => undef
,_logged_in => undef
,_anonymous => undef
,_user => undef
);
return access_denied($c)
if $url =~ /(screenshot|\.json)/
&& !_logged_in($c);
if ($url =~ m{^/machine/display/} && !_logged_in($c)) {
$USER = _get_anonymous_user($c);
return if $USER->is_temporary;
}
return login($c)
if
$url !~ m{^/(anonymous|login|logout|requirements|request|robots.txt)}
&& $url !~ m{^/(css|font|img|js)}
&& !_logged_in($c);
_logged_in($c) if $url =~ m{^/requirements};
};
############################################################################3
any '/robots.txt' => sub {
my $c = shift;
warn "robots";
return $c->render(text => "User-agent: *\nDisallow: /\n", format => 'text');
};
any '/' => sub {
my $c = shift;
return quick_start($c);
};
any '/index.html' => sub {
my $c = shift;
return quick_start($c);
};
any '/login' => sub {
my $c = shift;
return login($c);
};
any '/test' => sub {
my $c = shift;
my $logged = _logged_in($c);
my $count = $c->session('count');
$c->session(count => ++$count);
my $name_mojo = $c->signed_cookie('mojolicious');
my $dump_log = ''.(Dumper($logged) or '');
return $c->render(text => "$count ".($name_mojo or '')."<br> ".$dump_log
."<br>"
."<script>alert(window.screen.availWidth"
."+\" \"+window.screen.availHeight)</script>"
);
};
any '/logout' => sub {
my $c = shift;
logout($c);
$c->redirect_to('/');
};
get '/anonymous' => sub {
my $c = shift;
# $c->render(template => 'bases', base => list_bases());
$USER = _anonymous_user($c);
return list_bases_anonymous($c);
};
get '/anonymous_logout.html' => sub {
my $c = shift;
$c->session('anonymous_user' => '');
return $c->redirect_to('/');
};
get '/anonymous/(#base_id).html' => sub {
my $c = shift;
$c->stash(_anonymous => 1 , _logged_in => 0);
_init_error($c);
my $base_id = $c->stash('base_id');
my $base = $RAVADA->search_domain_by_id($base_id);
$USER = _anonymous_user($c);
return quick_start_domain($c,$base->id, $USER->name);
};
any '/admin' => sub {
my $c = shift;
$c->redirect_to("/admin/machines")
};
any '/admin/(#type)' => sub {
my $c = shift;
return admin($c) if $c->stash('type') eq 'machines'
&& $USER->is_operator;
return access_denied($c) if !$USER->is_operator;
return admin($c);
};
any '/new_machine' => sub {
my $c = shift;
return access_denied($c) if !$USER->can_create_domain;
return new_machine($c);
};
get '/domain/new.html' => sub {
my $c = shift;
return access_denied($c) if !_logged_in($c) || !$USER->is_admin();
$c->stash(error => []);
return $c->render(template => "main/new_machine");
};
get '/list_vm_types.json' => sub {
my $c = shift;
$c->render(json => $RAVADA->list_vm_types);
};
get '/list_bases.json' => sub {
my $c = shift;
my $domains = $RAVADA->list_bases();
my @domains_show = @$domains;
if (!$USER->is_admin) {
@domains_show = ();
for (@$domains) {
push @domains_show,($_) if $_->{is_public};
}
}
$c->render(json => [@domains_show]);
};
get '/list_images.json' => sub {
my $c = shift;
my $vm_name = $c->param('backend');
$c->render(json => $RAVADA->list_iso_images($vm_name or undef));
};
get '/iso_file.json' => sub {
my $c = shift;
my @isos =('<NONE>');
push @isos,(@{$RAVADA->iso_file});
$c->render(json => \@isos);
};
get '/list_machines.json' => sub {
my $c = shift;
return access_denied($c) if !_logged_in($c) || !$USER->is_admin();
$c->render(json => $RAVADA->list_domains);
};
get '/list_bases_anonymous.json' => sub {
my $c = shift;
# shouldn't this be "list_bases" ?
$c->render(json => $RAVADA->list_bases_anonymous(_remote_ip($c)));
};
get '/list_lxc_templates.json' => sub {
my $c = shift;
$c->render(json => $RAVADA->list_lxc_templates);
};
get '/pingbackend.json' => sub {
my $c = shift;
$c->render(json => $RAVADA->ping_backend);
};
# machine commands
get '/machine/info/(:id).(:type)' => sub {
my $c = shift;
my $id = $c->stash('id');
die "No id " if !$id;
my ($domain) = _search_requested_machine($c);
return access_denied($c) if !$domain;
return access_denied($c) unless $USER->is_admin
|| $domain->id_owner == $USER->id;
$c->render(json => $RAVADA->domain_info(id => $id));
};
any '/machine/settings/(:id).(:type)' => sub {
my $c = shift;
return access_denied($c) if !$USER->can_change_settings();
return settings_machine($c);
};
any '/machine/manage/(:id).(:type)' => sub {
my $c = shift;
my ($domain) = _search_requested_machine($c);
return access_denied($c) if !$domain;
return access_denied($c) unless $USER->is_admin
|| $domain->id_owner == $USER->id;
return manage_machine($c);
};
get '/machine/view/(:id).(:type)' => sub {
my $c = shift;
my $id = $c->stash('id');
my $type = $c->stash('type');
my ($domain) = _search_requested_machine($c);
return access_denied($c) if !$domain;
return access_denied($c) unless $USER->is_admin
|| $domain->id_owner == $USER->id;
return view_machine($c);
};
get '/machine/clone/(:id).(:type)' => sub {
my $c = shift;
return access_denied($c) if !$USER->can_clone();
return clone_machine($c);
};
get '/machine/shutdown/(:id).(:type)' => sub {
my $c = shift;
return access_denied($c) if !$USER ->can_shutdown_all();
return shutdown_machine($c);
};
get '/machine/shutdown/(:id).(:type)' => sub {
my $c = shift;
return access_denied($c) if !$USER ->can_shutdown_all();
return shutdown_machine($c);
};
any '/machine/remove/(:id).(:type)' => sub {
my $c = shift;
return access_denied($c) if !$USER -> can_remove();
return remove_machine($c);
};
any '/machine/remove_clones/(:id).(:type)' => sub {
my $c = shift;
return access_denied($c) if !$USER ->can_remove_clone();
return remove_clones($c);
};
get '/machine/prepare/(:id).(:type)' => sub {
my $c = shift;
return prepare_machine($c);
};
get '/machine/remove_b/(:id).(:type)' => sub {
my $c = shift;
return remove_base($c);
};
get '/machine/remove_base/(:id).(:type)' => sub {
my $c = shift;
return remove_base($c);
};
get '/machine/screenshot/(:id).(:type)' => sub {
my $c = shift;
return access_denied($c) if !$USER->can_screenshot();
return screenshot_machine($c);
};
get '/machine/pause/(:id).(:type)' => sub {
my $c = shift;
return pause_machine($c);
};
get '/machine/hybernate/(:id).(:type)' => sub {
my $c = shift;
return access_denied($c) if !$USER ->can_hibernate_all();
return hybernate_machine($c);
};
get '/machine/resume/(:id).(:type)' => sub {
my $c = shift;
return resume_machine($c);
};
get '/machine/start/(:id).(:type)' => sub {
my $c = shift;
return start_machine($c);
};
get '/machine/exists/#name' => sub {
my $c = shift;
my $name = $c->stash('name');
#TODO
# return failure if it can't find the name in the URL
return $c->render(json => $RAVADA->domain_exists($name));
};
get '/machine/rename/#id' => sub {
my $c = shift;
return access_denied($c) if !$USER -> can_rename();
return rename_machine($c);
};
get '/machine/rename/#id/#value' => sub {
my $c = shift;
return rename_machine($c);
};
any '/machine/copy' => sub {
my $c = shift;
return access_denied($c) if !$USER -> can_clone_all();
return copy_machine($c);
};
get '/machine/public/#id' => sub {
my $c = shift;
return machine_is_public($c);
};
get '/machine/public/#id/#value' => sub {
my $c = shift;
return machine_is_public($c);
};
get '/machine/display/#id' => sub {
my $c = shift;
my $id = $c->stash('id');
my $domain = $RAVADA->search_domain_by_id($id);
return $c->render(text => "unknown machine id=$id") if !$id;
return access_denied($c)
if $USER->id ne $domain->id_owner
&& !$USER->is_admin;
$c->res->headers->content_type('application/x-virt-viewer');
$c->res->headers->content_disposition(
"attachment;filename=".$domain->id.".vv");
return $c->render(data => $domain->display_file($USER));
};
# Users ##########################################################3
##add user
any '/users/register' => sub {
my $c = shift;
return register($c);
};
any '/admin/user/(:id).(:type)' => sub {
my $c = shift;
return access_denied($c) if !$USER->can_manage_users();
my $user = Ravada::Auth::SQL->search_by_id($c->stash('id'));
return $c->render(text => "Unknown user id: ".$c->stash('id'))
if !$user;
if ($c->param('make_admin')) {
$USER->make_admin($c->stash('id')) if $c->param('is_admin');
$USER->remove_admin($c->stash('id'))if !$c->param('is_admin');
$user = Ravada::Auth::SQL->search_by_id($c->stash('id'));
}
if ($c->param('grant')) {
return access_denied($c) if !$USER->can_grant();
my %grant;
for my $param_name (@{$c->req->params->names}) {
if ( $param_name =~ /^perm_(.*)/ ) {
$grant{$1} = 1;
} elsif ($param_name =~ /^off_perm_(.*)/) {
$grant{$1} = 0 if !exists $grant{$1};
}
}
for my $perm (keys %grant) {
if ( $grant{$perm} ) {
$USER->grant($user, $perm);
} else {
$USER->revoke($user, $perm);
}
}
}
$c->stash(user => $user);
return $c->render(template => 'main/manage_user');
};
##############################################
get '/request/(:id).(:type)' => sub {
my $c = shift;
my $id = $c->stash('id');
return _show_request($c,$id);
};
get '/anonymous/request/(:id).(:type)' => sub {
my $c = shift;
my $id = $c->stash('id');
$USER = _anonymous_user($c);
return _show_request($c,$id);
};
get '/requests.json' => sub {
my $c = shift;
return list_requests($c);
};
get '/messages.json' => sub {
my $c = shift;
return $c->render( json => [$USER->messages()] );
};
get '/unshown_messages.json' => sub {
my $c = shift;
return $c->redirect_to('/login') if !_logged_in($c);
return $c->render( json => [$USER->unshown_messages()] );
};
get '/messages/read/all.html' => sub {
my $c = shift;
$USER->mark_all_messages_read;
return $c->render(inline => "1");
};
get '/messages/read/(#id).json' => sub {
my $c = shift;
my $id = $c->stash('id');
$USER->mark_message_read($id);
return $c->render(inline => "1");
};
get '/messages/unread/(#id).json' => sub {
my $c = shift;
my $id = $c->stash('id');
$USER->mark_message_unread($id);
return $c->render(inline => "1");
};
get '/messages/view/(#id).html' => sub {
my $c = shift;
my $id = $c->stash('id');
return $c->render( json => $USER->show_message($id) );
};
any '/ng-templates/(#template).html' => sub {
my $c = shift;
my $id = $c->stash('template');
return $c->render(template => 'ng-templates/'.$id);
};
any '/about' => sub {
my $c = shift;
$c->render(template => 'main/about');
};
any '/requirements' => sub {
my $c = shift;
$c->render(template => 'main/requirements');
};
any '/settings' => sub {
my $c = shift;
$c->stash(version => $RAVADA->version );
$c->render(template => 'main/settings');
};
any '/auto_start/(#value)/' => sub {
my $c = shift;
my $value = $c->stash('value');
if ($value =~ /toggle/i) {
$value = $c->session('auto_start');
if ($value) {
$value = 0;
} else {
$value = 1;
}
}
$c->session('auto_start' => $value);
return $c->render(json => {auto_start => $c->session('auto_start') });
};
get '/auto_start' => sub {
my $c = shift;
return $c->render(json => {auto_start => $c->session('auto_start') });
};
###################################################
## user_settings
any '/user_settings' => sub {
my $c = shift;
user_settings($c);
};
sub user_settings {
my $c = shift;
my $changed_lang;
my $changed_pass;
if ($c->req->method('POST')) {
$USER->language($c->param('tongue'));
$changed_lang = $c->param('tongue');
_logged_in($c);
}
$c->param('tongue' => $USER->language);
my @errors;
if ($c->param('button_click')) {
if (($c->param('password') eq "") || ($c->param('conf_password') eq "") || ($c->param('current_password') eq "")) {
push @errors,("Some of the password's fields are empty");
}
else {
my $comp_password = $USER->compare_password($c->param('current_password'));
if ($comp_password) {
if ($c->param('password') eq $c->param('conf_password')) {
eval {
$USER->change_password($c->param('password'));
_logged_in($c);
};
if ($@ =~ /Password too small/) {
push @errors,("New Password is too small");
}
else {
$changed_pass = 1;
};
}
else {
push @errors,("Password fields aren't equal");
}
}
else {
push @errors, ("Invalid Current Password");
}
}
}
$c->render(template => 'bootstrap/user_settings', changed_lang=> $changed_lang, changed_pass => $changed_pass
,errors =>\@errors);
};
get '/img/screenshots/:file' => sub {
my $c = shift;
my $file = $c->param('file');
my $path = $DOCUMENT_ROOT."/".$c->req->url->to_abs->path;
my ($id_domain ) =$path =~ m{/(\d+)\..+$};
if (!$id_domain) {
warn"ERROR : no id domain in $path";
return $c->reply->not_found;
}
if (!$USER->is_admin) {
my $domain = $RAVADA->search_domain_by_id($id_domain);
return $c->reply->not_found if !$domain;
unless ($domain->is_base && $domain->is_public) {
return access_denied($c) if $USER->id != $domain->id_owner;
}
}
return $c->reply->not_found if ! -e $path;
return $c->render_file(
filepath => $path
,'content_disposition' => 'inline'
);
};
get '/iso/download/(#id).json' => sub {
my $c = shift;
return access_denied($c) if !$USER->is_admin;
my $id = $c->stash('id');
my $req = Ravada::Request->download(
id_iso => $id
,uid => $USER->id
);
return $c->render(json => {request => $req->id});
};
###################################################
sub _init_error {
my $c = shift;
$c->stash(error_title => '');
$c->stash(error => []);
$c->stash(link => '');
$c->stash(link_msg => '');
}
sub _logged_in {
my $c = shift;
confess "missing \$c" if !defined $c;
$USER = undef;
_init_error($c);
$c->stash(_logged_in => undef , _user => undef, _anonymous => 1);
my $login = $c->session('login');
if ($login) {
$USER = Ravada::Auth::SQL->new(name => $login);
#Mojolicious::Plugin::I18N::
$c->languages($USER->language);
$c->stash(_logged_in => $login );
$c->stash(_user => $USER);
$c->stash(_anonymous => !$USER);
}
$c->stash(url => undef);
return $USER;
}
sub login {
my $c = shift;
$c->session(login => undef);
my $login = $c->param('login');
my $password = $c->param('password');
my $form_hash = $c->param('login_hash');
my $url = ($c->param('url') or $c->req->url->to_abs->path);
$url = '/' if $url =~ m{^/login};
my @error =();
# TODO: improve this hash
my ($time) = time =~ m{(.*)...$};
my $login_hash1 = $time.($CONFIG_FRONT->{secrets}->[0] or '');
# let login varm be valid for 60 seconds
($time) = (time-60) =~ m{(.*)...$};
my $login_hash2 = $time.($CONFIG_FRONT->{secrets}->[0] or '');
if (defined $login || defined $password || $c->param('submit')) {
push @error,("Empty login name") if !length $login;
push @error,("Empty password") if !length $password;
push @error,("Session timeout")
if $form_hash ne sha256_hex($login_hash1)
&& $form_hash ne sha256_hex($login_hash2);
}
if ( !@error && defined $login && defined $password) {
my $auth_ok;
eval { $auth_ok = Ravada::Auth::login($login, $password)};
if ( $auth_ok && !$@) {
$c->session('login' => $login);
my $expiration = $SESSION_TIMEOUT;
$expiration = $SESSION_TIMEOUT_ADMIN if $auth_ok->is_admin;
$c->session(expiration => $expiration);
return $c->redirect_to($url);
} else {
push @error,("Access denied");
}
}
my @css_snippets = ["\t.intro {\n\t\tbackground:"
." url($CONFIG_FRONT->{login_bg_file})"
." no-repeat bottom center scroll;\n\t}"];
sleep 5 if scalar(@error);
$c->render(
template => ($CONFIG_FRONT->{login_custom} or 'main/start')
,css => ['/css/main.css']
,csssnippets => @css_snippets
,js => ['/js/main.js']
,navbar_custom => 1
,login => $login
,login_hash => sha256_hex($login_hash1)
,error => \@error
,login_header => $CONFIG_FRONT->{login_header}
,login_message => $CONFIG_FRONT->{login_message}
);
}
sub logout {
my $c = shift;
$USER = undef;
$c->session(expires => 1);
$c->session(login => undef);
}
sub quick_start {
my $c = shift;
_logged_in($c);
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 ) {
my $log_ok;
eval { $log_ok = Ravada::Auth::login($login, $password) };
if ($log_ok) {
$c->session('login' => $login);
} else {
push @error,("Access denied");
}
}
if ( $c->param('submit') && _logged_in($c) && defined $id_base ) {
return quick_start_domain($c, $id_base, ($login or $c->session('login')));
}
return render_machines_user($c);
}
sub render_machines_user {
my $c = shift;
return $c->render(
template => 'main/list_bases2'
,machines => $RAVADA->list_machines_user($USER)
,user => $USER
);
}
sub create_domain {
my ($c, $id_base, $domain_name, $ram, $disk) = @_;
return $c->redirect_to('/login') if !$USER;
my $base = $RAVADA->search_domain_by_id($id_base) or die "I can't find base $id_base";
my $domain = provision($c, $id_base, $domain_name, $ram, $disk);
return show_failure($c, $domain_name) if !$domain;
return show_link($c,$domain);
}
sub quick_start_domain {
my ($c, $id_base, $name) = @_;
return $c->redirect_to('/login') if !$USER;
confess "Missing id_base" if !defined $id_base;
$name = $c->session('login') if !$name;
my $base = $RAVADA->search_domain_by_id($id_base) or die "I can't find base $id_base";
my $domain_name = $base->name."-".$name;
$domain_name =~ tr/[\.]/[\-]/;
my $domain = $RAVADA->search_clone(id_base => $base->id, id_owner => $USER->id);
$domain = provision($c, $id_base, $domain_name)
if !$domain || $domain->is_base;
return show_failure($c, $domain_name) if !$domain;
return show_link($c,$domain);
}
sub show_failure {
my $c = shift;
my $name = shift;
$c->render(template => 'main/fail', name => $name);
}
#######################################################
sub admin {
my $c = shift;
my $page = $c->stash('type');
my @error = ();
push @{$c->stash->{css}}, '/css/admin.css';
push @{$c->stash->{js}}, '/js/admin.js';
if ($page eq 'users') {
$c->stash(list_users => []);
$c->stash(name => $c->param('name' or ''));
if ( $c->param('name') ) {
$c->stash(list_users => $RAVADA->list_users($c->param('name') ))
}
}
if ($page eq 'machines') {
$c->stash(hide_clones => 0 );
my $list_domains = $RAVADA->list_domains();
$c->stash(hide_clones => 1 )
if scalar @$list_domains
> $CONFIG_FRONT->{admin}->{hide_clones};
# count clones from list_domains grepping those that have id_base
$c->stash(n_clones => scalar(grep { $_->{id_base} } @$list_domains) );
# if we find no clones do not hide them. They may be created later
$c->stash(hide_clones => 0 ) if !$c->stash('n_clones');
}
$c->render(template => 'main/admin_'.$page);
};
sub new_machine {
my $c = shift;
my @error ;
if ($c->param('submit')) {
push @error,("Name is mandatory") if !$c->param('name');
push @error,("Invalid name '".$c->param('name')."'"
.".It can only contain words and numbers.")
if $c->param('name') && $c->param('name') !~ /^[a-zA-Z0-9]+$/;
if (!@error) {
req_new_domain($c);
$c->redirect_to("/admin/machines");
}
} else {
my $req = Ravada::Request->refresh_storage();
# TODO handle possible errors
}
$c->stash(errors => \@error);
push @{$c->stash->{js}}, '/js/admin.js';
my %valid_vm = map { $_ => 1 } @{$RAVADA->list_vm_types};
$c->render(template => 'main/new_machine'
, name => $c->param('name')
, valid_vm => \%valid_vm
);
};
sub req_new_domain {
my $c = shift;
my $name = $c->param('name');
my $swap = ($c->param('swap') or 0);
my $vm = ( $c->param('backend') or 'KVM');
$swap *= 1024*1024*1024;
my %args = (
name => $name
,id_iso => $c->param('id_iso')
,id_template => $c->param('id_template')
,iso_file => $c->param('iso_file')
,vm=> $vm
,id_owner => $USER->id
,swap => $swap
);
$args{memory} = int($c->param('memory')*1024*1024) if $c->param('memory');
$args{disk} = int($c->param('disk')*1024*1024*1024) if $c->param('disk');
$args{id_template} = $c->param('id_template') if $vm =~ /^LX/;
$args{id_iso} = $c->param('id_iso') if $vm eq 'KVM';