[pve-devel] [PATCH common 1/1] PBSClient: add get_repository to generate repository urls from configs

Dominik Csapak d.csapak at proxmox.com
Thu Dec 3 12:43:38 CET 2020


we can use this everywhere we need to have a repository url

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 src/PVE/PBSClient.pm | 36 +++++++++++++++++++++++++++++++-----
 1 file changed, 31 insertions(+), 5 deletions(-)

diff --git a/src/PVE/PBSClient.pm b/src/PVE/PBSClient.pm
index 44e3176..f05471c 100644
--- a/src/PVE/PBSClient.pm
+++ b/src/PVE/PBSClient.pm
@@ -10,7 +10,35 @@ use JSON;
 use POSIX qw(strftime ENOENT);
 
 use PVE::JSONSchema qw(get_standard_option);
-use PVE::Tools qw(run_command file_set_contents file_get_contents file_read_firstline);
+use PVE::Tools qw(run_command file_set_contents file_get_contents file_read_firstline $IPV6RE);
+
+# returns a repository string suitable for proxmox-backup-client, pbs-restore, etc.
+# $scfg must have the following structure:
+# {
+#     datastore
+#     server
+#     port        (optional defaults to 8007)
+#     username    (optional defaults to 'root at pam')
+# }
+sub get_repository {
+    my ($scfg) = @_;
+
+    my $server = $scfg->{server};
+    die "no server given\n" if !defined($server);
+
+    $server = "[$server]" if $server =~ /^$IPV6RE$/;
+
+    if (my $port = $scfg->{port}) {
+	$server .= ":$port" if $port != 8007;
+    }
+
+    my $datastore = $scfg->{datastore};
+    die "no datastore given\n" if !defined($datastore);
+
+    my $username = $scfg->{username} // 'root at pam';
+
+    return "$username\@$server:$datastore";
+}
 
 sub new {
     my ($class, $scfg, $storeid, $sdir) = @_;
@@ -116,9 +144,7 @@ my sub do_raw_client_cmd {
 	if ! -x $client_exe;
 
     my $scfg = $self->{scfg};
-    my $server = $scfg->{server};
-    my $datastore = $scfg->{datastore};
-    my $username = $scfg->{username} // 'root at pam';
+    my $repo = get_repository($scfg);
 
     my $userns_cmd = delete $opts{userns_cmd};
 
@@ -144,7 +170,7 @@ my sub do_raw_client_cmd {
 
     push @$cmd, @$param if defined($param);
 
-    push @$cmd, "--repository", "$username\@$server:$datastore";
+    push @$cmd, "--repository", $repo;
 
     local $ENV{PBS_PASSWORD} = $self->get_password();
 
-- 
2.20.1






More information about the pve-devel mailing list