]> git.proxmox.com Git - pve-manager.git/blob - PVE/API2/Tasks.pm
implement PasswordEdit dialog
[pve-manager.git] / PVE / API2 / Tasks.pm
1 package PVE::API2::Tasks;
2
3 use strict;
4 use warnings;
5 use POSIX;
6 use IO::File;
7 use File::ReadBackwards;
8 use PVE::Tools;
9 use PVE::SafeSyslog;
10 use PVE::RESTHandler;
11 use PVE::ProcFSTools;
12 use PVE::RPCEnvironment;
13 use PVE::JSONSchema qw(get_standard_option);
14 use PVE::AccessControl;
15
16 use base qw(PVE::RESTHandler);
17
18 __PACKAGE__->register_method({
19 name => 'node_tasks',
20 path => '',
21 method => 'GET',
22 permissions => { user => 'all' },
23 description => "Read task list for one node (finished tasks).",
24 proxyto => 'node',
25 parameters => {
26 additionalProperties => 0,
27 properties => {
28 node => get_standard_option('pve-node'),
29 start => {
30 type => 'integer',
31 minimum => 0,
32 optional => 1,
33 },
34 limit => {
35 type => 'integer',
36 minimum => 0,
37 optional => 1,
38 },
39 userfilter => {
40 type => 'string',
41 optional => 1,
42 },
43 errors => {
44 type => 'boolean',
45 optional => 1,
46 },
47 },
48 },
49 returns => {
50 type => 'array',
51 items => {
52 type => "object",
53 properties => {
54 upid => { type => 'string' },
55 },
56 },
57 links => [ { rel => 'child', href => "{upid}" } ],
58 },
59 code => sub {
60 my ($param) = @_;
61
62 my $rpcenv = PVE::RPCEnvironment::get();
63 my $user = $rpcenv->get_user();
64
65 my $res = [];
66
67 my $filename = "/var/log/pve/tasks/index";
68
69 my $node = $param->{node};
70 my $start = $param->{start} || 0;
71 my $limit = $param->{limit} || 50;
72 my $userfilter = $param->{userfilter};
73 my $errors = $param->{errors};
74
75 my $count = 0;
76 my $line;
77
78 my $auditor = $rpcenv->check($user, "/nodes/$node", [ 'Sys.Audit' ], 1);
79
80 my $parse_line = sub {
81 if ($line =~ m/^(\S+)(\s([0-9A-Za-z]{8})(\s(\S.*))?)?$/) {
82 my $upid = $1;
83 my $endtime = $3;
84 my $status = $5;
85 if ((my $task = PVE::Tools::upid_decode($upid, 1))) {
86 return if $userfilter && $task->{user} !~ m/\Q$userfilter\E/i;
87 next if !($auditor || $user eq $task->{user});
88
89 return if $errors && $status && $status eq 'OK';
90
91 return if $count++ < $start;
92 return if $limit <= 0;
93
94 $task->{upid} = $upid;
95 $task->{endtime} = hex($endtime) if $endtime;
96 $task->{status} = $status if $status;
97 push @$res, $task;
98 $limit--;
99 }
100 }
101 };
102
103 if (my $bw = File::ReadBackwards->new($filename)) {
104 while (defined ($line = $bw->readline)) {
105 &$parse_line();
106 }
107 $bw->close();
108 }
109 if (my $bw = File::ReadBackwards->new("$filename.1")) {
110 while (defined ($line = $bw->readline)) {
111 &$parse_line();
112 }
113 $bw->close();
114 }
115
116 $rpcenv->set_result_attrib('total', $count);
117
118 return $res;
119 }});
120
121 __PACKAGE__->register_method({
122 name => 'upid_index',
123 path => '{upid}',
124 method => 'GET',
125 description => '', # index helper
126 permissions => { user => 'all' },
127 parameters => {
128 additionalProperties => 0,
129 properties => {
130 node => get_standard_option('pve-node'),
131 upid => { type => 'string' },
132 }
133 },
134 returns => {
135 type => 'array',
136 items => {
137 type => "object",
138 properties => {},
139 },
140 links => [ { rel => 'child', href => "{name}" } ],
141 },
142 code => sub {
143 my ($param) = @_;
144
145 return [
146 { name => 'log' },
147 { name => 'status' }
148 ];
149 }});
150
151 __PACKAGE__->register_method({
152 name => 'stop_task',
153 path => '{upid}',
154 method => 'DELETE',
155 description => 'Stop a task.',
156 permissions => { user => 'all' },
157 protected => 1,
158 proxyto => 'node',
159 parameters => {
160 additionalProperties => 0,
161 properties => {
162 node => get_standard_option('pve-node'),
163 upid => { type => 'string' },
164 }
165 },
166 returns => { type => 'null' },
167 code => sub {
168 my ($param) = @_;
169
170 my ($task, $filename) = PVE::Tools::upid_decode($param->{upid}, 1);
171 raise_param_exc({ upid => "unable to parse worker upid" }) if !$task;
172 raise_param_exc({ upid => "no such task" }) if ! -f $filename;
173
174 my $rpcenv = PVE::RPCEnvironment::get();
175 my $user = $rpcenv->get_user();
176 my $node = $param->{node};
177
178 if ($user ne $task->{user}) {
179 $rpcenv->check($user, "/nodes/$node", [ 'Sys.Console' ]);
180 }
181
182 PVE::RPCEnvironment::check_worker($param->{upid}, 1);
183
184 return undef;
185 }});
186
187 __PACKAGE__->register_method({
188 name => 'read_task_log',
189 path => '{upid}/log',
190 method => 'GET',
191 permissions => { user => 'all' },
192 protected => 1,
193 description => "Read task log.",
194 proxyto => 'node',
195 parameters => {
196 additionalProperties => 0,
197 properties => {
198 node => get_standard_option('pve-node'),
199 upid => { type => 'string' },
200 start => {
201 type => 'integer',
202 minimum => 0,
203 optional => 1,
204 },
205 limit => {
206 type => 'integer',
207 minimum => 0,
208 optional => 1,
209 },
210 },
211 },
212 returns => {
213 type => 'array',
214 items => {
215 type => "object",
216 properties => {
217 n => {
218 description=> "Line number",
219 type=> 'integer',
220 },
221 t => {
222 description=> "Line text",
223 type => 'string',
224 }
225 }
226 }
227 },
228 code => sub {
229 my ($param) = @_;
230
231 my ($task, $filename) = PVE::Tools::upid_decode($param->{upid}, 1);
232 raise_param_exc({ upid => "unable to parse worker upid" }) if !$task;
233
234 my $lines = [];
235
236 my $rpcenv = PVE::RPCEnvironment::get();
237 my $user = $rpcenv->get_user();
238 my $node = $param->{node};
239
240 if ($user ne $task->{user}) {
241 $rpcenv->check($user, "/nodes/$node", [ 'Sys.Audit' ]);
242 }
243
244 my $fh = IO::File->new($filename, "r");
245 raise_param_exc({ upid => "no such task - unable to open file - $!" }) if !$fh;
246
247 my $start = $param->{start} || 0;
248 my $limit = $param->{limit} || 50;
249 my $count = 0;
250 my $line;
251 while (defined ($line = <$fh>)) {
252 next if $count++ < $start;
253 next if $limit <= 0;
254 chomp $line;
255 push @$lines, { n => $count, t => $line};
256 $limit--;
257 }
258
259 close($fh);
260
261 # HACK: ExtJS store.guaranteeRange() does not like empty array
262 # so we add a line
263 if (!$count) {
264 $count++;
265 push @$lines, { n => $count, t => "no content"};
266 }
267
268 $rpcenv->set_result_attrib('total', $count);
269
270 return $lines;
271 }});
272
273 __PACKAGE__->register_method({
274 name => 'read_task_status',
275 path => '{upid}/status',
276 method => 'GET',
277 permissions => { user => 'all' },
278 protected => 1,
279 description => "Read task status.",
280 proxyto => 'node',
281 parameters => {
282 additionalProperties => 0,
283 properties => {
284 node => get_standard_option('pve-node'),
285 upid => { type => 'string' },
286 },
287 },
288 returns => {
289 type => "object",
290 properties => {
291 pid => {
292 type => 'integer'
293 },
294 status => {
295 type => 'string', enum => ['running', 'stopped'],
296 },
297 },
298 },
299 code => sub {
300 my ($param) = @_;
301
302 my ($task, $filename) = PVE::Tools::upid_decode($param->{upid}, 1);
303 raise_param_exc({ upid => "unable to parse worker upid" }) if !$task;
304 raise_param_exc({ upid => "no such task" }) if ! -f $filename;
305
306 my $lines = [];
307
308 my $rpcenv = PVE::RPCEnvironment::get();
309 my $user = $rpcenv->get_user();
310 my $node = $param->{node};
311
312 if ($user ne $task->{user}) {
313 $rpcenv->check($user, "/nodes/$node", [ 'Sys.Audit' ]);
314 }
315
316 my $pstart = PVE::ProcFSTools::read_proc_starttime($task->{pid});
317 $task->{status} = ($pstart && ($pstart == $task->{pstart})) ?
318 'running' : 'stopped';
319
320 $task->{upid} = $param->{upid}; # include upid
321
322 return $task;
323 }});