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