Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
Fix nixos service to work without cardano-db-sync service
Browse files Browse the repository at this point in the history
  • Loading branch information
jbgi committed Jul 6, 2020
1 parent dfce7a1 commit b0657fd
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 25 deletions.
68 changes: 63 additions & 5 deletions nix/nixos/smash-service.nix
Expand Up @@ -13,27 +13,85 @@ in {
};
package = lib.mkOption {
type = lib.types.package;
default = (import ../. {}).smashHaskellPackages.smash.components.exes.smash-exe;
default = pkgs.smashHaskellPackages.smash.components.exes.smash-exe or (import ../. {}).smashHaskellPackages.smash.components.exes.smash-exe;
};
configFile = lib.mkOption {
type = lib.types.path;
default = builtins.toFile "config.json" (builtins.toJSON ({
NetworkName = cfg.environmentName;
GenesisHash = "";
inherit (cfg.environment.nodeConfig) RequiresNetworkMagic;
} // cfg.logConfig));
};
environment = lib.mkOption {
type = lib.types.attrs;
default = pkgs.iohkNix.cardanoLib.environments.${cfg.environmentName};
};
logConfig = lib.mkOption {
type = lib.types.attrs;
default = pkgs.iohkNix.cardanoLib.defaultExplorerLogConfig;
};
environmentName = lib.mkOption {
type = lib.types.str;
description = "environment name";
};
socketPath = lib.mkOption {
type = lib.types.path;
};
user = lib.mkOption {
type = lib.types.str;
default = "smash";
description = "the user to run as";
};
postgres = {
pgpass = lib.mkOption {
type = lib.types.path;
default = builtins.toFile "pgpass" "${cfg.postgres.socketdir}:${toString cfg.postgres.port}:${cfg.postgres.database}:${cfg.postgres.user}:*";
};
socketdir = lib.mkOption {
type = lib.types.str;
default = "/run/postgresql";
description = "the path to the postgresql socket";
};
port = lib.mkOption {
type = lib.types.int;
default = config.services.postgresql.port;
description = "the postgresql port";
};
database = lib.mkOption {
type = lib.types.str;
default = cfg.postgres.user;
description = "the postgresql database to use";
};
user = lib.mkOption {
type = lib.types.str;
default = cfg.user;
description = "the postgresql user to use";
};
};
};
};
config = lib.mkIf cfg.enable {
services.smash.script = pkgs.writeShellScript "smash" ''
set -euo pipefail
cp ${config.services.cardano-db-sync.pgpass} $RUNTIME_DIRECTORY/pgpass
cp ${cfg.postgres.pgpass} $RUNTIME_DIRECTORY/pgpass
chmod 0600 $RUNTIME_DIRECTORY/pgpass
export SMASHPGPASSFILE=$RUNTIME_DIRECTORY/pgpass
${cfg.package}/bin/smash-exe run-migrations --mdir ${../../schema}
exec ${cfg.package}/bin/smash-exe run-app
exec ${cfg.package}/bin/smash-exe run-app-with-db-sync \
--config ${cfg.configFile} \
--genesis-file ${cfg.environment.genesisFile} \
--socket-path ${cfg.socketPath} \
--schema-dir ${../../schema}
'';
environment.systemPackages = [ cfg.package config.services.postgresql.package ];
systemd.services.smash = {
path = [ cfg.package config.services.postgresql.package pkgs.netcat ];
preStart = ''
for x in {1..10}; do
nc -z localhost ${toString config.services.cardano-db-sync.postgres.port} && break
nc -z localhost ${toString config.services.smash.postgres.port} && break
echo loop $x: waiting for postgresql 2 sec...
sleep 2
done
Expand All @@ -46,7 +104,7 @@ in {
};

wantedBy = [ "multi-user.target" ];
after = [ "postgres.service" "cardano-db-sync.service" ];
after = [ "postgres.service" "cardano-node.service" ];
requires = [ "postgresql.service" ];
};
};
Expand Down
25 changes: 13 additions & 12 deletions nix/nixos/tests/smash-test.nix
Expand Up @@ -8,38 +8,38 @@ with pkgs; with commonLib;
environment = {
systemPackages = with pkgs; [ curl jq ];
variables = {
PGPASSFILE = config.services.cardano-db-sync.pgpass;
SMASHPGPASSFILE = config.services.smash.postgres.pgpass;
};
};
imports = [
../smash-service.nix
(sources.cardano-db-sync + "/nix/nixos")
(sources.cardano-node + "/nix/nixos")
];
services.cardano-db-sync = {
services.smash = {
enable = true;
cluster = "ff";
environmentName = "shelley_testnet";
inherit (config.services.cardano-node) socketPath;
};
services.smash = {
services.cardano-node = {
enable = true;
package = smashHaskellPackages.smash.components.exes.smash-exe;
environment = "shelley_testnet";
};
services.postgresql = {
enable = true;
package = postgresql_12;
enableTCPIP = false;
ensureDatabases = [ "${config.services.cardano-db-sync.user}" ];
ensureDatabases = [ "${config.services.smash.postgres.database}" ];
ensureUsers = [
{
name = "${config.services.cardano-db-sync.user}";
name = "${config.services.smash.postgres.user}";
ensurePermissions = {
"DATABASE ${config.services.cardano-db-sync.user}" = "ALL PRIVILEGES";
"DATABASE ${config.services.smash.postgres.database}" = "ALL PRIVILEGES";
};
}
];
identMap = ''
cdbsync-users root ${config.services.cardano-db-sync.user}
cdbsync-users smash ${config.services.cardano-db-sync.user}
cdbsync-users ${config.services.cardano-db-sync.user} ${config.services.cardano-db-sync.user}
cdbsync-users root ${config.services.smash.postgres.user}
cdbsync-users ${config.services.smash.user} ${config.services.smash.postgres.user}
cdbsync-users postgres postgres
'';
authentication = ''
Expand All @@ -51,6 +51,7 @@ with pkgs; with commonLib;
testScript = ''
startAll
$machine->waitForUnit("postgresql.service");
$machine->waitForUnit("cardano-node.service");
$machine->waitForUnit("smash.service");
$machine->waitForOpenPort(3100);
$machine->succeed("smash-exe insert-pool --metadata ${../../../test_pool.json} --poolhash \"cbdfc4f21feb0a414b2b9471fa56b0ebd312825e63db776d68cc3fa0ca1f5a2f\"");
Expand Down
16 changes: 8 additions & 8 deletions nix/sources.json
@@ -1,14 +1,14 @@
{
"cardano-db-sync": {
"branch": "master",
"description": "A component that follows the Cardano chain and stores blocks and transactions in PostgreSQL",
"homepage": "",
"cardano-node": {
"branch": "tags/1.14.2",
"description": "The core component that is used to participate in a Cardano decentralised blockchain.",
"homepage": "https://cardano.org",
"owner": "input-output-hk",
"repo": "cardano-db-sync",
"rev": "6d2917212848a7f70e7e84336b9291dd2634ea61",
"sha256": "0bi6v6b9c2i2d3h416lpn0p2qy8pd08w0g0zjjsnldr2sb7ph3s9",
"repo": "cardano-node",
"rev": "924a6f7d8c2bdb1bf525be8b0d5626e440697b01",
"sha256": "0bi1vwmb1w6wc2szpr8v1nbsci9y36lnjr4v5pli1wvll1hhpw3c",
"type": "tarball",
"url": "https://github.com/input-output-hk/cardano-db-sync/archive/6d2917212848a7f70e7e84336b9291dd2634ea61.tar.gz",
"url": "https://github.com/input-output-hk/cardano-node/archive/924a6f7d8c2bdb1bf525be8b0d5626e440697b01.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"haskell.nix": {
Expand Down

0 comments on commit b0657fd

Please sign in to comment.