]>
Commit | Line | Data |
---|---|---|
a31c7b27 DM |
1 | package PVE::APIClient::Config; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use JSON; | |
a31c7b27 | 6 | |
06039c7f RJ |
7 | use File::HomeDir (); |
8 | use PVE::Tools qw(file_get_contents file_set_contents); | |
a31c7b27 | 9 | |
06039c7f RJ |
10 | sub new { |
11 | my ($class) = @_; | |
a31c7b27 | 12 | |
06039c7f RJ |
13 | my $self = { |
14 | file => File::HomeDir::home() . '/.pveclient', | |
15 | }; | |
16 | bless $self => $class; | |
a31c7b27 | 17 | |
06039c7f RJ |
18 | $self->load(); |
19 | ||
20 | return $self; | |
21 | } | |
22 | ||
23 | sub load { | |
24 | my ($self) = @_; | |
25 | ||
26 | if (-e $self->{file}) { | |
27 | my $filemode = (stat($self->{file}))[2] & 07777; | |
28 | if ($filemode != 0600) { | |
29 | die sprintf "wrong permissions on '$self->{file}' %04o (expected 0600)\n", $filemode; | |
30 | } | |
31 | ||
32 | my $contents = file_get_contents($self->{file}); | |
33 | $self->{data} = from_json($contents); | |
34 | } else { | |
35 | $self->{data} = {}; | |
36 | } | |
37 | ||
38 | if (!exists($self->{data}->{remotes})) { | |
39 | $self->{data}->{remotes} = {}; | |
a31c7b27 DM |
40 | } |
41 | ||
06039c7f RJ |
42 | # Verify config |
43 | for my $name (@{$self->remote_names}) { | |
44 | my $cfg = $self->{data}->{remotes}->{$name}; | |
45 | ||
46 | foreach my $opt (qw(host port username fingerprint)) { | |
47 | die "missing option '$opt' (remote '$name')" if !defined($cfg->{$opt}); | |
48 | } | |
49 | } | |
aa570b38 DM |
50 | } |
51 | ||
06039c7f RJ |
52 | sub save { |
53 | my ($self) = @_; | |
54 | ||
55 | my $contents = to_json($self->{data}, {pretty => 1, canonical => 1}); | |
56 | file_set_contents($self->{file}, $contents, 0600); | |
57 | } | |
aa570b38 | 58 | |
06039c7f RJ |
59 | sub add_remote { |
60 | my ($self, $name, $host, $port, $fingerprint, $username, $password) = @_; | |
aa570b38 | 61 | |
06039c7f RJ |
62 | $self->{data}->{remotes}->{$name} = { |
63 | host => $host, | |
64 | port => $port, | |
65 | fingerprint => $fingerprint, | |
66 | username => $username, | |
67 | }; | |
aa570b38 | 68 | |
06039c7f RJ |
69 | if (defined($password)) { |
70 | $self->{data}->{remotes}->{$name}->{password} = $password; | |
aa570b38 | 71 | } |
06039c7f | 72 | } |
aa570b38 | 73 | |
06039c7f RJ |
74 | sub remote_names { |
75 | my ($self) = @_; | |
76 | ||
77 | return [keys %{$self->{data}->{remotes}}]; | |
aa570b38 DM |
78 | } |
79 | ||
06039c7f RJ |
80 | sub lookup_remote { |
81 | my ($self, $name) = @_; | |
82 | ||
83 | die "Unknown remote \"$name\" given" | |
84 | if (!exists($self->{data}->{remotes}->{$name})); | |
85 | ||
86 | return $self->{data}->{remotes}->{$name}; | |
87 | } | |
88 | ||
89 | sub remotes { | |
90 | my ($self) = @_; | |
91 | ||
92 | my $res = {}; | |
93 | ||
94 | # Remove the password from each remote. | |
95 | for my $name ($self->remote_names) { | |
96 | my $cfg = $self->{data}->{remotes}->{$name}; | |
97 | $res->{$name} = { | |
98 | host => $cfg->{host}, | |
99 | port => $cfg->{port}, | |
100 | username => $cfg->{username}, | |
101 | fingerprint => $cfg->{fingerprint}, | |
102 | }; | |
103 | } | |
104 | ||
105 | return $res; | |
106 | } | |
107 | ||
108 | sub remove_remote { | |
109 | my ($self, $remote) = @_; | |
110 | ||
111 | $self->lookup_remote($remote); | |
112 | ||
113 | delete($self->{data}->{remotes}->{$remote}); | |
114 | ||
115 | $self->save(); | |
116 | } | |
117 | ||
118 | sub remote_conn { | |
119 | my ($self, $remote) = @_; | |
120 | ||
121 | my $section = $self->lookup_remote($remote); | |
122 | my $conn = PVE::APIClient::LWP->new( | |
123 | username => $section->{username}, | |
124 | password => $section->{password}, | |
125 | host => $section->{host}, | |
126 | port => $section->{port}, | |
127 | cached_fingerprints => { | |
128 | $section->{fingerprint} => 1, | |
129 | } | |
130 | ); | |
aa570b38 | 131 | |
06039c7f | 132 | $conn->login; |
aa570b38 | 133 | |
06039c7f | 134 | return $conn; |
aa570b38 | 135 | } |
a31c7b27 DM |
136 | |
137 | 1; |