]>
Commit | Line | Data |
---|---|---|
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::CLIHandler; | |
12 | ||
13 | use PVE::APIClient::LWP; | |
14 | use PVE::APIClient::Helpers; | |
15 | use PVE::APIClient::Commands::config; | |
16 | use PVE::APIClient::Commands::remote; | |
17 | use PVE::APIClient::Commands::list; | |
18 | use PVE::APIClient::Commands::lxc; | |
19 | use PVE::APIClient::Commands::GuestStatus; | |
20 | ||
21 | use JSON; | |
22 | ||
23 | sub call_method { | |
24 | my ($remote, $path, $method, $params) = @_; | |
25 | ||
26 | die "missing API path\n" if !defined($path); | |
27 | ||
28 | my $info = PVE::APIClient::Helpers::lookup_api_method($path, $method); | |
29 | print Dumper($params); | |
30 | ||
31 | die "implement me"; | |
32 | } | |
33 | ||
34 | use base qw(PVE::APIClient::CLIHandler); | |
35 | ||
36 | my $cmd = $ARGV[0]; | |
37 | ||
38 | if ($cmd && $cmd eq 'packagedepends') { | |
39 | # experimental code to print required perl packages | |
40 | my $packages = {}; | |
41 | my $dir = Cwd::getcwd; | |
42 | ||
43 | foreach my $k (keys %INC) { | |
44 | my $file = abs_path($INC{$k}); | |
45 | next if $file =~ m/^\Q$dir\E/; | |
46 | my $res = `dpkg -S '$file'`; | |
47 | if ($res && $res =~ m/^(\S+): $file$/) { | |
48 | my $debian_package = $1; | |
49 | $debian_package =~ s/:amd64$//; | |
50 | $packages->{$debian_package} = 1; | |
51 | } else { | |
52 | die "unable to find package for '$file'\n"; | |
53 | } | |
54 | } | |
55 | print join("\n", sort(keys %$packages)) . "\n"; | |
56 | ||
57 | exit(0); | |
58 | } | |
59 | ||
60 | my $path_properties = {}; | |
61 | my $path_returns = { type => 'null' }; | |
62 | ||
63 | # dynamically update schema definition for direct API call | |
64 | # like: pveclient api <get|set|create|delete|help> <remote> <path> | |
65 | if (my $info = PVE::APIClient::Helpers::extract_path_info()) { | |
66 | $path_properties = $info->{parameters}->{properties}; | |
67 | $path_returns = $info->{returns}; | |
68 | } | |
69 | ||
70 | $path_properties->{remote} = get_standard_option('pveclient-remote-name'); | |
71 | $path_properties->{api_path} = { | |
72 | description => "API path.", | |
73 | type => 'string', | |
74 | completion => sub { | |
75 | my ($cmd, $pname, $cur, $args) = @_; | |
76 | return PVE::APIClient::Helpers::complete_api_path($cur); | |
77 | }, | |
78 | }; | |
79 | ||
80 | __PACKAGE__->register_method ({ | |
81 | name => 'pveclient_get', | |
82 | path => 'pveclient_get', | |
83 | method => 'GET', | |
84 | description => "call API GET on <path>.", | |
85 | parameters => { | |
86 | additionalProperties => 0, | |
87 | properties => $path_properties, | |
88 | }, | |
89 | returns => $path_returns, | |
90 | code => sub { | |
91 | my ($param) = @_; | |
92 | ||
93 | my $path = PVE::Tools::extract_param($param, 'api_path'); | |
94 | my $remote = PVE::Tools::extract_param($param, 'remote'); | |
95 | ||
96 | return call_method($remote, $path, 'GET', $param); | |
97 | }}); | |
98 | ||
99 | __PACKAGE__->register_method ({ | |
100 | name => 'pveclient_set', | |
101 | path => 'pveclient_set', | |
102 | method => 'PUT', | |
103 | description => "call API PUT on <path>.", | |
104 | parameters => { | |
105 | additionalProperties => 0, | |
106 | properties => $path_properties, | |
107 | }, | |
108 | returns => $path_returns, | |
109 | code => sub { | |
110 | my ($param) = @_; | |
111 | ||
112 | print Dumper($param); | |
113 | ||
114 | die "implement me"; | |
115 | ||
116 | }}); | |
117 | ||
118 | __PACKAGE__->register_method ({ | |
119 | name => 'pveclient_create', | |
120 | path => 'pveclient_create', | |
121 | method => 'PUSH', | |
122 | description => "call API PUSH on <path>.", | |
123 | parameters => { | |
124 | additionalProperties => 0, | |
125 | properties => $path_properties, | |
126 | }, | |
127 | returns => $path_returns, | |
128 | code => sub { | |
129 | my ($param) = @_; | |
130 | ||
131 | print Dumper($param); | |
132 | ||
133 | die "implement me"; | |
134 | ||
135 | }}); | |
136 | ||
137 | __PACKAGE__->register_method ({ | |
138 | name => 'pveclient_delete', | |
139 | path => 'pveclient_delete', | |
140 | method => 'DELETE', | |
141 | description => "call API DELETE on <path>.", | |
142 | parameters => { | |
143 | additionalProperties => 0, | |
144 | properties => $path_properties, | |
145 | }, | |
146 | returns => $path_returns, | |
147 | code => sub { | |
148 | my ($param) = @_; | |
149 | ||
150 | print Dumper($param); | |
151 | ||
152 | die "implement me"; | |
153 | ||
154 | }}); | |
155 | ||
156 | ||
157 | our $cmddef = { | |
158 | config => $PVE::APIClient::Commands::config::cmddef, | |
159 | list => $PVE::APIClient::Commands::list::cmddef, | |
160 | lxc => $PVE::APIClient::Commands::lxc::cmddef, | |
161 | remote => $PVE::APIClient::Commands::remote::cmddef, | |
162 | ||
163 | spice => [ 'PVE::APIClient::Commands::GuestStatus', 'spice', ['remote', 'vmid']], | |
164 | start => [ 'PVE::APIClient::Commands::GuestStatus', 'start', ['remote', 'vmid']], | |
165 | stop => [ 'PVE::APIClient::Commands::GuestStatus', 'stop', ['remote', 'vmid']], | |
166 | ||
167 | api => { | |
168 | get => [ __PACKAGE__, 'pveclient_get', ['remote', 'api_path']], | |
169 | set => [ __PACKAGE__, 'pveclient_set', ['remote', 'api_path']], | |
170 | create => [ __PACKAGE__, 'pveclient_create', ['remote', 'api_path']], | |
171 | delete => [ __PACKAGE__, 'pveclient_delete', ['remote', 'api_path']], | |
172 | } | |
173 | }; | |
174 | ||
175 | ||
176 | __PACKAGE__->run_cli_handler(); |