]>
git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Config.pm
a783ab3870a4f01bdd9fe85988c40c50e3427173
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 $dir = PVE
::APIClient
::Helpers
::configuration_directory
();
73 my $filename = "$dir/.config.lck";
75 my $res = PVE
::APIClient
::Tools
::lock_file
($filename, $timeout, $code, @param);
82 sub format_section_header
{
83 my ($class, $type, $sectionId, $scfg, $done_hash) = @_;
85 if ($type eq 'defaults') {
88 return "$type: $sectionId\n";
92 sub parse_section_header
{
93 my ($class, $line) = @_;
95 if ($line =~ m/^defaults:\s*$/) {
96 return ('defaults', $defaults_section, undef, {});
97 } elsif ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
98 my ($type, $name) = (lc($1), $2);
100 die "invalid remote name '$name'\n"
101 if $name eq $defaults_section || $name !~ m/^$remote_namne_regex$/;
103 return ($type, $name, $@, {});
111 my $filename = $class->config_filename();
116 my $filemode = (stat($filename))[2] & 07777;
117 if ($filemode != 0600) {
118 die sprintf "wrong permissions on '$filename' %04o (expected 0600)\n", $filemode;
121 $raw = file_get_contents
($filename);
124 return $class->parse_config($filename, $raw);
128 my ($class, $cfg) = @_;
130 my $filename = $class->config_filename();
132 make_path
(dirname
($filename));
134 $cfg->{order
}->{$defaults_section} = -1; # write as first section
135 my $raw = $class->write_config($filename, $cfg);
137 file_set_contents
($filename, $raw, 0600);
141 my ($class, $cfg) = @_;
143 $cfg->{ids
}->{$defaults_section} //= {};
145 return $cfg->{ids
}->{$defaults_section};
149 my ($class, $cfg, $name, $noerr) = @_;
151 my $data = $cfg->{ids
}->{$name};
153 return $data if $noerr || defined($data);
155 die "unknown remote \"$name\"\n";
159 my ($class, $cfg, $remote) = @_;
161 my $section = $class->lookup_remote($cfg, $remote);
164 my ($ticket_or_password) = @_;
166 if (!defined($ticket_or_password)) {
167 $ticket_or_password = PVE::APIClient::PTY::read_password("Remote password
: ")
171 username => $section->{username},
172 password => $ticket_or_password,
173 host => $section->{host},
174 port => $section->{port} // 8006,
175 cached_fingerprints => {
176 $section->{fingerprint} => 1,
180 my $conn = PVE::APIClient::LWP->new(%$setup);
187 my $password = $section->{password};
191 if (defined($password)) {
192 $conn = $trylogin->($password);
195 if (my $ticket = PVE::APIClient::Helpers::ticket_cache_lookup($remote)) {
196 eval { $conn = $trylogin->($ticket); };
198 PVE::APIClient::Helpers::ticket_cache_update($remote, undef);
199 if (ref($err) && (ref($err) eq 'PVE::APIClient::Exception') && ($err->{code} == 401)) {
200 $conn = $trylogin->();
206 $conn = $trylogin->();
210 PVE::APIClient::Helpers::ticket_cache_update($remote, $conn->{ticket});
215 package PVE::APIClient::RemoteConfig;
220 use PVE::APIClient::JSONSchema qw(register_standard_option get_standard_option);
221 use PVE
::APIClient
::SectionConfig
;
223 use base
qw( PVE::APIClient::Config);
231 name
=> get_standard_option
('pveclient-remote-name'),
233 description
=> "The host.",
234 type
=> 'string', format
=> 'address',
238 description
=> "The username.",
243 description
=> "The users password.",
248 description
=> "The port.",
254 description
=> "Fingerprint.",
259 description
=> "Description.",
269 name
=> { optional
=> 0 },
270 host
=> { optional
=> 0 },
271 comment
=> { optional
=> 1 },
272 username
=> { optional
=> 0 },
273 password
=> { optional
=> 1 },
274 port
=> { optional
=> 1 },
275 fingerprint
=> { optional
=> 1 },
279 __PACKAGE__-
>register();
282 package PVE
::APIClient
::DefaultsConfig
;
287 use PVE
::APIClient
::JSONSchema
qw(register_standard_option get_standard_option);
289 use base
qw( PVE::APIClient::Config);
298 name
=> { optional
=> 1 },
299 username
=> { optional
=> 1 },
300 port
=> { optional
=> 1 },
304 __PACKAGE__-
>register();
307 PVE
::APIClient
::Config-
>init();