]> git.proxmox.com Git - pve-manager.git/commitdiff
jobs: move base registry to pve-common & split vzdump base out to guest-common
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Sat, 12 Nov 2022 15:56:23 +0000 (16:56 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Sat, 12 Nov 2022 15:59:38 +0000 (16:59 +0100)
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
PVE/API2/Backup.pm
PVE/Jobs.pm
PVE/Jobs/Makefile
PVE/Jobs/Plugin.pm [deleted file]
PVE/Jobs/VZDump.pm

index 233a6ebfedcc190059d3f88044289b845ed4f3c3..b6f5916d40defaf9cab357a23a24be699fca45b3 100644 (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);
index 39aafc828750b480c8b9ee399e74cad170a3538f..3e49e14bbfaa7fe542bfaf95f03fe0d8fde4f6d8 100644 (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 = $@) {
index 6023c3bab955c460b332b61652d1534deedb095a..34953e510ae4053da196daa1f3c6eadfc5a61a78 100644 (file)
@@ -1,8 +1,7 @@
 include ../../defines.mk
 
 PERLSOURCE =   \
-       Plugin.pm\
-       VZDump.pm
+       VZDump.pm \
 
 all:
 
diff --git a/PVE/Jobs/Plugin.pm b/PVE/Jobs/Plugin.pm
deleted file mode 100644 (file)
index aedfcf3..0000000
+++ /dev/null
@@ -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;
index 2963b3483f4e60a5f5e153a2b20e3748be94c6f5..3c8179ada010f76400347378b560e15005711889 100644 (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);
-
-sub type {
-    return '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) = @_;
+use PVE::VZDump::Common;
 
-    if ((my $format = $PVE::VZDump::Common::PROPERTY_STRINGS->{$key}) && ref($value) eq 'HASH') {
-       $value = PVE::JSONSchema::print_property_string($value, $format);
-    }
+use PVE::API2::VZDump;
 
-    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);
 }