Enable uploading.
[goaljobs-goals.git] / libguestfs_upstream.ml
1 (* This goal script is responsible for:
2  *  - testing each new commit to the libguestfs source repo
3  *  - checking for new upstream releases of libguestfs
4  *  - testing new upstream releases
5  *  - packaging them as a source tarball
6  *  - testing the source tarball on a variety of systems
7  *  - if all the above works, uploading the external website
8  * Note this doesn't build the Fedora releases.  See 'libguestfs_fedora.ml'.
9  *)
10
11 open Goaljobs
12 open Printf
13 open Config
14
15 (* Enable debugging. *)
16 let () =
17   Unix.putenv "LIBGUESTFS_DEBUG" "1";
18   Unix.putenv "LIBGUESTFS_TRACE" "1"
19
20 (* Log program output. *)
21 let from = "rjones@redhat.com"
22 let to_ = "rjones@redhat.com"
23 let logfile = log_program_output ()
24 let () = eprintf "logging to %s\n%!" logfile
25
26 let package = "libguestfs"
27
28 (* Helper object which stores everything about a version. *)
29 type info = {
30   version : string;              (* The version as a normal string. *)
31   major : int;                   (* Broken-out version fields. *)
32   minor : int;
33   release: int;
34   is_stable : bool;              (* is a stable version of libguestfs? *)
35   branch : string;               (* 'master' or 'stable-1.xx' *)
36   package_version : string;      (* package-version *)
37   tarball : string;              (* package-version.tar.gz *)
38   urlpath : string;              (* download/1.X-(stable|development)/tarball *)
39   url : string;                  (* full download URL of tarball *)
40 }
41
42 (* Helper: Fetch latest gnulib into $buildtmp/repos/gnulib
43  * XXX Move to Gnulib module.
44  *)
45 let get_gnulib () =
46   sh "
47     cd %s/repos
48     if [ ! -d gnulib ]; then git clone git://git.sv.gnu.org/gnulib.git; fi
49     cd gnulib
50     git checkout --force master
51     git pull
52   " buildtmp
53
54 (* Goal: the website has been updated to 'version'. *)
55 let rec goal website_updated version =
56   target (url_exists version.url);
57
58   require (tarball_created version);
59   require (tarball_tested version);
60
61   (* We only update the website for the development releases. *)
62   if not version.is_stable then
63     require (website_built version);
64
65   require (website_cvs_checked_in version);
66   require (website_rsync_done version)
67
68 (* Goal: website has been rsync'd. *)
69 and website_rsync_done version =
70   target (url_contains_string "http://libguestfs.org" version.version &&
71           url_exists version.url);
72
73   sh "
74     cd %s
75     ./.rsync
76   " libguestfs_website_cvs
77
78 (* Goal: Tarball added to CVS and CVS checked in. *)
79 and website_cvs_checked_in version =
80   let key = sprintf "libguestfs_website_cvs_checked_in_%s" version.version in
81   target (memory_exists key);
82   onrun (fun () -> memory_set key "1");
83
84   require (tarball_created version);
85   require (tarball_tested version);
86
87   sh "
88     cd %s
89     cp %s/tarballs/%s %s
90     cvs add -kb %s
91     cvs ci -m \"Version %s\"
92   " libguestfs_website_cvs
93     buildtmp version.tarball version.urlpath
94     version.urlpath
95     version.version
96
97 (* Goal: website (local copy) has been built. *)
98 and website_built version =
99   let index_file = sprintf "%s/index.html" libguestfs_website_cvs in
100   target (file_contains_string index_file version.version);
101
102   require (tarball_created version);
103   require (tarball_tested version);
104
105   (* We should only update the website on development releases. *)
106   assert (not version.is_stable);
107
108   sh "
109     tar zxf %s/tarballs/%s
110     cd %s
111
112     echo %s > localconfigure
113     chmod +x localconfigure
114     echo %s > localenv
115
116     ./localconfigure
117     make
118     make website
119   " buildtmp version.tarball
120     version.package_version
121     (quote (libguestfs_localconfigure `Tarball))
122     (quote libguestfs_localenv)
123
124 (* Goal: the tarball has passed the required set of tests before
125  * a release is allowed.
126  *)
127 and tarball_tested version =
128   let key = sprintf "libguestfs_tarball_tested_%s" version.version in
129   target (memory_exists key);
130   onrun (fun () -> memory_set key "1");
131
132   require (tarball_created version);
133
134   sh "
135     tar zxf %s/tarballs/%s
136     cd %s
137
138     echo %s > localconfigure
139     chmod +x localconfigure
140     echo %s > localenv
141
142     ./localconfigure
143     make
144     make check-release
145   " buildtmp version.tarball
146     version.package_version
147     (quote (libguestfs_localconfigure `Tarball))
148     (quote libguestfs_localenv)
149
150 (* Goal: the tarball has been created from git. *)
151 and tarball_created version =
152   let filename = sprintf "%s/tarballs/%s" buildtmp version.tarball in
153   target (file_exists filename);
154
155   let repodir = sprintf "%s/repos/%s-%s" buildtmp package version.branch in
156   require (directory_exists repodir);
157
158   sh "
159     cp -a %s libguestfs
160     cd libguestfs
161     git reset --hard %s
162
163     echo %s > localconfigure
164     chmod +x localconfigure
165     echo %s > localenv
166
167     ./localconfigure
168     make
169     make dist
170     mv %s %s/tarballs/%s
171   " repodir
172     version.version
173     (quote (libguestfs_localconfigure `Git))
174     (quote libguestfs_localenv)
175     version.tarball buildtmp version.tarball
176
177 (* Goal: test a commit. *)
178 and commit_tested branch commit =
179   onfail (
180     fun _ ->
181       let subject = sprintf "goal: %s: FAILED" goalname in
182       mailto ~from ~subject ~attach:[logfile] to_
183   );
184
185   let key = sprintf "libguestfs_commit_tested_%s" commit in
186   target (memory_exists key);
187   onrun (fun () -> memory_set key "1");
188
189   let repodir = sprintf "%s/repos/%s-%s" buildtmp package branch in
190   require (directory_exists repodir);
191
192   sh "
193     cp -a %s libguestfs
194     cd libguestfs
195     git reset --hard %s
196
197     echo %s > localconfigure
198     chmod +x localconfigure
199     echo %s > localenv
200
201     ./localconfigure
202     make
203     make check-release
204   " repodir
205     commit
206     (quote (libguestfs_localconfigure `Git))
207     (quote libguestfs_localenv)
208
209 (* Helper function to make a full 'info' object from a version
210  * number.
211  *)
212 let vernames version =
213   Scanf.sscanf version "%d.%d.%d" (
214     fun major minor release ->
215       let is_stable = minor mod 2 = 0 in
216       let branch =
217         if is_stable then
218           sprintf "stable-%d.%d" major minor
219         else
220           sprintf "master" in
221       let package_version = sprintf "%s-%d.%d.%d" package major minor release in
222       let tarball = sprintf "%s.tar.gz" package_version in
223       let urlpath =
224         if is_stable then
225           sprintf "download/%d.%d-stable/%s" major minor tarball
226         else
227           sprintf "download/%d.%d-development/%s" major minor tarball in
228       let url = "http://libguestfs.org/" ^ urlpath in
229       { version = version;
230         major = major; minor = minor; release = release;
231         is_stable = is_stable;
232         branch = branch;
233         package_version = package_version;
234         tarball = tarball;
235         urlpath = urlpath;
236         url = url }
237   )
238
239 (* Helper function to read the latest version in a repo and return
240  * the version.
241  *)
242 let git_latest_version branch =
243   let v = shout "
244     cd %s/repos/%s-%s
245     git describe --tags --abbrev=0
246   " buildtmp package (quote branch) in
247   vernames v
248
249 (* Get the latest commit. *)
250 let git_latest_commit branch =
251   shout "
252     cd %s/repos/%s-%s
253     git rev-parse HEAD
254   " buildtmp package (quote branch)
255
256 (* Clone or update a repo to the latest version on a branch, by force.
257  * It is cached in name = $buildtmp/repos/<package>-<branch>
258  *)
259 let git_force url branch =
260   sh "
261     cd %s/repos
262     if [ ! -d %s-%s ]; then git clone %s %s-%s; fi
263     cd %s-%s
264     git checkout --force %s
265     git pull
266     # Copy or update gnulib
267     git submodule init
268     git submodule update
269   " buildtmp
270     package (quote branch) (quote url) package (quote branch)
271     package (quote branch)
272     (quote branch)
273
274 let () =
275   (* Add a periodic job to check for new git commits and test them. *)
276   every libguestfs_query_mins minutes ~name:"new libguestfs commit" (
277     fun () ->
278       git_force "https://github.com/libguestfs/libguestfs.git" "master";
279
280       let commit = git_latest_commit "master" in
281       require (commit_tested "master" commit);
282   );
283
284   (* Periodic job to build new tarballs. *)
285   every libguestfs_query_mins minutes ~name:"new libguestfs version" (
286     fun () ->
287       git_force "https://github.com/libguestfs/libguestfs.git" "master";
288
289       let version = git_latest_version "master" in
290       require (website_updated version)
291   )
292
293 (* Allow these jobs to run from the command line. *)
294 let () =
295   publish "commit" (
296     function
297     | [commit] -> require (commit_tested "master" commit)
298     | [branch; commit] -> require (commit_tested branch commit)
299     | _ ->
300       failwith "use './libguestfs_upstream commit [<branch>] <commit>'"
301   );
302   publish "release" (
303     function
304     | [version] -> require (website_updated (vernames version))
305     | _ ->
306       failwith "use './libguestfs_upstream release <version>'"
307   )