mirror of
https://git.proxmox.com/git/pve-common
synced 2025-07-27 11:44:45 +00:00
PBS client: use our normal blessed method call style
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
parent
0904f388a7
commit
243568ca26
@ -1,16 +1,16 @@
|
|||||||
package PVE::PBSClient;
|
package PVE::PBSClient;
|
||||||
|
# utility functions for interaction with Proxmox Backup client CLI executable
|
||||||
# utility functions for interaction with Proxmox Backup Server
|
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
|
|
||||||
use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC);
|
use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC);
|
||||||
use IO::File;
|
use IO::File;
|
||||||
use JSON;
|
use JSON;
|
||||||
use POSIX qw(strftime ENOENT);
|
use POSIX qw(strftime ENOENT);
|
||||||
|
|
||||||
use PVE::Tools qw(run_command file_set_contents file_get_contents file_read_firstline);
|
|
||||||
use PVE::JSONSchema qw(get_standard_option);
|
use PVE::JSONSchema qw(get_standard_option);
|
||||||
|
use PVE::Tools qw(run_command file_set_contents file_get_contents file_read_firstline);
|
||||||
|
|
||||||
sub new {
|
sub new {
|
||||||
my ($class, $scfg, $storeid, $sdir) = @_;
|
my ($class, $scfg, $storeid, $sdir) = @_;
|
||||||
@ -20,7 +20,12 @@ sub new {
|
|||||||
|
|
||||||
my $secret_dir = $sdir // '/etc/pve/priv/storage';
|
my $secret_dir = $sdir // '/etc/pve/priv/storage';
|
||||||
|
|
||||||
my $self = bless { scfg => $scfg, storeid => $storeid, secret_dir => $secret_dir }, $class;
|
my $self = bless {
|
||||||
|
scfg => $scfg,
|
||||||
|
storeid => $storeid,
|
||||||
|
secret_dir => $secret_dir
|
||||||
|
}, $class;
|
||||||
|
return $self;
|
||||||
}
|
}
|
||||||
|
|
||||||
my sub password_file_name {
|
my sub password_file_name {
|
||||||
@ -32,7 +37,7 @@ my sub password_file_name {
|
|||||||
sub set_password {
|
sub set_password {
|
||||||
my ($self, $password) = @_;
|
my ($self, $password) = @_;
|
||||||
|
|
||||||
my $pwfile = password_file_name($self);
|
my $pwfile = $self->password_file_name();
|
||||||
mkdir $self->{secret_dir};
|
mkdir $self->{secret_dir};
|
||||||
|
|
||||||
PVE::Tools::file_set_contents($pwfile, "$password\n", 0600);
|
PVE::Tools::file_set_contents($pwfile, "$password\n", 0600);
|
||||||
@ -41,7 +46,7 @@ sub set_password {
|
|||||||
sub delete_password {
|
sub delete_password {
|
||||||
my ($self) = @_;
|
my ($self) = @_;
|
||||||
|
|
||||||
my $pwfile = password_file_name($self);
|
my $pwfile = $self->password_file_name();
|
||||||
|
|
||||||
unlink $pwfile;
|
unlink $pwfile;
|
||||||
};
|
};
|
||||||
@ -49,7 +54,7 @@ sub delete_password {
|
|||||||
sub get_password {
|
sub get_password {
|
||||||
my ($self) = @_;
|
my ($self) = @_;
|
||||||
|
|
||||||
my $pwfile = password_file_name($self);
|
my $pwfile = $self->password_file_name();
|
||||||
|
|
||||||
return PVE::Tools::file_read_firstline($pwfile);
|
return PVE::Tools::file_read_firstline($pwfile);
|
||||||
}
|
}
|
||||||
@ -63,7 +68,7 @@ sub encryption_key_file_name {
|
|||||||
sub set_encryption_key {
|
sub set_encryption_key {
|
||||||
my ($self, $key) = @_;
|
my ($self, $key) = @_;
|
||||||
|
|
||||||
my $encfile = encryption_key_file_name($self);
|
my $encfile = $self->encryption_key_file_name();
|
||||||
mkdir $self->{secret_dir};
|
mkdir $self->{secret_dir};
|
||||||
|
|
||||||
PVE::Tools::file_set_contents($encfile, "$key\n", 0600);
|
PVE::Tools::file_set_contents($encfile, "$key\n", 0600);
|
||||||
@ -72,7 +77,7 @@ sub set_encryption_key {
|
|||||||
sub delete_encryption_key {
|
sub delete_encryption_key {
|
||||||
my ($self) = @_;
|
my ($self) = @_;
|
||||||
|
|
||||||
my $encfile = encryption_key_file_name($self);
|
my $encfile = $self->encryption_key_file_name();
|
||||||
|
|
||||||
if (!unlink $encfile) {
|
if (!unlink $encfile) {
|
||||||
return if $! == ENOENT;
|
return if $! == ENOENT;
|
||||||
@ -84,7 +89,7 @@ sub delete_encryption_key {
|
|||||||
my sub open_encryption_key {
|
my sub open_encryption_key {
|
||||||
my ($self) = @_;
|
my ($self) = @_;
|
||||||
|
|
||||||
my $encryption_key_file = encryption_key_file_name($self);
|
my $encryption_key_file = $self->encryption_key_file_name();
|
||||||
|
|
||||||
my $keyfd;
|
my $keyfd;
|
||||||
if (!open($keyfd, '<', $encryption_key_file)) {
|
if (!open($keyfd, '<', $encryption_key_file)) {
|
||||||
@ -126,7 +131,7 @@ my sub do_raw_client_cmd {
|
|||||||
# This must live in the top scope to not get closed before the `run_command`
|
# This must live in the top scope to not get closed before the `run_command`
|
||||||
my $keyfd;
|
my $keyfd;
|
||||||
if ($use_crypto) {
|
if ($use_crypto) {
|
||||||
if (defined($keyfd = open_encryption_key($self))) {
|
if (defined($keyfd = $self->open_encryption_key())) {
|
||||||
my $flags = fcntl($keyfd, F_GETFD, 0)
|
my $flags = fcntl($keyfd, F_GETFD, 0)
|
||||||
// die "failed to get file descriptor flags: $!\n";
|
// die "failed to get file descriptor flags: $!\n";
|
||||||
fcntl($keyfd, F_SETFD, $flags & ~FD_CLOEXEC)
|
fcntl($keyfd, F_SETFD, $flags & ~FD_CLOEXEC)
|
||||||
@ -141,7 +146,7 @@ my sub do_raw_client_cmd {
|
|||||||
|
|
||||||
push @$cmd, "--repository", "$username\@$server:$datastore";
|
push @$cmd, "--repository", "$username\@$server:$datastore";
|
||||||
|
|
||||||
local $ENV{PBS_PASSWORD} = get_password($self);
|
local $ENV{PBS_PASSWORD} = $self->get_password();
|
||||||
|
|
||||||
local $ENV{PBS_FINGERPRINT} = $scfg->{fingerprint};
|
local $ENV{PBS_FINGERPRINT} = $scfg->{fingerprint};
|
||||||
|
|
||||||
@ -158,7 +163,7 @@ my sub do_raw_client_cmd {
|
|||||||
|
|
||||||
my sub run_raw_client_cmd {
|
my sub run_raw_client_cmd {
|
||||||
my ($self, $client_cmd, $param, %opts) = @_;
|
my ($self, $client_cmd, $param, %opts) = @_;
|
||||||
return do_raw_client_cmd($self, $client_cmd, $param, %opts);
|
return $self->do_raw_client_cmd($client_cmd, $param, %opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
my sub run_client_cmd {
|
my sub run_client_cmd {
|
||||||
@ -172,8 +177,12 @@ my sub run_client_cmd {
|
|||||||
|
|
||||||
$param = [@$param, '--output-format=json'] if !$no_output;
|
$param = [@$param, '--output-format=json'] if !$no_output;
|
||||||
|
|
||||||
do_raw_client_cmd($self, $client_cmd, $param,
|
$self->do_raw_client_cmd(
|
||||||
outfunc => $outfunc, errmsg => 'proxmox-backup-client failed');
|
$client_cmd,
|
||||||
|
$param,
|
||||||
|
outfunc => $outfunc,
|
||||||
|
errmsg => 'proxmox-backup-client failed'
|
||||||
|
);
|
||||||
|
|
||||||
return undef if $no_output;
|
return undef if $no_output;
|
||||||
|
|
||||||
@ -184,7 +193,7 @@ my sub run_client_cmd {
|
|||||||
|
|
||||||
sub autogen_encryption_key {
|
sub autogen_encryption_key {
|
||||||
my ($self) = @_;
|
my ($self) = @_;
|
||||||
my $encfile = encryption_key_file_name($self);
|
my $encfile = $self->encryption_key_file_name();
|
||||||
run_command(['proxmox-backup-client', 'key', 'create', '--kdf', 'none', $encfile]);
|
run_command(['proxmox-backup-client', 'key', 'create', '--kdf', 'none', $encfile]);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -196,7 +205,7 @@ sub get_snapshots {
|
|||||||
push @$param, $opts->{group};
|
push @$param, $opts->{group};
|
||||||
}
|
}
|
||||||
|
|
||||||
return run_client_cmd($self, "snapshots", $param);
|
return $self->run_client_cmd("snapshots", $param);
|
||||||
};
|
};
|
||||||
|
|
||||||
sub backup_tree {
|
sub backup_tree {
|
||||||
@ -219,7 +228,7 @@ sub backup_tree {
|
|||||||
push @$param, '--backup-id', $id;
|
push @$param, '--backup-id', $id;
|
||||||
push @$param, '--backup-time', $time if defined($time);
|
push @$param, '--backup-time', $time if defined($time);
|
||||||
|
|
||||||
return run_raw_client_cmd($self, 'backup', $param, %$opts);
|
return $self->run_raw_client_cmd('backup', $param, %$opts);
|
||||||
};
|
};
|
||||||
|
|
||||||
sub restore_pxar {
|
sub restore_pxar {
|
||||||
@ -240,7 +249,7 @@ sub restore_pxar {
|
|||||||
push @$param, "$target";
|
push @$param, "$target";
|
||||||
push @$param, "--allow-existing-dirs", 0;
|
push @$param, "--allow-existing-dirs", 0;
|
||||||
|
|
||||||
return run_raw_client_cmd($self, 'restore', $param, %$opts);
|
return $self->run_raw_client_cmd('restore', $param, %$opts);
|
||||||
};
|
};
|
||||||
|
|
||||||
sub forget_snapshot {
|
sub forget_snapshot {
|
||||||
@ -252,7 +261,7 @@ sub forget_snapshot {
|
|||||||
|
|
||||||
push @$param, "$snapshot";
|
push @$param, "$snapshot";
|
||||||
|
|
||||||
return run_raw_client_cmd($self, 'forget', $param);
|
return $self->run_raw_client_cmd('forget', $param);
|
||||||
};
|
};
|
||||||
|
|
||||||
sub prune_group {
|
sub prune_group {
|
||||||
@ -276,7 +285,7 @@ sub prune_group {
|
|||||||
}
|
}
|
||||||
push @$param, "$group";
|
push @$param, "$group";
|
||||||
|
|
||||||
return run_client_cmd($self, 'prune', $param);
|
return $self->run_client_cmd('prune', $param);
|
||||||
};
|
};
|
||||||
|
|
||||||
sub status {
|
sub status {
|
||||||
@ -288,7 +297,7 @@ sub status {
|
|||||||
my $active = 0;
|
my $active = 0;
|
||||||
|
|
||||||
eval {
|
eval {
|
||||||
my $res = run_client_cmd($self, "status");
|
my $res = $self->run_client_cmd("status");
|
||||||
|
|
||||||
$active = 1;
|
$active = 1;
|
||||||
$total = $res->{total};
|
$total = $res->{total};
|
||||||
|
Loading…
Reference in New Issue
Block a user