]>
Commit | Line | Data |
---|---|---|
2de4cfb2 SI |
1 | package PMG::PBSConfig; |
2 | ||
3 | # section config implementation for PBS integration in PMG | |
4 | ||
5 | use strict; | |
6 | use warnings; | |
7 | ||
8 | use PVE::Tools qw(extract_param); | |
9 | use PVE::SectionConfig; | |
10 | use PVE::JSONSchema qw(get_standard_option); | |
11 | use PVE::PBSClient; | |
12 | ||
13 | use base qw(PVE::SectionConfig); | |
14 | ||
15 | my $inotify_file_id = 'pmg-pbs.conf'; | |
16 | my $secret_dir = '/etc/pmg/pbs'; | |
17 | my $config_filename = "${secret_dir}/pbs.conf"; | |
18 | ||
2de4cfb2 SI |
19 | my %prune_option = ( |
20 | optional => 1, | |
21 | type => 'integer', minimum => '0', | |
22 | format_description => 'N', | |
23 | ); | |
24 | ||
25 | my %prune_properties = ( | |
26 | 'keep-last' => { | |
27 | %prune_option, | |
28 | description => 'Keep the last <N> backups.', | |
29 | }, | |
30 | 'keep-hourly' => { | |
31 | %prune_option, | |
e8a18e4b TL |
32 | description => 'Keep backups for the last <N> different hours. If there is' |
33 | .' more than one backup for a single hour, only the latest one is kept.' | |
2de4cfb2 SI |
34 | }, |
35 | 'keep-daily' => { | |
36 | %prune_option, | |
e8a18e4b TL |
37 | description => 'Keep backups for the last <N> different days. If there is' |
38 | .' more than one backup for a single day, only the latest one is kept.' | |
2de4cfb2 SI |
39 | }, |
40 | 'keep-weekly' => { | |
41 | %prune_option, | |
e8a18e4b TL |
42 | description => 'Keep backups for the last <N> different weeks. If there is' |
43 | .'more than one backup for a single week, only the latest one is kept.' | |
2de4cfb2 SI |
44 | }, |
45 | 'keep-monthly' => { | |
46 | %prune_option, | |
e8a18e4b TL |
47 | description => 'Keep backups for the last <N> different months. If there is' |
48 | .' more than one backup for a single month, only the latest one is kept.' | |
2de4cfb2 SI |
49 | }, |
50 | 'keep-yearly' => { | |
51 | %prune_option, | |
e8a18e4b TL |
52 | description => 'Keep backups for the last <N> different years. If there is' |
53 | .' more than one backup for a single year, only the latest one is kept.' | |
2de4cfb2 SI |
54 | }, |
55 | ); | |
56 | ||
57 | my $defaultData = { | |
58 | propertyList => { | |
59 | type => { description => "Section type." }, | |
60 | remote => { | |
61 | description => "Proxmox Backup Server ID.", | |
62 | type => 'string', format => 'pve-configid', | |
63 | }, | |
64 | }, | |
65 | }; | |
66 | ||
67 | sub properties { | |
68 | return { | |
69 | datastore => { | |
e8a18e4b | 70 | description => "Proxmox Backup Server datastore name.", |
2de4cfb2 SI |
71 | type => 'string', |
72 | }, | |
73 | server => { | |
e8a18e4b | 74 | description => "Proxmox Backup Server address.", |
2de4cfb2 SI |
75 | type => 'string', format => 'address', |
76 | maxLength => 256, | |
77 | }, | |
78 | disable => { | |
e8a18e4b | 79 | description => "Flag to disable (deactivate) the entry.", |
2de4cfb2 SI |
80 | type => 'boolean', |
81 | optional => 1, | |
82 | }, | |
83 | password => { | |
e8a18e4b TL |
84 | description => "Password or API token secret for the user on the" |
85 | ." Proxmox Backup Server.", | |
2de4cfb2 SI |
86 | type => 'string', |
87 | optional => 1, | |
88 | }, | |
89 | username => get_standard_option('pmg-email-address', { | |
e8a18e4b | 90 | description => "Username or API token ID on the Proxmox Backup Server" |
2de4cfb2 SI |
91 | }), |
92 | fingerprint => get_standard_option('fingerprint-sha256'), | |
d6e25685 SI |
93 | 'include-statistics' => { |
94 | description => "Include statistics in scheduled backups", | |
95 | type => 'boolean', | |
96 | optional => 1, | |
97 | }, | |
2de4cfb2 SI |
98 | %prune_properties, |
99 | }; | |
100 | } | |
101 | ||
102 | sub options { | |
103 | return { | |
104 | server => {}, | |
105 | datastore => {}, | |
106 | disable => { optional => 1 }, | |
107 | username => { optional => 1 }, | |
108 | password => { optional => 1 }, | |
109 | fingerprint => { optional => 1 }, | |
d6e25685 | 110 | 'include-statistics' => { optional => 1 }, |
2de4cfb2 SI |
111 | 'keep-last' => { optional => 1 }, |
112 | 'keep-hourly' => { optional => 1 }, | |
113 | 'keep-daily' => { optional => 1 }, | |
114 | 'keep-weekly' => { optional => 1 }, | |
115 | 'keep-monthly' => { optional => 1 }, | |
116 | 'keep-yearly' => { optional => 1 }, | |
117 | }; | |
118 | } | |
119 | ||
120 | sub type { | |
121 | return 'pbs'; | |
122 | } | |
123 | ||
124 | sub private { | |
125 | return $defaultData; | |
126 | } | |
127 | ||
128 | sub prune_options { | |
129 | my ($self, $remote) = @_; | |
130 | ||
131 | my $remote_cfg = $self->{ids}->{$remote}; | |
132 | ||
133 | my $res = {}; | |
d423685c | 134 | my $pruning_setup; |
2de4cfb2 | 135 | foreach my $keep_opt (keys %prune_properties) { |
2de4cfb2 | 136 | if (defined($remote_cfg->{$keep_opt})) { |
d423685c | 137 | $pruning_setup = 1; |
2de4cfb2 SI |
138 | $res->{$keep_opt} = $remote_cfg->{$keep_opt}; |
139 | } | |
140 | } | |
d423685c | 141 | return $pruning_setup ? $res : undef; |
2de4cfb2 SI |
142 | } |
143 | ||
144 | sub new { | |
145 | my ($type) = @_; | |
146 | ||
147 | my $class = ref($type) || $type; | |
148 | ||
149 | my $cfg = PVE::INotify::read_file($inotify_file_id); | |
150 | ||
151 | $cfg->{secret_dir} = $secret_dir; | |
152 | ||
153 | return bless $cfg, $class; | |
154 | } | |
155 | ||
156 | sub write { | |
157 | my ($self) = @_; | |
158 | ||
159 | PVE::INotify::write_file($inotify_file_id, $self); | |
160 | } | |
161 | ||
2de4cfb2 SI |
162 | sub lock_config { |
163 | my ($code, $errmsg) = @_; | |
164 | ||
e8a18e4b TL |
165 | my $lockfile = "/var/lock/pmgpbsconfig.lck"; |
166 | ||
2de4cfb2 SI |
167 | my $p = PVE::Tools::lock_file($lockfile, undef, $code); |
168 | if (my $err = $@) { | |
169 | $errmsg ? die "$errmsg: $err" : die $err; | |
170 | } | |
171 | } | |
172 | ||
2de4cfb2 SI |
173 | __PACKAGE__->register(); |
174 | __PACKAGE__->init(); | |
175 | ||
176 | sub read_pmg_pbs_conf { | |
177 | my ($filename, $fh) = @_; | |
178 | ||
179 | local $/ = undef; # slurp mode | |
180 | ||
181 | my $raw = defined($fh) ? <$fh> : ''; | |
182 | ||
183 | return __PACKAGE__->parse_config($filename, $raw); | |
184 | } | |
185 | ||
186 | sub write_pmg_pbs_conf { | |
187 | my ($filename, $fh, $cfg) = @_; | |
188 | ||
189 | my $raw = __PACKAGE__->write_config($filename, $cfg); | |
190 | ||
191 | PVE::Tools::safe_print($filename, $fh, $raw); | |
192 | } | |
193 | ||
e8a18e4b TL |
194 | PVE::INotify::register_file( |
195 | $inotify_file_id, | |
196 | $config_filename, | |
197 | \&read_pmg_pbs_conf, | |
198 | \&write_pmg_pbs_conf, | |
199 | undef, | |
200 | always_call_parser => 1 | |
201 | ); | |
2de4cfb2 SI |
202 | |
203 | 1; |