Skip to content

Commit

Permalink
centralize globals
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinMinkov committed Dec 23, 2023
1 parent a72ed07 commit bfe4719
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 313 deletions.
8 changes: 1 addition & 7 deletions src/lib/integration_test_lib/network_state.ml
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,7 @@ module Make
update ~f:(fun state ->
[%log debug]
"Updating network state with initialization event of $node"
~metadata:
[ ("node", `String (Node.infra_id node))
; ( "event"
, Event_type.event_to_yojson
(Event_type.Event (Event_type.Node_initialization, ()))
)
] ;
~metadata:[ ("node", `String (Node.infra_id node)) ] ;
let node_initialization' =
String.Map.set state.node_initialization ~key:(Node.id node)
~data:true
Expand Down
6 changes: 0 additions & 6 deletions src/lib/integration_test_lib/wait_condition.ml
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ struct
( nodes |> List.map ~f:Node.id |> String.concat ~sep:", "
|> sprintf "[%s] to initialize" )
~f:(fun (state : Network_state.t) ->
List.iter nodes ~f:(fun node ->
print_endline (sprintf "DEBUG: node: %s" (Node.id node)) ) ;
String.Map.iteri state.node_initialization ~f:(fun ~key ~data ->
print_endline
(sprintf "DEBUG: node_initialization: %s -> %b" key data) ) ;
print_endline "------------------------------------------" ;
List.for_all nodes ~f:(fun node ->
String.Map.find state.node_initialization (Node.id node)
|> Option.value ~default:false ) )
Expand Down
10 changes: 6 additions & 4 deletions src/lib/integration_test_local_engine/docker_compose.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ module Dockerfile = struct
type t = { version : string; services : service_map } [@@deriving to_yojson]

let to_string = Fn.compose Yojson.Safe.pretty_to_string to_yojson
end

type t = Dockerfile.t [@@deriving to_yojson]

let to_string = Fn.compose Yojson.Safe.pretty_to_string to_yojson
let write_config t ~dir ~filename =
Out_channel.with_file ~fail_if_exists:false
(dir ^ "/" ^ filename)
~f:(fun ch -> t |> to_string |> Out_channel.output_string ch) ;
Util.run_cmd_exn dir "chmod" [ "600"; filename ]
end
112 changes: 46 additions & 66 deletions src/lib/integration_test_local_engine/docker_network.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ let get_container_id service_id =
let open Malleable_error.Let_syntax in
let%bind container_ids =
Deferred.bind ~f:Malleable_error.or_hard_error
(* docker ps is a fuzzy search, we assume that the first result is the most accurate result *)
(Integration_test_lib.Util.run_cmd_or_error cwd "docker"
[ "ps"; "-f"; sprintf "name=%s" service_id; "--quiet" ] )
in
Expand All @@ -26,33 +25,27 @@ let run_in_container ?(exit_code = 10) container_id ~cmd =
Integration_test_lib.Util.run_cmd_or_hard_error ~exit_code cwd "docker"
([ "exec"; container_id ] @ cmd)

type config = { stack_name : string; graphql_enabled : bool }

module Node = struct
type service_info =
type config =
{ network_keypair : Network_keypair.t option
; service_id : string
; is_archive_container : bool
; postgres_connection_uri : string option
; graphql_port : int
}

type t =
{ service_info : service_info
; config : config
; mutable should_be_running : bool
}
type t = { config : config; mutable should_be_running : bool }

let id { service_info; _ } = service_info.service_id
let id { config; _ } = config.service_id

let infra_id { service_info; _ } = service_info.service_id
let infra_id { config; _ } = config.service_id

let should_be_running { should_be_running; _ } = should_be_running

let network_keypair { service_info; _ } = service_info.network_keypair
let network_keypair { config; _ } = config.network_keypair

let get_ingress_uri node =
Uri.make ~scheme:"http" ~host:"127.0.0.1" ~path:"/graphql"
~port:node.service_info.graphql_port ()
~port:node.config.graphql_port ()

let get_container_index_from_service_name service_name =
match String.split_on_chars ~on:[ '_' ] service_name with
Expand All @@ -62,28 +55,23 @@ module Node = struct
failwith "get_container_index_from_service_name: bad service name"

let dump_archive_data ~logger (t : t) ~data_file =
let service_name = t.service_info.service_id in
if not t.service_info.is_archive_container then
failwith
(sprintf "dump_archive_data: %s not an archive container" service_name)
else
let open Malleable_error.Let_syntax in
let%bind container_id = get_container_id service_name in
[%log info] "Dumping archive data from (node: %s, container: %s)"
service_name container_id ;
let postgres_url =
Docker_node_config.Postgres_config.default_connection_uri
~host:
(sprintf "postgres_%s"
(get_container_index_from_service_name service_name) )
in
let%map data =
run_in_container container_id
~cmd:[ "pg_dump"; "--create"; "--no-owner"; postgres_url ]
in
[%log info] "Dumping archive data to file %s" data_file ;
Out_channel.with_file data_file ~f:(fun out_ch ->
Out_channel.output_string out_ch data )
let service_name = t.config.service_id in
match t.config.postgres_connection_uri with
| None ->
failwith
(sprintf "dump_archive_data: %s not an archive container" service_name)
| Some postgres_uri ->
let open Malleable_error.Let_syntax in
let%bind container_id = get_container_id service_name in
[%log info] "Dumping archive data from (node: %s, container: %s)"
service_name container_id ;
let%map data =
run_in_container container_id
~cmd:[ "pg_dump"; "--create"; "--no-owner"; postgres_uri ]
in
[%log info] "Dumping archive data to file %s" data_file ;
Out_channel.with_file data_file ~f:(fun out_ch ->
Out_channel.output_string out_ch data )

let get_logs_in_container container_id =
let%bind.Deferred cwd = Unix.getcwd () in
Expand All @@ -92,9 +80,9 @@ module Node = struct

let dump_mina_logs ~logger (t : t) ~log_file =
let open Malleable_error.Let_syntax in
let%bind container_id = get_container_id t.service_info.service_id in
let%bind container_id = get_container_id t.config.service_id in
[%log info] "Dumping mina logs from (node: %s, container: %s)"
t.service_info.service_id container_id ;
t.config.service_id container_id ;
let%map logs = get_logs_in_container container_id in
[%log info] "Dumping mina logs to file %s" log_file ;
Out_channel.with_file log_file ~f:(fun out_ch ->
Expand All @@ -114,9 +102,9 @@ module Node = struct

let run_replayer ?(start_slot_since_genesis = 0) ~logger (t : t) =
let open Malleable_error.Let_syntax in
let%bind container_id = get_container_id t.service_info.service_id in
let%bind container_id = get_container_id t.config.service_id in
[%log info] "Running replayer on (node: %s, container: %s)"
t.service_info.service_id container_id ;
t.config.service_id container_id ;
let%bind accounts =
run_in_container container_id
~cmd:[ "jq"; "-c"; ".ledger.accounts"; "/root/runtime_config.json" ]
Expand All @@ -135,10 +123,7 @@ module Node = struct
~dest )
>>| ignore
in
let postgres_url =
Docker_node_config.Postgres_config.default_connection_uri
~host:"postgres_1"
in
let postgres_url = Option.value_exn t.config.postgres_connection_uri in
run_in_container container_id
~cmd:
[ "mina-replayer"
Expand All @@ -153,10 +138,10 @@ module Node = struct

let dump_precomputed_blocks ~logger (t : t) =
let open Malleable_error.Let_syntax in
let container_id = t.service_info.service_id in
let container_id = t.config.service_id in
[%log info]
"Dumping precomputed blocks from logs for (node: %s, container: %s)"
t.service_info.service_id container_id ;
t.config.service_id container_id ;
let%bind logs = get_logs_in_container container_id in
(* kubectl logs may include non-log output, like "Using password from environment variable" *)
let log_lines =
Expand Down Expand Up @@ -232,7 +217,7 @@ module Node = struct

let start ~fresh_state node : unit Malleable_error.t =
let open Malleable_error.Let_syntax in
let%bind container_id = get_container_id node.service_info.service_id in
let%bind container_id = get_container_id node.config.service_id in
node.should_be_running <- true ;
let%bind () =
if fresh_state then
Expand All @@ -244,32 +229,28 @@ module Node = struct

let stop node =
let open Malleable_error.Let_syntax in
let%bind container_id = get_container_id node.service_info.service_id in
let%bind container_id = get_container_id node.config.service_id in
node.should_be_running <- false ;
run_in_container ~exit_code:12 container_id ~cmd:[ "/stop.sh" ] >>| ignore
end

module Service_to_deploy = struct
type service_to_deploy_config =
type config =
{ network_keypair : Network_keypair.t option
; is_archive_container : bool
; postgres_connection_uri : string option
; graphql_port : int
}

type t =
{ stack_name : string
; service_name : string
; service_info : service_to_deploy_config
}
type t = { stack_name : string; service_name : string; config : config }

let construct_service stack_name service_name service_info : t =
{ stack_name; service_name; service_info }
let construct_service stack_name service_name config : t =
{ stack_name; service_name; config }

let init_service_to_deploy_config ?(network_keypair = None)
?(is_archive_container = false) ~graphql_port =
{ network_keypair; is_archive_container; graphql_port }
?(postgres_connection_uri = None) ~graphql_port =
{ network_keypair; postgres_connection_uri; graphql_port }

let get_node_from_service t ~config =
let get_node_from_service t =
let%bind cwd = Unix.getcwd () in
let open Malleable_error.Let_syntax in
let service_id = t.stack_name ^ "_" ^ t.service_name in
Expand All @@ -279,13 +260,12 @@ module Service_to_deploy = struct
t.service_name
else
return
{ Node.service_info =
{ Node.config =
{ service_id
; network_keypair = t.service_info.network_keypair
; is_archive_container = t.service_info.is_archive_container
; graphql_port = t.service_info.graphql_port
; network_keypair = t.config.network_keypair
; postgres_connection_uri = t.config.postgres_connection_uri
; graphql_port = t.config.graphql_port
}
; config
; should_be_running = false
}
end
Expand Down Expand Up @@ -345,6 +325,6 @@ let genesis_keypairs { genesis_keypairs; _ } = genesis_keypairs
let all_ids t =
let deployments = all_nodes t |> Core.Map.to_alist in
List.fold deployments ~init:[] ~f:(fun acc (_, node) ->
List.cons node.service_info.service_id acc )
List.cons node.config.service_id acc )

let initialize_infra ~logger network = Malleable_error.return ()
Loading

0 comments on commit bfe4719

Please sign in to comment.