]>
git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Config.pm
1 package PVE
::APIClient
::Config
;
8 use PVE
::SectionConfig
;
9 use PVE
::Tools
qw(file_get_contents file_set_contents);
11 use base
qw(PVE::SectionConfig);
13 my $remote_namne_regex = qw(\w+);
15 my $defaults_section = '!DEFAULTS';
17 my $complete_remote_name = sub {
19 my $config = PVE
::APIClient
::Config-
>load();
21 foreach my $name (keys %{$config->{ids
}}) {
22 push @$list, $name if $name ne $defaults_section;
27 PVE
::JSONSchema
::register_standard_option
('pveclient-remote-name', {
28 description
=> "The name of the remote.",
30 pattern
=> $remote_namne_regex,
31 completion
=> $complete_remote_name,
37 description
=> "Section type.",
50 my $home = $ENV{HOME
};
52 die "environment HOME not set\n" if !defined($home);
54 return "$home/.pveclient";
57 sub format_section_header
{
58 my ($class, $type, $sectionId, $scfg, $done_hash) = @_;
60 if ($type eq 'defaults') {
63 return "$type: $sectionId\n";
67 sub parse_section_header
{
68 my ($class, $line) = @_;
70 if ($line =~ m/^defaults:\s*$/) {
71 return ('defaults', $defaults_section, undef, {});
72 } elsif ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
73 my ($type, $name) = (lc($1), $2);
75 die "invalid remote name '$name'\n"
76 if $name eq $defaults_section || $name !~ m/^$remote_namne_regex$/;
78 return ($type, $name, $@, {});
86 my $filename = $class->config_filename();
91 my $filemode = (stat($filename))[2] & 07777;
92 if ($filemode != 0600) {
93 die sprintf "wrong permissions on '$filename' %04o (expected 0600)\n", $filemode;
96 $raw = file_get_contents
($filename);
99 return $class->parse_config($filename, $raw);
103 my ($class, $cfg) = @_;
105 my $filename = $class->config_filename();
107 $cfg->{order
}->{$defaults_section} = -1; # write as first section
108 my $raw = $class->write_config($filename, $cfg);
110 file_set_contents
($filename, $raw, 0600);
114 my ($class, $cfg) = @_;
116 $cfg->{ids
}->{$defaults_section} //= {};
118 return $cfg->{ids
}->{$defaults_section};
122 my ($class, $cfg, $name, $noerr) = @_;
124 my $data = $cfg->{ids
}->{$name};
126 return $data if $noerr || defined($data);
128 die "unknown remote \"$name\"\n";
132 my ($class, $cfg, $remote) = @_;
134 my $section = $class->lookup_remote($cfg, $remote);
136 my $password = $section->{password};
137 if (!defined($password)) {
138 $password = PVE::PTY::read_password("Remote password
: ")
141 my $conn = PVE::APIClient::LWP->new(
142 username => $section->{username},
143 password => $password,
144 host => $section->{host},
145 port => $section->{port} // 8006,
146 cached_fingerprints => {
147 $section->{fingerprint} => 1,
156 package PVE::APIClient::RemoteConfig;
161 use PVE::JSONSchema qw(register_standard_option get_standard_option);
162 use PVE
::SectionConfig
;
164 use base
qw( PVE::APIClient::Config);
172 name
=> get_standard_option
('pveclient-remote-name'),
174 description
=> "The host.",
175 type
=> 'string', format
=> 'address',
179 description
=> "The username.",
184 description
=> "The users password.",
189 description
=> "The port.",
195 description
=> "Fingerprint.",
200 description
=> "Description.",
210 name
=> { optional
=> 0 },
211 host
=> { optional
=> 0 },
212 comment
=> { optional
=> 1 },
213 username
=> { optional
=> 0 },
214 password
=> { optional
=> 1 },
215 port
=> { optional
=> 1 },
216 fingerprint
=> { optional
=> 1 },
220 __PACKAGE__-
>register();
223 package PVE
::APIClient
::DefaultsConfig
;
228 use PVE
::JSONSchema
qw(register_standard_option get_standard_option);
230 use base
qw( PVE::APIClient::Config);
239 name
=> { optional
=> 1 },
240 username
=> { optional
=> 1 },
241 port
=> { optional
=> 1 },
245 __PACKAGE__-
>register();
248 PVE
::APIClient
::Config-
>init();