Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file dockerfile_cmd.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141(*
* Copyright (c) 2017 Anil Madhavapeddy <anil@recoil.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*)openSexplib.ConvopenRresultopenBosopenAstringopenR.InfixmoduleOC=OS.Cmdletreciterfnl=matchlwith|hd::tl->fnhd>>=fun()->iterfntl|[]->Ok()letmapfnl=List.mapfnl|>List.fold_left(funaccb->matchacc,bwith|Okacc,Okv->Ok(v::acc)|Ok_acc,Errorv->Errorv|Error_ase,_->e)(Ok[])|>function|Okv->Ok(List.revv)|e->etypecmd_log={command:string;stdout:string;success:bool;status:[`Signaledofint|`Exitedofint]}[@@derivingsexp]letrun_log?(ok_to_fail=true)?envlog_dirnamecmd=letcommand=Cmd.to_stringcmdinOS.Cmd.(run_out?env~err:err_run_out)cmd|>OS.Cmd.out_string>>=fun(stdout,(_,status))->letsuccess=status=`Exited0inletcmd_log={command;stdout;success;status}inletpath=Fpath.(log_dir/(name^".sxp"))inOS.File.writepath(Sexplib.Sexp.to_string_hum(sexp_of_cmd_logcmd_log))>>=fun()->matchstatuswith|`Signaledn->ifok_to_failthenOk()elseR.error_msg(Fmt.strf"Signal %d"n)|`Exited0->Ok()|`Exitedcode->ifok_to_failthenOk()elseR.error_msg(Fmt.strf"Exit code %d"code)(** Docker *)moduleDocker=structletbin=Cmd.(v"docker")letinfo=Cmd.(bin%"info")letexists()=OS.Cmd.run_outinfo|>OS.Cmd.out_string|>R.is_ok|>function|true->Logs.info(funl->l"Docker is running");true|false->Logs.err(funl->l"Docker not running");falseletbuild_cmd?(squash=false)?(pull=true)?(cache=true)?dockerfile?tagpath=letopenCmdinletcache=ifcachethenemptyelsev"--no-cache"inletpull=ifpullthenv"--pull"elseemptyinletsquash=ifsquashthenv"--squash"elseemptyinletdfile=matchdockerfilewithNone->empty|Somed->v"-f"%pdinlettag=matchtagwithNone->empty|Somet->v"-t"%tinbin%"build"%%tag%%cache%%pull%%squash%%dfile%ppathletvolume_cmd=Cmd.(bin%"volume")letpush_cmdtag=Cmd.(bin%"push"%tag)(* Find the image id that we just built *)letbuild_idlog=letrecfind_id=function|hd::tlwhenString.is_prefix~affix:"Successfully tagged "hd->find_idtl|hd::_whenString.is_prefix~affix:"Successfully built "hd->beginmatchString.cut~sep:"Successfully built "hdwith|Some("",id)->R.okid|Some_->R.error_msg"Unexpected internal error in build_id"|None->R.error_msg"Malformed successfully built log"end|_hd::_tl->R.error_msg"Unexpected lines at end of log"|[]->R.error_msg"Unable to find container id in log"inOS.File.read_lineslog>>=funlines->List.revlines|>funlines->find_idlinesletmanifest_push_cli~platforms~template~target=letplatforms=String.concat~sep:","platformsinCmd.(v"manifest-tool"%"push"%"from-args"%"--platforms"%platforms%"--template"%template%"--target"%target)letmanifest_push_filefile=Cmd.(v"manifest-tool"%"push"%"from-spec"%pfile)letrun_cmd?(mounts=[])?(volumes=[])?(rm=true)imgcmd=letrm=ifrmthenCmd.(v"--rm")elseCmd.emptyinletmounts=List.map(fun(src,dst)->["--mount";Fmt.strf"source=%s,destination=%s"srcdst])mounts|>List.flatten|>Cmd.of_listinletvols=List.map(fun(src,dst)->["-v";Fmt.strf"%s:%s"srcdst])volumes|>List.flatten|>Cmd.of_listinCmd.(bin%"run"%%rm%%mounts%%vols%img%%cmd)end(** Opam *)moduleOpam=structletbin=Cmd.(v"opam")letopam_env~root~jobs=OS.Env.current()>>=funenv->String.Map.add"OPAMROOT"(Cmd.proot)env|>String.Map.add"OPAMYES""1"|>String.Map.add"OPAMJOBS"(string_of_intjobs)|>funenv->R.returnenvendopenCmdlinerletsetup_logs()=letsetup_logstyle_rendererlevel=Fmt_tty.setup_std_outputs?style_renderer();Logs.set_levellevel;Logs.set_reporter(Logs_fmt.reporter())inletglobal_option_section="COMMON OPTIONS"inTerm.(constsetup_log$Fmt_cli.style_renderer~docs:global_option_section()$Logs_cli.level~docs:global_option_section())