]>
git.proxmox.com Git - pve-manager.git/blob - PVE/API2/Tasks.pm
1 package PVE
::API2
::Tasks
;
7 use File
::ReadBackwards
;
12 use PVE
::RPCEnvironment
;
13 use PVE
::JSONSchema
qw(get_standard_option);
14 use PVE
::AccessControl
;
16 use base
qw(PVE::RESTHandler);
18 __PACKAGE__-
>register_method({
22 permissions
=> { user
=> 'all' },
23 description
=> "Read task list for one node (finished tasks).",
26 additionalProperties
=> 0,
28 node
=> get_standard_option
('pve-node'),
54 upid
=> { type
=> 'string' },
57 links
=> [ { rel
=> 'child', href
=> "{upid}" } ],
62 my $rpcenv = PVE
::RPCEnvironment
::get
();
63 my $user = $rpcenv->get_user();
67 my $filename = "/var/log/pve/tasks/index";
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
};
78 my $auditor = $rpcenv->check($user, "/nodes/$node", [ 'Sys.Audit' ], 1);
80 my $parse_line = sub {
81 if ($line =~ m/^(\S+)(\s([0-9A-Za-z]{8})(\s(\S.*))?)?$/) {
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
});
89 return if $errors && $status && $status eq 'OK';
91 return if $count++ < $start;
92 return if $limit <= 0;
94 $task->{upid
} = $upid;
95 $task->{endtime
} = hex($endtime) if $endtime;
96 $task->{status
} = $status if $status;
103 if (my $bw = File
::ReadBackwards-
>new($filename)) {
104 while (defined ($line = $bw->readline)) {
109 if (my $bw = File
::ReadBackwards-
>new("$filename.1")) {
110 while (defined ($line = $bw->readline)) {
116 $rpcenv->set_result_attrib('total', $count);
121 __PACKAGE__-
>register_method({
122 name
=> 'upid_index',
125 description
=> '', # index helper
126 permissions
=> { user
=> 'all' },
128 additionalProperties
=> 0,
130 node
=> get_standard_option
('pve-node'),
131 upid
=> { type
=> 'string' },
140 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
151 __PACKAGE__-
>register_method({
155 description
=> 'Stop a task.',
156 permissions
=> { user
=> 'all' },
160 additionalProperties
=> 0,
162 node
=> get_standard_option
('pve-node'),
163 upid
=> { type
=> 'string' },
166 returns
=> { type
=> 'null' },
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;
174 my $rpcenv = PVE
::RPCEnvironment
::get
();
175 my $user = $rpcenv->get_user();
176 my $node = $param->{node
};
178 if ($user ne $task->{user
}) {
179 $rpcenv->check($user, "/nodes/$node", [ 'Sys.Console' ]);
182 PVE
::RPCEnvironment
::check_worker
($param->{upid
}, 1);
187 __PACKAGE__-
>register_method({
188 name
=> 'read_task_log',
189 path
=> '{upid}/log',
191 permissions
=> { user
=> 'all' },
193 description
=> "Read task log.",
196 additionalProperties
=> 0,
198 node
=> get_standard_option
('pve-node'),
199 upid
=> { type
=> 'string' },
218 description
=> "Line number",
222 description
=> "Line text",
231 my ($task, $filename) = PVE
::Tools
::upid_decode
($param->{upid
}, 1);
232 raise_param_exc
({ upid
=> "unable to parse worker upid" }) if !$task;
236 my $rpcenv = PVE
::RPCEnvironment
::get
();
237 my $user = $rpcenv->get_user();
238 my $node = $param->{node
};
240 if ($user ne $task->{user
}) {
241 $rpcenv->check($user, "/nodes/$node", [ 'Sys.Audit' ]);
244 my $fh = IO
::File-
>new($filename, "r");
245 raise_param_exc
({ upid
=> "no such task - unable to open file - $!" }) if !$fh;
247 my $start = $param->{start
} || 0;
248 my $limit = $param->{limit
} || 50;
251 while (defined ($line = <$fh>)) {
252 next if $count++ < $start;
255 push @$lines, { n
=> $count, t
=> $line};
261 # HACK: ExtJS store.guaranteeRange() does not like empty array
265 push @$lines, { n
=> $count, t
=> "no content"};
268 $rpcenv->set_result_attrib('total', $count);
273 __PACKAGE__-
>register_method({
274 name
=> 'read_task_status',
275 path
=> '{upid}/status',
277 permissions
=> { user
=> 'all' },
279 description
=> "Read task status.",
282 additionalProperties
=> 0,
284 node
=> get_standard_option
('pve-node'),
285 upid
=> { type
=> 'string' },
295 type
=> 'string', enum
=> ['running', 'stopped'],
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;
308 my $rpcenv = PVE
::RPCEnvironment
::get
();
309 my $user = $rpcenv->get_user();
310 my $node = $param->{node
};
312 if ($user ne $task->{user
}) {
313 $rpcenv->check($user, "/nodes/$node", [ 'Sys.Audit' ]);
316 my $pstart = PVE
::ProcFSTools
::read_proc_starttime
($task->{pid
});
317 $task->{status
} = ($pstart && ($pstart == $task->{pstart
})) ?
318 'running' : 'stopped';
320 $task->{upid
} = $param->{upid
}; # include upid