]> git.proxmox.com Git - pve-client.git/blob - pveclient
use new features from pve-common
[pve-client.git] / pveclient
1 #!/usr/bin/perl
2
3 package PVE::CLI::pveclient;
4
5 use strict;
6 use warnings;
7 use Cwd 'abs_path';
8 use Data::Dumper;
9
10 use PVE::APIClient::JSONSchema qw(register_standard_option get_standard_option);
11 use PVE::APIClient::CLIFormatter;
12 use PVE::APIClient::CLIHandler;
13 use PVE::APIClient::PTY;
14
15 use PVE::APIClient::LWP;
16 use PVE::APIClient::Helpers;
17 use PVE::APIClient::Config;
18 use PVE::APIClient::Commands::config;
19 use PVE::APIClient::Commands::remote;
20 use PVE::APIClient::Commands::list;
21 use PVE::APIClient::Commands::lxc;
22 use PVE::APIClient::Commands::GuestStatus;
23
24 use JSON;
25
26 sub call_api_method {
27 my ($method, $param, $options) = @_;
28
29 my $path = PVE::APIClient::Tools::extract_param($param, 'api_path');
30 die "missing API path\n" if !defined($path);
31
32 my $remote = PVE::APIClient::Tools::extract_param($param, 'remote');
33 die "missing remote\n" if !defined($remote);
34
35 my $config = PVE::APIClient::Config->load();
36
37 my $uri_param = {};
38 my $info = PVE::APIClient::Helpers::find_method_info($path, $method, $uri_param);
39
40 my $conn = PVE::APIClient::Config->remote_conn($config, $remote);
41
42 my $res = $conn->call($method, "api2/json/$path", $param);
43 die "undefined result" if !defined($res);
44 die "undefined result data" if !exists($res->{data});
45
46 return $res->{data};
47 }
48
49 use base qw(PVE::APIClient::CLIHandler);
50
51 sub read_password {
52 return PVE::APIClient::PTY::read_password("Remote password: ")
53 }
54
55
56 my $cmd = $ARGV[0];
57
58 if ($cmd && $cmd eq 'packagedepends') {
59 # experimental code to print required perl packages
60 my $packages = {};
61 my $dir = Cwd::getcwd;
62
63 foreach my $k (keys %INC) {
64 my $file = abs_path($INC{$k});
65 next if $file =~ m/^\Q$dir\E/;
66 my $res = `dpkg -S '$file'`;
67 if ($res && $res =~ m/^(\S+): $file$/) {
68 my $debian_package = $1;
69 $debian_package =~ s/:amd64$//;
70 $packages->{$debian_package} = 1;
71 } else {
72 die "unable to find package for '$file'\n";
73 }
74 }
75 print join("\n", sort(keys %$packages)) . "\n";
76
77 exit(0);
78 }
79
80 my $path_properties = {};
81 my $path_returns = { type => 'null' };
82
83 my $api_path_property = {
84 description => "API path.",
85 type => 'string',
86 completion => sub {
87 my ($cmd, $pname, $cur, $args) = @_;
88 return PVE::APIClient::Helpers::complete_api_path($cur);
89 },
90 };
91
92 # dynamically update schema definition for direct API call
93 # like: pveclient api <get|set|create|delete|help> <remote> <path>
94 my $uri_param = {};
95 if (my $info = PVE::APIClient::Helpers::extract_path_info($uri_param)) {
96 foreach my $key (keys %{$info->{parameters}->{properties}}) {
97 next if defined($uri_param->{$key});
98 $path_properties->{$key} = $info->{parameters}->{properties}->{$key};
99 }
100 $path_returns = $info->{returns};
101 }
102
103 $path_properties->{remote} = get_standard_option('pveclient-remote-name');
104 $path_properties->{api_path} = $api_path_property;
105
106 my $format_result = sub {
107 my ($data, $schema, $options) = @_;
108
109 # NOTE; we need to use $path_returns instead of $schema
110 PVE::APIClient::CLIFormatter::print_api_result($data, $path_returns, undef, $options);
111 };
112
113 __PACKAGE__->register_method ({
114 name => 'pveclient_get',
115 path => 'pveclient_get',
116 method => 'GET',
117 description => "Call API GET on <api_path>.",
118 parameters => {
119 additionalProperties => 0,
120 properties => $path_properties,
121 },
122 returns => $path_returns,
123 code => sub {
124 my ($param, $options) = @_;
125
126 return call_api_method('GET', $param, $options);
127 }});
128
129 __PACKAGE__->register_method ({
130 name => 'pveclient_set',
131 path => 'pveclient_set',
132 method => 'PUT',
133 description => "Call API PUT on <api_path>.",
134 parameters => {
135 additionalProperties => 0,
136 properties => $path_properties,
137 },
138 returns => $path_returns,
139 code => sub {
140 my ($param, $options) = @_;
141
142 return call_api_method('PUT', $param, $options);
143 }});
144
145 __PACKAGE__->register_method ({
146 name => 'pveclient_create',
147 path => 'pveclient_create',
148 method => 'POST',
149 description => "Call API POST on <api_path>.",
150 parameters => {
151 additionalProperties => 0,
152 properties => $path_properties,
153 },
154 returns => $path_returns,
155 code => sub {
156 my ($param, $options) = @_;
157
158 return call_api_method('PUSH', $param, $options);
159 }});
160
161 __PACKAGE__->register_method ({
162 name => 'pveclient_delete',
163 path => 'pveclient_delete',
164 method => 'DELETE',
165 description => "Call API DELETE on <api_path>.",
166 parameters => {
167 additionalProperties => 0,
168 properties => $path_properties,
169 },
170 returns => $path_returns,
171 code => sub {
172 my ($param, $options) = @_;
173
174 return call_api_method('DELETE', $param, $options);
175 }});
176
177 __PACKAGE__->register_method ({
178 name => 'pveclient_usage',
179 path => 'pveclient_usage',
180 method => 'GET',
181 description => "print API usage information for <api_path>.",
182 parameters => {
183 additionalProperties => 0,
184 properties => {
185 api_path => $api_path_property,
186 verbose => {
187 description => "Verbose output format.",
188 type => 'boolean',
189 optional => 1,
190 },
191 returns => {
192 description => "Including schema for returned data.",
193 type => 'boolean',
194 optional => 1,
195 },
196 command => {
197 description => "API command.",
198 type => 'string',
199 enum => [ keys %$PVE::APIClient::Helpers::method_map ],
200 optional => 1,
201 },
202 },
203 },
204 returns => { type => 'null' },
205 code => sub {
206 my ($param) = @_;
207
208 my $path = $param->{api_path};
209
210 my $found = 0;
211 foreach my $cmd (qw(get set create delete)) {
212 next if $param->{command} && $cmd ne $param->{command};
213 my $method = $PVE::APIClient::Helpers::method_map->{$cmd};
214 my $uri_param = {};
215 my $info = PVE::APIClient::Helpers::find_method_info($path, $method, $uri_param, 1);
216 next if !$info;
217 $found = 1;
218
219 my $prefix = "pveclient api $cmd <remote> $path";
220 if ($param->{verbose}) {
221 print PVE::APIClient::RESTHandler::getopt_usage(
222 $info, $prefix, undef, $uri_param, 'full');
223
224 } else {
225 print "USAGE: " . PVE::APIClient::RESTHandler::getopt_usage(
226 $info, $prefix, undef, $uri_param, 'short');
227 }
228 if ($param-> {returns}) {
229 my $schema = to_json($info->{returns}, {utf8 => 1, canonical => 1, pretty => 1 });
230 print "RETURNS: $schema\n";
231 }
232 }
233
234 if (!$found) {
235 if ($param->{command}) {
236 die "no '$param->{command}' handler for '$path'\n";
237 } else {
238 die "no such resource '$path'\n"
239 }
240 }
241
242 return undef;
243 }});
244
245 our $cmddef = {
246 config => $PVE::APIClient::Commands::config::cmddef,
247 list => $PVE::APIClient::Commands::list::cmddef,
248 lxc => $PVE::APIClient::Commands::lxc::cmddef,
249 remote => $PVE::APIClient::Commands::remote::cmddef,
250
251 resume => [ 'PVE::APIClient::Commands::GuestStatus', 'resume', ['remote', 'vmid']],
252 shutdown => [ 'PVE::APIClient::Commands::GuestStatus', 'shutdown', ['remote', 'vmid']],
253 spice => [ 'PVE::APIClient::Commands::GuestStatus', 'spice', ['remote', 'vmid']],
254 start => [ 'PVE::APIClient::Commands::GuestStatus', 'start', ['remote', 'vmid']],
255 stop => [ 'PVE::APIClient::Commands::GuestStatus', 'stop', ['remote', 'vmid']],
256 suspend => [ 'PVE::APIClient::Commands::GuestStatus', 'suspend', ['remote', 'vmid']],
257
258 api => {
259 usage => [ __PACKAGE__, 'pveclient_usage', ['api_path']],
260 get => [ __PACKAGE__, 'pveclient_get', ['remote', 'api_path'], {}, $format_result ],
261 set => [ __PACKAGE__, 'pveclient_set', ['remote', 'api_path'], {}, $format_result ],
262 create => [ __PACKAGE__, 'pveclient_create', ['remote', 'api_path'], {}, $format_result ],
263 delete => [ __PACKAGE__, 'pveclient_delete', ['remote', 'api_path'], {}, $format_result ],
264 },
265 };
266
267
268 if ($cmd && $cmd eq 'printsynopsis') {
269
270 print __PACKAGE__->generate_asciidoc_synopsis();
271
272 exit(0);
273 }
274
275 __PACKAGE__->run_cli_handler();