-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathspooledit.cc
456 lines (391 loc) · 12.7 KB
/
spooledit.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
*
* The Contents of this file are made available subject to the terms of
* the Sun Industry Standards Source License Version 1.2
*
* Sun Microsystems Inc., March, 2001
*
*
* Sun Industry Standards Source License Version 1.2
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.2 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2001 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
* Portions of this software are Copyright (c) 2023-2024 HPC-Gridware GmbH
*
************************************************************************/
/*___INFO__MARK_END__*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cerrno>
#include "uti/sge_bootstrap.h"
#include "uti/sge_dstring.h"
#include "uti/sge_log.h"
#include "uti/sge_rmon_macros.h"
#include "uti/sge_spool.h"
#include "uti/sge_stdio.h"
#include "uti/sge_string.h"
#include "uti/sge_uidgid.h"
#include "uti/sge_unistd.h"
#include "sgeobj/cull/sge_all_listsL.h"
#include "sgeobj/sge_feature.h"
#include "sgeobj/sge_answer.h"
#include "spool/sge_spooling.h"
#include "spool/loader/sge_spooling_loader.h"
//#include "spool/berkeleydb/sge_bdb.h"
#include "gdi/ocs_gdi_ClientBase.h"
#include "msg_common.h"
#include "msg_utilbin.h"
static void
usage(const char *argv0)
{
fprintf(stderr, "%s\n %s command\n\n", MSG_UTILBIN_USAGE, argv0);
fprintf(stderr, "%s\n", MSG_DBSTAT_COMMANDINTRO1);
fprintf(stderr, "%s\n", MSG_DBSTAT_COMMANDINTRO2);
fprintf(stderr, "%s\n", MSG_DBSTAT_LIST);
fprintf(stderr, "%s\n", MSG_DBSTAT_DUMP);
fprintf(stderr, "%s\n", MSG_DBSTAT_LOAD);
fprintf(stderr, "%s\n", MSG_DBSTAT_DELETE);
}
static int
init_framework()
{
int ret = EXIT_FAILURE;
lList *answer_list = nullptr;
lListElem *spooling_context = nullptr;
const char *spooling_method = bootstrap_get_spooling_method();
const char *spooling_lib = bootstrap_get_spooling_lib();
const char *spooling_params = bootstrap_get_spooling_params();
DENTER(TOP_LAYER);
/* create spooling context */
spooling_context = spool_create_dynamic_context(&answer_list,
spooling_method,
spooling_lib,
spooling_params);
answer_list_output(&answer_list);
if (!strcmp(bootstrap_get_spooling_method(),"classic")) {
CRITICAL(SFNMAX, MSG_SPOOLDEFAULTS_CANTHANDLECLASSICSPOOLING);
} else if (spooling_context == nullptr) {
CRITICAL(SFNMAX, MSG_SPOOLDEFAULTS_CANNOTCREATECONTEXT);
} else {
spool_set_default_context(spooling_context);
spool_set_option(&answer_list, spooling_context, "recover=false");
answer_list_output(&answer_list);
/* initialize spooling context */
if (!spool_startup_context(&answer_list, spooling_context, true)) {
CRITICAL(SFNMAX, MSG_SPOOLDEFAULTS_CANNOTSTARTUPCONTEXT);
} else {
ret = EXIT_SUCCESS;
}
answer_list_output(&answer_list);
}
DRETURN(ret);
}
#if 0
static bdb_database
get_database_from_key(const char *key)
{
bdb_database database = BDB_CONFIG_DB;
if (key != nullptr) {
if (strncmp(key, "J", 1) == 0 ||
strncmp(key, "PET", 3) == 0) {
database = BDB_JOB_DB;
}
}
return database;
}
#endif
static const lDescr *
get_descr_from_key(const char *key)
{
const lDescr *descr = nullptr;
DENTER(TOP_LAYER);
if (key != nullptr) {
struct saved_vars_s *context = nullptr;
const char *type_name;
type_name = sge_strtok_r(key, ":", &context);
if (type_name != nullptr) {
sge_object_type type = object_name_get_type(type_name);
if (type != SGE_TYPE_ALL) {
descr = object_type_get_descr(type);
}
}
sge_free_saved_vars(context);
if (descr == nullptr) {
ERROR(MSG_DBSTAT_INVALIDKEY_S, key);
}
}
DRETURN(descr);
}
static int
list_objects(const char *key)
{
int ret = EXIT_SUCCESS;
bool dbret = false;
lList *answer_list = nullptr;
lList *stu_list = nullptr;
DENTER(TOP_LAYER);
/*
* Start transaction to sync with other transaction-protected
* cursor operations possibly launched from qmaster and friends.
*/
dbret = spool_transaction(&answer_list, spool_get_default_context(), STC_begin);
if (dbret) {
/*
* Transaction started; read the list
*/
dbret = spool_read_keys(&answer_list, spool_get_default_context(), &stu_list, key);
} else {
answer_list_output(&answer_list);
ret = EXIT_FAILURE;
}
if (dbret) {
/*
* Sucess: print out data.
*/
const lListElem *stu_elem = nullptr;
for_each_ep(stu_elem, stu_list) {
fprintf(stdout, "%s\n", lGetString(stu_elem, STU_name));
}
} else {
answer_list_output(&answer_list);
ret = EXIT_FAILURE;
}
/*
* Done. Close transaction.
*/
dbret = spool_transaction(&answer_list, spool_get_default_context(),
ret == EXIT_SUCCESS ? STC_commit : STC_rollback);
if (!dbret) {
answer_list_output(&answer_list);
ret = EXIT_FAILURE;
}
lFreeList(&stu_list);
lFreeList(&answer_list);
DRETURN(ret);
}
static int
dump_object(const char *key)
{
int ret = EXIT_SUCCESS;
bool dbret;
lList *answer_list = nullptr;
DENTER(TOP_LAYER);
/* start a transaction */
dbret = spool_transaction(&answer_list, spool_get_default_context(), STC_begin);
if (!dbret) {
answer_list_output(&answer_list);
ret = EXIT_FAILURE;
} else {
/* job script is spooled as string, not as cull object */
if (strncmp(key, "JOBSCRIPT:", 10) == 0) {
const char *job_script = nullptr;
lListElem *job_script_ep;
// job script is written as a pseudo STU_Type object in field STU_name
job_script_ep = spool_read_object(&answer_list, spool_get_default_context(),
SGE_TYPE_JOBSCRIPT, key);
if (job_script_ep != nullptr) {
job_script = lGetString(job_script_ep, STU_name);
}
if (job_script == nullptr) {
answer_list_output(&answer_list);
ret = EXIT_FAILURE;
} else {
/* dump job script with a trailing linefeed, it might be missing in the script */
printf("%s\n", job_script != nullptr ? job_script : "no job script");
sge_free(&job_script);
}
} else {
/* read object */
lListElem *object;
object = spool_read_object(&answer_list, spool_get_default_context(),
object_name_get_type(key), key);
if (object == nullptr) {
answer_list_output(&answer_list);
ret = EXIT_FAILURE;
} else {
lDumpElemFp(stdout, object, 0);
lFreeElem(&object);
}
}
}
/* close the transaction */
dbret = spool_transaction(&answer_list, spool_get_default_context(),
ret == EXIT_SUCCESS ? STC_commit : STC_rollback);
answer_list_output(&answer_list);
if (!dbret) {
ret = EXIT_FAILURE;
}
DRETURN(ret);
}
static int
load_object(const char *key, const char *fname)
{
int ret = EXIT_SUCCESS;
bool dbret;
lList *answer_list = nullptr;
const lDescr *descr;
lListElem *object = nullptr;
DENTER(TOP_LAYER);
descr = get_descr_from_key(key);
if (descr == nullptr) {
ret = EXIT_FAILURE;
}
if (ret == EXIT_SUCCESS) {
FILE *fd;
if((fd = fopen(fname, "r")) == nullptr) {
ERROR(MSG_ERROROPENINGFILEFORREADING_SS, fname, strerror(errno));
ret = EXIT_FAILURE;
} else {
object = lUndumpElemFp(fd, descr);
FCLOSE(fd);
if (object == nullptr) {
ERROR(MSG_DBSTAT_ERRORUNDUMPING_S, fname);
ret = EXIT_FAILURE;
}
}
}
if (object != nullptr) {
/* start a transaction */
dbret = spool_transaction(&answer_list, spool_get_default_context(), STC_begin);
if (!dbret) {
answer_list_output(&answer_list);
ret = EXIT_FAILURE;
} else {
/* read object */
// @todo: what is key? Something like JATASK:123123.342342?
// wouldn't we need to strip the JATASK: then?
dbret = spool_write_object(&answer_list, spool_get_default_context(), object, key,
object_name_get_type(key), true);
if (!dbret) {
answer_list_output(&answer_list);
ret = EXIT_FAILURE;
}
/* close the transaction */
dbret = spool_transaction(&answer_list, spool_get_default_context(),
ret == EXIT_SUCCESS ? STC_commit : STC_rollback);
if (!dbret) {
answer_list_output(&answer_list);
ret = EXIT_FAILURE;
}
}
}
lFreeElem(&object);
DRETURN(ret);
FCLOSE_ERROR:
ERROR(MSG_FILE_ERRORCLOSEINGXY_SS, fname, strerror(errno));
DRETURN(EXIT_FAILURE);
}
static int
delete_object(const char *key)
{
int ret = EXIT_SUCCESS;
bool dbret;
lList *answer_list = nullptr;
DENTER(TOP_LAYER);
/* start a transaction */
dbret = spool_transaction(&answer_list, spool_get_default_context(), STC_begin);
if (!dbret) {
answer_list_output(&answer_list);
ret = EXIT_FAILURE;
} else {
/* delete object with given key */
dbret = spool_delete_object(&answer_list, spool_get_default_context(),
object_name_get_type(key), key, true);
if (!dbret) {
answer_list_output(&answer_list);
ret = EXIT_FAILURE;
} else {
fprintf(stdout, "deleted object with key " SFQ "\n", key);
}
}
/* close the transaction */
dbret = spool_transaction(&answer_list, spool_get_default_context(),
ret == EXIT_SUCCESS ? STC_commit : STC_rollback);
if (!dbret) {
answer_list_output(&answer_list);
ret = EXIT_FAILURE;
}
DRETURN(ret);
}
int
main(int argc, char *argv[])
{
int ret = EXIT_SUCCESS;
lList *answer_list = nullptr;
DENTER_MAIN(TOP_LAYER, "spooledit");
if (ocs::gdi::ClientBase::setup(SPOOLDEFAULTS, MAIN_THREAD, &answer_list, false) != ocs::gdi::ErrorValue::AE_OK) {
answer_list_output(&answer_list);
sge_exit(1);
}
if (ret == EXIT_SUCCESS) {
/* parse commandline */
if (argc < 2) {
usage(argv[0]);
ret = EXIT_FAILURE;
} else {
ret = init_framework();
if (ret == EXIT_SUCCESS) {
if (strcmp(argv[1], "list") == 0) {
ret = list_objects(argc > 2 ? argv[2] : "");
} else if (strcmp(argv[1], "dump") == 0) {
if (argc < 3) {
usage(argv[0]);
ret = EXIT_FAILURE;
} else {
ret = dump_object(argv[2]);
}
} else if (strcmp(argv[1], "load") == 0) {
if (argc < 4) {
usage(argv[0]);
ret = EXIT_FAILURE;
} else {
ret = load_object(argv[2], argv[3]);
}
} else if (strcmp(argv[1], "delete") == 0) {
if (argc < 3) {
usage(argv[0]);
ret = EXIT_FAILURE;
} else {
ret = delete_object(argv[2]);
}
} else {
usage(argv[0]);
ret = EXIT_FAILURE;
}
}
}
}
if (spool_get_default_context() != nullptr) {
#if 0
u_long64 next_trigger = 0;
if (!spool_trigger_context(&answer_list, spool_get_default_context(),
0, &next_trigger)) {
ret = EXIT_FAILURE;
}
#endif
if (!spool_shutdown_context(&answer_list, spool_get_default_context())) {
ret = EXIT_FAILURE;
}
}
answer_list_output(&answer_list);
sge_exit(ret);
DRETURN(ret);
}