fix #1819: fork_worker: ensure sync'ed workers control terminal
[pve-common.git] / src / PVE / CLIHandler.pm
1 package PVE::CLIHandler;
2
3 use strict;
4 use warnings;
5 use JSON;
6
7 use PVE::SafeSyslog;
8 use PVE::Exception qw(raise raise_param_exc);
9 use PVE::RESTHandler;
10 use PVE::PTY;
11 use PVE::INotify;
12 use PVE::CLIFormatter;
13
14 use base qw(PVE::RESTHandler);
15
16 # $cmddef defines which (sub)commands are available in a specific CLI class.
17 # A real command is always an array consisting of its class, name, array of
18 # position fixed (required) parameters and hash of predefined parameters when
19 # mapping a CLI command t o an API call. Optionally an output method can be
20 # passed at the end, e.g., for formatting or transformation purpose.
21 #
22 # [class, name, fixed_params, API_pre-set params, output_sub ]
23 #
24 # In case of so called 'simple commands', the $cmddef can be also just an
25 # array.
26 #
27 # Examples:
28 # $cmddef = {
29 # command => [ 'PVE::API2::Class', 'command', [ 'arg1', 'arg2' ], { node => $nodename } ],
30 # do => {
31 # this => [ 'PVE::API2::OtherClass', 'method', [ 'arg1' ], undef, sub {
32 # my ($res) = @_;
33 # print "$res\n";
34 # }],
35 # that => [ 'PVE::API2::OtherClass', 'subroutine' [] ],
36 # },
37 # dothat => { alias => 'do that' },
38 # }
39 my $cmddef;
40 my $exename;
41 my $cli_handler_class;
42
43 my $standard_mappings = {
44 'pve-password' => {
45 name => 'password',
46 desc => '<password>',
47 interactive => 1,
48 func => sub {
49 my ($value) = @_;
50 return $value if $value;
51 return PVE::PTY::get_confirmed_password();
52 },
53 },
54 };
55 sub get_standard_mapping {
56 my ($name, $base) = @_;
57
58 my $std = $standard_mappings->{$name};
59 die "no such standard mapping '$name'\n" if !$std;
60
61 my $res = $base || {};
62
63 foreach my $opt (keys %$std) {
64 next if defined($res->{$opt});
65 $res->{$opt} = $std->{$opt};
66 }
67
68 return $res;
69 }
70
71 my $gen_param_mapping_func = sub {
72 my ($cli_handler_class) = @_;
73
74 my $param_mapping = $cli_handler_class->can('param_mapping');
75
76 if (!$param_mapping) {
77 my $read_password = $cli_handler_class->can('read_password');
78 my $string_param_mapping = $cli_handler_class->can('string_param_file_mapping');
79
80 return $string_param_mapping if !$read_password;
81
82 $param_mapping = sub {
83 my ($name) = @_;
84
85 my $password_map = get_standard_mapping('pve-password', {
86 func => $read_password
87 });
88 my $map = $string_param_mapping ? $string_param_mapping->($name) : [];
89 return [@$map, $password_map];
90 };
91 }
92
93 return $param_mapping;
94 };
95
96 my $assert_initialized = sub {
97 my @caller = caller;
98 die "$caller[0]:$caller[2] - not initialized\n"
99 if !($cmddef && $exename && $cli_handler_class);
100 };
101
102 my $abort = sub {
103 my ($reason, $cmd) = @_;
104 print_usage_short (\*STDERR, $reason, $cmd);
105 exit (-1);
106 };
107
108 my $expand_command_name = sub {
109 my ($def, $cmd) = @_;
110
111 return $cmd if exists $def->{$cmd}; # command is already complete
112
113 my $is_alias = sub { ref($_[0]) eq 'HASH' && exists($_[0]->{alias}) };
114 my @expanded = grep { /^\Q$cmd\E/ && !$is_alias->($def->{$_}) } keys %$def;
115
116 return $expanded[0] if scalar(@expanded) == 1; # enforce exact match
117
118 return undef;
119 };
120
121 my $get_commands = sub {
122 my $def = shift // die "no command definition passed!";
123 return [ grep { !(ref($def->{$_}) eq 'HASH' && defined($def->{$_}->{alias})) } sort keys %$def ];
124 };
125
126 my $complete_command_names = sub { $get_commands->($cmddef) };
127
128 # traverses the command definition using the $argv array, resolving one level
129 # of aliases.
130 # Returns the matching (sub) command and its definition, and argument array for
131 # this (sub) command and a hash where we marked which (sub) commands got
132 # expanded (e.g. st => status) while traversing
133 sub resolve_cmd {
134 my ($argv, $is_alias) = @_;
135
136 my ($def, $cmd) = ($cmddef, $argv);
137 my $cmdstr = $exename;
138
139 if (ref($argv) eq 'ARRAY') {
140 my $expanded_last_arg;
141 my $last_arg_id = scalar(@$argv) - 1;
142
143 for my $i (0..$last_arg_id) {
144 $cmd = $expand_command_name->($def, $argv->[$i]);
145 if (defined($cmd)) {
146 # If the argument was expanded (or was already complete) and it
147 # is the final argument, tell our caller about it:
148 $expanded_last_arg = $cmd if $i == $last_arg_id;
149 } else {
150 # Otherwise continue with the unexpanded version of it.
151 $cmd = $argv->[$i];
152 }
153 $cmdstr .= " $cmd";
154 $def = $def->{$cmd};
155 last if !defined($def);
156
157 if (ref($def) eq 'ARRAY') {
158 # could expand to a real command, rest of $argv are its arguments
159 my $cmd_args = [ @$argv[$i+1..$last_arg_id] ];
160 return ($cmd, $def, $cmd_args, $expanded_last_arg, $cmdstr);
161 }
162
163 if (defined($def->{alias})) {
164 die "alias loop detected for '$cmd'" if $is_alias; # avoids cycles
165 # replace aliased (sub)command with the expanded aliased command
166 splice @$argv, $i, 1, split(/ +/, $def->{alias});
167 return resolve_cmd($argv, 1);
168 }
169 }
170 # got either a special command (bashcomplete, verifyapi) or an unknown
171 # cmd, just return first entry as cmd and the rest of $argv as cmd_arg
172 my $cmd_args = [ @$argv[1..$last_arg_id] ];
173 return ($argv->[0], $def, $cmd_args, $expanded_last_arg, $cmdstr);
174 }
175 return ($cmd, $def, undef, undef, $cmdstr);
176 }
177
178 sub generate_usage_str {
179 my ($format, $cmd, $indent, $separator, $sortfunc) = @_;
180
181 $assert_initialized->();
182 die 'format required' if !$format;
183
184 $sortfunc //= sub { sort keys %{$_[0]} };
185 $separator //= '';
186 $indent //= '';
187
188 my $param_cb = $gen_param_mapping_func->($cli_handler_class);
189
190 my ($subcmd, $def, undef, undef, $cmdstr) = resolve_cmd($cmd);
191 $abort->("unknown command '$cmdstr'") if !defined($def) && ref($cmd) eq 'ARRAY';
192
193 my $generate;
194 $generate = sub {
195 my ($indent, $separator, $def, $prefix) = @_;
196
197 my $str = '';
198 if (ref($def) eq 'HASH') {
199 my $oldclass = undef;
200 foreach my $cmd (&$sortfunc($def)) {
201
202 if (ref($def->{$cmd}) eq 'ARRAY') {
203 my ($class, $name, $arg_param, $fixed_param) = @{$def->{$cmd}};
204
205 $str .= $separator if $oldclass && $oldclass ne $class;
206 $str .= $indent;
207 $str .= $class->usage_str($name, "$prefix $cmd", $arg_param,
208 $fixed_param, $format, $param_cb);
209 $oldclass = $class;
210
211 } elsif (defined($def->{$cmd}->{alias}) && ($format eq 'asciidoc')) {
212
213 $str .= "*$prefix $cmd*\n\nAn alias for '$exename $def->{$cmd}->{alias}'.\n\n";
214
215 } else {
216 next if $def->{$cmd}->{alias};
217
218 my $substr = $generate->($indent, $separator, $def->{$cmd}, "$prefix $cmd");
219 if ($substr) {
220 $substr .= $separator if $substr !~ /\Q$separator\E{2}/;
221 $str .= $substr;
222 }
223 }
224
225 }
226 } else {
227 my ($class, $name, $arg_param, $fixed_param) = @$def;
228 $abort->("unknown command '$cmd'") if !$class;
229
230 $str .= $indent;
231 $str .= $class->usage_str($name, $prefix, $arg_param, $fixed_param, $format, $param_cb);
232 }
233 return $str;
234 };
235
236 return $generate->($indent, $separator, $def, $cmdstr);
237 }
238
239 __PACKAGE__->register_method ({
240 name => 'help',
241 path => 'help',
242 method => 'GET',
243 description => "Get help about specified command.",
244 parameters => {
245 additionalProperties => 0,
246 properties => {
247 'extra-args' => PVE::JSONSchema::get_standard_option('extra-args', {
248 description => 'Shows help for a specific command',
249 completion => $complete_command_names,
250 }),
251 verbose => {
252 description => "Verbose output format.",
253 type => 'boolean',
254 optional => 1,
255 },
256 },
257 },
258 returns => { type => 'null' },
259
260 code => sub {
261 my ($param) = @_;
262
263 $assert_initialized->();
264
265 my $cmd = $param->{'extra-args'};
266
267 my $verbose = defined($cmd) && $cmd;
268 $verbose = $param->{verbose} if defined($param->{verbose});
269
270 if (!$cmd) {
271 if ($verbose) {
272 print_usage_verbose();
273 } else {
274 print_usage_short(\*STDOUT);
275 }
276 return undef;
277 }
278
279 my $str;
280 if ($verbose) {
281 $str = generate_usage_str('full', $cmd, '');
282 } else {
283 $str = generate_usage_str('short', $cmd, ' ' x 7);
284 }
285 $str =~ s/^\s+//;
286
287 if ($verbose) {
288 print "$str\n";
289 } else {
290 print "USAGE: $str\n";
291 }
292
293 return undef;
294
295 }});
296
297 sub print_simple_asciidoc_synopsis {
298 $assert_initialized->();
299
300 my $synopsis = "*${exename}* `help`\n\n";
301 $synopsis .= generate_usage_str('asciidoc');
302
303 return $synopsis;
304 }
305
306 sub print_asciidoc_synopsis {
307 $assert_initialized->();
308
309 my $synopsis = "";
310
311 $synopsis .= "*${exename}* `<COMMAND> [ARGS] [OPTIONS]`\n\n";
312
313 $synopsis .= generate_usage_str('asciidoc');
314
315 $synopsis .= "\n";
316
317 return $synopsis;
318 }
319
320 sub print_usage_verbose {
321 $assert_initialized->();
322
323 print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
324
325 my $str = generate_usage_str('full');
326
327 print "$str\n";
328 }
329
330 sub print_usage_short {
331 my ($fd, $msg, $cmd) = @_;
332
333 $assert_initialized->();
334
335 print $fd "ERROR: $msg\n" if $msg;
336 print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n";
337
338 print {$fd} generate_usage_str('short', $cmd, ' ' x 7, "\n", sub {
339 my ($h) = @_;
340 return sort {
341 if (ref($h->{$a}) eq 'ARRAY' && ref($h->{$b}) eq 'ARRAY') {
342 # $a and $b are both real commands order them by their class
343 return $h->{$a}->[0] cmp $h->{$b}->[0] || $a cmp $b;
344 } elsif (ref($h->{$a}) eq 'ARRAY' xor ref($h->{$b}) eq 'ARRAY') {
345 # real command and subcommand mixed, put sub commands first
346 return ref($h->{$b}) eq 'ARRAY' ? -1 : 1;
347 } else {
348 # both are either from the same class or subcommands
349 return $a cmp $b;
350 }
351 } keys %$h;
352 });
353 }
354
355 my $print_bash_completion = sub {
356 my ($simple_cmd, $bash_command, $cur, $prev) = @_;
357
358 my $debug = 0;
359
360 return if !(defined($cur) && defined($prev) && defined($bash_command));
361 return if !defined($ENV{COMP_LINE});
362 return if !defined($ENV{COMP_POINT});
363
364 my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT});
365 print STDERR "\nCMDLINE: $ENV{COMP_LINE}\n" if $debug;
366
367 my $args = PVE::Tools::split_args($cmdline);
368 shift @$args; # no need for program name
369 my $print_result = sub {
370 foreach my $p (@_) {
371 print "$p\n" if $p =~ m/^\Q$cur\E/;
372 }
373 };
374
375 my ($cmd, $def) = ($simple_cmd, $cmddef);
376 if (!$simple_cmd) {
377 ($cmd, $def, $args, my $expanded) = resolve_cmd($args);
378
379 if (defined($expanded) && $prev ne $expanded) {
380 print "$expanded\n";
381 return;
382 }
383
384 if (ref($def) eq 'HASH') {
385 &$print_result(@{$get_commands->($def)});
386 return;
387 }
388 }
389 return if !$def;
390
391 my $pos = scalar(@$args) - 1;
392 $pos += 1 if $cmdline =~ m/\s+$/;
393 print STDERR "pos: $pos\n" if $debug;
394 return if $pos < 0;
395
396 my $skip_param = {};
397
398 my ($class, $name, $arg_param, $uri_param) = @$def;
399 $arg_param //= [];
400 $uri_param //= {};
401
402 $arg_param = [ $arg_param ] if !ref($arg_param);
403
404 map { $skip_param->{$_} = 1; } @$arg_param;
405 map { $skip_param->{$_} = 1; } keys %$uri_param;
406
407 my $info = $class->map_method_by_name($name);
408
409 my $prop = $info->{parameters}->{properties};
410
411 my $print_parameter_completion = sub {
412 my ($pname) = @_;
413 my $d = $prop->{$pname};
414 if ($d->{completion}) {
415 my $vt = ref($d->{completion});
416 if ($vt eq 'CODE') {
417 my $res = $d->{completion}->($cmd, $pname, $cur, $args);
418 &$print_result(@$res);
419 }
420 } elsif ($d->{type} eq 'boolean') {
421 &$print_result('0', '1');
422 } elsif ($d->{enum}) {
423 &$print_result(@{$d->{enum}});
424 }
425 };
426
427 # positional arguments
428 if ($pos < scalar(@$arg_param)) {
429 my $pname = $arg_param->[$pos];
430 &$print_parameter_completion($pname);
431 return;
432 }
433
434 my @option_list = ();
435 foreach my $key (keys %$prop) {
436 next if $skip_param->{$key};
437 push @option_list, "--$key";
438 }
439
440 if ($cur =~ m/^-/) {
441 &$print_result(@option_list);
442 return;
443 }
444
445 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
446 my $pname = $1;
447 &$print_parameter_completion($pname);
448 return;
449 }
450
451 &$print_result(@option_list);
452 };
453
454 sub verify_api {
455 my ($class) = @_;
456
457 # simply verify all registered methods
458 PVE::RESTHandler::validate_method_schemas();
459 }
460
461 my $get_exe_name = sub {
462 my ($class) = @_;
463
464 my $name = $class;
465 $name =~ s/^.*:://;
466 $name =~ s/_/-/g;
467
468 return $name;
469 };
470
471 sub generate_bash_completions {
472 my ($class) = @_;
473
474 # generate bash completion config
475
476 $exename = &$get_exe_name($class);
477
478 print <<__EOD__;
479 # $exename bash completion
480
481 # see http://tiswww.case.edu/php/chet/bash/FAQ
482 # and __ltrim_colon_completions() in /usr/share/bash-completion/bash_completion
483 # this modifies global var, but I found no better way
484 COMP_WORDBREAKS=\${COMP_WORDBREAKS//:}
485
486 complete -o default -C '$exename bashcomplete' $exename
487 __EOD__
488 }
489
490 sub generate_asciidoc_synopsys {
491 my ($class) = @_;
492 $class->generate_asciidoc_synopsis();
493 };
494
495 sub generate_asciidoc_synopsis {
496 my ($class) = @_;
497
498 $cli_handler_class = $class;
499
500 $exename = &$get_exe_name($class);
501
502 no strict 'refs';
503 my $def = ${"${class}::cmddef"};
504 $cmddef = $def;
505
506 if (ref($def) eq 'ARRAY') {
507 print_simple_asciidoc_synopsis();
508 } else {
509 $cmddef->{help} = [ __PACKAGE__, 'help', ['cmd'] ];
510
511 print_asciidoc_synopsis();
512 }
513 }
514
515 # overwrite this if you want to run/setup things early
516 sub setup_environment {
517 my ($class) = @_;
518
519 # do nothing by default
520 }
521
522 my $handle_cmd = sub {
523 my ($args, $preparefunc, $param_cb) = @_;
524
525 $cmddef->{help} = [ __PACKAGE__, 'help', ['extra-args'] ];
526
527 my ($cmd, $def, $cmd_args, undef, $cmd_str) = resolve_cmd($args);
528
529 $abort->("no command specified") if !$cmd;
530
531 # call verifyapi before setup_environment(), don't execute any real code in
532 # this case
533 if ($cmd eq 'verifyapi') {
534 PVE::RESTHandler::validate_method_schemas();
535 return;
536 }
537
538 $cli_handler_class->setup_environment();
539
540 if ($cmd eq 'bashcomplete') {
541 &$print_bash_completion(undef, @$cmd_args);
542 return;
543 }
544
545 # checked special commands, if def is still a hash we got an incomplete sub command
546 $abort->("incomplete command '$cmd_str'", $args) if ref($def) eq 'HASH';
547
548 &$preparefunc() if $preparefunc;
549
550 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$def || []};
551 $abort->("unknown command '$cmd_str'") if !$class;
552
553 my $res = $class->cli_handler($cmd_str, $name, $cmd_args, $arg_param, $uri_param, $param_cb);
554
555 if (defined $outsub) {
556 my $result_schema = $class->map_method_by_name($name)->{returns};
557 $outsub->($res, $result_schema);
558 }
559 };
560
561 my $handle_simple_cmd = sub {
562 my ($args, $preparefunc, $param_cb) = @_;
563
564 my ($class, $name, $arg_param, $uri_param, $outsub) = @{$cmddef};
565 die "no class specified" if !$class;
566
567 if (scalar(@$args) >= 1) {
568 if ($args->[0] eq 'help') {
569 my $str = "USAGE: $name help\n";
570 $str .= generate_usage_str('long');
571 print STDERR "$str\n\n";
572 return;
573 } elsif ($args->[0] eq 'verifyapi') {
574 PVE::RESTHandler::validate_method_schemas();
575 return;
576 }
577 }
578
579 $cli_handler_class->setup_environment();
580
581 if (scalar(@$args) >= 1) {
582 if ($args->[0] eq 'bashcomplete') {
583 shift @$args;
584 &$print_bash_completion($name, @$args);
585 return;
586 }
587 }
588
589 &$preparefunc() if $preparefunc;
590
591 my $res = $class->cli_handler($name, $name, \@ARGV, $arg_param, $uri_param, $param_cb);
592
593 if (defined $outsub) {
594 my $result_schema = $class->map_method_by_name($name)->{returns};
595 $outsub->($res, $result_schema);
596 }
597 };
598
599 sub run_cli_handler {
600 my ($class, %params) = @_;
601
602 $cli_handler_class = $class;
603
604 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
605
606 foreach my $key (keys %params) {
607 next if $key eq 'prepare';
608 next if $key eq 'no_init'; # not used anymore
609 next if $key eq 'no_rpcenv'; # not used anymore
610 die "unknown parameter '$key'";
611 }
612
613 my $preparefunc = $params{prepare};
614
615 my $param_cb = $gen_param_mapping_func->($cli_handler_class);
616
617 $exename = &$get_exe_name($class);
618
619 initlog($exename);
620
621 no strict 'refs';
622 $cmddef = ${"${class}::cmddef"};
623
624 if (ref($cmddef) eq 'ARRAY') {
625 $handle_simple_cmd->(\@ARGV, $preparefunc, $param_cb);
626 } else {
627 $handle_cmd->(\@ARGV, $preparefunc, $param_cb);
628 }
629
630 exit 0;
631 }
632
633 1;