]> git.proxmox.com Git - pmg-api.git/blobdiff - PMG/CLI/pmgcm.pm
rewrite config log followup: move common log message out in closure
[pmg-api.git] / PMG / CLI / pmgcm.pm
index 0100f22cdd4c627750368ecd1c4ad5210358b9bd..c41c24d5d012905f10d7fd9e2d24213058f76f5b 100644 (file)
@@ -12,6 +12,7 @@ use PVE::Tools qw(extract_param);
 use PVE::INotify;
 use PVE::CLIHandler;
 
+use PMG::Utils;
 use PMG::Ticket;
 use PMG::RESTEnvironment;
 use PMG::DBTools;
@@ -56,20 +57,7 @@ my $format_nodelist = sub {
            $state = "ERROR: $err";
        }
 
-       my $uptime = '-';
-       if (my $ut = $ni->{uptime}) {
-           my $days = int($ut/86400);
-           $ut -= $days*86400;
-           my $hours = int($ut/3600);
-           $ut -= $hours*3600;
-           my $mins = $ut/60;
-           if ($days) {
-               my $ds = $days > 1 ? 'days' : 'day';
-               $uptime = sprintf "%d $ds %02d:%02d", $days, $hours, $mins;
-           } else {
-               $uptime = sprintf "%02d:%02d", $hours, $mins;
-           }
-       }
+       my $uptime = $ni->{uptime} ? PMG::Utils::format_uptime($ni->{uptime}) : '-';
 
        my $loadavg1 = '-';
        if (my $d = $ni->{loadavg}) {
@@ -120,6 +108,52 @@ __PACKAGE__->register_method({
        return undef;
     }});
 
+__PACKAGE__->register_method({
+    name => 'delete',
+    path => 'delete',
+    method => 'GET',
+    description => "Remove a node from the cluster.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           cid => {
+               description => "Cluster Node ID.",
+               type => 'integer',
+               minimum => 1,
+           },
+       },
+    },
+    returns => { type => 'null' },
+    code => sub {
+       my ($param) = @_;
+
+       my $code = sub {
+           my $cinfo = PMG::ClusterConfig->new();
+
+           die "no cluster defined\n" if !scalar(keys %{$cinfo->{ids}});
+
+           my $master = $cinfo->{master} || die "unable to lookup master node\n";
+
+           die "operation not permitted (not master)\n"
+               if $cinfo->{local}->{cid} != $master->{cid};
+
+           my $cid = $param->{cid};
+
+           die "unable to delete master node\n"
+               if $cinfo->{local}->{cid} == $cid;
+
+           die "no such node (cid == $cid does not exists)\n" if !$cinfo->{ids}->{$cid};
+
+           delete $cinfo->{ids}->{$cid};
+
+           $cinfo->write();
+       };
+
+       PMG::ClusterConfig::lock_config($code, "delete cluster node failed");
+
+       return undef;
+    }});
+
 __PACKAGE__->register_method({
     name => 'join',
     path => 'join',
@@ -147,7 +181,7 @@ __PACKAGE__->register_method({
        my $code = sub {
            my $cinfo = PMG::ClusterConfig->new();
 
-           die "cluster alreayd defined\n" if scalar(keys %{$cinfo->{ids}});
+           die "cluster already defined\n" if scalar(keys %{$cinfo->{ids}});
 
            my $term = new Term::ReadLine ('pmgcm');
            my $attribs = $term->Attribs;
@@ -218,10 +252,49 @@ __PACKAGE__->register_method({
        PMG::Cluster::sync_config_from_master($master_name, $master_ip);
 
        my $cfg = PMG::Config->new();
-       my $ruledb = PMG::RuleDB->new();
-       my $rulecache = PMG::RuleCache->new($ruledb);
 
-       $cfg->rewrite_config($rulecache, 1);
+       $cfg->rewrite_config(undef, 1);
+
+       return undef;
+    }});
+
+__PACKAGE__->register_method({
+    name => 'promote',
+    path => 'promote',
+    method => 'POST',
+    description => "Promote current node to become the new master.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {},
+    },
+    returns => { type => 'null' },
+    code => sub {
+       my ($param) = @_;
+
+       my $code = sub {
+           my $cinfo = PMG::ClusterConfig->new();
+
+           die "no cluster defined\n" if !scalar(keys %{$cinfo->{ids}});
+
+           my $master = $cinfo->{master} || die "unable to lookup master node\n";
+
+           die "this node is already master\n"
+               if $cinfo->{local}->{cid} == $master->{cid};
+
+           my $maxcid = $master->{maxcid};
+           $master->{type} = 'node';
+
+           my $newmaster = $cinfo->{local};
+
+           $newmaster->{maxcid} = $maxcid;
+           $newmaster->{type} = 'master';
+
+           $cinfo->{master} = $newmaster;
+
+           $cinfo->write();
+       };
+
+       PMG::ClusterConfig::lock_config($code, "promote new master failed");
 
        return undef;
     }});
@@ -229,9 +302,11 @@ __PACKAGE__->register_method({
 our $cmddef = {
     status => [ 'PMG::API2::Cluster', 'status', [], {}, $format_nodelist],
     create => [ 'PMG::API2::Cluster', 'create', [], {}, $upid_exit],
+    delete => [ __PACKAGE__, 'delete', ['cid']],
     join => [ __PACKAGE__, 'join', ['master_ip']],
     join_cmd => [ __PACKAGE__, 'join_cmd', []],
     sync => [ __PACKAGE__, 'sync', []],
+    promote => [ __PACKAGE__, 'promote', []],
 };
 
 1;