From 4081698c20787788754ab4f88fae6f2e0ea836d2 Mon Sep 17 00:00:00 2001 From: Federico Mastellone Date: Wed, 30 Nov 2022 13:31:14 +0000 Subject: [PATCH] workbench: refactoring of modules dependencies - Remove repeated code (services-config) from backends - Move 'services-config' to profiles and out of the backends - Move the backend dependency out from 'all-profiles' - Remove unused code and parameters or its defaults - Remove 'stateDir' from the backends - Renames for clarity --- flake.nix | 4 +- nix/pkgs.nix | 45 ++++--- nix/workbench/backend/nomad.nix | 111 ++++++++---------- nix/workbench/backend/{run.nix => runner.nix} | 43 +++---- nix/workbench/backend/supervisor-conf.nix | 7 +- nix/workbench/backend/supervisor.nix | 99 +++++++--------- nix/workbench/default.nix | 52 ++++---- nix/workbench/profile.nix | 6 +- nix/workbench/profiles/default.nix | 24 ++-- .../{backend => profiles}/services-config.nix | 10 +- nix/workbench/shell.nix | 16 +-- nix/workbench/tests/default.nix | 8 +- shell.nix | 26 ++-- 13 files changed, 206 insertions(+), 245 deletions(-) rename nix/workbench/backend/{run.nix => runner.nix} (81%) rename nix/workbench/{backend => profiles}/services-config.nix (95%) diff --git a/flake.nix b/flake.nix index 95614249e3b..28e9dd57943 100644 --- a/flake.nix +++ b/flake.nix @@ -277,7 +277,7 @@ benchmarks = collectComponents' "benchmarks" projectPackages; }); - inherit (pkgs) workbench all-profiles-json workbench-instance; + inherit (pkgs) workbench all-profiles-json workbench-runner; packages = exes @@ -291,7 +291,7 @@ ## This is a very light profile, no caching&pinning needed. workbench-ci-test = - (pkgs.workbench-instance + (pkgs.workbench-runner { profileName = "ci-test-bage"; backendName = "supervisor"; diff --git a/nix/pkgs.nix b/nix/pkgs.nix index d3363131fd1..ec1bb4a9424 100644 --- a/nix/pkgs.nix +++ b/nix/pkgs.nix @@ -79,34 +79,43 @@ final: prev: with final; { # A generic, parameteric version of the workbench development environment. workbench = pkgs.callPackage ./workbench {}; - all-profiles-json = (workbench.all-profiles - { inherit (workbench-instance { backendName = "supervisor"; - profileName = "default-bage"; - }) backend; }).JSON; + all-profiles-json = workbench.profile-names-json; # A parametrisable workbench, that can be used with nix-shell or lorri. # See https://input-output-hk.github.io/haskell.nix/user-guide/development/ - workbench-instance = + # The general idea is: + # backendName -> useCabalRun -> backend + # stateDir -> batchName -> profileName -> backend -> workbench -> runner + # * `workbench` is in case a pinned version of the workbench is needed. + workbench-runner = let backendRegistry = { supervisor = ./workbench/backend/supervisor.nix; - nomad = ./workbench/backend/nomad.nix; + nomad = ./workbench/backend/nomad.nix; }; in - { backendName - , profileName ? customConfig.localCluster.profileName - , batchName ? customConfig.localCluster.batchName - , useCabalRun ? false - , workbenchDevMode ? false - , profiled ? false - , workbench ? pkgs.workbench - , backendWorkbench ? pkgs.callPackage (backendRegistry."${backendName}") - { inherit useCabalRun workbench; } - , cardano-node-rev ? null + { stateDir ? customConfig.localCluster.stateDir + , batchName ? customConfig.localCluster.batchName + , profileName ? customConfig.localCluster.profileName + , backendName ? customConfig.localCluster.backendName + , useCabalRun ? false + , profiled ? false + , cardano-node-rev ? null + , workbench ? pkgs.workbench + , workbenchDevMode ? false }: - pkgs.callPackage ./workbench/backend/run.nix + let + # The `useCabalRun` flag is set in the backend to allow the backend to + # override its value. The runner uses the value of `useCabalRun` from + # the backend to prevent a runner using a different value. + backend = import (backendRegistry."${backendName}") + { inherit pkgs lib useCabalRun; }; + in import ./workbench/backend/runner.nix { - inherit batchName profileName backendWorkbench cardano-node-rev; + inherit pkgs lib cardanoNodePackages; + inherit stateDir batchName profileName backend; + inherit cardano-node-rev; + inherit workbench workbenchDevMode; }; # Disable failing python uvloop tests diff --git a/nix/workbench/backend/nomad.nix b/nix/workbench/backend/nomad.nix index d939cf268a5..39785eba149 100644 --- a/nix/workbench/backend/nomad.nix +++ b/nix/workbench/backend/nomad.nix @@ -1,79 +1,60 @@ -let - basePort = 30000; - cacheDirDefault = "${__getEnv "HOME"}/.cache/cardano-workbench"; - stateDir = "run/current"; -in { pkgs , lib -, workbench -## -, cacheDir ? cacheDirDefault -, extraBackendConfig ? {} ## `useCabalRun` not used here like in `supervisor.nix`. , ... }: let - backend = - rec - { name = "nomad"; - - # Unlike the supervisor backend `useCabalRun` is always false here. - useCabalRun = false; + name = "nomad"; - services-config = import ./services-config.nix {inherit lib workbench basePort stateDir; useCabalRun = false;}; + # Unlike the supervisor backend `useCabalRun` is always false here. + useCabalRun = false; - extraShellPkgs = with pkgs; [ - # https://docs.podman.io/en/latest/markdown/podman.1.html#rootless-mode - podman - # Was not needed even thou it says so! - # https://docs.podman.io/en/latest/markdown/podman.1.html#note-unsupported-file-systems-in-rootless-mode - # fuse-overlayfs - nomad - nomad-driver-podman - ]; + extraShellPkgs = with pkgs; [ + # https://docs.podman.io/en/latest/markdown/podman.1.html#rootless-mode + podman + # Was not needed even thou it says so! + # https://docs.podman.io/en/latest/markdown/podman.1.html#note-unsupported-file-systems-in-rootless-mode + # fuse-overlayfs + nomad + nomad-driver-podman + ]; - materialise-profile = - { profileNix }: - let - supervisorConfPath = - import ./supervisor-conf.nix - { inherit (profileNix) node-services; - inherit - pkgs lib stateDir - basePort - extraBackendConfig; - unixHttpServerPort = "/tmp/supervisor.sock"; - }; - nomadConf = - import ./nomad-conf.nix - { inherit pkgs; - inherit - (pkgs.cardanoNodePackages) - cardano-node cardano-tracer tx-generator; - }; - in pkgs.runCommand "workbench-backend-output-${profileNix.name}-${name}" - (rec { - inherit supervisorConfPath; - # All In One - clusterImage = nomadConf.clusterImage; - clusterImageCopyToPodman = clusterImage.copyToPodman; - clusterImageName = clusterImage.imageName; - clusterImageTag = clusterImage.imageTag; - }) - '' - mkdir $out + materialise-profile = + { stateDir, profileNix }: + let + supervisorConfPath = + import ./supervisor-conf.nix + { inherit (profileNix) node-services; + inherit pkgs lib stateDir; + unixHttpServerPort = "/tmp/supervisor.sock"; + }; + nomadConf = + import ./nomad-conf.nix + { inherit pkgs; + inherit + (pkgs.cardanoNodePackages) + cardano-node cardano-tracer tx-generator; + }; + in pkgs.runCommand "workbench-backend-output-${profileNix.name}-${name}" + (rec { + inherit supervisorConfPath; + # All In One + clusterImage = nomadConf.clusterImage; + clusterImageCopyToPodman = clusterImage.copyToPodman; + clusterImageName = clusterImage.imageName; + clusterImageTag = clusterImage.imageTag; + }) + '' + mkdir $out - ln -s $supervisorConfPath $out/supervisor.conf + ln -s $supervisorConfPath $out/supervisor.conf - ln -s $clusterImage $out/clusterImage - echo $clusterImageName > $out/clusterImageName - echo $clusterImageTag > $out/clusterImageTag - ln -s $clusterImageCopyToPodman/bin/copy-to-podman $out/clusterImageCopyToPodman - ''; - }; + ln -s $clusterImage $out/clusterImage + echo $clusterImageName > $out/clusterImageName + echo $clusterImageTag > $out/clusterImageTag + ln -s $clusterImageCopyToPodman/bin/copy-to-podman $out/clusterImageCopyToPodman + ''; in { - inherit cacheDir stateDir basePort; - inherit workbench; - inherit backend; + inherit name useCabalRun extraShellPkgs materialise-profile; } diff --git a/nix/workbench/backend/run.nix b/nix/workbench/backend/runner.nix similarity index 81% rename from nix/workbench/backend/run.nix rename to nix/workbench/backend/runner.nix index 148395da708..2aab5c6f8b3 100644 --- a/nix/workbench/backend/run.nix +++ b/nix/workbench/backend/runner.nix @@ -1,30 +1,28 @@ -let - batchNameDefault = "plain"; - profileNameDefault = "default-bage"; -in { pkgs +, lib , cardanoNodePackages -, backendWorkbench ## -, profileName ? profileNameDefault -, batchName ? batchNameDefault +, stateDir +, batchName +, profileName +, backend ## -, workbenchDevMode ? false , cardano-node-rev ? "0000000000000000000000000000000000000000" +, workbench +, workbenchDevMode ? false +## +, cacheDir ? "${__getEnv "HOME"}/.cache/cardano-workbench" +, basePort ? 30000 }: let - inherit (backendWorkbench) workbench backend cacheDir stateDir basePort; - - backendName = backendWorkbench.backend.name; + backendName = backend.name; - useCabalRun = backendWorkbench.backend.useCabalRun; + inherit (backend) useCabalRun; - with-backend-profile = - { envArgsOverride ? {} }: ## TODO: envArgsOverride is not used! - workbench.with-profile - { inherit backend profileName; }; + with-backend-profile = workbench.with-profile + { inherit stateDir profileName backend basePort workbench; }; - inherit (with-backend-profile {}) profileNix profile topology genesis; + inherit (with-backend-profile) profileNix profile topology genesis; in let @@ -74,8 +72,7 @@ in { trace ? false }: let inherit - (with-backend-profile - { envArgsOverride = { cacheDir = "./cache"; stateDir = "./"; }; }) + (with-backend-profile) profileNix profile topology genesis; run = pkgs.runCommand "workbench-run-${backendName}-${profileName}" @@ -93,7 +90,7 @@ in zstd ] ++ - backendWorkbench.backend.extraShellPkgs + backend.extraShellPkgs ; } '' @@ -148,10 +145,8 @@ in }; in { - inherit stateDir; - inherit profileName; - inherit workbench backendWorkbench; - inherit (backendWorkbench) backend; + inherit stateDir batchName profileName backend; + inherit workbench; inherit profileNix profile topology genesis; inherit interactive-start interactive-stop interactive-restart; inherit profile-run; diff --git a/nix/workbench/backend/supervisor-conf.nix b/nix/workbench/backend/supervisor-conf.nix index 9b03b38880e..51703db5a5b 100644 --- a/nix/workbench/backend/supervisor-conf.nix +++ b/nix/workbench/backend/supervisor-conf.nix @@ -1,12 +1,9 @@ { pkgs , lib , stateDir -, basePort , node-services , unixHttpServerPort ? null , inetHttpServerPort ? null - ## Last-moment overrides: -, extraBackendConfig }: with lib; @@ -71,9 +68,7 @@ let stopasgroup = true; killasgroup = true; }; - } - // - extraBackendConfig; + }; ## ## nodeSvcSupervisorProgram :: NodeService -> SupervisorConfSection diff --git a/nix/workbench/backend/supervisor.nix b/nix/workbench/backend/supervisor.nix index 3539640a35f..1c3106fa276 100644 --- a/nix/workbench/backend/supervisor.nix +++ b/nix/workbench/backend/supervisor.nix @@ -1,70 +1,51 @@ -let - basePort = 30000; - cacheDirDefault = "${__getEnv "HOME"}/.cache/cardano-workbench"; - stateDir = "run/current"; -in { pkgs , lib -, workbench -## -, cacheDir ? cacheDirDefault -, extraBackendConfig ? {} -, useCabalRun ? false -## +, useCabalRun , ... }: let - backend = - rec - { name = "supervisor"; - - # Unlike the nomad backend `useCabalRun` is honored here. - inherit useCabalRun; + name = "supervisor"; - services-config = import ./services-config.nix {inherit lib workbench basePort stateDir useCabalRun;}; + # Unlike the nomad backend `useCabalRun` is honored here. + inherit useCabalRun; - extraShellPkgs = with pkgs; [ - python3Packages.supervisor - ] - ++ lib.optionals ( useCabalRun) - (with haskellPackages; [ - cabalWrapped - ghcid - haskellBuildUtils - cabal-plan - ]) - ## Workbench's main script is called directly in dev mode. - ++ lib.optionals (!useCabalRun) - (with cardanoNodePackages; [ - cardano-node - cardano-tracer - tx-generator - ]); + extraShellPkgs = with pkgs; + [ + python3Packages.supervisor + ] + ++ lib.optionals ( useCabalRun) + (with haskellPackages; [ + cabalWrapped + ghcid + haskellBuildUtils + cabal-plan + ]) + ## Workbench's main script is called directly in dev mode. + ++ lib.optionals (!useCabalRun) + (with cardanoNodePackages; [ + cardano-node + cardano-tracer + tx-generator + ]); - materialise-profile = - { profileNix }: - pkgs.runCommand "workbench-backend-output-${profileNix.name}-${name}d" - { - ## Backend-specific Nix bits: - ## mkBackendConf :: Profile -> SupervisorConf/DockerConf - supervisorConfPath = - import ./supervisor-conf.nix - { inherit (profileNix) node-services; - inherit - pkgs lib stateDir - basePort - extraBackendConfig; - inetHttpServerPort = "127.0.0.1:9001"; - }; - } - '' - mkdir $out - cp $supervisorConfPath $out/supervisor.conf - ''; - }; + materialise-profile = + { stateDir, profileNix }: + pkgs.runCommand "workbench-backend-output-${profileNix.name}-${name}" + { + ## Backend-specific Nix bits: + ## mkBackendConf :: Profile -> SupervisorConf/DockerConf + supervisorConfPath = + import ./supervisor-conf.nix + { inherit (profileNix) node-services; + inherit pkgs lib stateDir; + inetHttpServerPort = "127.0.0.1:9001"; + }; + } + '' + mkdir $out + cp $supervisorConfPath $out/supervisor.conf + ''; in { - inherit cacheDir stateDir basePort; - inherit workbench; - inherit backend; + inherit name useCabalRun extraShellPkgs materialise-profile; } diff --git a/nix/workbench/default.nix b/nix/workbench/default.nix index 62cbbcf0f1c..009827bd3be 100644 --- a/nix/workbench/default.nix +++ b/nix/workbench/default.nix @@ -71,34 +71,28 @@ let profile-names = __fromJSON (__readFile profile-names-json); - all-profiles = - ## The backend is an attrset of AWS/supervisord-specific methods and parameters. - { backend }: - rec { - mkProfile = - profileName: - pkgs.callPackage ./profiles - { inherit - pkgs - runWorkbenchJqOnly runJq workbench - profileName; - inherit (backend) services-config; - }; - - value = genAttrs profile-names mkProfile; - - JSON = pkgs.writeText "all-profiles.json" (__toJSON (mapAttrs (_: x: x.value) value)); - }; - with-profile = - { backend, profileName }: + # `workbench` is the pinned workbench in case there is one. + { stateDir, profileName, backend, basePort, workbench }: let - ps = all-profiles { inherit backend; }; - - profileNix = ps.value."${profileName}" - or (throw "No such profile: ${profileName}; Known profiles: ${toString (__attrNames ps.value)}"); - - profile = import ./profile.nix { inherit pkgs lib profileNix backend; }; + ps = + let + mkProfile = + profileName: + pkgs.callPackage ./profiles + { inherit pkgs lib; + inherit stateDir profileName; + # `useCabalRun`, final decision, from the backend! + inherit (backend) useCabalRun; + inherit basePort; + inherit workbench; + }; + in genAttrs profile-names mkProfile; + + profileNix = ps."${profileName}" + or (throw "No such profile: ${profileName}; Known profiles: ${toString (__attrNames ps)}"); + + profile = import ./profile.nix { inherit pkgs lib stateDir profileNix backend; }; topology = import ./topology.nix { inherit pkgs profileNix profile; }; @@ -113,11 +107,13 @@ let run-analysis = import ./analyse.nix; in { + inherit workbench' workbench runWorkbench runWorkbenchJqOnly; + inherit runJq; - inherit workbench' workbench runWorkbench runWorkbenchJqOnly; + inherit profile-names profile-names-json; - inherit all-profiles profile-names profile-names-json with-profile; + inherit with-profile; inherit run-analysis; } diff --git a/nix/workbench/profile.nix b/nix/workbench/profile.nix index 9359b1d0e1b..c41a21766a5 100644 --- a/nix/workbench/profile.nix +++ b/nix/workbench/profile.nix @@ -1,10 +1,10 @@ -{ pkgs, lib, profileNix, backend }: +{ pkgs, lib, stateDir, profileNix, backend }: with lib; - pkgs.runCommand "workbench-profile-output-${profileNix.name}-${backend.name}d" + pkgs.runCommand "workbench-profile-output-${profileNix.name}-${backend.name}" { buildInputs = []; profileConfigJsonPath = profileNix.JSON; nodeSpecsJsonPath = profileNix.node-specs.JSON; - backendConfigPath = backend.materialise-profile { inherit profileNix; }; + backendConfigPath = backend.materialise-profile { inherit stateDir profileNix; }; nodeServices = __toJSON (flip mapAttrs profileNix.node-services diff --git a/nix/workbench/profiles/default.nix b/nix/workbench/profiles/default.nix index 783f987ecd7..5647231803d 100644 --- a/nix/workbench/profiles/default.nix +++ b/nix/workbench/profiles/default.nix @@ -1,13 +1,23 @@ -{ pkgs, cardanoLib -, runCommand, runWorkbenchJqOnly, runJq, workbench - -## An attrset of specific methods and parameters. -, services-config - +{ pkgs, lib, cardanoLib +, runCommand +, workbench +, stateDir , profileName +, useCabalRun +, basePort }: let + inherit (workbench) runWorkbenchJqOnly runJq; + + services-config = import ./services-config.nix + { + inherit lib workbench; + inherit stateDir; + inherit useCabalRun; + inherit basePort; + }; + JSON = runWorkbenchJqOnly "profile-${profileName}.json" "profile json ${profileName}"; @@ -21,7 +31,7 @@ let topology.files = runCommand "topology-${profileName}" {} - "${workbench}/bin/wb topology make ${JSON} $out"; + "${workbench.workbench}/bin/wb topology make ${JSON} $out"; node-specs = { diff --git a/nix/workbench/backend/services-config.nix b/nix/workbench/profiles/services-config.nix similarity index 95% rename from nix/workbench/backend/services-config.nix rename to nix/workbench/profiles/services-config.nix index 9be8f6b9fd8..56ff528a0e8 100644 --- a/nix/workbench/backend/services-config.nix +++ b/nix/workbench/profiles/services-config.nix @@ -1,9 +1,10 @@ { lib , workbench ## -, basePort ? 30000 -, stateDir ? "run/current" -, useCabalRun ? false +, stateDir +, useCabalRun +## +, basePort }: with lib; { @@ -69,9 +70,6 @@ with lib; } ); - finaliseNodeArgs = - profile: nodeSpec: args: args; - finaliseGeneratorService = profile: svc: recursiveUpdate svc ({ diff --git a/nix/workbench/shell.nix b/nix/workbench/shell.nix index 462e4474b95..a4006a8a365 100644 --- a/nix/workbench/shell.nix +++ b/nix/workbench/shell.nix @@ -7,7 +7,7 @@ ## , cardano-mainnet-mirror ## -, workbenchRun +, workbenchRunner , workbenchDevMode ? false ## , profiled ? false @@ -18,9 +18,9 @@ with lib; let - inherit (workbenchRun) profileName backend profile; + inherit (workbenchRunner) profileName backend profile; - shellHook = { profileName, backend, workbenchDevMode, profiled, withMainnet }: '' + shellHook = { profileName, backend, profiled, workbenchDevMode, withMainnet }: '' while test $# -gt 0 do shift; done ## Flush argv[] @@ -70,7 +70,7 @@ let in project.shellFor { name = "workbench-shell"; - shellHook = shellHook { inherit profileName backend workbenchDevMode profiled withMainnet; }; + shellHook = shellHook { inherit profileName backend profiled workbenchDevMode withMainnet; }; inherit withHoogle; @@ -109,15 +109,15 @@ in project.shellFor { pkgs.moreutils pkgs.pstree pkgs.time - workbenchRun.interactive-start - workbenchRun.interactive-stop - workbenchRun.interactive-restart + workbenchRunner.interactive-start + workbenchRunner.interactive-stop + workbenchRunner.interactive-restart ] ++ lib.optional haveGlibcLocales pkgs.glibcLocales ++ lib.optionals (!backend.useCabalRun) [cardano-topology cardano-cli locli] ++ backend.extraShellPkgs ++ lib.optionals (!workbenchDevMode) [ - workbenchRun.workbench.workbench + workbenchRunner.workbench.workbench ] ; diff --git a/nix/workbench/tests/default.nix b/nix/workbench/tests/default.nix index fe64e762e4c..befd0584423 100644 --- a/nix/workbench/tests/default.nix +++ b/nix/workbench/tests/default.nix @@ -1,13 +1,9 @@ { pkgs }: let - inherit (pkgs) workbench-instance cardano-cli cardanolib-py cardano-node; + inherit (pkgs) workbench-runner cardano-cli cardanolib-py cardano-node; stateDir = "./state-cluster-test"; # We want a really short duration for tests - cluster' = workbench-instance { - genesisParams = { - slotLength = 0.1; - decentralisationParam = 0.8; - }; + cluster' = workbench-runner { inherit stateDir; }; # Library bash functions for cluster tests diff --git a/shell.nix b/shell.nix index 4ff0c9ff66f..8ffb5d28c02 100644 --- a/shell.nix +++ b/shell.nix @@ -5,8 +5,8 @@ in { withHoogle ? defaultCustomConfig.withHoogle , profileName ? defaultCustomConfig.localCluster.profileName , backendName ? defaultCustomConfig.localCluster.backendName -, workbenchDevMode ? defaultCustomConfig.localCluster.workbenchDevMode , useCabalRun ? true +, workbenchDevMode ? defaultCustomConfig.localCluster.workbenchDevMode , customConfig ? { inherit withHoogle; localCluster = { @@ -64,22 +64,22 @@ let inherit cardano-mainnet-mirror; inherit workbenchDevMode; inherit profiled withHoogle; - workbenchRun = - pkgs.workbench-instance - { inherit backendName profileName useCabalRun profiled; }; + workbenchRunner = + pkgs.workbench-runner + { inherit profileName backendName useCabalRun profiled; }; }; devops = let profileName = "devops-bage"; - workbenchRun = pkgs.workbench-instance + workbenchRunner = pkgs.workbench-runner { inherit profileName; - useCabalRun = false; backendName = "supervisor"; + useCabalRun = false; }; devopsShellParams = { inherit profileName; - backend = workbenchRun.backend; + inherit (workbenchRunner) backend; inherit workbenchDevMode profiled; withMainnet = false; }; @@ -88,7 +88,7 @@ let { inherit pkgs lib haskellLib project; inherit setLocale haveGlibcLocales commandHelp; inherit cardano-mainnet-mirror; - inherit workbenchRun workbenchDevMode; + inherit workbenchRunner workbenchDevMode; inherit profiled withHoogle; }; in project.shellFor { @@ -110,11 +110,11 @@ let pkgs.graphviz python3Packages.supervisor python3Packages.ipython - workbenchRun.interactive-start - workbenchRun.interactive-stop - workbenchRun.interactive-restart + workbenchRunner.interactive-start + workbenchRunner.interactive-stop + workbenchRunner.interactive-restart cardanolib-py - workbenchRun.workbench.workbench + workbenchRunner.workbench.workbench pstree pkgs.time ]; @@ -127,7 +127,7 @@ let ${devopsShell.shellHook devopsShellParams} # Socket path default to first node launched by "start-cluster": - export CARDANO_NODE_SOCKET_PATH=$(wb backend get-node-socket-path ${workbenchRun.stateDir} 'node-0') + export CARDANO_NODE_SOCKET_PATH=$(wb backend get-node-socket-path ${workbenchRunner.stateDir} 'node-0') ${setLocale}