Commit 30bebea9 authored by Francesc Guasch's avatar Francesc Guasch
Browse files

wip(frontend): list machines and ping backend

issue #1196
parent 756796eb
......@@ -1070,9 +1070,13 @@ sub _upgrade_table {
my ($new_size) = $definition =~ m{\((\d+)};
my ($new_type) = $definition =~ m{(\w+)};
my ($new_default) = $definition =~ m{default (.*)}i;
my $sth = $dbh->column_info(undef,undef,$table,$field);
my $row = $sth->fetchrow_hashref;
$row->{COLUMN_DEF} = 'NULL'
if $row->{IS_NULLABLE} eq 'YES' && !defined $row->{COLUMN_DEF};
lock_hash(%$row);
$sth->finish;
if ( $dbh->{Driver}{Name} =~ /mysql/
&& $row
......@@ -1082,6 +1086,12 @@ sub _upgrade_table {
&& $new_size != $row->{COLUMN_SIZE}
) || (
lc($row->{TYPE_NAME}) ne lc($new_type)
) || ( defined $new_default && !defined $row->{COLUMN_DEF}
) || ( !defined $new_default && defined $row->{COLUMN_DEF}
) || (
defined $new_default
&& defined $row->{COLUMN_DEF}
&& lc($new_default) ne lc($row->{COLUMN_DEF})
)
)
){
......@@ -1089,7 +1099,10 @@ sub _upgrade_table {
warn "INFO: changing $field\n"
." $row->{COLUMN_SIZE} to ".($new_size or '')."\n"
." $row->{TYPE_NAME} -> $new_type \n"
." ".($row->{COLUMN_DEF} or '')." -> ".($new_default or '')."\n"
." in $table\n$definition\n" if $0 !~ /\.t$/;
warn Dumper($row);
exit;
$dbh->do("alter table $table change $field $field $definition");
return;
}
......@@ -1214,6 +1227,7 @@ sub _upgrade_tables {
$self->_upgrade_table('requests','at_time','int(11) DEFAULT NULL');
$self->_upgrade_table('requests','run_time','float DEFAULT NULL');
$self->_upgrade_table('requests','retry','int(11) DEFAULT NULL');
$self->_upgrade_table('requests','date_changed','timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP');
$self->_upgrade_table('iso_images','rename_file','varchar(80) DEFAULT NULL');
$self->_clean_iso_mini();
......@@ -1261,6 +1275,7 @@ sub _upgrade_tables {
$self->_upgrade_table('domains','is_pool','int NOT NULL default 0');
$self->_upgrade_table('domains','needs_restart','int not null default 0');
$self->_upgrade_table('domains_network','allowed','int not null default 1');
$self->_upgrade_table('iptables','id_vm','int DEFAULT NULL');
......
......@@ -706,7 +706,9 @@ Return true if alive, false otherwise.
sub ping_backend {
my $self = shift;
return 1 if $self->_ping_backend_localhost();
my $requests = $self->list_requests(undef, 90, 1);
my @requests2 = grep { $_->{status} ne 'requested' } @$requests;
return 1 if scalar (@requests2);
my $req = Ravada::Request->ping_backend();
$self->wait_request($req, 2);
......@@ -844,7 +846,7 @@ Returns a list of ruquests : ( id , domain_name, status, error )
=cut
sub list_requests($self, $id_domain_req=undef, $seconds=60) {
sub list_requests($self, $id_domain_req=undef, $seconds=60, $all=0) {
my @now = localtime(time-$seconds);
$now[4]++;
......@@ -882,6 +884,7 @@ sub list_requests($self, $id_domain_req=undef, $seconds=60) {
)->epoch;
}
}
if (!$all) {
next if $command eq 'enforce_limits'
|| $command eq 'refresh_vms'
|| $command eq 'refresh_storage'
......@@ -903,6 +906,7 @@ sub list_requests($self, $id_domain_req=undef, $seconds=60) {
&& time - $epoch_date_changed > 5
&& $status eq 'done'
&& !$error;
}
next if $id_domain_req && defined $id_domain && $id_domain != $id_domain_req;
my $args;
$args = decode_json($j_args) if $j_args;
......
......@@ -26,8 +26,11 @@ has ravada => (
my %SUB = (
list_alerts => \&_list_alerts
,list_machines => \&_list_machines
,list_machines_user => \&_list_machines_user
,list_bases_anonymous => \&_list_bases_anonymous
,list_requests => \&_list_requests
,machine_info => \&_get_machine_info
,ping_backend => \&_ping_backend
);
######################################################################
......@@ -74,6 +77,20 @@ sub _list_machines($rvd, $args) {
return $rvd->list_machines($user);
}
sub _list_machines_user($rvd, $args) {
my $login = $args->{login} or die "Error: no login arg ".Dumper($args);
my $user = Ravada::Auth::SQL->new(name => $login)
or die "Error: uknown user $login";
return $rvd->list_machines_user($user)
}
sub _list_bases_anonymous($rvd, $args) {
my $remote_ip = $args->{remote_ip} or die "Error: no remote_ip arg ".Dumper($args);
return $rvd->list_bases_anonymous($remote_ip);
}
sub _list_requests($rvd, $args) {
my $login = $args->{login} or die "Error: no login arg ".Dumper($args);
my $user = Ravada::Auth::SQL->new(name => $login) or die "Error: uknown user $login";
......@@ -100,6 +117,18 @@ sub _get_machine_info($rvd, $args) {
return $info;
}
sub _ping_backend($rvd, $args) {
my $requests = $rvd->list_requests(undef, 120, 1);
return 1 if !scalar(@$requests);
warn Dumper($requests);
my @requests2 = grep { $_->{status} ne 'requested' } @$requests;
return 0 if !scalar(@requests2) && grep { $_->{command} eq 'ping_backend'} @$requests;
warn "***".Dumper(map { [ $_->{command} => $_->{status} ] } @requests2);
return scalar (@requests2);
}
sub _different_list($list1, $list2) {
return 1 if scalar(@$list1) != scalar (@$list2);
for my $i (0 .. scalar(@$list1)-1) {
......
......@@ -107,16 +107,17 @@
}
};
$scope.list_machines_user = function() {
var seconds = 1000;
if ($scope.refresh <= 0) {
var url = '/list_machines_user.json';
if ($scope.anonymous) {
url = '/list_bases_anonymous.json';
}
$http.get(url).then(function(response) {
$scope.machines = response.data;
subscribe_list_machines_user = function(url) {
var channel = 'list_machines_user';
if ($scope.anonymous) {
channel = 'list_bases_anonymous';
}
var ws = new WebSocket(url);
ws.onopen = function(event) { ws.send(channel) };
ws.onmessage = function(event) {
var data = JSON.parse(event.data);
$scope.$apply(function () {
$scope.machines = data;
$scope.public_bases = 0;
$scope.private_bases = 0;
for (var i = 0; i < $scope.machines.length; i++) {
......@@ -126,29 +127,27 @@
$scope.private_bases++;
}
}
}, function error(response) {
console.log(response.status);
});
$scope.refresh = 5;
} else {
$scope.refresh--;
}
$timeout(function() {
$scope.list_machines_user();
}, seconds);
};
$url_list = "/list_bases.json";
if ( typeof $_anonymous !== 'undefined' && $_anonymous ) {
$url_list = "/list_bases_anonymous.json";
}
$http.get($url_list).then(function(response) {
$scope.list_bases= response.data;
});
subscribe_ping_backend= function(url) {
var ws = new WebSocket(url);
ws.onopen = function(event) { ws.send('ping_backend') };
ws.onmessage = function(event) {
var data = JSON.parse(event.data);
$scope.$apply(function () {
$scope.pingbe_fail = !data;
});
}
};
$scope.subscribe_ws = function(url) {
subscribe_list_machines_user(url);
subscribe_ping_backend(url);
};
$http.get('/pingbackend.json').then(function(response) {
$scope.pingbe_fail = !response.data;
});
$scope.only_public = false;
$scope.toggle_only_public=function() {
......
......@@ -153,6 +153,7 @@ hook before_routes => sub {
# anonymous URLs
if (($url =~ m{^/machine/(clone|display|info|view)/}
|| $url =~ m{^/(list_bases_anonymous|request/)}i
|| $url =~ m{^/(ws_subscribe|pingbackend.json)/?$}
) && !_logged_in($c)) {
$USER = _anonymous_user($c);
return if $USER->is_temporary;
......@@ -2349,7 +2350,7 @@ sub _random_name {
sub _new_anonymous_user {
my $c = shift;
my $name_mojo = reverse($c->signed_cookie('mojolicious'));
my $name_mojo = reverse($c->signed_cookie('mojolicious') or '');
my $length = 32;
$name_mojo = _random_name($length) if !$name_mojo;
......
......@@ -8,9 +8,10 @@
$_anonymous=<%= ($_anonymous or 0) %>;
</script>
<div id="page-wrapper" ng-controller="bases" ng-init="anonymous=<%= $_anonymous or 0 %>;list_machines_user();">
<div id="page-wrapper" ng-controller="bases" ng-init="anonymous=<%= $_anonymous or 0 %>;subscribe_ws('<%= url_for('ws_subscribe')->to_abs %>');">
%= include 'main/list_bases_ng_head'
<!--MACHINES SELECTION-->
%= include 'main/ping_backend'
<div class="card-body" ng-cloak>
<div ng-repeat="machine in machines | orderBy: 'id_clone'"
class="col-sm-4 d-inline-block mb-2"
......
<div ng-show="<%= $user->is_admin %> && pingbe_fail" class="alert alert-danger" ng-cloak>
<strong><%=l 'Error' %>:</strong> <%=l 'Backend no available!' %>
</div>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment