예제 #1
0
def redeploy(cluster_id, hosts_id_list, username=None):
    """Deploy clusters.

    :param cluster_hosts: clusters and hosts in each cluster to deploy.
    :type cluster_hosts: dict of int or str to list of int or str
    """
    with util.lock('serialized_action') as lock:
        if not lock:
            raise Exception('failed to acquire lock to deploy')

        user = user_db.get_user_object(username)
        cluster_info = ActionHelper.get_cluster_info(cluster_id, user)
        adapter_id = cluster_info[const.ADAPTER_ID]

        adapter_info = ActionHelper.get_adapter_info(adapter_id,
                                                     cluster_id,
                                                     user)
        hosts_info = ActionHelper.get_hosts_info(cluster_id,
                                                 hosts_id_list,
                                                 user)

        deploy_manager = DeployManager(adapter_info, cluster_info, hosts_info)
        # deploy_manager.prepare_for_deploy()
        deploy_manager.redeploy()
        ActionHelper.update_state(cluster_id, hosts_id_list, user)
def cluster_installed(cluster_id, clusterhosts_ready, username=None):
    """Callback when cluster is installed.

    :param cluster_id: cluster id
    :param clusterhosts_ready: clusterhosts that should trigger ready.

    .. note::
        The function should be called out of database session.
    """
    with util.lock("serialized_action") as lock:
        if not lock:
            raise Exception("failed to acquire lock to " "do the post action after cluster installation")
        logging.info("package installed on cluster %s with clusterhosts ready %s", cluster_id, clusterhosts_ready)
        if username:
            user = user_db.get_user_object(username)
        else:
            user = None
        cluster_info = util.ActionHelper.get_cluster_info(cluster_id, user)
        adapter_id = cluster_info[const.ADAPTER_ID]

        adapter_info = util.ActionHelper.get_adapter_info(adapter_id, cluster_id, user)
        hosts_info = util.ActionHelper.get_hosts_info(cluster_id, clusterhosts_ready.keys(), user)

        deploy_manager = DeployManager(adapter_info, cluster_info, hosts_info)

        deploy_manager.cluster_installed()
        util.ActionHelper.cluster_ready(cluster_id, True, user)
        for host_id, clusterhost_ready in clusterhosts_ready.items():
            if clusterhost_ready:
                util.ActionHelper.cluster_host_ready(cluster_id, host_id, False, user)
예제 #3
0
def delete_host(
    host_id, cluster_id_list, username=None
):
    with util.lock('serialized_action', timeout=100) as lock:
        if not lock:
            raise Exception('failed to acquire lock to delete host')

        user = user_db.get_user_object(username)
        for cluster_id in cluster_id_list:
            cluster_info = util.ActionHelper.get_cluster_info(
                cluster_id, user)
            adapter_id = cluster_info[const.ADAPTER_ID]

            adapter_info = util.ActionHelper.get_adapter_info(
                adapter_id, cluster_id, user)
            hosts_info = util.ActionHelper.get_hosts_info(
                cluster_id, [host_id], user)

            deploy_manager = DeployManager(
                adapter_info, cluster_info, hosts_info)

            deploy_manager.remove_hosts(
                package_only=True,
                delete_cluster=False
            )

        util.ActionHelper.delete_host(
            host_id, user
        )
예제 #4
0
def health_check(cluster_id, report_uri, username):
    with util.lock('cluster_health_check') as lock:
        if not lock:
            raise Exception('failed to acquire lock to check health')

        user = user_db.get_user_object(username)
        cluster_info = util.ActionHelper.get_cluster_info(cluster_id, user)
        adapter_id = cluster_info[const.ADAPTER_ID]

        adapter_info = util.ActionHelper.get_adapter_info(
            adapter_id, cluster_id, user
        )

        deploy_manager = DeployManager(adapter_info, cluster_info, None)
        try:
            deploy_manager.check_cluster_health(report_uri)
        except Exception as exc:
            logging.error("health_check exception: ============= %s" % exc)
            data = {'state': 'error', 'error_message': str(exc), 'report': {}}
            reports = health_check_db.list_health_reports(
                cluster_id, user=user)
            if not reports:
                # Exception before executing command remotely for health check.
                # No reports names sending back yet. Create a report
                name = 'pre_remote_health_check'
                health_check_db.add_report_record(
                    cluster_id, name, user=user, **data
                )

            health_check_db.update_multi_reports(cluster_id, user=user, **data)
예제 #5
0
def delete_cluster_host(
    cluster_id, host_id,
    username=None, delete_underlying_host=False
):
    with util.lock('serialized_action', timeout=100) as lock:
        if not lock:
            raise Exception('failed to acquire lock to delete clusterhost')

        user = user_db.get_user_object(username)
        cluster_info = util.ActionHelper.get_cluster_info(cluster_id, user)
        adapter_id = cluster_info[const.ADAPTER_ID]

        adapter_info = util.ActionHelper.get_adapter_info(
            adapter_id, cluster_id, user)
        hosts_info = util.ActionHelper.get_hosts_info(
            cluster_id, [host_id], user)

        deploy_manager = DeployManager(adapter_info, cluster_info, hosts_info)
        logging.debug('Created deploy manager with %s %s %s'
                      % (adapter_info, cluster_info, hosts_info))

        deploy_manager.remove_hosts(
            package_only=not delete_underlying_host,
            delete_cluster=False
        )
        util.ActionHelper.delete_cluster_host(
            cluster_id, host_id, user,
            delete_underlying_host
        )
예제 #6
0
def deploy(cluster_id, hosts_id_list, username=None):
    """Deploy clusters.

    :param cluster_hosts: clusters and hosts in each cluster to deploy.
    :type cluster_hosts: dict of int or str to list of int or str

    .. note::
        The function should be called out of database session.
    """
    with util.lock('serialized_action', timeout=1000) as lock:
        if not lock:
            raise Exception('failed to acquire lock to deploy')

        user = user_db.get_user_object(username)

        cluster_info = ActionHelper.get_cluster_info(cluster_id, user)
        adapter_id = cluster_info[const.ADAPTER_ID]

        adapter_info = ActionHelper.get_adapter_info(adapter_id, cluster_id,
                                                     user)
        hosts_info = ActionHelper.get_hosts_info(cluster_id, hosts_id_list,
                                                 user)

        deploy_manager = DeployManager(adapter_info, cluster_info, hosts_info)
        #deploy_manager.prepare_for_deploy()
        logging.debug('Created deploy manager with %s %s %s'
                      % (adapter_info, cluster_info, hosts_info))

        deployed_config = deploy_manager.deploy()
        ActionHelper.save_deployed_config(deployed_config, user)
        ActionHelper.update_state(cluster_id, hosts_id_list, user)
예제 #7
0
def health_check(cluster_id, report_uri, username):
    with util.lock('cluster_health_check') as lock:
        if not lock:
            raise Exception('failed to acquire lock to check health')

        user = user_db.get_user_object(username)
        cluster_info = util.ActionHelper.get_cluster_info(cluster_id, user)
        adapter_id = cluster_info[const.ADAPTER_ID]

        adapter_info = util.ActionHelper.get_adapter_info(
            adapter_id, cluster_id, user
        )

        deploy_manager = DeployManager(adapter_info, cluster_info, None)
        try:
            deploy_manager.check_cluster_health(report_uri)
        except Exception as exc:
            logging.error("health_check exception: ============= %s" % exc)
            data = {'state': 'error', 'error_message': str(exc), 'report': {}}
            reports = health_check_db.list_health_reports(
                cluster_id, user=user)
            if not reports:
                # Exception before executing command remotely for health check.
                # No reports names sending back yet. Create a report
                name = 'pre_remote_health_check'
                health_check_db.add_report_record(
                    cluster_id, name, user=user, **data
                )

            health_check_db.update_multi_reports(cluster_id, user=user, **data)
예제 #8
0
def delete_host(host_id, cluster_id_list, username=None):
    """Delete host and all clusterhosts on it.

    :param host_id: id of the host.
    :type host_id: int

    .. note::
        The function should be called out of database session.
    """
    with util.lock('serialized_action', timeout=100) as lock:
        if not lock:
            raise Exception('failed to acquire lock to delete host')

        user = user_db.get_user_object(username)
        for cluster_id in cluster_id_list:
            cluster_info = util.ActionHelper.get_cluster_info(cluster_id, user)
            adapter_id = cluster_info[const.ADAPTER_ID]

            adapter_info = util.ActionHelper.get_adapter_info(
                adapter_id, cluster_id, user)
            hosts_info = util.ActionHelper.get_hosts_info(
                cluster_id, [host_id], user)

            deploy_manager = DeployManager(adapter_info, cluster_info,
                                           hosts_info)

            deploy_manager.remove_hosts(package_only=True,
                                        delete_cluster=False)

        util.ActionHelper.delete_host(host_id, user)
예제 #9
0
def os_installed(
    host_id, clusterhosts_ready, clusters_os_ready,
    username=None
):
    """Callback when os is installed.

    :param host_id: host that os is installed.
    :type host_id: integer

    .. note::
        The function should be called out of database session.
    """
    with util.lock('serialized_action') as lock:
        if not lock:
            raise Exception(
                'failed to acquire lock to '
                'do the post action after os installation'
            )
        logging.info(
            'os installed on host %s '
            'with cluster host ready %s cluster os ready %s',
            host_id, clusterhosts_ready, clusters_os_ready
        )
        if username:
            user = user_db.get_user_object(username)
        else:
            user = None
        os_installed_triggered = False
        for cluster_id, clusterhost_ready in clusterhosts_ready.items():
            if not clusterhost_ready and os_installed_triggered:
                continue

            cluster_info = util.ActionHelper.get_cluster_info(
                cluster_id, user)
            adapter_id = cluster_info[const.ADAPTER_ID]

            adapter_info = util.ActionHelper.get_adapter_info(
                adapter_id, cluster_id, user)
            hosts_info = util.ActionHelper.get_hosts_info(
                cluster_id, [host_id], user)

            deploy_manager = DeployManager(
                adapter_info, cluster_info, hosts_info)

            if not os_installed_triggered:
                deploy_manager.os_installed()
                util.ActionHelper.host_ready(host_id, True, user)
                os_installed_triggered = True

            if clusterhost_ready:
                #deploy_manager.cluster_os_installed()
                util.ActionHelper.cluster_host_ready(
                    cluster_id, host_id, False, user
                )


            if util.ActionHelper.is_cluster_os_ready(cluster_id, user):
                logging.info("deploy_manager begin cluster_os_installed")
                deploy_manager.cluster_os_installed()
예제 #10
0
def poll_switch(poller_email,
                ip_addr,
                credentials,
                req_obj='mac',
                oper="SCAN"):
    """Query switch and update switch machines.

    .. note::
       When polling switch succeeds, for each mac it got from polling switch,
       A Machine record associated with the switch is added to the database.

    :param ip_addr: switch ip address.
    :type ip_addr: str
    :param credentials: switch crednetials.
    :type credentials: dict
    :param req_obj: the object requested to query from switch.
    :type req_obj: str
    :param oper: the operation to query the switch.
    :type oper: str, should be one of ['SCAN', 'GET', 'SET']

    .. note::
       The function should be called out of database session scope.
    """
    poller = user_api.get_user_object(poller_email)
    ip_int = long(netaddr.IPAddress(ip_addr))
    with util.lock('poll switch %s' % ip_addr, timeout=120) as lock:
        if not lock:
            raise Exception('failed to acquire lock to poll switch %s' %
                            ip_addr)

        # TODO(grace): before repoll the switch, set the state to repolling.
        # and when the poll switch is timeout, set the state to error.
        # the frontend should only consider some main state like INTIALIZED,
        # ERROR and SUCCESSFUL, REPOLLING is as an intermediate state to
        # indicate the switch is in learning the mac of the machines connected
        # to it.
        logging.debug('poll switch: %s', ip_addr)
        switch_dict, machine_dicts = _poll_switch(ip_addr,
                                                  credentials,
                                                  req_obj=req_obj,
                                                  oper=oper)
        switches = switch_api.list_switches(ip_int=ip_int, user=poller)
        if not switches:
            logging.error('no switch found for %s', ip_addr)
            return

        for switch in switches:
            for machine_dict in machine_dicts:
                logging.info('add machine: %s', machine_dict)
                machine_dict['owner_id'] = poller.id
                switch_api.add_switch_machine(switch['id'],
                                              False,
                                              user=poller,
                                              **machine_dict)
                switch_api.update_switch(switch['id'],
                                         user=poller,
                                         **switch_dict)
예제 #11
0
def clean_os_installer(os_installer_name, os_installer_settings):
    with util.lock('serialized_action', timeout=100) as lock:
        if not lock:
            raise Exception('failed to acquire lock to clean os installer')

        if os_installer_name not in OS_INSTALLERS:
            logging.error('%s not found in os_installers', os_installer_name)

        os_installer = OS_INSTALLERS[os_installer_name](os_installer_settings)
        os_installer.clean()
예제 #12
0
def os_installed(host_id,
                 clusterhosts_ready,
                 clusters_os_ready,
                 username=None):
    """Callback when os is installed.

    :param host_id: host that os is installed.
    :type host_id: integer
    :param clusterhosts_ready: the clusterhosts that should trigger ready.
    :param clusters_os_ready: the cluster that should trigger os ready.

    .. note::
        The function should be called out of database session.
    """
    with util.lock('serialized_action') as lock:
        if not lock:
            raise Exception('failed to acquire lock to '
                            'do the post action after os installation')
        logging.info(
            'os installed on host %s '
            'with cluster host ready %s cluster os ready %s', host_id,
            clusterhosts_ready, clusters_os_ready)
        if username:
            user = user_db.get_user_object(username)
        else:
            user = None
        os_installed_triggered = False
        for cluster_id, clusterhost_ready in clusterhosts_ready.items():
            if not clusterhost_ready and os_installed_triggered:
                continue

            cluster_info = util.ActionHelper.get_cluster_info(cluster_id, user)
            adapter_id = cluster_info[const.ADAPTER_ID]

            adapter_info = util.ActionHelper.get_adapter_info(
                adapter_id, cluster_id, user)
            hosts_info = util.ActionHelper.get_hosts_info(
                cluster_id, [host_id], user)

            deploy_manager = DeployManager(adapter_info, cluster_info,
                                           hosts_info)

            if not os_installed_triggered:
                deploy_manager.os_installed()
                util.ActionHelper.host_ready(host_id, True, user)
                os_installed_triggered = True

            if clusterhost_ready:
                # deploy_manager.cluster_os_installed()
                util.ActionHelper.cluster_host_ready(cluster_id, host_id,
                                                     False, user)

            if util.ActionHelper.is_cluster_os_ready(cluster_id, user):
                logging.info("deploy_manager begin cluster_os_installed")
                deploy_manager.cluster_os_installed()
예제 #13
0
def delete_cluster(
    cluster_id, host_id_list,
    username=None, delete_underlying_host=False
):
    """Delete cluster.

    :param cluster_id: id of the cluster.
    :type cluster_id: int

    .. note::
        The function should be called out of database session.
    """
    with util.lock('serialized_action', timeout=100) as lock:
        if not lock:
            raise Exception('failed to acquire lock to delete cluster')

        user = user_db.get_user_object(username)

        for host_id in host_id_list:
            cluster_api.update_cluster_host_state(
                user, cluster_id, host_id, state='ERROR'
            )
        cluster_api.update_cluster_state(
            user, cluster_id, state='ERROR'
        )

        cluster_api.update_cluster(
            user, cluster_id, reinstall_distributed_system=True
        )
        for host_id in host_id_list:
            cluster_api.update_cluster_host(
                user, cluster_id, host_id, reinstall_os=True
            )

        cluster_info = util.ActionHelper.get_cluster_info(cluster_id, user)
        adapter_id = cluster_info[const.ADAPTER_ID]

        adapter_info = util.ActionHelper.get_adapter_info(
            adapter_id, cluster_id, user)
        hosts_info = util.ActionHelper.get_hosts_info(
            cluster_id, host_id_list, user)

        logging.debug('adapter info: %s', adapter_info)
        logging.debug('cluster info: %s', cluster_info)
        logging.debug('hosts info: %s', hosts_info)
        deploy_manager = DeployManager(adapter_info, cluster_info, hosts_info)

        deploy_manager.remove_hosts(
            package_only=not delete_underlying_host,
            delete_cluster=True
        )
        util.ActionHelper.delete_cluster(
            cluster_id, host_id_list, user,
            delete_underlying_host
        )
예제 #14
0
def poll_switch(poller_email, ip_addr, credentials,
                req_obj='mac', oper="SCAN"):
    """Query switch and update switch machines.

    .. note::
       When polling switch succeeds, for each mac it got from polling switch,
       A Machine record associated with the switch is added to the database.

    :param ip_addr: switch ip address.
    :type ip_addr: str
    :param credentials: switch crednetials.
    :type credentials: dict
    :param req_obj: the object requested to query from switch.
    :type req_obj: str
    :param oper: the operation to query the switch.
    :type oper: str, should be one of ['SCAN', 'GET', 'SET']

    .. note::
       The function should be called out of database session scope.
    """
    poller = user_api.get_user_object(poller_email)
    ip_int = long(netaddr.IPAddress(ip_addr))
    with util.lock('poll switch %s' % ip_addr, timeout=120) as lock:
        if not lock:
            raise Exception(
                'failed to acquire lock to poll switch %s' % ip_addr
            )

        # TODO(grace): before repoll the switch, set the state to repolling.
        # and when the poll switch is timeout, set the state to error.
        # the frontend should only consider some main state like INTIALIZED,
        # ERROR and SUCCESSFUL, REPOLLING is as an intermediate state to
        # indicate the switch is in learning the mac of the machines connected
        # to it.
        logging.debug('poll switch: %s', ip_addr)
        switch_dict, machine_dicts = _poll_switch(
            ip_addr, credentials, req_obj=req_obj, oper=oper
        )
        switches = switch_api.list_switches(ip_int=ip_int, user=poller)
        if not switches:
            logging.error('no switch found for %s', ip_addr)
            return

        for switch in switches:
            for machine_dict in machine_dicts:
                logging.debug('add machine: %s', machine_dict)
                switch_api.add_switch_machine(
                    switch['id'], False, user=poller, **machine_dict
                )
                switch_api.update_switch(
                    switch['id'],
                    user=poller,
                    **switch_dict
                )
예제 #15
0
def reinstall(cluster_hosts):
    """Reinstall clusters.

    :param cluster_hosts: clusters and hosts in each cluster to reinstall.
    :type cluster_hosts: dict of int or str to list of int or str

    .. note::
        The function should be called out of database session.
    """
    with util.lock('serialized_action') as lock:
        if not lock:
            raise Exception('failed to acquire lock to reinstall')
        logging.debug('reinstall cluster_hosts: %s', cluster_hosts)
예제 #16
0
def clean_package_installer(package_installer_name,
                            package_installer_settings):
    with util.lock('serialized_action', timeout=100) as lock:
        if not lock:
            raise Exception(
                'failed to acquire lock to clean package installer')

        if package_installer_name not in PK_INSTALLERS:
            logging.error('%s not found in os_installers',
                          package_installer_name)

        package_installer = PK_INSTALLERS[package_installer_name](
            package_installer_settings)
        package_installer.clean()
예제 #17
0
def reinstall(cluster_hosts):
    """Reinstall clusters.

    :param cluster_hosts: clusters and hosts in each cluster to reinstall.
    :type cluster_hosts: dict of int or str to list of int or str

    .. note::
        The function should be called out of database session.
    """
    with util.lock('serialized_action') as lock:
        if not lock:
            raise Exception(
                'failed to acquire lock to reinstall')
        logging.debug('reinstall cluster_hosts: %s', cluster_hosts)
예제 #18
0
def poll_switch(poller_email, ip_addr, credentials,
                req_obj='mac', oper="SCAN"):
    """Query switch and update switch machines.

    .. note::
       When polling switch succeeds, for each mac it got from polling switch,
       A Machine record associated with the switch is added to the database.

    :param ip_addr: switch ip address.
    :type ip_addr: str
    :param credentials: switch crednetials.
    :type credentials: dict
    :param req_obj: the object requested to query from switch.
    :type req_obj: str
    :param oper: the operation to query the switch.
    :type oper: str, should be one of ['SCAN', 'GET', 'SET']

    .. note::
       The function should be called out of database session scope.
    """
    poller = user_api.get_user_object(poller_email)
    ip_int = long(netaddr.IPAddress(ip_addr))
    with util.lock('poll switch %s' % ip_addr, timeout=120) as lock:
        if not lock:
            raise Exception(
                'failed to acquire lock to poll switch %s' % ip_addr
            )

        logging.debug('poll switch: %s', ip_addr)
        switch_dict, machine_dicts = _poll_switch(
            ip_addr, credentials, req_obj=req_obj, oper=oper
        )
        switches = switch_api.list_switches(ip_int=ip_int, user=poller)
        if not switches:
            logging.error('no switch found for %s', ip_addr)
            return

        for switch in switches:
            for machine_dict in machine_dicts:
                logging.debug('add machine: %s', machine_dict)
                switch_api.add_switch_machine(
                    switch['id'], False, user=poller, **machine_dict
                )
                switch_api.update_switch(
                    switch['id'],
                    user=poller,
                    **switch_dict
                )
예제 #19
0
def package_installed(
    cluster_id, host_id, cluster_ready,
    host_ready, username=None
):
    """Callback when package is installed.

    :param cluster_id: cluster id.
    :param host_id: host id.
    :param cluster_ready: if the cluster should trigger ready.
    :param host_ready: if the host should trigger ready.

    .. note::
        The function should be called out of database session.
    """
    with util.lock('serialized_action') as lock:
        if not lock:
            raise Exception(
                'failed to acquire lock to '
                'do the post action after package installation'
            )
        logging.info(
            'package installed on cluster %s host %s '
            'with cluster ready %s host ready %s',
            cluster_id, host_id, cluster_ready, host_ready
        )

        if username:
            user = user_db.get_user_object(username)
        else:
            user = None
        cluster_info = util.ActionHelper.get_cluster_info(cluster_id, user)
        adapter_id = cluster_info[const.ADAPTER_ID]

        adapter_info = util.ActionHelper.get_adapter_info(
            adapter_id, cluster_id, user)
        hosts_info = util.ActionHelper.get_hosts_info(
            cluster_id, [host_id], user)

        deploy_manager = DeployManager(adapter_info, cluster_info, hosts_info)

        deploy_manager.package_installed()
        util.ActionHelper.cluster_host_ready(cluster_id, host_id, True, user)
        if cluster_ready:
            util.ActionHelper.cluster_ready(cluster_id, False, user)
        if host_ready:
            util.ActionHelper.host_ready(host_id, False, user)
예제 #20
0
def deploy(cluster_id, hosts_id_list, username=None):
    """Deploy clusters.

    :param cluster_hosts: clusters and hosts in each cluster to deploy.
    :type cluster_hosts: dict of int or str to list of int or str

    .. note::
        The function should be called out of database session.
    """
    with util.lock('serialized_action', timeout=1000) as lock:
        if not lock:
            raise Exception('failed to acquire lock to deploy')

        user = user_db.get_user_object(username)

        cluster_info = util.ActionHelper.get_cluster_info(cluster_id, user)
        adapter_id = cluster_info[const.ADAPTER_ID]

        adapter_info = util.ActionHelper.get_adapter_info(
            adapter_id, cluster_id, user)
        hosts_info = util.ActionHelper.get_hosts_info(
            cluster_id, hosts_id_list, user)

        deploy_successful = True
        try:
            deploy_manager = DeployManager(
                adapter_info, cluster_info, hosts_info)
            # deploy_manager.prepare_for_deploy()
            logging.debug('Created deploy manager with %s %s %s'
                          % (adapter_info, cluster_info, hosts_info))
            deployed_config = deploy_manager.deploy()
        except Exception as error:
            logging.exception(error)
            deploy_successful = False

        if deploy_successful:
            util.ActionHelper.save_deployed_config(deployed_config, user)
            util.ActionHelper.update_state(
                cluster_id, hosts_id_list, user, state='INSTALLING'
            )
        else:
            util.ActionHelper.update_state(
                cluster_id, hosts_id_list, user, state='ERROR',
                message='failed to start deployment', severity='ERROR'
            )
예제 #21
0
def deploy(cluster_id, hosts_id_list, username=None):
    """Deploy clusters.

    :param cluster_hosts: clusters and hosts in each cluster to deploy.
    :type cluster_hosts: dict of int or str to list of int or str

    .. note::
        The function should be called out of database session.
    """
    with util.lock('serialized_action', timeout=1000) as lock:
        if not lock:
            raise Exception('failed to acquire lock to deploy')

        user = user_db.get_user_object(username)

        cluster_info = util.ActionHelper.get_cluster_info(cluster_id, user)
        adapter_id = cluster_info[const.ADAPTER_ID]

        adapter_info = util.ActionHelper.get_adapter_info(
            adapter_id, cluster_id, user)
        hosts_info = util.ActionHelper.get_hosts_info(
            cluster_id, hosts_id_list, user)

        deploy_successful = True
        try:
            deploy_manager = DeployManager(
                adapter_info, cluster_info, hosts_info)
            # deploy_manager.prepare_for_deploy()
            logging.debug('Created deploy manager with %s %s %s'
                          % (adapter_info, cluster_info, hosts_info))
            deployed_config = deploy_manager.deploy()
        except Exception as error:
            logging.exception(error)
            deploy_successful = False

        if deploy_successful:
            util.ActionHelper.save_deployed_config(deployed_config, user)
            util.ActionHelper.update_state(
                cluster_id, hosts_id_list, user, state='INSTALLING'
            )
        else:
            util.ActionHelper.update_state(
                cluster_id, hosts_id_list, user, state='ERROR',
                message='failed to start deployment', severity='ERROR'
            )
예제 #22
0
def clean_deployment(cluster_hosts):
    """Clean deployment of clusters.

    :param cluster_hosts: clusters and hosts in each cluster to clean.
    :type cluster_hosts: dict of int or str to list of int or str

    .. note::
        The function should be called out of database session.
    """
    with util.lock('serialized_action'):
        logging.debug('clean cluster_hosts: %s', cluster_hosts)
        with database.session():
            cluster_hosts, os_versions, target_systems = (
                util.update_cluster_hosts(cluster_hosts))
            manager = ConfigManager()
            manager.clean_cluster_and_hosts(
                cluster_hosts, os_versions, target_systems)
            manager.sync()
예제 #23
0
def clean_package_installer(
    package_installer_name, package_installer_settings
):
    with util.lock('serialized_action', timeout=100) as lock:
        if not lock:
            raise Exception(
                'failed to acquire lock to clean package installer'
            )

        if package_installer_name not in PK_INSTALLERS:
            logging.error(
                '%s not found in os_installers',
                package_installer_name
            )

        package_installer = PK_INSTALLERS[package_installer_name](
            package_installer_settings
        )
        package_installer.clean()
예제 #24
0
def patch(cluster_id, username=None):
    """Patch cluster.

    :param cluster_id: id of the cluster
    :type cluster_id: int

    .. note::
        The function should be called out of database session.
    """
    with util.lock('serialized_action', timeout=1000) as lock:
        if not lock:
            raise Exception('failed to acquire lock to deploy')

        user = user_db.get_user_object(username)
        cluster_hosts = cluster_db.list_cluster_hosts(cluster_id, user)
        hosts_id_list = [host['id'] for host in cluster_hosts]
        cluster_info = util.ActionHelper.get_cluster_info(cluster_id, user)
        adapter_id = cluster_info[const.ADAPTER_ID]

        adapter_info = util.ActionHelper.get_adapter_info(
            adapter_id, cluster_id, user)
        hosts_info = util.ActionHelper.get_hosts_info(
            cluster_id, hosts_id_list, user)
        patch_successful = True
        try:
            patcher = Patcher(
                adapter_info, cluster_info, hosts_info, cluster_hosts)
            patched_config = patcher.patch()
        except Exception as error:
            logging.exception(error)
            patch_successful = False

        if patch_successful:
            clean_payload = '{"patched_roles": []}'
            clean_payload = json.loads(clean_payload)
            for cluster_host in cluster_hosts:
                cluster_db.update_cluster_host(
                    cluster_id, cluster_host['id'], user, **clean_payload)
                logging.info(
                    "cleaning up patched roles for host id: %s",
                    cluster_host['id']
                )
            logging.info("Patch successful: %s", patched_config)
예제 #25
0
def clean_os_installer(
    os_installer_name, os_installer_settings
):
    with util.lock('serialized_action', timeout=100) as lock:
        if not lock:
            raise Exception(
                'failed to acquire lock to clean os installer'
            )

        if os_installer_name not in OS_INSTALLERS:
            logging.error(
                '%s not found in os_installers',
                os_installer_name
            )

        os_installer = OS_INSTALLERS[os_installer_name](
            os_installer_settings
        )
        os_installer.clean()
예제 #26
0
def patch(cluster_id, username=None):
    """Patch cluster.

    :param cluster_id: id of the cluster
    :type cluster_id: int

    .. note::
        The function should be called out of database session.
    """
    with util.lock('serialized_action', timeout=1000) as lock:
        if not lock:
            raise Exception('failed to acquire lock to deploy')

        user = user_db.get_user_object(username)
        cluster_hosts = cluster_db.list_cluster_hosts(cluster_id, user)
        hosts_id_list = [host['id'] for host in cluster_hosts]
        cluster_info = util.ActionHelper.get_cluster_info(cluster_id, user)
        adapter_id = cluster_info[const.ADAPTER_ID]

        adapter_info = util.ActionHelper.get_adapter_info(
            adapter_id, cluster_id, user)
        hosts_info = util.ActionHelper.get_hosts_info(cluster_id,
                                                      hosts_id_list, user)
        patch_successful = True
        try:
            patcher = Patcher(adapter_info, cluster_info, hosts_info,
                              cluster_hosts)
            patched_config = patcher.patch()
        except Exception as error:
            logging.exception(error)
            patch_successful = False

        if patch_successful:
            clean_payload = '{"patched_roles": []}'
            clean_payload = json.loads(clean_payload)
            for cluster_host in cluster_hosts:
                cluster_db.update_cluster_host(cluster_id, cluster_host['id'],
                                               user, **clean_payload)
                logging.info("cleaning up patched roles for host id: %s",
                             cluster_host['id'])
            logging.info("Patch successful: %s", patched_config)
예제 #27
0
def redeploy(cluster_id, username=None):
    """Deploy clusters.

    :param cluster_hosts: clusters and hosts in each cluster to deploy.
    :type cluster_hosts: dict of int or str to list of int or str
    """
    with util.lock('serialized_action') as lock:
        if not lock:
            raise Exception('failed to acquire lock to deploy')

        user = user_db.get_user_object(username)
        cluster_info = util.ActionHelper.get_cluster_info(cluster_id, user)
        adapter_id = cluster_info[const.ADAPTER_ID]

        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)

        deploy_successful = True
        try:
            deploy_manager = DeployManager(
                adapter_info, cluster_info, hosts_info)
            # deploy_manager.prepare_for_deploy()
            deploy_manager.redeploy()
        except Exception as error:
            logging.exception(error)
            deploy_successful = False
        if deploy_successful:
            util.ActionHelper.update_state(
                cluster_id, hosts_id_list, user, state='INSTALLING',
            )
        else:
            util.ActionHelper.update_state(
                cluster_id, hosts_id_list, user, state='ERROR',
                message='failed to start redeployment', severity='ERROR'
            )
예제 #28
0
def delete_cluster(
    cluster_id, host_id_list,
    username=None, delete_underlying_host=False
):
    """Delete cluster and all clusterhosts on it.

    :param cluster_id: id of the cluster.
    :type cluster_id: int
    :param host_id_list: list of host id.
    :type host_id_list: list of int.

    If delete_underlying_host is set, all underlying hosts will
    be deleted.

    .. note::
        The function should be called out of database session.
    """
    with util.lock('serialized_action', timeout=100) as lock:
        if not lock:
            raise Exception('failed to acquire lock to delete cluster')

        user = user_db.get_user_object(username)

        cluster_info = util.ActionHelper.get_cluster_info(cluster_id, user)
        adapter_id = cluster_info[const.ADAPTER_ID]

        adapter_info = util.ActionHelper.get_adapter_info(
            adapter_id, cluster_id, user)
        hosts_info = util.ActionHelper.get_hosts_info(
            cluster_id, host_id_list, user)

        deploy_manager = DeployManager(adapter_info, cluster_info, hosts_info)

        deploy_manager.remove_hosts(
            package_only=not delete_underlying_host,
            delete_cluster=True
        )
        util.ActionHelper.delete_cluster(
            cluster_id, host_id_list, user,
            delete_underlying_host
        )
예제 #29
0
def deploy(cluster_hosts):
    """Deploy clusters.

    :param cluster_hosts: clusters and hosts in each cluster to deploy.
    :type cluster_hosts: dict of int or str to list of int or str

    .. note::
        The function should be called out of database session.
    """
    with util.lock('serialized_action') as lock:
        if not lock:
            raise Exception('failed to acquire lock to deploy')

        logging.debug('deploy cluster_hosts: %s', cluster_hosts)
        with database.session():
            cluster_hosts, os_versions, target_systems = (
                util.update_cluster_hosts(cluster_hosts))
            manager = ConfigManager()
            manager.install_cluster_and_hosts(cluster_hosts, os_versions,
                                              target_systems)
            manager.sync()
예제 #30
0
def deploy(cluster_hosts):
    """Deploy clusters.

    :param cluster_hosts: clusters and hosts in each cluster to deploy.
    :type cluster_hosts: dict of int or str to list of int or str

    .. note::
        The function should be called out of database session.
    """
    with util.lock('serialized_action') as lock:
        if not lock:
            raise Exception('failed to acquire lock to deploy')

        logging.debug('deploy cluster_hosts: %s', cluster_hosts)
        with database.session():
            cluster_hosts, os_versions, target_systems = (
                util.update_cluster_hosts(cluster_hosts))
            manager = ConfigManager()
            manager.install_cluster_and_hosts(
                cluster_hosts, os_versions, target_systems)
            manager.sync()
예제 #31
0
def update_progress(cluster_hosts):
    """Update status and installing progress of the given cluster.

    :param cluster_hosts: clusters and hosts in each cluster to update.
    :type cluster_hosts: dict of int or str to list of int or str

    .. note::
       The function should be called out of the database session scope.
       In the function, it will update the database cluster_state and
       host_state table for the deploying cluster and hosts.

       The function will also query log_progressing_history table to get
       the lastest installing progress and the position of log it has
       processed in the last run. The function uses these information to
       avoid recalculate the progress from the beginning of the log file.
       After the progress got updated, these information will be stored back
       to the log_progressing_history for next time run.
    """
    with util.lock('log_progressing', blocking=False) as lock:
        if not lock:
            logging.error(
                'failed to acquire lock to calculate installation progress')
            return

        logging.info('update installing progress of cluster_hosts: %s',
                     cluster_hosts)
        os_versions = {}
        target_systems = {}
        with database.session():
            cluster_hosts, os_versions, target_systems = (
                util.update_cluster_hosts(
                    cluster_hosts, _cluster_filter, _host_filter))

        progress_calculator.update_progress(
            setting.OS_INSTALLER,
            os_versions,
            setting.PACKAGE_INSTALLER,
            target_systems,
            cluster_hosts)
예제 #32
0
def clean_installing_progress(cluster_hosts):
    """Clean installing progress of clusters.

    :param cluster_hosts: clusters and hosts in each cluster to clean.
    :type cluster_hosts: dict of int or str to list of int or str

    .. note::
        The function should be called out of database session.
    """
    with util.lock('serialized_action') as lock:
        if not lock:
            raise Exception(
                'failed to acquire lock to clean installation progress')

        logging.info('clean installing progress of cluster_hosts: %s',
                     cluster_hosts)
        with database.session():
            cluster_hosts, os_versions, target_systems = (
                util.update_cluster_hosts(cluster_hosts))
            manager = ConfigManager()
            manager.clean_cluster_and_hosts_installing_progress(
                cluster_hosts, os_versions, target_systems)
            manager.sync()
예제 #33
0
def delete_host(
    host_id, cluster_id_list, username=None
):
    """Delete host and all clusterhosts on it.

    :param host_id: id of the host.
    :type host_id: int

    .. note::
        The function should be called out of database session.
    """
    with util.lock('serialized_action', timeout=100) as lock:
        if not lock:
            raise Exception('failed to acquire lock to delete host')

        user = user_db.get_user_object(username)
        for cluster_id in cluster_id_list:
            cluster_info = util.ActionHelper.get_cluster_info(
                cluster_id, user)
            adapter_id = cluster_info[const.ADAPTER_ID]

            adapter_info = util.ActionHelper.get_adapter_info(
                adapter_id, cluster_id, user)
            hosts_info = util.ActionHelper.get_hosts_info(
                cluster_id, [host_id], user)

            deploy_manager = DeployManager(
                adapter_info, cluster_info, hosts_info)

            deploy_manager.remove_hosts(
                package_only=True,
                delete_cluster=False
            )

        util.ActionHelper.delete_host(
            host_id, user
        )
예제 #34
0
def delete_cluster(cluster_id,
                   host_id_list,
                   username=None,
                   delete_underlying_host=False):
    """Delete cluster and all clusterhosts on it.

    :param cluster_id: id of the cluster.
    :type cluster_id: int
    :param host_id_list: list of host id.
    :type host_id_list: list of int.

    If delete_underlying_host is set, all underlying hosts will
    be deleted.

    .. note::
        The function should be called out of database session.
    """
    with util.lock('serialized_action', timeout=100) as lock:
        if not lock:
            raise Exception('failed to acquire lock to delete cluster')

        user = user_db.get_user_object(username)

        cluster_info = util.ActionHelper.get_cluster_info(cluster_id, user)
        adapter_id = cluster_info[const.ADAPTER_ID]

        adapter_info = util.ActionHelper.get_adapter_info(
            adapter_id, cluster_id, user)
        hosts_info = util.ActionHelper.get_hosts_info(cluster_id, host_id_list,
                                                      user)

        deploy_manager = DeployManager(adapter_info, cluster_info, hosts_info)

        deploy_manager.remove_hosts(package_only=not delete_underlying_host,
                                    delete_cluster=True)
        util.ActionHelper.delete_cluster(cluster_id, host_id_list, user,
                                         delete_underlying_host)
def clean_installing_progress(cluster_hosts):
    """Clean installing progress of clusters.

    :param cluster_hosts: clusters and hosts in each cluster to clean.
    :type cluster_hosts: dict of int or str to list of int or str

    .. note::
        The function should be called out of database session.
    """
    with util.lock('serialized_action') as lock:
        if not lock:
            raise Exception(
                'failed to acquire lock to clean installation progress')

        logging.info(
            'clean installing progress of cluster_hosts: %s',
            cluster_hosts)
        with database.session():
            cluster_hosts, os_versions, target_systems = (
                util.update_cluster_hosts(cluster_hosts))
            manager = ConfigManager()
            manager.clean_cluster_and_hosts_installing_progress(
                cluster_hosts, os_versions, target_systems)
            manager.sync()
예제 #36
0
def update_progress():
    """Update status and installing progress of the given cluster.

    :param cluster_hosts: clusters and hosts in each cluster to update.
    :type cluster_hosts: dict of int or str to list of int or str

    .. note::
       The function should be called out of the database session scope.
       In the function, it will update the database cluster_state and
       host_state table for the deploying cluster and hosts.

       The function will also query log_progressing_history table to get
       the lastest installing progress and the position of log it has
       processed in the last run. The function uses these information to
       avoid recalculate the progress from the beginning of the log file.
       After the progress got updated, these information will be stored back
       to the log_progressing_history for next time run.
    """
    with util.lock('log_progressing', timeout=60, blocking=False) as lock:
        if not lock:
            logging.error(
                'failed to acquire lock to calculate installation progress'
            )
            return

        logging.info('update installing progress')

        user = user_api.get_user_object(setting.COMPASS_ADMIN_EMAIL)
        hosts = host_api.list_hosts(user=user)
        host_mapping = {}
        for host in hosts:
            if 'id' not in host:
                logging.error('id is not in host %s', host)
                continue
            host_id = host['id']
            if 'os_name' not in host:
                logging.error('os_name is not in host %s', host)
                continue
            if 'os_installer' not in host:
                logging.error('os_installer is not in host %s', host)
                continue
            host_dirname = setting.HOST_INSTALLATION_LOGDIR_NAME
            if host_dirname not in host:
                logging.error(
                    '%s is not in host %s', host_dirname, host
                )
                continue
            host_state = host_api.get_host_state(host_id, user=user)
            if 'state' not in host_state:
                logging.error('state is not in host state %s', host_state)
                continue
            if host_state['state'] == 'INSTALLING':
                host_log_histories = host_api.get_host_log_histories(
                    host_id, user=user
                )
                host_log_history_mapping = {}
                for host_log_history in host_log_histories:
                    if 'filename' not in host_log_history:
                        logging.error(
                            'filename is not in host log history %s',
                            host_log_history
                        )
                        continue
                    host_log_history_mapping[
                        host_log_history['filename']
                    ] = host_log_history
                host_mapping[host_id] = (
                    host, host_state, host_log_history_mapping
                )
            else:
                logging.info(
                    'ignore host state %s since it is not in installing',
                    host_state
                )
        adapters = adapter_api.list_adapters(user=user)
        adapter_mapping = {}
        for adapter in adapters:
            if 'id' not in adapter:
                logging.error(
                    'id not in adapter %s', adapter
                )
                continue
            if 'package_installer' not in adapter:
                logging.info(
                    'package_installer not in adapter %s', adapter
                )
                continue
            adapter_id = adapter['id']
            adapter_mapping[adapter_id] = adapter
        clusters = cluster_api.list_clusters(user=user)
        cluster_mapping = {}
        for cluster in clusters:
            if 'id' not in cluster:
                logging.error('id not in cluster %s', cluster)
                continue
            cluster_id = cluster['id']
            if 'adapter_id' not in cluster:
                logging.error(
                    'adapter_id not in cluster %s',
                    cluster
                )
                continue
            cluster_state = cluster_api.get_cluster_state(
                cluster_id,
                user=user
            )
            if 'state' not in cluster_state:
                logging.error('state not in cluster state %s', cluster_state)
                continue
            cluster_mapping[cluster_id] = (cluster, cluster_state)
        clusterhosts = cluster_api.list_clusterhosts(user=user)
        clusterhost_mapping = {}
        for clusterhost in clusterhosts:
            if 'clusterhost_id' not in clusterhost:
                logging.error(
                    'clusterhost_id not in clusterhost %s',
                    clusterhost
                )
                continue
            clusterhost_id = clusterhost['clusterhost_id']
            if 'distributed_system_name' not in clusterhost:
                logging.error(
                    'distributed_system_name is not in clusterhost %s',
                    clusterhost
                )
                continue
            clusterhost_dirname = setting.CLUSTERHOST_INATALLATION_LOGDIR_NAME
            if clusterhost_dirname not in clusterhost:
                logging.error(
                    '%s is not in clusterhost %s',
                    clusterhost_dirname, clusterhost
                )
                continue
            if 'cluster_id' not in clusterhost:
                logging.error(
                    'cluster_id not in clusterhost %s',
                    clusterhost
                )
                continue
            cluster_id = clusterhost['cluster_id']
            if cluster_id not in cluster_mapping:
                logging.info(
                    'ignore clusterhost %s '
                    'since the cluster_id '
                    'is not in cluster_mapping %s',
                    clusterhost, cluster_mapping
                )
                continue
            cluster, _ = cluster_mapping[cluster_id]
            adapter_id = cluster['adapter_id']
            if adapter_id not in adapter_mapping:
                logging.info(
                    'ignore clusterhost %s '
                    'since the adapter_id %s '
                    'is not in adaper_mapping %s',
                    clusterhost, adapter_id, adapter_mapping
                )
                continue
            adapter = adapter_mapping[adapter_id]
            if 'package_installer' not in adapter:
                logging.info(
                    'ignore clusterhost %s '
                    'since the package_installer is not define '
                    'in adapter %s',
                    clusterhost, adapter
                )
                continue
            package_installer = adapter['package_installer']
            clusterhost['package_installer'] = package_installer
            clusterhost_state = cluster_api.get_clusterhost_self_state(
                clusterhost_id, user=user
            )
            if 'state' not in clusterhost_state:
                logging.error(
                    'state not in clusterhost_state %s',
                    clusterhost_state
                )
                continue
            if clusterhost_state['state'] == 'INSTALLING':
                clusterhost_log_histories = (
                    cluster_api.get_clusterhost_log_histories(
                        clusterhost_id, user=user
                    )
                )
                clusterhost_log_history_mapping = {}
                for clusterhost_log_history in clusterhost_log_histories:
                    if 'filename' not in clusterhost_log_history:
                        logging.error(
                            'filename not in clusterhost_log_history %s',
                            clusterhost_log_history
                        )
                        continue
                    clusterhost_log_history_mapping[
                        clusterhost_log_history['filename']
                    ] = clusterhost_log_history
                clusterhost_mapping[clusterhost_id] = (
                    clusterhost, clusterhost_state,
                    clusterhost_log_history_mapping
                )
            else:
                logging.info(
                    'ignore clusterhost state %s '
                    'since it is not in installing',
                    clusterhost_state
                )

        progress_calculator.update_host_progress(
            host_mapping)
        for host_id, (host, host_state, host_log_history_mapping) in (
            host_mapping.items()
        ):
            host_api.update_host_state(
                host_id, user=user,
                percentage=host_state.get('percentage', 0),
                message=host_state.get('message', ''),
                severity=host_state.get('severity', 'INFO')
            )
            for filename, host_log_history in (
                host_log_history_mapping.items()
            ):
                host_api.add_host_log_history(
                    host_id, filename=filename, user=user,
                    position=host_log_history.get('position', 0),
                    percentage=host_log_history.get('percentage', 0),
                    partial_line=host_log_history.get('partial_line', ''),
                    message=host_log_history.get('message', ''),
                    severity=host_log_history.get('severity', 'INFO'),
                    line_matcher_name=host_log_history.get(
                        'line_matcher_name', 'start'
                    )
                )
        progress_calculator.update_clusterhost_progress(
            clusterhost_mapping)
        for (
            clusterhost_id,
            (clusterhost, clusterhost_state, clusterhost_log_history_mapping)
        ) in (
            clusterhost_mapping.items()
        ):
            cluster_api.update_clusterhost_state(
                clusterhost_id, user=user,
                percentage=clusterhost_state.get('percentage', 0),
                message=clusterhost_state.get('message', ''),
                severity=clusterhost_state.get('severity', 'INFO')
            )
            for filename, clusterhost_log_history in (
                clusterhost_log_history_mapping.items()
            ):
                cluster_api.add_clusterhost_log_history(
                    clusterhost_id, user=user, filename=filename,
                    position=clusterhost_log_history.get('position', 0),
                    percentage=clusterhost_log_history.get('percentage', 0),
                    partial_line=clusterhost_log_history.get(
                        'partial_line', ''),
                    message=clusterhost_log_history.get('message', ''),
                    severity=clusterhost_log_history.get('severity', 'INFO'),
                    line_matcher_name=(
                        clusterhost_log_history.get(
                            'line_matcher_name', 'start'
                        )
                    )
                )
        progress_calculator.update_cluster_progress(
            cluster_mapping)
        for cluster_id, (cluster, cluster_state) in cluster_mapping.items():
            cluster_api.update_cluster_state(
                cluster_id, user=user
            )
예제 #37
0
def update_progress():
    """Update status and installing progress of the given cluster.

    :param cluster_hosts: clusters and hosts in each cluster to update.
    :type cluster_hosts: dict of int or str to list of int or str

    .. note::
       The function should be called out of the database session scope.
       In the function, it will update the database cluster_state and
       host_state table for the deploying cluster and hosts.

       The function will also query log_progressing_history table to get
       the lastest installing progress and the position of log it has
       processed in the last run. The function uses these information to
       avoid recalculate the progress from the beginning of the log file.
       After the progress got updated, these information will be stored back
       to the log_progressing_history for next time run.
    """
    with util.lock('log_progressing', timeout=60, blocking=False) as lock:
        if not lock:
            logging.error(
                'failed to acquire lock to calculate installation progress'
            )
            return

        logging.info('update installing progress')

        user = user_api.get_user_object(setting.COMPASS_ADMIN_EMAIL)
        hosts = host_api.list_hosts(user=user)
        host_mapping = {}
        for host in hosts:
            if 'id' not in host:
                logging.error('id is not in host %s', host)
                continue
            host_id = host['id']
            if 'os_name' not in host:
                logging.error('os_name is not in host %s', host)
                continue
            if 'os_installer' not in host:
                logging.error('os_installer is not in host %s', host)
                continue
            host_dirname = setting.HOST_INSTALLATION_LOGDIR_NAME
            if host_dirname not in host:
                logging.error(
                    '%s is not in host %s', host_dirname, host
                )
                continue
            host_state = host_api.get_host_state(host_id, user=user)
            if 'state' not in host_state:
                logging.error('state is not in host state %s', host_state)
                continue
            if host_state['state'] == 'INSTALLING':
                host_log_histories = host_api.get_host_log_histories(
                    host_id, user=user
                )
                host_log_history_mapping = {}
                for host_log_history in host_log_histories:
                    if 'filename' not in host_log_history:
                        logging.error(
                            'filename is not in host log history %s',
                            host_log_history
                        )
                        continue
                    host_log_history_mapping[
                        host_log_history['filename']
                    ] = host_log_history
                host_mapping[host_id] = (
                    host, host_state, host_log_history_mapping
                )
            else:
                logging.info(
                    'ignore host state %s since it is not in installing',
                    host_state
                )
        adapters = adapter_api.list_adapters(user=user)
        adapter_mapping = {}
        for adapter in adapters:
            if 'id' not in adapter:
                logging.error(
                    'id not in adapter %s', adapter
                )
                continue
            if 'package_installer' not in adapter:
                logging.info(
                    'package_installer not in adapter %s', adapter
                )
                continue
            adapter_id = adapter['id']
            adapter_mapping[adapter_id] = adapter
        clusters = cluster_api.list_clusters(user=user)
        cluster_mapping = {}
        for cluster in clusters:
            if 'id' not in cluster:
                logging.error('id not in cluster %s', cluster)
                continue
            cluster_id = cluster['id']
            if 'adapter_id' not in cluster:
                logging.error(
                    'adapter_id not in cluster %s',
                    cluster
                )
                continue
            cluster_state = cluster_api.get_cluster_state(
                cluster_id,
                user=user
            )
            if 'state' not in cluster_state:
                logging.error('state not in cluster state %s', cluster_state)
                continue
            cluster_mapping[cluster_id] = (cluster, cluster_state)
        clusterhosts = cluster_api.list_clusterhosts(user=user)
        clusterhost_mapping = {}
        for clusterhost in clusterhosts:
            if 'clusterhost_id' not in clusterhost:
                logging.error(
                    'clusterhost_id not in clusterhost %s',
                    clusterhost
                )
                continue
            clusterhost_id = clusterhost['clusterhost_id']
            if 'cluster_id' not in clusterhost:
                logging.error(
                    'cluster_id not in clusterhost %s',
                    clusterhost
                )
                continue
            cluster_id = clusterhost['cluster_id']
            if cluster_id not in cluster_mapping:
                logging.info(
                    'ignore clusterhost %s '
                    'since the cluster_id '
                    'is not in cluster_mapping %s',
                    clusterhost, cluster_mapping
                )
                continue
            cluster, _ = cluster_mapping[cluster_id]
            if 'flavor_name' not in cluster:
                logging.error(
                    'flavor_name is not in clusterhost %s related cluster',
                    clusterhost
                )
                continue
            clusterhost_dirname = setting.CLUSTERHOST_INATALLATION_LOGDIR_NAME
            if clusterhost_dirname not in clusterhost:
                logging.error(
                    '%s is not in clusterhost %s',
                    clusterhost_dirname, clusterhost
                )
                continue
            adapter_id = cluster['adapter_id']
            if adapter_id not in adapter_mapping:
                logging.info(
                    'ignore clusterhost %s '
                    'since the adapter_id %s '
                    'is not in adaper_mapping %s',
                    clusterhost, adapter_id, adapter_mapping
                )
                continue
            adapter = adapter_mapping[adapter_id]
            if 'package_installer' not in adapter:
                logging.info(
                    'ignore clusterhost %s '
                    'since the package_installer is not define '
                    'in adapter %s',
                    clusterhost, adapter
                )
                continue
            package_installer = adapter['package_installer']
            clusterhost['package_installer'] = package_installer
            clusterhost['adapter_name'] = adapter['name']
            clusterhost_state = cluster_api.get_clusterhost_self_state(
                clusterhost_id, user=user
            )
            if 'state' not in clusterhost_state:
                logging.error(
                    'state not in clusterhost_state %s',
                    clusterhost_state
                )
                continue
            if clusterhost_state['state'] == 'INSTALLING':
                clusterhost_log_histories = (
                    cluster_api.get_clusterhost_log_histories(
                        clusterhost_id, user=user
                    )
                )
                clusterhost_log_history_mapping = {}
                for clusterhost_log_history in clusterhost_log_histories:
                    if 'filename' not in clusterhost_log_history:
                        logging.error(
                            'filename not in clusterhost_log_history %s',
                            clusterhost_log_history
                        )
                        continue
                    clusterhost_log_history_mapping[
                        clusterhost_log_history['filename']
                    ] = clusterhost_log_history
                clusterhost_mapping[clusterhost_id] = (
                    clusterhost, clusterhost_state,
                    clusterhost_log_history_mapping
                )
            else:
                logging.info(
                    'ignore clusterhost state %s '
                    'since it is not in installing',
                    clusterhost_state
                )

        progress_calculator.update_host_progress(
            host_mapping)
        for host_id, (host, host_state, host_log_history_mapping) in (
            host_mapping.items()
        ):
            host_api.update_host_state(
                host_id, user=user,
                percentage=host_state.get('percentage', 0),
                message=host_state.get('message', ''),
                severity=host_state.get('severity', 'INFO')
            )
            for filename, host_log_history in (
                host_log_history_mapping.items()
            ):
                host_api.add_host_log_history(
                    host_id, filename=filename, user=user,
                    position=host_log_history.get('position', 0),
                    percentage=host_log_history.get('percentage', 0),
                    partial_line=host_log_history.get('partial_line', ''),
                    message=host_log_history.get('message', ''),
                    severity=host_log_history.get('severity', 'INFO'),
                    line_matcher_name=host_log_history.get(
                        'line_matcher_name', 'start'
                    )
                )
        progress_calculator.update_clusterhost_progress(
            clusterhost_mapping)
        for (
            clusterhost_id,
            (clusterhost, clusterhost_state, clusterhost_log_history_mapping)
        ) in (
            clusterhost_mapping.items()
        ):
            cluster_api.update_clusterhost_state(
                clusterhost_id, user=user,
                percentage=clusterhost_state.get('percentage', 0),
                message=clusterhost_state.get('message', ''),
                severity=clusterhost_state.get('severity', 'INFO')
            )
            for filename, clusterhost_log_history in (
                clusterhost_log_history_mapping.items()
            ):
                cluster_api.add_clusterhost_log_history(
                    clusterhost_id, user=user, filename=filename,
                    position=clusterhost_log_history.get('position', 0),
                    percentage=clusterhost_log_history.get('percentage', 0),
                    partial_line=clusterhost_log_history.get(
                        'partial_line', ''),
                    message=clusterhost_log_history.get('message', ''),
                    severity=clusterhost_log_history.get('severity', 'INFO'),
                    line_matcher_name=(
                        clusterhost_log_history.get(
                            'line_matcher_name', 'start'
                        )
                    )
                )
        progress_calculator.update_cluster_progress(
            cluster_mapping)
        for cluster_id, (cluster, cluster_state) in cluster_mapping.items():
            cluster_api.update_cluster_state(
                cluster_id, user=user
            )