Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nixos service updates #815

Merged
merged 4 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ RUN curl --proto '=https' --tlsv1.2 -sSf -L https://www.postgresql.org/media/key
apt-get update && apt-get install -y --no-install-recommends \
ca-certificates
COPY --from=downloader /usr/local/bin/hasura /usr/local/bin/hasura
COPY --from=downloader /root/.hasura/plugins/bin/hasura-cli_ext /usr/local/bin/hasura-cli_ext
ENV \
HASURA_CLI_PATH=/usr/local/bin/hasura \
HASURA_CLI_EXT_PATH=/usr/local/bin/hasura-cli_ext \
HASURA_URI="http://hasura:8080" \
LD_LIBRARY_PATH="/usr/local/lib:$LD_LIBRARY_PATH" \
METADATA_SERVER_URI=${METADATA_SERVER_URI} \
Expand Down
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
} @ inputs:
std.growOn {
inherit inputs;
systems = ["x86_64-linux"];
cellsFrom = nix/cells;
cellBlocks = [
(std.functions "library")
Expand Down
2 changes: 1 addition & 1 deletion nix/cells/automation/pipelines.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
skippedDescription = lib.escapeShellArg "No nix builder for this system";
};

memory = 1024 * 8;
memory = 1024 * 16;
nomad.resources.cpu = 10000;
};
}
44 changes: 28 additions & 16 deletions nix/nixos/cardano-graphql-background-service.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,31 @@
{ lib, pkgs, config, ... }:
let
cfg = config.services.cardano-graphql-background;
selfPkgs = import ../pkgs.nix {};
in {
options = {
services.cardano-graphql-background = {
enable = lib.mkEnableOption "cardano-explorer graphql background service";

frontendPkg = lib.mkOption {
type = lib.types.package;
default = (import ../pkgs.nix {}).packages.cardano-graphql;
};

persistPkg = lib.mkOption {
type = lib.types.package;
default = (import ../pkgs.nix {}).packages.persistgraphql;
};

hasuraCliPkg = lib.mkOption {
type = lib.types.package;
default = (import ../pkgs.nix {}).packages.hasura-cli;
};

hasuraCliExtPkg = lib.mkOption {
type = lib.types.package;
default = (import ../pkgs.nix {}).packages.hasura-cli-ext;
};

assetMetadataUpdateInterval = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = null;
Expand Down Expand Up @@ -76,30 +95,27 @@ in {
};
config = let
boolToNodeJSEnv = bool: if bool then "true" else "false";
frontend = selfPkgs.packages.cardano-graphql;
persistgraphql = selfPkgs.packages.persistgraphql;
hasura-cli = selfPkgs.packages.hasura-cli;
hasura-cli-ext = selfPkgs.packages.hasura-cli-ext;
frontend = cfg.frontendPkg;
persistgraphql = cfg.persistPkg;
hasura-cli = cfg.hasuraCliPkg;
hasura-cli-ext = cfg.hasuraCliExtPkg;
hasuraBaseUri = "${cfg.hasuraProtocol}://${cfg.hasuraIp}:${toString cfg.enginePort}";
pluginLibPath = pkgs.lib.makeLibraryPath [
pkgs.stdenv.cc.cc.lib
];
installHasuraCLI = ''
# always start with no plugins so future upgrades will work
rm -rf ~/.hasura/plugins
mkdir -p ~/.hasura/plugins/store/cli-ext/v${hasura-cli-ext.version}
ln -s ${hasura-cli-ext}/bin/cli-ext-hasura-linux ~/.hasura/plugins/store/cli-ext/v${hasura-cli-ext.version}/cli-ext-hasura-linux
'';
in lib.mkIf cfg.enable {
systemd.services.cardano-graphql = {
systemd.services.cardano-graphql-background = {
wantedBy = [ "multi-user.target" ];
wants = [ "graphql-engine.service" ];
after = [ "graphql-engine.service" ];
environment = lib.filterAttrs (k: v: v != null) {
HASURA_CLI_PATH = hasura-cli + "/bin/hasura";
HASURA_CLI_EXT_PATH = hasura-cli-ext + "/bin/cli-ext-hasura-linux";
HASURA_GRAPHQL_ENABLE_TELEMETRY = toString false;
HASURA_URI = hasuraBaseUri;
LOGGER_MIN_SEVERITY = cfg.loggerMinSeverity;
OGMIOS_HOST = cfg.ogmiosHost;
OGMIOS_PORT = toString cfg.ogmiosPort;
POSTGRES_DB = cfg.db;
POSTGRES_HOST = cfg.dbHost;
POSTGRES_PASSWORD = cfg.dbPassword;
Expand All @@ -109,10 +125,6 @@ in {
(lib.optionalAttrs (cfg.assetMetadataUpdateInterval != null) { ASSET_METADATA_UPDATE_INTERVAL = toString cfg.assetMetadataUpdateInterval; }) //
(lib.optionalAttrs (cfg.metadataServerUri != null) { METADATA_SERVER_URI = toString cfg.metadataServerUri; });
path = with pkgs; [ netcat curl postgresql frontend hasura-cli glibc.bin patchelf ];
preStart = ''
set -exuo pipefail
${installHasuraCLI}
'';
script = ''
exec cardano-graphql-background
'';
Expand Down
15 changes: 12 additions & 3 deletions nix/nixos/cardano-graphql-service.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@
{ lib, pkgs, config, ... }:
let
cfg = config.services.cardano-graphql;
selfPkgs = import ../pkgs.nix {};
in {
options = {
services.cardano-graphql = {
enable = lib.mkEnableOption "cardano-explorer graphql service";

frontendPkg = lib.mkOption {
type = lib.types.package;
default = (import ../pkgs.nix {}).packages.cardano-graphql;
};

persistPkg = lib.mkOption {
type = lib.types.package;
default = (import ../pkgs.nix {}).packages.persistgraphql;
};

enginePort = lib.mkOption {
type = lib.types.int;
default = 9999;
Expand Down Expand Up @@ -93,8 +102,8 @@ in {
config = let
# TODO: there has to be a better way to handle boolean env vars in nodejs???
boolToNodeJSEnv = bool: if bool then "true" else "false";
frontend = selfPkgs.packages.cardano-graphql;
persistgraphql = selfPkgs.packages.persistgraphql;
frontend = cfg.frontendPkg;
persistgraphql = cfg.persistPkg;
hasuraBaseUri = "${cfg.hasuraProtocol}://${cfg.hasuraIp}:${toString cfg.enginePort}";
pluginLibPath = pkgs.lib.makeLibraryPath [
pkgs.stdenv.cc.cc.lib
Expand Down
22 changes: 12 additions & 10 deletions nix/nixos/graphql-engine-service.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ in {
services.graphql-engine = {
enable = lib.mkEnableOption "graphql engine service";

package = lib.mkOption {
type = lib.types.package;
default = (import ../pkgs.nix {}).packages.graphql-engine;
};

host = lib.mkOption {
type = lib.types.str;
default = "/var/run/postgresql";
default = "";
};

dbUser = lib.mkOption {
Expand All @@ -19,7 +24,7 @@ in {

password = lib.mkOption {
type = lib.types.str;
default = ''""'';
default = "";
};

dbAdminUser = lib.mkOption {
Expand All @@ -44,7 +49,6 @@ in {
};
};
config = let
graphqlEngine = (import ../pkgs.nix {}).packages.graphql-engine;
hasuraDbPerms = pkgs.writeScript "hasuraDbPerms.sql" ''
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE SCHEMA IF NOT EXISTS hdb_catalog;
Expand All @@ -54,14 +58,17 @@ in {
GRANT SELECT ON ALL TABLES IN SCHEMA information_schema TO ${cfg.dbUser};
GRANT SELECT ON ALL TABLES IN SCHEMA pg_catalog TO ${cfg.dbUser};
'';
postgresqlIp = if ((__head (pkgs.lib.stringToCharacters cfg.host)) == "/")
postgresqlIp = if (cfg.host == "" || (__head (pkgs.lib.stringToCharacters cfg.host)) == "/")
then "127.0.0.1"
else cfg.host;
in lib.mkIf cfg.enable {
systemd.services.graphql-engine = {
wantedBy = [ "multi-user.target" ];
requires = [ "postgresql.service" ];
path = with pkgs; [ curl netcat postgresql sudo ];
environment = {
HASURA_GRAPHQL_DATABASE_URL = "postgres://${cfg.dbUser}:${cfg.password}@${cfg.host}${if cfg.host == "" then "" else toString ":${toString cfg.dbPort}"}/${cfg.db}";
};
preStart = ''
for x in {1..10}; do
nc -z ${postgresqlIp} ${toString cfg.dbPort} && break
Expand All @@ -71,12 +78,7 @@ in {
sudo -u ${cfg.dbAdminUser} -- psql ${cfg.db} < ${hasuraDbPerms}
'';
script = ''
exec ${graphqlEngine}/bin/graphql-engine \
--host ${cfg.host} \
-u ${cfg.dbUser} \
--password ${cfg.password} \
-d ${cfg.db} \
--port ${toString cfg.dbPort} \
exec ${cfg.package}/bin/graphql-engine \
serve \
--server-port ${toString cfg.enginePort} \
--enable-telemetry=false \
Expand Down
3 changes: 2 additions & 1 deletion packages/api-cardano-db-hasura/src/HasuraBackgroundClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class HasuraBackgroundClient {

constructor (
readonly hasuraCliPath: string,
readonly hasuraCliExtPath: string,
readonly hasuraUri: string,
private logger: Logger = dummyLogger
) {
Expand All @@ -39,7 +40,7 @@ export class HasuraBackgroundClient {
private async hasuraCli (command: string) {
return new Promise((resolve, reject) => {
exec(
`${this.hasuraCliPath} --skip-update-check --project ${path.resolve(__dirname, '..', 'hasura', 'project')} --endpoint ${this.hasuraUri} ${command}`,
`${this.hasuraCliPath} --cli-ext-path ${this.hasuraCliExtPath} --skip-update-check --project ${path.resolve(__dirname, '..', 'hasura', 'project')} --endpoint ${this.hasuraUri} ${command}`,
(error, stdout) => {
if (error) {
reject(error)
Expand Down
7 changes: 7 additions & 0 deletions packages/api-cardano-db-hasura/src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class MissingConfig extends CustomError {
export interface BackgroundConfig {
db: DbConfig,
hasuraCliPath: string,
hasuraCliExtPath: string,
hasuraUri: string,
loggerMinSeverity: LogLevelString,
metadataServerUri: string,
Expand All @@ -34,6 +35,9 @@ async function getConfig (): Promise<BackgroundConfig> {
if (!env.hasuraCliPath) {
throw new MissingConfig('HASURA_CLI_PATH env not set')
}
if (!env.hasuraCliExtPath) {
throw new MissingConfig('HASURA_CLI_EXT_PATH env not set')
}
if (!env.hasuraUri) {
throw new MissingConfig('HASURA_URI env not set')
}
Expand Down Expand Up @@ -79,6 +83,7 @@ function filterAndTypecastEnvs (env: any) {
const {
ASSET_METADATA_UPDATE_INTERVAL,
HASURA_CLI_PATH,
HASURA_CLI_EXT_PATH,
HASURA_URI,
LOGGER_MIN_SEVERITY,
METADATA_SERVER_URI,
Expand All @@ -95,6 +100,7 @@ function filterAndTypecastEnvs (env: any) {
} = env as NodeJS.ProcessEnv
return {
hasuraCliPath: HASURA_CLI_PATH,
hasuraCliExtPath: HASURA_CLI_EXT_PATH,
hasuraUri: HASURA_URI,
loggerMinSeverity: LOGGER_MIN_SEVERITY as LogLevelString,
metadataServerUri: METADATA_SERVER_URI,
Expand Down Expand Up @@ -127,6 +133,7 @@ function filterAndTypecastEnvs (env: any) {
try {
const hasuraBackgroundClient = new HasuraBackgroundClient(
config.hasuraCliPath,
config.hasuraCliExtPath,
config.hasuraUri,
logger
)
Expand Down