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

use warnings;
use strict;

use lib './lib';
7

8
use Data::Dumper;
9
use Getopt::Long;
10
11
use Proc::PID::File;

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

my $help;

18
my ($DEBUG, $ADD_USER );
19

Francesc Guasch's avatar
Francesc Guasch committed
20
21
my $FILE_CONFIG_DEFAULT = "/etc/ravada.conf";
my $FILE_CONFIG;
22

23
my $ADD_USER_LDAP;
24
my $IMPORT_DOMAIN;
25
my $CHANGE_PASSWORD;
26
my $NOFORK;
27
28
my $MAKE_ADMIN_USER;
my $REMOVE_ADMIN_USER;
Francesc Guasch's avatar
Francesc Guasch committed
29
my $START = 1;
30
31
32

my $URL_ISOS;

33
my $USAGE = "$0 "
Francesc Guasch's avatar
Francesc Guasch committed
34
        ." [--debug] [--config=$FILE_CONFIG_DEFAULT] [--add-user=name] [--add-user-ldap=name]"
35
        ." [--change-password] [--make-admin=username]"
36
37
        ." [-X] [start|stop|status]"
        ."\n"
Francesc Guasch's avatar
Francesc Guasch committed
38
        ." --add-user : adds a new db user\n"
39
        ." --add-user-ldap : adds a new LDAP user\n"
40
        ." --change-password : changes the password of an user\n"
41
        ." --import-domain : import a domain\n"
42
        ." --make-admin : make user admin\n"
Francesc Guasch's avatar
Francesc Guasch committed
43
        ." --config : config file, defaults to $FILE_CONFIG_DEFAULT"
44
        ." -X : start in foreground\n"
45
        ." --url-isos=(URL|default)\n"
Francesc Guasch's avatar
Francesc Guasch committed
46
47
    ;

48
$START = 0 if scalar @ARGV && $ARGV[0] ne '&';
49

50
51
GetOptions (       help => \$help
                 ,debug => \$DEBUG
52
              ,'no-fork'=> \$NOFORK
53
54
             ,'config=s'=> \$FILE_CONFIG
           ,'add-user=s'=> \$ADD_USER
55
           ,'url-isos=s'=> \$URL_ISOS
56
57
        ,'make-admin=s' => \$MAKE_ADMIN_USER
      ,'remove-admin=s' => \$REMOVE_ADMIN_USER
58
      ,'change-password'=> \$CHANGE_PASSWORD
59
      ,'add-user-ldap=s'=> \$ADD_USER_LDAP
60
61
      ,'import-domain=s' => \$IMPORT_DOMAIN
) or exit;
Francesc Guasch's avatar
Francesc Guasch committed
62

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

Francesc Guasch's avatar
Francesc Guasch committed
65
66
67
68
69
70
71
72
73
#####################################################################
#
# check arguments
#
if ($help) {
    print $USAGE;
    exit;
}

74
die "Only root can do that\n" if $> && ( $ADD_USER || $ADD_USER_LDAP || $IMPORT_DOMAIN);
75
76
77
78
79
die "ERROR: Missing file config $FILE_CONFIG\n"
    if $FILE_CONFIG && ! -e $FILE_CONFIG;

my %CONFIG;
%CONFIG = ( config => $FILE_CONFIG )    if $FILE_CONFIG;
80

Francesc Guasch's avatar
Francesc Guasch committed
81
$Ravada::DEBUG=1    if $DEBUG;
82
$Ravada::CAN_FORK=0    if $NOFORK;
Francesc Guasch's avatar
Francesc Guasch committed
83
84
###################################################################

85
my $PID_LONGS;
Francesc Guasch's avatar
Francesc Guasch committed
86
87
88
###################################################################
#

89
sub do_start {
90
    warn "Starting rvd_back v".Ravada::version."\n";
91
92
93
    my $old_error = ($@ or '');
    my $cnt_error = 0;

94
    clean_killed_requests();
95

Francesc Guasch's avatar
Francesc Guasch committed
96
97
    start_process_longs() if !$NOFORK;

98
    my $ravada = Ravada->new( %CONFIG );
Francesc Guasch's avatar
Francesc Guasch committed
99
    for (;;) {
100
        my $t0 = time;
101
102
        $ravada->process_requests();
        $ravada->process_long_requests(0,$NOFORK)   if $NOFORK;
103
        sleep 1 if time - $t0 <1;
Francesc Guasch's avatar
Francesc Guasch committed
104
105
106
    }
}

107
108
109
110
111
112
113
114
115
sub start_process_longs {
    my $pid = fork();
    die "I can't fork" if !defined $pid;
    if ( $pid ) {
        $PID_LONGS = $pid;
        return;
    }
    
    warn "Processing long requests in pid $$\n" if $DEBUG;
116
    my $ravada = Ravada->new( %CONFIG );
117
    for (;;) {
118
        my $t0 = time;
119
        $ravada->process_long_requests();
120
        sleep 1 if time - $t0 <1;
121
122
    }
}
Francesc Guasch's avatar
Francesc Guasch committed
123

124
sub clean_killed_requests {
125
    my $ravada = Ravada->new( %CONFIG );
126
127
128
    $ravada->clean_killed_requests();
}

129
sub start {
130
    {
131
        my $ravada = Ravada->new( %CONFIG );
132
        $Ravada::CONNECTOR->dbh;
133
134
135
136
        for my $vm (@{$ravada->vm}) {
            $vm->id;
            $vm->vm;
        }
137
    }
138
139
140
141
142
143
144
145
146
147
148
149
    for (;;) {
        my $pid = fork();
        die "I can't fork $!" if !defined $pid;
        if ($pid == 0 ) {
            do_start();
            exit;
        }
        warn "Waiting for pid $pid\n";
        waitpid($pid,0);
    }
}

Francesc Guasch's avatar
Francesc Guasch committed
150
151
152
sub add_user {
    my $login = shift;

153
    my $ravada = Ravada->new( %CONFIG);
154

155
    print "$login password: ";
Francesc Guasch's avatar
Francesc Guasch committed
156
157
158
    my $password = <STDIN>;
    chomp $password;

Francesc Guasch's avatar
Francesc Guasch committed
159
160
161
162
163
164
    print "is admin ? : [y/n] ";
    my $is_admin_q = <STDIN>;
    my $is_admin = 0;

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

165
166
167
    Ravada::Auth::SQL::add_user(      name => $login
                                , password => $password
                                , is_admin => $is_admin);
Francesc Guasch's avatar
Francesc Guasch committed
168
169
}

170
171
172
173
174
175
176
177
178
179
sub add_user_ldap {
    my $login = shift;

    print "password : ";
    my $password = <STDIN>;
    chomp $password;

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

180
181
182
183
184
185
sub change_password {
    print "User login name : ";
    my $login = <STDIN>;
    chomp $login;
    return if !$login;

186
    my $ravada = Ravada->new( %CONFIG );
187
    
188
189
190
191
192
193
194
195
196
    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);
}

197
198
199
sub make_admin {
    my $login = shift;

200
    my $ravada = Ravada->new( %CONFIG);
201
202
203
204
205
206
207
208
209
210
    my $user = Ravada::Auth::SQL->new(name => $login);
    die "ERROR: Unknown user '$login'\n" if !$user->id;

    $Ravada::USER_DAEMON->make_admin($user->id);
    print "USER $login granted admin permissions\n";
}

sub remove_admin {
    my $login = shift;

211
    my $ravada = Ravada->new( %CONFIG);
212
213
214
215
216
217
218
219
    my $user = Ravada::Auth::SQL->new(name => $login);
    die "ERROR: Unknown user '$login'\n" if !$user->id;

    $Ravada::USER_DAEMON->remove_admin($user->id);
    print "USER $login removed admin permissions, granted normal user permissions.\n";
}


220
221
222
sub import_domain {
    my $name = shift;
    print "Virtual Manager: KVM\n";
223
    print "User name that will own the domain in Ravada : ";
224
225
    my $user = <STDIN>;
    chomp $user;
226
    my $ravada = Ravada->new( %CONFIG );
227
228
229
    $ravada->import_domain(name => $name, vm => 'KVM', user => $user);
}

230
231
232
233
234
235
236
237
238
239
240
241
242
243
sub set_url_isos {
    my $url = shift;
    my $rvd_back = Ravada->new(%CONFIG);
    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";
    }
}

244
245
246
247
248
249
250
251
252
sub DESTROY {
    return if !$PID_LONGS;
    warn "Killing pid: $PID_LONGS";

    my $cnt = kill 15 , $PID_LONGS;
    return if !$cnt;

    kill 9 , $PID_LONGS;
    
253
254
}

Francesc Guasch's avatar
Francesc Guasch committed
255
#################################################################
Francesc Guasch's avatar
Francesc Guasch committed
256
257
258
259
260
261
add_user($ADD_USER)                 if $ADD_USER;
add_user($ADD_USER_LDAP)            if $ADD_USER_LDAP;
change_password()                   if $CHANGE_PASSWORD;
import_domain($IMPORT_DOMAIN)       if $IMPORT_DOMAIN;
make_admin($MAKE_ADMIN_USER)        if $MAKE_ADMIN_USER;
remove_admin($REMOVE_ADMIN_USER)    if $REMOVE_ADMIN_USER;
262
set_url_isos($URL_ISOS)             if $URL_ISOS;
Francesc Guasch's avatar
Francesc Guasch committed
263
264
265
266
267

if ($START) {
    die "Already started" if Proc::PID::File->running( name => 'rvd_back');
    start();
}