2020-12-08 10:49:26 +00:00
|
|
|
open Lwt_result.Infix
|
2020-12-02 13:33:15 +00:00
|
|
|
|
2021-01-08 12:47:17 +00:00
|
|
|
let src = Logs.Src.create "builder-web.model" ~doc:"Builder_web model"
|
|
|
|
module Log = (val Logs.src_log src : Logs.LOG)
|
2020-12-14 07:18:13 +00:00
|
|
|
|
2021-01-08 12:47:17 +00:00
|
|
|
module type CONN = Caqti_lwt.CONNECTION
|
|
|
|
|
2021-01-21 16:51:58 +00:00
|
|
|
type error = [ Caqti_error.call_or_retrieve | `Not_found | `File_error of Fpath.t | `Msg of string ]
|
2021-01-08 12:47:17 +00:00
|
|
|
|
|
|
|
let pp_error ppf = function
|
|
|
|
| `Not_found -> Format.fprintf ppf "value not found in database"
|
|
|
|
| `File_error path -> Format.fprintf ppf "error reading file %a" Fpath.pp path
|
2021-01-21 16:51:58 +00:00
|
|
|
| `Msg e -> Format.fprintf ppf "error %s" e
|
2021-01-08 12:47:17 +00:00
|
|
|
| #Caqti_error.call_or_retrieve as e ->
|
|
|
|
Caqti_error.pp ppf e
|
|
|
|
|
|
|
|
let not_found = function
|
2022-07-15 09:13:11 +00:00
|
|
|
| None -> Lwt_result.fail `Not_found
|
2021-01-08 12:47:17 +00:00
|
|
|
| Some v -> Lwt_result.return v
|
|
|
|
|
2021-03-24 13:49:23 +00:00
|
|
|
let staging datadir = Fpath.(datadir / "_staging")
|
2023-09-14 08:58:09 +00:00
|
|
|
let artifacts_dir datadir = Fpath.(datadir / "_artifacts")
|
|
|
|
let artifact_path artifact =
|
|
|
|
let (`Hex sha256) = Hex.of_cstruct artifact.Builder_db.sha256 in
|
|
|
|
(* NOTE: [sha256] is 64 characters when it's a hex sha256 checksum *)
|
|
|
|
(* NOTE: We add the prefix to reduce the number of files in a directory - a
|
|
|
|
workaround for inferior filesystems. We can easily revert this by changing
|
|
|
|
this function and adding a migration. *)
|
|
|
|
let prefix = String.sub sha256 0 2 in
|
|
|
|
Fpath.(v "_artifacts" / prefix / sha256)
|
2021-03-24 13:49:23 +00:00
|
|
|
|
2021-06-01 15:43:55 +00:00
|
|
|
let read_file datadir filepath =
|
|
|
|
let filepath = Fpath.(datadir // filepath) in
|
2021-01-08 12:47:17 +00:00
|
|
|
Lwt.try_bind
|
|
|
|
(fun () -> Lwt_io.open_file ~mode:Lwt_io.Input (Fpath.to_string filepath))
|
2021-10-18 13:45:53 +00:00
|
|
|
(fun ic ->
|
|
|
|
let open Lwt.Infix in
|
|
|
|
Lwt_io.read ic >>= fun data ->
|
|
|
|
Lwt_io.close ic >>= fun () ->
|
|
|
|
Lwt_result.return data)
|
2021-01-08 12:47:17 +00:00
|
|
|
(function
|
|
|
|
| Unix.Unix_error (e, _, _) ->
|
2022-02-21 11:11:06 +00:00
|
|
|
Log.warn (fun m -> m "Error reading local file %a: %s"
|
2021-01-08 12:47:17 +00:00
|
|
|
Fpath.pp filepath (Unix.error_message e));
|
|
|
|
Lwt.return_error (`File_error filepath)
|
|
|
|
| e -> Lwt.fail e)
|
|
|
|
|
2021-06-05 06:13:46 +00:00
|
|
|
let build_artifact build filepath (module Db : CONN) =
|
2021-01-08 12:47:17 +00:00
|
|
|
Db.find_opt Builder_db.Build_artifact.get_by_build_uuid (build, filepath)
|
2021-06-05 06:13:46 +00:00
|
|
|
>>= not_found >|= snd
|
|
|
|
|
2021-09-08 09:10:30 +00:00
|
|
|
let build_artifact_by_id id (module Db : CONN) =
|
|
|
|
Db.find Builder_db.Build_artifact.get id
|
|
|
|
|
2021-06-05 06:13:46 +00:00
|
|
|
let build_artifact_data datadir file =
|
2023-09-14 08:58:09 +00:00
|
|
|
read_file datadir (artifact_path file)
|
2020-12-14 07:18:13 +00:00
|
|
|
|
2021-01-08 12:47:17 +00:00
|
|
|
let build_artifacts build (module Db : CONN) =
|
|
|
|
Db.collect_list Builder_db.Build_artifact.get_all_by_build build >|=
|
|
|
|
List.map snd
|
|
|
|
|
2021-12-14 11:02:35 +00:00
|
|
|
let solo5_manifest datadir file =
|
2023-09-14 08:58:09 +00:00
|
|
|
let buf = Owee_buf.map_binary Fpath.(to_string (datadir // artifact_path file)) in
|
2022-02-25 11:21:16 +00:00
|
|
|
Solo5_elftool.query_manifest buf |> Result.to_option
|
2021-12-14 11:02:35 +00:00
|
|
|
|
2021-11-08 10:55:11 +00:00
|
|
|
let platforms_of_job id (module Db : CONN) =
|
|
|
|
Db.collect_list Builder_db.Build.get_platforms_for_job id
|
|
|
|
|
2021-01-08 12:47:17 +00:00
|
|
|
let build uuid (module Db : CONN) =
|
|
|
|
Db.find_opt Builder_db.Build.get_by_uuid uuid >>=
|
|
|
|
not_found
|
2020-12-02 13:33:15 +00:00
|
|
|
|
2021-11-08 10:55:11 +00:00
|
|
|
let build_with_main_binary job platform (module Db : CONN) =
|
2021-11-17 16:00:58 +00:00
|
|
|
Db.find_opt Builder_db.Build.get_latest_successful_with_binary (job, platform) >|=
|
2021-11-05 12:49:16 +00:00
|
|
|
Option.map (fun (_id, build, file) -> (build, file))
|
2021-01-29 13:34:56 +00:00
|
|
|
|
2021-02-02 08:34:21 +00:00
|
|
|
let build_hash hash (module Db : CONN) =
|
2021-08-31 11:59:45 +00:00
|
|
|
Db.find_opt Builder_db.Build.get_with_jobname_by_hash hash
|
2021-02-02 08:34:21 +00:00
|
|
|
|
2021-01-22 12:13:09 +00:00
|
|
|
let build_exists uuid (module Db : CONN) =
|
|
|
|
Db.find_opt Builder_db.Build.get_by_uuid uuid >|=
|
|
|
|
Option.is_some
|
|
|
|
|
2021-11-17 15:28:15 +00:00
|
|
|
let latest_successful_build job_id platform (module Db : CONN) =
|
2021-11-18 10:56:17 +00:00
|
|
|
Db.find_opt Builder_db.Build.get_latest_successful (job_id, platform)
|
2021-11-08 10:55:11 +00:00
|
|
|
|
2021-11-17 15:28:15 +00:00
|
|
|
let latest_successful_build_uuid job_id platform db =
|
|
|
|
latest_successful_build job_id platform db >|= fun build ->
|
|
|
|
Option.map (fun build -> build.Builder_db.Build.uuid) build
|
2021-06-02 12:23:40 +00:00
|
|
|
|
2021-11-17 15:28:15 +00:00
|
|
|
let previous_successful_build_different_output id (module Db : CONN) =
|
|
|
|
Db.find_opt Builder_db.Build.get_previous_successful_different_output id
|
|
|
|
|
|
|
|
let next_successful_build_different_output id (module Db : CONN) =
|
|
|
|
Db.find_opt Builder_db.Build.get_next_successful_different_output id
|
2021-04-23 13:00:12 +00:00
|
|
|
|
2021-11-17 15:54:13 +00:00
|
|
|
let failed_builds ~start ~count platform (module Db : CONN) =
|
2021-11-18 10:56:17 +00:00
|
|
|
Db.collect_list Builder_db.Build.get_all_failed (start, count, platform)
|
2021-11-17 14:02:04 +00:00
|
|
|
|
2021-07-08 11:33:27 +00:00
|
|
|
let builds_with_different_input_and_same_main_binary id (module Db : CONN) =
|
|
|
|
Db.collect_list Builder_db.Build.get_different_input_same_output_input_ids id >>= fun ids ->
|
|
|
|
Lwt_list.fold_left_s (fun acc input_id ->
|
|
|
|
match acc with
|
|
|
|
| Error _ as e -> Lwt.return e
|
|
|
|
| Ok metas ->
|
|
|
|
Db.find Builder_db.Build.get_one_by_input_id input_id >>= fun build ->
|
|
|
|
Lwt.return (Ok (build :: metas)))
|
|
|
|
(Ok []) ids
|
|
|
|
|
|
|
|
let builds_with_same_input_and_same_main_binary id (module Db : CONN) =
|
|
|
|
Db.collect_list Builder_db.Build.get_same_input_same_output_builds id
|
|
|
|
|
|
|
|
let builds_with_same_input_and_different_main_binary id (module Db : CONN) =
|
|
|
|
Db.collect_list Builder_db.Build.get_same_input_different_output_hashes id >>= fun hashes ->
|
|
|
|
Lwt_list.fold_left_s (fun acc hash ->
|
|
|
|
match acc with
|
|
|
|
| Error _ as e -> Lwt.return e
|
|
|
|
| Ok metas ->
|
2021-08-31 11:59:45 +00:00
|
|
|
Db.find Builder_db.Build.get_by_hash hash >>= fun build ->
|
2021-07-08 11:33:27 +00:00
|
|
|
Lwt.return (Ok (build :: metas)))
|
|
|
|
(Ok []) hashes
|
|
|
|
|
2021-09-09 16:06:08 +00:00
|
|
|
let build_console_by_uuid datadir uuid (module Db : CONN) =
|
|
|
|
build uuid (module Db) >>= fun (_id, { Builder_db.Build.console; _ })->
|
|
|
|
read_file datadir console
|
|
|
|
|
|
|
|
let build_script_by_uuid datadir uuid (module Db : CONN) =
|
|
|
|
build uuid (module Db) >>= fun (_id, { Builder_db.Build.script; _ })->
|
|
|
|
read_file datadir script
|
|
|
|
|
2021-06-02 13:05:10 +00:00
|
|
|
let job_id job_name (module Db : CONN) =
|
2021-06-09 09:48:51 +00:00
|
|
|
Db.find_opt Builder_db.Job.get_id_by_name job_name
|
2021-06-02 13:05:10 +00:00
|
|
|
|
2021-06-30 12:47:30 +00:00
|
|
|
let readme job (module Db : CONN) =
|
2021-06-09 09:48:51 +00:00
|
|
|
job_id job (module Db) >>= not_found >>= fun job_id ->
|
2021-06-30 12:47:30 +00:00
|
|
|
Db.find Builder_db.Tag.get_id_by_name "readme.md" >>= fun readme_id ->
|
|
|
|
Db.find_opt Builder_db.Job_tag.get_value (readme_id, job_id)
|
|
|
|
|
|
|
|
let job_and_readme job (module Db : CONN) =
|
|
|
|
job_id job (module Db) >>= not_found >>= fun job_id ->
|
|
|
|
Db.find Builder_db.Tag.get_id_by_name "readme.md" >>= fun readme_id ->
|
2021-11-08 10:55:11 +00:00
|
|
|
Db.find_opt Builder_db.Job_tag.get_value (readme_id, job_id) >|= fun readme ->
|
|
|
|
job_id, readme
|
|
|
|
|
|
|
|
let builds_grouped_by_output job_id platform (module Db : CONN) =
|
2021-11-18 10:56:17 +00:00
|
|
|
Db.collect_list Builder_db.Build.get_all_artifact_sha (job_id, platform) >>= fun sha ->
|
2021-07-08 11:33:27 +00:00
|
|
|
Lwt_list.fold_left_s (fun acc hash ->
|
2021-11-17 16:39:49 +00:00
|
|
|
match acc with
|
|
|
|
| Error _ as e -> Lwt.return e
|
|
|
|
| Ok builds ->
|
|
|
|
Db.find Builder_db.Build.get_with_main_binary_by_hash hash >|= fun b ->
|
|
|
|
b :: builds)
|
|
|
|
(Ok []) sha >|= List.rev
|
|
|
|
|
|
|
|
let builds_grouped_by_output_with_failed job_id platform ((module Db : CONN) as db) =
|
|
|
|
builds_grouped_by_output job_id platform db >>= fun builds ->
|
2021-11-18 10:56:17 +00:00
|
|
|
Db.collect_list Builder_db.Build.get_failed_builds (job_id, platform) >|= fun failed ->
|
2021-11-17 16:39:49 +00:00
|
|
|
let failed = List.map (fun b -> b, None) failed in
|
|
|
|
let cmp (a, _) (b, _) = Ptime.compare b.Builder_db.Build.start a.Builder_db.Build.start in
|
|
|
|
List.merge cmp builds failed
|
2021-06-07 13:52:37 +00:00
|
|
|
|
2021-06-29 14:59:08 +00:00
|
|
|
let jobs_with_section_synopsis (module Db : CONN) =
|
|
|
|
Db.collect_list Builder_db.Job.get_all_with_section_synopsis ()
|
|
|
|
|
2021-02-15 10:48:10 +00:00
|
|
|
let job_name id (module Db : CONN) =
|
|
|
|
Db.find Builder_db.Job.get id
|
|
|
|
|
2021-01-20 21:50:35 +00:00
|
|
|
let user username (module Db : CONN) =
|
2021-06-08 14:54:23 +00:00
|
|
|
Db.find_opt Builder_db.User.get_user username
|
|
|
|
|
|
|
|
let authorized user_id job_name (module Db : CONN) =
|
2021-06-09 09:48:51 +00:00
|
|
|
job_id job_name (module Db) >>=
|
|
|
|
(function None -> Lwt_result.fail (`Msg "No such job") | Some r -> Lwt_result.return r) >>= fun job_id ->
|
2021-06-08 14:54:23 +00:00
|
|
|
Db.find Builder_db.Access_list.get (user_id, job_id) >|= fun _id ->
|
|
|
|
()
|
2021-01-20 21:50:35 +00:00
|
|
|
|
2021-03-24 13:49:23 +00:00
|
|
|
let cleanup_staging datadir (module Db : Caqti_lwt.CONNECTION) =
|
|
|
|
let cleanup_staged staged =
|
|
|
|
match Uuidm.of_string (Fpath.to_string staged) with
|
|
|
|
| None ->
|
|
|
|
Log.warn (fun m -> m "Non-uuid staged files: %a" Fpath.pp
|
|
|
|
Fpath.(staging datadir // staged));
|
|
|
|
Lwt.return (Bos.OS.Path.delete ~recurse:true Fpath.(staging datadir // staged))
|
|
|
|
| Some uuid ->
|
|
|
|
let staged = Fpath.(staging datadir // staged) in
|
|
|
|
Db.find_opt Builder_db.Build.get_by_uuid uuid >>= function
|
|
|
|
| Some (_id, build) ->
|
|
|
|
Db.find Builder_db.Job.get build.job_id >>= fun job_name ->
|
|
|
|
let destdir = Fpath.(datadir / job_name / Uuidm.to_string uuid) in
|
|
|
|
Lwt.return (Bos.OS.Path.move staged destdir)
|
|
|
|
| None ->
|
|
|
|
Lwt.return (Bos.OS.Path.delete ~recurse:true Fpath.(staging datadir // staged))
|
|
|
|
in
|
|
|
|
Lwt.return (Bos.OS.Dir.contents ~rel:true (staging datadir)) >>= fun stageds ->
|
|
|
|
Lwt_result.ok @@
|
|
|
|
Lwt_list.iter_s
|
|
|
|
(fun staged ->
|
|
|
|
Lwt.map (function
|
|
|
|
| Error (_ as e) ->
|
|
|
|
Log.warn (fun m -> m "Failed cleaning up staged files %a in %a: %a"
|
|
|
|
Fpath.pp staged
|
|
|
|
Fpath.pp Fpath.(staging datadir // staged)
|
|
|
|
pp_error e)
|
|
|
|
| Ok () -> ())
|
|
|
|
(cleanup_staged staged))
|
|
|
|
stageds
|
|
|
|
|
2023-09-14 08:58:09 +00:00
|
|
|
let save path data =
|
2021-01-21 16:51:58 +00:00
|
|
|
let open Lwt.Infix in
|
2021-01-21 17:12:26 +00:00
|
|
|
Lwt.catch
|
|
|
|
(fun () ->
|
2023-09-14 08:58:09 +00:00
|
|
|
Lwt_io.open_file ~mode:Lwt_io.Output (Fpath.to_string path) >>= fun oc ->
|
2021-01-21 17:12:26 +00:00
|
|
|
Lwt_io.write oc data >>= fun () ->
|
|
|
|
Lwt_io.close oc
|
|
|
|
|> Lwt_result.ok)
|
|
|
|
(function
|
|
|
|
| Unix.Unix_error (e, _, _) ->
|
|
|
|
Lwt_result.fail (`Msg (Unix.error_message e))
|
|
|
|
| e -> Lwt.fail e)
|
2021-01-21 16:51:58 +00:00
|
|
|
|
2023-09-14 08:58:09 +00:00
|
|
|
let save_artifacts staging artifacts =
|
2021-01-21 17:12:26 +00:00
|
|
|
List.fold_left
|
2023-09-14 08:58:09 +00:00
|
|
|
(fun r (file, data) ->
|
|
|
|
r >>= fun () ->
|
|
|
|
let (`Hex sha256) = Hex.of_cstruct file.Builder_db.sha256 in
|
|
|
|
let destpath = Fpath.(staging / sha256) in
|
|
|
|
save destpath data)
|
|
|
|
(Lwt_result.return ())
|
|
|
|
artifacts
|
|
|
|
|
|
|
|
let commit_files datadir staging_dir job_name uuid artifacts =
|
|
|
|
(* First we move the artifacts *)
|
|
|
|
Lwt.return (Bos.OS.Dir.create (artifacts_dir datadir)) >>= fun _ ->
|
|
|
|
List.fold_left
|
|
|
|
(fun r artifact ->
|
|
|
|
r >>= fun () ->
|
|
|
|
let (`Hex sha256) = Hex.of_cstruct artifact.Builder_db.sha256 in
|
|
|
|
let src = Fpath.(staging_dir / sha256) in
|
|
|
|
let dest = Fpath.(datadir // artifact_path artifact) in
|
|
|
|
Lwt.return (Bos.OS.Dir.create (Fpath.parent dest)) >>= fun _created ->
|
|
|
|
Lwt.return (Bos.OS.Path.move ~force:true src dest))
|
|
|
|
(Lwt_result.return ())
|
|
|
|
artifacts >>= fun () ->
|
|
|
|
(* Now the staging dir only contains script & console *)
|
2021-06-01 15:43:55 +00:00
|
|
|
let job_dir = Fpath.(datadir / job_name) in
|
2021-03-24 13:49:23 +00:00
|
|
|
let dest = Fpath.(job_dir / Uuidm.to_string uuid) in
|
|
|
|
Lwt.return (Bos.OS.Dir.create job_dir) >>= fun _ ->
|
|
|
|
Lwt.return (Bos.OS.Path.move staging_dir dest)
|
|
|
|
|
2021-11-08 10:55:11 +00:00
|
|
|
let infer_section_and_synopsis artifacts =
|
|
|
|
let infer_synopsis_and_descr switch root =
|
|
|
|
match OpamPackage.Name.Map.find_opt root.OpamPackage.name switch.OpamFile.SwitchExport.overlays with
|
|
|
|
| None -> None, None
|
|
|
|
| Some opam -> OpamFile.OPAM.synopsis opam, OpamFile.OPAM.descr_body opam
|
2021-06-29 14:59:08 +00:00
|
|
|
in
|
2021-11-08 10:55:11 +00:00
|
|
|
let infer_section switch root =
|
2022-11-04 13:51:58 +00:00
|
|
|
let root_pkg = root.OpamPackage.name in
|
2022-12-29 15:41:33 +00:00
|
|
|
let is_unikernel =
|
|
|
|
(* since mirage 4.2.0, the x-mirage-opam-lock-location is emitted *)
|
|
|
|
Option.value ~default:false
|
|
|
|
(Option.map (fun opam ->
|
2023-03-13 15:25:52 +00:00
|
|
|
Option.is_some (OpamFile.OPAM.extended opam "x-mirage-opam-lock-location" Fun.id))
|
2022-12-29 15:41:33 +00:00
|
|
|
(OpamPackage.Name.Map.find_opt root_pkg switch.OpamFile.SwitchExport.overlays))
|
|
|
|
in
|
2022-11-04 13:51:58 +00:00
|
|
|
let root_pkg_name = OpamPackage.Name.to_string root_pkg in
|
2022-12-29 15:41:33 +00:00
|
|
|
if is_unikernel || Astring.String.is_prefix ~affix:"mirage-unikernel-" root_pkg_name then
|
2022-11-04 13:51:58 +00:00
|
|
|
let metrics_influx =
|
|
|
|
let influx = OpamPackage.Name.of_string "metrics-influx" in
|
|
|
|
OpamPackage.Set.exists (fun p -> OpamPackage.Name.equal p.OpamPackage.name influx)
|
|
|
|
switch.OpamFile.SwitchExport.selections.OpamTypes.sel_installed
|
|
|
|
in
|
|
|
|
let mirage_monitoring =
|
|
|
|
let monitoring = OpamPackage.Name.of_string "mirage-monitoring" in
|
|
|
|
match OpamPackage.Name.Map.find_opt root_pkg switch.OpamFile.SwitchExport.overlays with
|
|
|
|
| None -> false
|
|
|
|
| Some opam ->
|
|
|
|
let depends = OpamFile.OPAM.depends opam in
|
|
|
|
OpamFormula.fold_left (fun acc (n', _) ->
|
|
|
|
acc || OpamPackage.Name.equal n' monitoring)
|
|
|
|
false depends
|
|
|
|
in
|
|
|
|
if metrics_influx || mirage_monitoring then
|
2021-11-08 10:55:11 +00:00
|
|
|
"Unikernels (with metrics reported to Influx)"
|
2021-11-03 14:40:04 +00:00
|
|
|
else
|
2021-11-08 10:55:11 +00:00
|
|
|
"Unikernels"
|
2021-11-03 14:40:04 +00:00
|
|
|
else
|
2021-11-08 10:55:11 +00:00
|
|
|
"Packages"
|
2021-06-29 14:59:08 +00:00
|
|
|
in
|
2021-11-08 10:55:11 +00:00
|
|
|
match List.find_opt (fun (p, _) -> String.equal (Fpath.basename p) "opam-switch") artifacts with
|
2021-06-29 15:15:16 +00:00
|
|
|
| None -> None, (None, None)
|
2021-11-08 10:55:11 +00:00
|
|
|
| Some (_, data) ->
|
|
|
|
try
|
|
|
|
let switch = OpamFile.SwitchExport.read_from_string data in
|
|
|
|
let root = switch.OpamFile.SwitchExport.selections.OpamTypes.sel_roots in
|
|
|
|
assert (OpamPackage.Set.cardinal root = 1);
|
|
|
|
let root = OpamPackage.Set.choose root in
|
|
|
|
Some (infer_section switch root), infer_synopsis_and_descr switch root
|
|
|
|
with _ -> None, (None, None)
|
2021-06-29 14:59:08 +00:00
|
|
|
|
2021-07-06 13:41:26 +00:00
|
|
|
let compute_input_id artifacts =
|
|
|
|
let get_hash filename =
|
|
|
|
match List.find_opt (fun b -> Fpath.equal b.Builder_db.filepath filename) artifacts with
|
|
|
|
| None -> None
|
|
|
|
| Some x -> Some x.sha256
|
|
|
|
in
|
|
|
|
match
|
|
|
|
get_hash (Fpath.v "opam-switch"),
|
|
|
|
get_hash (Fpath.v "build-environment"),
|
|
|
|
get_hash (Fpath.v "system-packages")
|
|
|
|
with
|
|
|
|
| Some a, Some b, Some c -> Some (Mirage_crypto.Hash.SHA256.digest (Cstruct.concat [a;b;c]))
|
|
|
|
| _ -> None
|
|
|
|
|
2021-09-07 11:53:51 +00:00
|
|
|
let save_console_and_script staging_dir job_name uuid console script =
|
|
|
|
let out name = Fpath.(v job_name / Uuidm.to_string uuid / name + "txt") in
|
2021-08-31 11:59:45 +00:00
|
|
|
let out_staging name = Fpath.(staging_dir / name + "txt") in
|
|
|
|
let console_to_string console =
|
2021-09-07 10:04:11 +00:00
|
|
|
List.rev_map (fun (delta, data) ->
|
2021-08-31 11:59:45 +00:00
|
|
|
Printf.sprintf "%.3fs:%s\n" (Duration.to_f (Int64.of_int delta)) data)
|
|
|
|
console
|
|
|
|
|> String.concat ""
|
|
|
|
in
|
|
|
|
save (out_staging "script") script >>= fun () ->
|
|
|
|
save (out_staging "console") (console_to_string console) >|= fun () ->
|
2021-09-10 10:52:31 +00:00
|
|
|
(out "console", out "script")
|
2021-08-31 11:59:45 +00:00
|
|
|
|
2021-08-31 12:15:18 +00:00
|
|
|
let prepare_staging staging_dir =
|
|
|
|
Lwt.return (Bos.OS.Dir.create staging_dir) >>= fun created ->
|
|
|
|
if not created
|
|
|
|
then Lwt_result.fail (`Msg "build directory already exists")
|
|
|
|
else Lwt_result.return ()
|
|
|
|
|
2023-09-14 08:58:09 +00:00
|
|
|
(* saving:
|
|
|
|
- for each artifact compute its sha256 checksum -- calling Lwt.pause in
|
|
|
|
between
|
|
|
|
- lookup artifact sha256 in the database and filter them out of the list: not_in_db
|
|
|
|
- mkdir -p _staging/uuid/
|
|
|
|
- save console & script to _staging/uuid/
|
|
|
|
- save each artifact in not_in_db as _staging/uuid/sha256
|
|
|
|
committing:
|
|
|
|
- for each artifact mv _staging/uuid/sha256 _artifacts/sha256
|
|
|
|
(or _artifacts/prefix(sha256)/sha256 where prefix(sha256) is the first two hex digits in sha256)
|
|
|
|
- now _staging/uuid only contains console & script so we mv _staging/uuid _staging/job/uuid
|
|
|
|
potential issues:
|
|
|
|
- race condition in uploading same artifact:
|
|
|
|
* if the artifact already exists in the database and thus filesystem then nothing is done
|
|
|
|
* if the artifact is added to the database and/or filesystem we atomically overwrite it
|
|
|
|
- input_id depends on a sort order?
|
|
|
|
*)
|
|
|
|
|
|
|
|
|
2021-01-21 16:51:58 +00:00
|
|
|
let add_build
|
2022-02-21 11:11:06 +00:00
|
|
|
~datadir
|
2022-03-29 20:27:24 +00:00
|
|
|
~cachedir
|
|
|
|
~configdir
|
2021-06-09 11:54:24 +00:00
|
|
|
user_id
|
2021-07-12 14:10:54 +00:00
|
|
|
((job : Builder.script_job), uuid, console, start, finish, result, raw_artifacts)
|
2021-01-21 16:51:58 +00:00
|
|
|
(module Db : CONN) =
|
2021-01-20 21:50:35 +00:00
|
|
|
let open Builder_db in
|
|
|
|
let job_name = job.Builder.name in
|
2021-06-01 15:43:55 +00:00
|
|
|
let staging_dir = Fpath.(staging datadir / Uuidm.to_string uuid) in
|
2021-03-24 13:49:23 +00:00
|
|
|
let or_cleanup x =
|
2022-06-24 14:45:27 +00:00
|
|
|
Lwt_result.map_error (fun e ->
|
2021-03-24 13:49:23 +00:00
|
|
|
Bos.OS.Dir.delete ~recurse:true staging_dir
|
|
|
|
|> Result.iter_error (fun e ->
|
|
|
|
Log.err (fun m -> m "Failed to remove staging dir %a: %a"
|
|
|
|
Fpath.pp staging_dir
|
|
|
|
pp_error e));
|
|
|
|
e)
|
|
|
|
x
|
|
|
|
in
|
2023-09-14 08:58:09 +00:00
|
|
|
let not_interesting p =
|
|
|
|
String.equal (Fpath.basename p) "README.md" || String.equal (Fpath.get_ext p) ".build-hashes"
|
2021-07-12 14:10:54 +00:00
|
|
|
in
|
2023-09-14 08:58:09 +00:00
|
|
|
begin
|
|
|
|
List.fold_left
|
|
|
|
(fun r (filepath, data) ->
|
|
|
|
r >>= fun acc ->
|
|
|
|
if not_interesting filepath then
|
|
|
|
Lwt_result.return acc
|
|
|
|
else
|
|
|
|
let sha256 = Mirage_crypto.Hash.SHA256.digest (Cstruct.of_string data)
|
|
|
|
and size = String.length data in
|
|
|
|
Lwt_result.ok (Lwt.pause ()) >|= fun () ->
|
|
|
|
({ filepath; sha256; size }, data) :: acc)
|
|
|
|
(Lwt_result.return [])
|
|
|
|
raw_artifacts
|
|
|
|
end >>= fun artifacts ->
|
2021-08-31 12:15:18 +00:00
|
|
|
or_cleanup (prepare_staging staging_dir) >>= fun () ->
|
2021-09-07 11:53:51 +00:00
|
|
|
or_cleanup (save_console_and_script staging_dir job_name uuid console job.Builder.script)
|
2021-08-31 11:59:45 +00:00
|
|
|
>>= fun (console, script) ->
|
2023-09-14 08:58:09 +00:00
|
|
|
List.fold_left
|
|
|
|
(fun r ((f, _) as artifact) ->
|
|
|
|
r >>= fun acc ->
|
|
|
|
Db.find Builder_db.Build_artifact.exists f.sha256 >|= fun exists ->
|
|
|
|
if exists then acc else artifact :: acc)
|
|
|
|
(Lwt_result.return [])
|
|
|
|
artifacts >>= fun artifacts_to_save ->
|
|
|
|
or_cleanup (save_artifacts staging_dir artifacts_to_save) >>= fun () ->
|
|
|
|
let artifacts = List.map fst artifacts in
|
2021-03-24 13:49:23 +00:00
|
|
|
let r =
|
|
|
|
Db.start () >>= fun () ->
|
|
|
|
Db.exec Job.try_add job_name >>= fun () ->
|
2021-06-09 09:48:51 +00:00
|
|
|
Db.find_opt Job.get_id_by_name job_name >>= fun job_id ->
|
|
|
|
Lwt.return (Option.to_result ~none:(`Msg "No such job id") job_id) >>= fun job_id ->
|
2021-06-29 14:59:08 +00:00
|
|
|
let section_tag = "section" in
|
|
|
|
Db.exec Tag.try_add section_tag >>= fun () ->
|
|
|
|
Db.find Tag.get_id_by_name section_tag >>= fun section_id ->
|
|
|
|
let synopsis_tag = "synopsis" in
|
|
|
|
Db.exec Tag.try_add synopsis_tag >>= fun () ->
|
|
|
|
Db.find Tag.get_id_by_name synopsis_tag >>= fun synopsis_id ->
|
2021-06-29 15:15:16 +00:00
|
|
|
let descr_tag = "description" in
|
|
|
|
Db.exec Tag.try_add descr_tag >>= fun () ->
|
|
|
|
Db.find Tag.get_id_by_name descr_tag >>= fun descr_id ->
|
2021-06-30 12:47:30 +00:00
|
|
|
let readme_tag = "readme.md" in
|
|
|
|
Db.exec Tag.try_add readme_tag >>= fun () ->
|
|
|
|
Db.find Tag.get_id_by_name readme_tag >>= fun readme_id ->
|
2021-07-06 13:41:26 +00:00
|
|
|
let input_id = compute_input_id artifacts in
|
2021-11-05 10:45:26 +00:00
|
|
|
let platform = job.Builder.platform in
|
2021-07-08 11:33:27 +00:00
|
|
|
Db.exec Build.add { Build.uuid; start; finish; result;
|
2021-11-05 10:45:26 +00:00
|
|
|
console; script; platform;
|
2021-07-08 11:33:27 +00:00
|
|
|
main_binary = None; input_id; user_id; job_id } >>= fun () ->
|
2021-03-24 13:49:23 +00:00
|
|
|
Db.find last_insert_rowid () >>= fun id ->
|
2021-11-08 10:55:11 +00:00
|
|
|
let sec_syn = infer_section_and_synopsis raw_artifacts in
|
2021-06-30 10:55:00 +00:00
|
|
|
let add_or_update tag_id tag_value =
|
|
|
|
Db.find_opt Job_tag.get_value (tag_id, job_id) >>= function
|
|
|
|
| None -> Db.exec Job_tag.add (tag_id, tag_value, job_id)
|
|
|
|
| Some _ -> Db.exec Job_tag.update (tag_id, tag_value, job_id)
|
|
|
|
in
|
2021-06-29 14:59:08 +00:00
|
|
|
(match fst sec_syn with
|
2021-06-30 12:47:30 +00:00
|
|
|
| None -> Lwt_result.return ()
|
|
|
|
| Some section_v -> add_or_update section_id section_v) >>= fun () ->
|
2021-06-29 14:59:08 +00:00
|
|
|
(match snd sec_syn with
|
2021-06-30 12:47:30 +00:00
|
|
|
| None, _-> Lwt_result.return ()
|
|
|
|
| Some synopsis_v, _ -> add_or_update synopsis_id synopsis_v) >>= fun () ->
|
2021-06-29 15:15:16 +00:00
|
|
|
(match snd sec_syn with
|
2021-06-30 12:47:30 +00:00
|
|
|
| _, None -> Lwt_result.return ()
|
|
|
|
| _, Some descr_v -> add_or_update descr_id descr_v) >>= fun () ->
|
2021-06-30 14:10:02 +00:00
|
|
|
(let readme =
|
|
|
|
List.find_opt (fun (p, _) -> Fpath.(equal (v "README.md") p)) raw_artifacts
|
|
|
|
in
|
|
|
|
let readme_anywhere =
|
|
|
|
List.find_opt (fun (p, _) -> String.equal "README.md" (Fpath.basename p)) raw_artifacts
|
|
|
|
in
|
|
|
|
match readme, readme_anywhere with
|
|
|
|
| None, None -> Lwt_result.return ()
|
|
|
|
| Some (_, data), _ | None, Some (_, data) -> add_or_update readme_id data) >>= fun () ->
|
2022-02-24 11:52:05 +00:00
|
|
|
(match List.partition (fun p -> Fpath.(is_prefix (v "bin/") p.filepath)) artifacts with
|
|
|
|
| [ main_binary ], other_artifacts ->
|
|
|
|
Db.exec Build_artifact.add (main_binary, id) >>= fun () ->
|
|
|
|
Db.find Builder_db.last_insert_rowid () >>= fun main_binary_id ->
|
|
|
|
Db.exec Build.set_main_binary (id, main_binary_id) >|= fun () ->
|
|
|
|
Some main_binary, other_artifacts
|
|
|
|
| [], _ ->
|
|
|
|
Log.debug (fun m -> m "Zero binaries for build %a" Uuidm.pp uuid);
|
|
|
|
Lwt_result.return (None, artifacts)
|
|
|
|
| xs, _ ->
|
|
|
|
Log.warn (fun m -> m "Multiple binaries for build %a: %a" Uuidm.pp uuid
|
|
|
|
Fmt.(list ~sep:(any ",") Fpath.pp)
|
|
|
|
(List.map (fun a -> a.filepath) xs));
|
|
|
|
Lwt_result.return (None, artifacts)) >>= fun (main_binary, remaining_artifacts_to_add) ->
|
2021-03-24 13:49:23 +00:00
|
|
|
List.fold_left
|
|
|
|
(fun r file ->
|
|
|
|
r >>= fun () ->
|
|
|
|
Db.exec Build_artifact.add (file, id))
|
|
|
|
(Lwt_result.return ())
|
2022-02-24 11:52:05 +00:00
|
|
|
remaining_artifacts_to_add >>= fun () ->
|
2021-03-24 13:49:23 +00:00
|
|
|
Db.commit () >>= fun () ->
|
2023-09-14 08:58:09 +00:00
|
|
|
commit_files datadir staging_dir job_name uuid artifacts >|= fun () ->
|
2022-02-21 11:11:06 +00:00
|
|
|
main_binary
|
2021-03-24 13:49:23 +00:00
|
|
|
in
|
2022-06-24 14:45:27 +00:00
|
|
|
Lwt_result.bind_lwt_error (or_cleanup r)
|
2021-03-25 09:42:04 +00:00
|
|
|
(fun e ->
|
|
|
|
Db.rollback ()
|
|
|
|
|> Lwt.map (fun r ->
|
|
|
|
Result.iter_error
|
|
|
|
(fun e' -> Log.err (fun m -> m "Failed rollback: %a" Caqti_error.pp e'))
|
|
|
|
r;
|
2022-02-21 11:11:06 +00:00
|
|
|
e)) >>= function
|
|
|
|
| None -> Lwt.return (Ok ())
|
2022-06-08 10:18:46 +00:00
|
|
|
| Some main_binary ->
|
|
|
|
let time =
|
2022-02-21 11:11:06 +00:00
|
|
|
let (y, m, d), ((hh, mm, ss), _) = Ptime.to_date_time start in
|
|
|
|
Printf.sprintf "%04d%02d%02d%02d%02d%02d" y m d hh mm ss
|
2022-06-08 10:18:46 +00:00
|
|
|
and uuid = Uuidm.to_string uuid
|
2022-02-21 11:11:06 +00:00
|
|
|
and job = job.name
|
2022-02-21 14:50:41 +00:00
|
|
|
and platform = job.platform
|
2022-06-08 10:18:46 +00:00
|
|
|
and `Hex sha256 = Hex.of_cstruct main_binary.sha256
|
2022-02-24 11:52:05 +00:00
|
|
|
in
|
|
|
|
let fp_str p = Fpath.(to_string (datadir // p)) in
|
2022-02-21 11:11:06 +00:00
|
|
|
let args =
|
|
|
|
String.concat " "
|
|
|
|
(List.map (fun s -> "\"" ^ String.escaped s ^ "\"")
|
2022-06-08 10:18:46 +00:00
|
|
|
[ "--build-time=" ^ time ; "--sha256=" ^ sha256 ; "--job=" ^ job ;
|
|
|
|
"--uuid=" ^ uuid ; "--platform=" ^ platform ;
|
|
|
|
"--cache-dir=" ^ Fpath.to_string cachedir ;
|
|
|
|
"--data-dir=" ^ Fpath.to_string datadir ;
|
2023-09-14 08:58:09 +00:00
|
|
|
fp_str Fpath.(datadir // artifact_path main_binary) ])
|
2022-02-21 11:11:06 +00:00
|
|
|
in
|
|
|
|
Log.debug (fun m -> m "executing hooks with %s" args);
|
|
|
|
let dir = Fpath.(configdir / "upload-hooks") in
|
|
|
|
(try
|
|
|
|
Lwt.return (Ok (Some (Unix.opendir (Fpath.to_string dir))))
|
|
|
|
with
|
|
|
|
Unix.Unix_error _ -> Lwt.return (Ok None)) >>= function
|
|
|
|
| None -> Lwt.return (Ok ())
|
|
|
|
| Some dh ->
|
|
|
|
try
|
|
|
|
let is_executable file =
|
|
|
|
let st = Unix.stat (Fpath.to_string file) in
|
|
|
|
st.Unix.st_perm land 0o111 = 0o111 &&
|
|
|
|
st.Unix.st_kind = Unix.S_REG
|
|
|
|
in
|
|
|
|
let rec go () =
|
|
|
|
let next_file = Unix.readdir dh in
|
|
|
|
let file = Fpath.(dir / next_file) in
|
2022-08-26 16:23:23 +00:00
|
|
|
if is_executable file && Fpath.has_ext ".sh" file then
|
2022-02-21 11:11:06 +00:00
|
|
|
ignore (Sys.command (Fpath.to_string file ^ " " ^ args ^ " &"));
|
|
|
|
go ()
|
|
|
|
in
|
|
|
|
go ()
|
|
|
|
with
|
|
|
|
| End_of_file ->
|
|
|
|
Unix.closedir dh;
|
|
|
|
Lwt.return (Ok ())
|