I fwe are not part of a cluster we do not need to worry about other
members messing with the config. But there may be local contenders,
e.g., two automation script instances started in parallel by mistake
or two admin (sessions) which start a create or join clsuter request
at the same time.
Reuse the local flock for this purpose.

Signed-off-by: Thomas Lamprecht <[email protected]>
---
 data/PVE/API2/ClusterConfig.pm | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/data/PVE/API2/ClusterConfig.pm b/data/PVE/API2/ClusterConfig.pm
index 2ef3733..45455dd 100644
--- a/data/PVE/API2/ClusterConfig.pm
+++ b/data/PVE/API2/ClusterConfig.pm
@@ -16,6 +16,7 @@ use base qw(PVE::RESTHandler);
 
 my $clusterconf = "/etc/pve/corosync.conf";
 my $authfile = "/etc/corosync/authkey";
+my $local_cluster_change_lock = "/var/lock/pvecm.lock";
 
 __PACKAGE__->register_method({
     name => 'index',
@@ -109,7 +110,7 @@ __PACKAGE__->register_method ({
        my $rpcenv = PVE::RPCEnvironment::get();
        my $authuser = $rpcenv->get_user();
 
-       my $worker = sub {
+       my $code = sub {
            PVE::Cluster::setup_sshd_config(1);
            PVE::Cluster::setup_rootsshconfig();
            PVE::Cluster::setup_ssh_keys();
@@ -135,6 +136,10 @@ __PACKAGE__->register_method ({
            PVE::Tools::run_command('systemctl restart corosync pve-cluster');
        };
 
+       my $worker = sub {
+           PVE::Tools::lock_file($local_cluster_change_lock, 10, $code);
+       };
+
        return $rpcenv->fork_worker('clustercreate', '',  $authuser, $worker);
 }});
 
@@ -176,8 +181,7 @@ __PACKAGE__->register_method({
 my $config_change_lock = sub {
     my ($code) = @_;
 
-    my $local_lock_fn = "/var/lock/pvecm.lock";
-    PVE::Tools::lock_file($local_lock_fn, 10, sub {
+    PVE::Tools::lock_file($local_cluster_change_lock, 10, sub {
        PVE::Cluster::cfs_update(1);
        my $members = PVE::Cluster::get_members();
        if (scalar(keys %$members) > 1) {
@@ -509,7 +513,7 @@ __PACKAGE__->register_method ({
        my $rpcenv = PVE::RPCEnvironment::get();
        my $authuser = $rpcenv->get_user();
 
-       my $worker = sub {
+       my $code = sub {
            PVE::Cluster::setup_sshd_config();
            PVE::Cluster::setup_rootsshconfig();
            PVE::Cluster::setup_ssh_keys();
@@ -567,6 +571,10 @@ __PACKAGE__->register_method ({
            PVE::Cluster::finish_join($nodename, $res->{corosync_conf}, 
$res->{corosync_authkey});
        };
 
+       my $worker = sub {
+           PVE::Tools::lock_file($local_cluster_change_lock, 10, $code);
+       };
+
        return $rpcenv->fork_worker('clusterjoin', '',  $authuser, $worker);
     }});
 
-- 
2.11.0


_______________________________________________
pve-devel mailing list
[email protected]
https://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to