rvd_back 18.9 KB
Newer Older
Francesc Guasch's avatar
Francesc Guasch committed
1
2
3
4
5
#!/usr/bin/perl

use warnings;
use strict;

6
use Data::Dumper;
7
use Getopt::Long;
8
use POSIX ":sys_wait_h";
9
10
use Proc::PID::File;

11
use Ravada;
Francesc Guasch's avatar
Francesc Guasch committed
12
use Ravada::Auth::SQL;
13
use Ravada::Auth::LDAP;
14
use Ravada::Utils;
Francesc Guasch's avatar
Francesc Guasch committed
15

16
$|=1;
Francesc Guasch's avatar
Francesc Guasch committed
17

Francesc Guasch's avatar
Francesc Guasch committed
18
19
my $help;

20
my ($DEBUG, $ADD_USER );
21

22
23
my $VERBOSE = $ENV{TERM};

Francesc Guasch's avatar
Francesc Guasch committed
24
25
my $FILE_CONFIG_DEFAULT = "/etc/ravada.conf";
my $FILE_CONFIG;
26

27
my $ADD_USER_LDAP;
28
my $REMOVE_USER;
29
my $IMPORT_DOMAIN;
fv3rdugo's avatar
fv3rdugo committed
30
my $IMPORT_VBOX;
31
my $CHANGE_PASSWORD;
32
my $NOFORK;
33
34
my $MAKE_ADMIN_USER;
my $REMOVE_ADMIN_USER;
Francesc Guasch's avatar
Francesc Guasch committed
35
my $START = 1;
Francesc Guasch's avatar
Francesc Guasch committed
36
my $TEST_LDAP;
37
38

my $URL_ISOS;
Francesc Guasch's avatar
Francesc Guasch committed
39
my $ALL;
40
my $HIBERNATED;
41
my $DISCONNECTED;
42

Francesc Guasch's avatar
Francesc Guasch committed
43
my $LIST;
44
45

my $HIBERNATE_DOMAIN;
Francesc Guasch's avatar
Francesc Guasch committed
46
my $START_DOMAIN;
47
my $SHUTDOWN_DOMAIN;
Francesc Guasch's avatar
Francesc Guasch committed
48
my $REMOVE_DOMAIN;
Francesc Guasch's avatar
Francesc Guasch committed
49
my $REBASE;
Francesc Guasch's avatar
Francesc Guasch committed
50
my $RUN_REQUEST;
51

52
53
my $IMPORT_DOMAIN_OWNER;

Francesc Guasch's avatar
Francesc Guasch committed
54
55
my $ADD_LOCALE_REPOSITORY;

56
my $USAGE = "$0 "
Francesc Guasch's avatar
Francesc Guasch committed
57
        ." [--debug] [--config=$FILE_CONFIG_DEFAULT] [--add-user=name] [--add-user-ldap=name]"
fv3rdugo's avatar
fv3rdugo committed
58
        ." [--change-password] [--make-admin=username] [--import-vbox=image_file.vdi]"
Francesc Guasch's avatar
Francesc Guasch committed
59
        ." [--test-ldap] "
fv3rdugo's avatar
fv3rdugo committed
60
        ." [-X] [start|stop|status]"
Francesc Guasch's avatar
Francesc Guasch committed
61
        ." [--rebase MACHINE]"
62
        ." [--remove-user=name]"
63
        ."\n"
Francesc Guasch's avatar
Francesc Guasch committed
64
        ." --add-user : adds a new db user\n"
65
        ." --add-user-ldap : adds a new LDAP user\n"
66
        ." --remove-user : removes a db user\n"
67
        ." --change-password : changes the password of an user\n"
68
        ." --import-domain : import a domain\n"
69
        ." --import-domain-owner : owner of the domain to import\n"
70
        ." --make-admin : make user admin\n"
Francesc Guasch's avatar
Francesc Guasch committed
71
        ." --config : config file, defaults to $FILE_CONFIG_DEFAULT"
72
        ." --no-fork : start in foreground\n"
73
        ." --url-isos=(URL|default)\n"
fv3rdugo's avatar
fv3rdugo committed
74
        ." --import-vbox : import a VirtualBox image\n"
Francesc Guasch's avatar
Francesc Guasch committed
75
        .' --add-locale-repository LOCALE : adds ISO repositories for this locale'
Francesc Guasch's avatar
Francesc Guasch committed
76
77
78
79
80
        ."\n"
        ."Operations on Virtual Machines:\n"
        ." --list\n"
        ." --start\n"
        ." --hibernate machine\n"
81
        ." --shutdown machine\n"
Francesc Guasch's avatar
Francesc Guasch committed
82
        ." --remove machine\n"
83
84
        ."\n"
        ."Operations modifiers:\n"
Francesc Guasch's avatar
Francesc Guasch committed
85
86
        ." --all : execute on all virtual machines\n"
        ."          For hibernate, it is executed on all the actives\n"
87
        ." --hibernated: execute on hibernated machines\n"
88
        ." --disconnected: execute on disconnected machines\n"
89
        ."\n"
Francesc Guasch's avatar
Francesc Guasch committed
90
91
    ;

92
$START = 0 if scalar @ARGV && $ARGV[0] ne '&';
93

94
GetOptions (       help => \$help
Francesc Guasch's avatar
Francesc Guasch committed
95
96
                   ,all => \$ALL
                  ,list => \$LIST
97
                 ,debug => \$DEBUG
Francesc Guasch's avatar
Francesc Guasch committed
98
                ,verbose => \$VERBOSE
Francesc Guasch's avatar
Francesc Guasch committed
99
                ,rebase => \$REBASE
100
              ,'no-fork'=> \$NOFORK
101
             ,'start=s' => \$START_DOMAIN
102
             ,'config=s'=> \$FILE_CONFIG
103
           ,'hibernated'=> \$HIBERNATED
Francesc Guasch's avatar
Francesc Guasch committed
104
            ,'test-ldap'=> \$TEST_LDAP
105
           ,'add-user=s'=> \$ADD_USER
106
           ,'url-isos=s'=> \$URL_ISOS
107
108
           ,'shutdown:s'=> \$SHUTDOWN_DOMAIN
          ,'hibernate:s'=> \$HIBERNATE_DOMAIN
Francesc Guasch's avatar
Francesc Guasch committed
109
             ,'remove:s'=> \$REMOVE_DOMAIN
110
         ,'disconnected'=> \$DISCONNECTED
111
        ,'remove-user=s'=> \$REMOVE_USER
112
113
        ,'make-admin=s' => \$MAKE_ADMIN_USER
      ,'remove-admin=s' => \$REMOVE_ADMIN_USER
114
      ,'change-password'=> \$CHANGE_PASSWORD
115
      ,'add-user-ldap=s'=> \$ADD_USER_LDAP
116
      ,'import-domain=s' => \$IMPORT_DOMAIN
fv3rdugo's avatar
fv3rdugo committed
117
      ,'import-vbox=s' => \$IMPORT_VBOX
118
,'import-domain-owner=s' => \$IMPORT_DOMAIN_OWNER
Francesc Guasch's avatar
Francesc Guasch committed
119
120

    ,'add-locale-repository=s' => \$ADD_LOCALE_REPOSITORY
Francesc Guasch's avatar
Francesc Guasch committed
121
    ,'run-request=s' => \$RUN_REQUEST
122
) or exit;
Francesc Guasch's avatar
Francesc Guasch committed
123

Francesc Guasch's avatar
Francesc Guasch committed
124
125
$START = 1 if $DEBUG || $FILE_CONFIG || $NOFORK;

Francesc Guasch's avatar
Francesc Guasch committed
126

Francesc Guasch's avatar
Francesc Guasch committed
127
128
129
130
131
132
133
134
135
#####################################################################
#
# check arguments
#
if ($help) {
    print $USAGE;
    exit;
}

136
die "Only root can do that\n" if $> && ( $ADD_USER || $REMOVE_USER || $ADD_USER_LDAP || $IMPORT_DOMAIN);
137
138
139
die "ERROR: Missing file config $FILE_CONFIG\n"
    if $FILE_CONFIG && ! -e $FILE_CONFIG;

140
141
142
die "ERROR: Shutdown requires a domain name, or --all , --hibernated , --disconnected\n"
    if defined $SHUTDOWN_DOMAIN && !$SHUTDOWN_DOMAIN && !$ALL && !$HIBERNATED
                                && !$DISCONNECTED;
143

144
145
die "ERROR: Hibernate requires a domain name, or --all , --disconnected\n"
    if defined $HIBERNATE_DOMAIN && !$HIBERNATE_DOMAIN && !$ALL && !$DISCONNECTED;
146

Francesc Guasch's avatar
Francesc Guasch committed
147
148
149
die "ERROR: Missing the machine name or id\n$USAGE"
    if $REBASE && !@ARGV;

150
151
my %CONFIG;
%CONFIG = ( config => $FILE_CONFIG )    if $FILE_CONFIG;
152

Francesc Guasch's avatar
Francesc Guasch committed
153
$Ravada::DEBUG=1    if $DEBUG;
Francesc Guasch's avatar
Francesc Guasch committed
154
$Ravada::VERBOSE=1      if $VERBOSE;
155
$Ravada::CAN_FORK=0    if $NOFORK;
156

Francesc Guasch's avatar
Francesc Guasch committed
157
158
159
160
161
###################################################################

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

162
sub do_start {
163
    warn "Starting rvd_back v".Ravada::version."\n";
164
165
166
    my $old_error = ($@ or '');
    my $cnt_error = 0;

167

168
169
    my $t_refresh = 0;

170
    my $ravada = Ravada->new( %CONFIG );
Francesc Guasch's avatar
Francesc Guasch committed
171
172
    #    Ravada::Request->enforce_limits();
    #Ravada::Request->refresh_vms();
Francesc Guasch's avatar
Francesc Guasch committed
173
    for (;;) {
174
        my $t0 = time;
175
176
        $ravada->process_priority_requests();
        $ravada->process_long_requests();
177
        $ravada->process_requests();
178

Francesc Guasch's avatar
Francesc Guasch committed
179
180
        exit if done_request();

181
        if ( time - $t_refresh > 60 ) {
182
            Ravada::Request->cleanup();
183
184
            Ravada::Request->refresh_vms()      if rand(5)<3;
            Ravada::Request->enforce_limits()   if rand(5)<2;
Francesc Guasch's avatar
Francesc Guasch committed
185
            Ravada::Request->manage_pools()     if rand(5)<2;
186
            $t_refresh = time;
187
        }
188
        sleep 1 if time - $t0 <1;
189
    }
190

191
}
Francesc Guasch's avatar
Francesc Guasch committed
192

Francesc Guasch's avatar
Francesc Guasch committed
193
194
195
196
197
198
199
200
201
202
sub done_request {
    return 0 if !$RUN_REQUEST;
    my $req;
    eval { $req = Ravada::Request->open($RUN_REQUEST) };
    warn $req->status;
    warn $@ if $@;
    return 1 if !$req || $req->status eq 'done';

}

203
sub clean_old_requests {
204
    my $ravada = Ravada->new( %CONFIG );
205
    $ravada->clean_old_requests();
206
207
}

208
209
sub autostart_machines {
    my $ravada = shift;
210
211
212
213
    my $req = Ravada::Request->check_storage(
        uid => Ravada::Utils::user_daemon->id
        ,retry => 10
    );
214
215
216
217
218
219
220
221
222
    for my $domain ( $ravada->list_domains_data ) {
        next unless $domain->{autostart} && ! $domain->{is_base}
              && $domain->{status} !~ /active/i;

        print "Auto start $domain->{name} [$domain->{status}]\n" if $VERBOSE;

        Ravada::Request->start_domain(
            id_domain => $domain->{id}
            ,uid => $domain->{id_owner}
223
            ,after_request_ok => $req->id
224
225
226
227
        );
    }
}

228
sub start {
229
    {
230
        my $ravada = Ravada->new( %CONFIG );
231
        $Ravada::CONNECTOR->dbh;
232
        $ravada->_install();
233
234
235
        for my $vm (@{$ravada->vm}) {
            $vm->id;
        }
236
        $ravada->_wait_pids();
237
        autostart_machines($ravada);
238
    }
Francesc Guasch's avatar
Francesc Guasch committed
239
    clean_old_requests();
240
    for (;;) {
241
242
        eval { do_start() };
        warn $@ if $@;
Francesc Guasch's avatar
Francesc Guasch committed
243
        exit if done_request();
244
245
246
    }
}

Francesc Guasch's avatar
Francesc Guasch committed
247
248
249
sub add_user {
    my $login = shift;

250
    my $ravada = Ravada->new( %CONFIG);
251
    $ravada->_install();
252

253
    print "$login password: ";
Francesc Guasch's avatar
Francesc Guasch committed
254
255
256
    my $password = <STDIN>;
    chomp $password;

Francesc Guasch's avatar
Francesc Guasch committed
257
258
259
260
261
262
    print "is admin ? : [y/n] ";
    my $is_admin_q = <STDIN>;
    my $is_admin = 0;

    $is_admin = 1 if $is_admin_q =~ /y/i;

263

264
265
266
    Ravada::Auth::SQL::add_user(      name => $login
                                , password => $password
                                , is_admin => $is_admin);
Francesc Guasch's avatar
Francesc Guasch committed
267
268
}

269
270
271
sub add_user_ldap {
    my $login = shift;

272
273
    my $ravada = Ravada->new( %CONFIG);

274
275
276
277
278
279
280
    print "password : ";
    my $password = <STDIN>;
    chomp $password;

    Ravada::Auth::LDAP::add_user($login, $password);
}

281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
sub remove_user {
    my $login = shift;
    my $ravada = Ravada->new( %CONFIG);

    my $user = Ravada::Auth::SQL->new(name => $login);

    die "ERROR: Unknown user '$login'\n" if !$user->id;
    print "Are you sure you want remove $login user ? : [y/n] ";
    my $remove_it = <STDIN>;
    if ( $remove_it =~ /y/i ) {
        $user->remove();
        print "USER $login was removed\n";
    }
}

296
297
298
299
300
301
sub change_password {
    print "User login name : ";
    my $login = <STDIN>;
    chomp $login;
    return if !$login;

302
    my $ravada = Ravada->new( %CONFIG );
303

304
305
306
307
308
309
310
311
312
    my $user = Ravada::Auth::SQL->new(name => $login);
    die "ERROR: Unknown user '$login'\n" if !$user->id;

    print "password : ";
    my $password = <STDIN>;
    chomp $password;
    $user->change_password($password);
}

313
314
315
sub make_admin {
    my $login = shift;

316
    my $ravada = Ravada->new( %CONFIG);
317
318
319
    my $user = Ravada::Auth::SQL->new(name => $login);
    die "ERROR: Unknown user '$login'\n" if !$user->id;

320
    Ravada::Utils::user_daemon()->make_admin($user->id);
321
322
323
324
325
326
    print "USER $login granted admin permissions\n";
}

sub remove_admin {
    my $login = shift;

327
    my $ravada = Ravada->new( %CONFIG);
328
329
330
    my $user = Ravada::Auth::SQL->new(name => $login);
    die "ERROR: Unknown user '$login'\n" if !$user->id;

331
    Ravada::Utils::user_daemon()->remove_admin($user->id);
332
333
334
335
    print "USER $login removed admin permissions, granted normal user permissions.\n";
}


336
337
338
sub import_domain {
    my $name = shift;
    print "Virtual Manager: KVM\n";
339
340
341
342
343
344
    my $user = $IMPORT_DOMAIN_OWNER;
    if (!$user) {
        print "User name that will own the domain in Ravada : ";
        $user = <STDIN>;
        chomp $user;
    }
345
    my $ravada = Ravada->new( %CONFIG );
346
347
348
    $ravada->import_domain(name => $name, vm => 'KVM', user => $user);
}

fv3rdugo's avatar
fv3rdugo committed
349
350
sub import_vbox {
    my $file_vdi = shift;
fv3rdugo's avatar
fv3rdugo committed
351
352
353
    my $rvd = Ravada->new(%CONFIG);
    my $kvm = $rvd->search_vm('KVM');
    my $default_storage_pool = $kvm->storage_pool();
fv3rdugo's avatar
fv3rdugo committed
354
355
356
357
358
359
360
361
362
363
364
    if ($file_vdi =~ /\.vdi$/i) {
        print "Import VirtualBox image from vdi file\n";
        print "Name for the new domain : ";
        my $name = <STDIN>;
        chomp $name;
        print "Change default storage pool in /var/lib/libvirt/images/ [y/N]:";
        my $default_pool_q = <STDIN>;
        my $storage_pool = "/var/lib/libvirt/images";

        if ( $default_pool_q =~ /y/i ) {
            print "Insert storage pool path : ";
fv3rdugo's avatar
fv3rdugo committed
365
            $storage_pool = <STDIN>;
fv3rdugo's avatar
fv3rdugo committed
366
367
368
            chomp $storage_pool;
        }
        print "STORAGE POOL IS $storage_pool \n";
fv3rdugo's avatar
fv3rdugo committed
369
370
        print "DEFAULT STORAGE POOL IS $default_storage_pool \n";

fv3rdugo's avatar
fv3rdugo committed
371
372
373
374
375
        if ( $name && $file_vdi ) {
            my @cmd = ("qemu-img convert -p -f vdi -O qcow2 $file_vdi $storage_pool/$name.qcow2");
            system(@cmd);
        }
        print "Warning: Missing args! \n";
fv3rdugo's avatar
fv3rdugo committed
376
377
378
379
380
381
382
383
384
385
        #new machine xml change source file
        #remove swap
        #remove cdrom

        exit;
    }
    print "Warning: $file_vdi is not a vdi file \n";
    print "Check if the path has spaces, if so insert it in quotes \n";
}

386
387
388
sub set_url_isos {
    my $url = shift;
    my $rvd_back = Ravada->new(%CONFIG);
389

390
391
392
393
394
395
396
397
398
399
400
    if ($url =~ /^default$/i) {
        my $sth = $rvd_back->connector->dbh->prepare("DROP TABLE iso_images");
        $sth->execute;
        $sth->finish;
        my $rvd_back2 = Ravada->new(%CONFIG);
    } else {
        $rvd_back->_set_url_isos($url);
        print "ISO_IMAGES table URLs set from $url\n";
    }
}

Francesc Guasch's avatar
Francesc Guasch committed
401
402
403
404
405
406
sub list {
    my $all = shift;
    my $rvd_back = Ravada->new(%CONFIG);

    my $found = 0;
    for my $domain ($rvd_back->list_domains) {
407
        next if !$all && !$domain->is_active && !$domain->is_hibernated;
Francesc Guasch's avatar
Francesc Guasch committed
408
409
410
411
        $found++;
        print $domain->name."\t";
        if ($domain->is_active) {
            print "active";
412
413
414
415
416
417
            my $status = $domain->client_status;
            if ( $domain->remote_ip ) {
                $status .= " , "    if $status;
                $status .= $domain->remote_ip;
            }
            print " ( $status ) " if $status;
Francesc Guasch's avatar
Francesc Guasch committed
418
419
420
421
422
423
424
425
426
427
        } elsif ($domain->is_hibernated) {
            print "hibernated";
        } else {
            print "down";
        }
        print "\n";
    }
    print "$found machines found.\n";
}

428
429
sub hibernate {
    my $domain_name = shift;
Francesc Guasch's avatar
Francesc Guasch committed
430
431
    my $all = shift;

432
433
434
435
436
    my $rvd_back = Ravada->new(%CONFIG);

    my $down = 0;
    my $found = 0;
    for my $domain ($rvd_back->list_domains) {
Francesc Guasch's avatar
Francesc Guasch committed
437
        if ( ($all && $domain->is_active)
438
439
440
441
                || ($domain_name && $domain->name eq $domain_name)
                || ($DISCONNECTED && $domain->client_status
                        && $domain->client_status eq 'disconnected')
           ) {
442
443
444
            $found++;
            if (!$domain->is_active) {
                warn "WARNING: Virtual machine ".$domain->name
445
                    ." is already down.\n";
446
447
                next;
            }
448
449
450
451
            if ( $DISCONNECTED && $domain->client_status
                    && $domain->client_status eq 'disconnected') {
                next if _verify_connection($domain);
            }
452
            if ($domain->can_hibernate) {
453
                $domain->hibernate( Ravada::Utils::user_daemon() );
Francesc Guasch's avatar
Francesc Guasch committed
454
                $down++;
455
456
457
458
459
460
            } else {
                warn "WARNING: Virtual machine ".$domain->name
                    ." can't hibernate because it is not supported in ".$domain->type
                    ." domains."
                    ."\n";
            }
461
462
463
        }
    }
    print "$down machines hibernated.\n";
464
    warn "ERROR: Domain $domain_name not found.\n"
465
        if !$domain_name && !$found;
466
467
}

Francesc Guasch's avatar
Francesc Guasch committed
468
469
470
471
472
473
474
475
476
477
478
479
480
481
sub remove_domain {
    my $domain_name = shift;

    my $rvd_back = Ravada->new(%CONFIG);
    my $domain = $rvd_back->search_domain($domain_name);
    die "Error: domain $domain_name not found\n" if !$domain;

    Ravada::Request->remove_domain(
                uid => Ravada::Utils::user_daemon()->id
                ,name => $domain->name
    );
    print "Removing $domain_name\n";
}

Francesc Guasch's avatar
Francesc Guasch committed
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
sub start_domain {
    my $domain_name = shift;

    my $rvd_back = Ravada->new(%CONFIG);

    my $up= 0;
    my $found = 0;
    for my $domain ($rvd_back->list_domains) {
        if ($domain->name eq $domain_name) {
            $found++;
            if ($domain->is_active) {
                warn "WARNING: Virtual machine ".$domain->name
                    ." is already up.\n";
                next;
            }
497
            eval { $domain->start(user => Ravada::Utils::user_daemon() ) };
498
499
500
501
            if ($@) {
                warn $@;
                next;
            }
Francesc Guasch's avatar
Francesc Guasch committed
502
503
504
505
506
507
508
509
            print $domain->name." started.\n"
                if $domain->is_active;
        }
    }
    warn "ERROR: Domain $domain_name not found.\n"
        if !$found;
}

510
511
512
513
514
515
516
517
sub shutdown_domain {
    my $domain_name = shift;
    my ($all,$hibernated) = @_;

    my $rvd_back = Ravada->new(%CONFIG);

    my $down = 0;
    my $found = 0;
518
    DOMAIN:
Francesc Guasch's avatar
Francesc Guasch committed
519
520
    for my $domain_data ($rvd_back->list_domains_data) {
        my $domain = Ravada::Front::Domain->open($domain_data->{id});
521
522
        if ((defined $domain_name && $domain->name eq $domain_name)
            || ($hibernated && $domain->is_hibernated)
523
524
            || ($DISCONNECTED
                    && ( $domain->client_status && $domain->client_status eq 'disconnected' ))
525
526
527
528
529
530
531
532
533
            || $all ){
            $found++;
            if (!$domain->is_active && !$domain->is_hibernated) {
                warn "WARNING: Virtual machine ".$domain->name
                    ." is already down.\n"
                        if !$all;
                next;
            }
            if ($domain->is_hibernated) {
534
                print "Starting ".$domain->name."\n";
535
                eval { $domain->start(user => Ravada::Utils::user_daemon()) };
536
537
538
                warn $@ if $@;
                sleep 5 if !$@;

539
            }
540
541
542
543
544
            if ($DISCONNECTED && $domain->client_status
                    && $domain->client_status eq 'disconnected') {

                next DOMAIN if _verify_connection($domain);
            }
545
            print "Shutting down ".$domain->name.".\n";
Francesc Guasch's avatar
Francesc Guasch committed
546
547
548
549
550
            Ravada::Request->shutdown_domain(uid => Ravada::Utils::user_daemon()->id
                ,id_domain => $domain->id
                , timeout => 300
            );
            $down++;
551
            warn $@ if $@;
552
553
554
555
556
557
        }
    }
    warn "ERROR: Domain $domain_name not found.\n"
        if $domain_name && !$found;
    print "$down domains shut down.\n";
}
Francesc Guasch's avatar
Francesc Guasch committed
558

559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
sub _verify_connection {
    my $domain = shift;
    print "Verifying connection for ".$domain->name
                        ." ".($domain->remote_ip or '')." "
        if $VERBOSE;
    for ( 1 .. 25 ) {
        if ( $domain->client_status(1)
                            && $domain->client_status(1) ne 'disconnected' ) {
            print "\n\t".$domain->client_status." ".$domain->remote_ip
                            ." Shutdown dismissed.\n";
            return 1;
        }
        print "." if $VERBOSE && !($_ % 5);
        sleep 1;
     }
     print "\n" if $VERBOSE;
    return 0;
}

Francesc Guasch's avatar
Francesc Guasch committed
578
579
580
581
582
583
584
585
586
587
588
589
590
sub test_ldap {
    my $rvd_back = Ravada->new(%CONFIG);
    eval { Ravada::Auth::LDAP::_init_ldap_admin() };
    die "No LDAP connection, error: $@\n" if $@;
    print "Connection to LDAP ok\n";
    print "login: ";
    my $name=<STDIN>;
    chomp $name;
    print "password: ";
    my $password = <STDIN>;
    chomp $password;
    my $ok= Ravada::Auth::login( $name, $password);
    if ($ok) {
591
        print "LOGIN OK $ok->{_auth}\n";
Francesc Guasch's avatar
Francesc Guasch committed
592
593
594
595
596
597
    } else {
        print "LOGIN FAILED\n";
    }
    exit;
}

Francesc Guasch's avatar
Francesc Guasch committed
598
599
600
601
602
603
604
605
606
sub add_locale_repository {
    my $locale = shift;
    for my $lang ( split /,/, $locale ) {
        print "Adding locales for $lang.\n";
        my $found = Ravada::Repository::ISO::insert_iso_locale($lang, 'verbose');
        print "$found found.\n";
    }
}

Francesc Guasch's avatar
Francesc Guasch committed
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
sub rebase {
    my ($domain_name) = $ARGV[0];
    my $rvd_back = Ravada->new(%CONFIG);
    my $domain;
    if ($domain_name =~ /^\d+$/) {
        $domain = Ravada::Domain->open($domain_name);
    } else {
        $domain = $rvd_back->search_domain($domain_name);
    }
    die "Error: Unknown domain $domain_name\n"      if !$domain;
    die "Error: ".$domain->name." is not a clone\n" if !$domain->id_base;

    my $base = Ravada::Domain->open($domain->id_base);
    $base->rebase(Ravada::Utils::user_daemon, $domain);
}

Francesc Guasch's avatar
Francesc Guasch committed
623
624
625
626
627
628
629
630
631
632
633
sub run_request {
    my $id_request = shift;
    my $rvd_back = Ravada->new(%CONFIG);
    my $req = Ravada::Request->open($id_request);
    $req->status('requested');
    $rvd_back->_execute($req,1);
    warn $req->command." ".$req->status
        .($req->error or '')
    ."\n";
}

634
sub DESTROY {
635
636
}

Francesc Guasch's avatar
Francesc Guasch committed
637
#################################################################
638
639
640
641
642

{

my $rvd_back = Ravada->new(%CONFIG);

Francesc Guasch's avatar
Francesc Guasch committed
643
add_user($ADD_USER)                 if $ADD_USER;
644
add_user_ldap($ADD_USER_LDAP)       if $ADD_USER_LDAP;
645
remove_user($REMOVE_USER)           if $REMOVE_USER;
Francesc Guasch's avatar
Francesc Guasch committed
646
647
change_password()                   if $CHANGE_PASSWORD;
import_domain($IMPORT_DOMAIN)       if $IMPORT_DOMAIN;
fv3rdugo's avatar
fv3rdugo committed
648
import_vbox($IMPORT_VBOX)           if $IMPORT_VBOX;
Francesc Guasch's avatar
Francesc Guasch committed
649
650
make_admin($MAKE_ADMIN_USER)        if $MAKE_ADMIN_USER;
remove_admin($REMOVE_ADMIN_USER)    if $REMOVE_ADMIN_USER;
651
set_url_isos($URL_ISOS)             if $URL_ISOS;
Francesc Guasch's avatar
Francesc Guasch committed
652
test_ldap                           if $TEST_LDAP;
Francesc Guasch's avatar
Francesc Guasch committed
653
rebase()                            if $REBASE;
Francesc Guasch's avatar
Francesc Guasch committed
654

Francesc Guasch's avatar
Francesc Guasch committed
655
list($ALL)                          if $LIST;
656
hibernate($HIBERNATE_DOMAIN , $ALL) if defined $HIBERNATE_DOMAIN;
Francesc Guasch's avatar
Francesc Guasch committed
657
remove_domain($REMOVE_DOMAIN)              if defined $REMOVE_DOMAIN;
Francesc Guasch's avatar
Francesc Guasch committed
658
start_domain($START_DOMAIN)         if $START_DOMAIN;
659

660
661
662
shutdown_domain($SHUTDOWN_DOMAIN, $ALL, $HIBERNATED)
                                    if defined $SHUTDOWN_DOMAIN;

Francesc Guasch's avatar
Francesc Guasch committed
663
add_locale_repository($ADD_LOCALE_REPOSITORY) if $ADD_LOCALE_REPOSITORY;
Francesc Guasch's avatar
Francesc Guasch committed
664
665

run_request($RUN_REQUEST)           if $RUN_REQUEST;
666
667
668
}


Francesc Guasch's avatar
Francesc Guasch committed
669
670
671
672
if ($START) {
    die "Already started" if Proc::PID::File->running( name => 'rvd_back');
    start();
}