From dd2169ec7b4b28926ee2f2e7f187de3f9f437627 Mon Sep 17 00:00:00 2001 From: Nurzhan Saken Date: Mon, 1 Jul 2024 14:46:31 +0400 Subject: [PATCH] refactor(swarm): build with `irohad0` instead of dummy builder (#4794) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nurzhan Sakén --- configs/swarm/docker-compose.local.yml | 14 +- configs/swarm/docker-compose.single.yml | 8 +- tools/swarm/README.md | 2 +- tools/swarm/src/lib.rs | 141 +++++++++++++--- tools/swarm/src/schema.rs | 208 +++++++++++++++--------- tools/swarm/src/schema/serde_impls.rs | 13 +- 6 files changed, 256 insertions(+), 130 deletions(-) diff --git a/configs/swarm/docker-compose.local.yml b/configs/swarm/docker-compose.local.yml index 21c9966b69d..63ffcf574ad 100644 --- a/configs/swarm/docker-compose.local.yml +++ b/configs/swarm/docker-compose.local.yml @@ -3,15 +3,9 @@ # Seed: Iroha services: - builder: - image: hyperledger/iroha:local - build: ../.. - pull_policy: never - command: echo ok irohad0: - depends_on: - - builder image: hyperledger/iroha:local + build: ../.. pull_policy: never environment: CHAIN: 00000000-0000-0000-0000-000000000000 @@ -54,7 +48,7 @@ services: " irohad1: depends_on: - - builder + - irohad0 image: hyperledger/iroha:local pull_policy: never environment: @@ -79,7 +73,7 @@ services: start_period: 4s irohad2: depends_on: - - builder + - irohad0 image: hyperledger/iroha:local pull_policy: never environment: @@ -104,7 +98,7 @@ services: start_period: 4s irohad3: depends_on: - - builder + - irohad0 image: hyperledger/iroha:local pull_policy: never environment: diff --git a/configs/swarm/docker-compose.single.yml b/configs/swarm/docker-compose.single.yml index dc5670c20fb..252af253ae1 100644 --- a/configs/swarm/docker-compose.single.yml +++ b/configs/swarm/docker-compose.single.yml @@ -3,15 +3,9 @@ # Seed: Iroha services: - builder: - image: hyperledger/iroha:local - build: ../.. - pull_policy: never - command: echo ok irohad0: - depends_on: - - builder image: hyperledger/iroha:local + build: ../.. pull_policy: never environment: CHAIN: 00000000-0000-0000-0000-000000000000 diff --git a/tools/swarm/README.md b/tools/swarm/README.md index e17670c5906..637b8efe49d 100644 --- a/tools/swarm/README.md +++ b/tools/swarm/README.md @@ -74,4 +74,4 @@ iroha_swarm \ ## Note on configuration structure -When using the `--build` option, the generated configuration will consist of a number of peer services that depend on a dummy `builder` service that only builds the image and terminates. The builder service is needed to avoid redundant building of the same image by every peer. \ No newline at end of file +When using the `--build` option, the first peer in the generated configuration builds the image, while the rest of the peers depend on it. This is needed to avoid redundant building of the same image by every peer. \ No newline at end of file diff --git a/tools/swarm/src/lib.rs b/tools/swarm/src/lib.rs index 66fd62991ff..a3343c61167 100644 --- a/tools/swarm/src/lib.rs +++ b/tools/swarm/src/lib.rs @@ -178,15 +178,9 @@ mod tests { # Single-line banner services: - builder: - image: hyperledger/iroha:dev - build: ../.. - pull_policy: never - command: echo ok irohad0: - depends_on: - - builder image: hyperledger/iroha:dev + build: ../.. pull_policy: never environment: CHAIN: 00000000-0000-0000-0000-000000000000 @@ -216,7 +210,7 @@ mod tests { kagami genesis sign /tmp/genesis.json \\ --public-key $$GENESIS_PUBLIC_KEY \\ --private-key $$GENESIS_PRIVATE_KEY \\ - --out-file $$GENESIS\\ + --out-file $$GENESIS \\ && \\ irohad " @@ -236,16 +230,10 @@ mod tests { # Multi-line banner 2 services: - builder: + irohad0: image: hyperledger/iroha:dev build: ../.. pull_policy: build - command: echo ok - irohad0: - depends_on: - - builder - image: hyperledger/iroha:dev - pull_policy: never environment: CHAIN: 00000000-0000-0000-0000-000000000000 PUBLIC_KEY: ed012087FDCACF58B891947600B0C37795CADB5A2AE6DE612338FDA9489AB21CE427BA @@ -274,7 +262,7 @@ mod tests { kagami genesis sign /tmp/genesis.json \\ --public-key $$GENESIS_PUBLIC_KEY \\ --private-key $$GENESIS_PRIVATE_KEY \\ - --out-file $$GENESIS\\ + --out-file $$GENESIS \\ && \\ irohad " @@ -287,6 +275,115 @@ mod tests { )); } + #[test] + fn multiple_build_banner_nocache() { + expect_test::expect!([r##" + # Single-line banner + + services: + irohad0: + image: hyperledger/iroha:dev + build: ../.. + pull_policy: build + environment: + CHAIN: 00000000-0000-0000-0000-000000000000 + PUBLIC_KEY: ed012087FDCACF58B891947600B0C37795CADB5A2AE6DE612338FDA9489AB21CE427BA + PRIVATE_KEY: 802640F173D8C4913E2244715B9BF810AC0A4DBE1C9E08F595C8D9510E3E335EF964BB87FDCACF58B891947600B0C37795CADB5A2AE6DE612338FDA9489AB21CE427BA + P2P_ADDRESS: 0.0.0.0:1337 + API_ADDRESS: 0.0.0.0:8080 + GENESIS_PUBLIC_KEY: ed0120F9F92758E815121F637C9704DFDA54842BA937AA721C0603018E208D6E25787E + TRUSTED_PEERS: '[{"address":"irohad3:1340","public_key":"ed012063ED3DFEDEBD8A86B4941CC4379D2EF0B74BDFE61F033FC0C89867D57C882A26"},{"address":"irohad1:1338","public_key":"ed012064BD9B25BF8477144D03B26FC8CF5D8A354B2F780DA310EE69933DC1E86FBCE2"},{"address":"irohad2:1339","public_key":"ed01208EA177921AF051CD12FC07E3416419320908883A1104B31401B650EEB820A300"}]' + GENESIS_PRIVATE_KEY: 802640FB8B867188E4952F1E83534B9B2E0A12D5122BD6F417CBC79D50D8A8C9C917B0F9F92758E815121F637C9704DFDA54842BA937AA721C0603018E208D6E25787E + GENESIS: /tmp/genesis.signed.scale + TOPOLOGY: '[{"address":"irohad3:1340","public_key":"ed012063ED3DFEDEBD8A86B4941CC4379D2EF0B74BDFE61F033FC0C89867D57C882A26"},{"address":"irohad1:1338","public_key":"ed012064BD9B25BF8477144D03B26FC8CF5D8A354B2F780DA310EE69933DC1E86FBCE2"},{"address":"irohad0:1337","public_key":"ed012087FDCACF58B891947600B0C37795CADB5A2AE6DE612338FDA9489AB21CE427BA"},{"address":"irohad2:1339","public_key":"ed01208EA177921AF051CD12FC07E3416419320908883A1104B31401B650EEB820A300"}]' + ports: + - 1337:1337 + - 8080:8080 + volumes: + - ./:/config + init: true + command: |- + /bin/sh -c " + EXECUTOR_RELATIVE_PATH=$(jq -r '.executor' /config/genesis.json) && \\ + EXECUTOR_ABSOLUTE_PATH=$(realpath \"/config/$$EXECUTOR_RELATIVE_PATH\") && \\ + jq \\ + --arg executor \"$$EXECUTOR_ABSOLUTE_PATH\" \\ + --argjson topology \"$$TOPOLOGY\" \\ + '.executor = $$executor | .topology = $$topology' /config/genesis.json \\ + >/tmp/genesis.json && \\ + kagami genesis sign /tmp/genesis.json \\ + --public-key $$GENESIS_PUBLIC_KEY \\ + --private-key $$GENESIS_PRIVATE_KEY \\ + --out-file $$GENESIS \\ + && \\ + irohad + " + irohad1: + depends_on: + - irohad0 + image: hyperledger/iroha:dev + pull_policy: never + environment: + CHAIN: 00000000-0000-0000-0000-000000000000 + PUBLIC_KEY: ed012064BD9B25BF8477144D03B26FC8CF5D8A354B2F780DA310EE69933DC1E86FBCE2 + PRIVATE_KEY: 802640FD8E2F03755AA130464ABF57A75E207BE870636B57F614D7A7B94E42318F9CA964BD9B25BF8477144D03B26FC8CF5D8A354B2F780DA310EE69933DC1E86FBCE2 + P2P_ADDRESS: 0.0.0.0:1338 + API_ADDRESS: 0.0.0.0:8081 + GENESIS_PUBLIC_KEY: ed0120F9F92758E815121F637C9704DFDA54842BA937AA721C0603018E208D6E25787E + TRUSTED_PEERS: '[{"address":"irohad3:1340","public_key":"ed012063ED3DFEDEBD8A86B4941CC4379D2EF0B74BDFE61F033FC0C89867D57C882A26"},{"address":"irohad0:1337","public_key":"ed012087FDCACF58B891947600B0C37795CADB5A2AE6DE612338FDA9489AB21CE427BA"},{"address":"irohad2:1339","public_key":"ed01208EA177921AF051CD12FC07E3416419320908883A1104B31401B650EEB820A300"}]' + ports: + - 1338:1338 + - 8081:8081 + volumes: + - ./:/config + init: true + irohad2: + depends_on: + - irohad0 + image: hyperledger/iroha:dev + pull_policy: never + environment: + CHAIN: 00000000-0000-0000-0000-000000000000 + PUBLIC_KEY: ed01208EA177921AF051CD12FC07E3416419320908883A1104B31401B650EEB820A300 + PRIVATE_KEY: 8026403A18FAC2654F1C8A331A84F4B142396EEC900022B38842D88D55E0DE144C8DF28EA177921AF051CD12FC07E3416419320908883A1104B31401B650EEB820A300 + P2P_ADDRESS: 0.0.0.0:1339 + API_ADDRESS: 0.0.0.0:8082 + GENESIS_PUBLIC_KEY: ed0120F9F92758E815121F637C9704DFDA54842BA937AA721C0603018E208D6E25787E + TRUSTED_PEERS: '[{"address":"irohad3:1340","public_key":"ed012063ED3DFEDEBD8A86B4941CC4379D2EF0B74BDFE61F033FC0C89867D57C882A26"},{"address":"irohad1:1338","public_key":"ed012064BD9B25BF8477144D03B26FC8CF5D8A354B2F780DA310EE69933DC1E86FBCE2"},{"address":"irohad0:1337","public_key":"ed012087FDCACF58B891947600B0C37795CADB5A2AE6DE612338FDA9489AB21CE427BA"}]' + ports: + - 1339:1339 + - 8082:8082 + volumes: + - ./:/config + init: true + irohad3: + depends_on: + - irohad0 + image: hyperledger/iroha:dev + pull_policy: never + environment: + CHAIN: 00000000-0000-0000-0000-000000000000 + PUBLIC_KEY: ed012063ED3DFEDEBD8A86B4941CC4379D2EF0B74BDFE61F033FC0C89867D57C882A26 + PRIVATE_KEY: 8026409464445DBA9030D6AC4F83161D3219144F886068027F6708AF9686F85DF6C4F063ED3DFEDEBD8A86B4941CC4379D2EF0B74BDFE61F033FC0C89867D57C882A26 + P2P_ADDRESS: 0.0.0.0:1340 + API_ADDRESS: 0.0.0.0:8083 + GENESIS_PUBLIC_KEY: ed0120F9F92758E815121F637C9704DFDA54842BA937AA721C0603018E208D6E25787E + TRUSTED_PEERS: '[{"address":"irohad1:1338","public_key":"ed012064BD9B25BF8477144D03B26FC8CF5D8A354B2F780DA310EE69933DC1E86FBCE2"},{"address":"irohad0:1337","public_key":"ed012087FDCACF58B891947600B0C37795CADB5A2AE6DE612338FDA9489AB21CE427BA"},{"address":"irohad2:1339","public_key":"ed01208EA177921AF051CD12FC07E3416419320908883A1104B31401B650EEB820A300"}]' + ports: + - 1340:1340 + - 8083:8083 + volumes: + - ./:/config + init: true + "##]).assert_eq(&build_as_string( + nonzero_ext::nonzero!(4u16), + false, + Some("."), + true, + Some(&["Single-line banner"]), + )); + } + #[test] fn single_pull_healthcheck() { expect_test::expect!([r#" @@ -327,7 +424,7 @@ mod tests { kagami genesis sign /tmp/genesis.json \\ --public-key $$GENESIS_PUBLIC_KEY \\ --private-key $$GENESIS_PRIVATE_KEY \\ - --out-file $$GENESIS\\ + --out-file $$GENESIS \\ && \\ irohad " @@ -354,7 +451,7 @@ mod tests { P2P_ADDRESS: 0.0.0.0:1337 API_ADDRESS: 0.0.0.0:8080 GENESIS_PUBLIC_KEY: ed0120F9F92758E815121F637C9704DFDA54842BA937AA721C0603018E208D6E25787E - SUMERAGI_TRUSTED_PEERS: '[{"address":"irohad3:1340","public_key":"ed012063ED3DFEDEBD8A86B4941CC4379D2EF0B74BDFE61F033FC0C89867D57C882A26"},{"address":"irohad1:1338","public_key":"ed012064BD9B25BF8477144D03B26FC8CF5D8A354B2F780DA310EE69933DC1E86FBCE2"},{"address":"irohad2:1339","public_key":"ed01208EA177921AF051CD12FC07E3416419320908883A1104B31401B650EEB820A300"}]' + TRUSTED_PEERS: '[{"address":"irohad3:1340","public_key":"ed012063ED3DFEDEBD8A86B4941CC4379D2EF0B74BDFE61F033FC0C89867D57C882A26"},{"address":"irohad1:1338","public_key":"ed012064BD9B25BF8477144D03B26FC8CF5D8A354B2F780DA310EE69933DC1E86FBCE2"},{"address":"irohad2:1339","public_key":"ed01208EA177921AF051CD12FC07E3416419320908883A1104B31401B650EEB820A300"}]' GENESIS_PRIVATE_KEY: 802640FB8B867188E4952F1E83534B9B2E0A12D5122BD6F417CBC79D50D8A8C9C917B0F9F92758E815121F637C9704DFDA54842BA937AA721C0603018E208D6E25787E GENESIS: /tmp/genesis.signed.scale TOPOLOGY: '[{"address":"irohad3:1340","public_key":"ed012063ED3DFEDEBD8A86B4941CC4379D2EF0B74BDFE61F033FC0C89867D57C882A26"},{"address":"irohad1:1338","public_key":"ed012064BD9B25BF8477144D03B26FC8CF5D8A354B2F780DA310EE69933DC1E86FBCE2"},{"address":"irohad0:1337","public_key":"ed012087FDCACF58B891947600B0C37795CADB5A2AE6DE612338FDA9489AB21CE427BA"},{"address":"irohad2:1339","public_key":"ed01208EA177921AF051CD12FC07E3416419320908883A1104B31401B650EEB820A300"}]' @@ -382,7 +479,7 @@ mod tests { kagami genesis sign /tmp/genesis.json \\ --public-key $$GENESIS_PUBLIC_KEY \\ --private-key $$GENESIS_PRIVATE_KEY \\ - --out-file $$GENESIS\\ + --out-file $$GENESIS \\ && \\ irohad " @@ -396,7 +493,7 @@ mod tests { P2P_ADDRESS: 0.0.0.0:1338 API_ADDRESS: 0.0.0.0:8081 GENESIS_PUBLIC_KEY: ed0120F9F92758E815121F637C9704DFDA54842BA937AA721C0603018E208D6E25787E - SUMERAGI_TRUSTED_PEERS: '[{"address":"irohad3:1340","public_key":"ed012063ED3DFEDEBD8A86B4941CC4379D2EF0B74BDFE61F033FC0C89867D57C882A26"},{"address":"irohad0:1337","public_key":"ed012087FDCACF58B891947600B0C37795CADB5A2AE6DE612338FDA9489AB21CE427BA"},{"address":"irohad2:1339","public_key":"ed01208EA177921AF051CD12FC07E3416419320908883A1104B31401B650EEB820A300"}]' + TRUSTED_PEERS: '[{"address":"irohad3:1340","public_key":"ed012063ED3DFEDEBD8A86B4941CC4379D2EF0B74BDFE61F033FC0C89867D57C882A26"},{"address":"irohad0:1337","public_key":"ed012087FDCACF58B891947600B0C37795CADB5A2AE6DE612338FDA9489AB21CE427BA"},{"address":"irohad2:1339","public_key":"ed01208EA177921AF051CD12FC07E3416419320908883A1104B31401B650EEB820A300"}]' ports: - 1338:1338 - 8081:8081 @@ -419,7 +516,7 @@ mod tests { P2P_ADDRESS: 0.0.0.0:1339 API_ADDRESS: 0.0.0.0:8082 GENESIS_PUBLIC_KEY: ed0120F9F92758E815121F637C9704DFDA54842BA937AA721C0603018E208D6E25787E - SUMERAGI_TRUSTED_PEERS: '[{"address":"irohad3:1340","public_key":"ed012063ED3DFEDEBD8A86B4941CC4379D2EF0B74BDFE61F033FC0C89867D57C882A26"},{"address":"irohad1:1338","public_key":"ed012064BD9B25BF8477144D03B26FC8CF5D8A354B2F780DA310EE69933DC1E86FBCE2"},{"address":"irohad0:1337","public_key":"ed012087FDCACF58B891947600B0C37795CADB5A2AE6DE612338FDA9489AB21CE427BA"}]' + TRUSTED_PEERS: '[{"address":"irohad3:1340","public_key":"ed012063ED3DFEDEBD8A86B4941CC4379D2EF0B74BDFE61F033FC0C89867D57C882A26"},{"address":"irohad1:1338","public_key":"ed012064BD9B25BF8477144D03B26FC8CF5D8A354B2F780DA310EE69933DC1E86FBCE2"},{"address":"irohad0:1337","public_key":"ed012087FDCACF58B891947600B0C37795CADB5A2AE6DE612338FDA9489AB21CE427BA"}]' ports: - 1339:1339 - 8082:8082 @@ -442,7 +539,7 @@ mod tests { P2P_ADDRESS: 0.0.0.0:1340 API_ADDRESS: 0.0.0.0:8083 GENESIS_PUBLIC_KEY: ed0120F9F92758E815121F637C9704DFDA54842BA937AA721C0603018E208D6E25787E - SUMERAGI_TRUSTED_PEERS: '[{"address":"irohad1:1338","public_key":"ed012064BD9B25BF8477144D03B26FC8CF5D8A354B2F780DA310EE69933DC1E86FBCE2"},{"address":"irohad0:1337","public_key":"ed012087FDCACF58B891947600B0C37795CADB5A2AE6DE612338FDA9489AB21CE427BA"},{"address":"irohad2:1339","public_key":"ed01208EA177921AF051CD12FC07E3416419320908883A1104B31401B650EEB820A300"}]' + TRUSTED_PEERS: '[{"address":"irohad1:1338","public_key":"ed012064BD9B25BF8477144D03B26FC8CF5D8A354B2F780DA310EE69933DC1E86FBCE2"},{"address":"irohad0:1337","public_key":"ed012087FDCACF58B891947600B0C37795CADB5A2AE6DE612338FDA9489AB21CE427BA"},{"address":"irohad2:1339","public_key":"ed01208EA177921AF051CD12FC07E3416419320908883A1104B31401B650EEB820A300"}]' ports: - 1340:1340 - 8083:8083 diff --git a/tools/swarm/src/schema.rs b/tools/swarm/src/schema.rs index f999585a0f8..015069a6863 100644 --- a/tools/swarm/src/schema.rs +++ b/tools/swarm/src/schema.rs @@ -61,22 +61,15 @@ impl Pull { #[serde(transparent)] struct HostPath<'a>(&'a path::RelativePath); -/// Dummy command used to terminate a service instead of running the image. -#[derive(Copy, Clone, Debug)] -struct EchoOk; - -const ECHO_OK: &str = "echo ok"; - -/// Dummy service that builds the image and terminates. +/// Image build settings. #[derive(serde::Serialize, Copy, Clone, Debug)] -struct ImageBuilder<'a> { +struct BuildImage<'a> { image: ImageId<'a>, build: HostPath<'a>, pull_policy: Build, - command: EchoOk, } -impl<'a> ImageBuilder<'a> { +impl<'a> BuildImage<'a> { fn new(image: ImageId<'a>, build: HostPath<'a>, ignore_cache: bool) -> Self { Self { image, @@ -86,21 +79,20 @@ impl<'a> ImageBuilder<'a> { } else { Build::OnCacheMiss }, - command: EchoOk, } } } -/// Reference to the image builder. +/// Reference to the first peer. #[derive(Copy, Clone, Debug)] -struct ImageBuilderRef; +struct Irohad0Ref; -const IMAGE_BUILDER: &str = "builder"; +const IROHAD0: &str = "irohad0"; /// Image that has been built. #[derive(serde::Serialize, Copy, Clone, Debug)] struct BuiltImage<'a> { - depends_on: [ImageBuilderRef; 1], + depends_on: [Irohad0Ref; 1], image: ImageId<'a>, pull_policy: UseBuilt, } @@ -116,7 +108,7 @@ struct PulledImage<'a> { impl<'a> BuiltImage<'a> { fn new(image: ImageId<'a>) -> Self { Self { - depends_on: [ImageBuilderRef], + depends_on: [Irohad0Ref], image, pull_policy: UseBuilt::UseCached, } @@ -336,23 +328,24 @@ where #[derive(Debug, PartialOrd, PartialEq, Ord, Eq)] struct IrohadRef(u16); -/// Peer services. #[derive(serde::Serialize, Debug)] -struct Services<'a, Image> -where - Image: serde::Serialize, -{ - irohad0: Irohad0<'a, Image>, - #[serde(flatten, skip_serializing_if = "std::collections::BTreeMap::is_empty")] - irohads: std::collections::BTreeMap>, +#[serde(untagged)] +enum BuildOrPull<'a> { + Build { + irohad0: Irohad0<'a, BuildImage<'a>>, + #[serde(flatten, skip_serializing_if = "std::collections::BTreeMap::is_empty")] + irohads: std::collections::BTreeMap>>, + }, + Pull { + irohad0: Irohad0<'a, PulledImage<'a>>, + #[serde(flatten, skip_serializing_if = "std::collections::BTreeMap::is_empty")] + irohads: std::collections::BTreeMap>>, + }, } -impl<'a, Image> Services<'a, Image> -where - Image: serde::Serialize + Copy, -{ - fn new( - image: Image, +impl<'a> BuildOrPull<'a> { + fn pull( + image: PulledImage<'a>, volumes: [PathMapping<'a>; 1], healthcheck: bool, chain: &'a iroha_data_model::ChainId, @@ -360,54 +353,112 @@ where network: &'a std::collections::BTreeMap, topology: &'a std::collections::BTreeSet, ) -> Self { - Self { - irohad0: { - let (_, ports, key_pair) = network.get(&0).expect("irohad0 must be present"); - Irohad0::new( - image, - GenesisEnv::new( - key_pair, + Self::Pull { + irohad0: Self::irohad0( + image, + volumes, + healthcheck, + chain, + (genesis_public_key, genesis_private_key), + network, + topology, + ), + irohads: Self::irohads( + image, + volumes, + healthcheck, + chain, + genesis_public_key, + network, + topology, + ), + } + } + + fn build( + image: BuildImage<'a>, + volumes: [PathMapping<'a>; 1], + healthcheck: bool, + chain: &'a iroha_data_model::ChainId, + (genesis_public_key, genesis_private_key): &'a peer::ExposedKeyPair, + network: &'a std::collections::BTreeMap, + topology: &'a std::collections::BTreeSet, + ) -> Self { + Self::Build { + irohad0: Self::irohad0( + image, + volumes, + healthcheck, + chain, + (genesis_public_key, genesis_private_key), + network, + topology, + ), + irohads: Self::irohads( + BuiltImage::new(image.image), + volumes, + healthcheck, + chain, + genesis_public_key, + network, + topology, + ), + } + } + + fn irohad0( + image: Image, + volumes: [PathMapping<'a>; 1], + healthcheck: bool, + chain: &'a iroha_data_model::ChainId, + (genesis_public_key, genesis_private_key): peer::ExposedKeyRefPair<'a>, + network: &'a std::collections::BTreeMap, + topology: &'a std::collections::BTreeSet, + ) -> Irohad0<'a, Image> { + let (_, ports, key_pair) = network.get(&0).expect("irohad0 must be present"); + Irohad0::new( + image, + GenesisEnv::new( + key_pair, + *ports, + chain, + (genesis_public_key, genesis_private_key), + topology, + ), + *ports, + volumes, + healthcheck, + ) + } + + fn irohads( + image: Image, + volumes: [PathMapping<'a>; 1], + healthcheck: bool, + chain: &'a iroha_data_model::ChainId, + genesis_public_key: &'a iroha_crypto::PublicKey, + network: &'a std::collections::BTreeMap, + topology: &'a std::collections::BTreeSet, + ) -> std::collections::BTreeMap> { + network + .iter() + .skip(1) + .map(|(id, (_, ports, key_pair))| { + ( + IrohadRef(*id), + Irohad::new( + image, + PeerEnv::new(key_pair, *ports, chain, genesis_public_key, topology), *ports, - chain, - (genesis_public_key, genesis_private_key), - topology, + volumes, + healthcheck, ), - *ports, - volumes, - healthcheck, ) - }, - irohads: network - .iter() - .skip(1) - .map(|(id, (_, ports, key_pair))| { - ( - IrohadRef(*id), - Irohad::new( - image, - PeerEnv::new(key_pair, *ports, chain, genesis_public_key, topology), - *ports, - volumes, - healthcheck, - ), - ) - }) - .collect(), - } + }) + .collect() } } -#[derive(serde::Serialize, Debug)] -#[serde(untagged)] -enum BuildOrPull<'a> { - Build { - builder: ImageBuilder<'a>, - #[serde(flatten)] - services: Services<'a, BuiltImage<'a>>, - }, - Pull(Services<'a, PulledImage<'a>>), -} - /// Docker Compose configuration. #[derive(serde::Serialize, Debug)] pub struct DockerCompose<'a> { @@ -439,7 +490,7 @@ impl<'a> DockerCompose<'a> { Self { services: build_dir.as_ref().map_or_else( || { - BuildOrPull::Pull(Services::new( + BuildOrPull::pull( PulledImage::new(image, *ignore_cache), volumes, *healthcheck, @@ -447,19 +498,18 @@ impl<'a> DockerCompose<'a> { genesis_key_pair, network, topology, - )) + ) }, - |build| BuildOrPull::Build { - builder: ImageBuilder::new(image, HostPath(build), *ignore_cache), - services: Services::new( - BuiltImage::new(image), + |build| { + BuildOrPull::build( + BuildImage::new(image, HostPath(build), *ignore_cache), volumes, *healthcheck, chain, genesis_key_pair, network, topology, - ), + ) }, ), } diff --git a/tools/swarm/src/schema/serde_impls.rs b/tools/swarm/src/schema/serde_impls.rs index 71d3fe75dc3..fb05da260fc 100644 --- a/tools/swarm/src/schema/serde_impls.rs +++ b/tools/swarm/src/schema/serde_impls.rs @@ -1,14 +1,5 @@ //! Custom `serde` impls. Keep them here to reduce clutter. -impl serde::Serialize for super::EchoOk { - fn serialize(&self, ser: S) -> Result - where - S: serde::Serializer, - { - ser.serialize_str(super::ECHO_OK) - } -} - impl serde::Serialize for super::Bool { fn serialize(&self, ser: S) -> Result where @@ -18,12 +9,12 @@ impl serde::Serialize for super::Bool { } } -impl serde::Serialize for super::ImageBuilderRef { +impl serde::Serialize for super::Irohad0Ref { fn serialize(&self, ser: S) -> Result where S: serde::Serializer, { - ser.serialize_str(super::IMAGE_BUILDER) + ser.serialize_str(super::IROHAD0) } }