Commit 0710edb3 authored by Dietmar Maurer's avatar Dietmar Maurer

ipmlement proxy code for pvesh

parent 16283a5a
......@@ -16,8 +16,6 @@ use PVE::RPCEnvironment;
use PVE::API2;
use JSON;
use Data::Dumper; # fixme: remove
PVE::INotify::inotify_init();
my $rpcenv = PVE::RPCEnvironment->init('cli');
......@@ -29,19 +27,35 @@ my $basedir = '/api2/json';
my $cdir = '';
my $cmd = shift;
if ($cmd && $cmd eq 'verifyapi') {
PVE::RESTHandler::validate_method_schemas();
exit 0;
}
sub print_usage {
my $msg = shift;
print STDERR "ERROR: $msg\n" if $msg;
print STDERR "USAGE: pvesh [verifyapi]\n";
print STDERR " pvesh CMD [OPTIONS]\n";
}
my $disable_proxy = 0;
my $cmd = shift;
if ($cmd eq '--noproxy') {
$cmd = shift;
$disable_proxy = 1;
}
if ($cmd) {
if ($cmd eq 'verifyapi') {
PVE::RESTHandler::validate_method_schemas();
exit 0;
} elsif ($cmd eq 'ls' || $cmd eq 'get' || $cmd eq 'create' ||
$cmd eq 'set' || $cmd eq 'delete' ||$cmd eq 'help' ) {
pve_command([ $cmd, @ARGV]);
exit(0);
} else {
print_usage ("unknown command '$cmd'");
exit (-1);
}
}
if (scalar (@ARGV) != 0) {
......@@ -200,11 +214,25 @@ sub check_proxyto {
if ($info->{proxyto}) {
my $pn = $info->{proxyto};
my $node = $uri_param->{$pn};
die "proxy parameter '$pn' does not exists" if !$node;
if ($node ne 'localhost' && ($node ne PVE::INotify::nodename())) {
die "can't proxy to remote node - not implemented";
die "proxy loop detected - aborting\n" if $disable_proxy;
my $remip = PVE::Cluster::remote_node_ip($node);
return ($node, $remip);
}
}
return undef;
}
sub proxy_handler {
my ($node, $remip, $dir, $cmd, $args) = @_;
my $remcmd = ['ssh', '-o', 'BatchMode=yes', "root\@$remip",
'pvesh', '--noproxy', $cmd, $dir, @$args];
system(@$remcmd) == 0 || die "proxy handler failed\n";
}
sub call_method {
......@@ -218,7 +246,8 @@ sub call_method {
die "no '$cmd' handler for '$dir'\n";
}
check_proxyto($info, $uri_param);
my ($node, $remip) = check_proxyto($info, $uri_param);
return proxy_handler($node, $remip, $dir, $cmd, $args) if $node;
my $data = $handler->cli_handler("$cmd $dir", $info->{name}, $args, [], $uri_param, $read_password);
......@@ -231,13 +260,13 @@ sub call_method {
if ($rtype eq 'string') {
print $data if $data;
return $data;
return;
}
}
print to_json($data, {allow_nonref => 1, canonical => 1, pretty => 1 });
return $data;
return;
}
sub find_resource_methods {
......@@ -380,12 +409,14 @@ sub list_dir {
die "no such resource\n";
}
check_proxyto($info, $uri_param);
if (!PVE::JSONSchema::method_get_child_link($info)) {
die "resource does not define child links\n";
}
my ($node, $remip) = check_proxyto($info, $uri_param);
return proxy_handler($node, $remip, $dir, 'ls', $args) if $node;
my $data = $handler->cli_handler("ls $dir", $info->{name}, $args, [], $uri_param, $read_password);
my $lnk = PVE::JSONSchema::method_get_child_link($info);
my $children = extract_children($lnk, $data);
......@@ -396,15 +427,14 @@ sub list_dir {
}
}
sub pve_command {
my $input = shift;
my $args = shift;
PVE::Cluster::cfs_update();
$rpcenv->init_request();
my $args = [ shellwords($input) ];
my $cmd = shift @$args;
if ($cmd eq 'cd') {
......@@ -508,7 +538,8 @@ while (defined ($input = $term->readline("pve:/$cdir> "))) {
$term->addhistory($input);
eval {
pve_command ($input);
my $args = [ shellwords($input) ];
pve_command($args);
};
warn $@ if $@;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment