]>
Commit | Line | Data |
---|---|---|
1 | package PMG::PBSSchedule; | |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use PVE::Tools qw(run_command file_set_contents file_get_contents trim dir_glob_foreach); | |
7 | use PVE::Systemd; | |
8 | ||
9 | # systemd timer | |
10 | sub get_schedules { | |
11 | my ($param) = @_; | |
12 | ||
13 | my $result = []; | |
14 | ||
15 | my $systemd_dir = '/etc/systemd/system'; | |
16 | ||
17 | dir_glob_foreach($systemd_dir, '^pmg-pbsbackup@.+\.timer$', sub { | |
18 | my ($filename) = @_; | |
19 | my $remote; | |
20 | if ($filename =~ /^pmg-pbsbackup\@(.+)\.timer$/) { | |
21 | $remote = PVE::Systemd::unescape_unit($1); | |
22 | } else { | |
23 | die 'Unrecognized timer name!\n'; | |
24 | } | |
25 | ||
26 | my $unitfile = "$systemd_dir/$filename"; | |
27 | my $unit = PVE::Systemd::read_ini($unitfile); | |
28 | ||
29 | push @$result, { | |
30 | unitfile => $unitfile, | |
31 | remote => $remote, | |
32 | schedule => $unit->{'Timer'}->{'OnCalendar'}, | |
33 | delay => $unit->{'Timer'}->{'RandomizedDelaySec'}, | |
34 | }; | |
35 | }); | |
36 | ||
37 | return $result; | |
38 | ||
39 | } | |
40 | ||
41 | sub create_schedule { | |
42 | my ($remote, $schedule, $delay) = @_; | |
43 | ||
44 | my $unit_name = 'pmg-pbsbackup@' . PVE::Systemd::escape_unit($remote); | |
45 | #my $service_unit = $unit_name . '.service'; | |
46 | my $timer_unit = $unit_name . '.timer'; | |
47 | my $timer_unit_path = "/etc/systemd/system/$timer_unit"; | |
48 | ||
49 | # create systemd timer | |
50 | run_command(['systemd-analyze', 'calendar', $schedule], errmsg => "Invalid schedule specification", outfunc => sub {}); | |
51 | run_command(['systemd-analyze', 'timespan', $delay], errmsg => "Invalid delay specification", outfunc => sub {}); | |
52 | my $timer = { | |
53 | 'Unit' => { | |
54 | 'Description' => "Timer for PBS Backup to remote $remote", | |
55 | }, | |
56 | 'Timer' => { | |
57 | 'OnCalendar' => $schedule, | |
58 | 'RandomizedDelaySec' => $delay, | |
59 | }, | |
60 | 'Install' => { | |
61 | 'WantedBy' => 'timers.target', | |
62 | }, | |
63 | }; | |
64 | ||
65 | eval { | |
66 | PVE::Systemd::write_ini($timer, $timer_unit_path); | |
67 | run_command(['systemctl', 'daemon-reload']); | |
68 | run_command(['systemctl', 'enable', $timer_unit]); | |
69 | run_command(['systemctl', 'start', $timer_unit]); | |
70 | ||
71 | }; | |
72 | if (my $err = $@) { | |
73 | die "Creating backup schedule for $remote failed: $err\n"; | |
74 | } | |
75 | ||
76 | return; | |
77 | } | |
78 | ||
79 | sub delete_schedule { | |
80 | my ($remote) = @_; | |
81 | ||
82 | my $schedules = get_schedules(); | |
83 | ||
84 | die "Schedule for $remote not found!\n" if !grep {$_->{remote} eq $remote} @$schedules; | |
85 | ||
86 | my $unit_name = 'pmg-pbsbackup@' . PVE::Systemd::escape_unit($remote); | |
87 | my $service_unit = $unit_name . '.service'; | |
88 | my $timer_unit = $unit_name . '.timer'; | |
89 | my $timer_unit_path = "/etc/systemd/system/$timer_unit"; | |
90 | ||
91 | eval { | |
92 | run_command(['systemctl', 'disable', $timer_unit]); | |
93 | unlink($timer_unit_path) || die "delete '$timer_unit_path' failed - $!\n"; | |
94 | run_command(['systemctl', 'daemon-reload']); | |
95 | ||
96 | }; | |
97 | if (my $err = $@) { | |
98 | die "Removing backup schedule for $remote failed: $err\n"; | |
99 | } | |
100 | ||
101 | return; | |
102 | } | |
103 | ||
104 | 1; |