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