Skip to content

Commit

Permalink
nixosModules.seaweedfs: Add Seaweedfs Module
Browse files Browse the repository at this point in the history
  • Loading branch information
liberodark committed Nov 5, 2024
1 parent 4aa3656 commit f984c57
Showing 1 changed file with 242 additions and 0 deletions.
242 changes: 242 additions & 0 deletions nixos/modules/services/network-filesystems/seaweedfs.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
{ config, lib, pkgs, ... }:

with lib;

let
cfg = config.services.seaweedfs;
in {
options.services.seaweedfs = {
package = mkOption {
type = types.package;
default = pkgs.seaweedfs;
defaultText = literalExpression "pkgs.seaweedfs";
description = "The SeaweedFS package to use.";
};

master = {
enable = mkEnableOption "SeaweedFS master server";

port = mkOption {
type = types.port;
default = 9333;
description = "Port for master server.";
};

ip = mkOption {
type = types.str;
description = "IP address to bind to.";
};

dataDir = mkOption {
type = types.str;
default = "/var/lib/seaweedfs/master";
description = "Data directory for master.";
};

peers = mkOption {
type = types.listOf types.str;
default = [];
example = [ "192.168.1.10:9333" "192.168.1.11:9333" ];
description = "List of master peers for clustering.";
};

volumeSizeLimitMB = mkOption {
type = types.int;
default = 1024;
description = "Volume size limit in MB.";
};
};

volume = {
enable = mkEnableOption "SeaweedFS volume server";

port = mkOption {
type = types.port;
default = 8080;
description = "Port for volume server.";
};

ip = mkOption {
type = types.str;
description = "IP address to bind to.";
};

dataDir = mkOption {
type = types.str;
default = "/var/lib/seaweedfs/volume";
description = "Data directory for volume.";
};

master = mkOption {
type = types.str;
description = "Master server address (e.g., localhost:9333).";
};

maxVolumes = mkOption {
type = types.int;
default = 7;
description = "Maximum number of volumes.";
};
};

filer = {
enable = mkEnableOption "SeaweedFS filer server";

port = mkOption {
type = types.port;
default = 8888;
description = "Port for filer server.";
};

ip = mkOption {
type = types.str;
description = "IP address to bind to.";
};

dataDir = mkOption {
type = types.str;
default = "/var/lib/seaweedfs/filer";
description = "Data directory for filer.";
};

master = mkOption {
type = types.str;
description = "Master server address (e.g., localhost:9333).";
};

maxMB = mkOption {
type = types.int;
default = 4;
description = "Split files larger than the limit.";
};
};
};

config = mkMerge [
(mkIf (cfg.master.enable || cfg.volume.enable || cfg.filer.enable) {
users.users.seaweedfs = {
isSystemUser = true;
group = "seaweedfs";
home = "/var/lib/seaweedfs";
createHome = true;
};

users.groups.seaweedfs = {};

systemd.tmpfiles.rules = [
"d /var/lib/seaweedfs 0755 seaweedfs seaweedfs -"
"d /var/lib/seaweedfs 0755 seaweedfs seaweedfs -"
"d /var/lib/seaweedfs/master 0755 seaweedfs seaweedfs -"
"d /var/lib/seaweedfs/volume 0755 seaweedfs seaweedfs -"
"d /var/lib/seaweedfs/filer 0755 seaweedfs seaweedfs -"
];
})

(mkIf cfg.master.enable {
systemd.services.seaweedfs-master = {
description = "SeaweedFS Master Server";
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
wantedBy = [ "multi-user.target" ];

preStart = ''
mkdir -p ${cfg.master.dataDir}
chown -R seaweedfs:seaweedfs ${cfg.master.dataDir}
chmod 755 ${cfg.master.dataDir}
'';

serviceConfig = {
ExecStart = ''
${cfg.package}/bin/weed master \
-port=${toString cfg.master.port} \
-ip=${cfg.master.ip} \
-mdir=${cfg.master.dataDir} \
-volumeSizeLimitMB=${toString cfg.master.volumeSizeLimitMB} \
${optionalString (cfg.master.peers != []) "-peers=${concatStringsSep "," cfg.master.peers}"}
'';
User = "seaweedfs";
Group = "seaweedfs";
StateDirectory = "seaweedfs";
RuntimeDirectory = "seaweedfs";
Restart = "always";
RestartSec = "10s";
WorkingDirectory = "${cfg.master.dataDir}";
LimitNOFILE = 65535;
AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ];
};
};
})

(mkIf cfg.volume.enable {
systemd.services.seaweedfs-volume = {
description = "SeaweedFS Volume Server";
after = [ "network-online.target" "seaweedfs-master.service" ];
wants = [ "network-online.target" ];
requires = [ "seaweedfs-master.service" ];
wantedBy = [ "multi-user.target" ];

preStart = ''
mkdir -p ${cfg.volume.dataDir}
chown -R seaweedfs:seaweedfs ${cfg.volume.dataDir}
chmod 755 ${cfg.volume.dataDir}
'';

serviceConfig = {
ExecStart = ''
${cfg.package}/bin/weed volume \
-port=${toString cfg.volume.port} \
-ip=${cfg.volume.ip} \
-dir=${cfg.volume.dataDir} \
-mserver=${cfg.volume.master} \
-max=${toString cfg.volume.maxVolumes}
'';
User = "seaweedfs";
Group = "seaweedfs";
StateDirectory = "seaweedfs";
RuntimeDirectory = "seaweedfs";
Restart = "always";
RestartSec = "10s";
WorkingDirectory = "${cfg.volume.dataDir}";
LimitNOFILE = 65535;
AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ];
};
};
})

(mkIf cfg.filer.enable {
systemd.services.seaweedfs-filer = {
description = "SeaweedFS Filer Server";
after = [ "network-online.target" "seaweedfs-master.service" ];
wants = [ "network-online.target" ];
requires = [ "seaweedfs-master.service" ];
wantedBy = [ "multi-user.target" ];

preStart = ''
mkdir -p ${cfg.filer.dataDir}
chown -R seaweedfs:seaweedfs ${cfg.filer.dataDir}
chmod 755 ${cfg.filer.dataDir}
'';

serviceConfig = {
ExecStart = ''
${cfg.package}/bin/weed filer \
-port=${toString cfg.filer.port} \
-ip=${cfg.filer.ip} \
-master=${cfg.filer.master} \
-defaultStoreDir=${cfg.filer.dataDir} \
-maxMB=${toString cfg.filer.maxMB}
'';
User = "seaweedfs";
Group = "seaweedfs";
StateDirectory = "seaweedfs";
RuntimeDirectory = "seaweedfs";
Restart = "always";
RestartSec = "10s";
WorkingDirectory = "${cfg.filer.dataDir}";
LimitNOFILE = 65535;
AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ];
};
};
})
];
}

0 comments on commit f984c57

Please sign in to comment.