]> git.proxmox.com Git - pve-manager.git/blob - PVE/API2/Cluster/BackupInfo.pm
bea0e46037861e8356ca6bcd9f340c67e96fc89f
[pve-manager.git] / PVE / API2 / Cluster / BackupInfo.pm
1 package PVE::API2::Cluster::BackupInfo;
2
3 use strict;
4 use warnings;
5 use Digest::SHA;
6
7 use PVE::SafeSyslog;
8 use PVE::Tools qw(extract_param);
9 use PVE::Cluster qw(cfs_lock_file cfs_read_file cfs_write_file);
10 use PVE::RESTHandler;
11 use PVE::RPCEnvironment;
12 use PVE::JSONSchema;
13 use PVE::Storage;
14 use PVE::Exception qw(raise_param_exc);
15 use PVE::VZDump;
16 use PVE::VZDump::Common;
17
18 use base qw(PVE::RESTHandler);
19
20 sub get_included_vmids {
21 my $vzconf = cfs_read_file('vzdump.cron');
22
23 my $all_jobs = $vzconf->{jobs} || [];
24 my @all_vmids = ();
25
26 for my $job (@$all_jobs) {
27 my $job_included_guests = PVE::VZDump::get_included_guests($job);
28 push @all_vmids, ( map { @{$_} } values %{$job_included_guests} );
29 }
30
31 my $vzjobs = cfs_read_file('jobs.cfg');
32
33 for my $job (values %{$vzjobs->{ids}}) {
34 next if $job->{type} ne 'vzdump';
35
36 my $job_included_guests = PVE::VZDump::get_included_guests($job);
37 push @all_vmids, ( map { @{$_} } values %{$job_included_guests} );
38 }
39
40 return { map { $_ => 1 } @all_vmids };
41 }
42
43 __PACKAGE__->register_method({
44 name => 'index',
45 path => '',
46 method => 'GET',
47 description => "Index for backup info related endpoints",
48 parameters => {
49 additionalProperties => 0,
50 properties => {},
51 },
52 returns => {
53 type => 'array',
54 description => 'Directory index.',
55 items => {
56 type => "object",
57 properties => {
58 subdir => {
59 type => 'string',
60 description => 'API sub-directory endpoint',
61 },
62 },
63 },
64 links => [ { rel => 'child', href => "{subdir}" } ],
65 },
66 code => sub {
67 return [
68 { subdir => 'not-backed-up' },
69 ];
70 }});
71
72 __PACKAGE__->register_method({
73 name => 'get_guests_not_in_backup',
74 path => 'not-backed-up',
75 method => 'GET',
76 protected => 1,
77 description => "Shows all guests which are not covered by any backup job.",
78 permissions => {
79 check => ['perm', '/', ['Sys.Audit']],
80 },
81 parameters => {
82 additionalProperties => 0,
83 properties => {},
84 },
85 returns => {
86 type => 'array',
87 description => 'Contains the guest objects.',
88 items => {
89 type => 'object',
90 properties => {
91 vmid => {
92 type => 'integer',
93 description => 'VMID of the guest.',
94 },
95 name => {
96 type => 'string',
97 description => 'Name of the guest',
98 optional => 1,
99 },
100 type => {
101 type => 'string',
102 description => 'Type of the guest.',
103 enum => ['qemu', 'lxc'],
104 },
105 },
106 },
107 },
108 code => sub {
109 my $rpcenv = PVE::RPCEnvironment::get();
110 my $user = $rpcenv->get_user();
111 my $rrd = PVE::Cluster::rrd_dump();
112 my $included_vmids = get_included_vmids();
113 my $vmlist = PVE::Cluster::get_vmlist();
114 my @vmids = ( keys %{$vmlist->{ids}} );
115
116 # remove VMIDs to which the user has no permission to not leak infos
117 # like the guest name
118 my @allowed_vmids = grep {
119 $rpcenv->check($user, "/vms/$_", [ 'VM.Audit' ], 1);
120 } @vmids;
121
122 my $result = [];
123
124 for my $vmid (@allowed_vmids) {
125
126 next if $included_vmids->{$vmid};
127
128 my $type = $vmlist->{ids}->{$vmid}->{type};
129 my $node = $vmlist->{ids}->{$vmid}->{node};
130
131 my $conf;
132 my $name = "";
133
134 if ($type eq 'qemu') {
135 $conf = PVE::QemuConfig->load_config($vmid, $node);
136 $name = $conf->{name};
137 } elsif ($type eq 'lxc') {
138 $conf = PVE::LXC::Config->load_config($vmid, $node);
139 $name = $conf->{hostname};
140 } else {
141 die "VMID $vmid is neither Qemu nor LXC guest\n";
142 }
143
144 push @{$result}, {
145 vmid => int($vmid),
146 name => $name,
147 type => $type,
148 };
149 }
150
151 return $result;
152 }});
153 1;