jobs: move base registry to pve-common & split vzdump base out to guest-common

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2022-11-12 16:56:23 +01:00
parent 42a6d6cc77
commit 6f2e57c050
5 changed files with 20 additions and 170 deletions

View File

@ -15,6 +15,7 @@ use PVE::Storage;
use PVE::Exception qw(raise_param_exc);
use PVE::VZDump;
use PVE::VZDump::Common;
use PVE::VZDump::JobBase;
use PVE::Jobs; # for VZDump Jobs
use Proxmox::RS::CalendarEvent;
@ -223,8 +224,7 @@ __PACKAGE__->register_method({
if $data->{ids}->{$id};
PVE::VZDump::verify_vzdump_parameters($param, 1);
my $plugin = PVE::Jobs::Plugin->lookup('vzdump');
my $opts = $plugin->check_config($id, $param, 1, 1);
my $opts = PVE::VZDump::JobBase->check_config($id, $param, 1, 1);
$data->{ids}->{$id} = $opts;
@ -434,8 +434,7 @@ __PACKAGE__->register_method({
die "no options specified\n" if !scalar(keys %$param);
PVE::VZDump::verify_vzdump_parameters($param);
my $plugin = PVE::Jobs::Plugin->lookup('vzdump');
my $opts = $plugin->check_config($id, $param, 0, 1);
my $opts = PVE::VZDump::JobBase->check_config($id, $param, 0, 1);
# try to find it in old vzdump.cron and convert it to a job
my ($idx) = grep { $jobs->[$_]->{id} eq $id } (0 .. scalar(@$jobs) - 1);

View File

@ -4,13 +4,19 @@ use strict;
use warnings;
use JSON;
use PVE::Cluster qw(cfs_read_file cfs_lock_file);
use PVE::Jobs::Plugin;
use PVE::Cluster qw(cfs_lock_file cfs_read_file cfs_register_file);
use PVE::Job::Registry;
use PVE::Jobs::VZDump;
use PVE::Tools;
PVE::Jobs::VZDump->register();
PVE::Jobs::Plugin->init();
PVE::Job::Registry->init();
cfs_register_file(
'jobs.cfg',
sub { PVE::Job::Registry->parse_config(@_); },
sub { PVE::Job::Registry->write_config(@_); },
);
my $state_dir = "/var/lib/pve-manager/jobs";
my $lock_dir = "/var/lock/pve-manager";
@ -284,7 +290,7 @@ sub run_jobs {
next if !defined($next_sync) || time() < $next_sync; # not yet its (next) turn
my $plugin = PVE::Jobs::Plugin->lookup($type);
my $plugin = PVE::Job::Registry->lookup($type);
if (starting_job($id, $type)) {
my $upid = eval { $plugin->run($cfg, $id, $schedule) };
if (my $err = $@) {

View File

@ -1,8 +1,7 @@
include ../../defines.mk
PERLSOURCE = \
Plugin.pm\
VZDump.pm
VZDump.pm \
all:

View File

@ -1,108 +0,0 @@
package PVE::Jobs::Plugin;
use strict;
use warnings;
use PVE::Cluster qw(cfs_register_file);
use base qw(PVE::SectionConfig);
cfs_register_file(
'jobs.cfg',
sub { __PACKAGE__->parse_config(@_); },
sub { __PACKAGE__->write_config(@_); }
);
my $defaultData = {
propertyList => {
type => { description => "Section type." },
id => {
description => "The ID of the job.",
type => 'string',
format => 'pve-configid',
maxLength => 64,
},
enabled => {
description => "Determines if the job is enabled.",
type => 'boolean',
default => 1,
optional => 1,
},
schedule => {
description => "Backup schedule. The format is a subset of `systemd` calendar events.",
type => 'string', format => 'pve-calendar-event',
maxLength => 128,
},
comment => {
optional => 1,
type => 'string',
description => "Description for the Job.",
maxLength => 512,
},
'repeat-missed' => {
optional => 1,
type => 'boolean',
description => "If true, the job will be run as soon as possible if it was missed".
" while the scheduler was not running.",
default => 0,
},
},
};
sub private {
return $defaultData;
}
sub parse_config {
my ($class, $filename, $raw) = @_;
my $cfg = $class->SUPER::parse_config($filename, $raw);
foreach my $id (sort keys %{$cfg->{ids}}) {
my $data = $cfg->{ids}->{$id};
$data->{id} = $id;
$data->{enabled} //= 1;
if (defined($data->{comment})) {
$data->{comment} = PVE::Tools::decode_text($data->{comment});
}
}
return $cfg;
}
# call the plugin specific decode/encode code
sub decode_value {
my ($class, $type, $key, $value) = @_;
my $plugin = __PACKAGE__->lookup($type);
return $plugin->decode_value($type, $key, $value);
}
sub encode_value {
my ($class, $type, $key, $value) = @_;
my $plugin = __PACKAGE__->lookup($type);
return $plugin->encode_value($type, $key, $value);
}
sub write_config {
my ($class, $filename, $cfg) = @_;
for my $job (values $cfg->{ids}->%*) {
if (defined($job->{comment})) {
$job->{comment} = PVE::Tools::encode_text($job->{comment});
}
}
$class->SUPER::write_config($filename, $cfg);
}
sub run {
my ($class, $cfg) = @_;
# implement in subclass
die "not implemented";
}
1;

View File

@ -3,71 +3,25 @@ package PVE::Jobs::VZDump;
use strict;
use warnings;
use PVE::INotify;
use PVE::VZDump::Common;
use PVE::API2::VZDump;
use PVE::Cluster;
use PVE::JSONSchema;
use base qw(PVE::Jobs::Plugin);
use PVE::VZDump::Common;
sub type {
return 'vzdump';
}
use PVE::API2::VZDump;
my $props = PVE::VZDump::Common::json_config_properties();
sub properties {
return $props;
}
sub options {
my $options = {
enabled => { optional => 1 },
schedule => {},
comment => { optional => 1 },
'repeat-missed' => { optional => 1 },
};
foreach my $opt (keys %$props) {
if ($props->{$opt}->{optional}) {
$options->{$opt} = { optional => 1 };
} else {
$options->{$opt} = {};
}
}
return $options;
}
sub decode_value {
my ($class, $type, $key, $value) = @_;
if ((my $format = $PVE::VZDump::Common::PROPERTY_STRINGS->{$key}) && !ref($value)) {
$value = PVE::JSONSchema::parse_property_string($format, $value);
}
return $value;
}
sub encode_value {
my ($class, $type, $key, $value) = @_;
if ((my $format = $PVE::VZDump::Common::PROPERTY_STRINGS->{$key}) && ref($value) eq 'HASH') {
$value = PVE::JSONSchema::print_property_string($value, $format);
}
return $value;
}
use base qw(PVE::VZDump::JobBase);
sub run {
my ($class, $conf) = @_;
my $props = $class->properties();
# remove all non vzdump related options
foreach my $opt (keys %$conf) {
delete $conf->{$opt} if !defined($props->{$opt});
}
# Required as string parameters
# Required as string parameters # FIXME why?! we could just check ref()
for my $key (keys $PVE::VZDump::Common::PROPERTY_STRINGS->%*) {
if ($conf->{$key} && ref($conf->{$key}) eq 'HASH') {
my $format = $PVE::VZDump::Common::PROPERTY_STRINGS->{$key};
@ -77,7 +31,7 @@ sub run {
$conf->{quiet} = 1; # do not write to stdout/stderr
PVE::Cluster::cfs_update(); # refresh vmlist
PVE::Cluster::cfs_update(); # refresh vmlist; FIXME: move this to the job run loop
return PVE::API2::VZDump->vzdump($conf);
}