[pve-devel] [PATCH cluster v5 04/17] node add: factor out local joining steps

Thomas Lamprecht t.lamprecht at proxmox.com
Fri Jan 26 14:25:14 CET 2018


Factor out the code which finishes the join to a cluster on the
joinee side, after a cluster member approved the join request and
supplied us with the necessary information.

Will be used by API and the SSH join code paths.

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 data/PVE/CLI/pvecm.pm | 90 ++-------------------------------------------------
 data/PVE/Cluster.pm   | 81 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+), 87 deletions(-)

diff --git a/data/PVE/CLI/pvecm.pm b/data/PVE/CLI/pvecm.pm
index 91bd469..4659d4b 100755
--- a/data/PVE/CLI/pvecm.pm
+++ b/data/PVE/CLI/pvecm.pm
@@ -21,44 +21,8 @@ $ENV{HOME} = '/root'; # for ssh-copy-id
 my $basedir = "/etc/pve";
 my $clusterconf = "$basedir/corosync.conf";
 my $libdir = "/var/lib/pve-cluster";
-my $backupdir = "/var/lib/pve-cluster/backup";
-my $dbfile = "$libdir/config.db";
 my $authfile = "/etc/corosync/authkey";
 
-sub backup_database {
-
-    print "backup old database\n";
-
-    mkdir $backupdir;
-
-    my $ctime = time();
-    my $cmd = [
-	['echo', '.dump'],
-	['sqlite3', $dbfile],
-	['gzip', '-', \ ">${backupdir}/config-${ctime}.sql.gz"],
-    ];
-
-    run_command($cmd, 'errmsg' => "cannot backup old database\n");
-
-    # purge older backup
-    my $maxfiles = 10;
-
-    my @bklist = ();
-    foreach my $fn (<$backupdir/config-*.sql.gz>) {
-	if ($fn =~ m!/config-(\d+)\.sql.gz$!) {
-	    push @bklist, [$fn, $1];
-	}
-    }
-
-    @bklist = sort { $b->[1] <=> $a->[1] } @bklist;
-
-    while (scalar (@bklist) >= $maxfiles) {
-	my $d = pop @bklist;
-	print "delete old backup '$d->[0]'\n";
-	unlink $d->[0];
-    }
-}
-
 
 __PACKAGE__->register_method ({
     name => 'keygen',
@@ -334,58 +298,10 @@ __PACKAGE__->register_method ({
 
 	    system(@$cmd) == 0 || die "can't rsync data from host '$host'\n";
 
-	    mkdir "/etc/corosync";
-	    my $confbase = basename($clusterconf);
+	    my $corosync_conf = PVE::Tools::file_get_contents("$tmpdir/corosync.conf");
+	    my $corosync_authkey = PVE::Tools::file_get_contents("$tmpdir/authkey");
 
-	    $cmd = "cp '$tmpdir/$confbase' '/etc/corosync/$confbase'";
-	    system($cmd) == 0 || die "can't copy cluster configuration\n";
-
-	    my $keybase = basename($authfile);
-	    system ("cp '$tmpdir/$keybase' '$authfile'") == 0 ||
-		die "can't copy '$tmpdir/$keybase' to '$authfile'\n";
-
-	    print "stopping pve-cluster service\n";
-
-	    system("umount $basedir -f >/dev/null 2>&1");
-	    system("systemctl stop pve-cluster") == 0 ||
-		die "can't stop pve-cluster service\n";
-
-	    backup_database();
-
-	    unlink $dbfile;
-
-	    system("systemctl start pve-cluster") == 0 ||
-		die "starting pve-cluster failed\n";
-
-	    system("systemctl start corosync");
-
-	    # wait for quorum
-	    my $printqmsg = 1;
-	    while (!PVE::Cluster::check_cfs_quorum(1)) {
-		if ($printqmsg) {
-		    print "waiting for quorum...";
-		    STDOUT->flush();
-		    $printqmsg = 0;
-		}
-		sleep(1);
-	    }
-	    print "OK\n" if !$printqmsg;
-
-	    my $local_ip_address = PVE::Cluster::remote_node_ip($nodename);
-
-	    print "generating node certificates\n";
-	    PVE::Cluster::gen_pve_node_files($nodename, $local_ip_address);
-
-	    print "merge known_hosts file\n";
-	    PVE::Cluster::ssh_merge_known_hosts($nodename, $local_ip_address, 1);
-
-	    print "restart services\n";
-	    # restart pvedaemon (changed certs)
-	    system("systemctl restart pvedaemon");
-	    # restart pveproxy (changed certs)
-	    system("systemctl restart pveproxy");
-
-	    print "successfully added node '$nodename' to cluster.\n";
+	    PVE::Cluster::finish_join($host, $corosync_conf, $corosync_authkey);
 	};
 	my $err = $@;
 
diff --git a/data/PVE/Cluster.pm b/data/PVE/Cluster.pm
index d6e9f37..2229972 100644
--- a/data/PVE/Cluster.pm
+++ b/data/PVE/Cluster.pm
@@ -38,7 +38,10 @@ my $authdir = "$basedir/priv";
 my $lockdir = "/etc/pve/priv/lock";
 
 # cfs and corosync files
+my $dbfile = "/var/lib/pve-cluster/config.db";
+my $dbbackupdir = "/var/lib/pve-cluster/backup";
 my $localclusterdir = "/etc/corosync";
+my $localclusterconf = "$localclusterdir/corosync.conf";
 my $authfile = "$localclusterdir/authkey";
 my $clusterconf = "$basedir/corosync.conf";
 
@@ -1748,4 +1751,82 @@ sub assert_joinable {
     die "detected the following error(s):\n$errors" if $errors;
 }
 
+my $backup_cfs_database = sub {
+    my ($dbfile) = @_;
+
+    mkdir $dbbackupdir;
+
+    print "backup old database\n";
+    my $ctime = time();
+    my $cmd = [
+	['echo', '.dump'],
+	['sqlite3', $dbfile],
+	['gzip', '-', \ ">${dbbackupdir}/config-${ctime}.sql.gz"],
+    ];
+
+    PVE::Tools::run_command($cmd, 'errmsg' => "cannot backup old database\n");
+
+    # purge older backup
+    my $maxfiles = 10;
+    my @bklist = ();
+    foreach my $fn (<$dbbackupdir/config-*.sql.gz>) {
+	if ($fn =~ m!/config-(\d+)\.sql.gz$!) {
+	    push @bklist, [$fn, $1];
+	}
+    }
+
+    @bklist = sort { $b->[1] <=> $a->[1] } @bklist;
+    while (scalar (@bklist) >= $maxfiles) {
+	my $d = pop @bklist;
+	print "delete old backup '$d->[0]'\n";
+	unlink $d->[0];
+    }
+};
+
+sub finish_join {
+    my ($nodename, $corosync_conf, $corosync_authkey) = @_;
+
+    mkdir "$localclusterdir";
+    PVE::Tools::file_set_contents($authfile, $corosync_authkey);
+    PVE::Tools::file_set_contents($localclusterconf, $corosync_conf);
+
+    print "stopping pve-cluster service\n";
+
+    system("umount $basedir -f >/dev/null 2>&1");
+    die "can't stop pve-cluster service\n" if system("systemctl stop pve-cluster") != 0;
+
+    $backup_cfs_database->($dbfile);
+    unlink $dbfile;
+
+    system("systemctl start pve-cluster") == 0 || die "starting pve-cluster failed\n";
+    system("systemctl start corosync");
+
+    # wait for quorum
+    my $printqmsg = 1;
+    while (!PVE::Cluster::check_cfs_quorum(1)) {
+	if ($printqmsg) {
+	    print "waiting for quorum...";
+	    STDOUT->flush();
+	    $printqmsg = 0;
+	}
+	sleep(1);
+    }
+    print "OK\n" if !$printqmsg;
+
+    my $local_ip_address = PVE::Cluster::remote_node_ip($nodename);
+
+    print "generating node certificates\n";
+    PVE::Cluster::gen_pve_node_files($nodename, $local_ip_address);
+
+    print "merge known_hosts file\n";
+    PVE::Cluster::ssh_merge_known_hosts($nodename, $local_ip_address, 1);
+
+    print "restart services\n";
+    # restart pvedaemon and pveproxy (changed certs)
+    system("systemctl restart pvedaemon pveproxy");
+
+    print "successfully added node '$nodename' to cluster.\n";
+}
+
+
 1;
-- 
2.14.2





More information about the pve-devel mailing list