]>
Commit | Line | Data |
---|---|---|
29505e2c DM |
1 | #!/usr/bin/perl |
2 | ||
b133a905 DM |
3 | package PVE::CLI::pveclient; |
4 | ||
29505e2c DM |
5 | use strict; |
6 | use warnings; | |
f5fcd826 | 7 | use Cwd 'abs_path'; |
29505e2c DM |
8 | use Data::Dumper; |
9 | ||
c9138c03 | 10 | use PVE::APIClient::JSONSchema qw(register_standard_option get_standard_option); |
5101e472 | 11 | use PVE::APIClient::CLIFormatter; |
c9138c03 | 12 | use PVE::APIClient::CLIHandler; |
4a7e63b8 | 13 | use PVE::APIClient::PTY; |
565bbc73 | 14 | |
29505e2c DM |
15 | use PVE::APIClient::LWP; |
16 | use PVE::APIClient::Helpers; | |
b0495d82 | 17 | use PVE::APIClient::Config; |
a6dab5b8 | 18 | use PVE::APIClient::Commands::config; |
565bbc73 | 19 | use PVE::APIClient::Commands::remote; |
5b090843 | 20 | use PVE::APIClient::Commands::list; |
adf285bf | 21 | use PVE::APIClient::Commands::lxc; |
89335fb1 | 22 | use PVE::APIClient::Commands::GuestStatus; |
565bbc73 | 23 | |
29505e2c DM |
24 | use JSON; |
25 | ||
61ad3df5 | 26 | sub call_api_method { |
59d3653b | 27 | my ($method, $param, $options) = @_; |
29505e2c | 28 | |
61ad3df5 | 29 | my $path = PVE::APIClient::Tools::extract_param($param, 'api_path'); |
29505e2c DM |
30 | die "missing API path\n" if !defined($path); |
31 | ||
61ad3df5 DM |
32 | my $remote = PVE::APIClient::Tools::extract_param($param, 'remote'); |
33 | die "missing remote\n" if !defined($remote); | |
34 | ||
b0495d82 DM |
35 | my $config = PVE::APIClient::Config->load(); |
36 | ||
520f543e DM |
37 | my $uri_param = {}; |
38 | my $info = PVE::APIClient::Helpers::find_method_info($path, $method, $uri_param); | |
29505e2c | 39 | |
b0495d82 DM |
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}; | |
29505e2c DM |
47 | } |
48 | ||
c9138c03 | 49 | use base qw(PVE::APIClient::CLIHandler); |
3a1bc22f | 50 | |
4a7e63b8 | 51 | sub read_password { |
4fbacba6 | 52 | return PVE::APIClient::PTY::read_password("Remote password: ") |
4a7e63b8 DM |
53 | } |
54 | ||
55 | ||
b133a905 | 56 | my $cmd = $ARGV[0]; |
dce6ecbc | 57 | |
b133a905 | 58 | if ($cmd && $cmd eq 'packagedepends') { |
f5fcd826 DM |
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"; | |
b133a905 DM |
76 | |
77 | exit(0); | |
78 | } | |
79 | ||
80 | my $path_properties = {}; | |
81 | my $path_returns = { type => 'null' }; | |
82 | ||
55d17e7e DM |
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 | ||
b133a905 DM |
92 | # dynamically update schema definition for direct API call |
93 | # like: pveclient api <get|set|create|delete|help> <remote> <path> | |
520f543e DM |
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 | } | |
b133a905 | 100 | $path_returns = $info->{returns}; |
29505e2c DM |
101 | } |
102 | ||
b133a905 | 103 | $path_properties->{remote} = get_standard_option('pveclient-remote-name'); |
55d17e7e | 104 | $path_properties->{api_path} = $api_path_property; |
61ad3df5 | 105 | |
b0495d82 | 106 | my $format_result = sub { |
59d3653b | 107 | my ($data, $schema, $options) = @_; |
5101e472 | 108 | |
59d3653b DM |
109 | # NOTE; we need to use $path_returns instead of $schema |
110 | PVE::APIClient::CLIFormatter::print_api_result($data, $path_returns, undef, $options); | |
b0495d82 DM |
111 | }; |
112 | ||
b133a905 DM |
113 | __PACKAGE__->register_method ({ |
114 | name => 'pveclient_get', | |
115 | path => 'pveclient_get', | |
116 | method => 'GET', | |
4fbacba6 | 117 | description => "Call API GET on <api_path>.", |
b133a905 DM |
118 | parameters => { |
119 | additionalProperties => 0, | |
120 | properties => $path_properties, | |
121 | }, | |
122 | returns => $path_returns, | |
123 | code => sub { | |
59d3653b | 124 | my ($param, $options) = @_; |
b133a905 | 125 | |
59d3653b | 126 | return call_api_method('GET', $param, $options); |
b133a905 DM |
127 | }}); |
128 | ||
129 | __PACKAGE__->register_method ({ | |
130 | name => 'pveclient_set', | |
131 | path => 'pveclient_set', | |
132 | method => 'PUT', | |
4fbacba6 | 133 | description => "Call API PUT on <api_path>.", |
b133a905 DM |
134 | parameters => { |
135 | additionalProperties => 0, | |
136 | properties => $path_properties, | |
137 | }, | |
138 | returns => $path_returns, | |
139 | code => sub { | |
59d3653b | 140 | my ($param, $options) = @_; |
b133a905 | 141 | |
59d3653b | 142 | return call_api_method('PUT', $param, $options); |
b133a905 DM |
143 | }}); |
144 | ||
145 | __PACKAGE__->register_method ({ | |
146 | name => 'pveclient_create', | |
147 | path => 'pveclient_create', | |
faa47c20 DM |
148 | method => 'POST', |
149 | description => "Call API POST on <api_path>.", | |
b133a905 DM |
150 | parameters => { |
151 | additionalProperties => 0, | |
152 | properties => $path_properties, | |
153 | }, | |
154 | returns => $path_returns, | |
155 | code => sub { | |
59d3653b | 156 | my ($param, $options) = @_; |
b133a905 | 157 | |
59d3653b | 158 | return call_api_method('PUSH', $param, $options); |
b133a905 DM |
159 | }}); |
160 | ||
161 | __PACKAGE__->register_method ({ | |
162 | name => 'pveclient_delete', | |
163 | path => 'pveclient_delete', | |
164 | method => 'DELETE', | |
4fbacba6 | 165 | description => "Call API DELETE on <api_path>.", |
b133a905 DM |
166 | parameters => { |
167 | additionalProperties => 0, | |
168 | properties => $path_properties, | |
169 | }, | |
170 | returns => $path_returns, | |
171 | code => sub { | |
59d3653b | 172 | my ($param, $options) = @_; |
b133a905 | 173 | |
59d3653b | 174 | return call_api_method('DELETE', $param, $options); |
b133a905 DM |
175 | }}); |
176 | ||
55d17e7e DM |
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 | }}); | |
b133a905 DM |
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, | |
4cedebf6 | 250 | |
42de9764 RJ |
251 | resume => [ 'PVE::APIClient::Commands::GuestStatus', 'resume', ['remote', 'vmid']], |
252 | shutdown => [ 'PVE::APIClient::Commands::GuestStatus', 'shutdown', ['remote', 'vmid']], | |
4cedebf6 | 253 | spice => [ 'PVE::APIClient::Commands::GuestStatus', 'spice', ['remote', 'vmid']], |
89335fb1 DM |
254 | start => [ 'PVE::APIClient::Commands::GuestStatus', 'start', ['remote', 'vmid']], |
255 | stop => [ 'PVE::APIClient::Commands::GuestStatus', 'stop', ['remote', 'vmid']], | |
42de9764 | 256 | suspend => [ 'PVE::APIClient::Commands::GuestStatus', 'suspend', ['remote', 'vmid']], |
b133a905 DM |
257 | |
258 | api => { | |
55d17e7e | 259 | usage => [ __PACKAGE__, 'pveclient_usage', ['api_path']], |
b0495d82 DM |
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 | }, | |
b133a905 DM |
265 | }; |
266 | ||
267 | ||
2767c2b9 DM |
268 | if ($cmd && $cmd eq 'printsynopsis') { |
269 | ||
270 | print __PACKAGE__->generate_asciidoc_synopsis(); | |
271 | ||
272 | exit(0); | |
273 | } | |
274 | ||
b133a905 | 275 | __PACKAGE__->run_cli_handler(); |