forked from alseambusher/crontab-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrontab.js
More file actions
867 lines (767 loc) · 25.3 KB
/
Copy pathcrontab.js
File metadata and controls
867 lines (767 loc) · 25.3 KB
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
'use strict';
const Datastore = require('@seald-io/nedb');
const path = require('path');
const os = require('os');
const { exec } = require('child_process');
const fs = require('fs');
const { CronExpressionParser } = require('cron-parser');
const cronstrue = require('cronstrue/i18n');
const createTestRunManager = require('./test-runs');
const humanCronLocale = process.env.HUMANCRON ?? 'en';
const dbFolder = process.env.CRON_DB_PATH || path.join(__dirname, 'crontabs');
console.log(`Cron db path: ${dbFolder}`);
const logFolder = path.join(dbFolder, 'logs');
const envFile = path.join(dbFolder, 'env.db');
const crontabDbFile = path.join(dbFolder, 'crontab.db');
const maxCodeUploadBytes = 1024 * 1024;
const codeRunnerByExtension = {
'.sh': 'bash',
'.bash': 'bash',
'.py': 'python3',
'.js': 'node',
'.mjs': 'node',
'.cjs': 'node',
};
function isPathInside(parent, child) {
const relative = path.relative(path.resolve(parent), path.resolve(child));
return relative === '' || (!!relative && !relative.startsWith('..') && !path.isAbsolute(relative));
}
function defaultDataFolder() {
const home = os.homedir();
if (!home) return path.join(__dirname, 'crontabs');
if (process.platform === 'darwin') {
return path.join(home, 'Library', 'Application Support', 'crontab-ui');
}
if (process.platform === 'win32') {
return path.join(process.env.LOCALAPPDATA || path.join(home, 'AppData', 'Local'), 'crontab-ui');
}
return path.join(process.env.XDG_DATA_HOME || path.join(home, '.local', 'share'), 'crontab-ui');
}
function resolveCodeUploadsFolder() {
if (process.env.CRONTAB_UI_CODE_PATH) {
return path.resolve(process.env.CRONTAB_UI_CODE_PATH);
}
if (isPathInside(os.tmpdir(), dbFolder)) {
return path.join(defaultDataFolder(), 'code_uploads');
}
return path.join(dbFolder, 'code_uploads');
}
const codeUploadsFolder = resolveCodeUploadsFolder();
console.log(`Code uploads path: ${codeUploadsFolder}`);
// PATCH: prologue lines prepended to every deployed crontab. Used to set
// SHELL=/bin/bash so cron lines run under bash (the default /bin/sh on
// Ubuntu is dash, which has no `source` builtin). User-editable via the
// "Edit global env vars" button in the UI -> POST /globals.
const globalsFile = path.join(dbFolder, 'globals.txt');
const defaultGlobals =
'SHELL=/bin/bash\nPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\n';
function readGlobalsSync() {
try {
const content = fs.readFileSync(globalsFile, 'utf8');
return content.endsWith('\n') ? content : content + '\n';
} catch (_e) {
return defaultGlobals;
}
}
exports.get_globals = () => {
try {
return fs.readFileSync(globalsFile, 'utf8');
} catch (_e) {
return defaultGlobals;
}
};
exports.set_globals = (content, callback) => {
const safe = (content || '').replace(/\r\n/g, '\n');
fs.writeFile(globalsFile, safe, (err) => callback(err));
};
const db = new Datastore({ filename: crontabDbFile, autocompactionInterval: 60000 });
let cronPath = '/tmp';
if (process.env.CRON_PATH !== undefined) {
console.log(`Path to crond files set using env variables ${process.env.CRON_PATH}`);
cronPath = process.env.CRON_PATH;
}
db.loadDatabase((err) => {
if (err) throw err;
});
if (!fs.existsSync(logFolder)) {
fs.mkdirSync(logFolder);
}
if (!fs.existsSync(codeUploadsFolder)) {
fs.mkdirSync(codeUploadsFolder, { recursive: true, mode: 0o700 });
}
function requestError(status, message, err) {
return { status, message, err };
}
function shellQuote(value) {
return `'${String(value).replace(/'/g, "'\\''")}'`;
}
function sanitizeCodeJobId(rawId) {
if (typeof rawId !== 'string' || !rawId || /[^A-Za-z0-9_-]/.test(rawId)) {
throw requestError(400, 'Invalid job id');
}
return rawId;
}
function sanitizeCodeFilename(rawName) {
const filename = String(rawName || '').trim();
if (!filename) {
throw requestError(400, 'Code file name is required');
}
if (filename !== path.basename(filename)) {
throw requestError(400, 'Code file name must not contain a path');
}
if (!/^[A-Za-z0-9._-]+$/.test(filename) || filename === '.' || filename === '..') {
throw requestError(400, 'Code file name contains unsupported characters');
}
if (filename.length > 128) {
throw requestError(400, 'Code file name is too long');
}
return filename;
}
function runnerForCodeFilename(filename) {
const extension = path.extname(filename).toLowerCase();
const runner = codeRunnerByExtension[extension];
if (!runner) {
throw requestError(400, 'Code file must end in .sh, .bash, .py, .js, .mjs, or .cjs');
}
return runner;
}
function normalizeCodeContent(rawContent) {
if (typeof rawContent !== 'string') {
throw requestError(400, 'Code content is required');
}
const content = rawContent.replace(/\r\n/g, '\n');
if (!content.trim()) {
throw requestError(400, 'Code content is required');
}
if (content.includes('\0')) {
throw requestError(400, 'Code content must be text');
}
const size = Buffer.byteLength(content, 'utf8');
if (size > maxCodeUploadBytes) {
throw requestError(400, 'Code content must be 1 MiB or smaller');
}
return { content, size };
}
function nextCodeVersion(uploads) {
return (uploads || []).reduce((max, upload) => (
Math.max(max, Number(upload.version) || 0)
), 0) + 1;
}
function buildCodeUpload(data, version) {
const filename = sanitizeCodeFilename(data.codeFilename);
const runner = runnerForCodeFilename(filename);
const normalized = normalizeCodeContent(data.codeContent);
const source = data.codeSource === 'upload' ? 'upload' : 'paste';
return {
id: `v${version}-${Date.now()}`,
version,
filename,
runner,
source,
content: normalized.content,
size: normalized.size,
created: new Date().toISOString(),
};
}
function currentCodeUpload(tab) {
if (!Array.isArray(tab.codeUploads) || tab.codeUploads.length === 0) return null;
if (tab.currentCodeUploadId) {
const current = tab.codeUploads.find((upload) => upload.id === tab.currentCodeUploadId);
if (current) return current;
}
return tab.codeUploads[tab.codeUploads.length - 1];
}
function codeUploadFilePath(jobId, upload) {
const safeJobId = sanitizeCodeJobId(jobId);
const filename = sanitizeCodeFilename(upload.filename);
return path.join(
codeUploadsFolder,
'jobs',
safeJobId,
'versions',
String(upload.version || 1),
filename
);
}
function commandForCodeUpload(jobId, upload) {
const filename = sanitizeCodeFilename(upload.filename);
const runner = runnerForCodeFilename(filename);
return `${runner} ${shellQuote(codeUploadFilePath(jobId, upload))}`;
}
function materializeCodeUploadSync(jobId, upload) {
if (!upload || typeof upload.content !== 'string') {
return commandForCodeUpload(jobId, upload);
}
const filePath = codeUploadFilePath(jobId, upload);
fs.mkdirSync(path.dirname(filePath), { recursive: true, mode: 0o700 });
fs.writeFileSync(filePath, upload.content, { mode: 0o700 });
fs.chmodSync(filePath, 0o700);
return commandForCodeUpload(jobId, upload);
}
function commandForTab(tab) {
if (tab.commandMode === 'code') {
const upload = currentCodeUpload(tab);
if (upload) {
try {
return materializeCodeUploadSync(tab._id, upload);
} catch (e) {
console.error(e);
}
}
}
return tab.command || '';
}
function redactedCodeUpload(upload) {
const copy = { ...upload };
copy.hasContent = typeof copy.content === 'string' && copy.content.length > 0;
delete copy.content;
return copy;
}
function publicCrontab(doc) {
const copy = { ...doc };
copy.lastTestRun = testRunManager.getLatestForJob(copy._id);
if (copy.commandMode === 'code') {
const upload = currentCodeUpload(copy);
if (upload) {
try {
copy.command = commandForCodeUpload(copy._id, upload);
copy.currentCodeUpload = redactedCodeUpload(upload);
} catch (e) {
console.error(e);
}
}
copy.codeUploads = (copy.codeUploads || []).map(redactedCodeUpload);
}
return copy;
}
function buildCrontab(name, command, schedule, stopped, logging, mailing, envVars) {
return {
name,
command,
schedule,
...(stopped !== null && { stopped }),
timestamp: new Date().toString(),
logging,
mailing: mailing || {},
envVars: envVars || '',
};
}
function makeCommand(tab, preserveExitCode) {
const stderr = path.join(cronPath, `${tab._id}.stderr`);
const stdout = path.join(cronPath, `${tab._id}.stdout`);
const logFile = path.join(logFolder, `${tab._id}.log`);
const logFileStdout = path.join(logFolder, `${tab._id}.stdout.log`);
let cmd = commandForTab(tab);
if (cmd[cmd.length - 1] !== ';') {
cmd += ';';
}
let result = `({ ${cmd} } | tee ${stdout})`;
result = `(${result} 3>&1 1>&2 2>&3 | tee ${stderr}) 3>&1 1>&2 2>&3`;
result = `(${result})`;
if (preserveExitCode) result += '; crontab_ui_job_status=$?';
if (tab.logging && tab.logging === 'true') {
result += `; if test -f ${stderr}; then date >> "${logFile}"; cat ${stderr} >> "${logFile}"; fi`;
result += `; if test -f ${stdout}; then date >> "${logFileStdout}"; cat ${stdout} >> "${logFileStdout}"; fi`;
}
if (tab.hook) {
result += `; if test -f ${stdout}; then ${tab.hook} < ${stdout}; fi`;
}
if (tab.mailing && JSON.stringify(tab.mailing) !== '{}') {
result += `; /usr/local/bin/node ${__dirname}/bin/crontab-ui-mailer.js ${tab._id} ${stdout} ${stderr}`;
}
if (preserveExitCode) {
result += '; exit "$crontab_ui_job_status"';
return `bash -o pipefail -c ${shellQuote(result)}`;
}
return result;
}
function addEnvVars(envVars, command) {
if (envVars) {
return `(${envVars.replace(/\s*\n\s*/g, ' ').trim()}; (${command}))`;
}
return command;
}
exports.db_folder = dbFolder;
exports.log_folder = logFolder;
exports.env_file = envFile;
exports.crontab_db_file = crontabDbFile;
exports.code_uploads_folder = codeUploadsFolder;
function normalizeCreateArgs(nameOrData, command, schedule, logging, mailing, envVars, callback) {
if (typeof nameOrData === 'object' && nameOrData !== null) {
return { data: nameOrData, callback: command };
}
return {
data: { name: nameOrData, command, schedule, logging, mailing, envVars },
callback,
};
}
function baseUpdatesFromData(data, version) {
return {
name: data.name,
schedule: data.schedule,
timestamp: new Date().toString(),
logging: data.logging,
mailing: data.mailing || {},
envVars: data.envVars || '',
version,
};
}
function hasSubmittedCode(data) {
return Object.prototype.hasOwnProperty.call(data, 'codeContent');
}
exports.create_new = (nameOrData, command, schedule, logging, mailing, envVars, callback) => {
const normalized = normalizeCreateArgs(nameOrData, command, schedule, logging, mailing, envVars, callback);
const data = normalized.data;
const cb = normalized.callback;
const codeMode = data.commandMode === 'code';
let upload = null;
if (codeMode) {
try {
upload = buildCodeUpload(data, 1);
} catch (e) {
if (cb) cb(e);
return;
}
}
const tab = buildCrontab(
data.name,
codeMode ? '' : data.command,
data.schedule,
false,
data.logging,
data.mailing,
data.envVars
);
tab.created = Date.now();
tab.version = 0;
tab.commandMode = codeMode ? 'code' : 'command';
tab.codeUploads = [];
tab.currentCodeUploadId = '';
db.insert(tab, (err, newDoc) => {
if (err || !codeMode) {
if (cb) cb(err, newDoc);
return;
}
upload.command = commandForCodeUpload(newDoc._id, upload);
try {
materializeCodeUploadSync(newDoc._id, upload);
} catch (e) {
db.remove({ _id: newDoc._id }, {}, () => {});
if (cb) cb(requestError(500, 'Unable to save code file', e));
return;
}
const updates = {
command: upload.command,
codeUploads: [upload],
currentCodeUploadId: upload.id,
};
db.update({ _id: newDoc._id }, { $set: updates }, {}, (uerr) => {
if (cb) cb(uerr ? requestError(500, 'Unable to save code job', uerr) : null, {
...newDoc,
...updates,
});
});
});
};
exports.update = (data, callback) => {
db.findOne({ _id: data._id }, (err, doc) => {
if (err) return callback && callback({ status: 500, err });
if (!doc) return callback && callback({ status: 404 });
const submittedVersion = Number(data.version);
const currentVersion = Number(doc.version || 0);
if (!Number.isNaN(submittedVersion) && submittedVersion !== currentVersion) {
return callback && callback({ status: 409, doc });
}
const updates = baseUpdatesFromData(data, currentVersion + 1);
if (data.commandMode === 'code') {
const uploads = Array.isArray(doc.codeUploads) ? doc.codeUploads.slice() : [];
let upload = null;
if (hasSubmittedCode(data)) {
try {
const submittedUpload = buildCodeUpload(data, nextCodeVersion(uploads));
const currentUpload = currentCodeUpload(doc);
const isUnchanged = currentUpload &&
currentUpload.filename === submittedUpload.filename &&
currentUpload.content === submittedUpload.content;
if (isUnchanged) {
upload = currentUpload;
} else {
upload = submittedUpload;
uploads.push(upload);
}
upload.command = commandForCodeUpload(data._id, upload);
materializeCodeUploadSync(data._id, upload);
} catch (e) {
return callback && callback(e);
}
} else {
upload = currentCodeUpload(doc);
if (!upload) {
return callback && callback(requestError(400, 'Code content is required'));
}
try {
upload.command = commandForCodeUpload(data._id, upload);
materializeCodeUploadSync(data._id, upload);
} catch (e) {
return callback && callback(requestError(500, 'Unable to save code file', e));
}
}
updates.commandMode = 'code';
updates.command = upload.command;
updates.codeUploads = uploads;
updates.currentCodeUploadId = upload.id;
} else {
updates.commandMode = 'command';
updates.command = data.command;
updates.codeUploads = [];
updates.currentCodeUploadId = '';
}
db.update({ _id: data._id }, { $set: updates }, {}, (uerr) => {
if (callback) callback(uerr ? requestError(500, 'Unable to update job', uerr) : null);
});
});
};
exports.status = (_id, stopped, callback) => {
db.update({ _id }, { $set: { stopped } }, {}, (err) => {
if (callback) callback(err);
});
};
exports.remove = (_id, callback) => {
db.remove({ _id }, {}, (err) => {
if (callback) callback(err);
});
};
exports.crontabs = (callback) => {
db.find({}).sort({ created: -1 }).exec((err, docs) => {
if (err) {
console.error(err);
return callback([]);
}
for (const doc of docs) {
if (doc.schedule === '@reboot') {
doc.next = 'Next Reboot';
} else {
try {
doc.human = cronstrue.toString(doc.schedule, { locale: humanCronLocale });
doc.next = CronExpressionParser.parse(doc.schedule).next().toString();
} catch (e) {
console.error(e);
doc.next = 'invalid';
}
}
}
callback(docs);
});
};
exports.public_crontabs = (callback) => {
exports.crontabs((docs) => {
callback(docs.map(publicCrontab));
});
};
exports.get_current_code = (_id, callback) => {
let jobId;
try {
jobId = sanitizeCodeJobId(_id);
} catch (e) {
callback(e);
return;
}
db.findOne({ _id: jobId }, (err, doc) => {
if (err) return callback(requestError(500, 'Unable to load job', err));
if (!doc) return callback(requestError(404, 'Job not found'));
if (doc.commandMode !== 'code') {
return callback(requestError(400, 'Job does not use managed code'));
}
const upload = currentCodeUpload(doc);
if (!upload) return callback(requestError(404, 'Managed code file not found'));
try {
const filePath = codeUploadFilePath(jobId, upload);
if (!fs.existsSync(filePath)) {
if (typeof upload.content !== 'string') {
return callback(requestError(404, 'Managed code file not found'));
}
materializeCodeUploadSync(jobId, upload);
}
const content = fs.readFileSync(filePath, 'utf8');
callback(null, {
filename: upload.filename,
version: upload.version,
content,
});
} catch (e) {
callback(requestError(500, 'Unable to read managed code file', e));
}
});
};
exports.get_crontab = (_id, callback) => {
db.find({ _id }).exec((err, docs) => {
callback(docs[0]);
});
};
function testRunCommandFromData(data) {
if (data.commandMode === 'code' && hasSubmittedCode(data)) {
const upload = buildCodeUpload(data, 1);
const testJobId = `test-${process.pid}-${Date.now()}`;
const jobRoot = path.join(codeUploadsFolder, 'jobs', testJobId);
upload.command = commandForCodeUpload(testJobId, upload);
materializeCodeUploadSync(testJobId, upload);
return {
command: upload.command,
cleanup: () => fs.rmSync(jobRoot, { recursive: true, force: true }),
};
}
return { command: data.command || '', cleanup: null };
}
const testRunManager = createTestRunManager({
folder: path.join(dbFolder, 'test-runs'),
prepare(data) {
const prepared = testRunCommandFromData(data || {});
const jobId = data && data.jobId ? sanitizeCodeJobId(String(data.jobId)) : null;
return {
command: addEnvVars(data && data.envVars, prepared.command),
cleanup: prepared.cleanup,
jobId,
runType: data && data.runType === 'run-now' ? 'run-now' : 'test',
};
},
});
exports.runjob = (_id, callback) => {
let jobId;
try {
jobId = sanitizeCodeJobId(_id);
} catch (error) {
callback(error);
return;
}
db.findOne({ _id: jobId }, (err, job) => {
if (err) return callback(requestError(500, 'Unable to load job', err));
if (!job) return callback(requestError(404, 'Job not found'));
const command = makeCommand(job, true);
return testRunManager.start({
command,
envVars: job.envVars,
jobId,
runType: 'run-now',
}, (startError, result) => {
if (!startError) {
console.log('Running job');
console.log(`ID: ${jobId}`);
console.log(`Original command: ${job.command}`);
console.log(`Executed command: ${addEnvVars(job.envVars, command)}`);
}
callback(startError, result);
});
});
};
exports.test_run = (data, callback) => testRunManager.start({
...(data || {}),
runType: 'test',
}, callback);
exports.get_test_run = testRunManager.get;
exports.get_active_test_run = testRunManager.getActive;
exports.get_latest_test_run = (_id, callback) => {
let jobId;
try {
jobId = sanitizeCodeJobId(_id);
} catch (error) {
callback(error);
return;
}
const latest = testRunManager.getLatestForJob(jobId);
if (!latest) {
callback(requestError(404, 'No test run found for this job'));
return;
}
testRunManager.get(latest.id, {}, callback);
};
exports.stop_test_run = testRunManager.stop;
exports.shutdown_test_runs = testRunManager.shutdown;
exports.test_runs_folder = testRunManager.folder;
let deployInFlight = false;
let pendingDeploy = null;
function runDeploy(callback) {
// Import any externally-added system crontab lines into the DB first, so a
// deploy triggered by an unrelated UI action doesn't wipe lines the user
// added directly via `crontab -e` between page loads.
exports.import_crontab(() => {
exports.crontabs((tabs) => {
// PATCH: prepend user-editable global env vars (see readGlobalsSync above)
let crontabString = readGlobalsSync();
for (const tab of tabs) {
if (!tab.stopped) {
const wrapped = addEnvVars(tab.envVars, makeCommand(tab));
crontabString += `${tab.schedule} ${wrapped}\n`;
}
}
const fileName = process.env.CRON_IN_DOCKER !== undefined ? 'root' : 'crontab';
const filePath = path.join(cronPath, fileName);
fs.writeFile(filePath, crontabString, (err) => {
if (err) {
console.error(err);
return callback(err);
}
if (process.env.CRONTAB_UI_SKIP_DEPLOY === 'true') {
return callback();
}
exec(`crontab ${filePath}`, (execErr) => {
if (execErr) {
console.error(execErr);
return callback(execErr);
}
callback();
});
});
});
});
}
exports.deploy = (callback) => {
const cb = callback || (() => {});
if (deployInFlight) {
if (!pendingDeploy) pendingDeploy = { callbacks: [] };
pendingDeploy.callbacks.push(cb);
return;
}
deployInFlight = true;
runDeploy((err) => {
deployInFlight = false;
cb(err);
if (pendingDeploy) {
const cbs = pendingDeploy.callbacks;
pendingDeploy = null;
exports.deploy((err2) => cbs.forEach((c) => c(err2)));
}
});
};
exports.get_backup_names = () => {
const backups = fs.readdirSync(dbFolder)
.filter((file) => file.indexOf('backup') === 0);
// Sort by file mtime, newest first. This is more reliable than parsing
// a timestamp out of the filename because (a) custom-named backups have
// no timestamp in the name and (b) re-using a name overwrites the file,
// which should bump it to the top of the dropdown.
const mtime = (name) => {
try {
return fs.statSync(path.join(dbFolder, name)).mtime.valueOf();
} catch (_e) {
return 0;
}
};
backups.sort((a, b) => mtime(b) - mtime(a));
return backups;
};
exports.get_backup_details = () => {
return exports.get_backup_names().map((file) => {
let mtime = 0;
try {
mtime = fs.statSync(path.join(dbFolder, file)).mtime.valueOf();
} catch (_e) { /* missing/unreadable — surface as 0 */ }
return {
file,
name: file.replace(/^backup /, '').replace(/\.db$/, ''),
mtime,
};
});
};
function sanitizeBackupName(raw) {
if (!raw) return '';
return String(raw).replace(/[^A-Za-z0-9 _.-]/g, '').trim().slice(0, 64);
}
exports.backup = (nameOrCallback, callback) => {
let name = '';
let cb = callback;
if (typeof nameOrCallback === 'function') {
cb = nameOrCallback;
} else {
name = sanitizeBackupName(nameOrCallback);
}
const filename = name
? `backup ${name}.db`
: `backup ${new Date().toString().replace('+', ' ')}.db`;
const dest = path.join(dbFolder, filename);
fs.copyFile(crontabDbFile, dest, (err) => {
if (err) {
console.error(err);
return cb(err);
}
cb();
});
};
exports.restore = (dbName) => {
fs.createReadStream(path.join(dbFolder, dbName))
.pipe(fs.createWriteStream(crontabDbFile));
db.loadDatabase();
};
exports.reload_db = () => {
db.loadDatabase();
};
exports.get_env = () => {
if (fs.existsSync(envFile)) {
return fs.readFileSync(envFile, 'utf8').replace('\n', '\n');
}
return '';
};
function escapeRegex(s) {
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
function isManagedWrapperCommand(command) {
const currentPathRegex = new RegExp(
`${escapeRegex(cronPath)}/([A-Za-z0-9_-]+)\\.std(?:out|err)`
);
if (currentPathRegex.test(command)) return true;
return (
/\|\s*tee\s+\S+\.stdout\b/.test(command) &&
/\|\s*tee\s+\S+\.stderr\b/.test(command) &&
/3>&1\s+1>&2\s+2>&3/.test(command)
);
}
exports.import_crontab = (callback) => {
if (process.env.CRONTAB_UI_SKIP_SYSTEM_IMPORT === 'true') {
return process.nextTick(() => callback && callback());
}
const handleCrontab = (stdout) => {
const lines = (stdout || '').split('\n');
const namePrefix = Date.now();
const lineRegex = /^((@[a-zA-Z]+\s+)|(([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+))/;
const tasks = lines.map((rawLine, index) => new Promise((resolve) => {
const line = rawLine.replace(/\t+/g, ' ').trim();
if (!line || line.startsWith('#')) return resolve();
const command = line.replace(lineRegex, '').trim();
const schedule = line.replace(command, '').trim();
let isValid = false;
try {
isValid = CronExpressionParser.parse(schedule) !== null;
} catch (_e) { /* ignore */ }
if (!command || !schedule || !isValid) return resolve();
if (isManagedWrapperCommand(command)) {
// Wrapper line — either managed (skip) or orphan (skip; next deploy cleans).
return resolve();
}
db.findOne({ command, schedule }, (err, doc) => {
if (err || doc) return resolve();
const name = `${namePrefix}_${index}`;
exports.create_new(name, command, schedule, null, null, () => resolve());
});
}));
Promise.all(tasks).then(() => callback && callback());
};
if (process.env.CRONTAB_UI_SYSTEM_CRONTAB_FILE) {
return fs.readFile(process.env.CRONTAB_UI_SYSTEM_CRONTAB_FILE, 'utf8', (_err, stdout) => {
handleCrontab(stdout);
});
}
exec('crontab -l', (_error, stdout) => handleCrontab(stdout));
};
exports.preview_crontab = (callback) => {
exports.crontabs((tabs) => {
// PATCH: force bash for every cron line (POSIX /bin/sh has no `source`)
let crontabString = 'SHELL=/bin/bash\nPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\n';
for (const tab of tabs) {
if (!tab.stopped) {
const wrapped = addEnvVars(tab.envVars, makeCommand(tab));
crontabString += `${tab.schedule} ${wrapped}\n`;
}
}
callback(crontabString);
});
};
exports.autosave_crontab = (callback) => exports.deploy(callback);