Skip to content

Commit

Permalink
nixosModules.MooseFS: Format
Browse files Browse the repository at this point in the history
  • Loading branch information
liberodark committed Dec 22, 2024
1 parent 93ff48c commit 10f6382
Showing 1 changed file with 191 additions and 134 deletions.
325 changes: 191 additions & 134 deletions nixos/modules/services/network-filesystems/moosefs.nix
Original file line number Diff line number Diff line change
@@ -1,29 +1,55 @@
{ config, lib, pkgs, ... }:
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.moosefs;

mfsUser = if cfg.runAsUser then "moosefs" else "root";

settingsFormat = let
listSep = " ";
allowedTypes = with lib.types; [ bool int float str ];
valueToString = val:
if lib.isList val then lib.concatStringsSep listSep (map (x: valueToString x) val)
else if lib.isBool val then (if val then "1" else "0")
else toString val;

in {
type = with lib.types; let
valueType = oneOf ([
(listOf valueType)
] ++ allowedTypes) // {
description = "Flat key-value file";
};
in attrsOf valueType;

generate = name: value:
pkgs.writeText name ( lib.concatStringsSep "\n" (
lib.mapAttrsToList (key: val: "${key} = ${valueToString val}") value ));
settingsFormat =
let
listSep = " ";
allowedTypes = with lib.types; [
bool
int
float
str
];
valueToString =
val:
if lib.isList val then
lib.concatStringsSep listSep (map (x: valueToString x) val)
else if lib.isBool val then
(if val then "1" else "0")
else
toString val;

in
{
type =
with lib.types;
let
valueType =
oneOf (
[
(listOf valueType)
]
++ allowedTypes
)
// {
description = "Flat key-value file";
};
in
attrsOf valueType;

generate =
name: value:
pkgs.writeText name (
lib.concatStringsSep "\n" (lib.mapAttrsToList (key: val: "${key} = ${valueToString val}") value)
);
};

# Manual initialization tool
Expand All @@ -44,18 +70,22 @@ let
systemdService = name: extraConfig: configFile: {
wantedBy = [ "multi-user.target" ];
wants = [ "network-online.target" ];
after = [ "network.target" "network-online.target" ];
after = [
"network.target"
"network-online.target"
];

serviceConfig = {
Type = "forking";
ExecStart = "${pkgs.moosefs}/bin/mfs${name} -c ${configFile} start";
ExecStop = "${pkgs.moosefs}/bin/mfs${name} -c ${configFile} stop";
ExecStart = "${pkgs.moosefs}/bin/mfs${name} -c ${configFile} start";
ExecStop = "${pkgs.moosefs}/bin/mfs${name} -c ${configFile} stop";
ExecReload = "${pkgs.moosefs}/bin/mfs${name} -c ${configFile} reload";
PIDFile = "${cfg."${name}".settings.DATA_PATH}/.mfs${name}.lock";
} // extraConfig;
};

in {
in
{
###### interface
options = {
services.moosefs = {
Expand Down Expand Up @@ -150,7 +180,10 @@ in {
type = with lib.types; listOf str;
default = null;
description = "Mount points used by chunkserver for data storage (see mfshdd.cfg).";
example = [ "/mnt/hdd1" "/mnt/hdd2" ];
example = [
"/mnt/hdd1"
"/mnt/hdd2"
];
};

settings = lib.mkOption {
Expand Down Expand Up @@ -197,123 +230,147 @@ in {
};
};
};
default = {};
default = { };
description = "CGI server configuration options.";
};
};
};
};

###### implementation
config = lib.mkIf (cfg.client.enable || cfg.master.enable || cfg.metalogger.enable || cfg.chunkserver.enable || cfg.cgiserver.enable) {
warnings = [ ( lib.mkIf (!cfg.runAsUser) "Running MooseFS services as root is not recommended.") ];
config =
lib.mkIf
(
cfg.client.enable
|| cfg.master.enable
|| cfg.metalogger.enable
|| cfg.chunkserver.enable
|| cfg.cgiserver.enable
)
{
warnings = [ (lib.mkIf (!cfg.runAsUser) "Running MooseFS services as root is not recommended.") ];

services.moosefs = {
master.settings = lib.mkIf cfg.master.enable (
lib.mkMerge [
{
WORKING_USER = mfsUser;
EXPORTS_FILENAME = toString (
pkgs.writeText "mfsexports.cfg" (lib.concatStringsSep "\n" cfg.master.exports)
);
}
(lib.mkIf cfg.cgiserver.enable {
MFSCGISERV = toString cfg.cgiserver.settings.PORT;
})
]
);

metalogger.settings = lib.mkIf cfg.metalogger.enable {
WORKING_USER = mfsUser;
MASTER_HOST = cfg.masterHost;
};

services.moosefs = {
master.settings = lib.mkIf cfg.master.enable (lib.mkMerge [
{
WORKING_USER = mfsUser;
EXPORTS_FILENAME = toString ( pkgs.writeText "mfsexports.cfg"
(lib.concatStringsSep "\n" cfg.master.exports));
}
(lib.mkIf cfg.cgiserver.enable {
MFSCGISERV = toString cfg.cgiserver.settings.PORT;
})
]);

metalogger.settings = lib.mkIf cfg.metalogger.enable {
WORKING_USER = mfsUser;
MASTER_HOST = cfg.masterHost;
};
chunkserver.settings = lib.mkIf cfg.chunkserver.enable {
WORKING_USER = mfsUser;
MASTER_HOST = cfg.masterHost;
HDD_CONF_FILENAME = toString (
pkgs.writeText "mfshdd.cfg" (lib.concatStringsSep "\n" cfg.chunkserver.hdds)
);
};
};

chunkserver.settings = lib.mkIf cfg.chunkserver.enable {
WORKING_USER = mfsUser;
MASTER_HOST = cfg.masterHost;
HDD_CONF_FILENAME = toString ( pkgs.writeText "mfshdd.cfg"
(lib.concatStringsSep "\n" cfg.chunkserver.hdds));
};
};
users =
lib.mkIf
(
cfg.runAsUser
&& (cfg.master.enable || cfg.metalogger.enable || cfg.chunkserver.enable || cfg.cgiserver.enable)
)
{
users.moosefs = {
isSystemUser = true;
description = "MooseFS daemon user";
group = "moosefs";
};
groups.moosefs = { };
};

users = lib.mkIf ( cfg.runAsUser && ( cfg.master.enable || cfg.metalogger.enable || cfg.chunkserver.enable || cfg.cgiserver.enable ) ) {
users.moosefs = {
isSystemUser = true;
description = "MooseFS daemon user";
group = "moosefs";
environment.systemPackages =
(lib.optional cfg.client.enable pkgs.moosefs) ++ (lib.optional cfg.master.enable initTool);

networking.firewall.allowedTCPPorts = lib.mkMerge [
(lib.optionals cfg.master.openFirewall [
9419
9420
9421
])
(lib.optional cfg.chunkserver.openFirewall 9422)
(lib.optional (cfg.cgiserver.enable && cfg.cgiserver.openFirewall) cfg.cgiserver.settings.PORT)
];

systemd.tmpfiles.rules =
[
# Master directories
(lib.optionalString cfg.master.enable "d ${cfg.master.settings.DATA_PATH} 0700 ${mfsUser} ${mfsUser} -")

# Metalogger directories
(lib.optionalString cfg.metalogger.enable "d ${cfg.metalogger.settings.DATA_PATH} 0700 ${mfsUser} ${mfsUser} -")

# Chunkserver directories
(lib.optionalString cfg.chunkserver.enable "d ${cfg.chunkserver.settings.DATA_PATH} 0700 ${mfsUser} ${mfsUser} -")
]
++ lib.optionals (cfg.chunkserver.enable && cfg.chunkserver.hdds != null) (
map (dir: "d ${dir} 0755 ${mfsUser} ${mfsUser} -") cfg.chunkserver.hdds
);

systemd.services = lib.mkMerge [
(lib.mkIf cfg.master.enable {
mfs-master = (
lib.mkMerge [
(systemdService "master" {
TimeoutStartSec = 1800;
TimeoutStopSec = 1800;
Restart = "on-failure";
User = mfsUser;
} masterCfg)
{
preStart = lib.mkIf cfg.master.autoInit "${initTool}/bin/mfsmaster-init";
}
]
);
})

(lib.mkIf cfg.metalogger.enable {
mfs-metalogger = systemdService "metalogger" {
Restart = "on-abnormal";
User = mfsUser;
} metaloggerCfg;
})

(lib.mkIf cfg.chunkserver.enable {
mfs-chunkserver = systemdService "chunkserver" {
Restart = "on-abnormal";
User = mfsUser;
} chunkserverCfg;
})

(lib.mkIf cfg.cgiserver.enable {
mfs-cgiserv = {
description = "MooseFS CGI Server";
wantedBy = [ "multi-user.target" ];
after = [ "mfs-master.service" ];

serviceConfig = {
Type = "simple";
ExecStart = "${pkgs.moosefs}/bin/mfscgiserv -D /var/lib/mfs -f start";
ExecStop = "${pkgs.moosefs}/bin/mfscgiserv -D /var/lib/mfs stop";
Restart = "on-failure";
RestartSec = "30s";
User = mfsUser;
Group = mfsUser;
WorkingDirectory = "/var/lib/mfs";
};
};
})
];
};
groups.moosefs = {};
};

environment.systemPackages =
(lib.optional cfg.client.enable pkgs.moosefs) ++
(lib.optional cfg.master.enable initTool);

networking.firewall.allowedTCPPorts = lib.mkMerge [
(lib.optionals cfg.master.openFirewall [ 9419 9420 9421 ])
(lib.optional cfg.chunkserver.openFirewall 9422)
(lib.optional (cfg.cgiserver.enable && cfg.cgiserver.openFirewall) cfg.cgiserver.settings.PORT)
];

systemd.tmpfiles.rules = [
# Master directories
(lib.optionalString cfg.master.enable
"d ${cfg.master.settings.DATA_PATH} 0700 ${mfsUser} ${mfsUser} -")

# Metalogger directories
(lib.optionalString cfg.metalogger.enable
"d ${cfg.metalogger.settings.DATA_PATH} 0700 ${mfsUser} ${mfsUser} -")

# Chunkserver directories
(lib.optionalString cfg.chunkserver.enable
"d ${cfg.chunkserver.settings.DATA_PATH} 0700 ${mfsUser} ${mfsUser} -")
] ++ lib.optionals (cfg.chunkserver.enable && cfg.chunkserver.hdds != null)
(map (dir: "d ${dir} 0755 ${mfsUser} ${mfsUser} -") cfg.chunkserver.hdds);

systemd.services = lib.mkMerge [
(lib.mkIf cfg.master.enable {
mfs-master = (lib.mkMerge [
(systemdService "master" {
TimeoutStartSec = 1800;
TimeoutStopSec = 1800;
Restart = "on-failure";
User = mfsUser;
} masterCfg)
{
preStart = lib.mkIf cfg.master.autoInit "${initTool}/bin/mfsmaster-init";
}
]);
})

(lib.mkIf cfg.metalogger.enable {
mfs-metalogger = systemdService "metalogger" {
Restart = "on-abnormal";
User = mfsUser;
} metaloggerCfg;
})

(lib.mkIf cfg.chunkserver.enable {
mfs-chunkserver = systemdService "chunkserver" {
Restart = "on-abnormal";
User = mfsUser;
} chunkserverCfg;
})

(lib.mkIf cfg.cgiserver.enable {
mfs-cgiserv = {
description = "MooseFS CGI Server";
wantedBy = [ "multi-user.target" ];
after = [ "mfs-master.service" ];

serviceConfig = {
Type = "simple";
ExecStart = "${pkgs.moosefs}/bin/mfscgiserv -D /var/lib/mfs -f start";
ExecStop = "${pkgs.moosefs}/bin/mfscgiserv -D /var/lib/mfs stop";
Restart = "on-failure";
RestartSec = "30s";
User = mfsUser;
Group = mfsUser;
WorkingDirectory = "/var/lib/mfs";
};
};
})
];
};
}

0 comments on commit 10f6382

Please sign in to comment.