]> git.proxmox.com Git - pve-manager.git/blame - PVE/CLI/pvesr.pm
ui: don't translate NDP
[pve-manager.git] / PVE / CLI / pvesr.pm
CommitLineData
892821fd
DM
1package PVE::CLI::pvesr;
2
3use strict;
4use warnings;
5use POSIX qw(strftime);
6use JSON;
7
8use PVE::JSONSchema qw(get_standard_option);
9use PVE::INotify;
10use PVE::RPCEnvironment;
11use PVE::Tools qw(extract_param);
12use PVE::SafeSyslog;
13use PVE::CLIHandler;
14
fae99506 15use PVE::Cluster;
892821fd
DM
16use PVE::Replication;
17use PVE::API2::ReplicationConfig;
18use PVE::API2::Replication;
19
20use base qw(PVE::CLIHandler);
21
22my $nodename = PVE::INotify::nodename();
23
24sub setup_environment {
25 PVE::RPCEnvironment->setup_default_cli_env();
26}
27
91ee6a2f
DM
28# fixme: get from plugin??
29my $replicatable_storage_types = {
30 zfspool => 1,
31};
32
33my $check_wanted_volid = sub {
34 my ($storecfg, $vmid, $volid, $local_node) = @_;
35
36 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
37 my $scfg = PVE::Storage::storage_check_enabled($storecfg, $storeid, $local_node);
38 die "storage '$storeid' is not replicatable\n"
39 if !$replicatable_storage_types->{$scfg->{type}};
40
41 my ($vtype, undef, $ownervm) = PVE::Storage::parse_volname($storecfg, $volid);
42 die "volume '$volid' has wrong vtype ($vtype != 'images')\n"
43 if $vtype ne 'images';
44 die "volume '$volid' has wrong owner\n"
45 if !$ownervm || $vmid != $ownervm;
46
47 return $storeid;
48};
49
fae99506
DM
50__PACKAGE__->register_method ({
51 name => 'prepare_local_job',
52 path => 'prepare_local_job',
53 method => 'POST',
54 description => "Prepare for starting a replication job. This is called on the target node before replication starts. This call is for internal use, and return a JSON object on stdout. The method first test if VM <vmid> reside on the local node. If so, stop immediately. After that the method scans all volume IDs for snapshots, and removes all replications snapshots with timestamps different than <last_sync>. It also removes any unused volumes. Returns a hash with boolean markers for all volumes with existing replication snapshots.",
55 parameters => {
56 additionalProperties => 0,
57 properties => {
58 id => get_standard_option('pve-replication-id'),
fae99506
DM
59 'extra-args' => get_standard_option('extra-args', {
60 description => "The list of volume IDs to consider." }),
91ee6a2f
DM
61 scan => {
62 description => "List of storage IDs to scan for stale volumes.",
63 type => 'string', format => 'pve-storage-id-list',
64 optional => 1,
65 },
f9d38c54
DM
66 force => {
67 description => "Allow to remove all existion volumes (empty volume list).",
68 type => 'boolean',
69 optional => 1,
70 default => 0,
71 },
fae99506
DM
72 last_sync => {
73 description => "Time (UNIX epoch) of last successful sync. If not specified, all replication snapshots get removed.",
74 type => 'integer',
75 minimum => 0,
76 optional => 1,
77 },
91ee6a2f
DM
78 parent_snapname => get_standard_option('pve-snapshot-name', {
79 optional => 1,
80 }),
fae99506
DM
81 },
82 },
83 returns => { type => 'null' },
84 code => sub {
85 my ($param) = @_;
86
91ee6a2f
DM
87 my $logfunc = sub {
88 my ($msg) = @_;
89 print STDERR "$msg\n";
90 };
fae99506
DM
91
92 my $local_node = PVE::INotify::nodename();
93
91ee6a2f
DM
94 die "no volumes specified\n"
95 if !$param->{force} && !scalar(@{$param->{'extra-args'}});
96
97 my ($vmid, undef, $jobid) = PVE::ReplicationConfig::parse_replication_job_id($param->{id});
98
fae99506
DM
99 my $vms = PVE::Cluster::get_vmlist();
100 die "guest '$vmid' is on local node\n"
101 if $vms->{ids}->{$vmid} && $vms->{ids}->{$vmid}->{node} eq $local_node;
102
91ee6a2f
DM
103 my $last_sync = $param->{last_sync} // 0;
104 my $parent_snapname = $param->{parent_snapname};
fae99506 105
91ee6a2f 106 my $storecfg = PVE::Storage::config();
fae99506 107
91ee6a2f
DM
108 # compute list of storages we want to scan
109 my $storage_hash = {};
110 foreach my $storeid (PVE::Tools::split_list($param->{scan})) {
111 my $scfg = PVE::Storage::storage_check_enabled($storecfg, $storeid, $local_node, 1);
112 next if !$scfg; # simply ignore unavailable storages here
113 die "storage '$storeid' is not replicatable\n" if !$replicatable_storage_types->{$scfg->{type}};
114 $storage_hash->{$storeid} = 1;
115 }
fae99506 116
91ee6a2f 117 my $wanted_volids = {};
fae99506 118 foreach my $volid (@{$param->{'extra-args'}}) {
91ee6a2f
DM
119 my $storeid = $check_wanted_volid->($storecfg, $vmid, $volid, $local_node);
120 $wanted_volids->{$volid} = 1;
121 $storage_hash->{$storeid} = 1;
fae99506 122 }
91ee6a2f
DM
123 my $storage_list = [ sort keys %$storage_hash ];
124
125 # activate all used storage
126 my $cache = {};
127 PVE::Storage::activate_storage_list($storecfg, $storage_list, $cache);
128
129 my $snapname = PVE::ReplicationState::replication_snapshot_name($jobid, $last_sync);
130
131 # find replication snapshots
544bb4e2 132 my $volids = [];
91ee6a2f
DM
133 foreach my $storeid (@$storage_list) {
134 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
135 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
37efc934 136 my $images = $plugin->list_images($storeid, $scfg, $vmid, undef, $cache);
544bb4e2
FG
137 push @$volids, map { $_->{volid} } @$images;
138 }
139 my ($last_snapshots, $cleaned_replicated_volumes) = PVE::Replication::prepare($storecfg, $volids, $jobid, $last_sync, $parent_snapname, $logfunc);
140 foreach my $volid (keys %$cleaned_replicated_volumes) {
141 if (!$wanted_volids->{$volid}) {
142 $logfunc->("$jobid: delete stale volume '$volid'");
143 PVE::Storage::vdisk_free($storecfg, $volid);
144 delete $last_snapshots->{$volid};
fae99506
DM
145 }
146 }
147
fae99506
DM
148 print to_json($last_snapshots) . "\n";
149
150 return undef;
151 }});
152
fcc22c0b
DM
153__PACKAGE__->register_method ({
154 name => 'finalize_local_job',
155 path => 'finalize_local_job',
156 method => 'POST',
157 description => "Finalize a replication job. This removes all replications snapshots with timestamps different than <last_sync>.",
158 parameters => {
159 additionalProperties => 0,
160 properties => {
161 id => get_standard_option('pve-replication-id'),
fcc22c0b
DM
162 'extra-args' => get_standard_option('extra-args', {
163 description => "The list of volume IDs to consider." }),
164 last_sync => {
165 description => "Time (UNIX epoch) of last successful sync. If not specified, all replication snapshots gets removed.",
166 type => 'integer',
167 minimum => 0,
168 optional => 1,
169 },
170 },
171 },
172 returns => { type => 'null' },
173 code => sub {
174 my ($param) = @_;
175
a9da300d 176 my ($vmid, undef, $jobid) = PVE::ReplicationConfig::parse_replication_job_id($param->{id});
fcc22c0b
DM
177 my $last_sync = $param->{last_sync} // 0;
178
179 my $local_node = PVE::INotify::nodename();
180
181 my $vms = PVE::Cluster::get_vmlist();
182 die "guest '$vmid' is on local node\n"
183 if $vms->{ids}->{$vmid} && $vms->{ids}->{$vmid}->{node} eq $local_node;
184
185 my $storecfg = PVE::Storage::config();
186
187 my $volids = [];
188
189 die "no volumes specified\n" if !scalar(@{$param->{'extra-args'}});
190
191 foreach my $volid (@{$param->{'extra-args'}}) {
91ee6a2f 192 $check_wanted_volid->($storecfg, $vmid, $volid, $local_node);
fcc22c0b
DM
193 push @$volids, $volid;
194 }
195
196 $volids = [ sort @$volids ];
197
198 my $logfunc = sub {
c364b61f 199 my ($msg) = @_;
fcc22c0b
DM
200 print STDERR "$msg\n";
201 };
202
203 my $last_snapshots = PVE::Replication::prepare(
204 $storecfg, $volids, $jobid, $last_sync, undef, $logfunc);
205
206 return undef;
207 }});
208
892821fd
DM
209__PACKAGE__->register_method ({
210 name => 'run',
211 path => 'run',
212 method => 'POST',
213 description => "This method is called by the systemd-timer and executes all (or a specific) sync jobs.",
214 parameters => {
215 additionalProperties => 0,
216 properties => {
217 id => get_standard_option('pve-replication-id', { optional => 1 }),
f70997ea
DM
218 verbose => {
219 description => "Print more verbose logs to stdout.",
220 type => 'boolean',
221 default => 0,
222 optional => 1,
223 },
892821fd
DM
224 },
225 },
226 returns => { type => 'null' },
227 code => sub {
228 my ($param) = @_;
229
f70997ea
DM
230 my $logfunc;
231
232 if ($param->{verbose}) {
233 $logfunc = sub {
c364b61f 234 my ($msg) = @_;
f70997ea
DM
235 print "$msg\n";
236 };
237 }
238
892821fd
DM
239 if (my $id = extract_param($param, 'id')) {
240
810c6776 241 PVE::API2::Replication::run_single_job($id, undef, $logfunc);
892821fd
DM
242
243 } else {
244
810c6776 245 PVE::API2::Replication::run_jobs(undef, $logfunc);
892821fd
DM
246 }
247
248 return undef;
249 }});
250
251__PACKAGE__->register_method ({
252 name => 'enable',
253 path => 'enable',
254 method => 'POST',
255 description => "Enable a replication job.",
256 parameters => {
257 additionalProperties => 0,
258 properties => {
259 id => get_standard_option('pve-replication-id'),
260 },
261 },
262 returns => { type => 'null' },
263 code => sub {
264 my ($param) = @_;
265
266 $param->{disable} = 0;
267
268 return PVE::API2::ReplicationConfig->update($param);
269 }});
270
271__PACKAGE__->register_method ({
272 name => 'disable',
273 path => 'disable',
274 method => 'POST',
275 description => "Disable a replication job.",
276 parameters => {
277 additionalProperties => 0,
278 properties => {
279 id => get_standard_option('pve-replication-id'),
280 },
281 },
282 returns => { type => 'null' },
283 code => sub {
284 my ($param) = @_;
285
286 $param->{disable} = 1;
287
288 return PVE::API2::ReplicationConfig->update($param);
289 }});
290
291my $print_job_list = sub {
292 my ($list) = @_;
293
a9da300d 294 my $format = "%-20s %-20s %10s %5s %8s\n";
892821fd 295
a9da300d 296 printf($format, "JobID", "Target", "Schedule", "Rate", "Enabled");
892821fd
DM
297
298 foreach my $job (sort { $a->{guest} <=> $b->{guest} } @$list) {
299 my $plugin = PVE::ReplicationConfig->lookup($job->{type});
300 my $tid = $plugin->get_unique_target_id($job);
301
a9da300d 302 printf($format, $job->{id}, $tid,
5c180db3 303 defined($job->{schedule}) ? $job->{schedule} : '*/15',
892821fd
DM
304 defined($job->{rate}) ? $job->{rate} : '-',
305 $job->{disable} ? 'no' : 'yes'
306 );
307 }
308};
309
310my $print_job_status = sub {
311 my ($list) = @_;
312
a9da300d 313 my $format = "%-20s %-20s %20s %20s %10s %10s %s\n";
892821fd 314
a9da300d 315 printf($format, "JobID", "Target", "LastSync", "NextSync", "Duration", "FailCount", "State");
892821fd
DM
316
317 foreach my $job (sort { $a->{guest} <=> $b->{guest} } @$list) {
318 my $plugin = PVE::ReplicationConfig->lookup($job->{type});
319 my $tid = $plugin->get_unique_target_id($job);
320
5c180db3
DM
321 my $timestr = '-';
322 if ($job->{last_sync}) {
323 $timestr = strftime("%Y-%m-%d_%H:%M:%S", localtime($job->{last_sync}));
324 }
325
326 my $nextstr = '-';
327 if (my $next = $job->{next_sync}) {
328 my $now = time();
329 if ($next > $now) {
330 $nextstr = strftime("%Y-%m-%d_%H:%M:%S", localtime($job->{next_sync}));
331 } else {
332 $nextstr = 'now';
333 }
334 }
892821fd 335
483f89dd
DM
336 my $state = $job->{pid} ? "SYNCING" : $job->{error} // 'OK';
337
a9da300d 338 printf($format, $job->{id}, $tid,
5c180db3 339 $timestr, $nextstr, $job->{duration} // '-',
483f89dd 340 $job->{fail_count}, $state);
892821fd
DM
341 }
342};
343
344our $cmddef = {
345 status => [ 'PVE::API2::Replication', 'status', [], { node => $nodename }, $print_job_status ],
346
5c180db3 347 list => [ 'PVE::API2::ReplicationConfig', 'index' , [], {}, $print_job_list ],
892821fd
DM
348 read => [ 'PVE::API2::ReplicationConfig', 'read' , ['id'], {},
349 sub { my $res = shift; print to_json($res, { utf8 => 1, pretty => 1, canonical => 1}); }],
350 update => [ 'PVE::API2::ReplicationConfig', 'update' , ['id'], {} ],
351 delete => [ 'PVE::API2::ReplicationConfig', 'delete' , ['id'], {} ],
a9da300d 352 'create-local-job' => [ 'PVE::API2::ReplicationConfig', 'create' , ['id', 'target'],
892821fd
DM
353 { type => 'local' } ],
354
355 enable => [ __PACKAGE__, 'enable', ['id'], {}],
356 disable => [ __PACKAGE__, 'disable', ['id'], {}],
357
a9da300d
DM
358 'prepare-local-job' => [ __PACKAGE__, 'prepare_local_job', ['id', 'extra-args'], {} ],
359 'finalize-local-job' => [ __PACKAGE__, 'finalize_local_job', ['id', 'extra-args'], {} ],
fae99506 360
892821fd
DM
361 run => [ __PACKAGE__ , 'run'],
362};
363
3641;