]> git.proxmox.com Git - pve-client.git/blame - PVE/APIClient/Config.pm
CLIHandler: copied updates from pve-common
[pve-client.git] / PVE / APIClient / Config.pm
CommitLineData
a31c7b27
DM
1package PVE::APIClient::Config;
2
3use strict;
4use warnings;
5use JSON;
a31c7b27 6
184877d4 7use PVE::JSONSchema qw(register_standard_option get_standard_option);
69aa81b3 8use PVE::SectionConfig;
06039c7f 9use PVE::Tools qw(file_get_contents file_set_contents);
a31c7b27 10
69aa81b3
DM
11use base qw(PVE::SectionConfig);
12
184877d4
DM
13my $complete_remote_name = sub {
14
69aa81b3
DM
15 my $config = PVE::APIClient::Config->load();
16 return [keys %{$config->{ids}}];
184877d4
DM
17};
18
19register_standard_option('pveclient-remote-name', {
20 description => "The name of the remote.",
21 type => 'string',
22 pattern => qr(\w+),
23 completion => $complete_remote_name,
24});
25
a31c7b27 26
69aa81b3
DM
27my $defaultData = {
28 propertyList => {
29 type => {
30 description => "Section type.",
31 optional => 1,
32 },
33 name => get_standard_option('pveclient-remote-name'),
34 host => {
35 description => "The host.",
36 type => 'string', format => 'address',
37 optional => 1,
38 },
39 username => {
40 description => "The username.",
41 type => 'string',
42 optional => 1,
43 },
44 password => {
45 description => "The users password.",
46 type => 'string',
47 optional => 1,
48 },
49 port => {
50 description => "The port.",
51 type => 'integer',
52 optional => 1,
53 default => 8006,
54 },
55 fingerprint => {
56 description => "Fingerprint.",
57 type => 'string',
58 optional => 1,
59 },
60 comment => {
61 description => "Description.",
62 type => 'string',
63 optional => 1,
64 maxLength => 4096,
65 },
66 },
67};
06039c7f 68
69aa81b3
DM
69sub type {
70 return 'remote';
06039c7f
RJ
71}
72
69aa81b3
DM
73sub options {
74 return {
75 name => { optional => 0 },
76 host => { optional => 0 },
77 comment => { optional => 1 },
78 username => { optional => 0 },
8842464b 79 password => { optional => 1 },
69aa81b3
DM
80 port => { optional => 1 },
81 fingerprint => { optional => 1 },
82 };
aa570b38
DM
83}
84
69aa81b3
DM
85sub private {
86 return $defaultData;
06039c7f 87}
aa570b38 88
69aa81b3
DM
89sub config_filename {
90 my ($class) = @_;
aa570b38 91
aa5833d6
DM
92 my $home = $ENV{HOME};
93
94 die "environment HOME not set\n" if !defined($home);
95
96 return "$home/.pveclient";
06039c7f 97}
aa570b38 98
69aa81b3
DM
99sub load {
100 my ($class) = @_;
06039c7f 101
69aa81b3 102 my $filename = $class->config_filename();
aa570b38 103
69aa81b3 104 my $raw = '';
06039c7f 105
69aa81b3
DM
106 if (-e $filename) {
107 my $filemode = (stat($filename))[2] & 07777;
108 if ($filemode != 0600) {
109 die sprintf "wrong permissions on '$filename' %04o (expected 0600)\n", $filemode;
110 }
06039c7f 111
69aa81b3
DM
112 $raw = file_get_contents($filename);
113 }
06039c7f 114
69aa81b3
DM
115 return $class->parse_config($filename, $raw);
116}
06039c7f 117
69aa81b3
DM
118sub save {
119 my ($class, $cfg) = @_;
06039c7f 120
69aa81b3
DM
121 my $filename = $class->config_filename();
122 my $raw = $class->write_config($filename, $cfg);
06039c7f 123
69aa81b3 124 file_set_contents($filename, $raw, 0600);
06039c7f
RJ
125}
126
69aa81b3
DM
127sub lookup_remote {
128 my ($class, $cfg, $name, $noerr) = @_;
06039c7f 129
69aa81b3 130 my $data = $cfg->{ids}->{$name};
06039c7f 131
69aa81b3 132 return $data if $noerr || defined($data);
06039c7f 133
69aa81b3 134 die "unknown remote \"$name\"\n";
06039c7f
RJ
135}
136
137sub remote_conn {
69aa81b3 138 my ($class, $cfg, $remote) = @_;
06039c7f 139
69aa81b3 140 my $section = $class->lookup_remote($cfg, $remote);
8842464b
DM
141
142 my $password = $section->{password};
143 if (!defined($password)) {
144 $password = PVE::PTY::read_password("Remote password: ")
145 }
146
06039c7f
RJ
147 my $conn = PVE::APIClient::LWP->new(
148 username => $section->{username},
8842464b 149 password => $password,
06039c7f 150 host => $section->{host},
8842464b 151 port => $section->{port} // 8006,
06039c7f
RJ
152 cached_fingerprints => {
153 $section->{fingerprint} => 1,
154 }
155 );
aa570b38 156
06039c7f 157 $conn->login;
aa570b38 158
06039c7f 159 return $conn;
aa570b38 160}
a31c7b27 161
69aa81b3
DM
162__PACKAGE__->register();
163__PACKAGE__->init();
164
a31c7b27 1651;