]>
git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Config.pm
a4aa4c6ec8697563cd6875310f28d9e401364115
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-output-format', {
33 description
=> 'Output format.',
34 enum
=> [ 'text', 'json' ],
39 PVE
::APIClient
::JSONSchema
::register_standard_option
('pveclient-remote-name', {
40 description
=> "The name of the remote.",
42 pattern
=> $remote_namne_regex,
43 completion
=> $complete_remote_name,
49 description
=> "Section type.",
62 my $dir = PVE
::APIClient
::Helpers
::configuration_directory
();
68 my ($class, $timeout, $code, @param) = @_;
70 my $filename = $class->config_filename();
72 my $res = PVE
::APIClient
::Tools
::lock_file
($filename, $timeout, $code, @param);
79 sub format_section_header
{
80 my ($class, $type, $sectionId, $scfg, $done_hash) = @_;
82 if ($type eq 'defaults') {
85 return "$type: $sectionId\n";
89 sub parse_section_header
{
90 my ($class, $line) = @_;
92 if ($line =~ m/^defaults:\s*$/) {
93 return ('defaults', $defaults_section, undef, {});
94 } elsif ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
95 my ($type, $name) = (lc($1), $2);
97 die "invalid remote name '$name'\n"
98 if $name eq $defaults_section || $name !~ m/^$remote_namne_regex$/;
100 return ($type, $name, $@, {});
108 my $filename = $class->config_filename();
113 my $filemode = (stat($filename))[2] & 07777;
114 if ($filemode != 0600) {
115 die sprintf "wrong permissions on '$filename' %04o (expected 0600)\n", $filemode;
118 $raw = file_get_contents
($filename);
121 return $class->parse_config($filename, $raw);
125 my ($class, $cfg) = @_;
127 my $filename = $class->config_filename();
129 make_path
(dirname
($filename));
131 $cfg->{order
}->{$defaults_section} = -1; # write as first section
132 my $raw = $class->write_config($filename, $cfg);
134 file_set_contents
($filename, $raw, 0600);
138 my ($class, $cfg) = @_;
140 $cfg->{ids
}->{$defaults_section} //= {};
142 return $cfg->{ids
}->{$defaults_section};
146 my ($class, $cfg, $name, $noerr) = @_;
148 my $data = $cfg->{ids
}->{$name};
150 return $data if $noerr || defined($data);
152 die "unknown remote \"$name\"\n";
156 my ($class, $cfg, $remote) = @_;
158 my $section = $class->lookup_remote($cfg, $remote);
161 my ($ticket_or_password) = @_;
163 if (!defined($ticket_or_password)) {
164 $ticket_or_password = PVE::APIClient::PTY::read_password("Remote password
: ")
168 username => $section->{username},
169 password => $ticket_or_password,
170 host => $section->{host},
171 port => $section->{port} // 8006,
172 cached_fingerprints => {
173 $section->{fingerprint} => 1,
177 my $conn = PVE::APIClient::LWP->new(%$setup);
184 my $password = $section->{password};
188 if (defined($password)) {
189 $conn = $trylogin->($password);
192 if (my $ticket = PVE::APIClient::Helpers::ticket_cache_lookup($remote)) {
193 eval { $conn = $trylogin->($ticket); };
195 PVE::APIClient::Helpers::ticket_cache_update($remote, undef);
196 if (ref($err) && (ref($err) eq 'PVE::APIClient::Exception') && ($err->{code} == 401)) {
197 $conn = $trylogin->();
203 $conn = $trylogin->();
207 PVE::APIClient::Helpers::ticket_cache_update($remote, $conn->{ticket});
212 package PVE::APIClient::RemoteConfig;
217 use PVE::APIClient::JSONSchema qw(register_standard_option get_standard_option);
218 use PVE
::APIClient
::SectionConfig
;
220 use base
qw( PVE::APIClient::Config);
228 name
=> get_standard_option
('pveclient-remote-name'),
230 description
=> "The host.",
231 type
=> 'string', format
=> 'address',
235 description
=> "The username.",
240 description
=> "The users password.",
245 description
=> "The port.",
251 description
=> "Fingerprint.",
256 description
=> "Description.",
266 name
=> { optional
=> 0 },
267 host
=> { optional
=> 0 },
268 comment
=> { optional
=> 1 },
269 username
=> { optional
=> 0 },
270 password
=> { optional
=> 1 },
271 port
=> { optional
=> 1 },
272 fingerprint
=> { optional
=> 1 },
276 __PACKAGE__-
>register();
279 package PVE
::APIClient
::DefaultsConfig
;
284 use PVE
::APIClient
::JSONSchema
qw(register_standard_option get_standard_option);
286 use base
qw( PVE::APIClient::Config);
295 name
=> { optional
=> 1 },
296 username
=> { optional
=> 1 },
297 port
=> { optional
=> 1 },
301 __PACKAGE__-
>register();
304 PVE
::APIClient
::Config-
>init();