]> git.proxmox.com Git - pve-client.git/blob - pveclient
config set: check digest
[pve-client.git] / pveclient
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::CLIFormatter;
12 use PVE::APIClient::CLIHandler;
13 use PVE::APIClient::PTY;
14
15 use PVE::APIClient::LWP;
16 use PVE::APIClient::Helpers;
17 use PVE::APIClient::Config;
18 use PVE::APIClient::Commands::config;
19 use PVE::APIClient::Commands::remote;
20 use PVE::APIClient::Commands::list;
21 use PVE::APIClient::Commands::lxc;
22 use PVE::APIClient::Commands::GuestStatus;
23
24 use JSON;
25
26 sub call_api_method {
27 my ($method, $param) = @_;
28
29 my $path = PVE::APIClient::Tools::extract_param($param, 'api_path');
30 die "missing API path\n" if !defined($path);
31
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
38 my $config = PVE::APIClient::Config->load();
39
40 my $uri_param = {};
41 my $info = PVE::APIClient::Helpers::find_method_info($path, $method, $uri_param);
42
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};
50 }
51
52 use base qw(PVE::APIClient::CLIHandler);
53
54 sub read_password {
55 return PVE::APIClient::PTY::read_password("Remote password: ")
56 }
57
58
59 my $cmd = $ARGV[0];
60
61 if ($cmd && $cmd eq 'packagedepends') {
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";
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>
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 }
94 $path_returns = $info->{returns};
95 }
96
97 $path_properties->{format} = get_standard_option('pve-output-format'),
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
108
109 my $format_result = sub {
110 my ($data) = @_;
111
112 my $format = PVE::APIClient::Helpers::get_output_format();
113
114 my $options = PVE::APIClient::CLIFormatter::query_terminal_options({});
115
116 PVE::APIClient::CLIFormatter::print_api_result($format, $data, $path_returns, undef, $options);
117 };
118
119 __PACKAGE__->register_method ({
120 name => 'pveclient_get',
121 path => 'pveclient_get',
122 method => 'GET',
123 description => "Call API GET on <api_path>.",
124 parameters => {
125 additionalProperties => 0,
126 properties => $path_properties,
127 },
128 returns => $path_returns,
129 code => sub {
130 my ($param) = @_;
131
132 return call_api_method('GET', $param);
133 }});
134
135 __PACKAGE__->register_method ({
136 name => 'pveclient_set',
137 path => 'pveclient_set',
138 method => 'PUT',
139 description => "Call API PUT on <api_path>.",
140 parameters => {
141 additionalProperties => 0,
142 properties => $path_properties,
143 },
144 returns => $path_returns,
145 code => sub {
146 my ($param) = @_;
147
148 return call_api_method('PUT', $param);
149 }});
150
151 __PACKAGE__->register_method ({
152 name => 'pveclient_create',
153 path => 'pveclient_create',
154 method => 'POST',
155 description => "Call API POST on <api_path>.",
156 parameters => {
157 additionalProperties => 0,
158 properties => $path_properties,
159 },
160 returns => $path_returns,
161 code => sub {
162 my ($param) = @_;
163
164 return call_api_method('PUSH', $param);
165 }});
166
167 __PACKAGE__->register_method ({
168 name => 'pveclient_delete',
169 path => 'pveclient_delete',
170 method => 'DELETE',
171 description => "Call API DELETE on <api_path>.",
172 parameters => {
173 additionalProperties => 0,
174 properties => $path_properties,
175 },
176 returns => $path_returns,
177 code => sub {
178 my ($param) = @_;
179
180 return call_api_method('DELETE', $param);
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,
189
190 resume => [ 'PVE::APIClient::Commands::GuestStatus', 'resume', ['remote', 'vmid']],
191 shutdown => [ 'PVE::APIClient::Commands::GuestStatus', 'shutdown', ['remote', 'vmid']],
192 spice => [ 'PVE::APIClient::Commands::GuestStatus', 'spice', ['remote', 'vmid']],
193 start => [ 'PVE::APIClient::Commands::GuestStatus', 'start', ['remote', 'vmid']],
194 stop => [ 'PVE::APIClient::Commands::GuestStatus', 'stop', ['remote', 'vmid']],
195 suspend => [ 'PVE::APIClient::Commands::GuestStatus', 'suspend', ['remote', 'vmid']],
196
197 api => {
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 },
203 };
204
205
206 if ($cmd && $cmd eq 'printsynopsis') {
207
208 print __PACKAGE__->generate_asciidoc_synopsis();
209
210 exit(0);
211 }
212
213 __PACKAGE__->run_cli_handler();