use PVE::INotify;
use PVE::CLIHandler;
+use PMG::Utils;
use PMG::Ticket;
use PMG::RESTEnvironment;
use PMG::DBTools;
$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}) {
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',
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;
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;
}});
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;