]>
git.proxmox.com Git - pve-storage.git/blob - PVE/CLI/pvesr.pm
1 package PVE
::CLI
::pvesr
;
5 use POSIX
qw(strftime);
7 use PVE
::JSONSchema
qw(get_standard_option);
9 use PVE
::RPCEnvironment
;
10 use PVE
::Tools
qw(extract_param);
14 use PVE
::API2
::Storage
::Replication
;
16 use base
qw(PVE::CLIHandler);
18 my $nodename = PVE
::INotify
::nodename
();
20 sub setup_environment
{
21 PVE
::RPCEnvironment-
>setup_default_cli_env();
24 my $print_list = sub {
25 my ($conf, $json) = @_;
28 print JSON
::encode_json
($conf);
30 printf("%-10s%-20s%-20s%-5s%-10s%-5s\n",
31 "VMID", "DEST", "LAST SYNC","IVAL", "STATE", "FAIL");
33 foreach my $vmid (sort keys %$conf) {
34 my $job = $conf->{$vmid};
35 my $timestr = strftime
("%Y-%m-%d_%H:%M:%S", localtime($job->{lastsync
}));
37 printf("%-9s ", $vmid);
38 printf("%-19s ", $job->{tnode
});
39 printf("%-19s ", $timestr);
40 printf("%-4s ", $job->{interval
});
41 printf("%-9s ", $job->{state});
42 printf("%-9s\n", $job->{fail
});
49 my ($list, $synctime, $vmid) = @_;
51 if (defined($list->{$synctime})) {
52 $list = set_list
($list,$synctime+1, $vmid);
54 $list->{$synctime} = $vmid;
59 my $get_replica_list = sub {
61 my $jobs = PVE
::ReplicationTools
::read_state
();
64 foreach my $vmid (keys %$jobs) {
65 my $job = $jobs->{$vmid};
66 my $lastsync = $job->{lastsync
};
69 my $interval = $job->{interval
};
71 my $fail = $job->{fail
};
73 my $synctime = $lastsync + $interval * 60;
75 if ($now >= $synctime && $job->{state} eq 'ok') {
76 $list = set_list
($list, $synctime, $vmid);
77 } elsif ($job->{state} eq 'sync') {
79 my $synctime += $interval * ($job->{fail
}+1);
80 $list = set_list
($list, $synctime, $vmid)
81 if ($now >= $synctime);
89 my $replicate_vms = sub {
92 my @sorted_times = reverse sort keys %$list;
94 foreach my $synctime (@sorted_times) {
96 PVE
::ReplicationTools
::sync_guest
($list->{$synctime});
99 syslog
('err', $err );
104 __PACKAGE__-
>register_method ({
108 description
=> "This method will run by the systemd-timer and sync all jobs",
111 check
=> ['perm', '/', [ 'Sys.Console' ]],
116 additionalProperties
=> 0,
120 returns
=> { type
=> 'null' },
123 my $list = &$get_replica_list();
124 &$replicate_vms($list);
129 __PACKAGE__-
>register_method ({
130 name
=> 'destroyjob',
131 path
=> 'destroyjob',
133 description
=> "Destroy an async replication job",
136 check
=> ['perm', '/storage', ['Datastore.Allocate']],
141 additionalProperties
=> 0,
144 type
=> 'string', format
=> 'pve-vmid',
145 description
=> "The VMID of the guest.",
146 completion
=> \
&PVE
::Cluster
::complete_local_vmid
,
150 returns
=> { type
=> 'null' },
154 my $vmid = extract_param
($param, 'vmid');
156 PVE
::ReplicationTools
::destroy_replica
($vmid);
160 __PACKAGE__-
>register_method ({
164 description
=> "List of all replication jobs.",
170 additionalProperties
=> 0,
173 type
=> 'string', format
=> 'pve-vmid',
174 description
=> "The VMID of the guest.",
175 completion
=> \
&PVE
::Cluster
::complete_local_vmid
,
182 additionalProperties
=> 0,
184 node
=> get_standard_option
('pve-node'),
185 nodes
=> get_standard_option
('pve-node-list' ,
186 {description
=> "Notes where the jobs is located.",
191 description
=> "Output in JSON format.",
195 returns
=> { type
=> 'string' },
199 if ($param->{nodes
}) {
200 foreach my $node (PVE
::Tools
::split_list
($param->{nodes
})) {
201 die "Node: $node does not exists.\n" if
202 !PVE
::Cluster
::check_node_exists
($node);
206 my $nodes = $param->{nodes
} ?
207 $param->{nodes
} : $param->{node
};
209 my $list = PVE
::ReplicationTools
::get_all_jobs
($nodes);
211 &$print_list($list, $param->{json
});
216 list
=> [ __PACKAGE__
, 'list' , [], {node
=> $nodename}],
217 run
=> [ __PACKAGE__
, 'run'],
218 destroyjob
=> [ __PACKAGE__
, 'destroyjob', ['vmid']],