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