]>
git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Config.pm
40caed86f92f3869a79be0ebfc78b14df48c6ff0
1 package PVE
::APIClient
::Config
;
8 use PVE
::JSONSchema
qw(register_standard_option get_standard_option);
9 use PVE
::Tools
qw(file_get_contents file_set_contents);
11 my $complete_remote_name = sub {
13 my $config = PVE
::APIClient
::Config-
>new();
14 return $config->remote_names;
17 register_standard_option
('pveclient-remote-name', {
18 description
=> "The name of the remote.",
21 completion
=> $complete_remote_name,
28 file
=> File
::HomeDir
::home
() . '/.pveclient',
30 bless $self => $class;
40 if (-e
$self->{file
}) {
41 my $filemode = (stat($self->{file
}))[2] & 07777;
42 if ($filemode != 0600) {
43 die sprintf "wrong permissions on '$self->{file}' %04o (expected 0600)\n", $filemode;
46 my $contents = file_get_contents
($self->{file
});
47 $self->{data
} = from_json
($contents);
52 if (!exists($self->{data
}->{remotes
})) {
53 $self->{data
}->{remotes
} = {};
57 for my $name (@{$self->remote_names}) {
58 my $cfg = $self->{data
}->{remotes
}->{$name};
60 foreach my $opt (qw(host port username fingerprint)) {
61 die "missing option '$opt' (remote '$name')" if !defined($cfg->{$opt});
69 my $contents = to_json
($self->{data
}, {pretty
=> 1, canonical
=> 1});
70 file_set_contents
($self->{file
}, $contents, 0600);
74 my ($self, $name, $host, $port, $fingerprint, $username, $password) = @_;
76 $self->{data
}->{remotes
}->{$name} = {
79 fingerprint
=> $fingerprint,
80 username
=> $username,
83 if (defined($password)) {
84 $self->{data
}->{remotes
}->{$name}->{password
} = $password;
91 return [keys %{$self->{data
}->{remotes
}}];
95 my ($self, $name) = @_;
97 die "Unknown remote \"$name\" given"
98 if (!exists($self->{data}->{remotes}->{$name}));
100 return $self->{data}->{remotes}->{$name};
108 # Remove the password from each remote.
109 for my $name ($self->remote_names) {
110 my $cfg = $self->{data}->{remotes}->{$name};
112 host => $cfg->{host},
113 port => $cfg->{port},
114 username => $cfg->{username},
115 fingerprint => $cfg->{fingerprint},
123 my ($self, $remote) = @_;
125 $self->lookup_remote($remote);
127 delete($self->{data}->{remotes}->{$remote});
133 my ($self, $remote) = @_;
135 my $section = $self->lookup_remote($remote);
136 my $conn = PVE::APIClient::LWP->new(
137 username => $section->{username},
138 password => $section->{password},
139 host => $section->{host},
140 port => $section->{port},
141 cached_fingerprints => {
142 $section->{fingerprint} => 1,