1 package PVE
::APIClient
::Helpers
;
8 use File
::Path
qw(make_path);
10 use PVE
::APIClient
::JSONSchema
;
11 use PVE
::APIClient
::Exception
qw(raise);
12 use PVE
::APIClient
::CLIFormatter
;
13 use PVE
::APIClient
::CLIHandler
;
14 use PVE
::APIClient
::PTY
;
17 use HTTP
::Status
qw(:constants);
19 my $pve_api_definition;
21 my $pve_api_definition_fn = "/usr/share/pve-client/pve-api-definition.dat";
30 my $default_output_format = 'text';
31 my $client_output_format = $default_output_format;
33 sub set_output_format
{
36 if (!defined($format)) {
37 $client_output_format = $default_output_format;
39 $client_output_format = $format;
43 sub get_output_format
{
44 return $client_output_format;
47 my $__real_remove_formats; $__real_remove_formats = sub {
48 my ($properties) = @_;
50 foreach my $pname (keys %$properties) {
51 if (my $d = $properties->{$pname}) {
52 if (defined(my $format = $d->{format
})) {
54 $__real_remove_formats->($format);
55 } elsif (!PVE
::APIClient
::JSONSchema
::get_format
($format)) {
56 # simply remove unknown format definitions
64 my $remove_unknown_formats; $remove_unknown_formats = sub {
67 my $class = ref($tree);
70 if ($class eq 'ARRAY') {
71 foreach my $el (@$tree) {
72 $remove_unknown_formats->($el);
74 } elsif ($class eq 'HASH') {
75 if (my $info = $tree->{info
}) {
76 for my $method (qw(GET PUT PUSH DELETE)) {
77 next if !$info->{$method};
78 my $properties = $info->{$method}->{parameters
}->{properties
};
79 $__real_remove_formats->($properties) if $properties;
82 if ($tree->{children
}) {
83 $remove_unknown_formats->($tree->{children
});
89 sub get_api_definition
{
91 if (!defined($pve_api_definition)) {
92 open(my $fh, '<', $pve_api_definition_fn) ||
93 die "unable to open '$pve_api_definition_fn' - $!\n";
94 $pve_api_definition = Storable
::fd_retrieve
($fh);
96 $remove_unknown_formats->($pve_api_definition);
99 return $pve_api_definition;
102 my $map_path_to_info = sub {
103 my ($child_list, $stack, $uri_param) = @_;
105 while (defined(my $comp = shift @$stack)) {
106 foreach my $child (@$child_list) {
107 my $text = $child->{text
};
109 if ($text eq $comp) {
111 } elsif ($text =~ m/^\{(\S+)\}$/) {
113 $uri_param->{$1} = $comp;
115 next; # text next child
117 if ($child->{leaf
} || !scalar(@$stack)) {
120 $child_list = $child->{children
};
121 last; # test next path component
128 sub find_method_info
{
129 my ($path, $method, $uri_param, $noerr) = @_;
133 my $stack = [ grep { length($_) > 0 } split('\/+' , $path)]; # skip empty fragments
135 my $api = get_api_definition
();
137 my $child = scalar(@$stack) ?
$map_path_to_info->($api->{children
}, $stack, $uri_param) : $api;
139 if (!($child && $child->{info
} && $child->{info
}->{$method})) {
140 return undef if $noerr;
141 die "unable to find API method '$method' for path '$path'\n";
144 return $child->{info
}->{$method};
147 sub complete_api_call_options
{
148 my ($cmd, $prop, $prev, $cur, $args) = @_;
150 my $print_result = sub {
152 print "$p\n" if $p =~ m/^$cur/;
156 my $print_parameter_completion = sub {
158 my $d = $prop->{$pname};
159 if ($d->{completion
}) {
160 my $vt = ref($d->{completion
});
162 my $res = $d->{completion
}->($cmd, $pname, $cur, $args);
163 &$print_result(@$res);
165 } elsif ($d->{type
} eq 'boolean') {
166 &$print_result('0', '1');
167 } elsif ($d->{enum
}) {
168 &$print_result(@{$d->{enum
}});
172 my @option_list = ();
173 foreach my $key (keys %$prop) {
174 push @option_list, "--$key";
178 &$print_result(@option_list);
182 if ($prev =~ m/^--?(.+)$/ && $prop->{$1}) {
184 &$print_parameter_completion($pname);
188 &$print_result(@option_list);
191 sub merge_api_definition_properties
{
192 my ($path, $method, $properties) = @_;
194 my $info = PVE
::APIClient
::Helpers
::find_method_info
($path, $method);
196 return { %{$info->{parameters
}->{properties
}}, %$properties };
199 sub complete_api_path
{
202 my $api = get_api_definition
(); # make sure API data is loaded
206 my ($dir, $rest) = $text =~ m
|^(?
:(.*)/)?(?:([^/]*))?
$|;
209 if (!defined($dir)) {
213 my $stack = [ grep { length($_) > 0 } split('\/+' , $dir)]; # skip empty fragments
214 $info = $map_path_to_info->($api->{children
}, $stack, {});
219 my $prefix = length($dir) ?
"/$dir/" : '/';
220 if (my $children = $info->{children
}) {
221 foreach my $c (@$children) {
222 my $ctext = $c->{text
};
223 if ($ctext =~ m/^\{(\S+)\}$/) {
224 push @$res, "$prefix$ctext";
225 push @$res, "$prefix$ctext/";
227 push @$res, "$prefix$rest";
228 push @$res, "$prefix$rest/";
230 } elsif ($ctext =~ m/^\Q$rest/) {
231 push @$res, "$prefix$ctext";
232 push @$res, "$prefix$ctext/" if $c->{children
};
241 # test for command lines with api calls (or similar bash completion calls):
242 # example1: pveclient api get remote1 /cluster
243 sub extract_path_info
{
244 my ($uri_param) = @_;
248 my $test_path_properties = sub {
251 return if scalar(@$args) < 5;
252 return if $args->[1] ne 'api';
254 my $path = $args->[4];
255 if (my $method = $method_map->{$args->[2]}) {
256 $info = find_method_info
($path, $method, $uri_param, 1);
260 if (defined(my $cmd = $ARGV[0])) {
262 $test_path_properties->([$0, @ARGV]);
263 } elsif ($cmd eq 'bashcomplete') {
264 my $cmdline = substr($ENV{COMP_LINE
}, 0, $ENV{COMP_POINT
});
265 my $args = PVE
::APIClient
::Tools
::split_args
($cmdline);
266 $test_path_properties->($args);
273 sub get_vmid_resource
{
274 my ($conn, $vmid) = @_;
276 my $resources = $conn->get('api2/json/cluster/resources', {type
=> 'vm'});
279 for my $tmp (@$resources) {
280 if ($tmp->{vmid
} eq $vmid) {
286 if (!defined($resource)) {
287 die "\"$vmid\" not found
";
294 my ($conn, $node, $upid, $quiet) = @_;
296 my $path = "api2
/json/nodes
/$node/tasks/$upid/status";
302 my $path = "api2
/json/nodes
/$node/tasks/$upid/log";
303 my $task_log = $conn->get($path, {start => $last_line});
306 for my $li (@$task_log) {
307 if ($li->{t} eq 'no content') {
310 $printme .= $li->{t} . "\n";
311 $last_line = $li->{n};
314 if ($printme ne '') {
319 $task_status = $conn->get($path, {});
321 if ($task_status->{status} eq "stopped
") {
328 if ($task_status->{exitstatus} ne "OK
") {
329 die $task_status->{exitstatus};
332 return $task_status->{exitstatus};
335 sub configuration_directory {
337 my $home = $ENV{HOME} // '';
338 my $xdg = $ENV{XDG_CONFIG_HOME} // '';
340 my $subdir = "pveclient
";
342 return "$xdg/$subdir" if length($xdg);
344 return "$home/.config
/$subdir" if length($home);
346 die "neither XDG_CONFIG_HOME nor HOME environment variable set
\n";
349 my $ticket_cache_filename = "/.tickets
";
351 sub ticket_cache_lookup {
354 my $dir = configuration_directory();
355 my $filename = "$dir/$ticket_cache_filename";
358 eval { $data = from_json(PVE::APIClient::Tools::file_get_contents($filename)); };
361 my $ticket = $data->{$remote};
362 return undef if !defined($ticket);
365 my $max_age = 3600*2 - 60;
367 if ($ticket =~ m/:([a-fA-F0-9]{8})::/) {
370 my $age = $ctime - $ttime;
372 return $ticket if ($age > $min_age) && ($age < $max_age);
378 sub ticket_cache_update {
379 my ($remote, $ticket) = @_;
381 my $dir = configuration_directory();
382 my $filename = "$dir/$ticket_cache_filename";
388 my $raw = PVE::APIClient::Tools::file_get_contents($filename);
389 eval { $data = from_json($raw); };
392 $data->{$remote} = $ticket;
394 PVE::APIClient::Tools::file_set_contents($filename, to_json($data), 0600);
397 PVE::APIClient::Tools::lock_file($filename, undef, $code);
401 sub extract_even_elements {
405 return [ grep { ($ind++ % 2) == 0 } @$list ];
408 sub print_ordered_result {
409 my ($property_list, $data, $result_schema) = @_;
411 my $format = get_output_format();
412 my $param_order = extract_even_elements($property_list);
415 PVE::APIClient::CLIFormatter::query_terminal_options($options);
417 PVE::APIClient::CLIFormatter::print_api_result($format, $data, $result_schema, $param_order, $options);