示例#1
0
static int _set_res_cond(int *start, int argc, char *argv[],
			     slurmdb_res_cond_t *res_cond,
			     List format_list)
{
	int i;
	int set = 0;
	int end = 0;
	int command_len = 0;

	if (!res_cond) {
		error("No res_cond given");
		return -1;
	}

	for (i=(*start); i<argc; i++) {
		end = parse_option_end(argv[i]);
		if (!end)
			command_len=strlen(argv[i]);
		else {
			command_len=end-1;
			if (argv[i][end] == '=') {
				end++;
			}
		}

		if (!strncasecmp(argv[i], "Set", MAX(command_len, 3))) {
			i--;
			break;
		} else if (!end && !strncasecmp(argv[i], "WithDeleted",
						 MAX(command_len, 5))) {
			res_cond->with_deleted = 1;
		} else if (!end && !strncasecmp(argv[i], "WithClusters",
						 MAX(command_len, 5))) {
			res_cond->with_clusters = 1;
		} else if (!end && !strncasecmp(argv[i], "where",
						MAX(command_len, 5))) {
			continue;
		} else if (!end
			   || !strncasecmp(argv[i], "Names",
					   MAX(command_len, 1))) {
			if (!res_cond->name_list) {
				res_cond->name_list =
					list_create(slurm_destroy_char);
			}
			if (slurm_addto_char_list(res_cond->name_list,
						  argv[i]+end))
				set = 1;
		} else if (!end
			   || !strncasecmp(argv[i], "Clusters",
					   MAX(command_len, 1))) {
			if (!res_cond->cluster_list) {
				res_cond->cluster_list =
					list_create(slurm_destroy_char);
			}

			slurm_addto_char_list(res_cond->cluster_list,
					      argv[i]+end);
			if (sacctmgr_validate_cluster_list(
				    res_cond->cluster_list) != SLURM_SUCCESS) {
				exit_code=1;
				fprintf(stderr,
					" Need a valid cluster name to "
					"add a cluster resource.\n");
			} else
				set = 1;
		} else if (!strncasecmp(argv[i], "Descriptions",
				MAX(command_len, 1))) {
		if (!res_cond->description_list) {
			res_cond->description_list =
					list_create(slurm_destroy_char);
			}
			if (slurm_addto_char_list(
				    res_cond->description_list,
				    argv[i]+end))
				set = 1;
		} else if (!strncasecmp(argv[i], "Format",
					 MAX(command_len, 1))) {
			if (format_list)
				slurm_addto_char_list(format_list, argv[i]+end);
		} else if (!strncasecmp(argv[i], "Ids", MAX(command_len, 1))) {
			ListIterator itr = NULL;
			char *temp = NULL;
			uint32_t id = 0;

			if (!res_cond->id_list) {
				res_cond->id_list =
					list_create(slurm_destroy_char);
			}
			if (slurm_addto_char_list(res_cond->id_list,
						  argv[i]+end))
				set = 1;

			/* check to make sure user gave ints here */
			itr = list_iterator_create(res_cond->id_list);
			while ((temp = list_next(itr))) {
				if (get_uint(temp, &id, "RES ID")
				    != SLURM_SUCCESS) {
					exit_code = 1;
					list_delete_item(itr);
				}
			}
			list_iterator_destroy(itr);
		} else if (!strncasecmp(argv[i], "Manager",
					 MAX(command_len, 2))) {
			if (!res_cond->manager_list) {
				res_cond->manager_list =
					list_create(slurm_destroy_char);
			}
			if (slurm_addto_char_list(res_cond->manager_list,
						  argv[i]+end))
				set = 1;
		} else if (!strncasecmp(argv[i], "Server",
					 MAX(command_len, 2))) {
			if (!res_cond->server_list) {
				res_cond->server_list =
					list_create(slurm_destroy_char);
			}
			if (slurm_addto_char_list(res_cond->server_list,
						  argv[i]+end))
				set = 1;
		} else {
			exit_code=1;
			fprintf(stderr, " Unknown condition: %s\n"
				" Use keyword 'set' to modify "
				"SLURM_PRINT_VALUE\n", argv[i]);
		}
	}

	(*start) = i;
	return set;
}
示例#2
0
static int _set_res_rec(int *start, int argc, char *argv[],
			List name_list, List cluster_list,
			slurmdb_res_rec_t *res)
{
	int i;
	int set = 0;
	int end = 0;
	int command_len = 0;
	int option = 0;

	xassert(res);

	for (i=(*start); i<argc; i++) {
		end = parse_option_end(argv[i]);
		if (!end)
			command_len=strlen(argv[i]);
		else {
			command_len=end-1;
			if (argv[i][end] == '=') {
				option = (int)argv[i][end-1];
				end++;
			}
		}

		if (!strncasecmp(argv[i], "Where", MAX(command_len, 5))) {
			i--;
			break;
		} else if (!end && !strncasecmp(argv[i], "set",
						MAX(command_len, 3))) {
			continue;
		} else if (!end
			  || !strncasecmp(argv[i], "Names",
					  MAX(command_len, 1))
			   || !strncasecmp(argv[i], "Resources",
					   MAX(command_len, 1))) {
			if (name_list)
				slurm_addto_char_list(name_list, argv[i]+end);
		} else if (!strncasecmp(argv[i], "Clusters",
					 MAX(command_len, 1))) {
			if (cluster_list) {
				slurm_addto_char_list(cluster_list,
						      argv[i]+end);
				if (sacctmgr_validate_cluster_list(
					    cluster_list) != SLURM_SUCCESS) {
					exit_code=1;
					fprintf(stderr,
						" Need a valid cluster name to "
						"add a cluster resource.\n");
				}
			} else {
				exit_code=1;
				fprintf(stderr,
					" Can't modify the cluster "
					"of an resource\n");
			}
		} else if (!strncasecmp(argv[i], "Count",
					MAX(command_len, 3))) {
			if (get_uint(argv[i]+end, &res->count,
				     "count") == SLURM_SUCCESS) {
				set = 1;
			}
		} else if (!strncasecmp(argv[i], "Description",
					 MAX(command_len, 1))) {
			if (!res->description)
				res->description =
					strip_quotes(argv[i]+end, NULL, 1);
			set = 1;
		} else if (!strncasecmp(argv[i], "Flags",
					 MAX(command_len, 2))) {
			res->flags = str_2_res_flags(argv[i]+end, option);
			if (res->flags == SLURMDB_RES_FLAG_NOTSET) {
				char *tmp_char = slurmdb_res_flags_str(
					SLURMDB_RES_FLAG_BASE);
				printf(" Unknown Server Resource flag used "
				       "in:\n  '%s'\n"
				       " Valid Server Resource flags are\n"
				       " '%s'\n", argv[i]+end, tmp_char);
				xfree(tmp_char);
				exit_code = 1;
			} else
				set = 1;
		} else if (!strncasecmp(argv[i], "Manager",
					 MAX(command_len, 1))) {
			if (!res->manager)
				res->manager =
					strip_quotes(argv[i]+end, NULL, 1);
			set = 1;
		} else if (!strncasecmp(argv[i], "PercentAllowed",
					MAX(command_len, 1))) {
			/* overload percent_used here */
			if (get_uint16(argv[i]+end, &res->percent_used,
				       "PercentAllowed") == SLURM_SUCCESS) {
				set = 1;
			}
		} else if (!strncasecmp(argv[i], "Server",
					  MAX(command_len, 1))) {
			if (!res->server) {
				res->server=
					strip_quotes(argv[i]+end, NULL, 1);
			}
			set = 1;
		} else if (!strncasecmp(argv[i], "Type",
					MAX(command_len, 1))) {
			char *temp = strip_quotes(argv[i]+end, NULL, 1);

			if (!strncasecmp("License", temp,
					 MAX(strlen(temp), 1))) {
				res->type = SLURMDB_RESOURCE_LICENSE;
			} else {
				exit_code=1;
				fprintf(stderr,
					" Unknown resource type: '%s'  "
					"Valid resources is License.\n",
					temp);
			}
		} else {
			exit_code = 1;
			printf(" Unknown option: %s\n"
			       " Use keyword 'where' to modify condition\n",
			       argv[i]);
		}
	}

	(*start) = i;

	return set;
}
示例#3
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_assoc_rec_t *assoc = NULL;
	slurmdb_assoc_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_assoc_rec_t *start_assoc =
		xmalloc(sizeof(slurmdb_assoc_rec_t));

	slurmdb_init_assoc_rec(start_assoc, 0);

	for (i = 0; i < argc; i++) {
		int command_len = strlen(argv[i]);
		if (!xstrncasecmp(argv[i], "Where", MAX(command_len, 5))
		    || !xstrncasecmp(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) {
		slurmdb_destroy_assoc_rec(start_assoc);
		slurmdb_destroy_account_rec(start_acct);
		return SLURM_ERROR;
	}

	if (!name_list || !list_count(name_list)) {
		FREE_NULL_LIST(name_list);
		FREE_NULL_LIST(cluster_list);
		slurmdb_destroy_assoc_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_assoc_cond_t));

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

		local_account_list = slurmdb_accounts_get(
			db_conn, &account_cond);
	}

	if (!local_account_list) {
		exit_code = 1;
		fprintf(stderr, " Problem getting accounts from database.  "
			"Contact your admin.\n");
		FREE_NULL_LIST(name_list);
		FREE_NULL_LIST(cluster_list);
		slurmdb_destroy_assoc_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 =
			slurmdb_clusters_get(db_conn, NULL);
		if (!tmp_list) {
			exit_code=1;
			fprintf(stderr,
				" Problem getting clusters from database.  "
				"Contact your admin.\n");
			FREE_NULL_LIST(name_list);
			FREE_NULL_LIST(cluster_list);
			slurmdb_destroy_assoc_rec(start_assoc);
			slurmdb_destroy_account_rec(start_acct);
			FREE_NULL_LIST(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");
			FREE_NULL_LIST(name_list);
			FREE_NULL_LIST(cluster_list);
			slurmdb_destroy_assoc_rec(start_assoc);
			slurmdb_destroy_account_rec(start_acct);
			FREE_NULL_LIST(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);
		FREE_NULL_LIST(tmp_list);
	} else if (sacctmgr_validate_cluster_list(cluster_list)
		   != SLURM_SUCCESS) {
		slurmdb_destroy_assoc_rec(start_assoc);
		slurmdb_destroy_account_rec(start_acct);
		FREE_NULL_LIST(local_account_list);

		return SLURM_ERROR;
	}


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

	memset(&assoc_cond, 0, sizeof(slurmdb_assoc_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 = slurmdb_associations_get(
		db_conn, &assoc_cond);
	FREE_NULL_LIST(assoc_cond.acct_list);
	if (!local_assoc_list) {
		exit_code=1;
		fprintf(stderr, " Problem getting associations from database.  "
			"Contact your admin.\n");
		FREE_NULL_LIST(name_list);
		FREE_NULL_LIST(cluster_list);
		slurmdb_destroy_assoc_rec(start_assoc);
		slurmdb_destroy_account_rec(start_acct);
		FREE_NULL_LIST(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_assoc_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 (xstrcmp(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_assoc_rec_t));
			slurmdb_init_assoc_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;

			slurmdb_copy_assoc_rec_limits(assoc, start_assoc);

			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);
	FREE_NULL_LIST(local_account_list);
	FREE_NULL_LIST(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 = slurmdb_accounts_add(db_conn, acct_list);


	if (rc == SLURM_SUCCESS) {
		if (list_count(assoc_list))
			rc = slurmdb_associations_add(db_conn, 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?")) {
			slurmdb_connection_commit(db_conn, 1);
		} else {
			printf(" Changes Discarded\n");
			slurmdb_connection_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:
	FREE_NULL_LIST(name_list);
	FREE_NULL_LIST(cluster_list);
	FREE_NULL_LIST(acct_list);
	FREE_NULL_LIST(assoc_list);

	slurmdb_destroy_assoc_rec(start_assoc);
	slurmdb_destroy_account_rec(start_acct);
	return rc;
}