]> git.proxmox.com Git - pve-manager.git/commitdiff
api: ceph: add cmd-safety endpoint
authorAaron Lauterer <a.lauterer@proxmox.com>
Thu, 17 Nov 2022 14:10:00 +0000 (15:10 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 17 Nov 2022 17:27:48 +0000 (18:27 +0100)
Ceph provides us with several safety checks to verify that an action is
safe to perform. This endpoint provides means to acces them.
The actual mon commands are not exposed directly. Instead the two
actions "stop" and "destroy" are offered.

In case it is not okay to perform an action, Ceph provides a status
message explaining why. This message is part of the returned values.

For now there are the following checks for these services:

MON:
  - ok-to-stop
  - ok-to-rm
OSD:
  - ok-to-stop
  - safe-to-destroy
MDS:
  - ok-to-stop

Even though OSDs have a check if it is okay to destroy them, it is for
now not really usable in our workflow because it needs the OSD to be up
and running to return useful information. Our workflow in the GUI
currently is that the OSD needs to be stopped in order to destroy it.

Signed-off-by: Aaron Lauterer <a.lauterer@proxmox.com>
PVE/API2/Ceph.pm

index 3bbcfe4c792618e864aff5f30dff13df923050d3..f344240877856f6c041c2789c81c34a0cc44572a 100644 (file)
@@ -641,4 +641,100 @@ __PACKAGE__->register_method ({
        return $res;
     }});
 
+__PACKAGE__->register_method ({
+    name => 'cmd_safety',
+    path => 'cmd-safety',
+    method => 'GET',
+    description => "Heuristical check if it is safe to perform an action.",
+    proxyto => 'node',
+    protected => 1,
+    permissions => {
+       check => ['perm', '/', [ 'Sys.audit' ]],
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           service => {
+               description => 'Service type',
+               type => 'string',
+               enum => ['osd', 'mon', 'mds'],
+           },
+           id => {
+               description => 'ID of the service',
+               type => 'string',
+           },
+           action => {
+               description => 'Action to check',
+               type => 'string',
+               enum => ['stop', 'destroy'],
+           },
+       },
+    },
+    returns => {
+       type => 'object',
+       properties => {
+          safe  => {
+               type => 'boolean',
+               description => 'If it is safe to run the command.',
+           },
+           status => {
+               type => 'string',
+               optional => 1,
+               description => 'Status message given by Ceph.'
+           },
+       },
+    },
+    code => sub {
+       my ($param) = @_;
+
+       PVE::Ceph::Tools::check_ceph_inited();
+
+       my $id = $param->{id};
+       my $service = $param->{service};
+       my $action = $param->{action};
+
+       my $rados = PVE::RADOS->new();
+
+       my $supported_actions = {
+           osd => {
+               stop => 'ok-to-stop',
+               destroy => 'safe-to-destroy',
+           },
+           mon => {
+               stop => 'ok-to-stop',
+               destroy => 'ok-to-rm',
+           },
+           mds => {
+               stop => 'ok-to-stop',
+           },
+       };
+
+       die "Service does not support this action: ${service}: ${action}\n"
+           if !$supported_actions->{$service}->{$action};
+
+       my $result = {
+           safe => 0,
+           status => '',
+       };
+
+       my $params = {
+           prefix => "${service} $supported_actions->{$service}->{$action}",
+           format => 'plain',
+       };
+       if ($service eq 'mon' && $action eq 'destroy') {
+           $params->{id} = $id;
+       } else {
+           $params->{ids} = [ $id ];
+       }
+
+       $result = $rados->mon_cmd($params, 1);
+       die $@ if $@;
+
+       $result->{safe} = $result->{return_code} == 0 ? 1 : 0;
+       $result->{status} = $result->{status_message};
+
+       return $result;
+    }});
+
 1;