]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Enable cinder-manage to remove services
authorKendall Nelson <kjnelson@us.ibm.com>
Mon, 20 Jul 2015 20:06:21 +0000 (15:06 -0500)
committerKendall Nelson <kjnelson@us.ibm.com>
Fri, 14 Aug 2015 14:37:11 +0000 (09:37 -0500)
These changes are to enable cinder-manage to remove old services
from the database like nova does. All that is being added is the remove
method to manage.py and tests to cover this new method in test_cmd.py.
The command will remove the binary from the list of running services
in the db that can be viewed via "cinder-manage service list". NOTE:
Active and restarted services will re-populate the DB with their current
status automatically.

To use this new command, the user types "cinder-manage service remove
<binary> <host>" and the service will be removed.

DocImpact

Change-Id: I3bcec824d3c5f65fdd187459428025993a2010a0
Implements-blueprint: add-cinder-service-removal

cinder/cmd/manage.py
cinder/tests/unit/test_cmd.py

index 28690e5d9edda1485fe987f62ea2fb258471ecf3..ab1119885efef232e954ab7a77eec4ccc14e5dc0 100644 (file)
@@ -76,6 +76,7 @@ from cinder import context
 from cinder import db
 from cinder.db import migration as db_migration
 from cinder.db.sqlalchemy import api as db_api
+from cinder import exception
 from cinder.i18n import _
 from cinder import objects
 from cinder import rpc
@@ -455,6 +456,24 @@ class ServiceCommands(object):
                                   svc['availability_zone'], status, art,
                                   svc['updated_at']))
 
+    @args('binary', type=str,
+          help='Service to delete from the host.')
+    @args('host_name', type=str,
+          help='Host from which to remove the service.')
+    def remove(self, binary, host_name):
+        """Completely removes a service."""
+        ctxt = context.get_admin_context()
+        try:
+            svc = db.service_get_by_args(ctxt, host_name, binary)
+            db.service_destroy(ctxt, svc['id'])
+        except exception.HostBinaryNotFound as e:
+            print(_("Host not found. Failed to remove %(service)s"
+                    " on %(host)s.") %
+                  {'service': binary, 'host': host_name})
+            print (u"%s" % e.args)
+            return 2
+        print(_("Service %(service)s on host %(host)s removed.") %
+              {'service': binary, 'host': host_name})
 
 CATEGORIES = {
     'backup': BackupCommands,
index 25c04f9ecafc7affcb5fe87a3ce9d1fa14dbcf92..f1a8026b697abea14684ee2fdfe9709def56b1fb 100644 (file)
@@ -732,6 +732,21 @@ class TestCinderManageCmd(test.TestCase):
             self.assertFalse(log_setup.called)
             self.assertEqual(2, exit.code)
 
+    @mock.patch('cinder.db')
+    def test_remove_service_failure(self, mock_db):
+        mock_db.service_destroy.side_effect = SystemExit(1)
+        service_commands = cinder_manage.ServiceCommands()
+        exit = service_commands.remove('abinary', 'ahost')
+        self.assertEqual(2, exit)
+
+    @mock.patch('cinder.db.service_destroy')
+    @mock.patch('cinder.db.service_get_by_args',
+                return_value = {'id': 'volID'})
+    def test_remove_service_success(self, mock_get_by_args,
+                                    mock_service_destroy):
+        service_commands = cinder_manage.ServiceCommands()
+        self.assertIsNone(service_commands.remove('abinary', 'ahost'))
+
 
 class TestCinderRtstoolCmd(test.TestCase):