]> git.proxmox.com Git - pve-client.git/blame - pveclient
correctly use PVE::APIClient::PTY
[pve-client.git] / pveclient
CommitLineData
29505e2c
DM
1#!/usr/bin/perl
2
b133a905
DM
3package PVE::CLI::pveclient;
4
29505e2c
DM
5use strict;
6use warnings;
f5fcd826 7use Cwd 'abs_path';
29505e2c
DM
8use Data::Dumper;
9
c9138c03
DM
10use PVE::APIClient::JSONSchema qw(register_standard_option get_standard_option);
11use PVE::APIClient::CLIHandler;
4a7e63b8 12use PVE::APIClient::PTY;
565bbc73 13
29505e2c
DM
14use PVE::APIClient::LWP;
15use PVE::APIClient::Helpers;
b0495d82 16use PVE::APIClient::Config;
a6dab5b8 17use PVE::APIClient::Commands::config;
565bbc73 18use PVE::APIClient::Commands::remote;
5b090843 19use PVE::APIClient::Commands::list;
adf285bf 20use PVE::APIClient::Commands::lxc;
89335fb1 21use PVE::APIClient::Commands::GuestStatus;
565bbc73 22
29505e2c
DM
23use JSON;
24
61ad3df5
DM
25sub 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
39 # test if api path exists
29505e2c 40 my $info = PVE::APIClient::Helpers::lookup_api_method($path, $method);
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 51use base qw(PVE::APIClient::CLIHandler);
3a1bc22f 52
4a7e63b8
DM
53sub read_password {
54 return PVE::APIClient::PTY::read_password("Remote password: ")
55}
56
57
b133a905 58my $cmd = $ARGV[0];
dce6ecbc 59
b133a905 60if ($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
82my $path_properties = {};
83my $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>
87if (my $info = PVE::APIClient::Helpers::extract_path_info()) {
88 $path_properties = $info->{parameters}->{properties};
89 $path_returns = $info->{returns};
29505e2c
DM
90}
91
61ad3df5 92$path_properties->{format} = get_standard_option('pveclient-output-format'),
b133a905
DM
93$path_properties->{remote} = get_standard_option('pveclient-remote-name');
94$path_properties->{api_path} = {
95 description => "API path.",
96 type => 'string',
97 completion => sub {
98 my ($cmd, $pname, $cur, $args) = @_;
99 return PVE::APIClient::Helpers::complete_api_path($cur);
100 },
101};
102
61ad3df5 103
b0495d82 104my $format_result = sub {
61ad3df5
DM
105 my ($data) = @_;
106
4db9ff67 107 PVE::APIClient::Helpers::print_result($data, $path_returns);
b0495d82
DM
108};
109
b133a905
DM
110__PACKAGE__->register_method ({
111 name => 'pveclient_get',
112 path => 'pveclient_get',
113 method => 'GET',
114 description => "call API GET on <path>.",
115 parameters => {
116 additionalProperties => 0,
117 properties => $path_properties,
118 },
119 returns => $path_returns,
120 code => sub {
121 my ($param) = @_;
122
61ad3df5 123 return call_api_method('GET', $param);
b133a905
DM
124 }});
125
126__PACKAGE__->register_method ({
127 name => 'pveclient_set',
128 path => 'pveclient_set',
129 method => 'PUT',
130 description => "call API PUT on <path>.",
131 parameters => {
132 additionalProperties => 0,
133 properties => $path_properties,
134 },
135 returns => $path_returns,
136 code => sub {
137 my ($param) = @_;
138
61ad3df5 139 return call_api_method('PUT', $param);
b133a905
DM
140 }});
141
142__PACKAGE__->register_method ({
143 name => 'pveclient_create',
144 path => 'pveclient_create',
145 method => 'PUSH',
146 description => "call API PUSH on <path>.",
147 parameters => {
148 additionalProperties => 0,
149 properties => $path_properties,
150 },
151 returns => $path_returns,
152 code => sub {
153 my ($param) = @_;
154
61ad3df5 155 return call_api_method('PUSH', $param);
b133a905
DM
156 }});
157
158__PACKAGE__->register_method ({
159 name => 'pveclient_delete',
160 path => 'pveclient_delete',
161 method => 'DELETE',
162 description => "call API DELETE on <path>.",
163 parameters => {
164 additionalProperties => 0,
165 properties => $path_properties,
166 },
167 returns => $path_returns,
168 code => sub {
169 my ($param) = @_;
170
61ad3df5 171 return call_api_method('DELETE', $param);
b133a905
DM
172 }});
173
174
175our $cmddef = {
176 config => $PVE::APIClient::Commands::config::cmddef,
177 list => $PVE::APIClient::Commands::list::cmddef,
178 lxc => $PVE::APIClient::Commands::lxc::cmddef,
179 remote => $PVE::APIClient::Commands::remote::cmddef,
4cedebf6
RJ
180
181 spice => [ 'PVE::APIClient::Commands::GuestStatus', 'spice', ['remote', 'vmid']],
89335fb1
DM
182 start => [ 'PVE::APIClient::Commands::GuestStatus', 'start', ['remote', 'vmid']],
183 stop => [ 'PVE::APIClient::Commands::GuestStatus', 'stop', ['remote', 'vmid']],
b133a905
DM
184
185 api => {
b0495d82
DM
186 get => [ __PACKAGE__, 'pveclient_get', ['remote', 'api_path'], {}, $format_result ],
187 set => [ __PACKAGE__, 'pveclient_set', ['remote', 'api_path'], {}, $format_result ],
188 create => [ __PACKAGE__, 'pveclient_create', ['remote', 'api_path'], {}, $format_result ],
189 delete => [ __PACKAGE__, 'pveclient_delete', ['remote', 'api_path'], {}, $format_result ],
190 },
b133a905
DM
191};
192
193
194__PACKAGE__->run_cli_handler();