1 | <?php |
||
2 | /* (c) Asafov Sergey <[email protected]> |
||
3 | * Recipe for crontab jobs deploy |
||
4 | * Configuration: |
||
5 | You need no specify only crontab:jobs, which must be array of strings |
||
6 | */ |
||
7 | namespace Deployer; |
||
8 | |||
9 | // Get path to bin |
||
10 | set('bin/crontab', function () { |
||
11 | return run('which crontab'); |
||
12 | }); |
||
13 | |||
14 | desc('Load crontab'); |
||
15 | task('crontab:load', function () { |
||
16 | set('crontab:all', []); |
||
17 | |||
18 | // Crontab is empty |
||
19 | if (!test ("[ -f '/var/spool/cron/{{user}}' ]")) { |
||
20 | return; |
||
21 | } |
||
22 | |||
23 | $cronData = run ("{{bin/crontab}} -l"); |
||
24 | $cronLines = explode (PHP_EOL, $cronData); |
||
25 | |||
26 | $currentTasks = []; |
||
27 | foreach ($cronLines as $cronLine) { |
||
28 | $jobData = parseJob($cronLine); |
||
29 | if (is_null ($jobData)) { |
||
30 | continue; |
||
31 | } |
||
32 | |||
33 | $currentTasks[$jobData['ckey']] = $jobData; |
||
34 | } |
||
35 | |||
36 | set ('crontab:all', $currentTasks); |
||
37 | }); |
||
38 | |||
39 | desc('Sync crontab jobs'); |
||
40 | task('crontab:sync', function () { |
||
41 | $syncJobs = get('crontab:jobs', []); |
||
42 | |||
43 | if (count ($syncJobs) == 0) { |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
44 | writeln("Nothing to sync - configure crontab:jobs"); |
||
45 | return; |
||
46 | } |
||
47 | |||
48 | // Load current jobs |
||
49 | invoke('crontab:load'); |
||
50 | $cronJobs = get('crontab:all'); |
||
51 | |||
52 | foreach ($syncJobs as $syncJob) { |
||
53 | $syncJobData = parseJob($syncJob); |
||
54 | |||
55 | if (is_null ($syncJobData)) { |
||
56 | continue; |
||
57 | } |
||
58 | |||
59 | $cronJobData = $cronJobs[$syncJobData['ckey']] ?? NULL; |
||
60 | |||
61 | if (!is_null ($cronJobData) && $cronJobData['skey'] == $syncJobData['skey']) { |
||
62 | // Job is exists and correct |
||
63 | writeLn($syncJobData['cmd'] . ': <fg=green;options=bold>OK</>'); |
||
64 | } |
||
65 | else { |
||
66 | if (is_null ($cronJobData)) { |
||
67 | writeLn($syncJobData['cmd'] . ': <fg=yellow;options=bold>NEW</>'); |
||
68 | } |
||
69 | else { |
||
70 | writeLn($syncJobData['cmd'] . ': <fg=red;options=bold>FIX</>'); |
||
71 | } |
||
72 | |||
73 | $cronJobs[$syncJobData['ckey']] = $syncJobData; |
||
74 | } |
||
75 | } |
||
76 | |||
77 | if (test ("[ -f '/tmp/crontab_save' ]")) { |
||
78 | run ("unlink '/tmp/crontab_save'"); |
||
79 | } |
||
80 | |||
81 | foreach ($cronJobs as $cronJob) { |
||
82 | $jobString = $cronJob['minute'] . ' ' . $cronJob['hour'] . ' ' . $cronJob['day'] . ' ' . $cronJob['month'] . ' ' . $cronJob['weekday'] . ' ' . $cronJob['cmd']; |
||
83 | run ("echo '" . $jobString . "' >> '/tmp/crontab_save'"); |
||
84 | } |
||
85 | |||
86 | run ("{{bin/crontab}} /tmp/crontab_save"); |
||
87 | run ('unlink /tmp/crontab_save'); |
||
88 | }); |
||
89 | |||
90 | |||
91 | function parseJob ($job) { |
||
92 | if (!is_string($job)) { |
||
93 | return NULL; |
||
94 | } |
||
95 | |||
96 | if (substr ($job, 0, 1) == '#') { |
||
97 | return NULL; |
||
98 | } |
||
99 | |||
100 | $jobData = explode (' ', $job, 6); |
||
101 | |||
102 | if (count ($jobData) != 6) { |
||
103 | return NULL; |
||
104 | } |
||
105 | |||
106 | return [ |
||
107 | 'skey' => md5 ($job), |
||
108 | 'ckey' => md5 ($jobData['5']), |
||
109 | 'minute' => $jobData['0'], |
||
110 | 'hour' => $jobData['1'], |
||
111 | 'day' => $jobData['2'], |
||
112 | 'month' => $jobData['3'], |
||
113 | 'weekday' => $jobData['4'], |
||
114 | 'cmd' => $jobData['5'], |
||
115 | ]; |
||
116 | } |
||
117 | |||
118 |