From 3e2e0a2b26d6730f5e624ed5c3fbb4bec148d3d8 Mon Sep 17 00:00:00 2001 From: Haitao Yue Date: Wed, 10 Jan 2018 16:52:46 +0800 Subject: [PATCH] [CE-220] Fix host reset operation Can reset host in admin dashboard Add is_active function in DockerHost class Change-Id: Ief4c6ab28be5305ecf42f2d7b69cdb6a4fe84b30 Signed-off-by: Haitao Yue --- src/agent/docker/host.py | 3 +++ src/modules/host.py | 10 +++++----- src/resources/host_api.py | 9 +++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/agent/docker/host.py b/src/agent/docker/host.py index 6757611af..2d841a31d 100644 --- a/src/agent/docker/host.py +++ b/src/agent/docker/host.py @@ -41,6 +41,9 @@ def __init__(self, host_type): self.col = db["host"] self.host_type = host_type + def is_active(self, *args): + return check_daemon(args[1]) + def create(self, worker_api): """ Create a new docker host node diff --git a/src/modules/host.py b/src/modules/host.py index aa19adcc3..6257cf140 100644 --- a/src/modules/host.py +++ b/src/modules/host.py @@ -390,11 +390,11 @@ def reset(self, id): """ logger.debug("clean host with id = {}".format(id)) host = self.get_by_id(id) - if not host or ClusterModel.objects(host=host).count() > 0: + if not host or ClusterModel.objects(host=host).count() < 0: logger.warning("No find resettable host with id ={}".format(id)) return False - return self.host_agents[host.type].reset( - host_type=host.type, worker_api=host.worker_api) + host_type = host.type + return self.host_agents[host_type].reset(host_type, host.worker_api) def refresh_status(self, id): """ @@ -410,10 +410,10 @@ def refresh_status(self, id): if not self.host_agents[host.type]\ .refresh_status(host.worker_api): logger.warning("Host {} is inactive".format(id)) - self.db_set_by_id(id, status="inactive") + self.db_set_by_id(id, **{"status": "inactive"}) return False else: - self.db_set_by_id(id, status="active") + self.db_set_by_id(id, **{"status": "active"}) return True def is_active(self, host_id): diff --git a/src/resources/host_api.py b/src/resources/host_api.py index 960a3b831..26cd88765 100644 --- a/src/resources/host_api.py +++ b/src/resources/host_api.py @@ -19,6 +19,8 @@ request_debug from modules import host_handler +from modules.models import Cluster as ClusterModel +from modules.models import Host as HostModel from agent import detect_daemon_type logger = logging.getLogger(__name__) @@ -246,6 +248,13 @@ def host_actions(): elif action == "reset": if host_handler.reset(host_id): logger.debug("reset successfully") + try: + host_model = HostModel.objects.get(id=host_id) + clusters = ClusterModel.objects(host=host_model) + for cluster_item in clusters: + cluster_item.delete() + except Exception: + pass return make_ok_resp() else: error_msg = "Failed to reset the host."