PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/src/sacctmgr/user_functions.c

https://github.com/cfenoy/slurm
C | 2184 lines | 1837 code | 216 blank | 131 comment | 423 complexity | 623336ee72ea0be1dc86afe2a6cf0a7d MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. /*****************************************************************************\
  2. * user_functions.c - functions dealing with users in the accounting system.
  3. *****************************************************************************
  4. * Copyright (C) 2008 Lawrence Livermore National Security.
  5. * Copyright (C) 2002-2007 The Regents of the University of California.
  6. * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
  7. * Written by Danny Auble <da@llnl.gov>
  8. * CODE-OCEC-09-009. All rights reserved.
  9. *
  10. * This file is part of SLURM, a resource management program.
  11. * For details, see <http://www.schedmd.com/slurmdocs/>.
  12. * Please also read the included file: DISCLAIMER.
  13. *
  14. * SLURM is free software; you can redistribute it and/or modify it under
  15. * the terms of the GNU General Public License as published by the Free
  16. * Software Foundation; either version 2 of the License, or (at your option)
  17. * any later version.
  18. *
  19. * In addition, as a special exception, the copyright holders give permission
  20. * to link the code of portions of this program with the OpenSSL library under
  21. * certain conditions as described in each individual source file, and
  22. * distribute linked combinations including the two. You must obey the GNU
  23. * General Public License in all respects for all of the code used other than
  24. * OpenSSL. If you modify file(s) with this exception, you may extend this
  25. * exception to your version of the file(s), but you are not obligated to do
  26. * so. If you do not wish to do so, delete this exception statement from your
  27. * version. If you delete this exception statement from all source files in
  28. * the program, then also delete it here.
  29. *
  30. * SLURM is distributed in the hope that it will be useful, but WITHOUT ANY
  31. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  32. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  33. * details.
  34. *
  35. * You should have received a copy of the GNU General Public License along
  36. * with SLURM; if not, write to the Free Software Foundation, Inc.,
  37. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  38. \*****************************************************************************/
  39. #include "src/sacctmgr/sacctmgr.h"
  40. #include "src/common/assoc_mgr.h"
  41. #include "src/common/uid.h"
  42. #include "src/common/slurm_strcasestr.h"
  43. typedef struct {
  44. char *cluster;
  45. char *user;
  46. } regret_t;
  47. static int _set_cond(int *start, int argc, char *argv[],
  48. slurmdb_user_cond_t *user_cond,
  49. List format_list)
  50. {
  51. int i;
  52. int u_set = 0;
  53. int a_set = 0;
  54. int end = 0;
  55. slurmdb_association_cond_t *assoc_cond = NULL;
  56. int command_len = 0;
  57. int option = 0;
  58. if(!user_cond) {
  59. error("No user_cond given");
  60. return -1;
  61. }
  62. if (!user_cond->assoc_cond)
  63. user_cond->assoc_cond =
  64. xmalloc(sizeof(slurmdb_association_cond_t));
  65. assoc_cond = user_cond->assoc_cond;
  66. /* we need this to make sure we only change users, not
  67. * accounts if this list didn't exist it would change
  68. * accounts. Having it blank is fine, it just needs to
  69. * exist.
  70. */
  71. if(!assoc_cond->user_list)
  72. assoc_cond->user_list = list_create(slurm_destroy_char);
  73. for (i=(*start); i<argc; i++) {
  74. end = parse_option_end(argv[i]);
  75. if(!end)
  76. command_len=strlen(argv[i]);
  77. else {
  78. command_len=end-1;
  79. if(argv[i][end] == '=') {
  80. option = (int)argv[i][end-1];
  81. end++;
  82. }
  83. }
  84. if (!strncasecmp(argv[i], "Set", MAX(command_len, 3))) {
  85. i--;
  86. break;
  87. } else if (!end && !strncasecmp(argv[i], "WithAssoc",
  88. MAX(command_len, 5))) {
  89. user_cond->with_assocs = 1;
  90. } else if (!end &&
  91. !strncasecmp(argv[i], "WithCoordinators",
  92. MAX(command_len, 5))) {
  93. user_cond->with_coords = 1;
  94. } else if (!end &&
  95. !strncasecmp(argv[i], "WithDeleted",
  96. MAX(command_len, 5))) {
  97. user_cond->with_deleted = 1;
  98. assoc_cond->with_deleted = 1;
  99. } else if (!end &&
  100. !strncasecmp(argv[i], "WithRawQOSLevel",
  101. MAX(command_len, 5))) {
  102. assoc_cond->with_raw_qos = 1;
  103. } else if (!end && !strncasecmp(argv[i], "WOPLimits",
  104. MAX(command_len, 4))) {
  105. assoc_cond->without_parent_limits = 1;
  106. } else if(!end && !strncasecmp(argv[i], "where",
  107. MAX(command_len, 5))) {
  108. continue;
  109. } else if(!end
  110. || !strncasecmp(argv[i], "Names",
  111. MAX(command_len, 1))
  112. || !strncasecmp(argv[i], "Users",
  113. MAX(command_len, 1))) {
  114. if(slurm_addto_char_list(assoc_cond->user_list,
  115. argv[i]+end))
  116. u_set = 1;
  117. else
  118. exit_code=1;
  119. } else if (!strncasecmp(argv[i], "AdminLevel",
  120. MAX(command_len, 2))) {
  121. user_cond->admin_level =
  122. str_2_slurmdb_admin_level(argv[i]+end);
  123. u_set = 1;
  124. } else if (!strncasecmp(argv[i], "Clusters",
  125. MAX(command_len, 1))) {
  126. if (!assoc_cond->cluster_list)
  127. assoc_cond->cluster_list =
  128. list_create(slurm_destroy_char);
  129. if (slurm_addto_char_list(assoc_cond->cluster_list,
  130. argv[i]+end)) {
  131. u_set = 1;
  132. /* Don't set a_set here, it is only
  133. needed for deleting user and it is
  134. handled there later.
  135. */
  136. }
  137. } else if (!strncasecmp(argv[i], "DefaultAccount",
  138. MAX(command_len, 8))) {
  139. if(!user_cond->def_acct_list) {
  140. user_cond->def_acct_list =
  141. list_create(slurm_destroy_char);
  142. }
  143. if(slurm_addto_char_list(user_cond->def_acct_list,
  144. argv[i]+end))
  145. u_set = 1;
  146. else
  147. exit_code=1;
  148. } else if (!strncasecmp(argv[i], "DefaultWCKey",
  149. MAX(command_len, 8))) {
  150. if(!user_cond->def_wckey_list) {
  151. user_cond->def_wckey_list =
  152. list_create(slurm_destroy_char);
  153. }
  154. if(slurm_addto_char_list(user_cond->def_wckey_list,
  155. argv[i]+end))
  156. u_set = 1;
  157. else
  158. exit_code=1;
  159. } else if (!strncasecmp(argv[i], "Format",
  160. MAX(command_len, 1))) {
  161. if(format_list) {
  162. /* We need this to get the defaults. (Usually
  163. * only for the calling cluster) */
  164. if (slurm_strcasestr(argv[i]+end, "default"))
  165. assoc_cond->only_defs = 1;
  166. slurm_addto_char_list(format_list, argv[i]+end);
  167. }
  168. } else if(!(a_set = sacctmgr_set_association_cond(
  169. assoc_cond, argv[i], argv[i]+end,
  170. command_len, option))) {
  171. exit_code=1;
  172. fprintf(stderr, " Unknown condition: %s\n"
  173. " Use keyword 'set' to modify value\n",
  174. argv[i]);
  175. }
  176. }
  177. (*start) = i;
  178. if(u_set && a_set)
  179. return 3;
  180. else if(a_set) {
  181. return 2;
  182. } else if(u_set)
  183. return 1;
  184. return 0;
  185. }
  186. static int _set_rec(int *start, int argc, char *argv[],
  187. slurmdb_user_rec_t *user,
  188. slurmdb_association_rec_t *assoc)
  189. {
  190. int i;
  191. int u_set = 0;
  192. int a_set = 0;
  193. int end = 0;
  194. int command_len = 0;
  195. int option = 0;
  196. for (i=(*start); i<argc; i++) {
  197. end = parse_option_end(argv[i]);
  198. if(!end)
  199. command_len=strlen(argv[i]);
  200. else {
  201. command_len=end-1;
  202. if(argv[i][end] == '=') {
  203. option = (int)argv[i][end-1];
  204. end++;
  205. }
  206. }
  207. if (!strncasecmp(argv[i], "Where", MAX(command_len, 5))) {
  208. i--;
  209. break;
  210. } else if(!end && !strncasecmp(argv[i], "set",
  211. MAX(command_len, 3))) {
  212. continue;
  213. } else if(!end) {
  214. exit_code=1;
  215. fprintf(stderr,
  216. " Bad format on %s: End your option with "
  217. "an '=' sign\n", argv[i]);
  218. } else if (!strncasecmp(argv[i], "AdminLevel",
  219. MAX(command_len, 2))) {
  220. user->admin_level =
  221. str_2_slurmdb_admin_level(argv[i]+end);
  222. u_set = 1;
  223. } else if (!strncasecmp(argv[i], "DefaultAccount",
  224. MAX(command_len, 8))) {
  225. if(user->default_acct)
  226. xfree(user->default_acct);
  227. user->default_acct = strip_quotes(argv[i]+end, NULL, 1);
  228. u_set = 1;
  229. } else if (!strncasecmp(argv[i], "DefaultWCKey",
  230. MAX(command_len, 8))) {
  231. if(user->default_wckey)
  232. xfree(user->default_wckey);
  233. user->default_wckey =
  234. strip_quotes(argv[i]+end, NULL, 1);
  235. u_set = 1;
  236. } else if (!strncasecmp(argv[i], "NewName",
  237. MAX(command_len, 1))) {
  238. if(user->name)
  239. xfree(user->name);
  240. user->name = strip_quotes(argv[i]+end, NULL, 1);
  241. u_set = 1;
  242. } else if (!strncasecmp (argv[i], "RawUsage",
  243. MAX(command_len, 7))) {
  244. uint32_t usage;
  245. if(!assoc)
  246. continue;
  247. assoc->usage = xmalloc(sizeof(
  248. assoc_mgr_association_usage_t));
  249. if (get_uint(argv[i]+end, &usage,
  250. "RawUsage") == SLURM_SUCCESS) {
  251. assoc->usage->usage_raw = usage;
  252. a_set = 1;
  253. }
  254. } else if(!assoc ||
  255. (assoc && !(a_set = sacctmgr_set_association_rec(
  256. assoc, argv[i], argv[i]+end,
  257. command_len, option)))) {
  258. exit_code=1;
  259. fprintf(stderr, " Unknown option: %s\n"
  260. " Use keyword 'where' to modify condition\n",
  261. argv[i]);
  262. }
  263. }
  264. (*start) = i;
  265. if(u_set && a_set)
  266. return 3;
  267. else if(u_set)
  268. return 1;
  269. else if(a_set)
  270. return 2;
  271. return 0;
  272. }
  273. static int _check_and_set_cluster_list(List cluster_list)
  274. {
  275. int rc = SLURM_SUCCESS;
  276. List tmp_list = NULL;
  277. ListIterator itr_c;
  278. slurmdb_cluster_rec_t *cluster_rec = NULL;
  279. xassert(cluster_list);
  280. if (list_count(cluster_list))
  281. return rc;
  282. tmp_list = acct_storage_g_get_clusters(db_conn, my_uid, NULL);
  283. if (!tmp_list) {
  284. exit_code=1;
  285. fprintf(stderr,
  286. " Problem getting clusters from database. "
  287. "Contact your admin.\n");
  288. return SLURM_ERROR;
  289. }
  290. itr_c = list_iterator_create(tmp_list);
  291. while ((cluster_rec = list_next(itr_c))) {
  292. list_append(cluster_list, cluster_rec->name);
  293. cluster_rec->name = NULL;
  294. }
  295. list_iterator_destroy(itr_c);
  296. list_destroy(tmp_list);
  297. if (!list_count(cluster_list)) {
  298. exit_code=1;
  299. fprintf(stderr,
  300. " Can't add/modify users, no cluster defined yet.\n"
  301. " Please contact your administrator.\n");
  302. return SLURM_ERROR;
  303. }
  304. return rc;
  305. }
  306. static int _check_default_associations(char *def_acct,
  307. List user_list, List cluster_list)
  308. {
  309. char *user = NULL, *cluster = NULL;
  310. List regret_list = NULL;
  311. List local_assoc_list = NULL;
  312. ListIterator itr = NULL;
  313. ListIterator itr_c = NULL;
  314. regret_t *regret = NULL;
  315. slurmdb_association_cond_t assoc_cond;
  316. int rc = SLURM_SUCCESS;
  317. if (!def_acct)
  318. return rc;
  319. xassert(user_list);
  320. xassert(cluster_list);
  321. if (!list_count(user_list) || !list_count(cluster_list))
  322. return SLURM_ERROR;
  323. memset(&assoc_cond, 0, sizeof(slurmdb_association_cond_t));
  324. assoc_cond.user_list = user_list;
  325. assoc_cond.cluster_list = cluster_list;
  326. assoc_cond.acct_list = list_create(NULL);
  327. list_append(assoc_cond.acct_list, def_acct);
  328. local_assoc_list = acct_storage_g_get_associations(
  329. db_conn, my_uid, &assoc_cond);
  330. list_destroy(assoc_cond.acct_list);
  331. itr = list_iterator_create(user_list);
  332. itr_c = list_iterator_create(cluster_list);
  333. /* We have to check here for the user names to
  334. * make sure the user has an association with
  335. * the new default account. We have to wait
  336. * until we get the ret_list of names since
  337. * names are not required to change a user since
  338. * you can specify a user by something else
  339. * like default_account or something. If the
  340. * user doesn't have the account make
  341. * note of it.
  342. */
  343. while((user = list_next(itr))) {
  344. while((cluster = list_next(itr_c))) {
  345. if(!sacctmgr_find_association_from_list(
  346. local_assoc_list,
  347. user, def_acct, cluster, "*")) {
  348. regret = xmalloc(sizeof(regret_t));
  349. regret->user = user;
  350. regret->cluster = cluster;
  351. /* slurm_destroy_char just does an
  352. xfree so we can override it here
  353. since we aren't allocating any
  354. extra memory */
  355. if(!regret_list)
  356. regret_list =
  357. list_create(slurm_destroy_char);
  358. list_append(regret_list, regret);
  359. continue;
  360. }
  361. }
  362. list_iterator_reset(itr_c);
  363. }
  364. list_iterator_destroy(itr);
  365. list_iterator_destroy(itr_c);
  366. list_destroy(local_assoc_list);
  367. if(regret_list) {
  368. itr = list_iterator_create(regret_list);
  369. printf(" Can't modify because these users "
  370. "aren't associated with new "
  371. "default account '%s'...\n",
  372. def_acct);
  373. while((regret = list_next(itr))) {
  374. printf(" U = %s C = %s\n",
  375. regret->user, regret->cluster);
  376. }
  377. list_iterator_destroy(itr);
  378. exit_code=1;
  379. rc = SLURM_ERROR;
  380. list_destroy(regret_list);
  381. }
  382. return rc;
  383. }
  384. static int _check_default_wckeys(char *def_wckey,
  385. List user_list, List cluster_list)
  386. {
  387. char *user = NULL, *cluster = NULL;
  388. List regret_list = NULL;
  389. List local_wckey_list = NULL;
  390. ListIterator itr = NULL;
  391. ListIterator itr_c = NULL;
  392. regret_t *regret = NULL;
  393. slurmdb_wckey_cond_t wckey_cond;
  394. int rc = SLURM_SUCCESS;
  395. if (!def_wckey)
  396. return rc;
  397. xassert(user_list);
  398. xassert(cluster_list);
  399. if (!list_count(user_list) || !list_count(cluster_list))
  400. return SLURM_ERROR;
  401. memset(&wckey_cond, 0, sizeof(slurmdb_wckey_cond_t));
  402. wckey_cond.user_list = user_list;
  403. wckey_cond.cluster_list = cluster_list;
  404. wckey_cond.name_list = list_create(NULL);
  405. list_append(wckey_cond.name_list, def_wckey);
  406. local_wckey_list = acct_storage_g_get_wckeys(
  407. db_conn, my_uid, &wckey_cond);
  408. list_destroy(wckey_cond.name_list);
  409. itr = list_iterator_create(user_list);
  410. itr_c = list_iterator_create(cluster_list);
  411. /* We have to check here for the user names to
  412. * make sure the user has an wckey with
  413. * the new default wckey. We have to wait
  414. * until we get the ret_list of names since
  415. * names are not required to change a user since
  416. * you can specify a user by something else
  417. * like default_account or something. If the
  418. * user doesn't have the account make
  419. * note of it.
  420. */
  421. while((user = list_next(itr))) {
  422. while((cluster = list_next(itr_c))) {
  423. if(!sacctmgr_find_wckey_from_list(
  424. local_wckey_list,
  425. user, def_wckey, cluster)) {
  426. regret = xmalloc(sizeof(regret_t));
  427. regret->user = user;
  428. regret->cluster = cluster;
  429. /* slurm_destroy_char just does an
  430. xfree so we can override it here
  431. since we aren't allocating any
  432. extra memory */
  433. if(!regret_list)
  434. regret_list =
  435. list_create(slurm_destroy_char);
  436. list_append(regret_list, regret);
  437. continue;
  438. }
  439. }
  440. list_iterator_reset(itr_c);
  441. }
  442. list_iterator_destroy(itr);
  443. list_iterator_destroy(itr_c);
  444. list_destroy(local_wckey_list);
  445. if(regret_list) {
  446. itr = list_iterator_create(regret_list);
  447. printf(" Can't modify because these users "
  448. "aren't associated with new "
  449. "default wckey '%s'...\n",
  450. def_wckey);
  451. while((regret = list_next(itr))) {
  452. printf(" U = %s C = %s\n",
  453. regret->user, regret->cluster);
  454. }
  455. list_iterator_destroy(itr);
  456. exit_code=1;
  457. rc = SLURM_ERROR;
  458. list_destroy(regret_list);
  459. }
  460. return rc;
  461. }
  462. /*
  463. * IN: user_cond - used for the assoc_cond pointing to the user and
  464. * account list
  465. * IN: check - whether or not to check if the existance of the above lists
  466. */
  467. static int _check_coord_request(slurmdb_user_cond_t *user_cond, bool check)
  468. {
  469. ListIterator itr = NULL, itr2 = NULL;
  470. char *name = NULL;
  471. slurmdb_user_rec_t *user_rec = NULL;
  472. slurmdb_account_rec_t *acct_rec = NULL;
  473. slurmdb_account_cond_t account_cond;
  474. List local_acct_list = NULL;
  475. List local_user_list = NULL;
  476. int rc = SLURM_SUCCESS;
  477. if(!user_cond) {
  478. exit_code=1;
  479. fprintf(stderr, " You need to specify the user_cond here.\n");
  480. return SLURM_ERROR;
  481. }
  482. if(check && (!user_cond->assoc_cond->user_list
  483. || !list_count(user_cond->assoc_cond->user_list))) {
  484. exit_code=1;
  485. fprintf(stderr, " You need to specify a user list here.\n");
  486. return SLURM_ERROR;
  487. }
  488. if(check && (!user_cond->assoc_cond->acct_list
  489. || !list_count(user_cond->assoc_cond->acct_list))) {
  490. exit_code=1;
  491. fprintf(stderr, " You need to specify an account list here.\n");
  492. return SLURM_ERROR;
  493. }
  494. memset(&account_cond, 0, sizeof(slurmdb_account_cond_t));
  495. account_cond.assoc_cond = user_cond->assoc_cond;
  496. local_acct_list =
  497. acct_storage_g_get_accounts(db_conn, my_uid, &account_cond);
  498. if(!local_acct_list) {
  499. exit_code=1;
  500. fprintf(stderr, " Problem getting accounts from database. "
  501. "Contact your admin.\n");
  502. return SLURM_ERROR;
  503. }
  504. if(user_cond->assoc_cond->acct_list &&
  505. (list_count(local_acct_list) !=
  506. list_count(user_cond->assoc_cond->acct_list))) {
  507. itr = list_iterator_create(user_cond->assoc_cond->acct_list);
  508. itr2 = list_iterator_create(local_acct_list);
  509. while((name = list_next(itr))) {
  510. while((acct_rec = list_next(itr2))) {
  511. if(!strcmp(name, acct_rec->name))
  512. break;
  513. }
  514. list_iterator_reset(itr2);
  515. if(!acct_rec) {
  516. fprintf(stderr,
  517. " You specified a non-existant "
  518. "account '%s'.\n", name);
  519. exit_code=1;
  520. rc = SLURM_ERROR;
  521. }
  522. }
  523. list_iterator_destroy(itr);
  524. list_iterator_destroy(itr2);
  525. }
  526. local_user_list = acct_storage_g_get_users(db_conn, my_uid, user_cond);
  527. if(!local_user_list) {
  528. exit_code=1;
  529. fprintf(stderr, " Problem getting users from database. "
  530. "Contact your admin.\n");
  531. if(local_acct_list)
  532. list_destroy(local_acct_list);
  533. return SLURM_ERROR;
  534. }
  535. if(user_cond->assoc_cond->user_list &&
  536. (list_count(local_user_list) !=
  537. list_count(user_cond->assoc_cond->user_list))) {
  538. itr = list_iterator_create(user_cond->assoc_cond->user_list);
  539. itr2 = list_iterator_create(local_user_list);
  540. while((name = list_next(itr))) {
  541. while((user_rec = list_next(itr2))) {
  542. if(!strcmp(name, user_rec->name))
  543. break;
  544. }
  545. list_iterator_reset(itr2);
  546. if(!user_rec) {
  547. fprintf(stderr,
  548. " You specified a non-existant "
  549. "user '%s'.\n", name);
  550. exit_code=1;
  551. rc = SLURM_ERROR;
  552. }
  553. }
  554. list_iterator_destroy(itr);
  555. list_iterator_destroy(itr2);
  556. }
  557. if(local_acct_list)
  558. list_destroy(local_acct_list);
  559. if(local_user_list)
  560. list_destroy(local_user_list);
  561. return rc;
  562. }
  563. extern int sacctmgr_add_user(int argc, char *argv[])
  564. {
  565. int rc = SLURM_SUCCESS;
  566. int i=0;
  567. ListIterator itr = NULL;
  568. ListIterator itr_a = NULL;
  569. ListIterator itr_c = NULL;
  570. ListIterator itr_p = NULL;
  571. ListIterator itr_w = NULL;
  572. slurmdb_user_rec_t *user = NULL;
  573. slurmdb_association_rec_t *assoc = NULL;
  574. slurmdb_association_rec_t start_assoc;
  575. char *default_acct = NULL;
  576. char *default_wckey = NULL;
  577. slurmdb_association_cond_t *assoc_cond = NULL;
  578. slurmdb_wckey_rec_t *wckey = NULL;
  579. slurmdb_wckey_cond_t *wckey_cond = NULL;
  580. slurmdb_admin_level_t admin_level = SLURMDB_ADMIN_NOTSET;
  581. char *name = NULL, *account = NULL, *cluster = NULL, *partition = NULL;
  582. int partition_set = 0;
  583. List user_list = NULL;
  584. List assoc_list = NULL;
  585. List wckey_list = NULL;
  586. List local_assoc_list = NULL;
  587. List local_acct_list = NULL;
  588. List local_user_list = NULL;
  589. List local_wckey_list = NULL;
  590. char *user_str = NULL;
  591. char *assoc_str = NULL;
  592. char *wckey_str = NULL;
  593. int limit_set = 0;
  594. int first = 1;
  595. int acct_first = 1;
  596. int command_len = 0;
  597. int option = 0;
  598. uint16_t track_wckey = slurm_get_track_wckey();
  599. /* if(!list_count(sacctmgr_cluster_list)) { */
  600. /* printf(" Can't add users, no cluster defined yet.\n" */
  601. /* " Please contact your administrator.\n"); */
  602. /* return SLURM_ERROR; */
  603. /* } */
  604. slurmdb_init_association_rec(&start_assoc, 0);
  605. assoc_cond = xmalloc(sizeof(slurmdb_association_cond_t));
  606. assoc_cond->user_list = list_create(slurm_destroy_char);
  607. assoc_cond->acct_list = list_create(slurm_destroy_char);
  608. assoc_cond->cluster_list = list_create(slurm_destroy_char);
  609. assoc_cond->partition_list = list_create(slurm_destroy_char);
  610. wckey_cond = xmalloc(sizeof(slurmdb_wckey_cond_t));
  611. wckey_cond->name_list = list_create(slurm_destroy_char);
  612. for (i=0; i<argc; i++) {
  613. int end = parse_option_end(argv[i]);
  614. if(!end)
  615. command_len=strlen(argv[i]);
  616. else {
  617. command_len=end-1;
  618. if(argv[i][end] == '=') {
  619. option = (int)argv[i][end-1];
  620. end++;
  621. }
  622. }
  623. if(!end
  624. || !strncasecmp(argv[i], "Names", MAX(command_len, 1))
  625. || !strncasecmp(argv[i], "Users", MAX(command_len, 1))) {
  626. if(!slurm_addto_char_list(assoc_cond->user_list,
  627. argv[i]+end))
  628. exit_code=1;
  629. } else if (!strncasecmp(argv[i], "AdminLevel",
  630. MAX(command_len, 2))) {
  631. admin_level = str_2_slurmdb_admin_level(argv[i]+end);
  632. } else if (!strncasecmp(argv[i], "DefaultAccount",
  633. MAX(command_len, 8))) {
  634. if(default_acct) {
  635. fprintf(stderr,
  636. " Already listed DefaultAccount %s\n",
  637. default_acct);
  638. exit_code = 1;
  639. continue;
  640. }
  641. default_acct = strip_quotes(argv[i]+end, NULL, 1);
  642. slurm_addto_char_list(assoc_cond->acct_list,
  643. default_acct);
  644. } else if (!strncasecmp(argv[i], "DefaultWCKey",
  645. MAX(command_len, 8))) {
  646. if(default_wckey) {
  647. fprintf(stderr,
  648. " Already listed DefaultWCKey %s\n",
  649. default_wckey);
  650. exit_code = 1;
  651. continue;
  652. }
  653. default_wckey = strip_quotes(argv[i]+end, NULL, 1);
  654. slurm_addto_char_list(wckey_cond->name_list,
  655. default_wckey);
  656. } else if (!strncasecmp(argv[i], "WCKeys",
  657. MAX(command_len, 1))) {
  658. slurm_addto_char_list(wckey_cond->name_list,
  659. argv[i]+end);
  660. } else if(!(limit_set = sacctmgr_set_association_rec(
  661. &start_assoc, argv[i], argv[i]+end,
  662. command_len, option))
  663. && !(limit_set = sacctmgr_set_association_cond(
  664. assoc_cond, argv[i], argv[i]+end,
  665. command_len, option))) {
  666. exit_code=1;
  667. fprintf(stderr, " Unknown option: %s\n", argv[i]);
  668. }
  669. }
  670. if(exit_code) {
  671. slurmdb_destroy_wckey_cond(wckey_cond);
  672. slurmdb_destroy_association_cond(assoc_cond);
  673. return SLURM_ERROR;
  674. } else if(!list_count(assoc_cond->user_list)) {
  675. slurmdb_destroy_wckey_cond(wckey_cond);
  676. slurmdb_destroy_association_cond(assoc_cond);
  677. exit_code=1;
  678. fprintf(stderr, " Need name of user to add.\n");
  679. return SLURM_ERROR;
  680. } else {
  681. slurmdb_user_cond_t user_cond;
  682. slurmdb_association_cond_t temp_assoc_cond;
  683. memset(&user_cond, 0, sizeof(slurmdb_user_cond_t));
  684. memset(&temp_assoc_cond, 0, sizeof(slurmdb_association_cond_t));
  685. user_cond.with_wckeys = 1;
  686. user_cond.with_assocs = 1;
  687. temp_assoc_cond.only_defs = 1;
  688. temp_assoc_cond.user_list = assoc_cond->user_list;
  689. user_cond.assoc_cond = &temp_assoc_cond;
  690. local_user_list = acct_storage_g_get_users(
  691. db_conn, my_uid, &user_cond);
  692. }
  693. if(!local_user_list) {
  694. exit_code=1;
  695. fprintf(stderr, " Problem getting users from database. "
  696. "Contact your admin.\n");
  697. slurmdb_destroy_wckey_cond(wckey_cond);
  698. slurmdb_destroy_association_cond(assoc_cond);
  699. return SLURM_ERROR;
  700. }
  701. if(!list_count(assoc_cond->cluster_list)) {
  702. if (_check_and_set_cluster_list(assoc_cond->cluster_list)
  703. != SLURM_SUCCESS) {
  704. slurmdb_destroy_wckey_cond(wckey_cond);
  705. slurmdb_destroy_association_cond(assoc_cond);
  706. list_destroy(local_user_list);
  707. if(local_acct_list)
  708. list_destroy(local_acct_list);
  709. return SLURM_ERROR;
  710. }
  711. } else {
  712. List temp_list = NULL;
  713. slurmdb_cluster_cond_t cluster_cond;
  714. slurmdb_init_cluster_cond(&cluster_cond, 0);
  715. cluster_cond.cluster_list = assoc_cond->cluster_list;
  716. temp_list = acct_storage_g_get_clusters(db_conn, my_uid,
  717. &cluster_cond);
  718. itr_c = list_iterator_create(assoc_cond->cluster_list);
  719. itr = list_iterator_create(temp_list);
  720. while((cluster = list_next(itr_c))) {
  721. slurmdb_cluster_rec_t *cluster_rec = NULL;
  722. list_iterator_reset(itr);
  723. while((cluster_rec = list_next(itr))) {
  724. if(!strcasecmp(cluster_rec->name, cluster))
  725. break;
  726. }
  727. if(!cluster_rec) {
  728. exit_code=1;
  729. fprintf(stderr, " This cluster '%s' "
  730. "doesn't exist.\n"
  731. " Contact your admin "
  732. "to add it to accounting.\n",
  733. cluster);
  734. list_delete_item(itr_c);
  735. }
  736. }
  737. list_iterator_destroy(itr);
  738. list_iterator_destroy(itr_c);
  739. list_destroy(temp_list);
  740. if(!list_count(assoc_cond->cluster_list)) {
  741. slurmdb_destroy_wckey_cond(wckey_cond);
  742. slurmdb_destroy_association_cond(assoc_cond);
  743. list_destroy(local_user_list);
  744. if(local_acct_list)
  745. list_destroy(local_acct_list);
  746. return SLURM_ERROR;
  747. }
  748. }
  749. if(!list_count(assoc_cond->acct_list)) {
  750. if(!list_count(wckey_cond->name_list)) {
  751. slurmdb_destroy_wckey_cond(wckey_cond);
  752. slurmdb_destroy_association_cond(assoc_cond);
  753. exit_code=1;
  754. fprintf(stderr, " Need name of account to "
  755. "add user to.\n");
  756. return SLURM_ERROR;
  757. }
  758. } else {
  759. slurmdb_account_cond_t account_cond;
  760. slurmdb_association_cond_t query_assoc_cond;
  761. memset(&account_cond, 0, sizeof(slurmdb_account_cond_t));
  762. account_cond.assoc_cond = assoc_cond;
  763. local_acct_list = acct_storage_g_get_accounts(
  764. db_conn, my_uid, &account_cond);
  765. if(!local_acct_list) {
  766. exit_code=1;
  767. fprintf(stderr, " Problem getting accounts "
  768. "from database. Contact your admin.\n");
  769. list_destroy(local_user_list);
  770. slurmdb_destroy_wckey_cond(wckey_cond);
  771. slurmdb_destroy_association_cond(assoc_cond);
  772. return SLURM_ERROR;
  773. }
  774. memset(&query_assoc_cond, 0,
  775. sizeof(slurmdb_association_cond_t));
  776. query_assoc_cond.acct_list = assoc_cond->acct_list;
  777. query_assoc_cond.cluster_list = assoc_cond->cluster_list;
  778. local_assoc_list = acct_storage_g_get_associations(
  779. db_conn, my_uid, &query_assoc_cond);
  780. if (!local_assoc_list) {
  781. exit_code=1;
  782. fprintf(stderr, " Problem getting associations "
  783. "from database. Contact your admin.\n");
  784. list_destroy(local_user_list);
  785. list_destroy(local_acct_list);
  786. slurmdb_destroy_wckey_cond(wckey_cond);
  787. slurmdb_destroy_association_cond(assoc_cond);
  788. return SLURM_ERROR;
  789. }
  790. }
  791. if(track_wckey || default_wckey) {
  792. wckey_cond->cluster_list = assoc_cond->cluster_list;
  793. wckey_cond->user_list = assoc_cond->user_list;
  794. if(!(local_wckey_list = acct_storage_g_get_wckeys(
  795. db_conn, my_uid, wckey_cond)))
  796. info("If you are a coordinator ignore "
  797. "the previous error");
  798. wckey_cond->cluster_list = NULL;
  799. wckey_cond->user_list = NULL;
  800. }
  801. /* we are adding these lists to the global lists and will be
  802. freed when they are */
  803. user_list = list_create(slurmdb_destroy_user_rec);
  804. assoc_list = list_create(slurmdb_destroy_association_rec);
  805. wckey_list = list_create(slurmdb_destroy_wckey_rec);
  806. itr = list_iterator_create(assoc_cond->user_list);
  807. while((name = list_next(itr))) {
  808. slurmdb_user_rec_t *user_rec = NULL;
  809. char *local_def_acct = NULL;
  810. char *local_def_wckey = NULL;
  811. if(!name[0]) {
  812. exit_code=1;
  813. fprintf(stderr, " No blank names are "
  814. "allowed when adding.\n");
  815. rc = SLURM_ERROR;
  816. continue;
  817. }
  818. local_def_acct = xstrdup(default_acct);
  819. local_def_wckey = xstrdup(default_wckey);
  820. user = NULL;
  821. if(!(user_rec = sacctmgr_find_user_from_list(
  822. local_user_list, name))) {
  823. uid_t pw_uid;
  824. if (!local_def_acct
  825. && assoc_cond->acct_list
  826. && list_count(assoc_cond->acct_list))
  827. local_def_acct = xstrdup(
  828. list_peek(assoc_cond->acct_list));
  829. if(!local_def_wckey
  830. && wckey_cond->name_list
  831. && list_count(wckey_cond->name_list))
  832. local_def_wckey = xstrdup(
  833. list_peek(wckey_cond->name_list));
  834. if(!local_def_acct || !local_def_acct[0]) {
  835. exit_code=1;
  836. fprintf(stderr, " Need a default account for "
  837. "these users to add.\n");
  838. rc = SLURM_ERROR;
  839. xfree(local_def_acct);
  840. xfree(local_def_wckey);
  841. goto no_default;
  842. }
  843. if(first) {
  844. if(!sacctmgr_find_account_from_list(
  845. local_acct_list, local_def_acct)) {
  846. exit_code=1;
  847. fprintf(stderr, " This account '%s' "
  848. "doesn't exist.\n"
  849. " Contact your admin "
  850. "to add this account.\n",
  851. local_def_acct);
  852. xfree(local_def_acct);
  853. xfree(local_def_wckey);
  854. continue;
  855. }
  856. first = 0;
  857. }
  858. if (uid_from_string (name, &pw_uid) < 0) {
  859. char *warning = xstrdup_printf(
  860. "There is no uid for user '%s'"
  861. "\nAre you sure you want to continue?",
  862. name);
  863. if(!commit_check(warning)) {
  864. xfree(warning);
  865. rc = SLURM_ERROR;
  866. list_flush(user_list);
  867. xfree(local_def_acct);
  868. xfree(local_def_wckey);
  869. goto end_it;
  870. }
  871. xfree(warning);
  872. }
  873. user = xmalloc(sizeof(slurmdb_user_rec_t));
  874. user->assoc_list =
  875. list_create(slurmdb_destroy_association_rec);
  876. user->wckey_list =
  877. list_create(slurmdb_destroy_wckey_rec);
  878. user->name = xstrdup(name);
  879. user->default_acct = xstrdup(local_def_acct);
  880. user->default_wckey = xstrdup(local_def_wckey);
  881. user->admin_level = admin_level;
  882. xstrfmtcat(user_str, " %s\n", name);
  883. list_append(user_list, user);
  884. }
  885. itr_a = list_iterator_create(assoc_cond->acct_list);
  886. while ((account = list_next(itr_a))) {
  887. if (acct_first) {
  888. if (!sacctmgr_find_account_from_list(
  889. local_acct_list, account)) {
  890. exit_code=1;
  891. fprintf(stderr, " This account '%s' "
  892. "doesn't exist.\n"
  893. " Contact your admin "
  894. "to add this account.\n",
  895. account);
  896. continue;
  897. }
  898. }
  899. itr_c = list_iterator_create(assoc_cond->cluster_list);
  900. while ((cluster = list_next(itr_c))) {
  901. /* We need to check this every time
  902. for a cluster to make sure there
  903. isn't one already set for that
  904. cluster.
  905. */
  906. if (!sacctmgr_find_account_base_assoc_from_list(
  907. local_assoc_list, account,
  908. cluster)) {
  909. if (acct_first) {
  910. exit_code=1;
  911. fprintf(stderr, " This "
  912. "account '%s' "
  913. "doesn't exist on "
  914. "cluster %s\n"
  915. " Contact your "
  916. "admin to add "
  917. "this account.\n",
  918. account, cluster);
  919. }
  920. continue;
  921. } else if (!local_def_acct) {
  922. slurmdb_association_rec_t *assoc_rec;
  923. if (user_rec
  924. && (assoc_rec =
  925. sacctmgr_find_association_from_list(
  926. user_rec->assoc_list,
  927. name, NULL,
  928. cluster, "*")))
  929. local_def_acct = xstrdup(
  930. assoc_rec->acct);
  931. else if (assoc_cond
  932. && assoc_cond->acct_list
  933. && list_count(assoc_cond->
  934. acct_list))
  935. local_def_acct = xstrdup(
  936. list_peek(assoc_cond->
  937. acct_list));
  938. }
  939. itr_p = list_iterator_create(
  940. assoc_cond->partition_list);
  941. while((partition = list_next(itr_p))) {
  942. partition_set = 1;
  943. if(sacctmgr_find_association_from_list(
  944. local_assoc_list,
  945. name, account,
  946. cluster, partition))
  947. continue;
  948. assoc = xmalloc(
  949. sizeof(slurmdb_association_rec_t));
  950. slurmdb_init_association_rec(assoc, 0);
  951. assoc->user = xstrdup(name);
  952. assoc->acct = xstrdup(account);
  953. assoc->cluster = xstrdup(cluster);
  954. assoc->partition = xstrdup(partition);
  955. if (local_def_acct &&
  956. !strcmp(local_def_acct, account))
  957. assoc->is_def = 1;
  958. assoc->def_qos_id =
  959. start_assoc.def_qos_id;
  960. assoc->shares_raw =
  961. start_assoc.shares_raw;
  962. assoc->grp_cpu_mins =
  963. start_assoc.grp_cpu_mins;
  964. assoc->grp_cpus = start_assoc.grp_cpus;
  965. assoc->grp_jobs = start_assoc.grp_jobs;
  966. assoc->grp_mem = start_assoc.grp_mem;
  967. assoc->grp_nodes =
  968. start_assoc.grp_nodes;
  969. assoc->grp_submit_jobs =
  970. start_assoc.grp_submit_jobs;
  971. assoc->grp_wall = start_assoc.grp_wall;
  972. assoc->max_cpu_mins_pj =
  973. start_assoc.max_cpu_mins_pj;
  974. assoc->max_cpus_pj =
  975. start_assoc.max_cpus_pj;
  976. assoc->max_jobs = start_assoc.max_jobs;
  977. assoc->max_nodes_pj =
  978. start_assoc.max_nodes_pj;
  979. assoc->max_submit_jobs =
  980. start_assoc.max_submit_jobs;
  981. assoc->max_wall_pj =
  982. start_assoc.max_wall_pj;
  983. assoc->qos_list = copy_char_list(
  984. start_assoc.qos_list);
  985. if (user)
  986. list_append(user->assoc_list,
  987. assoc);
  988. else
  989. list_append(assoc_list, assoc);
  990. xstrfmtcat(assoc_str,
  991. " U = %-9.9s"
  992. " A = %-10.10s"
  993. " C = %-10.10s"
  994. " P = %-10.10s\n",
  995. assoc->user, assoc->acct,
  996. assoc->cluster,
  997. assoc->partition);
  998. }
  999. list_iterator_destroy(itr_p);
  1000. if(partition_set) {
  1001. if (!default_acct && local_def_acct)
  1002. xfree(local_def_acct);
  1003. continue;
  1004. }
  1005. if(sacctmgr_find_association_from_list(
  1006. local_assoc_list,
  1007. name, account, cluster, NULL)) {
  1008. if (!default_acct && local_def_acct)
  1009. xfree(local_def_acct);
  1010. continue;
  1011. }
  1012. assoc = xmalloc(
  1013. sizeof(slurmdb_association_rec_t));
  1014. slurmdb_init_association_rec(assoc, 0);
  1015. assoc->user = xstrdup(name);
  1016. if(local_def_acct
  1017. && !strcmp(local_def_acct, account))
  1018. assoc->is_def = 1;
  1019. assoc->acct = xstrdup(account);
  1020. assoc->cluster = xstrdup(cluster);
  1021. assoc->def_qos_id = start_assoc.def_qos_id;
  1022. assoc->shares_raw = start_assoc.shares_raw;
  1023. assoc->grp_cpu_mins =
  1024. start_assoc.grp_cpu_mins;
  1025. assoc->grp_cpus = start_assoc.grp_cpus;
  1026. assoc->grp_jobs = start_assoc.grp_jobs;
  1027. assoc->grp_mem = start_assoc.grp_mem;
  1028. assoc->grp_nodes = start_assoc.grp_nodes;
  1029. assoc->grp_submit_jobs =
  1030. start_assoc.grp_submit_jobs;
  1031. assoc->grp_wall = start_assoc.grp_wall;
  1032. assoc->max_cpu_mins_pj =
  1033. start_assoc.max_cpu_mins_pj;
  1034. assoc->max_cpus_pj = start_assoc.max_cpus_pj;
  1035. assoc->max_jobs = start_assoc.max_jobs;
  1036. assoc->max_nodes_pj = start_assoc.max_nodes_pj;
  1037. assoc->max_submit_jobs =
  1038. start_assoc.max_submit_jobs;
  1039. assoc->max_wall_pj = start_assoc.max_wall_pj;
  1040. assoc->qos_list =
  1041. copy_char_list(start_assoc.qos_list);
  1042. if(user)
  1043. list_append(user->assoc_list, assoc);
  1044. else
  1045. list_append(assoc_list, assoc);
  1046. xstrfmtcat(assoc_str,
  1047. " U = %-9.9s"
  1048. " A = %-10.10s"
  1049. " C = %-10.10s\n",
  1050. assoc->user, assoc->acct,
  1051. assoc->cluster);
  1052. if (!default_acct && local_def_acct)
  1053. xfree(local_def_acct);
  1054. }
  1055. list_iterator_destroy(itr_c);
  1056. }
  1057. list_iterator_destroy(itr_a);
  1058. acct_first = 0;
  1059. xfree(local_def_acct);
  1060. /* continue here if not doing wckeys */
  1061. if(!track_wckey && !local_def_wckey)
  1062. continue;
  1063. itr_w = list_iterator_create(wckey_cond->name_list);
  1064. while((account = list_next(itr_w))) {
  1065. itr_c = list_iterator_create(assoc_cond->cluster_list);
  1066. while((cluster = list_next(itr_c))) {
  1067. if(sacctmgr_find_wckey_from_list(
  1068. local_wckey_list, name, account,
  1069. cluster)) {
  1070. continue;
  1071. } else if (user_rec && !local_def_wckey) {
  1072. slurmdb_wckey_rec_t *wckey_rec;
  1073. if ((wckey_rec =
  1074. sacctmgr_find_wckey_from_list(
  1075. user_rec->wckey_list,
  1076. name, NULL, cluster)))
  1077. local_def_wckey = xstrdup(
  1078. wckey_rec->name);
  1079. else if (wckey_cond
  1080. && wckey_cond->name_list
  1081. && list_count(
  1082. wckey_cond->name_list))
  1083. local_def_wckey = xstrdup(
  1084. list_peek(wckey_cond->
  1085. name_list));
  1086. }
  1087. wckey = xmalloc(sizeof(slurmdb_wckey_rec_t));
  1088. wckey->user = xstrdup(name);
  1089. wckey->name = xstrdup(account);
  1090. wckey->cluster = xstrdup(cluster);
  1091. if(local_def_wckey
  1092. && !strcmp(local_def_wckey, account))
  1093. wckey->is_def = 1;
  1094. if(user)
  1095. list_append(user->wckey_list, wckey);
  1096. else
  1097. list_append(wckey_list, wckey);
  1098. xstrfmtcat(wckey_str,
  1099. " U = %-9.9s"
  1100. " W = %-10.10s"
  1101. " C = %-10.10s\n",
  1102. wckey->user, wckey->name,
  1103. wckey->cluster);
  1104. if (!default_wckey && local_def_wckey)
  1105. xfree(local_def_wckey);
  1106. }
  1107. list_iterator_destroy(itr_c);
  1108. }
  1109. list_iterator_destroy(itr_w);
  1110. xfree(local_def_wckey);
  1111. }
  1112. no_default:
  1113. list_iterator_destroy(itr);
  1114. list_destroy(local_user_list);
  1115. if(local_acct_list)
  1116. list_destroy(local_acct_list);
  1117. if(local_assoc_list)
  1118. list_destroy(local_assoc_list);
  1119. if(local_wckey_list)
  1120. list_destroy(local_wckey_list);
  1121. slurmdb_destroy_wckey_cond(wckey_cond);
  1122. slurmdb_destroy_association_cond(assoc_cond);
  1123. if(!list_count(user_list) && !list_count(assoc_list)
  1124. && !list_count(wckey_list)) {
  1125. printf(" Nothing new added.\n");
  1126. goto end_it;
  1127. } else if(!assoc_str && !wckey_str) {
  1128. exit_code=1;
  1129. fprintf(stderr, " No associations or wckeys created.\n");
  1130. goto end_it;
  1131. }
  1132. if(user_str) {
  1133. printf(" Adding User(s)\n%s", user_str);
  1134. printf(" Settings =\n");
  1135. printf(" Default Account = %s\n", default_acct);
  1136. if(default_wckey)
  1137. printf(" Default WCKey = %s\n", default_wckey);
  1138. if(admin_level != SLURMDB_ADMIN_NOTSET)
  1139. printf(" Admin Level = %s\n",
  1140. slurmdb_admin_level_str(admin_level));
  1141. xfree(user_str);
  1142. }
  1143. if(assoc_str) {
  1144. printf(" Associations =\n%s", assoc_str);
  1145. xfree(assoc_str);
  1146. }
  1147. if(wckey_str) {
  1148. printf(" WCKeys =\n%s", wckey_str);
  1149. xfree(wckey_str);
  1150. }
  1151. if(limit_set) {
  1152. printf(" Non Default Settings\n");
  1153. sacctmgr_print_assoc_limits(&start_assoc);
  1154. if(start_assoc.qos_list)
  1155. list_destroy(start_assoc.qos_list);
  1156. }
  1157. notice_thread_init();
  1158. if(list_count(user_list)) {
  1159. rc = acct_storage_g_add_users(db_conn, my_uid, user_list);
  1160. }
  1161. if(rc == SLURM_SUCCESS) {
  1162. if(list_count(assoc_list))
  1163. rc = acct_storage_g_add_associations(db_conn, my_uid,
  1164. assoc_list);
  1165. }
  1166. if(rc == SLURM_SUCCESS) {
  1167. if(list_count(wckey_list))
  1168. rc = acct_storage_g_add_wckeys(db_conn, my_uid,
  1169. wckey_list);
  1170. } else {
  1171. exit_code=1;
  1172. fprintf(stderr, " Problem adding users: %s\n",
  1173. slurm_strerror(rc));
  1174. rc = SLURM_ERROR;
  1175. notice_thread_fini();
  1176. goto end_it;
  1177. }
  1178. notice_thread_fini();
  1179. if(rc == SLURM_SUCCESS) {
  1180. if(commit_check("Would you like to commit changes?")) {
  1181. acct_storage_g_commit(db_conn, 1);
  1182. } else {
  1183. printf(" Changes Discarded\n");
  1184. acct_storage_g_commit(db_conn, 0);
  1185. }
  1186. } else {
  1187. exit_code=1;
  1188. fprintf(stderr, " Problem adding user associations: %s\n",
  1189. slurm_strerror(rc));
  1190. rc = SLURM_ERROR;
  1191. }
  1192. end_it:
  1193. list_destroy(user_list);
  1194. list_destroy(assoc_list);
  1195. list_destroy(wckey_list);
  1196. xfree(default_acct);
  1197. xfree(default_wckey);
  1198. return rc;
  1199. }
  1200. extern int sacctmgr_add_coord(int argc, char *argv[])
  1201. {
  1202. int rc = SLURM_SUCCESS;
  1203. int i=0;
  1204. int cond_set = 0, prev_set = 0;
  1205. slurmdb_user_cond_t *user_cond = xmalloc(sizeof(slurmdb_user_cond_t));
  1206. char *name = NULL;
  1207. char *user_str = NULL;
  1208. char *acct_str = NULL;
  1209. ListIterator itr = NULL;
  1210. for (i=0; i<argc; i++) {
  1211. int command_len = strlen(argv[i]);
  1212. if (!strncasecmp(argv[i], "Where", MAX(command_len, 5))
  1213. || !strncasecmp(argv[i], "Set", MAX(command_len, 3)))
  1214. i++;
  1215. prev_set = _set_cond(&i, argc, argv, user_cond, NULL);
  1216. cond_set |= prev_set;
  1217. }
  1218. if(exit_code) {
  1219. slurmdb_destroy_user_cond(user_cond);
  1220. return SLURM_ERROR;
  1221. } else if(!cond_set) {
  1222. exit_code=1;
  1223. fprintf(stderr, " You need to specify conditions to "
  1224. "to add the coordinator.\n");
  1225. slurmdb_destroy_user_cond(user_cond);
  1226. return SLURM_ERROR;
  1227. }
  1228. if((_check_coord_request(user_cond, true) == SLURM_ERROR)
  1229. || exit_code) {
  1230. slurmdb_destroy_user_cond(user_cond);
  1231. return SLURM_ERROR;
  1232. }
  1233. itr = list_iterator_create(user_cond->assoc_cond->user_list);
  1234. while((name = list_next(itr))) {
  1235. xstrfmtcat(user_str, " %s\n", name);
  1236. }
  1237. list_iterator_destroy(itr);
  1238. itr = list_iterator_create(user_cond->assoc_cond->acct_list);
  1239. while((name = list_next(itr))) {
  1240. xstrfmtcat(acct_str, " %s\n", name);
  1241. }
  1242. list_iterator_destroy(itr);
  1243. printf(" Adding Coordinator User(s)\n%s", user_str);
  1244. printf(" To Account(s) and all sub-accounts\n%s", acct_str);
  1245. notice_thread_init();
  1246. rc = acct_storage_g_add_coord(db_conn, my_uid,
  1247. user_cond->assoc_cond->acct_list,
  1248. user_cond);
  1249. notice_thread_fini();
  1250. slurmdb_destroy_user_cond(user_cond);
  1251. if(rc == SLURM_SUCCESS) {
  1252. if(commit_check("Would you like to commit changes?")) {
  1253. acct_storage_g_commit(db_conn, 1);
  1254. } else {
  1255. printf(" Changes Discarded\n");
  1256. acct_storage_g_commit(db_conn, 0);
  1257. }
  1258. } else {
  1259. exit_code=1;
  1260. fprintf(stderr, " Problem adding coordinator: %s\n",
  1261. slurm_strerror(rc));
  1262. rc = SLURM_ERROR;
  1263. }
  1264. return rc;
  1265. }
  1266. extern int sacctmgr_list_user(int argc, char *argv[])
  1267. {
  1268. int rc = SLURM_SUCCESS;
  1269. slurmdb_user_cond_t *user_cond = xmalloc(sizeof(slurmdb_user_cond_t));
  1270. List user_list;
  1271. int i=0, cond_set=0, prev_set=0;
  1272. ListIterator itr = NULL;
  1273. ListIterator itr2 = NULL;
  1274. slurmdb_user_rec_t *user = NULL;
  1275. slurmdb_association_rec_t *assoc = NULL;
  1276. print_field_t *field = NULL;
  1277. int field_count = 0;
  1278. List format_list = list_create(slurm_destroy_char);
  1279. List print_fields_list; /* types are of print_field_t */
  1280. user_cond->with_assocs = with_assoc_flag;
  1281. user_cond->assoc_cond = xmalloc(sizeof(slurmdb_association_cond_t));
  1282. for (i=0; i<argc; i++) {
  1283. int command_len = strlen(argv[i]);
  1284. if (!strncasecmp(argv[i], "Where", MAX(command_len, 5))
  1285. || !strncasecmp(argv[i], "Set", MAX(command_len, 3)))
  1286. i++;
  1287. prev_set = _set_cond(&i, argc, argv, user_cond, format_list);
  1288. cond_set |= prev_set;
  1289. }
  1290. if(exit_code) {
  1291. slurmdb_destroy_user_cond(user_cond);
  1292. list_destroy(format_list);
  1293. return SLURM_ERROR;
  1294. }
  1295. if(!list_count(format_list)) {
  1296. if(slurm_get_track_wckey())
  1297. slurm_addto_char_list(format_list,
  1298. "U,DefaultA,DefaultW,Ad");
  1299. else
  1300. slurm_addto_char_list(format_list, "U,DefaultA,Ad");
  1301. if (user_cond->with_coords)
  1302. slurm_addto_char_list(format_list, "Coord");
  1303. if (user_cond->with_assocs)
  1304. slurm_addto_char_list(format_list,
  1305. "Cl,Acc,Part,Share,"
  1306. "MaxJ,MaxN,MaxCPUs,MaxS,MaxW,"
  1307. "MaxCPUMins,QOS,DefaultQOS");
  1308. else
  1309. user_cond->assoc_cond->only_defs = 1;
  1310. }
  1311. /* If we are getting associations we want to disable only defs */
  1312. if (user_cond->with_assocs) {
  1313. user_cond->assoc_cond->only_defs = 0;
  1314. user_cond->with_wckeys = 1;
  1315. }
  1316. if(!user_cond->with_assocs && cond_set > 1) {
  1317. if(!commit_check("You requested options that are only vaild "
  1318. "when querying with the withassoc option.\n"
  1319. "Are you sure you want to continue?")) {
  1320. printf("Aborted\n");
  1321. list_destroy(format_list);
  1322. slurmdb_destroy_user_cond(user_cond);
  1323. return SLURM_SUCCESS;
  1324. }
  1325. }
  1326. print_fields_list = sacctmgr_process_format_list(format_list);
  1327. list_destroy(format_list);
  1328. if(exit_code) {
  1329. slurmdb_destroy_user_cond(user_cond);
  1330. list_destroy(print_fields_list);
  1331. return SLURM_ERROR;
  1332. }
  1333. user_list = acct_storage_g_get_users(db_conn, my_uid, user_cond);
  1334. slurmdb_destroy_user_cond(user_cond);
  1335. if(!user_list) {
  1336. exit_code=1;
  1337. fprintf(stderr, " Problem with query.\n");
  1338. list_destroy(print_fields_list);
  1339. return SLURM_ERROR;
  1340. }
  1341. itr = list_iterator_create(user_list);
  1342. itr2 = list_iterator_create(print_fields_list);
  1343. print_fields_header(print_fields_list);
  1344. field_count = list_count(print_fields_list);
  1345. while((user = list_next(itr))) {
  1346. if(user->assoc_list) {
  1347. char *curr_cluster = NULL;
  1348. ListIterator itr3 =
  1349. list_iterator_create(user->assoc_list);
  1350. ListIterator itr4 =
  1351. list_iterator_create(user->assoc_list);
  1352. ListIterator wckey_itr = NULL;
  1353. if (user->wckey_list)
  1354. wckey_itr =
  1355. list_iterator_create(user->wckey_list);
  1356. while((assoc = list_next(itr3))) {
  1357. int curr_inx = 1;
  1358. /* get the defaults */
  1359. if (!curr_cluster
  1360. || strcmp(curr_cluster, assoc->cluster)) {
  1361. slurmdb_association_rec_t *assoc2;
  1362. /* We shouldn't have to reset this
  1363. * unless no default is on the
  1364. * cluster. */
  1365. while ((assoc2 = list_next(itr4))) {
  1366. if (!assoc2->is_def ||
  1367. strcmp(assoc->cluster,
  1368. assoc2->cluster))
  1369. continue;
  1370. curr_cluster = assoc2->cluster;
  1371. xfree(user->default_acct);
  1372. user->default_acct =
  1373. xstrdup(assoc2->acct);
  1374. break;
  1375. }
  1376. /* This means there wasn't a
  1377. default on the current cluster.
  1378. */
  1379. if (!assoc2)
  1380. list_iterator_reset(itr4);
  1381. if (curr_cluster && wckey_itr) {
  1382. slurmdb_wckey_rec_t *wckey;
  1383. /* We shouldn't have
  1384. * to reset this
  1385. * unless no default
  1386. * is on the cluster. */
  1387. while ((wckey =
  1388. list_next(wckey_itr))) {
  1389. if (!wckey->is_def ||
  1390. strcmp(curr_cluster,
  1391. wckey->
  1392. cluster))
  1393. continue;
  1394. xfree(user->
  1395. default_wckey);
  1396. user->default_wckey =
  1397. xstrdup(wckey->
  1398. name);
  1399. break;
  1400. }
  1401. /* This means there wasn't a
  1402. default on the
  1403. current cluster.
  1404. */
  1405. if (!wckey)
  1406. list_iterator_reset(
  1407. wckey_itr);
  1408. }
  1409. }
  1410. while((field = list_next(itr2))) {
  1411. switch(field->type) {
  1412. case PRINT_ADMIN:
  1413. field->print_routine(
  1414. field,
  1415. slurmdb_admin_level_str(
  1416. user->
  1417. admin_level),
  1418. (curr_inx ==
  1419. field_count));
  1420. break;
  1421. case PRINT_COORDS:
  1422. field->print_routine(
  1423. field,
  1424. user->coord_accts,
  1425. (curr_inx ==
  1426. field_count));
  1427. break;
  1428. case PRINT_DACCT:
  1429. field->print_routine(
  1430. field,
  1431. user->default_acct,
  1432. (curr_inx ==
  1433. field_count),
  1434. (curr_inx ==
  1435. field_count));
  1436. break;
  1437. case PRINT_DWCKEY:
  1438. field->print_routine(
  1439. field,
  1440. user->default_wckey,
  1441. (curr_inx ==
  1442. field_count),
  1443. (curr_inx ==
  1444. field_count));
  1445. break;
  1446. default:
  1447. sacctmgr_print_association_rec(
  1448. assoc, field, NULL,
  1449. (curr_inx ==
  1450. field_count));
  1451. break;
  1452. }
  1453. curr_inx++;
  1454. }
  1455. list_iterator_reset(itr2);
  1456. printf("\n");
  1457. }
  1458. list_iterator_destroy(itr3);
  1459. } else {
  1460. int curr_inx = 1;
  1461. while((field = list_next(itr2))) {
  1462. switch(field->type) {
  1463. case PRINT_QOS:
  1464. field->print_routine(
  1465. field, NULL,
  1466. NULL,
  1467. (curr_inx == field_count));
  1468. break;
  1469. case PRINT_ADMIN:
  1470. field->print_routine(
  1471. field,
  1472. slurmdb_admin_level_str(
  1473. user->admin_level),
  1474. (curr_inx == field_count));
  1475. break;
  1476. case PRINT_COORDS:
  1477. field->print_routine(
  1478. field,
  1479. user->coord_accts,
  1480. (curr_inx == field_count));
  1481. break;
  1482. case PRINT_DACCT:
  1483. field->print_routine(
  1484. field,
  1485. user->default_acct,
  1486. (curr_inx == field_count));
  1487. break;
  1488. case PRINT_DWCKEY:
  1489. field->print_routine(
  1490. field,
  1491. user->default_wckey,
  1492. (curr_inx == field_count));
  1493. break;
  1494. case PRINT_USER:
  1495. field->print_routine(
  1496. field,
  1497. user->name,
  1498. (curr_inx == field_count));
  1499. break;
  1500. default:
  1501. field->print_routine(
  1502. field, NULL,
  1503. (curr_inx == field_count));
  1504. break;
  1505. }
  1506. curr_inx++;
  1507. }
  1508. list_iterator_reset(itr2);
  1509. printf("\n");
  1510. }
  1511. }
  1512. list_iterator_destroy(itr2);
  1513. list_iterator_destroy(itr);
  1514. list_destroy(user_list);
  1515. list_destroy(print_fields_list);
  1516. return rc;
  1517. }
  1518. extern int sacctmgr_modify_user(int argc, char *argv[])
  1519. {
  1520. int rc = SLURM_SUCCESS;
  1521. slurmdb_user_cond_t *user_cond = xmalloc(sizeof(slurmdb_user_cond_t));
  1522. slurmdb_user_rec_t *user = xmalloc(sizeof(slurmdb_user_rec_t));
  1523. slurmdb_association_rec_t *assoc =
  1524. xmalloc(sizeof(slurmdb_association_rec_t));
  1525. int i=0;
  1526. int cond_set = 0, prev_set = 0, rec_set = 0, set = 0;
  1527. List ret_list = NULL;
  1528. slurmdb_init_association_rec(assoc, 0);
  1529. user_cond->assoc_cond = xmalloc(sizeof(slurmdb_association_cond_t));
  1530. user_cond->assoc_cond->cluster_list = list_create(slurm_destroy_char);
  1531. for (i=0; i<argc; i++) {
  1532. int command_len = strlen(argv[i]);
  1533. if (!strncasecmp(argv[i], "Where", MAX(command_len, 5))) {
  1534. i++;
  1535. prev_set = _set_cond(&i, argc, argv, user_cond, NULL);
  1536. cond_set |= prev_set;
  1537. } else if (!strncasecmp(argv[i], "Set", MAX(command_len, 3))) {
  1538. i++;
  1539. prev_set = _set_rec(&i, argc, argv, user, assoc);
  1540. rec_set |= prev_set;
  1541. } else {
  1542. prev_set = _set_cond(&i, argc, argv, user_cond, NULL);
  1543. cond_set |= prev_set;
  1544. }
  1545. }
  1546. if(exit_code) {
  1547. slurmdb_destroy_user_cond(user_cond);
  1548. slurmdb_destroy_user_rec(user);
  1549. slurmdb_destroy_association_rec(assoc);
  1550. return SLURM_ERROR;
  1551. } else if(!rec_set) {
  1552. exit_code=1;
  1553. fprintf(stderr, " You didn't give me anything to set\n");
  1554. slurmdb_destroy_user_cond(user_cond);
  1555. slurmdb_destroy_user_rec(user);
  1556. slurmdb_destroy_association_rec(assoc);
  1557. return SLURM_ERROR;
  1558. } else if(!cond_set) {
  1559. if(!commit_check("You didn't set any conditions with 'WHERE'.\n"
  1560. "Are you sure you want to continue?")) {
  1561. printf("Aborted\n");
  1562. slurmdb_destroy_user_cond(user_cond);
  1563. slurmdb_destroy_user_rec(user);
  1564. slurmdb_destroy_association_rec(assoc);
  1565. return SLURM_SUCCESS;
  1566. }
  1567. }
  1568. // Special case: reset raw usage only
  1569. if (assoc->usage) {
  1570. rc = SLURM_ERROR;
  1571. if (user_cond->assoc_cond->acct_list) {
  1572. if (assoc->usage->usage_raw == 0.0)
  1573. rc = sacctmgr_remove_assoc_usage(
  1574. user_cond->assoc_cond);
  1575. else
  1576. error("Raw usage can only be set to 0 (zero)");
  1577. } else {
  1578. error("An account must be specified");
  1579. }
  1580. slurmdb_destroy_user_cond(user_cond);
  1581. slurmdb_destroy_user_rec(user);
  1582. slurmdb_destroy_association_rec(assoc);
  1583. return rc;
  1584. }
  1585. _check_and_set_cluster_list(user_cond->assoc_cond->cluster_list);
  1586. notice_thread_init();
  1587. if(rec_set & 1) { // process the account changes
  1588. if(cond_set == 2) {
  1589. rc = SLURM_ERROR;
  1590. exit_code=1;
  1591. fprintf(stderr,
  1592. " There was a problem with your "
  1593. "'where' options.\n");
  1594. goto assoc_start;
  1595. }
  1596. if(user_cond->assoc_cond
  1597. && user_cond->assoc_cond->acct_list
  1598. && list_count(user_cond->assoc_cond->acct_list)) {
  1599. notice_thread_fini();
  1600. if(commit_check(
  1601. " You specified Accounts in your "
  1602. "request. Did you mean "
  1603. "DefaultAccounts?\n")) {
  1604. if(!user_cond->def_acct_list)
  1605. user_cond->def_acct_list =
  1606. list_create(slurm_destroy_char);
  1607. list_transfer(user_cond->def_acct_list,
  1608. user_cond->assoc_cond->acct_list);
  1609. }
  1610. notice_thread_init();
  1611. }
  1612. ret_list = acct_storage_g_modify_users(
  1613. db_conn, my_uid, user_cond, user);
  1614. if (ret_list && list_count(ret_list)) {
  1615. set = 1;
  1616. if (user->default_acct
  1617. && _check_default_associations(
  1618. user->default_acct, ret_list,
  1619. user_cond->assoc_cond->cluster_list)
  1620. != SLURM_SUCCESS) {
  1621. set = 0;
  1622. }
  1623. if (user->default_wckey
  1624. && _check_default_wckeys(
  1625. user->default_wckey, ret_list,
  1626. user_cond->assoc_cond->cluster_list)
  1627. != SLURM_SUCCESS) {
  1628. set = 0;
  1629. }
  1630. if (set) {
  1631. char *object;
  1632. ListIterator itr =
  1633. list_iterator_create(ret_list);
  1634. printf(" Modified users...\n");
  1635. while ((object = list_next(itr))) {
  1636. printf(" %s\n", object);
  1637. }
  1638. list_iterator_destroy(itr);
  1639. }
  1640. } else if (ret_list) {
  1641. printf(" Nothing modified\n");
  1642. } else {
  1643. exit_code=1;
  1644. fprintf(stderr, " Error with request: %s\n",
  1645. slurm_strerror(errno));
  1646. if(errno == ESLURM_ONE_CHANGE)
  1647. fprintf(stderr, " If you are …

Large files files are truncated, but you can click here to view the full file