]>
Commit | Line | Data |
---|---|---|
a31c7b27 DM |
1 | package PVE::APIClient::Config; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use JSON; | |
a31c7b27 | 6 | |
184877d4 | 7 | use PVE::JSONSchema qw(register_standard_option get_standard_option); |
69aa81b3 | 8 | use PVE::SectionConfig; |
06039c7f | 9 | use PVE::Tools qw(file_get_contents file_set_contents); |
a31c7b27 | 10 | |
69aa81b3 DM |
11 | use base qw(PVE::SectionConfig); |
12 | ||
184877d4 DM |
13 | my $complete_remote_name = sub { |
14 | ||
69aa81b3 DM |
15 | my $config = PVE::APIClient::Config->load(); |
16 | return [keys %{$config->{ids}}]; | |
184877d4 DM |
17 | }; |
18 | ||
19 | register_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 |
27 | my $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 |
69 | sub type { |
70 | return 'remote'; | |
06039c7f RJ |
71 | } |
72 | ||
69aa81b3 DM |
73 | sub options { |
74 | return { | |
75 | name => { optional => 0 }, | |
76 | host => { optional => 0 }, | |
77 | comment => { optional => 1 }, | |
78 | username => { optional => 0 }, | |
79 | password => { optional => 0 }, | |
80 | port => { optional => 1 }, | |
81 | fingerprint => { optional => 1 }, | |
82 | }; | |
aa570b38 DM |
83 | } |
84 | ||
69aa81b3 DM |
85 | sub private { |
86 | return $defaultData; | |
06039c7f | 87 | } |
aa570b38 | 88 | |
69aa81b3 DM |
89 | sub 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 |
99 | sub 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 |
118 | sub 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 |
127 | sub 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 | ||
137 | sub remote_conn { | |
69aa81b3 | 138 | my ($class, $cfg, $remote) = @_; |
06039c7f | 139 | |
69aa81b3 | 140 | my $section = $class->lookup_remote($cfg, $remote); |
06039c7f RJ |
141 | my $conn = PVE::APIClient::LWP->new( |
142 | username => $section->{username}, | |
143 | password => $section->{password}, | |
144 | host => $section->{host}, | |
145 | port => $section->{port}, | |
146 | cached_fingerprints => { | |
147 | $section->{fingerprint} => 1, | |
148 | } | |
149 | ); | |
aa570b38 | 150 | |
06039c7f | 151 | $conn->login; |
aa570b38 | 152 | |
06039c7f | 153 | return $conn; |
aa570b38 | 154 | } |
a31c7b27 | 155 | |
69aa81b3 DM |
156 | __PACKAGE__->register(); |
157 | __PACKAGE__->init(); | |
158 | ||
a31c7b27 | 159 | 1; |