]>
Commit | Line | Data |
---|---|---|
1 | package PVE::CLIHandler; | |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use Data::Dumper; | |
6 | ||
7 | use PVE::SafeSyslog; | |
8 | use PVE::Exception qw(raise raise_param_exc); | |
9 | use PVE::RESTHandler; | |
10 | use PVE::PodParser; | |
11 | use PVE::INotify; | |
12 | ||
13 | use base qw(PVE::RESTHandler); | |
14 | ||
15 | my $cmddef; | |
16 | my $exename; | |
17 | my $cli_handler_class; | |
18 | ||
19 | my $expand_command_name = sub { | |
20 | my ($def, $cmd) = @_; | |
21 | ||
22 | if (!$def->{$cmd}) { | |
23 | my $expanded; | |
24 | for my $k (keys(%$def)) { | |
25 | if ($k =~ m/^$cmd/) { | |
26 | if ($expanded) { | |
27 | $expanded = undef; # more than one match | |
28 | last; | |
29 | } else { | |
30 | $expanded = $k; | |
31 | } | |
32 | } | |
33 | } | |
34 | $cmd = $expanded if $expanded; | |
35 | } | |
36 | return $cmd; | |
37 | }; | |
38 | ||
39 | my $complete_command_names = sub { | |
40 | my $res = []; | |
41 | ||
42 | return if ref($cmddef) ne 'HASH'; | |
43 | ||
44 | foreach my $cmd (keys %$cmddef) { | |
45 | next if $cmd eq 'help'; | |
46 | push @$res, $cmd; | |
47 | } | |
48 | ||
49 | return $res; | |
50 | }; | |
51 | ||
52 | __PACKAGE__->register_method ({ | |
53 | name => 'help', | |
54 | path => 'help', | |
55 | method => 'GET', | |
56 | description => "Get help about specified command.", | |
57 | parameters => { | |
58 | additionalProperties => 0, | |
59 | properties => { | |
60 | cmd => { | |
61 | description => "Command name", | |
62 | type => 'string', | |
63 | optional => 1, | |
64 | completion => $complete_command_names, | |
65 | }, | |
66 | verbose => { | |
67 | description => "Verbose output format.", | |
68 | type => 'boolean', | |
69 | optional => 1, | |
70 | }, | |
71 | }, | |
72 | }, | |
73 | returns => { type => 'null' }, | |
74 | ||
75 | code => sub { | |
76 | my ($param) = @_; | |
77 | ||
78 | die "not initialized" if !($cmddef && $exename && $cli_handler_class); | |
79 | ||
80 | my $cmd = $param->{cmd}; | |
81 | ||
82 | my $verbose = defined($cmd) && $cmd; | |
83 | $verbose = $param->{verbose} if defined($param->{verbose}); | |
84 | ||
85 | if (!$cmd) { | |
86 | if ($verbose) { | |
87 | print_usage_verbose(); | |
88 | } else { | |
89 | print_usage_short(\*STDOUT); | |
90 | } | |
91 | return undef; | |
92 | } | |
93 | ||
94 | $cmd = &$expand_command_name($cmddef, $cmd); | |
95 | ||
96 | my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd} || []}; | |
97 | ||
98 | raise_param_exc({ cmd => "no such command '$cmd'"}) if !$class; | |
99 | ||
100 | my $pwcallback = $cli_handler_class->can('read_password'); | |
101 | ||
102 | my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, | |
103 | $verbose ? 'full' : 'short', $pwcallback); | |
104 | if ($verbose) { | |
105 | print "$str\n"; | |
106 | } else { | |
107 | print "USAGE: $str\n"; | |
108 | } | |
109 | ||
110 | return undef; | |
111 | ||
112 | }}); | |
113 | ||
114 | sub print_simple_asciidoc_synopsys { | |
115 | my ($class, $name, $arg_param, $uri_param) = @_; | |
116 | ||
117 | die "not initialized" if !$cli_handler_class; | |
118 | ||
119 | my $pwcallback = $cli_handler_class->can('read_password'); | |
120 | ||
121 | my $synopsis = "*${name}* `help`\n\n"; | |
122 | ||
123 | $synopsis .= $class->usage_str($name, $name, $arg_param, $uri_param, 'asciidoc', $pwcallback); | |
124 | ||
125 | return $synopsis; | |
126 | } | |
127 | ||
128 | sub print_asciidoc_synopsys { | |
129 | ||
130 | die "not initialized" if !($cmddef && $exename && $cli_handler_class); | |
131 | ||
132 | my $pwcallback = $cli_handler_class->can('read_password'); | |
133 | ||
134 | my $synopsis = ""; | |
135 | ||
136 | $synopsis .= "*${exename}* `<COMMAND> [ARGS] [OPTIONS]`\n\n"; | |
137 | ||
138 | my $oldclass; | |
139 | foreach my $cmd (sort keys %$cmddef) { | |
140 | my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}}; | |
141 | my $str = $class->usage_str($name, "$exename $cmd", $arg_param, | |
142 | $uri_param, 'asciidoc', $pwcallback); | |
143 | $synopsis .= "\n" if $oldclass && $oldclass ne $class; | |
144 | ||
145 | $synopsis .= "$str\n\n"; | |
146 | $oldclass = $class; | |
147 | } | |
148 | ||
149 | $synopsis .= "\n"; | |
150 | ||
151 | return $synopsis; | |
152 | } | |
153 | ||
154 | sub print_simple_pod_manpage { | |
155 | my ($podfn, $class, $name, $arg_param, $uri_param) = @_; | |
156 | ||
157 | die "not initialized" if !$cli_handler_class; | |
158 | ||
159 | my $pwcallback = $cli_handler_class->can('read_password'); | |
160 | ||
161 | my $synopsis = " $name help\n\n"; | |
162 | my $str = $class->usage_str($name, $name, $arg_param, $uri_param, 'long', $pwcallback); | |
163 | $str =~ s/^USAGE://; | |
164 | $str =~ s/\n/\n /g; | |
165 | $synopsis .= $str; | |
166 | ||
167 | my $parser = PVE::PodParser->new(); | |
168 | $parser->{include}->{synopsis} = $synopsis; | |
169 | $parser->parse_from_file($podfn); | |
170 | } | |
171 | ||
172 | sub print_pod_manpage { | |
173 | my ($podfn) = @_; | |
174 | ||
175 | die "not initialized" if !($cmddef && $exename && $cli_handler_class); | |
176 | die "no pod file specified" if !$podfn; | |
177 | ||
178 | my $pwcallback = $cli_handler_class->can('read_password'); | |
179 | ||
180 | my $synopsis = ""; | |
181 | ||
182 | $synopsis .= " $exename <COMMAND> [ARGS] [OPTIONS]\n\n"; | |
183 | ||
184 | my $style = 'full'; # or should we use 'short'? | |
185 | my $oldclass; | |
186 | foreach my $cmd (sorted_commands()) { | |
187 | my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}}; | |
188 | my $str = $class->usage_str($name, "$exename $cmd", $arg_param, | |
189 | $uri_param, $style, $pwcallback); | |
190 | $str =~ s/^USAGE: //; | |
191 | ||
192 | $synopsis .= "\n" if $oldclass && $oldclass ne $class; | |
193 | $str =~ s/\n/\n /g; | |
194 | $synopsis .= " $str\n\n"; | |
195 | $oldclass = $class; | |
196 | } | |
197 | ||
198 | $synopsis .= "\n"; | |
199 | ||
200 | my $parser = PVE::PodParser->new(); | |
201 | $parser->{include}->{synopsis} = $synopsis; | |
202 | $parser->parse_from_file($podfn); | |
203 | } | |
204 | ||
205 | sub print_usage_verbose { | |
206 | ||
207 | die "not initialized" if !($cmddef && $exename && $cli_handler_class); | |
208 | ||
209 | my $pwcallback = $cli_handler_class->can('read_password'); | |
210 | ||
211 | print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n"; | |
212 | ||
213 | foreach my $cmd (sort keys %$cmddef) { | |
214 | my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}}; | |
215 | my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, | |
216 | 'full', $pwcallback); | |
217 | print "$str\n\n"; | |
218 | } | |
219 | } | |
220 | ||
221 | sub sorted_commands { | |
222 | return sort { ($cmddef->{$a}->[0] cmp $cmddef->{$b}->[0]) || ($a cmp $b)} keys %$cmddef; | |
223 | } | |
224 | ||
225 | sub print_usage_short { | |
226 | my ($fd, $msg) = @_; | |
227 | ||
228 | die "not initialized" if !($cmddef && $exename && $cli_handler_class); | |
229 | ||
230 | my $pwcallback = $cli_handler_class->can('read_password'); | |
231 | ||
232 | print $fd "ERROR: $msg\n" if $msg; | |
233 | print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n"; | |
234 | ||
235 | my $oldclass; | |
236 | foreach my $cmd (sorted_commands()) { | |
237 | my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}}; | |
238 | my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'short', $pwcallback); | |
239 | print $fd "\n" if $oldclass && $oldclass ne $class; | |
240 | print $fd " $str"; | |
241 | $oldclass = $class; | |
242 | } | |
243 | } | |
244 | ||
245 | my $print_bash_completion = sub { | |
246 | my ($cmddef, $simple_cmd, $bash_command, $cur, $prev) = @_; | |
247 | ||
248 | my $debug = 0; | |
249 | ||
250 | return if !(defined($cur) && defined($prev) && defined($bash_command)); | |
251 | return if !defined($ENV{COMP_LINE}); | |
252 | return if !defined($ENV{COMP_POINT}); | |
253 | ||
254 | my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT}); | |
255 | print STDERR "\nCMDLINE: $ENV{COMP_LINE}\n" if $debug; | |
256 | ||
257 | my $args = PVE::Tools::split_args($cmdline); | |
258 | my $pos = scalar(@$args) - 2; | |
259 | $pos += 1 if $cmdline =~ m/\s+$/; | |
260 | ||
261 | print STDERR "CMDLINE:$pos:$cmdline\n" if $debug; | |
262 | ||
263 | return if $pos < 0; | |
264 | ||
265 | my $print_result = sub { | |
266 | foreach my $p (@_) { | |
267 | print "$p\n" if $p =~ m/^$cur/; | |
268 | } | |
269 | }; | |
270 | ||
271 | my $cmd; | |
272 | if ($simple_cmd) { | |
273 | $cmd = $simple_cmd; | |
274 | } else { | |
275 | if ($pos == 0) { | |
276 | &$print_result(keys %$cmddef); | |
277 | return; | |
278 | } | |
279 | $cmd = $args->[1]; | |
280 | } | |
281 | ||
282 | my $def = $cmddef->{$cmd}; | |
283 | return if !$def; | |
284 | ||
285 | print STDERR "CMDLINE1:$pos:$cmdline\n" if $debug; | |
286 | ||
287 | my $skip_param = {}; | |
288 | ||
289 | my ($class, $name, $arg_param, $uri_param) = @$def; | |
290 | $arg_param //= []; | |
291 | $uri_param //= {}; | |
292 | ||
293 | $arg_param = [ $arg_param ] if !ref($arg_param); | |
294 | ||
295 | map { $skip_param->{$_} = 1; } @$arg_param; | |
296 | map { $skip_param->{$_} = 1; } keys %$uri_param; | |
297 | ||
298 | my $fpcount = scalar(@$arg_param); | |
299 | ||
300 | my $info = $class->map_method_by_name($name); | |
301 | ||
302 | my $schema = $info->{parameters}; | |
303 | my $prop = $schema->{properties}; | |
304 | ||
305 | my $print_parameter_completion = sub { | |
306 | my ($pname) = @_; | |
307 | my $d = $prop->{$pname}; | |
308 | if ($d->{completion}) { | |
309 | my $vt = ref($d->{completion}); | |
310 | if ($vt eq 'CODE') { | |
311 | my $res = $d->{completion}->($cmd, $pname, $cur, $args); | |
312 | &$print_result(@$res); | |
313 | } | |
314 | } elsif ($d->{type} eq 'boolean') { | |
315 | &$print_result('0', '1'); | |
316 | } elsif ($d->{enum}) { | |
317 | &$print_result(@{$d->{enum}}); | |
318 | } | |
319 | }; | |
320 | ||
321 | # positional arguments | |
322 | $pos += 1 if $simple_cmd; | |
323 | if ($fpcount && $pos <= $fpcount) { | |
324 | my $pname = $arg_param->[$pos -1]; | |
325 | &$print_parameter_completion($pname); | |
326 | return; | |
327 | } | |
328 | ||
329 | my @option_list = (); | |
330 | foreach my $key (keys %$prop) { | |
331 | next if $skip_param->{$key}; | |
332 | push @option_list, "--$key"; | |
333 | } | |
334 | ||
335 | if ($cur =~ m/^-/) { | |
336 | &$print_result(@option_list); | |
337 | return; | |
338 | } | |
339 | ||
340 | if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) { | |
341 | my $pname = $1; | |
342 | &$print_parameter_completion($pname); | |
343 | return; | |
344 | } | |
345 | ||
346 | &$print_result(@option_list); | |
347 | }; | |
348 | ||
349 | sub verify_api { | |
350 | my ($class) = @_; | |
351 | ||
352 | # simply verify all registered methods | |
353 | PVE::RESTHandler::validate_method_schemas(); | |
354 | } | |
355 | ||
356 | my $get_exe_name = sub { | |
357 | my ($class) = @_; | |
358 | ||
359 | my $name = $class; | |
360 | $name =~ s/^.*:://; | |
361 | $name =~ s/_/-/g; | |
362 | ||
363 | return $name; | |
364 | }; | |
365 | ||
366 | sub generate_bash_completions { | |
367 | my ($class) = @_; | |
368 | ||
369 | # generate bash completion config | |
370 | ||
371 | $exename = &$get_exe_name($class); | |
372 | ||
373 | print <<__EOD__; | |
374 | # $exename bash completion | |
375 | ||
376 | # see http://tiswww.case.edu/php/chet/bash/FAQ | |
377 | # and __ltrim_colon_completions() in /usr/share/bash-completion/bash_completion | |
378 | # this modifies global var, but I found no better way | |
379 | COMP_WORDBREAKS=\${COMP_WORDBREAKS//:} | |
380 | ||
381 | complete -o default -C '$exename bashcomplete' $exename | |
382 | __EOD__ | |
383 | } | |
384 | ||
385 | sub find_cli_class_source { | |
386 | my ($name) = @_; | |
387 | ||
388 | my $filename; | |
389 | ||
390 | $name =~ s/-/_/g; | |
391 | ||
392 | my $cpath = "PVE/CLI/${name}.pm"; | |
393 | my $spath = "PVE/Service/${name}.pm"; | |
394 | foreach my $p (@INC) { | |
395 | foreach my $s (($cpath, $spath)) { | |
396 | my $testfn = "$p/$s"; | |
397 | if (-f $testfn) { | |
398 | $filename = $testfn; | |
399 | last; | |
400 | } | |
401 | } | |
402 | last if defined($filename); | |
403 | } | |
404 | ||
405 | return $filename; | |
406 | } | |
407 | ||
408 | sub generate_pod_manpage { | |
409 | my ($class, $podfn) = @_; | |
410 | ||
411 | $cli_handler_class = $class; | |
412 | ||
413 | $exename = &$get_exe_name($class); | |
414 | ||
415 | $podfn = find_cli_class_source($exename) if !defined($podfn); | |
416 | ||
417 | die "unable to find source for class '$class'" if !$podfn; | |
418 | ||
419 | no strict 'refs'; | |
420 | my $def = ${"${class}::cmddef"}; | |
421 | ||
422 | if (ref($def) eq 'ARRAY') { | |
423 | print_simple_pod_manpage($podfn, @$def); | |
424 | } else { | |
425 | $cmddef = $def; | |
426 | ||
427 | $cmddef->{help} = [ __PACKAGE__, 'help', ['cmd'] ]; | |
428 | ||
429 | print_pod_manpage($podfn); | |
430 | } | |
431 | } | |
432 | ||
433 | sub generate_asciidoc_synopsys { | |
434 | my ($class) = @_; | |
435 | ||
436 | $cli_handler_class = $class; | |
437 | ||
438 | $exename = &$get_exe_name($class); | |
439 | ||
440 | no strict 'refs'; | |
441 | my $def = ${"${class}::cmddef"}; | |
442 | ||
443 | if (ref($def) eq 'ARRAY') { | |
444 | print_simple_asciidoc_synopsys(@$def); | |
445 | } else { | |
446 | $cmddef = $def; | |
447 | ||
448 | $cmddef->{help} = [ __PACKAGE__, 'help', ['cmd'] ]; | |
449 | ||
450 | print_asciidoc_synopsys(); | |
451 | } | |
452 | } | |
453 | ||
454 | my $handle_cmd = sub { | |
455 | my ($def, $cmdname, $cmd, $args, $pwcallback, $podfn, $preparefunc, $stringfilemap) = @_; | |
456 | ||
457 | $cmddef = $def; | |
458 | $exename = $cmdname; | |
459 | ||
460 | $cmddef->{help} = [ __PACKAGE__, 'help', ['cmd'] ]; | |
461 | ||
462 | if (!$cmd) { | |
463 | print_usage_short (\*STDERR, "no command specified"); | |
464 | exit (-1); | |
465 | } elsif ($cmd eq 'verifyapi') { | |
466 | PVE::RESTHandler::validate_method_schemas(); | |
467 | return; | |
468 | } elsif ($cmd eq 'printmanpod') { | |
469 | $podfn = find_cli_class_source($exename) if !defined($podfn); | |
470 | print_pod_manpage($podfn); | |
471 | return; | |
472 | } elsif ($cmd eq 'bashcomplete') { | |
473 | &$print_bash_completion($cmddef, 0, @$args); | |
474 | return; | |
475 | } | |
476 | ||
477 | &$preparefunc() if $preparefunc; | |
478 | ||
479 | $cmd = &$expand_command_name($cmddef, $cmd); | |
480 | ||
481 | my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef->{$cmd} || []}; | |
482 | ||
483 | if (!$class) { | |
484 | print_usage_short (\*STDERR, "unknown command '$cmd'"); | |
485 | exit (-1); | |
486 | } | |
487 | ||
488 | my $prefix = "$exename $cmd"; | |
489 | my $res = $class->cli_handler($prefix, $name, \@ARGV, $arg_param, $uri_param, $pwcallback, $stringfilemap); | |
490 | ||
491 | &$outsub($res) if $outsub; | |
492 | }; | |
493 | ||
494 | my $handle_simple_cmd = sub { | |
495 | my ($def, $args, $pwcallback, $podfn, $preparefunc, $stringfilemap) = @_; | |
496 | ||
497 | my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def}; | |
498 | die "no class specified" if !$class; | |
499 | ||
500 | if (scalar(@$args) >= 1) { | |
501 | if ($args->[0] eq 'help') { | |
502 | my $str = "USAGE: $name help\n"; | |
503 | $str .= $class->usage_str($name, $name, $arg_param, $uri_param, 'long', $pwcallback); | |
504 | print STDERR "$str\n\n"; | |
505 | return; | |
506 | } elsif ($args->[0] eq 'bashcomplete') { | |
507 | shift @$args; | |
508 | &$print_bash_completion({ $name => $def }, $name, @$args); | |
509 | return; | |
510 | } elsif ($args->[0] eq 'verifyapi') { | |
511 | PVE::RESTHandler::validate_method_schemas(); | |
512 | return; | |
513 | } elsif ($args->[0] eq 'printmanpod') { | |
514 | $podfn = find_cli_class_source($name) if !defined($podfn); | |
515 | print_simple_pod_manpage($podfn, @$def); | |
516 | return; | |
517 | } | |
518 | } | |
519 | ||
520 | &$preparefunc() if $preparefunc; | |
521 | ||
522 | my $res = $class->cli_handler($name, $name, \@ARGV, $arg_param, $uri_param, $pwcallback, $stringfilemap); | |
523 | ||
524 | &$outsub($res) if $outsub; | |
525 | }; | |
526 | ||
527 | sub run_cli { | |
528 | my ($class, $pwcallback, $podfn, $preparefunc) = @_; | |
529 | ||
530 | # Note: "depreciated function run_cli - use run_cli_handler instead"; | |
531 | ||
532 | die "password callback is no longer supported" if $pwcallback; | |
533 | ||
534 | run_cli_handler($class, podfn => $podfn, prepare => $preparefunc); | |
535 | } | |
536 | ||
537 | sub run_cli_handler { | |
538 | my ($class, %params) = @_; | |
539 | ||
540 | $cli_handler_class = $class; | |
541 | ||
542 | $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin'; | |
543 | ||
544 | foreach my $key (keys %params) { | |
545 | next if $key eq 'podfn'; | |
546 | next if $key eq 'prepare'; | |
547 | next if $key eq 'no_init'; # used by lxc hooks | |
548 | die "unknown parameter '$key'"; | |
549 | } | |
550 | ||
551 | my $podfn = $params{podfn}; | |
552 | my $preparefunc = $params{prepare}; | |
553 | my $no_init = $params{no_init}; | |
554 | ||
555 | my $pwcallback = $class->can('read_password'); | |
556 | my $stringfilemap = $class->can('string_param_file_mapping'); | |
557 | ||
558 | $exename = &$get_exe_name($class); | |
559 | ||
560 | initlog($exename); | |
561 | ||
562 | if ($class !~ m/^PVE::Service::/) { | |
563 | die "please run as root\n" if $> != 0; | |
564 | ||
565 | PVE::INotify::inotify_init() if !$no_init; | |
566 | ||
567 | my $rpcenv = PVE::RPCEnvironment->init('cli'); | |
568 | $rpcenv->init_request() if !$no_init; | |
569 | $rpcenv->set_language($ENV{LANG}); | |
570 | $rpcenv->set_user('root@pam'); | |
571 | } | |
572 | ||
573 | no strict 'refs'; | |
574 | my $def = ${"${class}::cmddef"}; | |
575 | ||
576 | if (ref($def) eq 'ARRAY') { | |
577 | &$handle_simple_cmd($def, \@ARGV, $pwcallback, $podfn, $preparefunc, $stringfilemap); | |
578 | } else { | |
579 | $cmddef = $def; | |
580 | my $cmd = shift @ARGV; | |
581 | &$handle_cmd($cmddef, $exename, $cmd, \@ARGV, $pwcallback, $podfn, $preparefunc, $stringfilemap); | |
582 | } | |
583 | ||
584 | exit 0; | |
585 | } | |
586 | ||
587 | 1; |