Beispiel #1
0
/*
 * get info from the storage
 * IN:  slurmdb_cluster_cond_t *
 * IN:  params void *
 * returns List of slurmdb_cluster_rec_t *
 * note List needs to be freed when called
 */
extern List slurmdb_clusters_get(void *db_conn,
				 slurmdb_cluster_cond_t *cluster_cond)
{
	if (db_api_uid == -1)
		db_api_uid = getuid();

	return acct_storage_g_get_clusters(db_conn, db_api_uid, cluster_cond);
}
Beispiel #2
0
static void _request_registrations(void *db_conn)
{
	List cluster_list = acct_storage_g_get_clusters(
		db_conn, getuid(), NULL);
	ListIterator itr;
	slurmdb_cluster_rec_t *cluster_rec = NULL;

	if (!cluster_list)
		return;
	itr = list_iterator_create(cluster_list);
	while ((cluster_rec = list_next(itr))) {
		if (!cluster_rec->control_port
		    || (cluster_rec->rpc_version < 9))
			continue;
		if (_send_slurmctld_register_req(cluster_rec) != SLURM_SUCCESS)
			/* mark this cluster as unresponsive */
			clusteracct_storage_g_fini_ctld(db_conn, cluster_rec);
	}
	list_iterator_destroy(itr);
	list_destroy(cluster_list);
}
Beispiel #3
0
extern int sacctmgr_dump_cluster (int argc, char **argv)
{
	slurmdb_user_cond_t user_cond;
	slurmdb_user_rec_t *user = NULL;
	slurmdb_hierarchical_rec_t *slurmdb_hierarchical_rec = NULL;
	slurmdb_assoc_rec_t *assoc = NULL;
	slurmdb_assoc_cond_t assoc_cond;
	List assoc_list = NULL;
	List acct_list = NULL;
	List user_list = NULL;
	List slurmdb_hierarchical_rec_list = NULL;
	char *cluster_name = NULL;
	char *file_name = NULL;
	char *user_name = NULL;
	char *line = NULL;
	int i, command_len = 0;
	FILE *fd = NULL;
	char *class_str = NULL;

	for (i = 0; i < argc; i++) {
		int end = parse_option_end(argv[i]);

		if (!end)
			command_len = strlen(argv[i]);
		else {
			command_len = end - 1;
			if (argv[i][end] == '=') {
				end++;
			}
		}
		if (!end || !strncasecmp(argv[i], "Cluster",
					 MAX(command_len, 1))) {
			if (cluster_name) {
				exit_code = 1;
				fprintf(stderr,
					" Can only do one cluster at a time.  "
					"Already doing %s\n", cluster_name);
				continue;
			}
			cluster_name = xstrdup(argv[i]+end);
		} else if (!strncasecmp(argv[i], "File",
					MAX(command_len, 1))) {
			if (file_name) {
				exit_code = 1;
				fprintf(stderr,
					" File name already set to %s\n",
					file_name);
				continue;
			}
			file_name = xstrdup(argv[i]+end);
		} else {
			exit_code = 1;
			fprintf(stderr, " Unknown option: %s\n", argv[i]);
		}
	}

	if (!cluster_name) {
		exit_code = 1;
		fprintf(stderr, " We need a cluster to dump.\n");
		xfree(file_name);
		return SLURM_ERROR;
	} else {
		List temp_list = NULL;
		slurmdb_cluster_cond_t cluster_cond;
		slurmdb_cluster_rec_t *cluster_rec = NULL;

		slurmdb_init_cluster_cond(&cluster_cond, 0);
		cluster_cond.cluster_list = list_create(NULL);
		list_push(cluster_cond.cluster_list, cluster_name);

		temp_list = acct_storage_g_get_clusters(db_conn, my_uid,
							&cluster_cond);
		FREE_NULL_LIST(cluster_cond.cluster_list);
		if (!temp_list) {
			exit_code = 1;
			fprintf(stderr,
				" Problem getting clusters from database.  "
				"Contact your admin.\n");
			xfree(cluster_name);
			xfree(file_name);
			return SLURM_ERROR;
		}

		cluster_rec = list_peek(temp_list);
		if (!cluster_rec) {
			exit_code = 1;
			fprintf(stderr, " Cluster %s doesn't exist.\n",
				cluster_name);
			xfree(cluster_name);
			xfree(file_name);
			FREE_NULL_LIST(temp_list);
			return SLURM_ERROR;
		}
		class_str = get_classification_str(cluster_rec->classification);
		FREE_NULL_LIST(temp_list);
	}

	if (!file_name) {
		file_name = xstrdup_printf("./%s.cfg", cluster_name);
		printf(" No filename given, using %s.\n", file_name);
	}

	memset(&user_cond, 0, sizeof(slurmdb_user_cond_t));
	user_cond.with_coords = 1;
	user_cond.with_wckeys = 1;
	user_cond.with_assocs = 1;

	memset(&assoc_cond, 0, sizeof(slurmdb_assoc_cond_t));
	assoc_cond.without_parent_limits = 1;
	assoc_cond.with_raw_qos = 1;
	assoc_cond.cluster_list = list_create(NULL);
	list_append(assoc_cond.cluster_list, cluster_name);
	/* this is needed for getting the correct wckeys */
	user_cond.assoc_cond = &assoc_cond;

	user_list = acct_storage_g_get_users(db_conn, my_uid, &user_cond);
	/* If not running with the DBD assoc_cond.user_list can be set,
	 * which will mess other things up.
	 */
	if (assoc_cond.user_list) {
		FREE_NULL_LIST(assoc_cond.user_list);
		assoc_cond.user_list = NULL;
	}

	/* make sure this person running is an admin */
	user_name = uid_to_string_cached(my_uid);
	if (!(user = sacctmgr_find_user_from_list(user_list, user_name))) {
		exit_code = 1;
		fprintf(stderr, " Your uid (%u) is not in the "
			"accounting system, can't dump cluster.\n", my_uid);
		FREE_NULL_LIST(assoc_cond.cluster_list);
		xfree(cluster_name);
		xfree(file_name);
		FREE_NULL_LIST(user_list);
		return SLURM_ERROR;

	} else {
		if (my_uid != slurm_get_slurm_user_id() && my_uid != 0
		    && user->admin_level < SLURMDB_ADMIN_SUPER_USER) {
			exit_code = 1;
			fprintf(stderr, " Your user does not have sufficient "
				"privileges to dump clusters.\n");
			FREE_NULL_LIST(assoc_cond.cluster_list);
			xfree(cluster_name);
			xfree(file_name);
			FREE_NULL_LIST(user_list);
			return SLURM_ERROR;
		}
	}
	xfree(user_name);

	/* assoc_cond is set up above */
	assoc_list = acct_storage_g_get_assocs(db_conn, my_uid,
						     &assoc_cond);
	FREE_NULL_LIST(assoc_cond.cluster_list);
	if (!assoc_list) {
		exit_code = 1;
		fprintf(stderr, " Problem with query.\n");
		xfree(cluster_name);
		xfree(file_name);
		return SLURM_ERROR;
	} else if (!list_count(assoc_list)) {
		exit_code = 1;
		fprintf(stderr, " Cluster %s returned nothing.\n",
			cluster_name);
		FREE_NULL_LIST(assoc_list);
		xfree(cluster_name);
		xfree(file_name);
		return SLURM_ERROR;
	}

	slurmdb_hierarchical_rec_list = slurmdb_get_acct_hierarchical_rec_list(
		assoc_list);

	acct_list = acct_storage_g_get_accounts(db_conn, my_uid, NULL);

	if ((fd = fopen(file_name,"w")) == NULL) {
		fprintf(stderr, "Can't open file %s, %s\n", file_name,
			slurm_strerror(errno));
		FREE_NULL_LIST(acct_list);
		FREE_NULL_LIST(assoc_list);
		xfree(cluster_name);
		xfree(file_name);
		FREE_NULL_LIST(slurmdb_hierarchical_rec_list);
		return SLURM_ERROR;
	}

	/* Add header */
	if (fprintf(fd,
		    "# To edit this file start with a cluster line "
		    "for the new cluster\n"
		    "# Cluster - 'cluster_name':MaxNodesPerJob=50\n"
		    "# Followed by Accounts you want in this fashion "
		    "(root is created by default)...\n"
		    "# Parent - 'root'\n"
		    "# Account - 'cs':MaxNodesPerJob=5:MaxJobs=4:"
		    "MaxTRESMins=cpu=20:FairShare=399:"
		    "MaxWallDuration=40:Description='Computer Science':"
		    "Organization='LC'\n"
		    "# Any of the options after a ':' can be left out and "
		    "they can be in any order.\n"
		    "# If you want to add any sub accounts just list the "
		    "Parent THAT HAS ALREADY \n"
		    "# BEEN CREATED before the account line in this "
		    "fashion...\n"
		    "# Parent - 'cs'\n"
		    "# Account - 'test':MaxNodesPerJob=1:MaxJobs=1:"
		    "MaxTRESMins=cpu=1:FairShare=1:"
		    "MaxWallDuration=1:"
		    "Description='Test Account':Organization='Test'\n"
		    "# To add users to a account add a line like this after a "
		    "Parent - 'line'\n"
		    "# User - 'lipari':MaxNodesPerJob=2:MaxJobs=3:"
		    "MaxTRESMins=cpu=4:FairShare=1:"
		    "MaxWallDurationPerJob=1\n") < 0) {
		exit_code = 1;
		fprintf(stderr, "Can't write to file");
		FREE_NULL_LIST(acct_list);
		FREE_NULL_LIST(assoc_list);
		xfree(cluster_name);
		xfree(file_name);
		FREE_NULL_LIST(slurmdb_hierarchical_rec_list);
		return SLURM_ERROR;
	}

	line = xstrdup_printf("Cluster - '%s'", cluster_name);

	if (class_str)
		xstrfmtcat(line, ":Classification='%s'", class_str);

	slurmdb_hierarchical_rec = list_peek(slurmdb_hierarchical_rec_list);
	assoc = slurmdb_hierarchical_rec->assoc;
	if (xstrcmp(assoc->acct, "root")) {
		fprintf(stderr, "Root association not on the top it was %s\n",
			assoc->acct);
	} else
		print_file_add_limits_to_line(&line, assoc);

	if (fprintf(fd, "%s\n", line) < 0) {
		exit_code = 1;
		fprintf(stderr, " Can't write to file");
		FREE_NULL_LIST(acct_list);
		FREE_NULL_LIST(assoc_list);
		xfree(cluster_name);
		xfree(file_name);
		xfree(line);
		FREE_NULL_LIST(slurmdb_hierarchical_rec_list);
		return SLURM_ERROR;
	}
	info("%s", line);
	xfree(line);

	print_file_slurmdb_hierarchical_rec_list(
		fd, slurmdb_hierarchical_rec_list, user_list, acct_list);

	FREE_NULL_LIST(acct_list);
	FREE_NULL_LIST(assoc_list);
	xfree(cluster_name);
	xfree(file_name);
	FREE_NULL_LIST(slurmdb_hierarchical_rec_list);
	fclose(fd);

	return SLURM_SUCCESS;
}
Beispiel #4
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;
}
Beispiel #5
0
extern int sacctmgr_list_cluster(int argc, char **argv)
{
	int rc = SLURM_SUCCESS;
	slurmdb_cluster_cond_t *cluster_cond =
		xmalloc(sizeof(slurmdb_cluster_cond_t));
	List cluster_list;
	int i=0;
	ListIterator itr = NULL;
	ListIterator itr2 = NULL;
	slurmdb_cluster_rec_t *cluster = NULL;
	char *tmp_char = NULL;

	int field_count = 0;

	print_field_t *field = NULL;

	List format_list = list_create(slurm_destroy_char);
	List print_fields_list; /* types are of print_field_t */

	slurmdb_init_cluster_cond(cluster_cond, 0);
	cluster_cond->cluster_list = list_create(slurm_destroy_char);
	for (i=0; i<argc; i++) {
		int command_len = strlen(argv[i]);
		if (!strncasecmp(argv[i], "Where", MAX(command_len, 5))
		    || !strncasecmp(argv[i], "Set", MAX(command_len, 3)))
			i++;
		_set_cond(&i, argc, argv, cluster_cond, format_list);
	}

	if (exit_code) {
		slurmdb_destroy_cluster_cond(cluster_cond);
		FREE_NULL_LIST(format_list);
		return SLURM_ERROR;
	}

	if (!list_count(format_list)) {
		slurm_addto_char_list(format_list,
				      "Cl,Controlh,Controlp,RPC");
		if (!without_limits)
			slurm_addto_char_list(format_list,
					      "Fa,GrpJ,GrpTRES,GrpS,MaxJ,"
					      "MaxTRES,MaxS,MaxW,QOS,"
					      "DefaultQOS");
		if (with_fed)
			slurm_addto_char_list(format_list,
					      "Federation,ID,Weight,FedState");
	}

	cluster_cond->with_deleted = with_deleted;

	print_fields_list = sacctmgr_process_format_list(format_list);
	FREE_NULL_LIST(format_list);

	if (exit_code) {
		slurmdb_destroy_cluster_cond(cluster_cond);
		FREE_NULL_LIST(print_fields_list);
		return SLURM_ERROR;
	}

	cluster_list = acct_storage_g_get_clusters(db_conn, my_uid,
						   cluster_cond);
	slurmdb_destroy_cluster_cond(cluster_cond);

	if (!cluster_list) {
		exit_code=1;
		fprintf(stderr, " Problem with query.\n");
		FREE_NULL_LIST(print_fields_list);
		return SLURM_ERROR;
	}

	itr = list_iterator_create(cluster_list);
	itr2 = list_iterator_create(print_fields_list);
	print_fields_header(print_fields_list);

	field_count = list_count(print_fields_list);

	while ((cluster = list_next(itr))) {
		int curr_inx = 1;

		/* set up the working cluster rec so nodecnt's and node names
		 * are handled correctly */
		working_cluster_rec = cluster;
		while((field = list_next(itr2))) {
			switch(field->type) {
			case PRINT_CLUSTER:
				field->print_routine(field, cluster->name,
						     (curr_inx == field_count));
				break;
			case PRINT_CHOST:
				field->print_routine(field,
						     cluster->control_host,
						     (curr_inx == field_count));
				break;
			case PRINT_CPORT:
				field->print_routine(field,
						     cluster->control_port,
						     (curr_inx == field_count));
				break;
			case PRINT_CLASS:
				field->print_routine(field,
						     get_classification_str(
						     cluster->classification),
						     (curr_inx == field_count));
				break;
			case PRINT_FEDERATION:
				field->print_routine(field, cluster->fed.name,
						     (curr_inx == field_count));
				break;
			case PRINT_FEDSTATE:
			{
				char *tmp_str = slurmdb_cluster_fed_states_str(
							cluster->fed.state);
				field->print_routine(field, tmp_str,
						     (curr_inx == field_count));
				break;
			}
			case PRINT_FEDSTATERAW:
				field->print_routine(field, cluster->fed.state,
						     (curr_inx == field_count));
				break;
			case PRINT_ID:
				field->print_routine(field, cluster->fed.id,
						     (curr_inx == field_count));
				break;
			case PRINT_WEIGHT:
				field->print_routine(field, cluster->fed.weight,
						     (curr_inx == field_count));
				break;
			case PRINT_TRES:
				sacctmgr_initialize_g_tres_list();

				tmp_char = slurmdb_make_tres_string_from_simple(
					cluster->tres_str, g_tres_list, NO_VAL,
					CONVERT_NUM_UNIT_EXACT);
				field->print_routine(field, tmp_char,
						     (curr_inx == field_count));
				xfree(tmp_char);
				break;
			case PRINT_FLAGS:
			{
				char *tmp_char = slurmdb_cluster_flags_2_str(
							     cluster->flags);
				field->print_routine(field, tmp_char,
						     (curr_inx == field_count));
				xfree(tmp_char);
				break;
			}
			case PRINT_NODECNT:
			{
				hostlist_t hl = hostlist_create(cluster->nodes);
				int cnt = 0;
				if (hl) {
					cnt = hostlist_count(hl);
					hostlist_destroy(hl);
				}
				field->print_routine(
					field,
					cnt,
					(curr_inx == field_count));
				break;
			}
			case PRINT_CLUSTER_NODES:
				field->print_routine(
					field,
					cluster->nodes,
					(curr_inx == field_count));
				break;
			case PRINT_RPC_VERSION:
				field->print_routine(
					field,
					cluster->rpc_version,
					(curr_inx == field_count));
				break;
			case PRINT_SELECT:
				field->print_routine(
					field,
					cluster->plugin_id_select,
					(curr_inx == field_count));
				break;
			default:
				sacctmgr_print_assoc_rec(cluster->root_assoc,
							 field, NULL,
							 (curr_inx ==
							  field_count));
				break;
			}
			curr_inx++;
		}
		list_iterator_reset(itr2);
		printf("\n");
	}
	/* clear the working cluster rec */
	working_cluster_rec = NULL;

	list_iterator_destroy(itr2);
	list_iterator_destroy(itr);
	FREE_NULL_LIST(cluster_list);
	FREE_NULL_LIST(print_fields_list);

	return rc;
}
Beispiel #6
0
extern int sacctmgr_add_cluster(int argc, char **argv)
{
	int rc = SLURM_SUCCESS;
	int i = 0;
	slurmdb_cluster_rec_t *cluster = NULL;
	slurmdb_cluster_rec_t *start_cluster =
		xmalloc(sizeof(slurmdb_cluster_rec_t));
	List name_list = list_create(slurm_destroy_char);
	List cluster_list = NULL;
	slurmdb_assoc_rec_t start_assoc;

	int limit_set = 0;
	ListIterator itr = NULL, itr_c = NULL;
	char *name = NULL;

	slurmdb_init_assoc_rec(&start_assoc, 0);
	slurmdb_init_cluster_rec(start_cluster, 0);

	for (i=0; i<argc; i++) {
		int command_len = strlen(argv[i]);
		if (!strncasecmp(argv[i], "Where", MAX(command_len, 5))
		    || !strncasecmp(argv[i], "Set", MAX(command_len, 3)))
			i++;
		limit_set += _set_rec(&i, argc, argv,
				      name_list, &start_assoc, start_cluster);
	}
	if (exit_code) {
		FREE_NULL_LIST(name_list);
		slurmdb_destroy_cluster_rec(start_cluster);
		return SLURM_ERROR;
	} else if (!list_count(name_list)) {
		FREE_NULL_LIST(name_list);
		slurmdb_destroy_cluster_rec(start_cluster);
		exit_code=1;
		fprintf(stderr, " Need name of cluster to add.\n");
		return SLURM_ERROR;
	} else {
		List temp_list = NULL;
		slurmdb_cluster_cond_t cluster_cond;

		slurmdb_init_cluster_cond(&cluster_cond, 0);
		cluster_cond.cluster_list = name_list;
		cluster_cond.classification = start_cluster->classification;

		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");
			slurmdb_destroy_cluster_rec(start_cluster);
			return SLURM_ERROR;
		}

		itr_c = list_iterator_create(name_list);
		itr = list_iterator_create(temp_list);
		while((name = list_next(itr_c))) {
			slurmdb_cluster_rec_t *cluster_rec = NULL;

			list_iterator_reset(itr);
			while((cluster_rec = list_next(itr))) {
				if (!xstrcasecmp(cluster_rec->name, name))
					break;
			}
			if (cluster_rec) {
				printf(" This cluster %s already exists.  "
				       "Not adding.\n", name);
				list_delete_item(itr_c);
			}
		}
		list_iterator_destroy(itr);
		list_iterator_destroy(itr_c);
		FREE_NULL_LIST(temp_list);
		if (!list_count(name_list)) {
			FREE_NULL_LIST(name_list);
			slurmdb_destroy_cluster_rec(start_cluster);
			return SLURM_ERROR;
		}
	}

	if (start_cluster->fed.name) {
		int rc;
		List fed_list = list_create(slurm_destroy_char);
		list_append(fed_list, xstrdup(start_cluster->fed.name));
		rc = verify_federations_exist(fed_list);
		FREE_NULL_LIST(fed_list);
		if (rc) {
			slurmdb_destroy_cluster_rec(start_cluster);
			FREE_NULL_LIST(name_list);
			return SLURM_ERROR;
		}
	}

	printf(" Adding Cluster(s)\n");
	cluster_list = list_create(slurmdb_destroy_cluster_rec);
	itr = list_iterator_create(name_list);
	while((name = list_next(itr))) {
		if (!name[0]) {
			exit_code=1;
			fprintf(stderr, " No blank names are "
				"allowed when adding.\n");
			rc = SLURM_ERROR;
			continue;
		}
		cluster = xmalloc(sizeof(slurmdb_cluster_rec_t));
		slurmdb_init_cluster_rec(cluster, 0);

		list_append(cluster_list, cluster);
		slurmdb_copy_cluster_rec(cluster, start_cluster);
		cluster->name = xstrdup(name);
		printf("  Name           = %s\n", cluster->name);

		cluster->root_assoc =
			xmalloc(sizeof(slurmdb_assoc_rec_t));
		slurmdb_init_assoc_rec(cluster->root_assoc, 0);
		cluster->root_assoc->def_qos_id = start_assoc.def_qos_id;
		cluster->root_assoc->shares_raw = start_assoc.shares_raw;

		slurmdb_copy_assoc_rec_limits(
			cluster->root_assoc, &start_assoc);
	}
	list_iterator_destroy(itr);
	FREE_NULL_LIST(name_list);

	if (limit_set)
		printf(" Setting\n");
	if (limit_set & CLUS_REC_SET)
		sacctmgr_print_cluster(start_cluster);
	if (limit_set & CLUS_ASSOC_SET) {
		printf("  Default Limits:\n");
		sacctmgr_print_assoc_limits(&start_assoc);
		FREE_NULL_LIST(start_assoc.qos_list);
	}
	slurmdb_destroy_cluster_rec(start_cluster);

	if (!list_count(cluster_list)) {
		printf(" Nothing new added.\n");
		rc = SLURM_ERROR;
		goto end_it;
	}

	/* Since we are creating tables with add cluster that can't be
	   rolled back.  So we ask before hand if they are serious
	   about it so we can rollback if needed.
	*/
	if (commit_check("Would you like to commit changes?")) {
		notice_thread_init();
		rc = acct_storage_g_add_clusters(db_conn, my_uid, cluster_list);
		notice_thread_fini();
		if (rc == SLURM_SUCCESS) {
			acct_storage_g_commit(db_conn, 1);
		} else {
			exit_code=1;
			fprintf(stderr, " Problem adding clusters: %s\n",
				slurm_strerror(rc));
			/* this isn't really needed, but just to be safe */
			acct_storage_g_commit(db_conn, 0);
		}
	} else {
		printf(" Changes Discarded\n");
		/* this isn't really needed, but just to be safe */
		acct_storage_g_commit(db_conn, 0);
	}

end_it:
	FREE_NULL_LIST(cluster_list);

	return rc;
}
Beispiel #7
0
/* Verify that clusters exist in the database.
 * Will remove clusters from list if they are already on the federation or if
 * a cluster is being removed and it doesn't exist on the federation.
 *
 * IN cluster_list: list of slurmdb_cluster_rec_t's with cluster names set.
 * IN fed_name: (optional) Name of federation that is being added/modified.
 * OUT existing_fed: Will be set to TRUE if a cluster in cluster_list is
 *                   assigned to a federation that is not fed_name. If fed_name
 *                   is set to NULL and a cluster is assigned to federation then
 *                   existing_fed will be set to TRUE.
 */
extern int verify_fed_clusters(List cluster_list, const char *fed_name,
			       bool *existing_fed)
{
	char        *missing_str  = NULL;
	char        *existing_str = NULL;
	List         temp_list    = NULL;
	ListIterator itr_db       = NULL;
	ListIterator itr_c        = NULL;
	slurmdb_cluster_rec_t *cluster_rec  = NULL;
	slurmdb_cluster_cond_t cluster_cond;

	/* Get existing clusters from database */
	slurmdb_init_cluster_cond(&cluster_cond, 0);
	cluster_cond.cluster_list = list_create(slurm_destroy_char);
	itr_c = list_iterator_create(cluster_list);
	while ((cluster_rec = list_next(itr_c))) {
		char *tmp_name = cluster_rec->name;
		if (!tmp_name)
			continue;

		if (tmp_name && (tmp_name[0] == '+' || tmp_name[0] == '-'))
			tmp_name++;
		list_append(cluster_cond.cluster_list, xstrdup(tmp_name));
	}
	temp_list = acct_storage_g_get_clusters(db_conn, my_uid,
						&cluster_cond);
	FREE_NULL_LIST(cluster_cond.cluster_list);
	if (!temp_list) {
		fprintf(stderr,
			" Problem getting clusters from database.  "
			"Contact your admin.\n");
		list_iterator_destroy(itr_c);
		return SLURM_ERROR;
	}

	/* See if the clusters we are looking to add are in the cluster list
	 * from the db. */
	list_iterator_reset(itr_c);
	itr_db = list_iterator_create(temp_list);
	while((cluster_rec = list_next(itr_c))) {
		slurmdb_cluster_rec_t *db_rec = NULL;
		char *tmp_name = cluster_rec->name;
		if (!tmp_name)
			continue;

		if (tmp_name[0] == '+' || tmp_name[0] == '-')
			tmp_name++;

		list_iterator_reset(itr_db);
		while((db_rec = list_next(itr_db))) {
			if (!strcasecmp(db_rec->name, tmp_name))
				break;
		}
		if (!db_rec) {
			xstrfmtcat(missing_str, " The cluster %s doesn't exist."
				   " Please add first.\n", tmp_name);
		} else if (*cluster_rec->name != '-' &&
			   db_rec->fed.name && *db_rec->fed.name) {

			if (fed_name && !xstrcmp(fed_name, db_rec->fed.name)) {
				fprintf(stderr, " The cluster %s is already "
						"assigned to federation %s\n",
					db_rec->name, db_rec->fed.name);
				list_delete_item(itr_c);
			} else {
				xstrfmtcat(existing_str, " The cluster %s is "
					   "assigned to federation %s\n",
					   db_rec->name, db_rec->fed.name);
			}
		} else if (*cluster_rec->name == '-' &&
			   fed_name && xstrcmp(fed_name, db_rec->fed.name)) {
			fprintf(stderr, " The cluster %s isn't assigned to "
					"federation %s\n",
				db_rec->name, fed_name);
			list_delete_item(itr_c);
		}
	}

	list_iterator_destroy(itr_db);
	list_iterator_destroy(itr_c);
	FREE_NULL_LIST(temp_list);
	if (missing_str) {
		fprintf(stderr, "%s", missing_str);
		xfree(missing_str);
		return SLURM_ERROR;
	} else if (existing_str) {
		*existing_fed = true;
		fprintf(stderr, "%s", existing_str);
	}
	xfree(existing_str);

	return SLURM_SUCCESS;
}
Beispiel #8
0
extern int sacctmgr_add_account(int argc, char *argv[])
{
    int rc = SLURM_SUCCESS;
    int i=0;
    ListIterator itr = NULL, itr_c = NULL;
    slurmdb_account_rec_t *acct = NULL;
    slurmdb_association_rec_t *assoc = NULL;
    slurmdb_association_cond_t assoc_cond;
    List name_list = list_create(slurm_destroy_char);
    List cluster_list = list_create(slurm_destroy_char);
    char *cluster = NULL;
    char *name = NULL;
    List acct_list = NULL;
    List assoc_list = NULL;
    List local_assoc_list = NULL;
    List local_account_list = NULL;
    char *acct_str = NULL;
    char *assoc_str = NULL;
    int limit_set = 0;
    slurmdb_account_rec_t *start_acct =
        xmalloc(sizeof(slurmdb_account_rec_t));
    slurmdb_association_rec_t *start_assoc =
        xmalloc(sizeof(slurmdb_association_rec_t));

    slurmdb_init_association_rec(start_assoc, 0);

    for (i=0; i<argc; i++) {
        int command_len = strlen(argv[i]);
        if (!strncasecmp(argv[i], "Where", MAX(command_len, 5))
                || !strncasecmp(argv[i], "Set", MAX(command_len, 3)))
            i++;
        limit_set += _set_rec(&i, argc, argv, name_list, cluster_list,
                              start_acct, start_assoc);
    }
    if (exit_code)
        return SLURM_ERROR;

    if (!name_list || !list_count(name_list)) {
        list_destroy(name_list);
        list_destroy(cluster_list);
        slurmdb_destroy_association_rec(start_assoc);
        slurmdb_destroy_account_rec(start_acct);
        exit_code=1;
        fprintf(stderr, " Need name of account to add.\n");
        return SLURM_SUCCESS;
    } else {
        slurmdb_account_cond_t account_cond;
        memset(&account_cond, 0, sizeof(slurmdb_account_cond_t));
        memset(&assoc_cond, 0, sizeof(slurmdb_association_cond_t));

        assoc_cond.acct_list = name_list;
        account_cond.assoc_cond = &assoc_cond;

        local_account_list = acct_storage_g_get_accounts(
                                 db_conn, my_uid, &account_cond);
    }

    if (!local_account_list) {
        exit_code=1;
        fprintf(stderr, " Problem getting accounts from database.  "
                "Contact your admin.\n");
        list_destroy(name_list);
        list_destroy(cluster_list);
        slurmdb_destroy_association_rec(start_assoc);
        slurmdb_destroy_account_rec(start_acct);
        return SLURM_ERROR;
    }

    if (!start_assoc->parent_acct)
        start_assoc->parent_acct = xstrdup("root");

    if (!cluster_list || !list_count(cluster_list)) {
        slurmdb_cluster_rec_t *cluster_rec = NULL;
        List tmp_list =
            acct_storage_g_get_clusters(db_conn, my_uid, NULL);
        if (!tmp_list) {
            exit_code=1;
            fprintf(stderr,
                    " Problem getting clusters from database.  "
                    "Contact your admin.\n");
            list_destroy(name_list);
            list_destroy(cluster_list);
            slurmdb_destroy_association_rec(start_assoc);
            slurmdb_destroy_account_rec(start_acct);
            list_destroy(local_account_list);
            return SLURM_ERROR;
        }

        if (!list_count(tmp_list)) {
            exit_code=1;
            fprintf(stderr,
                    "  Can't add accounts, no cluster "
                    "defined yet.\n"
                    " Please contact your administrator.\n");
            list_destroy(name_list);
            list_destroy(cluster_list);
            slurmdb_destroy_association_rec(start_assoc);
            slurmdb_destroy_account_rec(start_acct);
            list_destroy(local_account_list);
            return SLURM_ERROR;
        }
        if (!cluster_list)
            list_create(slurm_destroy_char);
        else
            list_flush(cluster_list);

        itr_c = list_iterator_create(tmp_list);
        while((cluster_rec = list_next(itr_c))) {
            list_append(cluster_list, xstrdup(cluster_rec->name));
        }
        list_iterator_destroy(itr_c);
        list_destroy(tmp_list);
    } else {
        List temp_list = NULL;
        slurmdb_cluster_cond_t cluster_cond;

        slurmdb_init_cluster_cond(&cluster_cond, 0);
        cluster_cond.cluster_list = cluster_list;

        temp_list = acct_storage_g_get_clusters(db_conn, my_uid,
                                                &cluster_cond);

        itr_c = list_iterator_create(cluster_list);
        itr = list_iterator_create(temp_list);
        while((cluster = list_next(itr_c))) {
            slurmdb_cluster_rec_t *cluster_rec = NULL;

            list_iterator_reset(itr);
            while((cluster_rec = list_next(itr))) {
                if (!strcasecmp(cluster_rec->name, cluster))
                    break;
            }
            if (!cluster_rec) {
                exit_code=1;
                fprintf(stderr, " This cluster '%s' "
                        "doesn't exist.\n"
                        "        Contact your admin "
                        "to add it to accounting.\n",
                        cluster);
                list_delete_item(itr_c);
            }
        }
        list_iterator_destroy(itr);
        list_iterator_destroy(itr_c);
        list_destroy(temp_list);

        if (!list_count(cluster_list)) {
            slurmdb_destroy_association_rec(start_assoc);
            slurmdb_destroy_account_rec(start_acct);
            list_destroy(local_account_list);
            return SLURM_ERROR;
        }
    }


    acct_list = list_create(slurmdb_destroy_account_rec);
    assoc_list = list_create(slurmdb_destroy_association_rec);

    memset(&assoc_cond, 0, sizeof(slurmdb_association_cond_t));

    assoc_cond.acct_list = list_create(NULL);
    itr = list_iterator_create(name_list);
    while((name = list_next(itr)))
        list_append(assoc_cond.acct_list, name);
    list_iterator_destroy(itr);
    list_append(assoc_cond.acct_list, start_assoc->parent_acct);

    assoc_cond.cluster_list = cluster_list;
    local_assoc_list = acct_storage_g_get_associations(
                           db_conn, my_uid, &assoc_cond);
    list_destroy(assoc_cond.acct_list);
    if (!local_assoc_list) {
        exit_code=1;
        fprintf(stderr, " Problem getting associations from database.  "
                "Contact your admin.\n");
        list_destroy(name_list);
        list_destroy(cluster_list);
        slurmdb_destroy_association_rec(start_assoc);
        slurmdb_destroy_account_rec(start_acct);
        list_destroy(local_account_list);
        return SLURM_ERROR;
    }

    itr = list_iterator_create(name_list);
    while((name = list_next(itr))) {
        if (!name[0]) {
            exit_code=1;
            fprintf(stderr, " No blank names are "
                    "allowed when adding.\n");
            rc = SLURM_ERROR;
            continue;
        }

        acct = NULL;
        if (!sacctmgr_find_account_from_list(local_account_list, name)) {
            acct = xmalloc(sizeof(slurmdb_account_rec_t));
            acct->assoc_list =
                list_create(slurmdb_destroy_association_rec);
            acct->name = xstrdup(name);
            if (start_acct->description)
                acct->description =
                    xstrdup(start_acct->description);
            else
                acct->description = xstrdup(name);

            if (start_acct->organization)
                acct->organization =
                    xstrdup(start_acct->organization);
            else if (strcmp(start_assoc->parent_acct, "root"))
                acct->organization =
                    xstrdup(start_assoc->parent_acct);
            else
                acct->organization = xstrdup(name);

            xstrfmtcat(acct_str, "  %s\n", name);
            list_append(acct_list, acct);
        }

        itr_c = list_iterator_create(cluster_list);
        while((cluster = list_next(itr_c))) {
            if (sacctmgr_find_account_base_assoc_from_list(
                        local_assoc_list, name, cluster)) {
                //printf(" already have this assoc\n");
                continue;
            }
            if (!sacctmgr_find_account_base_assoc_from_list(
                        local_assoc_list, start_assoc->parent_acct,
                        cluster)) {
                exit_code=1;
                fprintf(stderr, " Parent account '%s' "
                        "doesn't exist on "
                        "cluster %s\n"
                        "        Contact your admin "
                        "to add this account.\n",
                        start_assoc->parent_acct, cluster);
                continue;
            }

            assoc = xmalloc(sizeof(slurmdb_association_rec_t));
            slurmdb_init_association_rec(assoc, 0);
            assoc->acct = xstrdup(name);
            assoc->cluster = xstrdup(cluster);
            assoc->def_qos_id = start_assoc->def_qos_id;
            assoc->parent_acct = xstrdup(start_assoc->parent_acct);
            assoc->shares_raw = start_assoc->shares_raw;

            assoc->grp_cpu_mins = start_assoc->grp_cpu_mins;
            assoc->grp_cpu_run_mins = start_assoc->grp_cpu_run_mins;
            assoc->grp_cpus = start_assoc->grp_cpus;
            assoc->grp_jobs = start_assoc->grp_jobs;
            assoc->grp_mem = start_assoc->grp_mem;
            assoc->grp_nodes = start_assoc->grp_nodes;
            assoc->grp_submit_jobs = start_assoc->grp_submit_jobs;
            assoc->grp_wall = start_assoc->grp_wall;

            assoc->max_cpu_mins_pj = start_assoc->max_cpu_mins_pj;
            assoc->max_cpus_pj = start_assoc->max_cpus_pj;
            assoc->max_jobs = start_assoc->max_jobs;
            assoc->max_nodes_pj = start_assoc->max_nodes_pj;
            assoc->max_submit_jobs = start_assoc->max_submit_jobs;
            assoc->max_wall_pj = start_assoc->max_wall_pj;

            assoc->qos_list = copy_char_list(start_assoc->qos_list);

            if (acct)
                list_append(acct->assoc_list, assoc);
            else
                list_append(assoc_list, assoc);
            xstrfmtcat(assoc_str,
                       "  A = %-10.10s"
                       " C = %-10.10s\n",
                       assoc->acct,
                       assoc->cluster);

        }
        list_iterator_destroy(itr_c);
    }
    list_iterator_destroy(itr);
    list_destroy(local_account_list);
    list_destroy(local_assoc_list);


    if (!list_count(acct_list) && !list_count(assoc_list)) {
        printf(" Nothing new added.\n");
        rc = SLURM_ERROR;
        goto end_it;
    } else if (!assoc_str) {
        exit_code=1;
        fprintf(stderr, " No associations created.\n");
        goto end_it;
    }

    if (acct_str) {
        printf(" Adding Account(s)\n%s", acct_str);
        printf(" Settings\n");
        if (start_acct->description)
            printf("  Description     = %s\n",
                   start_acct->description);
        else
            printf("  Description     = %s\n", "Account Name");

        if (start_acct->organization)
            printf("  Organization    = %s\n",
                   start_acct->organization);
        else
            printf("  Organization    = %s\n",
                   "Parent/Account Name");

        xfree(acct_str);
    }

    if (assoc_str) {
        printf(" Associations\n%s", assoc_str);
        xfree(assoc_str);
    }

    if (limit_set) {
        printf(" Settings\n");
        sacctmgr_print_assoc_limits(start_assoc);
    }

    notice_thread_init();
    if (list_count(acct_list))
        rc = acct_storage_g_add_accounts(db_conn, my_uid, acct_list);


    if (rc == SLURM_SUCCESS) {
        if (list_count(assoc_list))
            rc = acct_storage_g_add_associations(db_conn, my_uid,
                                                 assoc_list);
    } else {
        exit_code=1;
        fprintf(stderr, " Problem adding accounts: %s\n",
                slurm_strerror(rc));
        rc = SLURM_ERROR;
        notice_thread_fini();
        goto end_it;
    }
    notice_thread_fini();

    if (rc == SLURM_SUCCESS) {
        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);
        }
    } else {
        exit_code=1;
        fprintf(stderr,
                " error: Problem adding account associations: %s\n",
                slurm_strerror(rc));
        rc = SLURM_ERROR;
    }

end_it:
    list_destroy(name_list);
    list_destroy(cluster_list);
    list_destroy(acct_list);
    list_destroy(assoc_list);

    slurmdb_destroy_association_rec(start_assoc);
    slurmdb_destroy_account_rec(start_acct);
    return rc;
}
extern int sacctmgr_modify_cluster(int argc, char *argv[])
{
	int rc = SLURM_SUCCESS;
	int i=0;
	slurmdb_association_rec_t *assoc =
		xmalloc(sizeof(slurmdb_association_rec_t));
	slurmdb_association_cond_t *assoc_cond =
		xmalloc(sizeof(slurmdb_association_cond_t));
	int cond_set = 0, prev_set = 0, rec_set = 0, set = 0;
	List ret_list = NULL;
	uint16_t class_rec = 0;
	slurmdb_cluster_cond_t cluster_cond;

	slurmdb_init_association_rec(assoc, 0);

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

	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, &class_rec);
			rec_set |= prev_set;
		} else {
			prev_set = _set_cond(&i, argc, argv,
					     &cluster_cond, NULL);
			cond_set |= prev_set;
		}
	}

	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;
		}
	} else if(exit_code) {
		rc = SLURM_ERROR;
		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 */
		if(assoc_cond->cluster_list)
			list_destroy(assoc_cond->cluster_list);
		assoc_cond->cluster_list = temp_list;
	}

	printf(" Setting\n");
	if(rec_set) {
		printf(" Default Limits =\n");
		sacctmgr_print_assoc_limits(assoc);
		if(class_rec)
			printf(" Cluster Classification = %s\n",
			       get_classification_str(class_rec));
	}

	list_append(assoc_cond->acct_list, "root");
	notice_thread_init();
	ret_list = acct_storage_g_modify_associations(
		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");
	} else {
		exit_code=1;
		fprintf(stderr, " Error with request: %s\n",
			slurm_strerror(errno));
		rc = SLURM_ERROR;
	}

	if(ret_list)
		list_destroy(ret_list);

	if(class_rec) {
		slurmdb_cluster_rec_t cluster_rec;

		slurmdb_init_cluster_rec(&cluster_rec, 0);
		/* the class has already returned these clusters so
		   just go with it */
		cluster_rec.classification = class_rec;

		ret_list = acct_storage_g_modify_clusters(
			db_conn, my_uid, &cluster_cond, &cluster_rec);

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

		if(ret_list)
			list_destroy(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_association_cond(assoc_cond);
	slurmdb_destroy_association_rec(assoc);

	return rc;
}
Beispiel #10
0
extern int sacctmgr_list_cluster(int argc, char *argv[])
{
	int rc = SLURM_SUCCESS;
	slurmdb_cluster_cond_t *cluster_cond =
		xmalloc(sizeof(slurmdb_cluster_cond_t));
	List cluster_list;
	int i=0;
	ListIterator itr = NULL;
	ListIterator itr2 = NULL;
	slurmdb_cluster_rec_t *cluster = NULL;
	char *tmp_char = NULL;

	int field_count = 0;

	print_field_t *field = NULL;

	List format_list = list_create(slurm_destroy_char);
	List print_fields_list; /* types are of print_field_t */

	slurmdb_init_cluster_cond(cluster_cond, 0);
	cluster_cond->cluster_list = list_create(slurm_destroy_char);
	for (i=0; i<argc; i++) {
		int command_len = strlen(argv[i]);
		if (!strncasecmp(argv[i], "Where", MAX(command_len, 5))
		    || !strncasecmp(argv[i], "Set", MAX(command_len, 3)))
			i++;
		_set_cond(&i, argc, argv, cluster_cond, format_list);
	}

	if(exit_code) {
		slurmdb_destroy_cluster_cond(cluster_cond);
		list_destroy(format_list);
		return SLURM_ERROR;
	}

	if(!list_count(format_list)) {
		slurm_addto_char_list(format_list,
				      "Cl,Controlh,Controlp,RPC");
		if(!without_limits)
			slurm_addto_char_list(format_list,
					      "Fa,GrpJ,GrpN,GrpS,MaxJ,MaxN,"
					      "MaxS,MaxW,QOS,DefaultQOS");
	}

	cluster_cond->with_deleted = with_deleted;

	print_fields_list = sacctmgr_process_format_list(format_list);
	list_destroy(format_list);

	if(exit_code) {
		slurmdb_destroy_cluster_cond(cluster_cond);
		list_destroy(print_fields_list);
		return SLURM_ERROR;
	}

	cluster_list = acct_storage_g_get_clusters(db_conn, my_uid,
						   cluster_cond);
	slurmdb_destroy_cluster_cond(cluster_cond);

	if(!cluster_list) {
		exit_code=1;
		fprintf(stderr, " Problem with query.\n");
		list_destroy(print_fields_list);
		return SLURM_ERROR;
	}

	itr = list_iterator_create(cluster_list);
	itr2 = list_iterator_create(print_fields_list);
	print_fields_header(print_fields_list);

	field_count = list_count(print_fields_list);

	while((cluster = list_next(itr))) {
		int curr_inx = 1;
		slurmdb_association_rec_t *assoc = cluster->root_assoc;
		/* set up the working cluster rec so nodecnt's and node names
		 * are handled correctly */
		working_cluster_rec = cluster;
		while((field = list_next(itr2))) {
			switch(field->type) {
			case PRINT_CLUSTER:
				field->print_routine(field,
						     cluster->name,
						     (curr_inx == field_count));
				break;
			case PRINT_CHOST:
				field->print_routine(field,
						     cluster->control_host,
						     (curr_inx == field_count));
				break;
			case PRINT_CPORT:
				field->print_routine(field,
						     cluster->control_port,
						     (curr_inx == field_count));
				break;
			case PRINT_CLASS:
				field->print_routine(field,
						     get_classification_str(
							     cluster->
							     classification),
						     (curr_inx == field_count));
				break;
			case PRINT_CPUS:
			{
				char tmp_char[9];
				convert_num_unit((float)cluster->cpu_count,
						 tmp_char, sizeof(tmp_char),
						 UNIT_NONE);
				field->print_routine(field,
						     tmp_char,
						     (curr_inx == field_count));
				break;
			}
			case PRINT_DQOS:
				if(!g_qos_list) {
					g_qos_list = acct_storage_g_get_qos(
						db_conn,
						my_uid,
						NULL);
				}
				tmp_char = slurmdb_qos_str(g_qos_list,
							   assoc->def_qos_id);
				field->print_routine(
					field,
					tmp_char,
					(curr_inx == field_count));
				break;
			case PRINT_FAIRSHARE:
				field->print_routine(
					field,
					assoc->shares_raw,
					(curr_inx == field_count));
				break;
			case PRINT_FLAGS:
			{
				char *tmp_char = slurmdb_cluster_flags_2_str(
					cluster->flags);
				field->print_routine(
					field,
					tmp_char,
					(curr_inx == field_count));
				xfree(tmp_char);
				break;
			}
			case PRINT_GRPC:
				field->print_routine(field,
						     assoc->grp_cpus,
						     (curr_inx == field_count));
				break;
			case PRINT_GRPJ:
				field->print_routine(field,
						     assoc->grp_jobs,
						     (curr_inx == field_count));
				break;
			case PRINT_GRPN:
				field->print_routine(field,
						     assoc->grp_nodes,
						     (curr_inx == field_count));
				break;
			case PRINT_GRPS:
				field->print_routine(field,
						     assoc->grp_submit_jobs,
						     (curr_inx == field_count));
				break;
			case PRINT_MAXCM:
				field->print_routine(
					field,
					assoc->max_cpu_mins_pj,
					(curr_inx == field_count));
				break;
			case PRINT_MAXC:
				field->print_routine(field,
						     assoc->max_cpus_pj,
						     (curr_inx == field_count));
				break;
			case PRINT_MAXJ:
				field->print_routine(field,
						     assoc->max_jobs,
						     (curr_inx == field_count));
				break;
			case PRINT_MAXN:
				field->print_routine(field,
						     assoc->max_nodes_pj,
						     (curr_inx == field_count));
				break;
			case PRINT_MAXS:
				field->print_routine(field,
						     assoc->max_submit_jobs,
						     (curr_inx == field_count));
				break;
			case PRINT_MAXW:
				field->print_routine(
					field,
					assoc->max_wall_pj,
					(curr_inx == field_count));
				break;

			case PRINT_NODECNT:
			{
				hostlist_t hl = hostlist_create(cluster->nodes);
				int cnt = 0;
				if(hl) {
					cnt = hostlist_count(hl);
					hostlist_destroy(hl);
				}
				field->print_routine(
					field,
					cnt,
					(curr_inx == field_count));
				break;
			}
			case PRINT_CLUSTER_NODES:
				field->print_routine(
					field,
					cluster->nodes,
					(curr_inx == field_count));
				break;
			case PRINT_QOS:
				if(!g_qos_list)
					g_qos_list = acct_storage_g_get_qos(
						db_conn, my_uid, NULL);

				field->print_routine(field,
						     g_qos_list,
						     assoc->qos_list,
						     (curr_inx == field_count));
				break;
			case PRINT_QOS_RAW:
				field->print_routine(field,
						     assoc->qos_list,
						     (curr_inx == field_count));
				break;
			case PRINT_RPC_VERSION:
				field->print_routine(
					field,
					cluster->rpc_version,
					(curr_inx == field_count));
				break;
			case PRINT_SELECT:
				field->print_routine(
					field,
					cluster->plugin_id_select,
					(curr_inx == field_count));
				break;
			default:
				field->print_routine(
					field, NULL,
					(curr_inx == field_count));
				break;
			}
			curr_inx++;
		}
		list_iterator_reset(itr2);
		printf("\n");
	}
	/* clear the working cluster rec */
	working_cluster_rec = NULL;

	list_iterator_destroy(itr2);
	list_iterator_destroy(itr);
	list_destroy(cluster_list);
	list_destroy(print_fields_list);

	return rc;
}
Beispiel #11
0
static List _process_util_by_report(void *db_conn, char *calling_name,
				    void *cond, cluster_report_t type)
{	ListIterator itr = NULL;
	ListIterator type_itr = NULL;
	slurmdb_cluster_cond_t cluster_cond;
	List type_list = NULL;
	List cluster_list = NULL;
	List first_list = NULL;
	slurmdb_cluster_rec_t *cluster = NULL;
	slurmdb_report_cluster_rec_t *slurmdb_report_cluster = NULL;
	time_t start_time, end_time;

	int exit_code = 0;

	uid_t my_uid = getuid();
	List ret_list = list_create(slurmdb_destroy_report_cluster_rec);

	slurmdb_init_cluster_cond(&cluster_cond, 0);

	cluster_cond.with_deleted = 1;
	cluster_cond.with_usage = 1;
	if ((type == CLUSTER_REPORT_UA) || (type == CLUSTER_REPORT_AU)) {
		start_time = ((slurmdb_assoc_cond_t *)cond)->usage_start;
		end_time = ((slurmdb_assoc_cond_t *)cond)->usage_end;

		cluster_cond.cluster_list =
			((slurmdb_assoc_cond_t *)cond)->cluster_list;
	} else if ((type == CLUSTER_REPORT_UW) || (type == CLUSTER_REPORT_WU)) {
		start_time = ((slurmdb_wckey_cond_t *)cond)->usage_start;
		end_time = ((slurmdb_wckey_cond_t *)cond)->usage_end;

		cluster_cond.cluster_list =
			((slurmdb_wckey_cond_t *)cond)->cluster_list;
	} else {
		error("unknown report type %d", type);
		return NULL;
	}

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


	cluster_list = acct_storage_g_get_clusters(
		db_conn, my_uid, &cluster_cond);

	if (!cluster_list) {
		exit_code=1;
		fprintf(stderr, "%s: Problem with cluster query.\n",
			calling_name);
		goto end_it;
	}

	if ((type == CLUSTER_REPORT_UA) || (type == CLUSTER_REPORT_AU)) {
		((slurmdb_assoc_cond_t *)cond)->usage_start = start_time;
		((slurmdb_assoc_cond_t *)cond)->usage_end = end_time;
		type_list = acct_storage_g_get_assocs(
			db_conn, my_uid, cond);
	} else if ((type == CLUSTER_REPORT_UW) || (type == CLUSTER_REPORT_WU)) {
		((slurmdb_wckey_cond_t *)cond)->usage_start = start_time;
		((slurmdb_wckey_cond_t *)cond)->usage_end = end_time;
		type_list = acct_storage_g_get_wckeys(
			db_conn, my_uid, cond);
	}

	if (!type_list) {
		exit_code=1;
		fprintf(stderr, "%s: Problem with get query.\n", calling_name);
		goto end_it;
	}

	if ((type == CLUSTER_REPORT_UA) || (type == CLUSTER_REPORT_AU)) {
		first_list = type_list;
		type_list = slurmdb_get_hierarchical_sorted_assoc_list(
			first_list, true);
	}

	/* set up the structures for easy retrieval later */
	itr = list_iterator_create(cluster_list);
	type_itr = list_iterator_create(type_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(ret_list, slurmdb_report_cluster);

		if ((type == CLUSTER_REPORT_UA) || (type == CLUSTER_REPORT_UW))
			slurmdb_report_cluster->user_list =
				list_create(slurmdb_destroy_report_user_rec);
		else if ((type == CLUSTER_REPORT_AU)
			|| (type == CLUSTER_REPORT_WU))
			slurmdb_report_cluster->assoc_list =
				list_create(slurmdb_destroy_report_assoc_rec);

		if ((type == CLUSTER_REPORT_UA) || (type == CLUSTER_REPORT_AU))
			_process_assoc_type(type_itr, slurmdb_report_cluster,
					    cluster->name, type);
		else if ((type == CLUSTER_REPORT_UW)
			|| (type == CLUSTER_REPORT_WU))
			_process_wckey_type(type_itr, slurmdb_report_cluster,
					    cluster->name, type);
		list_iterator_reset(type_itr);
	}
	list_iterator_destroy(type_itr);
	list_iterator_destroy(itr);

end_it:
	FREE_NULL_LIST(type_list);
	FREE_NULL_LIST(first_list);
	FREE_NULL_LIST(cluster_list);
	if (exit_code)
		FREE_NULL_LIST(ret_list);
	return ret_list;
}
Beispiel #12
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;
}
Beispiel #13
0
/*
 * get info from the storage
 * IN:  slurmdb_cluster_cond_t *
 * IN:  params void *
 * returns List of slurmdb_cluster_rec_t *
 * note List needs to be freed when called
 */
extern List slurmdb_clusters_get(void *db_conn,
				 slurmdb_cluster_cond_t *cluster_cond)
{
	return acct_storage_g_get_clusters(db_conn, getuid(), cluster_cond);
}