From 6b1d98a9c29e8db1360d28cbe5e289fb503eccdd Mon Sep 17 00:00:00 2001 From: Kendall Nelson Date: Mon, 20 Jul 2015 15:06:21 -0500 Subject: [PATCH] Enable cinder-manage to remove services 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 " and the service will be removed. DocImpact Change-Id: I3bcec824d3c5f65fdd187459428025993a2010a0 Implements-blueprint: add-cinder-service-removal --- cinder/cmd/manage.py | 19 +++++++++++++++++++ cinder/tests/unit/test_cmd.py | 15 +++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/cinder/cmd/manage.py b/cinder/cmd/manage.py index 28690e5d9..ab1119885 100644 --- a/cinder/cmd/manage.py +++ b/cinder/cmd/manage.py @@ -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, diff --git a/cinder/tests/unit/test_cmd.py b/cinder/tests/unit/test_cmd.py index 25c04f9ec..f1a8026b6 100644 --- a/cinder/tests/unit/test_cmd.py +++ b/cinder/tests/unit/test_cmd.py @@ -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): -- 2.45.2