[pve-devel] [PATCH] add resource library

Dietmar Maurer dietmar at proxmox.com
Mon Sep 2 10:10:54 CEST 2013


For now, IP pool is the only resource we have.

Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
 data/PVE/Cluster.pm         |    1 +
 data/PVE/Makefile.am        |    5 +++-
 data/PVE/Resource.pm        |   18 +++++++++++++++
 data/PVE/Resource/IPPool.pm |   49 +++++++++++++++++++++++++++++++++++++++
 data/PVE/Resource/Plugin.pm |   54 +++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 126 insertions(+), 1 deletion(-)
 create mode 100644 data/PVE/Resource.pm
 create mode 100755 data/PVE/Resource/IPPool.pm
 create mode 100755 data/PVE/Resource/Plugin.pm

diff --git a/data/PVE/Cluster.pm b/data/PVE/Cluster.pm
index 5ccd07f..eed090d 100644
--- a/data/PVE/Cluster.pm
+++ b/data/PVE/Cluster.pm
@@ -58,6 +58,7 @@ my $observed = {
     'vzdump.cron' => 1,
     'storage.cfg' => 1,
     'datacenter.cfg' => 1,
+    'resource.cfg' => 1,
     'cluster.conf' => 1,
     'cluster.conf.new' => 1,
     'user.cfg' => 1,
diff --git a/data/PVE/Makefile.am b/data/PVE/Makefile.am
index 7635a78..e0e0aa1 100644
--- a/data/PVE/Makefile.am
+++ b/data/PVE/Makefile.am
@@ -34,9 +34,12 @@ man5_MANS = datacenter.cfg.5.gz
 
 IPCC_so_SOURCES = IPCC.c ppport.h
 
-pvelib_DATA = IPCC.pm Cluster.pm
+pvelib_DATA = IPCC.pm Cluster.pm Resource.pm Resource/Plugin.pm
 pvelibdir = /usr/share/perl5/PVE
 
+pvereslib_DATA = Resource/Plugin.pm Resource/IPPool.pm
+pvereslibdir = /usr/share/perl5/PVE/Resource
+
 install-exec-hook:
 	perl -I.. ./pvecm verifyapi
 
diff --git a/data/PVE/Resource.pm b/data/PVE/Resource.pm
new file mode 100644
index 0000000..5acc37a
--- /dev/null
+++ b/data/PVE/Resource.pm
@@ -0,0 +1,18 @@
+package PVE::Resource;
+
+use strict;
+use warnings;
+
+use PVE::Resource::Plugin;
+use PVE::Resource::IPPool;
+
+# code to read/write /etc/pve/resource.cfg
+PVE::Resource::IPPool->register();
+PVE::Resource::Plugin->init();
+
+# resource related function follow
+
+# pve_allocate_ip($vmid, $ippool, $ip);
+# pve_free_ip($vmid, $ippool, $ip);
+
+1;
diff --git a/data/PVE/Resource/IPPool.pm b/data/PVE/Resource/IPPool.pm
new file mode 100755
index 0000000..16c14d8
--- /dev/null
+++ b/data/PVE/Resource/IPPool.pm
@@ -0,0 +1,49 @@
+package PVE::Resource::IPPool;
+
+use strict;
+use warnings;
+use PVE::JSONSchema;
+use PVE::Resource::Plugin;
+
+use base qw(PVE::Resource::Plugin);
+
+PVE::JSONSchema::register_format('iprange', \&pve_verify_iprange);
+sub pve_verify_iprange {
+    my ($iprange, $noerr) = @_;
+
+    if (!Net::IP->new($iprange))  {
+	return undef if $noerr;
+	die "value does not look like a valid IP range\n";
+    }
+    return $iprange;
+}
+
+sub type {
+    return 'ippool';
+}
+
+sub properties {
+    return {
+	iprange => {
+	    description => "IP range (examples: '195.114.80/24', 192.168.2.100 - 192.168.2.150)",
+	    type => 'string', format => 'iprange',
+	    maxLength => 256,
+	},
+	dhcp => {
+	    description => "Enable internal DHCP daemon on specified interface.",
+	    type => 'string',
+	    pattern => 'eth\d+|vmbr\d+|bond\d+',
+	    maxLength => 64,
+	}
+    };
+}
+
+sub options {
+    return {
+	iprange => { optional => 0 },	
+	dhcp => { optional => 1 },
+	comment => { optional => 1 },
+    };
+}
+
+1;
diff --git a/data/PVE/Resource/Plugin.pm b/data/PVE/Resource/Plugin.pm
new file mode 100755
index 0000000..083b8d2
--- /dev/null
+++ b/data/PVE/Resource/Plugin.pm
@@ -0,0 +1,54 @@
+package PVE::Resource::Plugin;
+
+use strict;
+use warnings;
+
+use PVE::INotify;
+use PVE::SectionConfig;
+use PVE::JSONSchema qw(get_standard_option);
+use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_lock_file);
+use Data::Dumper;
+
+use base qw(PVE::SectionConfig);
+
+my $resourceconfigfile = "resource.cfg";
+
+cfs_register_file($resourceconfigfile, 
+		  sub { __PACKAGE__->parse_config(@_); },
+		  sub { __PACKAGE__->write_config(@_); });
+
+my $defaultData = {
+    propertyList => {
+	type => { description => "Resource type." },
+	name => { 
+	    description => "Resource name.",
+	    type => 'string', format => 'pve-configid',
+	},
+	comment => { 
+	    description => "Description.",
+	    type => 'string', 
+	    optional => 1,
+	    maxLength => 4096,
+	},
+    },
+};
+
+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 { PVE::JSONSchema::pve_verify_configid($name); };
+	$errmsg = $@ if $@;
+	my $config = {}; # to return additional attributes
+	return ($type, $name, $errmsg, $config);
+    }
+    return undef;
+}
+
+sub private {
+    return $defaultData;
+}
+
+1;
-- 
1.7.10.4



More information about the pve-devel mailing list