[pve-devel] [PATCH RFC 17/21] expose addnode/delnode on new cluster config API

Dietmar Maurer dietmar at proxmox.com
Mon Nov 28 08:09:09 CET 2016


Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
 data/PVE/API2/ClusterConfig.pm | 168 ++++++++++++++++++++++++++++++++++++++++
 data/PVE/CLI/pvecm.pm          | 171 +----------------------------------------
 2 files changed, 170 insertions(+), 169 deletions(-)

diff --git a/data/PVE/API2/ClusterConfig.pm b/data/PVE/API2/ClusterConfig.pm
index f1c2461..6ac2378 100644
--- a/data/PVE/API2/ClusterConfig.pm
+++ b/data/PVE/API2/ClusterConfig.pm
@@ -70,6 +70,174 @@ __PACKAGE__->register_method({
 	return PVE::RESTHandler::hash_to_array($nodelist, 'node');
     }});
 
+__PACKAGE__->register_method ({
+    name => 'addnode',
+    path => 'nodes',
+    method => 'POST',
+    protected => 1,
+    description => "Adds a node to the cluster configuration.",
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    node => PVE::JSONSchema::get_standard_option('pve-node'),
+	    nodeid => {
+		type => 'integer',
+		description => "Node id for this node.",
+		minimum => 1,
+		optional => 1,
+	    },
+	    votes => {
+		type => 'integer',
+		description => "Number of votes for this node",
+		minimum => 0,
+		optional => 1,
+	    },
+	    force => {
+		type => 'boolean',
+		description => "Do not throw error if node already exists.",
+		optional => 1,
+	    },
+	    ring0_addr => {
+		type => 'string', format => 'address',
+		description => "Hostname (or IP) of the corosync ring0 address of this node.".
+		    " Defaults to nodes hostname.",
+		optional => 1,
+	    },
+	    ring1_addr => {
+		type => 'string', format => 'address',
+		description => "Hostname (or IP) of the corosync ring1 address, this".
+		    " needs an valid bindnet1_addr.",
+		optional => 1,
+	    },
+	},
+    },
+    returns => { type => 'null' },
+    code => sub {
+	my ($param) = @_;
+
+	PVE::Cluster::check_cfs_quorum();
+
+	my $conf = PVE::Cluster::cfs_read_file("corosync.conf");
+
+	my $nodelist = PVE::Cluster::corosync_nodelist($conf);
+
+	my $totem_cfg = PVE::Cluster::corosync_totem_config($conf);
+
+	my $name = $param->{node};
+
+	$param->{ring0_addr} = $name if !$param->{ring0_addr};
+
+	die " ring1_addr needs a configured ring 1 interface!\n"
+	    if $param->{ring1_addr} && !defined($totem_cfg->{interface}->{1});
+
+	if (defined(my $res = $nodelist->{$name})) {
+	    $param->{nodeid} = $res->{nodeid} if !$param->{nodeid};
+	    $param->{votes} = $res->{quorum_votes} if !defined($param->{votes});
+
+	    if ($res->{quorum_votes} == $param->{votes} &&
+		$res->{nodeid} == $param->{nodeid}) {
+		print "node $name already defined\n";
+		if ($param->{force}) {
+		    exit (0);
+		} else {
+		    exit (-1);
+		}
+	    } else {
+		die "can't add existing node\n";
+	    }
+	} elsif (!$param->{nodeid}) {
+	    my $nodeid = 1;
+
+	    while(1) {
+		my $found = 0;
+		foreach my $v (values %$nodelist) {
+		    if ($v->{nodeid} eq $nodeid) {
+			$found = 1;
+			$nodeid++;
+			last;
+		    }
+		}
+		last if !$found;
+	    };
+
+	    $param->{nodeid} = $nodeid;
+	}
+
+	$param->{votes} = 1 if !defined($param->{votes});
+
+	PVE::Cluster::gen_local_dirs($name);
+
+	eval { 	PVE::Cluster::ssh_merge_keys(); };
+	warn $@ if $@;
+
+	$nodelist->{$name} = {
+	    ring0_addr => $param->{ring0_addr},
+	    nodeid => $param->{nodeid},
+	    name => $name,
+	};
+	$nodelist->{$name}->{ring1_addr} = $param->{ring1_addr} if $param->{ring1_addr};
+	$nodelist->{$name}->{quorum_votes} = $param->{votes} if $param->{votes};
+
+	PVE::Cluster::corosync_update_nodelist($conf, $nodelist);
+
+	return undef;
+    }});
+
+
+__PACKAGE__->register_method ({
+    name => 'delnode',
+    path => 'nodes',
+    method => 'DELETE',
+    protected => 1,
+    description => "Removes a node from the cluster configuration.",
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    node => {
+		type => 'string',
+		description => "Hostname or IP of the corosync ring0 address of this node.",
+	    },
+	},
+    },
+    returns => { type => 'null' },
+    code => sub {
+	my ($param) = @_;
+
+	PVE::Cluster::check_cfs_quorum();
+
+	my $conf = PVE::Cluster::cfs_read_file("corosync.conf");
+
+	my $nodelist = PVE::Cluster::corosync_nodelist($conf);
+
+	my $node;
+	my $nodeid;
+
+	foreach my $tmp_node (keys %$nodelist) {
+	    my $d = $nodelist->{$tmp_node};
+	    my $ring0_addr = $d->{ring0_addr};
+	    my $ring1_addr = $d->{ring1_addr};
+	    if (($tmp_node eq $param->{node}) ||
+		(defined($ring0_addr) && ($ring0_addr eq $param->{node})) ||
+		(defined($ring1_addr) && ($ring1_addr eq $param->{node}))) {
+		$node = $tmp_node;
+		$nodeid = $d->{nodeid};
+		last;
+	    }
+	}
+
+	die "Node/IP: $param->{node} is not a known host of the cluster.\n"
+		if !defined($node);
+
+	delete $nodelist->{$node};
+
+	PVE::Cluster::corosync_update_nodelist($conf, $nodelist);
+
+	PVE::Tools::run_command(['corosync-cfgtool','-k', $nodeid])
+	    if defined($nodeid);
+
+	return undef;
+    }});
+
 __PACKAGE__->register_method({
     name => 'totem',
     path => 'totem',
diff --git a/data/PVE/CLI/pvecm.pm b/data/PVE/CLI/pvecm.pm
index b5b5311..3a44673 100755
--- a/data/PVE/CLI/pvecm.pm
+++ b/data/PVE/CLI/pvecm.pm
@@ -280,173 +280,6 @@ _EOD
 	return undef;
 }});
 
-__PACKAGE__->register_method ({
-    name => 'addnode', 
-    path => 'addnode',
-    method => 'PUT',
-    description => "Adds a node to the cluster configuration.",
-    parameters => {
-    	additionalProperties => 0,
-	properties => {
-	    node => PVE::JSONSchema::get_standard_option('pve-node'),
-	    nodeid => {
-		type => 'integer',
-		description => "Node id for this node.",
-		minimum => 1,
-		optional => 1,
-	    },
-	    votes => {
-		type => 'integer',
-		description => "Number of votes for this node",
-		minimum => 0,
-		optional => 1,
-	    },
-	    force => {
-		type => 'boolean',
-		description => "Do not throw error if node already exists.",
-		optional => 1,
-	    },
-	    ring0_addr => {
-		type => 'string', format => 'address',
-		description => "Hostname (or IP) of the corosync ring0 address of this node.".
-		    " Defaults to nodes hostname.",
-		optional => 1,
-	    },
-	    ring1_addr => {
-		type => 'string', format => 'address',
-		description => "Hostname (or IP) of the corosync ring1 address, this".
-		    " needs an valid bindnet1_addr.",
-		optional => 1,
-	    },
-	},
-    },
-    returns => { type => 'null' },
-    
-    code => sub {
-	my ($param) = @_;
-
-	PVE::Cluster::check_cfs_quorum();
-
-	my $conf = PVE::Cluster::cfs_read_file("corosync.conf");
-
-	my $nodelist = PVE::Cluster::corosync_nodelist($conf);
-
-	my $totem_cfg = PVE::Cluster::corosync_totem_config($conf);
-
-	my $name = $param->{node};
-
-	$param->{ring0_addr} = $name if !$param->{ring0_addr};
-
-	die " ring1_addr needs a configured ring 1 interface!\n"
-	    if $param->{ring1_addr} && !defined($totem_cfg->{interface}->{1});
-
-	if (defined(my $res = $nodelist->{$name})) {
-	    $param->{nodeid} = $res->{nodeid} if !$param->{nodeid};
-	    $param->{votes} = $res->{quorum_votes} if !defined($param->{votes});
-
-	    if ($res->{quorum_votes} == $param->{votes} &&
-		$res->{nodeid} == $param->{nodeid}) {
-		print "node $name already defined\n";
-		if ($param->{force}) {
-		    exit (0);
-		} else {
-		    exit (-1);
-		}
-	    } else {
-		die "can't add existing node\n";
-	    }
-	} elsif (!$param->{nodeid}) {
-	    my $nodeid = 1;
-	    
-	    while(1) {
-		my $found = 0; 
-		foreach my $v (values %$nodelist) {
-		    if ($v->{nodeid} eq $nodeid) {
-			$found = 1;
-			$nodeid++;
-			last;
-		    }
-		}
-		last if !$found;
-	    };
-
-	    $param->{nodeid} = $nodeid;
-	}
-
-	$param->{votes} = 1 if !defined($param->{votes});
-
-	PVE::Cluster::gen_local_dirs($name);
-
-	eval { 	PVE::Cluster::ssh_merge_keys(); };
-	warn $@ if $@;
-
-	$nodelist->{$name} = {
-	    ring0_addr => $param->{ring0_addr},
-	    nodeid => $param->{nodeid},
-	    name => $name,
-	};
-	$nodelist->{$name}->{ring1_addr} = $param->{ring1_addr} if $param->{ring1_addr};
-	$nodelist->{$name}->{quorum_votes} = $param->{votes} if $param->{votes};
-	
-	PVE::Cluster::corosync_update_nodelist($conf, $nodelist);
-	
-	exit (0);
-    }});
-
-
-__PACKAGE__->register_method ({
-    name => 'delnode', 
-    path => 'delnode',
-    method => 'PUT',
-    description => "Removes a node to the cluster configuration.",
-    parameters => {
-    	additionalProperties => 0,
-	properties => {
-	    node => {
-		type => 'string',
-		description => "Hostname or IP of the corosync ring0 address of this node.",
-	    },
-	},
-    },
-    returns => { type => 'null' },
-    
-    code => sub {
-	my ($param) = @_;
-
-	PVE::Cluster::check_cfs_quorum();
-
-	my $conf = PVE::Cluster::cfs_read_file("corosync.conf");
-
-	my $nodelist = PVE::Cluster::corosync_nodelist($conf);
-
-	my $node;
-	my $nodeid;
-
-	foreach my $tmp_node (keys %$nodelist) {
-	    my $d = $nodelist->{$tmp_node};
-	    my $ring0_addr = $d->{ring0_addr};
-	    my $ring1_addr = $d->{ring1_addr};
-	    if (($tmp_node eq $param->{node}) ||
-		(defined($ring0_addr) && ($ring0_addr eq $param->{node})) ||
-		(defined($ring1_addr) && ($ring1_addr eq $param->{node}))) {
-		$node = $tmp_node;
-		$nodeid = $d->{nodeid};
-		last;
-	    }
-	}
-
-	die "Node/IP: $param->{node} is not a known host of the cluster.\n"
-		if !defined($node);
-
-	delete $nodelist->{$node};
-
-	PVE::Cluster::corosync_update_nodelist($conf, $nodelist);
-
-	PVE::Tools::run_command(['corosync-cfgtool','-k', $nodeid])
-	    if defined($nodeid);
-
-	return undef;
-    }});
 
 __PACKAGE__->register_method ({
     name => 'add', 
@@ -787,8 +620,8 @@ our $cmddef = {
     keygen => [ __PACKAGE__, 'keygen', ['filename']],
     create => [ __PACKAGE__, 'create', ['clustername']],
     add => [ __PACKAGE__, 'add', ['hostname']],
-    addnode => [ __PACKAGE__, 'addnode', ['node']],
-    delnode => [ __PACKAGE__, 'delnode', ['node']],
+    addnode => [ 'PVE::API2::ClusterConfig', 'addnode', ['node']],
+    delnode => [ 'PVE::API2::ClusterConfig', 'delnode', ['node']],
     status => [ __PACKAGE__, 'status' ],
     nodes => [ __PACKAGE__, 'nodes' ],
     expected => [ __PACKAGE__, 'expected', ['expected']],
-- 
2.1.4




More information about the pve-devel mailing list