]>
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 DM |
26 | sub call_api_method { |
27 | my ($method, $param) = @_; | |
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 | ||
35 | my $format = PVE::APIClient::Tools::extract_param($param, 'format'); | |
36 | PVE::APIClient::Helpers::set_output_format($format); | |
37 | ||
b0495d82 DM |
38 | my $config = PVE::APIClient::Config->load(); |
39 | ||
520f543e DM |
40 | my $uri_param = {}; |
41 | my $info = PVE::APIClient::Helpers::find_method_info($path, $method, $uri_param); | |
29505e2c | 42 | |
b0495d82 DM |
43 | my $conn = PVE::APIClient::Config->remote_conn($config, $remote); |
44 | ||
45 | my $res = $conn->call($method, "api2/json/$path", $param); | |
46 | die "undefined result" if !defined($res); | |
47 | die "undefined result data" if !exists($res->{data}); | |
48 | ||
49 | return $res->{data}; | |
29505e2c DM |
50 | } |
51 | ||
c9138c03 | 52 | use base qw(PVE::APIClient::CLIHandler); |
3a1bc22f | 53 | |
4a7e63b8 | 54 | sub read_password { |
4fbacba6 | 55 | return PVE::APIClient::PTY::read_password("Remote password: ") |
4a7e63b8 DM |
56 | } |
57 | ||
58 | ||
b133a905 | 59 | my $cmd = $ARGV[0]; |
dce6ecbc | 60 | |
b133a905 | 61 | if ($cmd && $cmd eq 'packagedepends') { |
f5fcd826 DM |
62 | # experimental code to print required perl packages |
63 | my $packages = {}; | |
64 | my $dir = Cwd::getcwd; | |
65 | ||
66 | foreach my $k (keys %INC) { | |
67 | my $file = abs_path($INC{$k}); | |
68 | next if $file =~ m/^\Q$dir\E/; | |
69 | my $res = `dpkg -S '$file'`; | |
70 | if ($res && $res =~ m/^(\S+): $file$/) { | |
71 | my $debian_package = $1; | |
72 | $debian_package =~ s/:amd64$//; | |
73 | $packages->{$debian_package} = 1; | |
74 | } else { | |
75 | die "unable to find package for '$file'\n"; | |
76 | } | |
77 | } | |
78 | print join("\n", sort(keys %$packages)) . "\n"; | |
b133a905 DM |
79 | |
80 | exit(0); | |
81 | } | |
82 | ||
83 | my $path_properties = {}; | |
84 | my $path_returns = { type => 'null' }; | |
85 | ||
86 | # dynamically update schema definition for direct API call | |
87 | # like: pveclient api <get|set|create|delete|help> <remote> <path> | |
520f543e DM |
88 | my $uri_param = {}; |
89 | if (my $info = PVE::APIClient::Helpers::extract_path_info($uri_param)) { | |
90 | foreach my $key (keys %{$info->{parameters}->{properties}}) { | |
91 | next if defined($uri_param->{$key}); | |
92 | $path_properties->{$key} = $info->{parameters}->{properties}->{$key}; | |
93 | } | |
b133a905 | 94 | $path_returns = $info->{returns}; |
29505e2c DM |
95 | } |
96 | ||
faa47c20 | 97 | $path_properties->{format} = get_standard_option('pve-output-format'), |
b133a905 DM |
98 | $path_properties->{remote} = get_standard_option('pveclient-remote-name'); |
99 | $path_properties->{api_path} = { | |
100 | description => "API path.", | |
101 | type => 'string', | |
102 | completion => sub { | |
103 | my ($cmd, $pname, $cur, $args) = @_; | |
104 | return PVE::APIClient::Helpers::complete_api_path($cur); | |
105 | }, | |
106 | }; | |
107 | ||
61ad3df5 | 108 | |
b0495d82 | 109 | my $format_result = sub { |
61ad3df5 DM |
110 | my ($data) = @_; |
111 | ||
c27b93a5 | 112 | my $format = PVE::APIClient::Helpers::get_output_format(); |
5101e472 DM |
113 | |
114 | my $options = PVE::APIClient::CLIFormatter::query_terminal_options({}); | |
115 | ||
116 | PVE::APIClient::CLIFormatter::print_api_result($format, $data, $path_returns, undef, $options); | |
b0495d82 DM |
117 | }; |
118 | ||
b133a905 DM |
119 | __PACKAGE__->register_method ({ |
120 | name => 'pveclient_get', | |
121 | path => 'pveclient_get', | |
122 | method => 'GET', | |
4fbacba6 | 123 | description => "Call API GET on <api_path>.", |
b133a905 DM |
124 | parameters => { |
125 | additionalProperties => 0, | |
126 | properties => $path_properties, | |
127 | }, | |
128 | returns => $path_returns, | |
129 | code => sub { | |
130 | my ($param) = @_; | |
131 | ||
61ad3df5 | 132 | return call_api_method('GET', $param); |
b133a905 DM |
133 | }}); |
134 | ||
135 | __PACKAGE__->register_method ({ | |
136 | name => 'pveclient_set', | |
137 | path => 'pveclient_set', | |
138 | method => 'PUT', | |
4fbacba6 | 139 | description => "Call API PUT on <api_path>.", |
b133a905 DM |
140 | parameters => { |
141 | additionalProperties => 0, | |
142 | properties => $path_properties, | |
143 | }, | |
144 | returns => $path_returns, | |
145 | code => sub { | |
146 | my ($param) = @_; | |
147 | ||
61ad3df5 | 148 | return call_api_method('PUT', $param); |
b133a905 DM |
149 | }}); |
150 | ||
151 | __PACKAGE__->register_method ({ | |
152 | name => 'pveclient_create', | |
153 | path => 'pveclient_create', | |
faa47c20 DM |
154 | method => 'POST', |
155 | description => "Call API POST on <api_path>.", | |
b133a905 DM |
156 | parameters => { |
157 | additionalProperties => 0, | |
158 | properties => $path_properties, | |
159 | }, | |
160 | returns => $path_returns, | |
161 | code => sub { | |
162 | my ($param) = @_; | |
163 | ||
61ad3df5 | 164 | return call_api_method('PUSH', $param); |
b133a905 DM |
165 | }}); |
166 | ||
167 | __PACKAGE__->register_method ({ | |
168 | name => 'pveclient_delete', | |
169 | path => 'pveclient_delete', | |
170 | method => 'DELETE', | |
4fbacba6 | 171 | description => "Call API DELETE on <api_path>.", |
b133a905 DM |
172 | parameters => { |
173 | additionalProperties => 0, | |
174 | properties => $path_properties, | |
175 | }, | |
176 | returns => $path_returns, | |
177 | code => sub { | |
178 | my ($param) = @_; | |
179 | ||
61ad3df5 | 180 | return call_api_method('DELETE', $param); |
b133a905 DM |
181 | }}); |
182 | ||
183 | ||
184 | our $cmddef = { | |
185 | config => $PVE::APIClient::Commands::config::cmddef, | |
186 | list => $PVE::APIClient::Commands::list::cmddef, | |
187 | lxc => $PVE::APIClient::Commands::lxc::cmddef, | |
188 | remote => $PVE::APIClient::Commands::remote::cmddef, | |
4cedebf6 | 189 | |
42de9764 RJ |
190 | resume => [ 'PVE::APIClient::Commands::GuestStatus', 'resume', ['remote', 'vmid']], |
191 | shutdown => [ 'PVE::APIClient::Commands::GuestStatus', 'shutdown', ['remote', 'vmid']], | |
4cedebf6 | 192 | spice => [ 'PVE::APIClient::Commands::GuestStatus', 'spice', ['remote', 'vmid']], |
89335fb1 DM |
193 | start => [ 'PVE::APIClient::Commands::GuestStatus', 'start', ['remote', 'vmid']], |
194 | stop => [ 'PVE::APIClient::Commands::GuestStatus', 'stop', ['remote', 'vmid']], | |
42de9764 | 195 | suspend => [ 'PVE::APIClient::Commands::GuestStatus', 'suspend', ['remote', 'vmid']], |
b133a905 DM |
196 | |
197 | api => { | |
b0495d82 DM |
198 | get => [ __PACKAGE__, 'pveclient_get', ['remote', 'api_path'], {}, $format_result ], |
199 | set => [ __PACKAGE__, 'pveclient_set', ['remote', 'api_path'], {}, $format_result ], | |
200 | create => [ __PACKAGE__, 'pveclient_create', ['remote', 'api_path'], {}, $format_result ], | |
201 | delete => [ __PACKAGE__, 'pveclient_delete', ['remote', 'api_path'], {}, $format_result ], | |
202 | }, | |
b133a905 DM |
203 | }; |
204 | ||
205 | ||
2767c2b9 DM |
206 | if ($cmd && $cmd eq 'printsynopsis') { |
207 | ||
208 | print __PACKAGE__->generate_asciidoc_synopsis(); | |
209 | ||
210 | exit(0); | |
211 | } | |
212 | ||
b133a905 | 213 | __PACKAGE__->run_cli_handler(); |