]> git.proxmox.com Git - pve-cluster.git/blobdiff - data/PVE/CLI/pvecm.pm
pvecm: check if APIClient exception code is defined
[pve-cluster.git] / data / PVE / CLI / pvecm.pm
index a721e1a6ddcad0ec6c69d709e64a206c7adaa51f..746bcbec60f5a1336da5732ba69fab3d0609ad55 100755 (executable)
@@ -8,7 +8,7 @@ use File::Basename;
 use PVE::Tools qw(run_command);
 use PVE::Cluster;
 use PVE::INotify;
-use PVE::JSONSchema;
+use PVE::JSONSchema qw(get_standard_option);
 use PVE::RPCEnvironment;
 use PVE::CLIHandler;
 use PVE::PTY;
@@ -75,12 +75,7 @@ __PACKAGE__->register_method ({
                type => 'string',
                description => "Hostname (or IP) of an existing cluster member."
            },
-           nodeid => {
-               type => 'integer',
-               description => "Node id for this node.",
-               minimum => 1,
-               optional => 1,
-           },
+           nodeid => get_standard_option('corosync-nodeid'),
            votes => {
                type => 'integer',
                description => "Number of votes for this node",
@@ -92,19 +87,9 @@ __PACKAGE__->register_method ({
                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 configured ring 1 interface in the cluster.",
-               optional => 1,
-           },
-           fingerprint => PVE::JSONSchema::get_standard_option('fingerprint-sha256', {
+           ring0_addr => get_standard_option('corosync-ring0-addr'),
+           ring1_addr => get_standard_option('corosync-ring1-addr'),
+           fingerprint => get_standard_option('fingerprint-sha256', {
                optional => 1,
            }),
            'use_ssh' => {
@@ -125,71 +110,81 @@ __PACKAGE__->register_method ({
 
        PVE::Cluster::assert_joinable($param->{ring0_addr}, $param->{ring1_addr}, $param->{force});
 
-       if (!$param->{use_ssh}) {
-           print "Please enter superuser (root) password for '$host':\n";
-           my $password = PVE::PTY::read_password("Password for root\@$host: ");
+       my $worker = sub {
 
-           delete $param->{use_ssh};
-           $param->{password} = $password;
+           if (!$param->{use_ssh}) {
+               print "Please enter superuser (root) password for '$host':\n";
+               my $password = PVE::PTY::read_password("Password for root\@$host: ");
 
-           eval { PVE::Cluster::join($param) };
+               delete $param->{use_ssh};
+               $param->{password} = $password;
 
-           if (my $err = $@) {
-               if (ref($err) eq 'PVE::APIClient::Exception' && $err->{code} == 501) {
-                   $err = "Remote side is not able to use API for Cluster join!\n" .
-                          "Pass the 'use_ssh' switch or update the remote side.\n";
+               my $local_cluster_lock = "/var/lock/pvecm.lock";
+               PVE::Tools::lock_file($local_cluster_lock, 10, \&PVE::Cluster::join, $param);
+
+               if (my $err = $@) {
+                   if (ref($err) eq 'PVE::APIClient::Exception' && defined($err->{code}) && $err->{code} == 501) {
+                       $err = "Remote side is not able to use API for Cluster join!\n" .
+                              "Pass the 'use_ssh' switch or update the remote side.\n";
+                   }
+                   die $err;
                }
-               die $err;
+               return; # all OK, the API join endpoint successfully set us up
            }
-           return; # all OK, the API join endpoint successfully set us up
-       }
 
-       # allow fallback to old ssh only join if wished or needed
+           # allow fallback to old ssh only join if wished or needed
 
-       PVE::Cluster::setup_sshd_config();
-       PVE::Cluster::setup_rootsshconfig();
-       PVE::Cluster::setup_ssh_keys();
+           PVE::Cluster::setup_sshd_config();
+           PVE::Cluster::setup_rootsshconfig();
+           PVE::Cluster::setup_ssh_keys();
 
-       # make sure known_hosts is on local filesystem
-       PVE::Cluster::ssh_unmerge_known_hosts();
+           # make sure known_hosts is on local filesystem
+           PVE::Cluster::ssh_unmerge_known_hosts();
 
-       my $cmd = ['ssh-copy-id', '-i', '/root/.ssh/id_rsa', "root\@$host"];
-       run_command($cmd, 'outfunc' => sub {}, 'errfunc' => sub {},
-                               'errmsg' => "unable to copy ssh ID");
+           my $cmd = ['ssh-copy-id', '-i', '/root/.ssh/id_rsa', "root\@$host"];
+           run_command($cmd, 'outfunc' => sub {}, 'errfunc' => sub {},
+                                   'errmsg' => "unable to copy ssh ID");
 
-       $cmd = ['ssh', $host, '-o', 'BatchMode=yes',
-               'pvecm', 'addnode', $nodename, '--force', 1];
+           $cmd = ['ssh', $host, '-o', 'BatchMode=yes',
+                   'pvecm', 'addnode', $nodename, '--force', 1];
 
-       push @$cmd, '--nodeid', $param->{nodeid} if $param->{nodeid};
-       push @$cmd, '--votes', $param->{votes} if defined($param->{votes});
-       push @$cmd, '--ring0_addr', $param->{ring0_addr} if defined($param->{ring0_addr});
-       push @$cmd, '--ring1_addr', $param->{ring1_addr} if defined($param->{ring1_addr});
+           push @$cmd, '--nodeid', $param->{nodeid} if $param->{nodeid};
+           push @$cmd, '--votes', $param->{votes} if defined($param->{votes});
+           push @$cmd, '--ring0_addr', $param->{ring0_addr} if defined($param->{ring0_addr});
+           push @$cmd, '--ring1_addr', $param->{ring1_addr} if defined($param->{ring1_addr});
 
-       if (system (@$cmd) != 0) {
-           my $cmdtxt = join (' ', @$cmd);
-           die "unable to add node: command failed ($cmdtxt)\n";
-       }
+           if (system (@$cmd) != 0) {
+               my $cmdtxt = join (' ', @$cmd);
+               die "unable to add node: command failed ($cmdtxt)\n";
+           }
+
+           my $tmpdir = "$libdir/.pvecm_add.tmp.$$";
+           mkdir $tmpdir;
+
+           eval {
+               print "copy corosync auth key\n";
+               $cmd = ['rsync', '--rsh=ssh -l root -o BatchMode=yes', '-lpgoq',
+                       "[$host]:$authfile $clusterconf", $tmpdir];
 
-       my $tmpdir = "$libdir/.pvecm_add.tmp.$$";
-       mkdir $tmpdir;
+               system(@$cmd) == 0 || die "can't rsync data from host '$host'\n";
 
-       eval {
-           print "copy corosync auth key\n";
-           $cmd = ['rsync', '--rsh=ssh -l root -o BatchMode=yes', '-lpgoq',
-                   "[$host]:$authfile $clusterconf", $tmpdir];
+               my $corosync_conf = PVE::Tools::file_get_contents("$tmpdir/corosync.conf");
+               my $corosync_authkey = PVE::Tools::file_get_contents("$tmpdir/authkey");
 
-           system(@$cmd) == 0 || die "can't rsync data from host '$host'\n";
+               PVE::Cluster::finish_join($host, $corosync_conf, $corosync_authkey);
+           };
+           my $err = $@;
 
-           my $corosync_conf = PVE::Tools::file_get_contents("$tmpdir/corosync.conf");
-           my $corosync_authkey = PVE::Tools::file_get_contents("$tmpdir/authkey");
+           rmtree $tmpdir;
 
-           PVE::Cluster::finish_join($host, $corosync_conf, $corosync_authkey);
+           die $err if $err;
        };
-       my $err = $@;
 
-       rmtree $tmpdir;
+       # use a synced worker so we get a nice task log when joining through CLI
+       my $rpcenv = PVE::RPCEnvironment::get();
+       my $authuser = $rpcenv->get_user();
 
-       die $err if $err;
+       $rpcenv->fork_worker('clusterjoin', '',  $authuser, $worker);
 
        return undef;
     }});