]>
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-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);
148 my $password = $section->{password};
149 if (!defined($password)) {
150 $password = PVE::APIClient::PTY::read_password("Remote password
: ")
153 my $conn = PVE::APIClient::LWP->new(
154 username => $section->{username},
155 password => $password,
156 host => $section->{host},
157 port => $section->{port} // 8006,
158 cached_fingerprints => {
159 $section->{fingerprint} => 1,
168 package PVE::APIClient::RemoteConfig;
173 use PVE::APIClient::JSONSchema qw(register_standard_option get_standard_option);
174 use PVE
::APIClient
::SectionConfig
;
176 use base
qw( PVE::APIClient::Config);
184 name
=> get_standard_option
('pveclient-remote-name'),
186 description
=> "The host.",
187 type
=> 'string', format
=> 'address',
191 description
=> "The username.",
196 description
=> "The users password.",
201 description
=> "The port.",
207 description
=> "Fingerprint.",
212 description
=> "Description.",
222 name
=> { optional
=> 0 },
223 host
=> { optional
=> 0 },
224 comment
=> { optional
=> 1 },
225 username
=> { optional
=> 0 },
226 password
=> { optional
=> 1 },
227 port
=> { optional
=> 1 },
228 fingerprint
=> { optional
=> 1 },
232 __PACKAGE__-
>register();
235 package PVE
::APIClient
::DefaultsConfig
;
240 use PVE
::APIClient
::JSONSchema
qw(register_standard_option get_standard_option);
242 use base
qw( PVE::APIClient::Config);
251 name
=> { optional
=> 1 },
252 username
=> { optional
=> 1 },
253 port
=> { optional
=> 1 },
257 __PACKAGE__-
>register();
260 PVE
::APIClient
::Config-
>init();