]>
git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Config.pm
1 package PVE
::APIClient
::Config
;
6 use File
::Basename
qw(dirname);
7 use File
::Path
qw(make_path);
9 use PVE
::APIClient
::Helpers
;
10 use PVE
::APIClient
::JSONSchema
;
11 use PVE
::APIClient
::SectionConfig
;
12 use PVE
::APIClient
::PTY
;
13 use PVE
::APIClient
::Tools
qw(file_get_contents file_set_contents);
15 use base
qw(PVE::APIClient::SectionConfig);
17 my $remote_namne_regex = qw(\w+);
19 my $defaults_section = '!DEFAULTS';
21 my $complete_remote_name = sub {
23 my $config = PVE
::APIClient
::Config-
>load();
25 foreach my $name (keys %{$config->{ids
}}) {
26 push @$list, $name if $name ne $defaults_section;
31 PVE
::APIClient
::JSONSchema
::register_standard_option
('pveclient-remote-name', {
32 description
=> "The name of the remote.",
34 pattern
=> $remote_namne_regex,
35 completion
=> $complete_remote_name,
41 description
=> "Section type.",
54 my $dir = PVE
::APIClient
::Helpers
::configuration_directory
();
60 my ($class, $timeout, $code, @param) = @_;
62 my $dir = PVE
::APIClient
::Helpers
::configuration_directory
();
65 my $filename = "$dir/.config.lck";
67 my $res = PVE
::APIClient
::Tools
::lock_file
($filename, $timeout, $code, @param);
74 sub format_section_header
{
75 my ($class, $type, $sectionId, $scfg, $done_hash) = @_;
77 if ($type eq 'defaults') {
80 return "$type: $sectionId\n";
84 sub parse_section_header
{
85 my ($class, $line) = @_;
87 if ($line =~ m/^defaults:\s*$/) {
88 return ('defaults', $defaults_section, undef, {});
89 } elsif ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
90 my ($type, $name) = (lc($1), $2);
92 die "invalid remote name '$name'\n"
93 if $name eq $defaults_section || $name !~ m/^$remote_namne_regex$/;
95 return ($type, $name, $@, {});
103 my $filename = $class->config_filename();
108 my $filemode = (stat($filename))[2] & 07777;
109 if ($filemode != 0600) {
110 die sprintf "wrong permissions on '$filename' %04o (expected 0600)\n", $filemode;
113 $raw = file_get_contents
($filename);
116 return $class->parse_config($filename, $raw);
120 my ($class, $cfg) = @_;
122 my $filename = $class->config_filename();
124 make_path
(dirname
($filename));
126 $cfg->{order
}->{$defaults_section} = -1; # write as first section
127 my $raw = $class->write_config($filename, $cfg);
129 file_set_contents
($filename, $raw, 0600);
133 my ($class, $cfg) = @_;
135 $cfg->{ids
}->{$defaults_section} //= {};
137 return $cfg->{ids
}->{$defaults_section};
141 my ($class, $cfg, $name, $noerr) = @_;
143 my $data = $cfg->{ids
}->{$name};
145 return $data if $noerr || defined($data);
147 die "unknown remote \"$name\"\n";
151 my ($class, $cfg, $remote) = @_;
153 my $section = $class->lookup_remote($cfg, $remote);
156 my ($ticket_or_password) = @_;
158 if (!defined($ticket_or_password)) {
159 $ticket_or_password = PVE::APIClient::PTY::read_password("Remote password
: ")
163 username => $section->{username},
164 password => $ticket_or_password,
165 host => $section->{host},
166 port => $section->{port} // 8006,
167 cached_fingerprints => {
168 $section->{fingerprint} => 1,
172 my $conn = PVE::APIClient::LWP->new(%$setup);
179 my $password = $section->{password};
183 if (defined($password)) {
184 $conn = $trylogin->($password);
187 if (my $ticket = PVE::APIClient::Helpers::ticket_cache_lookup($remote)) {
188 eval { $conn = $trylogin->($ticket); };
190 PVE::APIClient::Helpers::ticket_cache_update($remote, undef);
191 if (ref($err) && (ref($err) eq 'PVE::APIClient::Exception') && ($err->{code} == 401)) {
192 $conn = $trylogin->();
198 $conn = $trylogin->();
202 PVE::APIClient::Helpers::ticket_cache_update($remote, $conn->{ticket});
207 package PVE::APIClient::RemoteConfig;
212 use PVE::APIClient::JSONSchema qw(register_standard_option get_standard_option);
213 use PVE
::APIClient
::SectionConfig
;
215 use base
qw( PVE::APIClient::Config);
223 name
=> get_standard_option
('pveclient-remote-name'),
225 description
=> "The host.",
226 type
=> 'string', format
=> 'address',
230 description
=> "The username.",
235 description
=> "The users password.",
240 description
=> "The port.",
246 description
=> "Fingerprint.",
251 description
=> "Description.",
261 name
=> { optional
=> 0 },
262 host
=> { optional
=> 0 },
263 comment
=> { optional
=> 1 },
264 username
=> { optional
=> 0 },
265 password
=> { optional
=> 1 },
266 port
=> { optional
=> 1 },
267 fingerprint
=> { optional
=> 1 },
271 __PACKAGE__-
>register();
274 package PVE
::APIClient
::DefaultsConfig
;
279 use PVE
::APIClient
::JSONSchema
qw(register_standard_option get_standard_option);
281 use base
qw( PVE::APIClient::Config);
290 name
=> { optional
=> 1 },
291 username
=> { optional
=> 1 },
292 port
=> { optional
=> 1 },
296 __PACKAGE__-
>register();
299 PVE
::APIClient
::Config-
>init();