26d078dde889bff76b5ab3744b741e1b0f4cbf9c
[goaljobs.git] / goaljobs.ml
1 (* goaljobs
2  * Copyright (C) 2013 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *)
18
19 open CalendarLib
20
21 open Unix
22 open Printf
23
24 open Goaljobs_config
25
26 type ('a, 'b) alternative = Either of 'a | Or of 'b
27
28 let (//) = Filename.concat
29 let quote = Filename.quote
30
31 type goal_result_t = Goal_OK | Goal_failed of string
32 exception Goal_result of goal_result_t
33
34 let goal_failed msg = raise (Goal_result (Goal_failed msg))
35
36 let depth = ref 0
37 let indent fs =
38   let do_indent str =
39     prerr_string "| ";
40     for i = 0 to !depth-1 do prerr_string "  " done;
41     prerr_string str;
42     Pervasives.flush Pervasives.stderr
43   in
44   ksprintf do_indent fs
45
46 let target v =
47   if v then raise (Goal_result Goal_OK)
48 let target_all vs = target (List.fold_left (&&) true vs)
49 let target_exists vs = target (List.fold_left (||) false vs)
50 let require name f =
51   indent "require: %s\n" name;
52   incr depth;
53   let r = (try Either (f ()) with exn -> Or exn) in
54   decr depth;
55   match r with
56   | Either x -> x
57   | Or exn -> raise exn
58
59 let _enter_goal name = indent "enter goal: %s\n" name
60 let _leave_goal name = indent "leave goal: %s\n" name
61
62 type period_t = Seconds | Days | Months | Years
63 let seconds = (1, Seconds)
64 let sec = seconds and secs = seconds and second = seconds
65 let minutes = (60, Seconds)
66 let min = minutes and mins = minutes and minute = minutes
67 let hours = (3600, Seconds)
68 let hour = hours
69 let days = (1, Days)
70 let day = days
71 let weeks = (7, Days)
72 let week = weeks
73 let months = (1, Months)
74 let month = months
75 let years = (1, Years)
76 let year = years
77
78 let periodic_jobs = ref []
79
80 (* Register a periodic job. *)
81 let every ?name i (j, t) f =
82   let period = i*j, t in (* 5 minutes -> ((5 * 60), Seconds) *)
83   periodic_jobs := (period, (name, f)) :: !periodic_jobs
84
85 (* [next_time t period] returns the earliest event of [period]
86    strictly after time [t].
87
88    Visualising periods as repeated events on a timeline, this
89    returns [t']:
90
91    {v
92    events:  ---+---------+---------+---------+---------+---------+-----
93    times:          t     t'
94    }
95
96    Note that [period_t] events are not necessarily regular.
97    eg. The start of a month is not a fixed number of seconds
98    after the start of the previous month.  'Epoch' refers
99    to the Unix Epoch (ie. 1970-01-01 00:00:00 UTC).
100
101    If [period = i, Seconds i] then events are when
102    [t' mod i == 0] when t' is the number of seconds since
103    the Epoch.  This returns the next t' > t.
104
105    If [period = i, Days] then events happen at
106    midnight UTC every [i] days since the Epoch.
107    This returns the next midnight > t.
108
109    If [period = i, Months] then events happen at
110    midnight UTC on the 1st day of the month every [i] months
111    since the Epoch.  This returns midnight on the
112    1st day of the next month > t.
113
114    If [period = i, Years] then events happen at
115    midnight UTC on the 1st day of the year when
116    [(y - 1970) mod i == 0].  This returns midnight on the
117    1st day of the next year > t. *)
118
119 let next_time =
120   (* Round up 'a' to the next multiple of 'i'. *)
121   let round_up_float a i =
122     let r = mod_float a i in
123     if r = 0. then a +. i else a +. (i -. r)
124   and round_up a i =
125     let r = a mod i in
126     if r = 0 then a + i else a + (i - r)
127   in
128
129   fun t -> function
130   | (i, Seconds) ->
131     let i = float_of_int i in
132     round_up_float t i
133
134   | (i, Years) ->
135     let tm = gmtime t in
136
137     (* Round 'tm' up to the first day of the next year. *)
138     let year = round_up tm.tm_year i in
139     let tm = { tm with tm_sec = 0; tm_min = 0; tm_hour = 0;
140                        tm_mday = 1; tm_mon = 0; tm_year = year } in
141     fst (mktime tm)
142
143   | (i, Days) ->
144     let t = Date.from_unixfloat t in
145     let t0 = Date.make 1970 1 1 in
146
147     (* Number of whole days since Unix Epoch. *)
148     let nb_days = Date.Period.safe_nb_days (Date.sub t t0) in
149
150     let nb_days = round_up nb_days i in
151     let t' = Date.add t0 (Date.Period.day nb_days) in
152     Date.to_unixfloat t'
153
154   | (i, Months) ->
155     (* Calculate number of whole months since Unix Epoch. *)
156     let tm = gmtime t in
157     let months = 12 * (tm.tm_year - 70) + tm.tm_mon in
158
159     let months = round_up months i in
160     let t0 = Date.make 1970 1 1 in
161     let t' = Date.add t0 (Date.Period.month months) in
162     Date.to_unixfloat t'
163
164 let file_exists = Sys.file_exists
165
166 let directory_exists path =
167   let s =
168     try Some (stat path)
169     with
170     | Unix_error (ENOENT, _, _) -> None
171     | Unix_error (err, _, _) ->
172       let msg = sprintf "directory_exists: %s: %s" path (error_message err) in
173       goal_failed msg in
174   match s with
175   | Some s -> s.st_kind = S_DIR
176   | None -> false
177
178 let file_newer_than f1 f2 =
179   let stat f =
180     try Some (stat f)
181     with
182     | Unix_error (ENOENT, _, _) -> None
183     | Unix_error (err, _, _) ->
184       let msg = sprintf "file_newer_than: %s: %s" f (error_message err) in
185       goal_failed msg
186   in
187   let s1 = stat f1 and s2 = stat f2 in
188   match s1 with
189   | None -> false
190   | Some s1 ->
191     match s2 with
192     | None ->
193       let msg = sprintf "file_newer_than: %s: file does not exist" f2 in
194       goal_failed msg
195     | Some s2 ->
196       s1.st_mtime >= s2.st_mtime
197
198 let more_recent objs srcs =
199   if not (List.for_all file_exists objs) then false
200   else (
201     List.for_all (
202       fun obj -> List.for_all (file_newer_than obj) srcs
203     ) objs
204   )
205
206 let url_exists url =
207   (* http://stackoverflow.com/questions/12199059/how-to-check-if-an-url-exists-with-the-shell-and-probably-curl *)
208   let cmd =
209     sprintf "curl --output /dev/null --silent --head --fail %s" (quote url) in
210   match Sys.command cmd with
211   | 0 -> true
212   | 19|22 -> false
213   | r ->
214     let msg = sprintf "curl error testing '%s': exit code %d, see curl(1)"
215       url r in
216     goal_failed msg
217
218 let file_contains_string filename str =
219   let cmd = sprintf "grep -q -F %s %s" (quote str) (quote filename) in
220   match Sys.command cmd with
221   | 0 -> true
222   | 1 -> false
223   | r ->
224     let msg = sprintf "grep error testing for '%s' in '%s' (exit code %d)"
225       str filename r in
226     goal_failed msg
227
228 let url_contains_string url str =
229   let tmp = Filename.temp_file "goaljobsurl" "" in
230   let cmd =
231     sprintf "curl --output %s --silent --fail %s" (quote tmp) (quote url) in
232   (match Sys.command cmd with
233   | 0 -> ()
234   | 19|22 ->
235     let msg = sprintf "curl failed to download URL '%s'" url in
236     goal_failed msg
237   | r ->
238     let msg = sprintf "curl error testing '%s': exit code %d, see curl(1)"
239       url r in
240     goal_failed msg
241   );
242   let r = file_contains_string tmp str in
243   unlink tmp;
244   r
245
246 (* Create a temporary directory.  It is *not* deleted on exit. *)
247 let make_tmpdir () =
248   let chan = open_in "/dev/urandom" in
249   let data = Bytes.create 16 in
250   really_input chan data 0 (Bytes.length data);
251   close_in chan;
252   let data = Bytes.to_string data in
253   let data = Digest.to_hex (Digest.string data) in
254   let dir = Filename.temp_dir_name // sprintf "goaljobstmp%s" data in
255   mkdir dir 0o700;
256   dir
257
258 (* Recursively remove directory. *)
259 let rm_rf dir =
260   let cmd = sprintf "rm -rf %s" (quote dir) in
261   ignore (Sys.command cmd)
262
263 let shell = ref "/bin/sh"
264
265 (* Used by sh, shout, shlines to handle the script and temporary dir. *)
266 let with_script ?(tmpdir = true) script f =
267   let dir = if tmpdir then Some (make_tmpdir ()) else None in
268   let script_file, chan =
269     match dir with
270     | Some dir ->
271       let script_file = dir // "script.sh" in
272       let chan = open_out script_file in
273       script_file, chan
274     | None -> Filename.open_temp_file "goaljobsscript" ".sh" in
275   chmod script_file 0o700;
276   fprintf chan "#!%s\n" !shell;
277   fprintf chan "set -e\n"; (* so that job exits on error *)
278   fprintf chan "set -x\n"; (* echo commands (must be last) *)
279   fprintf chan "\n";
280   output_string chan script;
281   close_out chan;
282   let cmd =
283     match dir with
284     | Some dir -> sprintf "cd %s && exec %s" (quote dir) (quote script_file)
285     | None -> sprintf "exec %s" (quote script_file) in
286   let r = try Either (f cmd) with exn -> Or exn in
287   (match dir with
288   | Some dir -> rm_rf dir
289   | None -> ()
290   );
291   match r with
292   | Either x -> x
293   | Or exn -> raise exn
294
295 let sh ?tmpdir fs =
296   let do_sh script =
297     with_script ?tmpdir script (
298       fun cmd ->
299         let r = Sys.command cmd in
300         if r <> 0 then (
301           let msg = sprintf "external command failed with code %d" r in
302           goal_failed msg
303         )
304     )
305   in
306   ksprintf do_sh fs
307
308 let do_shlines ?tmpdir script =
309   with_script ?tmpdir script (
310     fun cmd ->
311       let chan = open_process_in cmd in
312       let lines = ref [] in
313       let rec loop () =
314         let line = input_line chan in
315         eprintf "%s\n%!" line;
316         lines := line :: !lines;
317         loop ()
318       in
319       (try loop () with End_of_file -> ());
320       match close_process_in chan with
321       | WEXITED 0 -> List.rev !lines
322       | WEXITED i ->
323         let msg = sprintf "external command failed with code %d" i in
324         goal_failed msg
325       | WSIGNALED i ->
326         let msg = sprintf "external command was killed by signal %d" i in
327         goal_failed msg
328       | WSTOPPED i ->
329         let msg = sprintf "external command was stopped by signal %d" i in
330         goal_failed msg
331   )
332 let shlines ?tmpdir fs = ksprintf (do_shlines ?tmpdir) fs
333
334 let do_shout ?tmpdir script =
335   let lines = do_shlines ?tmpdir script in
336   String.concat "\n" lines
337 let shout ?tmpdir fs = ksprintf (do_shout ?tmpdir) fs
338
339 (*
340 val replace_substring : string -> string -> string -> string
341 *)
342
343 let change_file_extension ext filename =
344   let i =
345     try String.rindex filename '.'
346     with Not_found -> String.length filename in
347   String.sub filename 0 i ^ "." ^ ext
348
349 (*
350 val filter_file_extension : string -> string list -> string
351 *)
352
353 (* Persistent memory is stored in $HOME/.goaljobs-memory.  We have to
354  * lock this file each time we read or write because multiple concurrent
355  * jobs may access it at the same time.
356  *
357  * XXX Replace this with a more efficient and less fragile implementation.
358  *)
359
360 let with_memory_locked ?(write = false) f =
361   let filename = getenv "HOME" // ".goaljobs-memory" in
362   let fd = openfile filename [O_RDWR; O_CREAT] 0o644 in
363   lockf fd (if write then F_LOCK else F_RLOCK) 0;
364
365   (* If the file is newly created with zero size, write an
366    * empty hash table.
367    *)
368   if (fstat fd).st_size = 0 then (
369     let empty : (string, string) Hashtbl.t = Hashtbl.create 13 in
370     let chan = out_channel_of_descr fd in
371     output_value chan empty;
372     Pervasives.flush chan;
373     ignore (lseek fd 0 SEEK_SET)
374   );
375
376   (* Run the function. *)
377   let r = try Either (f fd) with exn -> Or exn in
378   lockf fd F_ULOCK 0;
379   close fd;
380   match r with
381   | Either x -> x
382   | Or exn -> raise exn
383
384 let memory_exists key =
385   with_memory_locked (
386     fun fd ->
387       let chan = in_channel_of_descr fd in
388       let memory : (string, string) Hashtbl.t = input_value chan in
389       Hashtbl.mem memory key
390   )
391
392 let memory_get key =
393   with_memory_locked (
394     fun fd ->
395       let chan = in_channel_of_descr fd in
396       let memory : (string, string) Hashtbl.t = input_value chan in
397       try Some (Hashtbl.find memory key) with Not_found -> None
398   )
399
400 let memory_set key value =
401   with_memory_locked ~write:true (
402     fun fd ->
403       let chan = in_channel_of_descr fd in
404       let memory : (string, string) Hashtbl.t = input_value chan in
405       Hashtbl.replace memory key value;
406       let chan = out_channel_of_descr fd in
407       seek_out chan 0;
408       output_value chan memory;
409       Pervasives.flush chan;
410   )
411
412 let memory_delete key =
413   with_memory_locked ~write:true (
414     fun fd ->
415       let chan = in_channel_of_descr fd in
416       let memory : (string, string) Hashtbl.t = input_value chan in
417       Hashtbl.remove memory key;
418       let chan = out_channel_of_descr fd in
419       seek_out chan 0;
420       output_value chan memory;
421       Pervasives.flush chan;
422   )
423
424 let memory_list () =
425   with_memory_locked (
426     fun fd ->
427       let chan = in_channel_of_descr fd in
428       let memory : (string, string) Hashtbl.t = input_value chan in
429       Hashtbl.fold (fun key value xs -> (key, value) :: xs) memory []
430   )
431
432 let published_goals = ref []
433 let publish name fn = published_goals := (name, fn) :: !published_goals
434 let get_goal name =
435   try Some (List.assoc name !published_goals) with Not_found -> None
436
437 let log_program_output () =
438   let filename = Filename.temp_file "goaljobslog" ".txt" in
439   let cmd = "tee " ^ quote filename in
440   let chan = open_process_out cmd in
441   let fd = descr_of_out_channel chan in
442   dup2 fd stdout;
443   dup2 fd stderr;
444   filename
445
446 let mailto ?from ~subject ?(attach = []) to_ =
447   let cmd = ref (sprintf "%s -s %s" mailx (quote subject)) in
448   (match from with
449   | None -> ()
450   | Some f -> cmd := !cmd ^ " -r " ^ quote f
451   );
452   List.iter (
453     fun a -> cmd := !cmd ^ " -a " ^ quote a
454   ) attach;
455   cmd := !cmd ^ " " ^ quote to_;
456   if Sys.command !cmd <> 0 then
457     goal_failed "mailto: could not send email"
458
459 let goal_file_exists filename =
460   if not (file_exists filename) then (
461     let msg = sprintf "file '%s' required but not found" filename in
462     goal_failed msg
463   )
464 let goal_directory_exists path =
465   if not (directory_exists path) then (
466     let msg = sprintf "directory '%s' required but not found" path in
467     goal_failed msg
468   )
469 let goal_file_newer_than f1 f2 =
470   if not (file_newer_than f1 f2) then (
471     let msg = sprintf "file %s is required to be newer than %s" f1 f2 in
472     goal_failed msg
473   )
474 let goal_more_recent objs srcs =
475   if not (more_recent objs srcs) then (
476     let msg = sprintf "object(s) %s are required to be newer than source(s) %s"
477       (String.concat " " objs) (String.concat " " srcs) in
478     goal_failed msg
479   )
480 let goal_url_exists url =
481   if not (url_exists url) then (
482     let msg = sprintf "url_exists: URL '%s' required but does not exist" url in
483     goal_failed msg
484   )
485 let goal_file_contains_string filename str =
486   if not (file_contains_string filename str) then (
487     let msg = sprintf "file_contains_string: file '%s' is required to contain string '%s'" filename str in
488     goal_failed msg
489   )
490 let goal_url_contains_string url str =
491   if not (url_contains_string url str) then (
492     let msg = sprintf "url_contains_string: URL '%s' is required to contain string '%s'" url str in
493     goal_failed msg
494   )
495 let goal_memory_exists k =
496   if not (memory_exists k) then (
497     let msg = sprintf "memory_exists: key '%s' required but does not exist" k in
498     goal_failed msg
499   )
500
501 let guard fn arg =
502   try fn arg; true
503   with
504   | Goal_result (Goal_failed msg) ->
505     prerr_endline ("error: " ^ msg);
506     false
507   | exn ->
508     prerr_endline (Printexc.to_string exn);
509     false
510
511 (* Run the program. *)
512 let rec init () =
513   let prog = Sys.executable_name in
514   let prog = Filename.basename prog in
515
516   (* Save the current working directory when the program started. *)
517   putenv "builddir" (getcwd ());
518
519   let args = ref [] in
520
521   let display_version () =
522     printf "%s %s\n" package_name package_version;
523     exit 0
524   in
525
526   let list_goals () =
527     let names = !published_goals in
528     let names = List.map fst names in
529     let names = List.sort compare names in
530     List.iter print_endline names;
531     exit 0
532   in
533
534   let argspec = Arg.align [
535     "--goals", Arg.Unit list_goals, " List all goals";
536     "-l", Arg.Unit list_goals, " List all goals";
537     "-V", Arg.Unit display_version, " Display version number and exit";
538     "--version", Arg.Unit display_version, " Display version number and exit";
539   ] in
540   let anon_fun str = args := str :: !args in
541   let usage_msg = sprintf "\
542 %s: a script generated by goaljobs
543
544 List all goals:                %s -l
545 Run a single goal like this:   %s <name-of-goal> [<goal-args ...>]
546
547 For more information see the goaljobs(1) man page.
548
549 Options:
550 " prog prog prog in
551
552   Arg.parse argspec anon_fun usage_msg;
553
554   let args = List.rev !args in
555
556   (* Was a goal named on the command line? *)
557   (match args with
558   | name :: args ->
559     (match get_goal name with
560     | Some fn ->
561       exit (if guard fn args then 0 else 1)
562     | None ->
563       eprintf "error: no goal called '%s' was found.\n" name;
564       eprintf "Use %s -l to list all published goals in this script.\n" name;
565       exit 1
566     )
567   | [] ->
568     (* If periodic jobs exist, fall through. *)
569     if !periodic_jobs = [] then (
570       (* Does a published 'all' goal exist? *)
571       match get_goal "all" with
572       | Some fn ->
573         exit (if guard fn [] then 0 else 1)
574       | None ->
575         (* No published 'all' goal. *)
576         eprintf "error: no goal called 'all' was found.\n";
577         exit 1
578     )
579   );
580
581   assert (!periodic_jobs <> []);
582
583   (* Run the periodic jobs.  Note these run forever, or until killed. *)
584   while true do
585     (* Find the next job to run. *)
586     let now = time () in
587     let jobs = List.map (
588       fun (period, (_, _ as name_f)) ->
589         next_time now period, name_f
590     ) !periodic_jobs in
591     let jobs = List.sort (fun (t1,_) (t2,_) -> compare t1 t2) jobs in
592
593     (* Find all jobs that have the same next time.
594      * XXX When we can handle parallel jobs we can do better here,
595      * but until them run all the ones which have the same time
596      * in series.
597      *)
598     let next_t = int_of_float (fst (List.hd jobs)) in
599     let jobs = List.filter (fun (t, _) -> int_of_float t = next_t) jobs in
600
601     (* Run next job(s) after waiting for the appropriate amount of time. *)
602     let seconds = next_t - int_of_float now in
603     eprintf "next job will run in %s\n%!" (printable_seconds seconds);
604     sleep seconds;
605
606     List.iter (
607       fun (_, (name, f)) ->
608         eprintf "running job: %s\n%!"
609           (match name with Some name -> name | None -> "[unnamed]");
610         ignore (guard f ())
611     ) jobs
612   done
613
614 and printable_seconds s =
615   if s < 60 then sprintf "%d seconds" s
616   else if s < 6000 then sprintf "%d minutes, %d seconds" (s/60) (s mod 60)
617   else if s < 86400 then sprintf "%d hours, %d minutes" (s/3600) (s/60)
618   else sprintf "about %d days" (s/86400)