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