]> git.proxmox.com Git - pmg-api.git/blob - src/PMG/CLI/pmgdb.pm
pmgdb: extend dump output to include add/invert
[pmg-api.git] / src / PMG / CLI / pmgdb.pm
1 package PMG::CLI::pmgdb;
2
3 use strict;
4 use warnings;
5 use Data::Dumper;
6 use Encode qw(encode);
7
8 use PVE::SafeSyslog;
9 use PVE::Tools qw(extract_param);
10 use PVE::INotify;
11 use PVE::CLIHandler;
12
13 use PMG::Utils;
14 use PMG::RESTEnvironment;
15 use PMG::DBTools;
16 use PMG::RuleDB;
17 use PMG::Cluster;
18 use PMG::ClusterConfig;
19 use PMG::Statistic;
20
21 use PMG::API2::RuleDB;
22
23 use base qw(PVE::CLIHandler);
24
25 sub setup_environment {
26 PMG::RESTEnvironment->setup_default_cli_env();
27 }
28
29 sub print_objects {
30 my ($ruledb, $og) = @_;
31
32 my $objects = $ruledb->load_group_objects ($og->{id});
33
34 foreach my $obj (@$objects) {
35 my $desc = encode('UTF-8', $obj->short_desc());
36 print " OBJECT $obj->{id}: $desc\n";
37 }
38 }
39
40 sub print_rule {
41 my ($ruledb, $rule) = @_;
42
43 $ruledb->load_rule_attributes($rule);
44
45 my $direction = {
46 0 => 'in',
47 1 => 'out',
48 2 => 'in+out',
49 };
50 my $active = $rule->{active} ? 'active' : 'inactive';
51 my $dir = $direction->{$rule->{direction}};
52 my $rulename = encode('UTF-8', $rule->{name});
53
54 print "Found RULE $rule->{id} (prio: $rule->{priority}, $dir, $active): $rulename\n";
55
56 my $print_group = sub {
57 my ($type, $og, $print_mode) = @_;
58 my $oname = encode('UTF-8', $og->{name});
59 my $mode = "";
60 if ($print_mode) {
61 my $and = $og->{and} // 0;
62 my $invert = $og->{invert} // 0;
63 $mode = " (and=$and, invert=$invert)";
64 }
65 print " FOUND $type GROUP $og->{id}${mode}: $oname\n";
66 print_objects($ruledb, $og);
67 };
68
69 my $print_type_mode = sub {
70 my ($type) = @_;
71 my $and = $rule->{"$type-and"};
72 my $invert = $rule->{"$type-invert"};
73 if (defined($and) || defined($invert)) {
74 my $print_type = uc($type);
75 print " $print_type mode: and=" . ($and // 0) . " invert=". ($invert // 0) . "\n";
76 }
77 };
78
79 my ($from, $to, $when, $what, $action) =
80 $ruledb->load_groups($rule);
81
82 $print_type_mode->("from") if scalar(@$from);
83 foreach my $og (@$from) {
84 $ruledb->load_group_attributes($og);
85 $print_group->("FROM", $og, 1);
86 }
87 $print_type_mode->("to") if scalar(@$to);
88 foreach my $og (@$to) {
89 $ruledb->load_group_attributes($og);
90 $print_group->("TO", $og, 1);
91 }
92 $print_type_mode->("when") if scalar(@$when);
93 foreach my $og (@$when) {
94 $ruledb->load_group_attributes($og);
95 $print_group->("WHEN", $og, 1);
96 }
97 $print_type_mode->("what") if scalar(@$what);
98 foreach my $og (@$what) {
99 $ruledb->load_group_attributes($og);
100 $print_group->("WHAT", $og, 1);
101 }
102 foreach my $og (@$action) {
103 $print_group->("ACTION", $og);
104 }
105 }
106
107 __PACKAGE__->register_method ({
108 name => 'dump',
109 path => 'dump',
110 method => 'GET',
111 description => "Print the PMG rule database.",
112 parameters => {
113 additionalProperties => 0,
114 properties => {},
115 },
116 returns => { type => 'null'},
117 code => sub {
118 my ($param) = @_;
119
120 my $dbh = PMG::DBTools::open_ruledb("Proxmox_ruledb");
121 my $ruledb = PMG::RuleDB->new($dbh);
122
123 my $rules = $ruledb->load_rules();
124
125 foreach my $rule (@$rules) {
126 print_rule($ruledb, $rule);
127 }
128
129 $ruledb->close();
130
131 return undef;
132 }});
133
134
135 __PACKAGE__->register_method ({
136 name => 'delete',
137 path => 'delete',
138 method => 'DELETE',
139 description => "Delete PMG rule database.",
140 parameters => {
141 additionalProperties => 0,
142 properties => {},
143 },
144 returns => { type => 'null'},
145 code => sub {
146 my ($param) = @_;
147
148 my $list = PMG::DBTools::database_list();
149
150 my $dbname = "Proxmox_ruledb";
151
152 die "Database '$dbname' does not exist\n" if !$list->{$dbname};
153
154 syslog('info', "delete rule database");
155
156 PMG::DBTools::delete_ruledb($dbname);
157
158 return undef;
159 }});
160
161 __PACKAGE__->register_method ({
162 name => 'init',
163 path => 'init',
164 method => 'POST',
165 description => "Initialize/Upgrade the PMG rule database.",
166 parameters => {
167 additionalProperties => 0,
168 properties => {
169 force => {
170 type => 'boolean',
171 description => "Delete existing database.",
172 optional => 1,
173 default => 0,
174 },
175 statistics => {
176 type => 'boolean',
177 description => "Reset and update statistic database.",
178 optional => 1,
179 default => 0,
180 },
181 }
182 },
183 returns => { type => 'null'},
184 code => sub {
185 my ($param) = @_;
186
187 PMG::Utils::cond_add_default_locale();
188
189 my $list = PMG::DBTools::database_list();
190
191 my $dbname = "Proxmox_ruledb";
192
193 if (!$list->{$dbname} || $param->{force}) {
194
195 if ($list->{$dbname}) {
196 print "Destroy existing rule database\n";
197 PMG::DBTools::delete_ruledb($dbname);
198 }
199
200 print "Initialize rule database\n";
201
202 my $dbh = PMG::DBTools::create_ruledb ($dbname);
203 my $ruledb = PMG::RuleDB->new($dbh);
204 PMG::DBTools::init_ruledb($ruledb);
205
206 $dbh->disconnect();
207
208 } else {
209
210 my $dbh = PMG::DBTools::open_ruledb("Proxmox_ruledb");
211 my $ruledb = PMG::RuleDB->new($dbh);
212
213 print "Analyzing/Upgrading existing Databases...";
214 PMG::DBTools::upgradedb ($ruledb);
215 print "done\n";
216
217 # reset and update statistic databases
218 if ($param->{statistics}) {
219 print "Generating Proxmox Statistic Databases... ";
220 PMG::Statistic::clear_stats($dbh);
221 my $cinfo = PVE::INotify::read_file("cluster.conf");
222 PMG::Statistic::update_stats($dbh, $cinfo);
223 print "done\n";
224 }
225
226 $dbh->disconnect();
227 }
228
229 return undef;
230 }});
231
232
233 __PACKAGE__->register_method ({
234 name => 'update',
235 path => 'update',
236 method => 'POST',
237 description => "Update the PMG statistic database.",
238 parameters => {
239 additionalProperties => 0,
240 properties => {},
241 },
242 returns => { type => 'null'},
243 code => sub {
244 my ($param) = @_;
245
246 my $dbh = PMG::DBTools::open_ruledb("Proxmox_ruledb");
247 print "Updating Proxmox Statistic Databases... ";
248 my $cinfo = PVE::INotify::read_file("cluster.conf");
249 PMG::Statistic::update_stats($dbh, $cinfo);
250 print "done\n";
251 $dbh->disconnect();
252
253 return undef;
254 }});
255
256 our $cmddef = {
257 'dump' => [ __PACKAGE__, 'dump', []],
258 delete => [ __PACKAGE__, 'delete', []],
259 init => [ __PACKAGE__, 'init', []],
260 reset => [ 'PMG::API2::RuleDB', 'reset_ruledb', []],
261 update => [ __PACKAGE__, 'update', []],
262 };
263
264 1;