]> git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Config.pm
implement basic websocket protocol
[pve-client.git] / PVE / APIClient / Config.pm
1 package PVE::APIClient::Config;
2
3 use strict;
4 use warnings;
5 use JSON;
6 use File::HomeDir;
7
8 use PVE::Tools;
9 use PVE::APIClient::LWP;
10
11 sub load_config {
12
13 my $filename = home() . '/.pveclient';
14 my $conf_str = PVE::Tools::file_get_contents($filename);
15
16 my $filemode = (stat($filename))[2] & 07777;
17 if ($filemode != 0600) {
18 die sprintf "wrong permissions on '$filename' %04o (expected 0600)\n", $filemode;
19 }
20
21 return decode_json($conf_str);
22 }
23
24 sub load_remote_config {
25 my ($remote) = @_;
26
27 my $conf = load_config();
28
29 my $remote_conf = $conf->{"remote_$remote"} ||
30 die "no such remote '$remote'\n";
31
32 foreach my $opt (qw(hostname username password fingerprint)) {
33 die "missing option '$opt' (remote '$remote')" if !defined($remote_conf->{$opt});
34 }
35
36 return $remote_conf;
37 }
38
39 sub get_remote_connection {
40 my ($remote) = @_;
41
42 my $conf = load_remote_config($remote);
43
44 return PVE::APIClient::LWP->new(
45 username => $conf->{username},
46 password => $conf->{password},
47 host => $conf->{hostname},
48 cached_fingerprints => {
49 $conf->{fingerprint} => 1
50 });
51 }
52
53 1;