]> git.proxmox.com Git - pmg-api.git/blob - PMG/API2/Nodes.pm
call verifyapi before install
[pmg-api.git] / PMG / API2 / Nodes.pm
1 package PMG::API2::NodeInfo;
2
3 use strict;
4 use warnings;
5
6 use PVE::INotify;
7 use PVE::RESTHandler;
8 use PVE::JSONSchema qw(get_standard_option);
9 use PVE::RESTEnvironment;
10 use PVE::SafeSyslog;
11
12 use PMG::Ticket;
13
14 use base qw(PVE::RESTHandler);
15
16 __PACKAGE__->register_method ({
17 name => 'index',
18 path => '',
19 method => 'GET',
20 permissions => { user => 'all' },
21 description => "Node index.",
22 parameters => {
23 additionalProperties => 0,
24 properties => {
25 node => get_standard_option('pve-node'),
26 },
27 },
28 returns => {
29 type => 'array',
30 items => {
31 type => "object",
32 properties => {},
33 },
34 links => [ { rel => 'child', href => "{name}" } ],
35 },
36 code => sub {
37 my ($param) = @_;
38
39 my $result = [
40 { name => 'vncshell' },
41 ];
42
43 return $result;
44 }});
45
46 __PACKAGE__->register_method ({
47 name => 'vncshell',
48 path => 'vncshell',
49 method => 'POST',
50 description => "Creates a VNC Shell proxy.",
51 parameters => {
52 additionalProperties => 0,
53 properties => {
54 node => get_standard_option('pve-node'),
55 websocket => {
56 optional => 1,
57 type => 'boolean',
58 description => "use websocket instead of standard vnc.",
59 default => 1,
60 },
61 },
62 },
63 returns => {
64 additionalProperties => 0,
65 properties => {
66 user => { type => 'string' },
67 ticket => { type => 'string' },
68 port => { type => 'integer' },
69 upid => { type => 'string' },
70 },
71 },
72 code => sub {
73 my ($param) = @_;
74
75 my $node = $param->{node};
76
77 # we only implement the websocket based VNC here
78 my $websocket = $param->{websocket} // 1;
79 die "standard VNC not implemented" if !$websocket;
80
81 my $authpath = "/nodes/$node";
82
83 my $restenv = PVE::RESTEnvironment->get();
84 my $user = $restenv->get_user();
85
86 my $ticket = PMG::Ticket::assemble_vnc_ticket($user, $authpath);
87
88 my $family = PVE::Tools::get_host_address_family($node);
89 my $port = PVE::Tools::next_vnc_port($family);
90
91 my $cmd = ['/usr/bin/vncterm', '-rfbport', $port,
92 '-timeout', 10, '-notls', '-listen', 'localhost',
93 '-c', '/usr/bin/top'];
94
95 my $realcmd = sub {
96 my $upid = shift;
97
98 syslog ('info', "starting vnc proxy $upid\n");
99
100 my $cmdstr = join (' ', @$cmd);
101 syslog ('info', "launch command: $cmdstr");
102
103 eval {
104 foreach my $k (keys %ENV) {
105 next if $k eq 'PATH' || $k eq 'TERM' || $k eq 'USER' || $k eq 'HOME';
106 delete $ENV{$k};
107 }
108 $ENV{PWD} = '/';
109
110 $ENV{PVE_VNC_TICKET} = $ticket; # pass ticket to vncterm
111
112 PVE::Tools::run_command($cmd, errmsg => "vncterm failed");
113 };
114 if (my $err = $@) {
115 syslog('err', $err);
116 }
117
118 return;
119 };
120
121 my $upid = $restenv->fork_worker('vncshell', "", $user, $realcmd);
122
123 PVE::Tools::wait_for_vnc_port($port);
124
125 return {
126 user => $user,
127 ticket => $ticket,
128 port => $port,
129 upid => $upid,
130 };
131 }});
132
133 __PACKAGE__->register_method({
134 name => 'vncwebsocket',
135 path => 'vncwebsocket',
136 method => 'GET',
137 description => "Opens a weksocket for VNC traffic.",
138 parameters => {
139 additionalProperties => 0,
140 properties => {
141 node => get_standard_option('pve-node'),
142 vncticket => {
143 description => "Ticket from previous call to vncproxy.",
144 type => 'string',
145 maxLength => 512,
146 },
147 port => {
148 description => "Port number returned by previous vncproxy call.",
149 type => 'integer',
150 minimum => 5900,
151 maximum => 5999,
152 },
153 },
154 },
155 returns => {
156 type => "object",
157 properties => {
158 port => { type => 'string' },
159 },
160 },
161 code => sub {
162 my ($param) = @_;
163
164 my $authpath = "/nodes/$param->{node}";
165
166 my $restenv = PVE::RESTEnvironment->get();
167 my $user = $restenv->get_user();
168
169 PMG::Ticket::verify_vnc_ticket($param->{vncticket}, $user, $authpath);
170
171 my $port = $param->{port};
172
173 return { port => $port };
174 }});
175
176
177 package PMG::API2::Nodes;
178
179 use strict;
180 use warnings;
181
182 use PVE::RESTHandler;
183 use PVE::JSONSchema qw(get_standard_option);
184
185 use base qw(PVE::RESTHandler);
186
187 __PACKAGE__->register_method ({
188 subclass => "PMG::API2::NodeInfo",
189 path => '{node}',
190 });
191
192 __PACKAGE__->register_method ({
193 name => 'index',
194 path => '',
195 method => 'GET',
196 permissions => { user => 'all' },
197 description => "Cluster node index.",
198 parameters => {
199 additionalProperties => 0,
200 properties => {},
201 },
202 returns => {
203 type => 'array',
204 items => {
205 type => "object",
206 properties => {},
207 },
208 links => [ { rel => 'child', href => "{node}" } ],
209 },
210 code => sub {
211 my ($param) = @_;
212
213 my $nodename = PVE::INotify::nodename();
214 my $res = [
215 { node => $nodename },
216 ];
217
218 return $res;
219 }});
220
221
222 1;