X-Git-Url: https://git.proxmox.com/?p=pve-client.git;a=blobdiff_plain;f=PVE%2FAPIClient%2FConfig.pm;h=5437be656ea1e2588f3d5528b434a0404f4f731e;hp=3f2efe596100db9d7ab27917e7a5927c5177f012;hb=faa47c20e27c8d4050594e9abaa2c65d792a7eda;hpb=269f8ffa5a9a5bfc67bff1682cc1cc3c39458d95 diff --git a/PVE/APIClient/Config.pm b/PVE/APIClient/Config.pm index 3f2efe5..5437be6 100644 --- a/PVE/APIClient/Config.pm +++ b/PVE/APIClient/Config.pm @@ -3,7 +3,10 @@ package PVE::APIClient::Config; use strict; use warnings; use JSON; +use File::Basename qw(dirname); +use File::Path qw(make_path); +use PVE::APIClient::Helpers; use PVE::APIClient::JSONSchema; use PVE::APIClient::SectionConfig; use PVE::APIClient::PTY; @@ -25,14 +28,6 @@ my $complete_remote_name = sub { return $list; }; -PVE::APIClient::JSONSchema::register_standard_option('pveclient-output-format', { - type => 'string', - description => 'Output format.', - enum => [ 'table', 'json' ], - optional => 1, - default => 'table', -}); - PVE::APIClient::JSONSchema::register_standard_option('pveclient-remote-name', { description => "The name of the remote.", type => 'string', @@ -56,11 +51,24 @@ sub private { sub config_filename { my ($class) = @_; - my $home = $ENV{HOME}; + my $dir = PVE::APIClient::Helpers::configuration_directory(); + + return "$dir/config"; +} + +sub lock_config { + my ($class, $timeout, $code, @param) = @_; - die "environment HOME not set\n" if !defined($home); + my $dir = PVE::APIClient::Helpers::configuration_directory(); + make_path($dir); - return "$home/.pveclient"; + my $filename = "$dir/.config.lck"; + + my $res = PVE::APIClient::Tools::lock_file($filename, $timeout, $code, @param); + + die $@ if $@; + + return $res; } sub format_section_header { @@ -113,6 +121,8 @@ sub save { my $filename = $class->config_filename(); + make_path(dirname($filename)); + $cfg->{order}->{$defaults_section} = -1; # write as first section my $raw = $class->write_config($filename, $cfg); @@ -142,22 +152,54 @@ sub remote_conn { my $section = $class->lookup_remote($cfg, $remote); + my $trylogin = sub { + my ($ticket_or_password) = @_; + + if (!defined($ticket_or_password)) { + $ticket_or_password = PVE::APIClient::PTY::read_password("Remote password: ") + } + + my $setup = { + username => $section->{username}, + password => $ticket_or_password, + host => $section->{host}, + port => $section->{port} // 8006, + cached_fingerprints => { + $section->{fingerprint} => 1, + } + }; + + my $conn = PVE::APIClient::LWP->new(%$setup); + + $conn->login(); + + return $conn; + }; + my $password = $section->{password}; - if (!defined($password)) { - $password = PVE::APIClient::PTY::read_password("Remote password: ") - } - my $conn = PVE::APIClient::LWP->new( - username => $section->{username}, - password => $password, - host => $section->{host}, - port => $section->{port} // 8006, - cached_fingerprints => { - $section->{fingerprint} => 1, + my $conn; + + if (defined($password)) { + $conn = $trylogin->($password); + } else { + + if (my $ticket = PVE::APIClient::Helpers::ticket_cache_lookup($remote)) { + eval { $conn = $trylogin->($ticket); }; + if (my $err = $@) { + PVE::APIClient::Helpers::ticket_cache_update($remote, undef); + if (ref($err) && (ref($err) eq 'PVE::APIClient::Exception') && ($err->{code} == 401)) { + $conn = $trylogin->(); + } else { + die $err; + } + } + } else { + $conn = $trylogin->(); } - ); + } - $conn->login; + PVE::APIClient::Helpers::ticket_cache_update($remote, $conn->{ticket}); return $conn; }