Esempio n. 1
0
extern int sacctmgr_modify_cluster(int argc, char **argv)
{
	int rc = SLURM_SUCCESS;
	int i=0;
	slurmdb_cluster_rec_t *cluster =
		xmalloc(sizeof(slurmdb_cluster_rec_t));
	slurmdb_assoc_rec_t *assoc =
		xmalloc(sizeof(slurmdb_assoc_rec_t));
	slurmdb_assoc_cond_t *assoc_cond =
		xmalloc(sizeof(slurmdb_assoc_cond_t));
	int cond_set = 0, prev_set = 0, rec_set = 0, set = 0;
	List ret_list = NULL;
	slurmdb_cluster_cond_t cluster_cond;
	bool existing_fed = false;

	slurmdb_init_assoc_rec(assoc, 0);

	assoc_cond->cluster_list = list_create(slurm_destroy_char);
	assoc_cond->acct_list = list_create(NULL);

	slurmdb_init_cluster_rec(cluster, 0);
	slurmdb_init_cluster_cond(&cluster_cond, 0);
	cluster_cond.cluster_list = assoc_cond->cluster_list;

	for (i=0; i<argc; i++) {
		int command_len = strlen(argv[i]);
		if (!strncasecmp(argv[i], "Where", MAX(command_len, 5))) {
			i++;
			prev_set = _set_cond(&i, argc, argv,
					     &cluster_cond, NULL);
			cond_set |= prev_set;
		} else if (!strncasecmp(argv[i], "Set", MAX(command_len, 3))) {
			i++;
			prev_set = _set_rec(&i, argc, argv,
					    NULL, assoc, cluster);
			rec_set |= prev_set;
		} else {
			prev_set = _set_cond(&i, argc, argv,
					     &cluster_cond, NULL);
			cond_set |= prev_set;
		}
	}

	if (exit_code) {
		rc = SLURM_ERROR;
		goto end_it;
	} else if (!rec_set) {
		exit_code=1;
		fprintf(stderr, " You didn't give me anything to set\n");
		rc = SLURM_ERROR;
		goto end_it;
	} else if (!cond_set) {
		if (!commit_check("You didn't set any conditions with 'WHERE'.\n"
				 "Are you sure you want to continue?")) {
			printf("Aborted\n");
			rc = SLURM_SUCCESS;
			goto end_it;
		}
	}

	if (cluster->fed.name && cluster->fed.name[0]) {
		int rc;
		/* Make sure federation exists. */
		List fed_list = list_create(slurm_destroy_char);
		list_append(fed_list, xstrdup(cluster->fed.name));
		rc = verify_federations_exist(fed_list);
		FREE_NULL_LIST(fed_list);
		if (rc)
			goto end_it;

		/* See if cluster is assigned to another federation already. */
		if (list_count(cluster_cond.cluster_list)) {
			if (_verify_fed_clusters(cluster_cond.cluster_list,
						 cluster->fed.name,
						 &existing_fed))
					goto end_it;
			else if (!list_count(cluster_cond.cluster_list)) {
				/* irrelevant changes have been removed and
				 * nothing to change now. */
				printf("Nothing to change\n");
				rc = SLURM_ERROR;
				(void)rc; /* CLANG false positive */
				goto end_it;
			} else if (existing_fed) {
				char *warning =
					"\nAre you sure you want to continue?";
				if (!commit_check(warning)) {
					rc = SLURM_ERROR;
					(void)rc; /* CLANG false positive */
					goto end_it;
				}
			}
		}
	}

	if (cond_set & 1) {
		List temp_list = NULL;

		temp_list = acct_storage_g_get_clusters(db_conn, my_uid,
							&cluster_cond);
		if (!temp_list) {
			exit_code=1;
			fprintf(stderr,
				" Problem getting clusters from database.  "
				"Contact your admin.\n");
			rc = SLURM_ERROR;
			goto end_it;
		} else if (!list_count(temp_list)) {
			fprintf(stderr,
				" Query didn't return any clusters.\n");
			rc = SLURM_ERROR;
			goto end_it;
		}
		/* we are only looking for the clusters returned from
		   this query, so we free the cluster_list and replace
		   it */
		FREE_NULL_LIST(assoc_cond->cluster_list);
		assoc_cond->cluster_list = temp_list;
	}

	printf(" Setting\n");
	if (rec_set & CLUS_REC_SET)
		sacctmgr_print_cluster(cluster);
	if (rec_set & CLUS_ASSOC_SET) {
		printf("  Default Limits:\n");
		sacctmgr_print_assoc_limits(assoc);
	}

	if (rec_set & CLUS_REC_SET) {
		notice_thread_init();
		ret_list = acct_storage_g_modify_clusters(
			db_conn, my_uid, &cluster_cond, cluster);

		if (ret_list && list_count(ret_list)) {
			char *object = NULL;
			ListIterator itr = list_iterator_create(ret_list);
			printf(" Modified cluster...\n");
			while((object = list_next(itr))) {
				printf("  %s\n", object);
			}
			list_iterator_destroy(itr);
			set = 1;
		} else if (ret_list) {
			printf(" Nothing modified\n");
			rc = SLURM_ERROR;
		} else {
			exit_code=1;
			fprintf(stderr, " Error with request: %s\n",
				slurm_strerror(errno));
			rc = SLURM_ERROR;
		}

		FREE_NULL_LIST(ret_list);
		notice_thread_fini();
	}

	if (rec_set & CLUS_ASSOC_SET) {
		list_append(assoc_cond->acct_list, "root");
		notice_thread_init();
		ret_list = acct_storage_g_modify_assocs(db_conn, my_uid,
							assoc_cond, assoc);

		if (ret_list && list_count(ret_list)) {
			char *object = NULL;
			ListIterator itr = list_iterator_create(ret_list);
			printf(" Modified cluster defaults for "
			       "associations...\n");
			while((object = list_next(itr))) {
				printf("  %s\n", object);
			}
			list_iterator_destroy(itr);
			set = 1;
		} else if (ret_list) {
			printf(" Nothing modified\n");
			rc = SLURM_ERROR;
		} else {
			exit_code=1;
			fprintf(stderr, " Error with request: %s\n",
				slurm_strerror(errno));
			rc = SLURM_ERROR;
		}
		FREE_NULL_LIST(ret_list);
		notice_thread_fini();
	}


	if (set) {
		if (commit_check("Would you like to commit changes?"))
			acct_storage_g_commit(db_conn, 1);
		else {
			printf(" Changes Discarded\n");
			acct_storage_g_commit(db_conn, 0);
		}
	}
end_it:
	slurmdb_destroy_assoc_cond(assoc_cond);
	slurmdb_destroy_assoc_rec(assoc);
	slurmdb_destroy_cluster_rec(cluster);

	return rc;
}
Esempio n. 2
0
extern List slurmdb_report_user_top_usage(void *db_conn,
        slurmdb_user_cond_t *user_cond,
        bool group_accounts)
{
    List cluster_list = NULL;
    ListIterator itr = NULL;
    ListIterator itr2 = NULL;
    ListIterator itr3 = NULL;
    ListIterator cluster_itr = NULL;
    slurmdb_cluster_cond_t cluster_cond;
    List user_list = NULL;
    List usage_cluster_list = NULL;
    char *object = NULL;
    int exit_code = 0;
    slurmdb_user_rec_t *user = NULL;
    slurmdb_cluster_rec_t *cluster = NULL;
    slurmdb_assoc_rec_t *assoc = NULL;
    slurmdb_report_user_rec_t *slurmdb_report_user = NULL;
    slurmdb_report_cluster_rec_t *slurmdb_report_cluster = NULL;
    uid_t my_uid = getuid();
    bool delete_user_cond = 0, delete_assoc_cond = 0,
         delete_cluster_list = 0;
    time_t start_time, end_time;

    if (!user_cond) {
        delete_user_cond = 1;
        user_cond = xmalloc(sizeof(slurmdb_user_cond_t));
    }

    if (!user_cond->assoc_cond) {
        delete_assoc_cond = 1;
        user_cond->assoc_cond =
            xmalloc(sizeof(slurmdb_assoc_cond_t));
    }

    if (!user_cond->assoc_cond->cluster_list) {
        delete_cluster_list = 1;
        user_cond->assoc_cond->cluster_list =
            list_create(slurm_destroy_char);
    }

    user_cond->with_deleted = 1;
    user_cond->with_assocs = 1;
    user_cond->assoc_cond->with_usage = 1;
    user_cond->assoc_cond->without_parent_info = 1;

    /* This needs to be done on some systems to make sure
       assoc_cond isn't messed up.  This has happened on some 64
       bit machines and this is here to be on the safe side.
    */
    start_time = user_cond->assoc_cond->usage_start;
    end_time = user_cond->assoc_cond->usage_end;
    slurmdb_report_set_start_end_time(&start_time, &end_time);
    user_cond->assoc_cond->usage_start = start_time;
    user_cond->assoc_cond->usage_end = end_time;

    user_list = acct_storage_g_get_users(db_conn, my_uid, user_cond);
    if (!user_list) {
        exit_code=1;
        fprintf(stderr, " Problem with user query.\n");
        goto end_it;
    }

    /* We have to get the clusters here or we will be unable to
       get the correct total time for the cluster if associations
       are not enforced.
    */
    slurmdb_init_cluster_cond(&cluster_cond, 0);
    cluster_cond.with_usage = 1;
    cluster_cond.with_deleted = 1;
    cluster_cond.usage_end = user_cond->assoc_cond->usage_end;
    cluster_cond.usage_start = user_cond->assoc_cond->usage_start;
    cluster_cond.cluster_list = user_cond->assoc_cond->cluster_list;

    usage_cluster_list = acct_storage_g_get_clusters(
                             db_conn, my_uid, &cluster_cond);
    if (!usage_cluster_list) {
        exit_code=1;
        fprintf(stderr, " Problem with cluster query.\n");
        goto end_it;
    }

    cluster_list = list_create(slurmdb_destroy_report_cluster_rec);

    itr = list_iterator_create(usage_cluster_list);
    while((cluster = list_next(itr))) {
        /* check to see if this cluster is around during the
           time we are looking at */
        if (!cluster->accounting_list
                || !list_count(cluster->accounting_list))
            continue;

        slurmdb_report_cluster = slurmdb_cluster_rec_2_report(cluster);

        list_append(cluster_list, slurmdb_report_cluster);

        slurmdb_report_cluster->user_list =
            list_create(slurmdb_destroy_report_user_rec);
    }
    list_iterator_destroy(itr);
    list_destroy(usage_cluster_list);

    itr = list_iterator_create(user_list);
    cluster_itr = list_iterator_create(cluster_list);
    while((user = list_next(itr))) {
        struct passwd *passwd_ptr = NULL;
        if (!user->assoc_list || !list_count(user->assoc_list))
            continue;

        passwd_ptr = getpwnam(user->name);
        if (passwd_ptr)
            user->uid = passwd_ptr->pw_uid;
        else
            user->uid = (uint32_t)NO_VAL;

        itr2 = list_iterator_create(user->assoc_list);
        while((assoc = list_next(itr2))) {

            if (!assoc->accounting_list
                    || !list_count(assoc->accounting_list))
                continue;

            while((slurmdb_report_cluster =
                        list_next(cluster_itr))) {
                if (!strcmp(slurmdb_report_cluster->name,
                            assoc->cluster)) {
                    ListIterator user_itr = NULL;
                    if (!group_accounts) {
                        slurmdb_report_user = NULL;
                        goto new_user;
                    }
                    user_itr = list_iterator_create(
                                   slurmdb_report_cluster->
                                   user_list);
                    while((slurmdb_report_user
                            = list_next(user_itr))) {
                        if (slurmdb_report_user->uid
                                != NO_VAL) {
                            if (slurmdb_report_user->
                                    uid
                                    == user->uid)
                                break;
                        } else if (slurmdb_report_user->
                                   name
                                   && !strcasecmp(
                                       slurmdb_report_user->
                                       name,
                                       user->name))
                            break;
                    }
                    list_iterator_destroy(user_itr);
new_user:
                    if (!slurmdb_report_user) {
                        slurmdb_report_user = xmalloc(
                                                  sizeof
                                                  (slurmdb_report_user_rec_t));
                        slurmdb_report_user->name =
                            xstrdup(assoc->user);
                        slurmdb_report_user->uid =
                            user->uid;
                        slurmdb_report_user->acct_list =
                            list_create
                            (slurm_destroy_char);
                        list_append(slurmdb_report_cluster->
                                    user_list,
                                    slurmdb_report_user);
                    }
                    break;
                }
            }
            if (!slurmdb_report_cluster) {
                error("This cluster '%s' hasn't "
                      "registered yet, but we have jobs "
                      "that ran?", assoc->cluster);
                slurmdb_report_cluster =
                    xmalloc(sizeof(slurmdb_report_cluster_rec_t));
                list_append(cluster_list, slurmdb_report_cluster);

                slurmdb_report_cluster->name = xstrdup(assoc->cluster);
                slurmdb_report_cluster->user_list =
                    list_create(slurmdb_destroy_report_user_rec);
                slurmdb_report_user =
                    xmalloc(sizeof(slurmdb_report_user_rec_t));
                slurmdb_report_user->name = xstrdup(assoc->user);
                slurmdb_report_user->uid = user->uid;
                slurmdb_report_user->acct_list =
                    list_create(slurm_destroy_char);
                list_append(slurmdb_report_cluster->user_list,
                            slurmdb_report_user);
            }
            list_iterator_reset(cluster_itr);

            itr3 = list_iterator_create(
                       slurmdb_report_user->acct_list);
            while((object = list_next(itr3))) {
                if (!strcmp(object, assoc->acct))
                    break;
            }
            list_iterator_destroy(itr3);

            if (!object)
                list_append(slurmdb_report_user->acct_list,
                            xstrdup(assoc->acct));
            slurmdb_transfer_acct_list_2_tres(
                assoc->accounting_list,
                &slurmdb_report_user->tres_list);
        }
        list_iterator_destroy(itr2);
    }
    list_iterator_destroy(itr);
    list_iterator_destroy(cluster_itr);

end_it:
    if (delete_cluster_list) {
        list_destroy(user_cond->assoc_cond->cluster_list);
        user_cond->assoc_cond->cluster_list = NULL;
    }

    if (delete_assoc_cond) {
        slurmdb_destroy_assoc_cond(user_cond->assoc_cond);
        user_cond->assoc_cond = NULL;
    }

    if (delete_user_cond) {
        slurmdb_destroy_user_cond(user_cond);
        user_cond = NULL;
    }

    if (user_list) {
        list_destroy(user_list);
        user_list = NULL;
    }

    if (exit_code) {
        if (cluster_list) {
            list_destroy(cluster_list);
            cluster_list = NULL;
        }
    }

    return cluster_list;
}