4 # implement persistent history ?
11 use HTTP
::Status
qw(:constants :is status_message);
17 use PVE
::RPCEnvironment
;
21 PVE
::INotify
::inotify_init
();
23 my $rpcenv = PVE
::RPCEnvironment-
>init('cli');
25 $rpcenv->set_language($ENV{LANG
});
26 $rpcenv->set_user('root@pam');
28 my $logid = $ENV{PVE_LOG_ID
} || 'pvesh';
31 my $basedir = '/api2/json';
38 print STDERR
"ERROR: $msg\n" if $msg;
39 print STDERR
"USAGE: pvesh [verifyapi]\n";
40 print STDERR
" pvesh CMD [OPTIONS]\n";
44 my $disable_proxy = 0;
53 if ($cmd eq '--noproxy') {
57 } elsif ($cmd eq '--nooutput') {
58 # we use this when starting task in CLI (suppress printing upid)
59 # for example 'pvesh --nooutput create /nodes/localhost/stopall'
68 if ($cmd eq 'verifyapi') {
69 PVE
::RESTHandler
::validate_method_schemas
();
71 } elsif ($cmd eq 'ls' || $cmd eq 'get' || $cmd eq 'create' ||
72 $cmd eq 'set' || $cmd eq 'delete' ||$cmd eq 'help' ) {
73 pve_command
([ $cmd, @ARGV], $opt_nooutput);
76 print_usage
("unknown command '$cmd'");
81 if (scalar (@ARGV) != 0) {
86 print "entering PVE shell - type 'help' for help\n";
88 my $term = new Term
::ReadLine
('pvesh');
89 my $attribs = $term->Attribs;
94 my ($dir, undef, $rest) = $text =~ m
|^(.*/)?(([^/]*))?
$|;
95 my $path = abs_path
($cdir, $dir);
99 my $di = dir_info
($path);
100 if (my $children = $di->{children
}) {
101 foreach my $c (@$children) {
102 if ($c =~ /^\Q$rest/) {
103 my $new = $dir ?
"$dir$c" : $c;
109 if (scalar(@res) == 0) {
111 } elsif (scalar(@res) == 1) {
112 return ($res[0], $res[0], "$res[0]/");
115 # lcd : lowest common denominator
118 for (my $i = 1; $i <= length($tmp); $i++) {
120 foreach my $p (@res) {
121 if (substr($tmp, 0, $i) ne substr($p, 0, $i)) {
127 $lcd = substr($tmp, 0, $i);
136 # just to avoid an endless loop (called by attempted_completion_function)
137 $attribs->{completion_entry_function
} = sub {
138 my($text, $state) = @_;
142 $attribs->{attempted_completion_function
} = sub {
143 my ($text, $line, $start) = @_;
145 my $prefix = substr($line, 0, $start);
146 if ($prefix =~ /^\s*$/) { # first word (command completeion)
147 $attribs->{completion_word
} = [qw(help ls cd get set create delete quit)];
148 return $term->completion_matches($text, $attribs->{list_completion_function
});
151 if ($prefix =~ /^\s*\S+\s+$/) { # second word (path completion)
152 return complete_path
($text);
159 my ($current, $path) = @_;
163 return $current if !defined($path);
165 $ret = '' if $path =~ m
|^\
/|;
167 foreach my $d (split (/\/+/ , $path)) {
170 } elsif ($d eq '..') {
171 $ret = dirname
($ret);
172 $ret = '' if $ret eq '.';
185 my $read_password = sub {
186 my $attribs = $term->Attribs;
187 my $old = $attribs->{redisplay_function
};
188 $attribs->{redisplay_function
} = $attribs->{shadow_redisplay
};
189 my $input = $term->readline('password: ');
190 my $conf = $term->readline('Retype new password: ');
191 $attribs->{redisplay_function
} = $old;
193 # remove password from history
194 if ($term->Features->{autohistory
}) {
195 my $historyPosition = $term->where_history();
196 $term->remove_history($historyPosition);
197 $term->remove_history($historyPosition - 1);
200 die "Passwords do not match.\n" if ($input ne $conf);
204 sub reverse_map_cmd
{
214 my $cmd = $mmap->{$method};
216 die "got strange value for method ('$method') - internal error" if !$cmd;
232 my $method = $mmap->{$cmd};
234 die "unable to map method" if !$method;
240 my ($info, $uri_param) = @_;
242 if ($info->{proxyto
}) {
243 my $pn = $info->{proxyto
};
244 my $node = $uri_param->{$pn};
245 die "proxy parameter '$pn' does not exists" if !$node;
247 if ($node ne 'localhost' && ($node ne PVE
::INotify
::nodename
())) {
248 die "proxy loop detected - aborting\n" if $disable_proxy;
249 my $remip = PVE
::Cluster
::remote_node_ip
($node);
250 return ($node, $remip);
258 my ($node, $remip, $dir, $cmd, $args) = @_;
260 my $remcmd = ['ssh', '-o', 'BatchMode=yes', "root\@$remip",
261 'pvesh', '--noproxy', $cmd, $dir, @$args];
263 system(@$remcmd) == 0 || die "proxy handler failed\n";
267 my ($dir, $cmd, $args, $nooutput) = @_;
269 my $method = map_cmd
($cmd);
272 my ($handler, $info) = PVE
::API2-
>find_handler($method, $dir, $uri_param);
273 if (!$handler || !$info) {
274 die "no '$cmd' handler for '$dir'\n";
277 my ($node, $remip) = check_proxyto
($info, $uri_param);
278 return proxy_handler
($node, $remip, $dir, $cmd, $args) if $node;
280 my $data = $handler->cli_handler("$cmd $dir", $info->{name
}, $args, [], $uri_param, $read_password);
284 warn "200 OK\n"; # always print OK status if successful
286 if ($info && $info->{returns
} && $info->{returns
}->{type
}) {
287 my $rtype = $info->{returns
}->{type
};
289 return if $rtype eq 'null';
291 if ($rtype eq 'string') {
292 print $data if $data;
297 print to_json
($data, {utf8
=> 1, allow_nonref
=> 1, canonical
=> 1, pretty
=> 1 });
302 sub find_resource_methods
{
303 my ($path, $ihash) = @_;
305 for my $method (qw(GET POST PUT DELETE)) {
308 my ($handler, $info) = PVE
::API2-
>find_handler($method, $path, $uri_param, \
$path_match);
309 if ($handler && $info && !$ihash->{$info}) {
314 uri_param
=> $uri_param,
321 my ($path, $opts) = @_;
325 find_resource_methods
($path, $ihash);
327 if (!scalar(keys(%$ihash))) {
328 die "no such resource\n";
331 my $di = dir_info
($path);
332 if (my $children = $di->{children
}) {
333 foreach my $c (@$children) {
334 my $cp = abs_path
($path, $c);
335 find_resource_methods
($cp, $ihash);
339 foreach my $mi (sort { $a->{path
} cmp $b->{path
} } values %$ihash) {
340 my $method = $mi->{info
}->{method};
342 # we skip index methods for now.
343 next if ($method eq 'GET') && PVE
::JSONSchema
::method_get_child_link
($mi->{info
});
345 my $path = $mi->{path
};
346 $path =~ s
|/+$||; # remove trailing slash
348 my $cmd = reverse_map_cmd
($method);
350 print $mi->{handler
}->usage_str($mi->{info
}->{name
}, "$cmd $path", [], $mi->{uri_param
},
351 $opts->{verbose
} ?
'full' : 'short', 1);
352 print "\n\n" if $opts->{verbose
};
362 my ($handler, $info) = PVE
::API2-
>find_handler('GET', $path);
363 if (!($handler && $info)) {
366 if (PVE
::JSONSchema
::method_get_child_link
($info)) {
373 ($handler, $info) = PVE
::API2-
>find_handler('PUT', $path);
374 if (!($handler && $info)) {
380 ($handler, $info) = PVE
::API2-
>find_handler('POST', $path);
381 if (!($handler && $info)) {
387 ($handler, $info) = PVE
::API2-
>find_handler('DELETE', $path);
388 if (!($handler && $info)) {
397 sub extract_children
{
398 my ($lnk, $data) = @_;
402 return $res if !($lnk && $data);
404 my $href = $lnk->{href
};
405 if ($href =~ m/^\{(\S+)\}$/) {
408 foreach my $elem (sort {$a->{$prop} cmp $b->{$prop}} @$data) {
410 my $value = $elem->{$prop};
421 my $res = { path
=> $path };
423 my ($handler, $info, $pm) = PVE
::API2-
>find_handler('GET', $path, $uri_param);
424 if ($handler && $info) {
426 my $data = $handler->handle($info, $uri_param);
427 my $lnk = PVE
::JSONSchema
::method_get_child_link
($info);
428 $res->{children
} = extract_children
($lnk, $data);
435 my ($dir, $args) = @_;
438 my ($handler, $info) = PVE
::API2-
>find_handler('GET', $dir, $uri_param);
439 if (!$handler || !$info) {
440 die "no such resource\n";
443 if (!PVE
::JSONSchema
::method_get_child_link
($info)) {
444 die "resource does not define child links\n";
447 my ($node, $remip) = check_proxyto
($info, $uri_param);
448 return proxy_handler
($node, $remip, $dir, 'ls', $args) if $node;
451 my $data = $handler->cli_handler("ls $dir", $info->{name
}, $args, [], $uri_param, $read_password);
452 my $lnk = PVE
::JSONSchema
::method_get_child_link
($info);
453 my $children = extract_children
($lnk, $data);
455 foreach my $c (@$children) {
456 my $cap = resource_cap
(abs_path
($dir, $c));
463 my ($args, $nooutput) = @_;
465 PVE
::Cluster
::cfs_update
();
467 $rpcenv->init_request();
469 my $cmd = shift @$args;
473 my $path = shift @$args;
475 die "usage: cd [dir]\n" if scalar(@$args);
477 if (!defined($path)) {
481 my $new_dir = abs_path
($cdir, $path);
482 my ($handler, $info) = PVE
::API2-
>find_handler('GET', $new_dir);
483 die "no such resource\n" if !$handler;
487 } elsif ($cmd eq 'help') {
489 my $help_usage_error = sub {
490 die "usage: help [path] [--verbose]\n";
495 &$help_usage_error() if !Getopt
::Long
::GetOptionsFromArray
($args, $opts, 'verbose');
498 if (scalar(@$args) && $args->[0] !~ m/^\-/) {
499 $path = shift @$args;
502 &$help_usage_error() if scalar(@$args);
504 print "help [path] [--verbose]\n";
506 print "ls [path]\n\n";
508 print_help
(abs_path
($cdir, $path), $opts);
510 } elsif ($cmd eq 'ls') {
512 if (scalar(@$args) && $args->[0] !~ m/^\-/) {
513 $path = shift @$args;
516 list_dir
(abs_path
($cdir, $path), $args);
518 } elsif ($cmd =~ m/^get|delete|set$/) {
521 if (scalar(@$args) && $args->[0] !~ m/^\-/) {
522 $path = shift @$args;
525 call_method
(abs_path
($cdir, $path), $cmd, $args);
527 } elsif ($cmd eq 'create') {
530 if (scalar(@$args) && $args->[0] !~ m/^\-/) {
531 $path = shift @$args;
534 call_method
(abs_path
($cdir, $path), $cmd, $args, $nooutput);
537 die "unknown command '$cmd'\n";
543 while (defined ($input = $term->readline("pve:/$cdir> "))) {
546 next if $input =~ m/^\s*$/;
548 if ($input =~ m/^\s*q(uit)?\s*$/) {
552 # add input to history if it gets not
553 # automatically added
554 if (!$term->Features->{autohistory
}) {
555 $term->addhistory($input);
559 my $args = [ shellwords
($input) ];
569 pvesh - shell interface to the Promox VE API
573 pvesh [get|set|create|delete|help] [REST API path] [--verbose]
577 pvesh provides a shell-like interface to the Proxmox VE REST API, in two different modes:
583 when called without parameters, pvesh starts an interactive client, where you can navigate
584 in the different parts of the API
588 when started with parameters pvesh will send a command to the corresponding REST url, and will
589 return the JSON formatted output
595 get the list of nodes in my cluster
599 get a list of available options for the datacenter
601 pvesh help cluster/options -v
603 set the HTMl5 NoVNC console as the default console for the datacenter
605 pvesh set cluster/options -console html5