]>
Commit | Line | Data |
---|---|---|
29505e2c DM |
1 | #!/usr/bin/perl |
2 | ||
b133a905 DM |
3 | package PVE::CLI::pveclient; |
4 | ||
29505e2c DM |
5 | use strict; |
6 | use warnings; | |
f5fcd826 | 7 | use Cwd 'abs_path'; |
29505e2c DM |
8 | use Data::Dumper; |
9 | ||
c9138c03 | 10 | use PVE::APIClient::JSONSchema qw(register_standard_option get_standard_option); |
5101e472 | 11 | use PVE::APIClient::CLIFormatter; |
c9138c03 | 12 | use PVE::APIClient::CLIHandler; |
4a7e63b8 | 13 | use PVE::APIClient::PTY; |
565bbc73 | 14 | |
29505e2c DM |
15 | use PVE::APIClient::LWP; |
16 | use PVE::APIClient::Helpers; | |
b0495d82 | 17 | use PVE::APIClient::Config; |
a6dab5b8 | 18 | use PVE::APIClient::Commands::config; |
565bbc73 | 19 | use PVE::APIClient::Commands::remote; |
5b090843 | 20 | use PVE::APIClient::Commands::list; |
adf285bf | 21 | use PVE::APIClient::Commands::lxc; |
89335fb1 | 22 | use PVE::APIClient::Commands::GuestStatus; |
565bbc73 | 23 | |
29505e2c DM |
24 | use JSON; |
25 | ||
61ad3df5 DM |
26 | sub call_api_method { |
27 | my ($method, $param) = @_; | |
29505e2c | 28 | |
61ad3df5 | 29 | my $path = PVE::APIClient::Tools::extract_param($param, 'api_path'); |
29505e2c DM |
30 | die "missing API path\n" if !defined($path); |
31 | ||
61ad3df5 DM |
32 | my $remote = PVE::APIClient::Tools::extract_param($param, 'remote'); |
33 | die "missing remote\n" if !defined($remote); | |
34 | ||
35 | my $format = PVE::APIClient::Tools::extract_param($param, 'format'); | |
36 | PVE::APIClient::Helpers::set_output_format($format); | |
37 | ||
b0495d82 DM |
38 | my $config = PVE::APIClient::Config->load(); |
39 | ||
520f543e DM |
40 | my $uri_param = {}; |
41 | my $info = PVE::APIClient::Helpers::find_method_info($path, $method, $uri_param); | |
29505e2c | 42 | |
b0495d82 DM |
43 | my $conn = PVE::APIClient::Config->remote_conn($config, $remote); |
44 | ||
45 | my $res = $conn->call($method, "api2/json/$path", $param); | |
46 | die "undefined result" if !defined($res); | |
47 | die "undefined result data" if !exists($res->{data}); | |
48 | ||
49 | return $res->{data}; | |
29505e2c DM |
50 | } |
51 | ||
c9138c03 | 52 | use base qw(PVE::APIClient::CLIHandler); |
3a1bc22f | 53 | |
4a7e63b8 | 54 | sub read_password { |
4fbacba6 | 55 | return PVE::APIClient::PTY::read_password("Remote password: ") |
4a7e63b8 DM |
56 | } |
57 | ||
58 | ||
b133a905 | 59 | my $cmd = $ARGV[0]; |
dce6ecbc | 60 | |
b133a905 | 61 | if ($cmd && $cmd eq 'packagedepends') { |
f5fcd826 DM |
62 | # experimental code to print required perl packages |
63 | my $packages = {}; | |
64 | my $dir = Cwd::getcwd; | |
65 | ||
66 | foreach my $k (keys %INC) { | |
67 | my $file = abs_path($INC{$k}); | |
68 | next if $file =~ m/^\Q$dir\E/; | |
69 | my $res = `dpkg -S '$file'`; | |
70 | if ($res && $res =~ m/^(\S+): $file$/) { | |
71 | my $debian_package = $1; | |
72 | $debian_package =~ s/:amd64$//; | |
73 | $packages->{$debian_package} = 1; | |
74 | } else { | |
75 | die "unable to find package for '$file'\n"; | |
76 | } | |
77 | } | |
78 | print join("\n", sort(keys %$packages)) . "\n"; | |
b133a905 DM |
79 | |
80 | exit(0); | |
81 | } | |
82 | ||
83 | my $path_properties = {}; | |
84 | my $path_returns = { type => 'null' }; | |
85 | ||
55d17e7e DM |
86 | my $api_path_property = { |
87 | description => "API path.", | |
88 | type => 'string', | |
89 | completion => sub { | |
90 | my ($cmd, $pname, $cur, $args) = @_; | |
91 | return PVE::APIClient::Helpers::complete_api_path($cur); | |
92 | }, | |
93 | }; | |
94 | ||
b133a905 DM |
95 | # dynamically update schema definition for direct API call |
96 | # like: pveclient api <get|set|create|delete|help> <remote> <path> | |
520f543e DM |
97 | my $uri_param = {}; |
98 | if (my $info = PVE::APIClient::Helpers::extract_path_info($uri_param)) { | |
99 | foreach my $key (keys %{$info->{parameters}->{properties}}) { | |
100 | next if defined($uri_param->{$key}); | |
101 | $path_properties->{$key} = $info->{parameters}->{properties}->{$key}; | |
102 | } | |
b133a905 | 103 | $path_returns = $info->{returns}; |
29505e2c DM |
104 | } |
105 | ||
faa47c20 | 106 | $path_properties->{format} = get_standard_option('pve-output-format'), |
b133a905 | 107 | $path_properties->{remote} = get_standard_option('pveclient-remote-name'); |
55d17e7e | 108 | $path_properties->{api_path} = $api_path_property; |
61ad3df5 | 109 | |
b0495d82 | 110 | my $format_result = sub { |
61ad3df5 DM |
111 | my ($data) = @_; |
112 | ||
c27b93a5 | 113 | my $format = PVE::APIClient::Helpers::get_output_format(); |
5101e472 DM |
114 | |
115 | my $options = PVE::APIClient::CLIFormatter::query_terminal_options({}); | |
116 | ||
117 | PVE::APIClient::CLIFormatter::print_api_result($format, $data, $path_returns, undef, $options); | |
b0495d82 DM |
118 | }; |
119 | ||
b133a905 DM |
120 | __PACKAGE__->register_method ({ |
121 | name => 'pveclient_get', | |
122 | path => 'pveclient_get', | |
123 | method => 'GET', | |
4fbacba6 | 124 | description => "Call API GET on <api_path>.", |
b133a905 DM |
125 | parameters => { |
126 | additionalProperties => 0, | |
127 | properties => $path_properties, | |
128 | }, | |
129 | returns => $path_returns, | |
130 | code => sub { | |
131 | my ($param) = @_; | |
132 | ||
61ad3df5 | 133 | return call_api_method('GET', $param); |
b133a905 DM |
134 | }}); |
135 | ||
136 | __PACKAGE__->register_method ({ | |
137 | name => 'pveclient_set', | |
138 | path => 'pveclient_set', | |
139 | method => 'PUT', | |
4fbacba6 | 140 | description => "Call API PUT on <api_path>.", |
b133a905 DM |
141 | parameters => { |
142 | additionalProperties => 0, | |
143 | properties => $path_properties, | |
144 | }, | |
145 | returns => $path_returns, | |
146 | code => sub { | |
147 | my ($param) = @_; | |
148 | ||
61ad3df5 | 149 | return call_api_method('PUT', $param); |
b133a905 DM |
150 | }}); |
151 | ||
152 | __PACKAGE__->register_method ({ | |
153 | name => 'pveclient_create', | |
154 | path => 'pveclient_create', | |
faa47c20 DM |
155 | method => 'POST', |
156 | description => "Call API POST on <api_path>.", | |
b133a905 DM |
157 | parameters => { |
158 | additionalProperties => 0, | |
159 | properties => $path_properties, | |
160 | }, | |
161 | returns => $path_returns, | |
162 | code => sub { | |
163 | my ($param) = @_; | |
164 | ||
61ad3df5 | 165 | return call_api_method('PUSH', $param); |
b133a905 DM |
166 | }}); |
167 | ||
168 | __PACKAGE__->register_method ({ | |
169 | name => 'pveclient_delete', | |
170 | path => 'pveclient_delete', | |
171 | method => 'DELETE', | |
4fbacba6 | 172 | description => "Call API DELETE on <api_path>.", |
b133a905 DM |
173 | parameters => { |
174 | additionalProperties => 0, | |
175 | properties => $path_properties, | |
176 | }, | |
177 | returns => $path_returns, | |
178 | code => sub { | |
179 | my ($param) = @_; | |
180 | ||
61ad3df5 | 181 | return call_api_method('DELETE', $param); |
b133a905 DM |
182 | }}); |
183 | ||
55d17e7e DM |
184 | __PACKAGE__->register_method ({ |
185 | name => 'pveclient_usage', | |
186 | path => 'pveclient_usage', | |
187 | method => 'GET', | |
188 | description => "print API usage information for <api_path>.", | |
189 | parameters => { | |
190 | additionalProperties => 0, | |
191 | properties => { | |
192 | api_path => $api_path_property, | |
193 | verbose => { | |
194 | description => "Verbose output format.", | |
195 | type => 'boolean', | |
196 | optional => 1, | |
197 | }, | |
198 | returns => { | |
199 | description => "Including schema for returned data.", | |
200 | type => 'boolean', | |
201 | optional => 1, | |
202 | }, | |
203 | command => { | |
204 | description => "API command.", | |
205 | type => 'string', | |
206 | enum => [ keys %$PVE::APIClient::Helpers::method_map ], | |
207 | optional => 1, | |
208 | }, | |
209 | }, | |
210 | }, | |
211 | returns => { type => 'null' }, | |
212 | code => sub { | |
213 | my ($param) = @_; | |
214 | ||
215 | my $path = $param->{api_path}; | |
216 | ||
217 | my $found = 0; | |
218 | foreach my $cmd (qw(get set create delete)) { | |
219 | next if $param->{command} && $cmd ne $param->{command}; | |
220 | my $method = $PVE::APIClient::Helpers::method_map->{$cmd}; | |
221 | my $uri_param = {}; | |
222 | my $info = PVE::APIClient::Helpers::find_method_info($path, $method, $uri_param, 1); | |
223 | next if !$info; | |
224 | $found = 1; | |
225 | ||
226 | my $prefix = "pveclient api $cmd <remote> $path"; | |
227 | if ($param->{verbose}) { | |
228 | print PVE::APIClient::RESTHandler::getopt_usage( | |
229 | $info, $prefix, undef, $uri_param, 'full'); | |
230 | ||
231 | } else { | |
232 | print "USAGE: " . PVE::APIClient::RESTHandler::getopt_usage( | |
233 | $info, $prefix, undef, $uri_param, 'short'); | |
234 | } | |
235 | if ($param-> {returns}) { | |
236 | my $schema = to_json($info->{returns}, {utf8 => 1, canonical => 1, pretty => 1 }); | |
237 | print "RETURNS: $schema\n"; | |
238 | } | |
239 | } | |
240 | ||
241 | if (!$found) { | |
242 | if ($param->{command}) { | |
243 | die "no '$param->{command}' handler for '$path'\n"; | |
244 | } else { | |
245 | die "no such resource '$path'\n" | |
246 | } | |
247 | } | |
248 | ||
249 | return undef; | |
250 | }}); | |
b133a905 DM |
251 | |
252 | our $cmddef = { | |
253 | config => $PVE::APIClient::Commands::config::cmddef, | |
254 | list => $PVE::APIClient::Commands::list::cmddef, | |
255 | lxc => $PVE::APIClient::Commands::lxc::cmddef, | |
256 | remote => $PVE::APIClient::Commands::remote::cmddef, | |
4cedebf6 | 257 | |
42de9764 RJ |
258 | resume => [ 'PVE::APIClient::Commands::GuestStatus', 'resume', ['remote', 'vmid']], |
259 | shutdown => [ 'PVE::APIClient::Commands::GuestStatus', 'shutdown', ['remote', 'vmid']], | |
4cedebf6 | 260 | spice => [ 'PVE::APIClient::Commands::GuestStatus', 'spice', ['remote', 'vmid']], |
89335fb1 DM |
261 | start => [ 'PVE::APIClient::Commands::GuestStatus', 'start', ['remote', 'vmid']], |
262 | stop => [ 'PVE::APIClient::Commands::GuestStatus', 'stop', ['remote', 'vmid']], | |
42de9764 | 263 | suspend => [ 'PVE::APIClient::Commands::GuestStatus', 'suspend', ['remote', 'vmid']], |
b133a905 DM |
264 | |
265 | api => { | |
55d17e7e | 266 | usage => [ __PACKAGE__, 'pveclient_usage', ['api_path']], |
b0495d82 DM |
267 | get => [ __PACKAGE__, 'pveclient_get', ['remote', 'api_path'], {}, $format_result ], |
268 | set => [ __PACKAGE__, 'pveclient_set', ['remote', 'api_path'], {}, $format_result ], | |
269 | create => [ __PACKAGE__, 'pveclient_create', ['remote', 'api_path'], {}, $format_result ], | |
270 | delete => [ __PACKAGE__, 'pveclient_delete', ['remote', 'api_path'], {}, $format_result ], | |
271 | }, | |
b133a905 DM |
272 | }; |
273 | ||
274 | ||
2767c2b9 DM |
275 | if ($cmd && $cmd eq 'printsynopsis') { |
276 | ||
277 | print __PACKAGE__->generate_asciidoc_synopsis(); | |
278 | ||
279 | exit(0); | |
280 | } | |
281 | ||
b133a905 | 282 | __PACKAGE__->run_cli_handler(); |