]> 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 ef8fac02690d761ef17de6b4dc4fa5c9b8bc550b..746bcbec60f5a1336da5732ba69fab3d0609ad55 100755 (executable)
@@ -3,13 +3,12 @@ package PVE::CLI::pvecm;
 use strict;
 use warnings;
 
-use Net::IP;
 use File::Path;
 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;
@@ -64,158 +63,6 @@ __PACKAGE__->register_method ({
        return undef;
     }});
 
-__PACKAGE__->register_method ({
-    name => 'create',
-    path => 'create',
-    method => 'PUT',
-    description => "Generate new cluster configuration.",
-    parameters => {
-       additionalProperties => 0,
-       properties => {
-           clustername => {
-               description => "The name of the cluster.",
-               type => 'string', format => 'pve-node',
-               maxLength => 15,
-           },
-           nodeid => {
-               type => 'integer',
-               description => "Node id for this node.",
-               minimum => 1,
-               optional => 1,
-           },
-           votes => {
-               type => 'integer',
-               description => "Number of votes for this node.",
-               minimum => 1,
-               optional => 1,
-           },
-           bindnet0_addr => {
-               type => 'string', format => 'ip',
-               description => "This specifies the network address the corosync ring 0".
-                   " executive should bind to and defaults to the local IP address of the node.",
-               optional => 1,
-           },
-           ring0_addr => {
-               type => 'string', format => 'address',
-               description => "Hostname (or IP) of the corosync ring0 address of this node.".
-                   " Defaults to the hostname of the node.",
-               optional => 1,
-           },
-           bindnet1_addr => {
-               type => 'string', format => 'ip',
-               description => "This specifies the network address the corosync ring 1".
-                   " executive should bind to and is optional.",
-               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) = @_;
-
-       -f $clusterconf && die "cluster config '$clusterconf' already exists\n";
-
-       PVE::Cluster::setup_sshd_config(1);
-       PVE::Cluster::setup_rootsshconfig();
-       PVE::Cluster::setup_ssh_keys();
-
-       -f $authfile || __PACKAGE__->keygen({filename => $authfile});
-
-       -f $authfile || die "no authentication key available\n";
-
-       my $clustername = $param->{clustername};
-
-       $param->{nodeid} = 1 if !$param->{nodeid};
-
-       $param->{votes} = 1 if !defined($param->{votes});
-
-       my $nodename = PVE::INotify::nodename();
-
-       my $local_ip_address = PVE::Cluster::remote_node_ip($nodename);
-
-       $param->{bindnet0_addr} = $local_ip_address
-           if !defined($param->{bindnet0_addr});
-
-       $param->{ring0_addr} = $nodename if !defined($param->{ring0_addr});
-
-       die "Param bindnet1_addr and ring1_addr are dependend, use both or none!\n"
-           if (defined($param->{bindnet1_addr}) != defined($param->{ring1_addr}));
-
-       my $bind_is_ipv6 = Net::IP::ip_is_ipv6($param->{bindnet0_addr});
-
-       # use string as here-doc format distracts more
-       my $interfaces = "interface {\n    ringnumber: 0\n" .
-           "    bindnetaddr: $param->{bindnet0_addr}\n  }";
-
-       my $ring_addresses = "ring0_addr: $param->{ring0_addr}" ;
-
-       # allow use of multiple rings (rrp) at cluster creation time
-       if ($param->{bindnet1_addr}) {
-           die "IPv6 and IPv4 cannot be mixed, use one or the other!\n"
-               if Net::IP::ip_is_ipv6($param->{bindnet1_addr}) != $bind_is_ipv6;
-
-           $interfaces .= "\n  interface {\n    ringnumber: 1\n" .
-               "    bindnetaddr: $param->{bindnet1_addr}\n  }\n";
-
-           $interfaces .= "rrp_mode: passive\n"; # only passive is stable and tested
-
-           $ring_addresses .= "\n    ring1_addr: $param->{ring1_addr}";
-       }
-
-       # No, corosync cannot deduce this on its own
-       my $ipversion = $bind_is_ipv6 ? 'ipv6' : 'ipv4';
-
-       my $config = <<_EOD;
-totem {
-  version: 2
-  secauth: on
-  cluster_name: $clustername
-  config_version: 1
-  ip_version: $ipversion
-  $interfaces
-}
-
-nodelist {
-  node {
-    $ring_addresses
-    name: $nodename
-    nodeid: $param->{nodeid}
-    quorum_votes: $param->{votes}
-  }
-}
-
-quorum {
-  provider: corosync_votequorum
-}
-
-logging {
-  to_syslog: yes
-  debug: off
-}
-_EOD
-;
-       PVE::Tools::file_set_contents($clusterconf, $config);
-
-       PVE::Cluster::ssh_merge_keys();
-
-       PVE::Cluster::gen_pve_node_files($nodename, $local_ip_address);
-
-       PVE::Cluster::ssh_merge_known_hosts($nodename, $local_ip_address, 1);
-
-       run_command('systemctl restart pve-cluster'); # restart
-
-       run_command('systemctl restart corosync'); # restart
-
-       return undef;
-}});
-
 __PACKAGE__->register_method ({
     name => 'add',
     path => 'add',
@@ -228,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",
@@ -245,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' => {
@@ -278,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 {
+
+           if (!$param->{use_ssh}) {
+               print "Please enter superuser (root) password for '$host':\n";
+               my $password = PVE::PTY::read_password("Password for root\@$host: ");
 
-           delete $param->{use_ssh};
-           $param->{password} = $password;
+               delete $param->{use_ssh};
+               $param->{password} = $password;
 
-           eval { PVE::Cluster::join($param) };
+               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' && $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";
+               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;
     }});
@@ -557,7 +399,7 @@ __PACKAGE__->register_method ({
 
 our $cmddef = {
     keygen => [ __PACKAGE__, 'keygen', ['filename']],
-    create => [ __PACKAGE__, 'create', ['clustername']],
+    create => [ 'PVE::API2::ClusterConfig', 'create', ['clustername']],
     add => [ __PACKAGE__, 'add', ['hostname']],
     addnode => [ 'PVE::API2::ClusterConfig', 'addnode', ['node']],
     delnode => [ 'PVE::API2::ClusterConfig', 'delnode', ['node']],