text stringlengths 0 601k |
|---|
let maybe_return x = Lwt . return ( Some ( x ) ) |
let maybe_fail _ = Lwt . return_none |
let send_lines p data = let channel = p # stdin in Lwt . finalize ( fun ( ) -> Lwt_io . write_lines channel data ) ( fun ( ) -> Lwt_io . close channel ) |
let recv_anything f p channel = let streamf _ = Lwt . catch ( function ( ) -> f channel >>= maybe_return ) ( function | Unix . Unix_error ( Unix . EPIPE , _ , _ ) | End_of_file -> Lwt_io . close channel >>= fun _ -> Lwt . return_none | exn -> Lwt . fail exn ) in Gc . finalise ( fun x -> ignore ( Lwt_io . close x ) ) ch... |
let recv_chars p channel = recv_anything Lwt_io . read_char p channel |
let recv_lines p channel = recv_anything Lwt_io . read_line p channel |
type ' a monitor_state = | Monitor_Initial | Monitor_Remember of ' a option Lwt . t | Monitor_Final |
let monitor sender stream = let sentinel = sender >>= maybe_fail in let state = ref Monitor_Initial in let streamf ( ) = match ! state with | Monitor_Initial -> let getter = Lwt . apply Lwt_stream . get stream >>= fun got -> match got with | Some ( x ) -> Lwt . return_some x | None -> sentinel in let answer _ = match L... |
let string_chomp s = let n = String . length s in if n > 0 && s . [ n - 1 ] = ' \ n ' then String . sub s 0 ( n - 1 ) else s |
let string_split delim s = let q = Queue . create ( ) in let b = Buffer . create 100 in let add ( ) = ( Queue . add ( Buffer . contents b ) q ; Buffer . clear b ) in let loop c = if c = delim then add ( ) else Buffer . add_char b c in String . iter loop s ; add ( ) ; List . rev ( Queue . fold ( fun ax item -> item :: a... |
let is_debugged command = List . mem command ( try string_split ' ' : ( Sys . getenv " RASHELL_DEBUG " ) with Not_found -> [ ] ) |
type process_status = Unix . process_status = | WEXITED of int | WSIGNALED of int | WSTOPPED of int |
type t = { program : string ; argv : string array ; env : string array option ; workdir : string option ; } |
let command_name cmd = try List . hd ( List . rev ( string_split ' ' / ( if cmd . program = " " then cmd . argv . ( 0 ) else cmd . program ) ) ) with _ -> " " |
let pp_print_process_status fft = function | WEXITED ( n ) -> Format . fprintf fft " WEXITED ( % d ) " n | WSIGNALED ( n ) -> Format . fprintf fft " WSIGNALED ( % d ) " n | WSTOPPED ( n ) -> Format . fprintf fft " WSTOPPED ( % d ) " n |
let pp_print_command fft cmd = let pp_print_field fft key value = Format . fprintf fft " [ @< 2 >% s = % S ; ] @@ " key value in let pp_print_array fft key a = Format . fprintf fft " [ @< 2 >% s = [ [ @< 2 >% a ] ] ; ] @@@ " key ( fun fft a -> Array . iter ( fun s -> Format . fprintf fft " @ % S ; " s ) a ) a in let pp... |
let pp_print_error fft = function | Error ( command , process_status , stderr ) -> Some ( Format . fprintf fft " % s . Error ( % a , % a , % S ) " " Rashell_Command " pp_print_command command pp_print_process_status process_status stderr ) | _ -> None |
let maybe_map f = function | Some ( x ) -> Some ( f x ) | None -> None |
let ( ) = Printexc . register_printer ( fun exn -> maybe_map Format . flush_str_formatter ( pp_print_error Format . str_formatter exn ) ) |
let command ? workdir ? env ( program , argv ) = { program ; argv ; env ; workdir ; } |
let outcome is_test cmd p = lwt_bind2 ( Lwt_stream . to_string ( recv_chars p p # stderr ) ) p # status ( fun stderr status -> if is_debugged ( command_name cmd ) then Format . eprintf " [ @< 2 >% s : outcome :@ % a @ % a @ % s ] @@\ n " %! " Rashell_Command " pp_print_command cmd pp_print_process_status status stderr ... |
let with_workdir path f x = let currentdir = Sys . getcwd ( ) in let ( ) = Sys . chdir path in let answer = f x in let ( ) = Sys . chdir currentdir in answer |
let open_process cmd = let open_process_in_cwd cmd = Lwt_process . open_process_full ? env : cmd . env ( cmd . program , cmd . argv ) in if is_debugged ( command_name cmd ) then Format . eprintf " [ @< 2 >% s : open_process :@ % a ] @@\ n " %! " Rashell_Command " pp_print_command cmd ; match cmd . workdir with | None -... |
let supervise f cmd = try f cmd with Sys_error ( mesg ) -> Lwt . fail ( Error ( cmd , WEXITED ( 127 ) , mesg ) ) |
let exec_utility_unsafe ( ? chomp = false ) cmd = let p = open_process cmd in Lwt_stream . to_string ( recv_chars p p # stdout ) >>= fun a -> ( outcome false cmd p >>= fun ( ) -> if chomp then Lwt . return ( string_chomp a ) else Lwt . return a ) |
let exec_utility ? chomp cmd = supervise ( exec_utility_unsafe ? chomp ) cmd |
let exec_test_unsafe cmd = let p = open_process cmd in Lwt_stream . to_string ( recv_chars p p # stdout ) >>= fun _ -> p # status >>= fun status -> ( outcome true cmd p >>= fun ( ) -> match status with | WEXITED ( 0 ) -> Lwt . return_true | WEXITED ( 1 ) -> Lwt . return_false | _ -> Printf . ksprintf Lwt . fail_with " ... |
let exec_test cmd = supervise exec_test_unsafe cmd |
let exec_query cmd = try let p = open_process cmd in monitor ( outcome false cmd p ) ( recv_lines p p # stdout ) with Sys_error ( mesg ) -> Lwt_stream . from ( fun ( ) -> Lwt . fail ( Error ( cmd , WEXITED ( 127 ) , mesg ) ) ) |
let exec_filter cmd lines = try let p = open_process cmd in let sender = let p1 = send_lines p lines and p2 = outcome false cmd p in lwt_bind2 p1 p2 ( fun ( ) ( ) -> Lwt . return_unit ) in monitor sender ( recv_lines p p # stdout ) with Sys_error ( mesg ) -> Lwt_stream . from ( fun ( ) -> Lwt . fail ( Error ( cmd , WEX... |
let exec_shell_unsafe cmd = let p = let open_process cmd = Lwt_process . open_process_none ? env : cmd . env ( cmd . program , cmd . argv ) in match cmd . workdir with | None -> open_process cmd | Some ( otherdir ) -> with_workdir otherdir open_process cmd in p # status >>= fun status -> match status with | WEXITED ( 0... |
let exec_shell cmd = supervise exec_shell_unsafe cmd |
let which ? path file = let actual_path = match path with | Some ( path ) -> path | None -> ( try string_split ' ' : ( Sys . getenv " PATH " ) with Not_found -> [ ] ) in let loop ax path = match ax with | Some ( _ ) -> ax | None -> ( if Sys . file_exists ( Filename . concat path file ) then Some ( Filename . concat pat... |
let expand_path name = let open Str in let buf = Buffer . create ( String . length name ) in let getenv key = try Sys . getenv key with Not_found -> " " in let homedir_other user = try Unix . ( ( getpwnam user ) . pw_dir ) with Not_found -> Printf . ksprintf failwith " Rashell_Command . expand_path : % S : Cannot find ... |
let chomp s = string_chomp s |
module Glob = struct let rec list_match pattern text = match pattern , text with | [ ] , [ ] -> true | ' ' * :: pattern_tl , [ ] -> list_match pattern_tl [ ] | ' ' * :: pattern_tl , text_hd :: text_tl -> list_match pattern_tl text || list_match pattern text_tl | ' ' ? :: pattern_tl , _ :: text_tl -> list_match pattern_... |
let string_match_glob pattern text = Glob . string_match pattern text |
type restart_policy = | Restart_No | Restart_Always | Restart_On_failure of int | Restart_Unless_Stopped |
type user = User_ID of int | User_Name of string |
type ports = Single of int | Range of int * int |
type volume_source = | Auto | Named of string | Path of string |
type volume_option = | RO | Relabel | Relabel_Private |
type command = { add_host : ( string * string ) list option ; argv : string array option ; cap_add : string list option ; cap_drop : string list option ; device : string list option ; entrypoint : string option ; env : string array option ; expose : int list option ; hostname : string option ; image_id : image_id ; lab... |
let ps_keyword = [ " CONTAINER ID " ; " IMAGE " ; " COMMAND " ; " CREATED " ; " STATUS " ; " PORTS " ; " NAMES " ; ] |
let image_keyword = [ " REPOSITORY " ; " TAG " ; " IMAGE ID " ; " CREATED " ; " ( \\ VIRTUAL ) \\? SIZE " ; ] |
let image_keyword_map = [ " REPOSITORY " , " REPOSITORY " ; " TAG " , " TAG " ; " IMAGE ID " , " IMAGE ID " ; " CREATED " , " CREATED " ; " VIRTUAL SIZE " , " SIZE " ; " SIZE " , " SIZE " ; ] |
type field = { field_name : string ; field_position : int ; field_width : int option ; } |
let error fmt = Printf . ksprintf ( fun s -> failwith ( " Rashell_Docker " " ^: " ^ s ) ) fmt |
let field_make kwlist header = let open Str in let pat = regexp ( " ( " \\ ^ ( String . concat " " \\| kwlist ) ^ " ) " ) \\ in let pat2 = regexp " ( \\ ) " *\\ in let rec loop ax i = match string_match pat header i , i = String . length header with | false , false -> error " field_make : Protocol mismatch . " | false ... |
let field_trim s = let open Str in global_replace ( regexp " ( \\^ *\\| ) " ) *$\\ " " s |
let field_get f s = let get ( ) = match f . field_width with | Some ( k ) -> String . sub s f . field_position k | None -> String . sub s f . field_position ( String . length s - f . field_position ) in try field_trim ( get ( ) ) with _ -> error " field_extract : Protocol mismatch . " |
let field_extract lst s = List . map ( fun f -> ( f . field_name , field_get f s ) ) lst |
let to_alist name kwlist lst = match lst with | hd :: tl -> Lwt . return ( List . map ( field_extract ( field_make kwlist hd ) ) tl ) | _ -> Lwt . fail_with ( " Rashell_Docker " " ^: " ^ name " ^: Protocol mismatch . " ) |
let tags ( ) = let triple_of_alist alist = let get field = List . assoc field alist in try ( get " IMAGE ID " , ( get " REPOSITORY " , get " TAG " ) ) with Not_found -> failwith ( " Rashell_Docker " " ^: images : Protocol mismatch . " ) in let pack lst = let images = Pool . elements ( List . fold_right Pool . add ( Lis... |
let _inspect of_json lst = let convert s = try Lwt . return ( of_json s ) with Ag_oj_run . Error ( mesg ) | Yojson . Json_error ( mesg ) -> prerr_endline s ; Printf . ksprintf Lwt . fail_with " % s . _inspect : % S : % s " " Rashell_Docker " s mesg in if lst = [ ] then Lwt . return [ ] else ( exec_utility ( command ( "... |
let _list resource of_json ( ) = Lwt_stream . to_list ( exec_query ( command ( " " , [ | ac_path_docker ; resource ; " -- all = true " ; " -- quiet = true " ; ] ) ) ) | >>= _inspect of_json |
let ps = _list " ps " containers_of_string |
let images = _list " images " images_of_string |
let _exec argv lst = if lst = [ ] then Lwt . return_unit else ( exec_utility ( command ( " " , Array . concat [ [ | ac_path_docker ; ] ; | argv ; Array . of_list lst ; ] ) ) ) |> Lwt . map ignore |
let stop lst = _exec [ | " stop " ] | lst |
let rm lst = _exec [ | " rm " ] | lst |
let rmi lst = _exec [ | " rmi " ] | lst |
let restart lst = _exec [ | " restart " ] | lst |
let pause lst = _exec [ | " pause " ] | lst |
let unpause lst = _exec [ | " unpause " ] | lst |
let maybe_get = function | None -> [ | ] | | Some ( array ) -> array |
let maybe_map f = function | None -> None | Some ( x ) -> Some ( f x ) |
let maybe_list f = maybe_map ( fun lst -> Array . concat ( List . map f lst ) ) |
let maybe_concat lst = Array . concat ( List . map maybe_get lst ) |
let string_of_volume_option = function | RO -> " : ro " | Relabel -> " : z " | Relabel_Private -> " : Z " |
let docker_args funcname cmd = let open Printf in maybe_concat [ ( maybe_list ( fun ( host , ip ) -> [ | " -- add - host " ; sprintf " % s :% s " host ip ] ) | cmd . add_host ) ; ( maybe_list ( fun cap -> [ | " -- cap - add " ; cap ] ) | cmd . cap_add ) ; ( maybe_list ( fun cap -> [ | " -- cap - drop " ; cap ] ) | cmd ... |
let _run funcname exec detach interactive cmd = let open Printf in let dockerargv = Array . append [ | ac_path_docker ; " run " ; sprintf " -- detach =% b " detach ; sprintf " -- interactive =% b " interactive ; sprintf " -- rm =% b " ( not detach ) ; ] | ( docker_args funcname cmd ) in exec ( command ( " " , dockerarg... |
let __run funcname exec detach interactive cmd = try _run funcname exec detach interactive cmd with Invalid_argument _ as exn -> Lwt . fail exn |
let run = __run " run " ( exec_utility ~ chomp : true ) true false |
let run_utility = __run " run_utility " exec_utility false false |
let run_query = _run " run_query " exec_query false false |
let run_test = __run " run_test " exec_test false false |
let run_shell = __run " run_shell " exec_shell false true |
let create cmd = let open Printf in let dockerargv = Array . append [ | ac_path_docker ; " create " ; ] | ( docker_args " create " cmd ) in exec_utility ~ chomp : true ( command ( " " , dockerargv ) ) |
let start containers = let open Printf in let dockerargv = Array . append [ | ac_path_docker ; " start " ; ] | ( Array . of_list containers ) in exec_test ( command ( " " , dockerargv ) ) |
let command ? add_host ? argv ? cap_add ? cap_drop ? device ? entrypoint ? env ? expose ? hostname ? labels ? link ? memory ? name ? net ? privileged ? publish ? publish_gen ? restart ? tty ? user ? volumes_from ? volumes image_id = { add_host ; argv ; cap_add ; cap_drop ; device ; entrypoint ; env ; expose ; hostname ... |
let ( / ) = Filename . concat |
let flag f option = match option with | Some ( true ) -> [ | f ] | | _ -> [ | ] | |
let option option moptarg = match moptarg with | Some ( optarg ) -> [ | option ; optarg ] | | None -> [ | ] | |
let option_no_space option moptarg = match moptarg with | Some ( optarg ) -> [ | Printf . sprintf " % s =% s " option optarg ] | | None -> [ | ] | |
let maybe_transform f = function | None -> [ | ] | | Some ( x ) -> f ( x ) |
let topleveldir ? workdir ( ) = exec_utility ~ chomp : true ( command ? workdir ( " " , [ | ac_path_git ; " rev - parse " ; " -- show - toplevel " ] ) ) | |
let _hook_dir_from_topleveldir dir = dir / " . git " / " hooks " |
let _hook_dir ? workdir ( ) = Lwt . map _hook_dir_from_topleveldir ( topleveldir ? workdir ( ) ) |
let hook_list ? workdir ( ) = let predicate = And [ Or [ Has_kind S_REG ; Has_kind S_LNK ; ] ; Not ( Name ( " . " ) ) ** ] in _hook_dir ? workdir ( ) >>= fun dir -> Lwt_stream . to_list ( find predicate [ dir ] ) |
let hook_install ? workdir ? force name program = _hook_dir ? workdir ( ) >>= fun dir -> Lwt_stream . to_list ( ln ? force ~ symbolic : true [ program ] ( dir / name ) ) |> Lwt . map ignore |
let hook_install_script ? workdir ( ? force = false ) ( ? perm = 0o700 ) name script = _hook_dir ? workdir ( ) >>= fun dir -> Lwt_io . with_file ~ flags : Unix . ( [ O_WRONLY ; O_CREAT ; O_TRUNC ] ( @ if force then [ ] else [ O_EXCL ] ) ) ~ perm ~ mode : Lwt_io . output ( dir / name ) ( fun ch -> Lwt_io . write ch scri... |
let hook_run ? workdir ? env ( ? important = false ) ( cmd , argv ) = topleveldir ? workdir ( ) >>= fun dir -> let hook = _hook_dir_from_topleveldir dir / cmd in test ~ follow : true Prune hook >>= function | true -> exec_test ( command ? workdir ? env ( hook , argv ) ) | false -> Lwt . return ( not important ) |
let branch_checkout ? workdir ? env ( ? create = false ) ? start branch = let reallycreate = match create , start with | _ , Some ( _ ) -> Some ( true ) | true , None -> Some ( true ) | false , None -> Some ( false ) in let argv = Array . concat [ [ | ac_path_git ; " checkout " ; ] ; | ( flag " - B " reallycreate ) ; [... |
let branch_list ? workdir ? env ? merged ? all ( ) = let open Str in let garbage = regexp " [ ] ^* \\|^ " in let alias = regexp " . * -> " in let remove_garbage s = replace_first garbage " " s in let is_not_alias s = try ignore ( search_forward alias s 0 ) ; false with Not_found -> true in let argv = Array . concat [ [... |
let branch_current ? workdir ? env ( ) = let argv = [ | ac_path_git ; " rev - parse " ; " -- abbrev - ref " ; " HEAD " ] | in exec_utility ~ chomp : true ( command ? workdir ? env ( ac_path_git , argv ) ) >>= function | " HEAD " -> Printf . ksprintf Lwt . fail_with " Rashell_Git . branch_current : % S : The repository ... |
let is_in_detached_head_state ? workdir ? env ( ) = let argv = [ | ac_path_git ; " rev - parse " ; " -- abbrev - ref " ; " HEAD " ] | in exec_utility ~ chomp : true ( command ? workdir ? env ( ac_path_git , argv ) ) >|= function | " HEAD " -> true | _ -> false |
let tag_create ? workdir ? env ? gpg_sign ? message tag = let argv = Array . concat [ [ | ac_path_git ; " commit " ; ] ; | ( flag " -- sign " gpg_sign ) ; ( option " - m " message ) ; [ | tag ] ; | ] in exec_utility ( command ? workdir ? env ( ac_path_git , argv ) ) >|= ignore |
let tag_list ? workdir ? env ( ) = let argv = [ | ac_path_git ; " tag " ; " -- list " ] ; | in Lwt_stream . to_list ( exec_query ( command ? workdir ? env ( ac_path_git , argv ) ) ) |
let clone ? workdir ? env ? template ? bare ? mirror ? origin ? branch ( ? config = [ ] ) ? depth ? single_branch ? recursive ? reference ? dissociate ? destdir repository = let open Printf in let configuration = Array . concat ( List . map ( fun ( k , v ) -> [ | " -- config " ; sprintf " % s =% s " k v ; ] ) | config ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.