Skip to content

Commit

Permalink
Use unix socket for mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucus16 committed Oct 13, 2024
1 parent ed70b18 commit d70368f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 36 deletions.
2 changes: 1 addition & 1 deletion nix/services/kn/django.nix
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ in {
DynamicUser = true;
User = "kndjango";
Group = "kndjango";
SupplementaryGroups = [ "fotos" "infra" ];
SupplementaryGroups = [ "fotos" "infra" "mongodb" ];
ReadWritePaths = [ config.kn.fotos.dir ];
CacheDirectory = "fotos";
Restart = "on-failure";
Expand Down
2 changes: 1 addition & 1 deletion nix/services/kn/giedo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ in {
ReadWritePaths = ["/run/infra"];
ExecStart = "${pkgs.kninfra}/utils/giedo.py";
Restart = "on-failure";
SupplementaryGroups = "infra";
SupplementaryGroups = [ "infra" "mongodb" ];
Type = "notify";
NotifyAccess = "all";
EnvironmentFile = config.age.secrets.kn-env.path;
Expand Down
2 changes: 1 addition & 1 deletion nix/services/kn/settings.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ in {
DEFAULT_FROM_EMAIL =
"Karpe Noktems ledenadministratie <root@${MAILDOMAIN}>";

MONGO_HOST = "localhost";
MONGO_HOST = "mongodb://%2Frun%2Fmongodb%2Fmongodb.sock";
MONGO_DB = "kn";

MODED_MAILINGLISTS = [ "discussie" "in" "uit" "test" ];
Expand Down
72 changes: 40 additions & 32 deletions nix/services/kn/shared.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,42 +29,50 @@ in {
# GRPC_VERBOSITY="DEBUG";
# GRPC_TRACE="tcp";
};
# TODO: limit access to mongodb
services.mongodb.enable = true;
services.mongodb.bind_ip = "/run/mongodb/mongodb.sock";
users.groups.infra = {};
environment.systemPackages = [ pkgs.mongosh knshell ];
systemd.services = lib.mkIf cfg.initialDB {
kn_initial_state = rec {
requires = [ "mongodb.service" ];
after = requires;
requiredBy = [ "giedo.service" "kndjango.service" "rimapd.service" ];
before = requiredBy;
serviceConfig = {
StateDirectory = "kndjango";
Type = "oneshot";
RemainAfterExit = true;
EnvironmentFile = config.age.secrets.kn-env.path;
};
script = ''
# initialize the DB if this has not happened before
if [ ! -f /var/lib/kndjango/database-initialized ]; then
${pkgs.kninfra}/libexec/initializeDb.py
touch /var/lib/kndjango/database-initialized
fi
systemd.services = lib.mkMerge [
{
mongodb.serviceConfig.RuntimeDirectory = "mongodb";
mongodb.postStart = ''
chmod g+rw /run/mongodb/mongodb.sock
'';
};
kn_initial_sync = rec {
requires = [ "giedo.service" "hans.service" "daan.service" ];
after = requires;
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
EnvironmentFile = config.age.secrets.kn-env.path;
ExecStart = "${pkgs.kninfra}/utils/giedo-sync.py";
}
(lib.mkIf cfg.initialDB {
kn_initial_state = rec {
requires = [ "mongodb.service" ];
after = requires;
requiredBy = [ "giedo.service" "kndjango.service" "rimapd.service" ];
before = requiredBy;
serviceConfig = {
StateDirectory = "kndjango";
Type = "oneshot";
RemainAfterExit = true;
EnvironmentFile = config.age.secrets.kn-env.path;
};
script = ''
# initialize the DB if this has not happened before
if [ ! -f /var/lib/kndjango/database-initialized ]; then
${pkgs.kninfra}/libexec/initializeDb.py
touch /var/lib/kndjango/database-initialized
fi
'';
};
environment = cfg.env;
};
};
kn_initial_sync = rec {
requires = [ "giedo.service" "hans.service" "daan.service" ];
after = requires;
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
EnvironmentFile = config.age.secrets.kn-env.path;
ExecStart = "${pkgs.kninfra}/utils/giedo-sync.py";
};
environment = cfg.env;
};
})
];
};
}
2 changes: 1 addition & 1 deletion salt/states/sankhara/initializeDb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import yaml

MONGO_DATABASE = 'kn'
MONGO_HOST = 'localhost'
MONGO_HOST = 'mongodb://%2Frun%2Fmongodb%2Fmongodb.sock'
DB_FILE = os.path.join(os.path.dirname(__file__), "initial-db.yaml")

yaml.SafeLoader.add_constructor('!id',
Expand Down

0 comments on commit d70368f

Please # to comment.