]> git.proxmox.com Git - pve-cluster.git/blobdiff - data/PVE/CLI/pvecm.pm
error out when getting remote ip address fails
[pve-cluster.git] / data / PVE / CLI / pvecm.pm
index 7fadb7d47e89ecba5bce0957f00286810d74307c..f5d5422eac91da581d20bc5838cc68fb31898693 100755 (executable)
@@ -13,7 +13,6 @@ use PVE::Tools;
 use PVE::Cluster;
 use PVE::INotify;
 use PVE::JSONSchema;
-use PVE::RPCEnvironment;
 use PVE::CLIHandler;
 
 use base qw(PVE::CLIHandler);
@@ -34,12 +33,13 @@ sub backup_database {
     mkdir $backupdir;
     
     my $ctime = time();
-    my $cmd = "echo '.dump' |";
-    $cmd .= "sqlite3 '$dbfile' |";
-    $cmd .= "gzip - >'${backupdir}/config-${ctime}.sql.gz'";
+    my $cmd = [
+       ['echo', '.dump'],
+       ['sqlite3', $dbfile],
+       ['gzip', '-', \">${backupdir}/config-${ctime}.sql.gz"],
+    ];
 
-    system($cmd) == 0 ||
-       die "can't backup old database: $!\n";
+    PVE::Tools::run_command($cmd, 'errmsg' => "can't backup old database\n");
 
     # purge older backup
     my $maxfiles = 10;
@@ -392,7 +392,10 @@ __PACKAGE__->register_method ({
     parameters => {
        additionalProperties => 0,
        properties => {
-           node => PVE::JSONSchema::get_standard_option('pve-node'),
+           node => {
+               type => 'string',
+               description => "Hostname or IP of the corosync ring0 address of this node.",
+           },
        },
     },
     returns => { type => 'null' },
@@ -406,11 +409,32 @@ __PACKAGE__->register_method ({
 
        my $nodelist = corosync_nodelist($conf);
 
-       my $nd = delete $nodelist->{$param->{node}};
-       die "no such node '$param->{node}'\n" if !$nd;
-       
+       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};
+
        corosync_update_nodelist($conf, $nodelist);
 
+       PVE::Tools::run_command(['corosync-cfgtool','-k', $nodeid])
+           if defined($nodeid);
+
        return undef;
     }});
 
@@ -493,9 +517,9 @@ __PACKAGE__->register_method ({
        # 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' >/dev/null 2>&1";
-       system ($cmd) == 0 ||
-           die "unable to copy ssh ID\n";
+       my $cmd = ['ssh-copy-id', '-i', '/root/.ssh/id_rsa', "root\@$host"];
+       PVE::Tools::run_command($cmd, 'outfunc' => sub {}, 'errfunc' => sub {},
+                               'errmsg' => "unable to copy ssh ID");
 
        $cmd = ['ssh', $host, '-o', 'BatchMode=yes',
                'pvecm', 'addnode', $nodename, '--force', 1];
@@ -601,6 +625,8 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
+       PVE::Cluster::check_corosync_conf_exists();
+
        my $cmd = ['corosync-quorumtool', '-siH'];
 
        exec (@$cmd);
@@ -622,6 +648,8 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
+       PVE::Cluster::check_corosync_conf_exists();
+
        my $cmd = ['corosync-quorumtool', '-l'];
 
        exec (@$cmd);
@@ -649,6 +677,8 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
+       PVE::Cluster::check_corosync_conf_exists();
+
        my $cmd = ['corosync-quorumtool', '-e', $param->{expected}];
 
        exec (@$cmd);
@@ -804,6 +834,59 @@ __PACKAGE__->register_method ({
        return undef;
     }});
 
+__PACKAGE__->register_method ({
+    name => 'mtunnel',
+    path => 'mtunnel',
+    method => 'POST',
+    description => "Used by VM/CT migration - do not use manually.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           get_migration_ip => {
+               type => 'boolean',
+               default => 0,
+               description => 'return the migration IP, if configured',
+               optional => 1,
+           },
+           migration_network => {
+               type => 'string',
+               format => 'CIDR',
+               description => 'the migration network used to detect the local migration IP',
+               optional => 1,
+           },
+       },
+    },
+    returns => { type => 'null'},
+    code => sub {
+       my ($param) = @_;
+
+       if (!PVE::Cluster::check_cfs_quorum(1)) {
+           print "no quorum\n";
+           return undef;
+       }
+
+       if ($param->{get_migration_ip}) {
+           my $network = $param->{migration_network};
+           if (my $ip = PVE::Cluster::get_local_migration_ip($network)) {
+               print "ip: '$ip'\n";
+           } else {
+               print "no ip\n";
+           }
+           # do not keep tunnel open when asked for migration ip
+           return undef;
+       }
+
+       print "tunnel online\n";
+       *STDOUT->flush();
+
+       while (my $line = <>) {
+           chomp $line;
+           last if $line =~ m/^quit$/;
+       }
+
+       return undef;
+    }});
+
 
 our $cmddef = {
     keygen => [ __PACKAGE__, 'keygen', ['filename']],
@@ -815,26 +898,7 @@ our $cmddef = {
     nodes => [ __PACKAGE__, 'nodes' ],
     expected => [ __PACKAGE__, 'expected', ['expected']],
     updatecerts => [ __PACKAGE__, 'updatecerts', []],
+    mtunnel => [ __PACKAGE__, 'mtunnel', []],
 };
 
 1;
-
-__END__
-
-=head1 NAME
-
-pvecm - Proxmox VE cluster manager toolkit
-
-=head1 SYNOPSIS
-
-=include synopsis
-
-=head1 DESCRIPTION
-
-pvecm is a program to manage the cluster configuration. It can be used
-to create a new cluster, join nodes to a cluster, leave the cluster,
-get status information and do various other cluster related tasks.
-
-=include pve_copyright
-
-