Commit 10755d24 authored by Francesc Guasch's avatar Francesc Guasch Committed by Fernando Verdugo
Browse files

feature(frontend): multiple copies (#1092)

Also improved get RAM size from base

issue #1091
parent 0359aa34
......@@ -174,6 +174,7 @@
$scope.list_ldap_attributes();
$scope.list_interfaces();
$scope.hardware_types = Object.keys(response.data.hardware);
$scope.copy_ram = $scope.showmachine.max_mem / 1024 / 1024;
});
};
$scope.list_interfaces = function() {
......@@ -309,6 +310,19 @@
$scope.refresh_machine();
});
};
$scope.copy_machine = function() {
$http.post('/machine/copy/'
, JSON.stringify({ 'id_base': $scope.showmachine.id
,'copy_number': $scope.copy_number
,'copy_ram': $scope.copy_ram
,'new_name': $scope.new_name
})
).then(function(response) {
$scope.getReqs();
$scope.refresh_machine();
});
};
//On load code
// $scope.showmachineId = window.location.pathname.split("/")[3].split(".")[0] || -1 ;
$scope.refresh_machine = function() {
......
......@@ -2075,32 +2075,56 @@ sub copy_machine {
return login($c) if !_logged_in($c);
my $arg = decode_json($c->req->body);
my $id_base= $c->param('id_base') or confess "Missing param id_base";
my $ram = $c->param('copy_ram');
$ram = 0 if $ram !~ /^\d+(\.\d+)?$/;
my $number = $arg->{copy_number};
my $id_base= $arg->{id_base} or confess "Missing param id_base";
my $ram = $arg->{copy_ram};
$ram = 0 if !$ram || $ram !~ /^\d+(\.\d+)?$/;
$ram = int($ram*1024*1024);
my $disk= $c->param('copy_disk');
$disk = 0 if $disk && $disk !~ /^\d+(\.\d+)?$/;
$disk = int($disk*1024*1024*1024) if $disk;
my ($param_name) = grep /^copy_name_\d+/,(@{$c->req->params->names});
my $base = $RAVADA->search_domain_by_id($id_base) or confess "I can't find domain $id_base";
my $name = $c->req->param($param_name) if $param_name;
$name = $base->name."-".$USER->name if !$name;
my $name = ( $arg->{new_name} or $base->name."-".$USER->name );
my @create_args =( memory => $ram ) if $ram;
push @create_args , ( disk => $disk ) if $disk;
my $req2 = Ravada::Request->clone(
uid => $USER->id
my @reqs;
if ($number == 1 ) {
my $req2 = Ravada::Request->clone(
uid => $USER->id
,name => $name
, id_domain => $base->id
,@create_args
);
$c->redirect_to("/machine/manage/".$base->id.".html");# if !@error;
, id_domain => $base->id
,@create_args
);
push @reqs, ( $req2 );
} else {
push @reqs,(copy_machine_many($base, $number, \@create_args));
}
return $c->render(json => { request => [map { $_->id } @ reqs ] } );
}
sub copy_machine_many($base, $number, $create_args) {
my $domains = $RAVADA->list_domains;
my %domain_exists = map { $_->{name} => 1 } @$domains;
my @reqs;
for ( 1 .. $number ) {
my $n = $_;
my $name;
for ( ;; ) {
while (length($n) < length($number)) { $n = "0".$n };
$name = $base->name."-".$n;
last if !$domain_exists{$name}++;
$n++;
}
my $req2 = Ravada::Request->clone(
uid => $USER->id
,name => $name
, id_domain => $base->id
,@$create_args
);
push @reqs, ( $req2 );
}
return @reqs;
}
sub machine_is_public {
......
<div class="card-body">
<form method="post" action="/machine/copy">
<div class="form-group">
<div class="form-group row">
<div class="col-md-2 mt-2">
<label class="control-label" for="clone_number">Clones</label>
</div>
<div class="col-md-2">
<input class="form-control"
ng-model="copy_number" type="text" ng-init="copy_number=1"
size="3" value="1">
</div>
</div>
<div class="form-group row" ng-show="copy_number == 1">
<div class="col-md-2 mt-2">
<input class="form-control" type="hidden" name="id_base" value="{{showmachine.id}}">
<label class="control-label"
for="copy_name_{{showmachine.id}}"><%=l 'Name' %></label>
for="copy_name"><%=l 'Name' %></label>
</div>
<div class="col-md-5">
<input class="form-control" name="copy_name_{{showmachine.id}}" type="text" size="40"
value="{{showmachine.name}}-copy"
<input class="form-control"
type="text" size="40"
ng-model="new_name"
ng-change="validate_new_name(showmachine.name)"
>
......@@ -21,19 +30,20 @@
<label class="control-label" for="copy_ram">RAM (Gb)</label>
</div>
<div class="col-md-2">
<input class="form-control" name="copy_ram" type="text" size="3" value="1">
<input class="form-control" ng-model="copy_ram" type="text" size="3">
</div>
</div>
<button type="reset" class="btn btn-outline-secondary mr-2"><%=l 'Cancel' %></button>
<input type="submit" class="btn btn-primary" ng-disabled="new_name_duplicated || new_name_invalid || new_name === showmachine.name" value="<%=l 'Submit' %>">
<button class="btn btn-primary"
ng-click="copy_machine()"
ng-disabled="copy_number <1 || copy_number == 1 && (new_name_duplicated || new_name_invalid || new_name === showmachine.name)"><%=l 'Submit' %></button>
</div>
</form>
<div class="form-group has-error">
<label ng-show="new_name_duplicated"
<label ng-show="copy_number==1 && new_name_duplicated"
class="alert alert-danger col-form-label" for="new_name">
This name is duplicated
</label>
<label ng-show="new_name_invalid"
<label ng-show="copy_number==1 && new_name_invalid"
class="alert alert-danger col-form-label" for="new_name">
This name is invalid. It can only contain alphabetic, numbers, undercores and dashes
and must start by a letter.
......
Supports Markdown
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