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