Пример #1
0
def delete_clusters():
    clusternames = [
        clustername
        for clustername in flags.OPTIONS.clusternames.split(',')
        if clustername
    ]
    user = user_api.get_user_object(setting.COMPASS_ADMIN_EMAIL)
    list_cluster_args = {}
    if clusternames:
        list_cluster_args['name'] = clusternames
    clusters = cluster_api.list_clusters(
        user, **list_cluster_args
    )
    delete_underlying_host = flags.OPTIONS.delete_hosts
    for cluster in clusters:
        cluster_id = cluster['id']
        hosts = cluster_api.list_cluster_hosts(user, cluster_id)
        host_id_list = [host['id'] for host in hosts]
        logging.info(
            'delete cluster %s and cluster hosts %s',
            cluster_id, host_id_list
        )
        logging.info('delete underlying host? %s', delete_underlying_host)
        if flags.OPTIONS.async:
            celery.send_task(
                'compass.tasks.delete_cluster',
                (
                    setting.COMPASS_ADMIN_EMAIL,
                    cluster_id,
                    host_id_list,
                    delete_underlying_host
                )
            )
        else:
            try:
                delete.delete_cluster(
                    cluster_id,
                    host_id_list,
                    setting.COMPASS_ADMIN_EMAIL,
                    delete_underlying_host
                )
            except Exception as error:
                logging.error('failed to delete cluster %s', cluster)
                logging.exception(error)
Пример #2
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)
Пример #3
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)
Пример #4
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'
            )
Пример #5
0
          default='')
flags.add_bool('delete_hosts',
               help='if all hosts related to the cluster will be deleted',
               default=False)


if __name__ == '__main__':
    flags.init()
    logsetting.init()
    database.init()
    clusternames = [
        clustername
        for clustername in flags.OPTIONS.clusternames.split(',')
        if clustername
    ]
    logging.info('delete clusters %s', clusternames)
    user = user_api.get_user_object(setting.COMPASS_ADMIN_EMAIL)
    clusters = cluster_api.list_clusters(
        user, name=clusternames
    )
    if flags.OPTIONS.delete_hosts:
        for cluster in clusters:
            hosts = cluster_api.list_cluster_hosts(
                user, cluster['id'])
            for host in hosts:
                logging.info('delete host %s', host['hostname'])
                host_api.del_host(user, host['id'])
    for cluster in clusters:
        logging.info('delete cluster %s', cluster['name'])
        cluster_api.del_cluster(user, cluster['id'])