Skip to content

Commit 52288a1

Browse files
committed
Duplicate Build code for standard backend and Docker backend
1 parent ad14a84 commit 52288a1

13 files changed

Lines changed: 359 additions & 156 deletions

File tree

lib/build.ml

Lines changed: 297 additions & 86 deletions
Large diffs are not rendered by default.

lib/build.mli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,9 @@ module Make (Store : S.STORE) (Sandbox : S.SANDBOX) (Fetch : S.FETCHER) : sig
2727

2828
val v : store:Store.t -> sandbox:Sandbox.t -> t
2929
end
30+
31+
module Docker : sig
32+
include S.BUILDER with type context := Context.t
33+
34+
val v : store:Docker_store.t -> sandbox:Docker_sandbox.t -> t
35+
end

lib/docker.ml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,3 @@ module Extract = struct
166166
in
167167
export_env base
168168
end
169-
170-
module Pull = struct
171-
let fetch ~log ~rootfs base =
172-
ignore rootfs;
173-
ignore log;
174-
Log.debug (fun f -> f "Docker fetcher pull rootfs:%s base:%s" rootfs base);
175-
let* () = pull (`Docker_image base) in
176-
Lwt.return_nil
177-
end

lib/docker.mli

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,3 @@ val obuilder_containers : unit -> [ `Docker_container of string ] list Lwt.t
8585
module Extract : sig
8686
include S.FETCHER
8787
end
88-
89-
(** Fetch (pull) base images using Docker *)
90-
module Pull : sig
91-
include S.FETCHER
92-
end

lib/dune

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,3 @@
1-
(rule
2-
(target sandbox.detected.ml)
3-
(enabled_if
4-
(= %{os_type} Win32))
5-
(action
6-
(copy# sandbox.docker.ml %{target})))
7-
8-
(rule
9-
(target sandbox.detected.ml)
10-
(enabled_if
11-
(<> %{os_type} Win32))
12-
(action
13-
(copy# sandbox.runc.ml %{target})))
14-
15-
(rule
16-
(target sandbox.ml)
17-
(mode fallback)
18-
(action
19-
(copy sandbox.detected.ml %{target})))
20-
211
(rule
222
(target Static_files.ml)
233
(deps
@@ -31,7 +11,7 @@
3111
(preprocess
3212
(pps ppx_sexp_conv))
3313
(modules Btrfs_store Build Build_log Config Dao Db Db_store Docker
34-
Docker_store Escape Log Manifest Obuilder Os S Sandbox Static_files
14+
Docker_store Escape Log Manifest Obuilder Os S Runc_sandbox Docker_sandbox Static_files
3515
Store_spec Tar_transfer Zfs_store)
3616
(libraries lwt lwt.unix fmt yojson tar-unix sexplib sqlite3 astring logs sha
3717
obuilder-spec cmdliner))

lib/obuilder.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,22 @@ module Context = Build.Context
1111
module Btrfs_store = Btrfs_store
1212
module Zfs_store = Zfs_store
1313
module Store_spec = Store_spec
14+
module Docker_store = Docker_store
1415

1516
(** {2 Fetchers} *)
16-
module Docker = Docker
17+
module Docker_extract = Docker.Extract
1718

1819
(** {2 Sandboxes} *)
1920

2021
module Config = Config
21-
module Sandbox = Sandbox
22+
module Runc_sandbox = Runc_sandbox
23+
module Docker_sandbox = Docker_sandbox
2224

2325
(** {2 Builders} *)
2426

2527
module type BUILDER = S.BUILDER with type context := Build.Context.t
2628
module Builder = Build.Make
29+
module Docker_builder = Build.Docker
2730
module Build_log = Build_log
2831

2932
(**/**)
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
open Lwt.Infix
22
open Sexplib.Conv
33

4-
let backend = `Runc
5-
64
let ( / ) = Filename.concat
75

86
type t = {

lib/s.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ end
6464
module type SANDBOX = sig
6565
type t
6666

67-
val backend : [ `Runc | `Docker | `Mock ]
68-
6967
val run :
7068
cancelled:unit Lwt.t ->
7169
?stdin:Os.unix_fd ->

main.ml

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ open Lwt.Infix
22

33
let ( / ) = Filename.concat
44

5-
module Sandbox = Obuilder.Sandbox
5+
module Runc_sandbox = Obuilder.Runc_sandbox
66

77
type builder = Builder : (module Obuilder.BUILDER with type t = 'a) * 'a -> builder
88

@@ -13,26 +13,39 @@ let log tag msg =
1313
| `Output -> output_string stdout msg; flush stdout
1414

1515
let create_builder spec conf =
16-
Obuilder.Store_spec.to_store spec >>= fun (Store ((module Store), store)) ->
17-
let (module Fetcher : Obuilder.S.FETCHER) =
18-
match Sandbox.backend with
19-
| `Runc | `Mock -> (module Obuilder.Docker.Extract : Obuilder.S.FETCHER)
20-
| `Docker -> (module Obuilder.Docker.Pull : Obuilder.S.FETCHER)
21-
in
22-
let module Builder = Obuilder.Builder(Store)(Sandbox)(Fetcher) in
23-
Sandbox.create ~state_dir:(Store.state_dir store / "sandbox") conf >|= fun sandbox ->
16+
let open Obuilder in
17+
Store_spec.to_store spec >>= fun (Store ((module Store), store)) ->
18+
let module Builder = Builder (Store) (Runc_sandbox) (Docker_extract) in
19+
Runc_sandbox.create ~state_dir:(Store.state_dir store / "sandbox") conf >|= fun sandbox ->
2420
let builder = Builder.v ~store ~sandbox in
2521
Builder ((module Builder), builder)
2622

23+
let create_docker_builder path conf =
24+
let open Obuilder in
25+
let module Builder = Docker_builder in
26+
Docker_store.create path >>= fun store ->
27+
Docker_sandbox.create ~state_dir:(Docker_store.state_dir store / "sandbox") conf >|= fun sandbox ->
28+
let builder = Docker_builder.v ~store ~sandbox in
29+
Builder ((module Docker_builder), builder)
30+
2731
let read_whole_file path =
2832
let ic = open_in_bin path in
2933
Fun.protect ~finally:(fun () -> close_in ic) @@ fun () ->
3034
let len = in_channel_length ic in
3135
really_input_string ic len
3236

33-
let build () store spec conf src_dir secrets =
37+
let build () store docker_backend spec runc_conf docker_conf src_dir secrets =
3438
Lwt_main.run begin
35-
create_builder store conf >>= fun (Builder ((module Builder), builder)) ->
39+
begin match store, docker_backend with
40+
| None, None ->
41+
Fmt.epr "Must select either a store or the Docker backend@.";
42+
exit 1
43+
| Some _, Some _ ->
44+
Fmt.epr "Cannot select a store and the Docker backend@.";
45+
exit 1
46+
| Some store, None -> create_builder store runc_conf
47+
| None, Some path -> create_docker_builder path docker_conf
48+
end >>= fun (Builder ((module Builder), builder)) ->
3649
let spec =
3750
try Obuilder.Spec.t_of_sexp (Sexplib.Sexp.load_sexp spec)
3851
with Failure msg ->
@@ -107,14 +120,30 @@ let src_dir =
107120
let store_t =
108121
Arg.conv Obuilder.Store_spec.(of_string, pp)
109122

110-
let store =
123+
let store_required =
111124
Arg.required @@
112125
Arg.opt Arg.(some store_t) None @@
113126
Arg.info
114-
~doc:"zfs:pool or btrfs:/path or docker:/path for build cache"
127+
~doc:"zfs:pool or btrfs:/path for build cache"
115128
~docv:"STORE"
116129
["store"]
117130

131+
let store =
132+
Arg.value @@
133+
Arg.opt Arg.(some store_t) None @@
134+
Arg.info
135+
~doc:"zfs:pool or btrfs:/path for build cache"
136+
~docv:"STORE"
137+
["store"]
138+
139+
let docker_backend =
140+
Arg.value @@
141+
Arg.opt Arg.(some string) None @@
142+
Arg.info
143+
~doc:"Use the Docker store and sandbox backend. Use $(docv) for temporary files."
144+
~docv:"path"
145+
["docker-backend"]
146+
118147
let id =
119148
Arg.required @@
120149
Arg.pos 0 Arg.(some string) None @@
@@ -133,12 +162,13 @@ let secrets =
133162

134163
let build =
135164
let doc = "Build a spec file." in
136-
Term.(const build $ setup_log $ store $ spec_file $ Sandbox.cmdliner $ src_dir $ secrets),
165+
Term.(const build $ setup_log $ store $ docker_backend $ spec_file
166+
$ Obuilder.Runc_sandbox.cmdliner $ Obuilder.Docker_sandbox.cmdliner $ src_dir $ secrets),
137167
Term.info "build" ~doc
138168

139169
let delete =
140170
let doc = "Recursively delete a cached build result." in
141-
Term.(const delete $ setup_log $ store $ Sandbox.cmdliner $ id),
171+
Term.(const delete $ setup_log $ store_required $ Runc_sandbox.cmdliner $ id),
142172
Term.info "delete" ~doc
143173

144174
let buildkit =
@@ -155,7 +185,7 @@ let dockerfile =
155185

156186
let healthcheck =
157187
let doc = "Perform a self-test" in
158-
Term.(const healthcheck $ setup_log $ store $ Sandbox.cmdliner),
188+
Term.(const healthcheck $ setup_log $ store_required $ Runc_sandbox.cmdliner),
159189
Term.info "healthcheck" ~doc
160190

161191
let cmds = [build; delete; dockerfile; healthcheck]

0 commit comments

Comments
 (0)