diff --git a/compass/actions/deploy.py b/compass/actions/deploy.py index e5fbe9d6..53179f56 100644 --- a/compass/actions/deploy.py +++ b/compass/actions/deploy.py @@ -17,6 +17,7 @@ import logging from compass.actions import util +from compass.db.api import cluster as cluster_db from compass.db.api import health_check_report as health_check_db from compass.db.api import user as user_db from compass.deployment.deploy_manager import DeployManager @@ -70,7 +71,7 @@ def deploy(cluster_id, hosts_id_list, username=None): ) -def redeploy(cluster_id, hosts_id_list, username=None): +def redeploy(cluster_id, username=None): """Deploy clusters. :param cluster_hosts: clusters and hosts in each cluster to deploy. @@ -86,6 +87,10 @@ def redeploy(cluster_id, hosts_id_list, username=None): adapter_info = util.ActionHelper.get_adapter_info( adapter_id, cluster_id, user) + + cluster_hosts = cluster_db.list_cluster_hosts(cluster_id, user) + hosts_id_list = [host['id'] for host in cluster_hosts] + hosts_info = util.ActionHelper.get_hosts_info( cluster_id, hosts_id_list, user) diff --git a/compass/api/api.py b/compass/api/api.py index 22e249b1..e2091835 100644 --- a/compass/api/api.py +++ b/compass/api/api.py @@ -2068,6 +2068,12 @@ def take_cluster_action(cluster_id): ), 202 ) + redeploy_cluster_func = _wrap_response( + functools.partial( + cluster_api.redeploy_cluster, cluster_id, user=current_user, + ), + 202 + ) patch_cluster_func = _wrap_response( functools.partial( cluster_api.patch_cluster, cluster_id, user=current_user, @@ -2090,6 +2096,7 @@ def take_cluster_action(cluster_id): remove_hosts=update_cluster_hosts_func, review=review_cluster_func, deploy=deploy_cluster_func, + redeploy=redeploy_cluster_func, apply_patch=patch_cluster_func, check_health=check_cluster_health_func ) diff --git a/compass/apiclient/restful.py b/compass/apiclient/restful.py index 27a02ead..bb82922c 100644 --- a/compass/apiclient/restful.py +++ b/compass/apiclient/restful.py @@ -634,6 +634,11 @@ class Client(object): data['deploy'] = deploy return self._post('/clusters/%s/action' % cluster_id, data=data) + def redeploy_cluster(self, cluster_id, deploy={}): + data = {} + data['redeploy'] = deploy + return self._post('/clusters/%s/action' % cluster_id, data=data) + def get_cluster_state(self, cluster_id): return self._get('/clusters/%s/state' % cluster_id) diff --git a/compass/db/api/cluster.py b/compass/db/api/cluster.py index 639c54c2..bb9805cd 100644 --- a/compass/db/api/cluster.py +++ b/compass/db/api/cluster.py @@ -1859,6 +1859,69 @@ def deploy_cluster( } +@utils.supported_filters(optional_support_keys=['redeploy']) +@database.run_in_session() +@user_api.check_user_permission( + permission.PERMISSION_DEPLOY_CLUSTER +) +@utils.wrap_to_dict( + RESP_DEPLOY_FIELDS, + cluster=RESP_CONFIG_FIELDS, + hosts=RESP_CLUSTERHOST_FIELDS +) +def redeploy_cluster( + cluster_id, deploy={}, user=None, session=None, **kwargs +): + """redeploy cluster. + + Args: + cluster_id: cluster id. + """ + from compass.db.api import host as host_api + from compass.tasks import client as celery_client + cluster = _get_cluster(cluster_id, session=session) + + check_cluster_editable(cluster, user=user) + check_cluster_validated(cluster) + utils.update_db_object( + session, cluster.state, + state='INITIALIZED', + percentage=0, + ready=False + ) + for clusterhost in cluster.clusterhosts: + host = clusterhost.host + # ignore checking if underlying host is validated if + # the host is not editable. + host_api.check_host_validated(host) + utils.update_db_object( + session, host.state, + state='INITIALIZED', + percentage=0, + ready=False + ) + if cluster.flavor_name: + check_clusterhost_validated(clusterhost) + utils.update_db_object( + session, + clusterhost.state, + state='INITIALIZED', + percentage=0, + ready=False + ) + + celery_client.celery.send_task( + 'compass.tasks.redeploy_cluster', + ( + user.email, cluster_id + ) + ) + return { + 'status': 'redeploy action sent', + 'cluster': cluster + } + + @utils.supported_filters(optional_support_keys=['apply_patch']) @database.run_in_session() @user_api.check_user_permission( diff --git a/compass/deployment/deploy_manager.py b/compass/deployment/deploy_manager.py index c7c5fd3e..baf7cd69 100644 --- a/compass/deployment/deploy_manager.py +++ b/compass/deployment/deploy_manager.py @@ -136,8 +136,9 @@ class DeployManager(object): """Redeploy target system for the cluster without changing config.""" if not self.pk_installer: logging.info("Redeploy_target_system: No installer found!") + return - self.os_installer.redeploy() + self.pk_installer.deploy() logging.info("Start to redeploy target system.") def redeploy(self): diff --git a/compass/deployment/installers/config_manager.py b/compass/deployment/installers/config_manager.py index 498d46dd..597c3a6c 100644 --- a/compass/deployment/installers/config_manager.py +++ b/compass/deployment/installers/config_manager.py @@ -286,8 +286,8 @@ class HostInfo(object): class BaseConfigManager(object): def __init__(self, adapter_info={}, cluster_info={}, hosts_info={}): assert(adapter_info and isinstance(adapter_info, dict)) - assert(cluster_info and isinstance(adapter_info, dict)) - assert(hosts_info and isinstance(adapter_info, dict)) + assert(cluster_info and isinstance(cluster_info, dict)) + assert(hosts_info and isinstance(hosts_info, dict)) self.adapter_info = AdapterInfo(adapter_info) self.cluster_info = ClusterInfo(cluster_info) diff --git a/compass/deployment/installers/pk_installers/ansible_installer/ansible_installer.py b/compass/deployment/installers/pk_installers/ansible_installer/ansible_installer.py index 32838004..dadfe1f2 100644 --- a/compass/deployment/installers/pk_installers/ansible_installer/ansible_installer.py +++ b/compass/deployment/installers/pk_installers/ansible_installer/ansible_installer.py @@ -258,6 +258,9 @@ class AnsibleInstaller(PKInstaller): return tmpl.respond() def _create_ansible_run_env(self, env_name, ansible_run_destination): + if os.path.exists(ansible_run_destination): + shutil.rmtree(ansible_run_destination, True) + os.mkdir(ansible_run_destination) # copy roles to run env diff --git a/compass/tasks/tasks.py b/compass/tasks/tasks.py index a57edbf9..f649afd7 100644 --- a/compass/tasks/tasks.py +++ b/compass/tasks/tasks.py @@ -113,6 +113,19 @@ def deploy_cluster(deployer_email, cluster_id, clusterhost_ids): logging.exception(error) +@celery.task(name='compass.tasks.redeploy_cluster') +def redeploy_cluster(deployer_email, cluster_id): + """Redeploy the given cluster. + + :param cluster_id: id of the cluster + :type cluster_id: int + """ + try: + deploy.redeploy(cluster_id, deployer_email) + except Exception as error: + logging.exception(error) + + @celery.task(name='compass.tasks.patch_cluster') def patch_cluster(patcher_email, cluster_id): """Patch the existing cluster.