]>
git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Config.pm
3878425dfff62ea1eecf558889185f7e25463c2e
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
();
67 sub format_section_header
{
68 my ($class, $type, $sectionId, $scfg, $done_hash) = @_;
70 if ($type eq 'defaults') {
73 return "$type: $sectionId\n";
77 sub parse_section_header
{
78 my ($class, $line) = @_;
80 if ($line =~ m/^defaults:\s*$/) {
81 return ('defaults', $defaults_section, undef, {});
82 } elsif ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
83 my ($type, $name) = (lc($1), $2);
85 die "invalid remote name '$name'\n"
86 if $name eq $defaults_section || $name !~ m/^$remote_namne_regex$/;
88 return ($type, $name, $@, {});
96 my $filename = $class->config_filename();
101 my $filemode = (stat($filename))[2] & 07777;
102 if ($filemode != 0600) {
103 die sprintf "wrong permissions on '$filename' %04o (expected 0600)\n", $filemode;
106 $raw = file_get_contents
($filename);
109 return $class->parse_config($filename, $raw);
113 my ($class, $cfg) = @_;
115 my $filename = $class->config_filename();
117 make_path
(dirname
($filename));
119 $cfg->{order
}->{$defaults_section} = -1; # write as first section
120 my $raw = $class->write_config($filename, $cfg);
122 file_set_contents
($filename, $raw, 0600);
126 my ($class, $cfg) = @_;
128 $cfg->{ids
}->{$defaults_section} //= {};
130 return $cfg->{ids
}->{$defaults_section};
134 my ($class, $cfg, $name, $noerr) = @_;
136 my $data = $cfg->{ids
}->{$name};
138 return $data if $noerr || defined($data);
140 die "unknown remote \"$name\"\n";
144 my ($class, $cfg, $remote) = @_;
146 my $section = $class->lookup_remote($cfg, $remote);
149 my ($ticket_or_password) = @_;
151 if (!defined($ticket_or_password)) {
152 $ticket_or_password = PVE::APIClient::PTY::read_password("Remote password
: ")
156 username => $section->{username},
157 password => $ticket_or_password,
158 host => $section->{host},
159 port => $section->{port} // 8006,
160 cached_fingerprints => {
161 $section->{fingerprint} => 1,
165 my $conn = PVE::APIClient::LWP->new(%$setup);
172 my $password = $section->{password};
176 if (defined($password)) {
177 $conn = $trylogin->($password);
180 if (my $ticket = PVE::APIClient::Helpers::ticket_cache_lookup($remote)) {
181 eval { $conn = $trylogin->($ticket); };
183 PVE::APIClient::Helpers::ticket_cache_update($remote, undef);
184 if (ref($err) && (ref($err) eq 'PVE::APIClient::Exception') && ($err->{code} == 401)) {
185 $conn = $trylogin->();
191 $conn = $trylogin->();
195 PVE::APIClient::Helpers::ticket_cache_update($remote, $conn->{ticket});
200 package PVE::APIClient::RemoteConfig;
205 use PVE::APIClient::JSONSchema qw(register_standard_option get_standard_option);
206 use PVE
::APIClient
::SectionConfig
;
208 use base
qw( PVE::APIClient::Config);
216 name
=> get_standard_option
('pveclient-remote-name'),
218 description
=> "The host.",
219 type
=> 'string', format
=> 'address',
223 description
=> "The username.",
228 description
=> "The users password.",
233 description
=> "The port.",
239 description
=> "Fingerprint.",
244 description
=> "Description.",
254 name
=> { optional
=> 0 },
255 host
=> { optional
=> 0 },
256 comment
=> { optional
=> 1 },
257 username
=> { optional
=> 0 },
258 password
=> { optional
=> 1 },
259 port
=> { optional
=> 1 },
260 fingerprint
=> { optional
=> 1 },
264 __PACKAGE__-
>register();
267 package PVE
::APIClient
::DefaultsConfig
;
272 use PVE
::APIClient
::JSONSchema
qw(register_standard_option get_standard_option);
274 use base
qw( PVE::APIClient::Config);
283 name
=> { optional
=> 1 },
284 username
=> { optional
=> 1 },
285 port
=> { optional
=> 1 },
289 __PACKAGE__-
>register();
292 PVE
::APIClient
::Config-
>init();