On 6/7/18 1:16 PM, Dominik Csapak wrote:
> writes the given content to the file
> 
> the size is at the moment limited by the max post size of the
> pveproxy/daemon
> 
> Signed-off-by: Dominik Csapak <[email protected]>
> ---
>  PVE/API2/Qemu/Agent.pm | 51 
> ++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 51 insertions(+)
> 
> diff --git a/PVE/API2/Qemu/Agent.pm b/PVE/API2/Qemu/Agent.pm
> index dad135c..a206050 100644
> --- a/PVE/API2/Qemu/Agent.pm
> +++ b/PVE/API2/Qemu/Agent.pm
> @@ -118,6 +118,7 @@ __PACKAGE__->register_method({
>           exec
>           exec-status
>           file-read
> +         file-write
>           set-user-password
>       );
>  
> @@ -417,4 +418,54 @@ __PACKAGE__->register_method({
>       return $result;
>      }});
>  
> +__PACKAGE__->register_method({
> +    name => 'file-write',
> +    path => 'file-write',
> +    method => 'POST',
> +    protected => 1,
> +    proxyto => 'node',
> +    description => "Writes the given file via guest agent.",
> +    permissions => { check => [ 'perm', '/vms/{vmid}', [ 'VM.Monitor' ]]},
> +    parameters => {
> +     additionalProperties => 0,
> +     properties => {
> +         node => get_standard_option('pve-node'),
> +         vmid => get_standard_option('pve-vmid', {
> +                 completion => \&PVE::QemuServer::complete_vmid_running }),
> +         file => {
> +             type => 'string',
> +             description => 'The path to the file.'
> +         },
> +         content => {
> +             type => 'string',
> +             description => "The content to write into the file."

maybe a note about our post size limit (currently 64KB)
could be helpful

> +         }
> +     },
> +    },
> +    returns => { type => 'null' },
> +    code => sub {
> +     my ($param) = @_;
> +
> +     my $vmid = $param->{vmid};
> +
> +     my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists
> +
> +     check_agent_available($vmid, $conf);
> +
> +     my $qgafh = PVE::QemuServer::vm_mon_cmd($vmid, "guest-file-open",  path 
> => $param->{file}, mode => 'wb');
> +

please stick this error handling call (which should get another
name or be wrapped, as stated in another reply here) below to
it's pardner above, else this gets a bit hard to read.
If every line has an extra newline in between it's almost
like none had.

> +     raise_agent_error($qgafh, "can't open file");
> +
> +     my $buf = encode_base64($param->{content});
> +
> +     my $write = PVE::QemuServer::vm_mon_cmd($vmid, "guest-file-write", 
> handle => $qgafh, 'buf-b64' => $buf);
> +

see above

> +     raise_agent_error($write, "can't write to file");
> +
> +     my $res = PVE::QemuServer::vm_mon_cmd($vmid, "guest-file-close", handle 
> => $qgafh);
> +     raise_agent_error($res, "can't close file");

the more often I read this the more I'd like to see an
(exportable) agent_cmd() helper int PVE::QS::Agent alá:

qga_cmd($vmid, $cmd, [$params], [$emsg])


all in all cool features in this series!

> +
> +     return undef;
> +    }});
> +
>  1;
> 



_______________________________________________
pve-devel mailing list
[email protected]
https://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to