]> git.proxmox.com Git - pve-ha-manager.git/commitdiff
add parser for resource configuration
authorDietmar Maurer <dietmar@proxmox.com>
Tue, 10 Mar 2015 16:22:59 +0000 (17:22 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Tue, 10 Mar 2015 16:24:47 +0000 (17:24 +0100)
src/PVE/HA/Groups.pm
src/PVE/HA/Resources.pm [new file with mode: 0644]
src/PVE/HA/Tools.pm

index 0f9c36697fd3efe57f00e160eb49511d9f4dd2e8..7d42879df8f53df55003953f0b9af5a438bdce3f 100644 (file)
@@ -6,31 +6,10 @@ use warnings;
 use Data::Dumper;
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::SectionConfig;
+use PVE::HA::Tools;
 
 use base qw(PVE::SectionConfig);
 
-PVE::JSONSchema::register_format('pve-ha-group-node', \&pve_verify_ha_group_node);
-sub pve_verify_ha_group_node {
-    my ($node, $noerr) = @_;
-
-    if ($node !~ m/^([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)(:\d+)?$/) {
-       return undef if $noerr;
-       die "value does not look like a valid ha group node\n";
-    }
-    return $node;
-}
-
-PVE::JSONSchema::register_standard_option('pve-ha-group-node-list', {
-    description => "List of cluster node names with optional priority. We use priority '0' as default. The CRM tries to run services on the node with higest priority (also see option 'nofailback').",
-    type => 'string', format => 'pve-ha-group-node-list',
-    typetext => '<node>[:<pri>]{,<node>[:<pri>]}*',
-}); 
-
-PVE::JSONSchema::register_standard_option('pve-ha-group-id', {
-    description => "The HA group identifier.",
-    type => 'string', format => 'pve-configid',
-}); 
-
 my $defaultData = {
     propertyList => {
        type => { description => "Section type." },
diff --git a/src/PVE/HA/Resources.pm b/src/PVE/HA/Resources.pm
new file mode 100644 (file)
index 0000000..6124894
--- /dev/null
@@ -0,0 +1,114 @@
+package PVE::HA::Resources;
+
+use strict;
+use warnings;
+
+use Data::Dumper;
+use PVE::JSONSchema qw(get_standard_option);
+use PVE::SectionConfig;
+use PVE::HA::Tools;
+
+use base qw(PVE::SectionConfig);
+
+my $defaultData = {
+    propertyList => {
+       type => { description => "Resource type." },
+       name => {
+           description => "Resource name.",
+           type => 'string',
+           optional => 1,
+       },
+       group => get_standard_option('pve-ha-group-id'),
+       comment => {
+           description => "Description.",
+           type => 'string',
+           optional => 1,
+           maxLength => 4096,
+       },
+    },
+};
+
+sub verify_name {
+    my ($class, $name) = @_;
+
+    die "implement this in subclass";
+}
+
+sub private {
+    return $defaultData;
+}
+
+sub parse_section_header {
+    my ($class, $line) = @_;
+
+    if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
+       my ($type, $name) = (lc($1), $2);
+       my $errmsg = undef; # set if you want to skip whole section
+       eval {
+           if (my $plugin = $defaultData->{plugins}->{$type}) {
+               $plugin->verify_name($name);
+           }
+           # fixme: ?
+           #PVE::JSONSchema::pve_verify_configid($name);
+       };
+       $errmsg = $@ if $@;
+       my $config = {
+           name => $name,
+       }; # to return additional attributes
+       return ($type, "$type:$name", $errmsg, $config);
+    }
+    return undef;
+}
+
+package PVE::HA::Resources::PVEVM;
+
+use strict;
+use warnings;
+
+use base qw(PVE::HA::Resources);
+
+sub type {
+    return 'pvevm';
+}
+
+sub verify_name {
+    my ($class, $name) = @_;
+
+    die "invalid VMID\n" if $name !~ m/^[1-9][0-0]+$/;
+}
+
+sub options {
+    return {
+       name => {},
+       group => { optional => 1 },
+       comment => { optional => 1 },
+    };
+}
+
+package PVE::HA::Resources::IPAddr;
+
+use strict;
+use warnings;
+use PVE::Tools qw($IPV4RE $IPV6RE);
+
+use base qw(PVE::HA::Resources);
+
+sub type {
+    return 'ipaddr';
+}
+
+sub verify_name {
+    my ($class, $name) = @_;
+
+    die "invalid IP address\n" if $name !~ m!^$IPV6RE|$IPV4RE$!;
+}
+
+sub options {
+    return {
+       name => {},
+       group => { optional => 1 },
+       comment => { optional => 1 },
+    };
+}
+
+1;
index f78f66b847ef8b7fbd9f5a2387f265c92e8a5a78..9accbc1cf8e7dd7cf4e76cf31cfa49d1ef2ead8a 100644 (file)
@@ -2,9 +2,32 @@ package PVE::HA::Tools;
 
 use strict;
 use warnings;
-use JSON; 
+use JSON;
+use PVE::JSONSchema;
 use PVE::Tools;
 
+PVE::JSONSchema::register_format('pve-ha-group-node', \&pve_verify_ha_group_node);
+sub pve_verify_ha_group_node {
+    my ($node, $noerr) = @_;
+
+    if ($node !~ m/^([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)(:\d+)?$/) {
+       return undef if $noerr;
+       die "value does not look like a valid ha group node\n";
+    }
+    return $node;
+}
+
+PVE::JSONSchema::register_standard_option('pve-ha-group-node-list', {
+    description => "List of cluster node names with optional priority. We use priority '0' as default. The CRM tries to run services on the node with higest priority (also see option 'nofailback').",
+    type => 'string', format => 'pve-ha-group-node-list',
+    typetext => '<node>[:<pri>]{,<node>[:<pri>]}*',
+});
+
+PVE::JSONSchema::register_standard_option('pve-ha-group-id', {
+    description => "The HA group identifier.",
+    type => 'string', format => 'pve-configid',
+});
+
 sub read_json_from_file {
     my ($filename, $default) = @_;
 
@@ -24,7 +47,7 @@ sub write_json_to_file {
     my ($filename, $data) = @_;
 
     my $raw = encode_json($data);
+
     PVE::Tools::file_set_contents($filename, $raw);
 }