Skip to content

Commit

Permalink
Switch to rust bookdb
Browse files Browse the repository at this point in the history
  • Loading branch information
barrucadu committed Feb 15, 2024
1 parent 033b9f7 commit c141cd8
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 66 deletions.
12 changes: 5 additions & 7 deletions hosts/carcosa/configuration.nix
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,6 @@ in

# bookdb
nixfiles.bookdb.enable = true;
nixfiles.bookdb.baseURI = "https://bookdb.barrucadu.co.uk";
nixfiles.bookdb.readOnly = true;

# bookmarks
Expand Down Expand Up @@ -433,13 +432,12 @@ in
exit 1
fi
/run/wrappers/bin/sudo ${pkgs.rsync}/bin/rsync -a --delete ~/bookdb-covers/ ${config.nixfiles.bookdb.dataDir}/covers || exit 1
/run/wrappers/bin/sudo ${pkgs.coreutils}/bin/chown -R ${config.users.users.bookdb.name}.nogroup ${config.nixfiles.bookdb.dataDir}/covers || exit 1
/run/wrappers/bin/sudo ${pkgs.rsync}/bin/rsync -a --delete ~/bookdb-covers/ ${config.systemd.services.bookdb.environment.BOOKDB_UPLOADS_DIR} || exit 1
/run/wrappers/bin/sudo ${pkgs.coreutils}/bin/chown -R ${config.users.users.bookdb.name}.nogroup ${config.systemd.services.bookdb.environment.BOOKDB_UPLOADS_DIR} || exit 1
'';
bookdb-receive-elasticsearch = ''
env ES_HOST=${config.systemd.services.bookdb.environment.ES_HOST} \
DELETE_EXISTING_INDEX=1 \
${pkgs.nixfiles.bookdb}/bin/python -m bookdb.index.create -
${pkgs.nixfiles.bookdb}/bin/bookdb_ctl import-index --drop-existing
'';
bookmarks-receive-elasticsearch = ''
env ES_HOST=${config.systemd.services.bookmarks.environment.ES_HOST} \
Expand All @@ -458,8 +456,8 @@ in
{
users = [ config.users.extraUsers.nyarlathotep-remote-sync.name ];
commands = [
{ command = "${pkgs.rsync}/bin/rsync -a --delete ${config.users.extraUsers.nyarlathotep-remote-sync.home}/bookdb-covers/ ${config.nixfiles.bookdb.dataDir}/covers"; options = [ "NOPASSWD" ]; }
{ command = "${pkgs.coreutils}/bin/chown -R ${config.users.users.bookdb.name}.nogroup ${config.nixfiles.bookdb.dataDir}/covers"; options = [ "NOPASSWD" ]; }
{ command = "${pkgs.rsync}/bin/rsync -a --delete ${config.users.extraUsers.nyarlathotep-remote-sync.home}/bookdb-covers/ ${config.systemd.services.bookdb.environment.BOOKDB_UPLOADS_DIR}"; options = [ "NOPASSWD" ]; }
{ command = "${pkgs.coreutils}/bin/chown -R ${config.users.users.bookdb.name}.nogroup ${config.systemd.services.bookdb.environment.BOOKDB_UPLOADS_DIR}"; options = [ "NOPASSWD" ]; }
];
}
];
Expand Down
7 changes: 3 additions & 4 deletions hosts/nyarlathotep/configuration.nix
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ in
###############################################################################

nixfiles.bookdb.enable = true;
nixfiles.bookdb.baseURI = "http://bookdb.nyarlathotep.lan";


###############################################################################
Expand Down Expand Up @@ -488,7 +487,7 @@ in
ExecStart = pkgs.writeShellScript "bookdb-sync" ''
set -ex
/run/wrappers/bin/sudo ${pkgs.coreutils}/bin/cp -r ${config.nixfiles.bookdb.dataDir}/covers/ ~/bookdb-covers
/run/wrappers/bin/sudo ${pkgs.coreutils}/bin/cp -r ${config.systemd.services.bookdb.environment.BOOKDB_UPLOADS_DIR}/ ~/bookdb-covers
trap "/run/wrappers/bin/sudo ${pkgs.coreutils}/bin/rm -rf ~/bookdb-covers" EXIT
rsync -az\
-e "ssh -i $SSH_KEY_FILE -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" \
Expand All @@ -501,7 +500,7 @@ in
bookdb-receive-covers
env "ES_HOST=$ES_HOST" \
${pkgs.nixfiles.bookdb}/bin/python -m bookdb.index.dump | \
${pkgs.nixfiles.bookdb}/bin/bookdb_ctl export-index | \
ssh -i "$SSH_KEY_FILE" \
-o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no \
Expand Down Expand Up @@ -544,7 +543,7 @@ in
{
users = [ config.users.extraUsers.remote-sync.name ];
commands = [
{ command = "${pkgs.coreutils}/bin/cp -r ${config.nixfiles.bookdb.dataDir}/covers/ ${config.users.extraUsers.remote-sync.home}/bookdb-covers"; options = [ "NOPASSWD" ]; }
{ command = "${pkgs.coreutils}/bin/cp -r ${config.systemd.services.bookdb.environment.BOOKDB_UPLOADS_DIR}/ ${config.users.extraUsers.remote-sync.home}/bookdb-covers"; options = [ "NOPASSWD" ]; }
{ command = "${pkgs.coreutils}/bin/rm -rf ${config.users.extraUsers.remote-sync.home}/bookdb-covers"; options = [ "NOPASSWD" ]; }
];
}
Expand Down
34 changes: 17 additions & 17 deletions packages/bookdb/default.nix
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# A database and web app to keep track of all my books.
{ poetry2nix, fetchFromGitHub, ... }:
{ rustPlatform, fetchFromGitHub, openssl, pkg-config, ... }:

let
githubOwner = "barrucadu";
githubRepo = "bookdb";
githubRev = "6040d270ae7ac7ecec09849885b6405d0650dff2";

app = poetry2nix.mkPoetryApplication {
projectDir = fetchFromGitHub {
owner = githubOwner;
repo = githubRepo;
rev = githubRev;
sha256 = "sha256-U93t2dbGjBej6+IsI2mUVqm0Sirw/DIJqYH0USUF7to=";
};
githubRev = "a8ae9b427d08ef7e30eee57ce43a367e85f63e70";
in
rustPlatform.buildRustPackage {
pname = githubRepo;
version = githubRev;

overrides = poetry2nix.overrides.withDefaults (self: super: {
elastic-transport = super.elastic-transport.overridePythonAttrs (old: {
buildInputs = (old.buildInputs or [ ]) ++ [ self.setuptools ];
});
});
src = fetchFromGitHub {
owner = githubOwner;
repo = githubRepo;
rev = githubRev;
sha256 = "sha256-7hD2BPEIl2j9dS86Bvx6ERTKzV84zzAp7b/jH48cUoY=";
};
in
app.dependencyEnv

cargoSha256 = "sha256-3/T6DKWkKjrxu3b25nDunmd3zGThL7uDre1pJ+HXkMc=";

nativeBuildInputs = [ pkg-config ];
buildInputs = [ openssl ];
}
14 changes: 7 additions & 7 deletions shared/bookdb/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ in
requires = [ "${backend}-bookdb-db.service" ];
path = [ pkgs.imagemagick ];
serviceConfig = {
ExecStart = "${pkgs.nixfiles.bookdb}/bin/gunicorn -w 4 -t 60 -b 127.0.0.1:${toString cfg.port} bookdb.serve:app";
ExecStart = "${pkgs.nixfiles.bookdb}/bin/bookdb ${optionalString (!cfg.readOnly) "--allow-writes"} ${./uuids.yaml}";
Restart = "always";
User = config.users.users.bookdb.name;
};
environment = {
"ALLOW_WRITES" = if cfg.readOnly then "0" else "1";
"BASE_URI" = cfg.baseURI;
"COVER_DIR" = "${cfg.dataDir}/covers";
"ES_HOST" = "http://127.0.0.1:${toString cfg.elasticsearchPort}";
"UUIDS_FILE" = ./uuids.yaml;
BOOKDB_ADDRESS = "127.0.0.1:${toString cfg.port}";
BOOKDB_UPLOADS_DIR = "${cfg.dataDir}/covers";
ES_HOST = "http://127.0.0.1:${toString cfg.elasticsearchPort}";
RUST_LOG = cfg.logLevel;
RUST_LOG_FORMAT = cfg.logFormat;
};
};

Expand All @@ -66,7 +66,7 @@ in

nixfiles.restic-backups.backups.bookdb = {
prepareCommand = ''
env ES_HOST=${config.systemd.services.bookdb.environment.ES_HOST} ${pkgs.nixfiles.bookdb}/bin/python -m bookdb.index.dump > elasticsearch-dump.json
env ES_HOST=${config.systemd.services.bookdb.environment.ES_HOST} ${pkgs.nixfiles.bookdb}/bin/bookdb_ctl dump-index > elasticsearch-dump.json
'';
paths = [
cfg.dataDir
Expand Down
24 changes: 16 additions & 8 deletions shared/bookdb/options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ with lib;
'';
};

baseURI = mkOption {
type = types.str;
example = "https://bookdb.barrucadu.co.uk";
description = mdDoc ''
URI which the service will be exposed on, used to generate URLs.
'';
};

readOnly = mkOption {
type = types.bool;
default = false;
Expand All @@ -63,5 +55,21 @@ with lib;
on the persistent volume.
'';
};

logLevel = mkOption {
type = types.str;
default = "info";
description = mdDoc ''
Verbosity of the log messages.
'';
};

logFormat = mkOption {
type = types.str;
default = "json,no-time";
description = mdDoc ''
Format of the log messages.
'';
};
};
}
46 changes: 23 additions & 23 deletions shared/bookdb/uuids.yaml
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
# shared between nyarlathotep & carcosa
ordered_locations:
locations:
- name: London
uuid: be60be7b-a10f-42e1-8769-d43f12cad02d
slug: be60be7b-a10f-42e1-8769-d43f12cad02d
- name: Missing
uuid: 6a233e5e-3b64-4169-ac67-cf46113afd97
slug: 6a233e5e-3b64-4169-ac67-cf46113afd97

ordered_categories:
categories:
- name: Light Novels + Manga
uuid: a3bbb1c6-5ff8-4ddf-81f4-820593a2a5ff
slug: a3bbb1c6-5ff8-4ddf-81f4-820593a2a5ff

- name: Nonfiction
uuid: 58c63aea-72b6-4988-96a0-aca572661303
ordered_children:
slug: 58c63aea-72b6-4988-96a0-aca572661303
children:
- name: Computer Science + Software Engineering
uuid: afc7135b-bf67-4284-bcc1-2bbd3386aea3
slug: afc7135b-bf67-4284-bcc1-2bbd3386aea3
- name: Kitchen, Food, + Recipe
uuid: b5451ba6-a3ef-4c27-99fc-a07d8cd54161
slug: b5451ba6-a3ef-4c27-99fc-a07d8cd54161
- name: Politics, Philosophy, Economics, + History
uuid: ac4706f3-54c3-4e6d-a72d-85321d9dcd72
slug: ac4706f3-54c3-4e6d-a72d-85321d9dcd72
- name: RPG
uuid: cf921942-f65a-40e3-8e98-2f565bb8d033
ordered_children:
slug: cf921942-f65a-40e3-8e98-2f565bb8d033
children:
- name: Call of Cthulhu
uuid: 08bc011e-1a42-4d23-961a-803d786d7e58
slug: 08bc011e-1a42-4d23-961a-803d786d7e58
- name: Traveller
uuid: d8d4ee9d-e5d8-4d4d-8a78-65ebc23fc451
slug: d8d4ee9d-e5d8-4d4d-8a78-65ebc23fc451
- name: Miscellaneous
uuid: 70196ec9-dd61-4241-afc9-dd6be7be30a6
slug: 70196ec9-dd61-4241-afc9-dd6be7be30a6
- name: Miscellaneous
uuid: fb2dd601-883a-4254-a294-fcf0a0f98d2f
slug: fb2dd601-883a-4254-a294-fcf0a0f98d2f

- name: Prose Fiction + Graphic Novels
uuid: 590ac55d-0644-4a71-b902-587faa5b03d9
slug: 590ac55d-0644-4a71-b902-587faa5b03d9

- name: Religion, Mythology, + Folklore
uuid: a7d83bc9-0352-4fb5-a9dc-68428562a17f
ordered_children:
slug: a7d83bc9-0352-4fb5-a9dc-68428562a17f
children:
- name: Abrahamic
uuid: 384664d5-e256-4787-a729-1eb5e7a7de6f
slug: 384664d5-e256-4787-a729-1eb5e7a7de6f
- name: Miscellaneous
uuid: 0435b803-d2e9-49b9-a1c7-bc7fc1b30cdd
slug: 0435b803-d2e9-49b9-a1c7-bc7fc1b30cdd

- name: Verse
uuid: 217f8eaa-b54a-466c-83f8-3f569f46732e
slug: 217f8eaa-b54a-466c-83f8-3f569f46732e

- name: To Donate
uuid: 957d6a70-5dfc-46e2-8e3c-2f193878b2a5
slug: 957d6a70-5dfc-46e2-8e3c-2f193878b2a5

0 comments on commit c141cd8

Please sign in to comment.