Commit 4b93da87 authored by joelalju's avatar joelalju
Browse files

805_snap_choice: feat(wip): Add upload screenshot function

parent 09b5d575
...@@ -1759,6 +1759,23 @@ sub _cmd_copy_screenshot { ...@@ -1759,6 +1759,23 @@ sub _cmd_copy_screenshot {
} }
} }
sub _cmd_upload_screenshot($self, $request) {
my $self = shift;
my $request = shift;
my $id_domain = $request->args('id_domain');
my $fileuploaded = $request->args('file');
my $filename = $request->args('filename');
if (!$domain->can_screenshot) {
die "I can't take a screenshot of the domain ".$domain->name;
} else {
$bytes = $domain->upload_screenshot($request->args('filename'), $request->args('file'));
$bytes = $domain->upload_screenshot($request->args('filename'), $request->args('file')) if !$bytes;
}
$request->error("No data received") if !$bytes;
}
sub _cmd_create{ sub _cmd_create{
my $self = shift; my $self = shift;
my $request = shift; my $request = shift;
...@@ -2318,6 +2335,7 @@ sub _req_method { ...@@ -2318,6 +2335,7 @@ sub _req_method {
,refresh_storage => \&_cmd_refresh_storage ,refresh_storage => \&_cmd_refresh_storage
,domain_autostart=> \&_cmd_domain_autostart ,domain_autostart=> \&_cmd_domain_autostart
,change_owner => \&_cmd_change_owner ,change_owner => \&_cmd_change_owner
,upload_screenshot => \&_cmd_upload_screenshot
); );
return $methods{$cmd}; return $methods{$cmd};
......
...@@ -174,6 +174,7 @@ after 'rename' => \&_post_rename; ...@@ -174,6 +174,7 @@ after 'rename' => \&_post_rename;
before 'clone' => \&_pre_clone; before 'clone' => \&_pre_clone;
after 'screenshot' => \&_post_screenshot; after 'screenshot' => \&_post_screenshot;
after 'upload_screenshot' => \&_post_screenshot;
after '_select_domain_db' => \&_post_select_domain_db; after '_select_domain_db' => \&_post_select_domain_db;
...@@ -326,7 +327,7 @@ sub _pre_prepare_base($self, $user, $request = undef ) { ...@@ -326,7 +327,7 @@ sub _pre_prepare_base($self, $user, $request = undef ) {
# TODO: if disk is not base and disks have not been modified, do not generate them # TODO: if disk is not base and disks have not been modified, do not generate them
# again, just re-attach them # again, just re-attach them
# $self->_check_disk_modified( # $self->_check_disk_modified(
die "ERROR: domain ".$self->name." is already a base" if $self->is_base(); die "ERROR: domain ".$self->name." is already a base" if $self->is_base();
$self->_check_has_clones(); $self->_check_has_clones();
...@@ -402,7 +403,7 @@ sub _check_free_memory{ ...@@ -402,7 +403,7 @@ sub _check_free_memory{
my $lxs = Sys::Statistics::Linux->new( memstats => 1 ); my $lxs = Sys::Statistics::Linux->new( memstats => 1 );
my $stat = $lxs->get; my $stat = $lxs->get;
die "ERROR: No free memory. Only ".int($stat->memstats->{realfree}/1024) die "ERROR: No free memory. Only ".int($stat->memstats->{realfree}/1024)
." MB out of ".int($MIN_FREE_MEMORY/1024)." MB required." ." MB out of ".int($MIN_FREE_MEMORY/1024)." MB required."
if ( $stat->memstats->{realfree} < $MIN_FREE_MEMORY ); if ( $stat->memstats->{realfree} < $MIN_FREE_MEMORY );
} }
...@@ -550,7 +551,7 @@ sub _data($self, $field, $value=undef, $table='domains') { ...@@ -550,7 +551,7 @@ sub _data($self, $field, $value=undef, $table='domains') {
@field_select = ( id_domain => $self->id ) if $table ne 'domains'; @field_select = ( id_domain => $self->id ) if $table ne 'domains';
$self->{$data} = $self->_select_domain_db( _table => $table, @field_select ); $self->{$data} = $self->_select_domain_db( _table => $table, @field_select );
confess "No DB info for domain @field_select in $table ".$self->name confess "No DB info for domain @field_select in $table ".$self->name
if ! exists $self->{$data}; if ! exists $self->{$data};
confess "No field $field in $data @field_select ".Dumper($self->{$data}) confess "No field $field in $data @field_select ".Dumper($self->{$data})
if !exists $self->{$data}->{$field}; if !exists $self->{$data}->{$field};
...@@ -1295,7 +1296,7 @@ sub _post_shutdown { ...@@ -1295,7 +1296,7 @@ sub _post_shutdown {
my $req = Ravada::Request->force_shutdown_domain( my $req = Ravada::Request->force_shutdown_domain(
id_domain => $self->id id_domain => $self->id
, uid => $arg{user}->id , uid => $arg{user}->id
, at => time+$timeout , at => time+$timeout
); );
} }
$self->_remove_temporary_machine(@_); $self->_remove_temporary_machine(@_);
...@@ -1493,9 +1494,9 @@ sub _add_iptable { ...@@ -1493,9 +1494,9 @@ sub _add_iptable {
@iptables_arg = ( '0.0.0.0/0' @iptables_arg = ( '0.0.0.0/0'
,$local_ip, 'filter', $IPTABLES_CHAIN, 'DROP', ,$local_ip, 'filter', $IPTABLES_CHAIN, 'DROP',
,{'protocol' => 'tcp', 's_port' => 0, 'd_port' => $local_port}); ,{'protocol' => 'tcp', 's_port' => 0, 'd_port' => $local_port});
($rv, $out_ar, $errs_ar) = $ipt_obj->append_ip_rule(@iptables_arg); ($rv, $out_ar, $errs_ar) = $ipt_obj->append_ip_rule(@iptables_arg);
$self->_log_iptable(iptables => \@iptables_arg, %args); $self->_log_iptable(iptables => \@iptables_arg, %args);
} }
...@@ -1977,7 +1978,7 @@ sub set_option($self, $option, $value) { ...@@ -1977,7 +1978,7 @@ sub set_option($self, $option, $value) {
} elsif ($option eq 'run_timeout') { } elsif ($option eq 'run_timeout') {
$self->run_timeout($value); $self->run_timeout($value);
} elsif ($option eq 'volatile_clones') { } elsif ($option eq 'volatile_clones') {
$self->volatile_clones($value); $self->volatile_clones($value);
} else { } else {
confess "ERROR: Unknown option '$option'"; confess "ERROR: Unknown option '$option'";
} }
......
...@@ -219,7 +219,7 @@ sub remove { ...@@ -219,7 +219,7 @@ sub remove {
# warn "WARNING: Problem removing ".$self->file_base_img." for ".$self->name # warn "WARNING: Problem removing ".$self->file_base_img." for ".$self->name
# ." , I will try again later : $@" if $@; # ." , I will try again later : $@" if $@;
$self->_post_remove_base_domain() if $self->is_base(); $self->_post_remove_base_domain() if $self->is_base();
eval { $self->domain->undefine() if $self->domain }; eval { $self->domain->undefine() if $self->domain };
...@@ -559,8 +559,8 @@ sub _pre_shutdown_domain { ...@@ -559,8 +559,8 @@ sub _pre_shutdown_domain {
my $self = shift; my $self = shift;
my ($state, $reason) = $self->domain->get_state(); my ($state, $reason) = $self->domain->get_state();
if ($state == Sys::Virt::Domain::STATE_PMSUSPENDED_UNKNOWN if ($state == Sys::Virt::Domain::STATE_PMSUSPENDED_UNKNOWN
|| $state == Sys::Virt::Domain::STATE_PMSUSPENDED_DISK_UNKNOWN || $state == Sys::Virt::Domain::STATE_PMSUSPENDED_DISK_UNKNOWN
|| $state == Sys::Virt::Domain::STATE_PMSUSPENDED) { || $state == Sys::Virt::Domain::STATE_PMSUSPENDED) {
$self->domain->pm_wakeup(); $self->domain->pm_wakeup();
for ( 1 .. 10 ) { for ( 1 .. 10 ) {
...@@ -937,6 +937,23 @@ sub screenshot { ...@@ -937,6 +937,23 @@ sub screenshot {
unlink $file_tmp or warn "$! removing $file_tmp"; unlink $file_tmp or warn "$! removing $file_tmp";
} }
=head2 upload_screenshot
Uploads a screenshot, it stores it in file.
=cut
sub upload_screenshot {
my $self = shift;
my $filename = (shift or $self->_file_screenshot);
my $file = shift;
my ($path) = $filename =~ m{(.*)/};
make_path($path) if ! -e $path;
$file->move_to($path);
}
sub _file_screenshot { sub _file_screenshot {
my $self = shift; my $self = shift;
my $doc = XML::LibXML->load_xml(string => $self->_vm->storage_pool->get_xml_description); my $doc = XML::LibXML->load_xml(string => $self->_vm->storage_pool->get_xml_description);
......
...@@ -65,6 +65,7 @@ our %VALID_ARG = ( ...@@ -65,6 +65,7 @@ our %VALID_ARG = (
,refresh_storage => { id_vm => 2 } ,refresh_storage => { id_vm => 2 }
,clone => { uid => 1, id_domain => 1, name => 1, memory => 2 } ,clone => { uid => 1, id_domain => 1, name => 1, memory => 2 }
,change_owner => {uid => 1, id_domain => 1} ,change_owner => {uid => 1, id_domain => 1}
,upload_screenshot => { id_domain => 1, file => 1, filename => 1}
); );
our %CMD_SEND_MESSAGE = map { $_ => 1 } our %CMD_SEND_MESSAGE = map { $_ => 1 }
...@@ -921,6 +922,32 @@ sub change_owner { ...@@ -921,6 +922,32 @@ sub change_owner {
); );
} }
=head2 upload_screenshot
Changes the screenshot of a machine with an image uploaded
my $req = Ravada::Request->upload_screenshot(
id_domain => $domain->id
,file => $c->param("myScreenshot")
);
=cut
sub upload_screenshot {
my $proto = shift;
my $class = ref($proto) || $proto;
my $args = _check_args('upload_screenshot', @_ );
my $self = {};
bless($self,$class);
return _new_request($self
, command => 'upload_screenshot'
, args =>$args
);
}
=head2 domain_autostart =head2 domain_autostart
Sets the autostart flag on a domain Sets the autostart flag on a domain
......
...@@ -12,6 +12,7 @@ use Mojolicious::Lite 'Ravada::I18N'; ...@@ -12,6 +12,7 @@ use Mojolicious::Lite 'Ravada::I18N';
use Time::Piece; use Time::Piece;
#use Mojolicious::Plugin::I18N; #use Mojolicious::Plugin::I18N;
use Mojo::Home; use Mojo::Home;
use Mojo::Upload;
##### #####
#my $self->plugin('I18N'); #my $self->plugin('I18N');
#package Ravada::I18N:en; #package Ravada::I18N:en;
...@@ -1434,6 +1435,24 @@ sub settings_machine { ...@@ -1434,6 +1435,24 @@ sub settings_machine {
_enable_buttons($c, $domain); _enable_buttons($c, $domain);
if ($c->req->upload('myScreenshot')) {
# my $filename = $upload->filename;
# $upload->filename = "${domain->id}."
# $upload = $upload->move_to('/tmp/${filename}');
warn "hey";
my $id = $domain->id;
# my $filename =~ s/.*\.(.*)/$id.$1/;
my $fileuploaded = $c->req->upload('myScreenshot');
my $filename = "$DOCUMENT_ROOT/img/screenshots/".$domain->id.$1;
warn "hey";
$req = Ravada::Request->upload_screenshot(
id_domain => $domain->name
, file => $fileuploaded
, filename => $filename
)
}
$c->stash(message => ''); $c->stash(message => '');
my @reqs = (); my @reqs = ();
for (qw(sound video network image jpeg zlib playback streaming)) { for (qw(sound video network image jpeg zlib playback streaming)) {
......
...@@ -3,19 +3,28 @@ ...@@ -3,19 +3,28 @@
<div ng-hide= "<%= $domain->is_base %>" class="row" ng-cloak> <div ng-hide= "<%= $domain->is_base %>" class="row" ng-cloak>
<div class="col-lg-2"> <div class="col-lg-2">
<button ng-click="screenshot(<%= $domain->id %>, <%= $domain->is_active %>)"> <button ng-click="screenshot(<%= $domain->id %>, <%= $domain->is_active %>)">
<%=l "Take screenshot" %></a> <%=l "Take screenshot" %></a>
</div> </div>
% if ( $USER->is_admin ) { % if ( $USER->is_admin ) {
<div class="col-lg-2"> <div class="col-lg-2">
<button ng-click="copy_screenshot(<%= $domain->id %>, '<%= $domain->file_screenshot %>')"> <button ng-click="copy_screenshot(<%= $domain->id %>, '<%= $domain->file_screenshot %>')">
<%=l "Set base picture" %></button> <%=l "Set base picture" %></button>
</div> </div>
% } % }
<i ng-show="reload_page_msg" ng-cloak><font color="green">Saving the screenshot, please wait a moment...</font></i> <i ng-show="reload_page_msg" ng-cloak><font color="green">Saving the screenshot, please wait a moment...</font></i>
<i ng-show="fail_page_msg" ng-cloak><font color="red">You need to run the machine in order to take a screenshot.</font></i> <i ng-show="fail_page_msg" ng-cloak><font color="red">You need to run the machine in order to take a screenshot.</font></i>
<i ng-show="reload_page_copy_msg" ng-cloak><font color="green">Copying the screenshot, please wait a moment...</font></i> <i ng-show="reload_page_copy_msg" ng-cloak><font color="green">Copying the screenshot, please wait a moment...</font></i>
<i ng-show="fail_page_copy_msg" ng-cloak><font color="red">You need a screenshot in order to copy it to the base machine.</font></i> <i ng-show="fail_page_copy_msg" ng-cloak><font color="red">You need a screenshot in order to copy it to the base machine.</font></i>
</div> </div>
% if ( $USER->is_admin ) {
<div class="row">
<form method='post' enctype="multipart/form-data">
<div class="col-lg-5"><input type="file" name="myScreenshot" id="myScreenshot" /></div>
<div class="col-lg-2"><input type="submit" onclick='this.form.submit();' name="uploadScreenshot" id="uploadScreenshot" value="<%=l "Upload Screenshot" %>"></input></div>
</form>
</div>
% }
<div class="row"> <div class="row">
<div class="col-lg-4"> <div class="col-lg-4">
<div class="panel panel-default"> <div class="panel panel-default">
...@@ -25,6 +34,6 @@ ...@@ -25,6 +34,6 @@
</div> </div>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
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