]> git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/Config.pm
40caed86f92f3869a79be0ebfc78b14df48c6ff0
[pve-client.git] / PVE / APIClient / Config.pm
1 package PVE::APIClient::Config;
2
3 use strict;
4 use warnings;
5 use JSON;
6
7 use File::HomeDir ();
8 use PVE::JSONSchema qw(register_standard_option get_standard_option);
9 use PVE::Tools qw(file_get_contents file_set_contents);
10
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
24 sub new {
25 my ($class) = @_;
26
27 my $self = {
28 file => File::HomeDir::home() . '/.pveclient',
29 };
30 bless $self => $class;
31
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} = {};
54 }
55
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 }
64 }
65
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 }
72
73 sub add_remote {
74 my ($self, $name, $host, $port, $fingerprint, $username, $password) = @_;
75
76 $self->{data}->{remotes}->{$name} = {
77 host => $host,
78 port => $port,
79 fingerprint => $fingerprint,
80 username => $username,
81 };
82
83 if (defined($password)) {
84 $self->{data}->{remotes}->{$name}->{password} = $password;
85 }
86 }
87
88 sub remote_names {
89 my ($self) = @_;
90
91 return [keys %{$self->{data}->{remotes}}];
92 }
93
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 );
145
146 $conn->login;
147
148 return $conn;
149 }
150
151 1;