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