0
0
Fork 0
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-07-13 21:50:33 +03:00

nixos/services.ceph: remove with lib;

This commit is contained in:
Felix Buehler 2024-08-30 00:47:07 +02:00
parent 1f34eeb672
commit 31c38324dd

View file

@ -1,16 +1,13 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
with lib;
let let
cfg = config.services.ceph; cfg = config.services.ceph;
# function that translates "camelCaseOptions" to "camel case options", credits to tilpner in #nixos@freenode # function that translates "camelCaseOptions" to "camel case options", credits to tilpner in #nixos@freenode
expandCamelCase = replaceStrings upperChars (map (s: " ${s}") lowerChars); expandCamelCase = lib.replaceStrings lib.upperChars (map (s: " ${s}") lib.lowerChars);
expandCamelCaseAttrs = mapAttrs' (name: value: nameValuePair (expandCamelCase name) value); expandCamelCaseAttrs = lib.mapAttrs' (name: value: lib.nameValuePair (expandCamelCase name) value);
makeServices = daemonType: daemonIds: makeServices = daemonType: daemonIds:
mkMerge (map (daemonId: lib.mkMerge (map (daemonId:
{ "ceph-${daemonType}-${daemonId}" = makeService daemonType daemonId cfg.global.clusterName cfg.${daemonType}.package; }) { "ceph-${daemonType}-${daemonId}" = makeService daemonType daemonId cfg.global.clusterName cfg.${daemonType}.package; })
daemonIds); daemonIds);
@ -18,8 +15,8 @@ let
let let
stateDirectory = "ceph/${if daemonType == "rgw" then "radosgw" else daemonType}/${clusterName}-${daemonId}"; in { stateDirectory = "ceph/${if daemonType == "rgw" then "radosgw" else daemonType}/${clusterName}-${daemonId}"; in {
enable = true; enable = true;
description = "Ceph ${builtins.replaceStrings lowerChars upperChars daemonType} daemon ${daemonId}"; description = "Ceph ${builtins.replaceStrings lib.lowerChars lib.upperChars daemonType} daemon ${daemonId}";
after = [ "network-online.target" "time-sync.target" ] ++ optional (daemonType == "osd") "ceph-mon.target"; after = [ "network-online.target" "time-sync.target" ] ++ lib.optional (daemonType == "osd") "ceph-mon.target";
wants = [ "network-online.target" "time-sync.target" ]; wants = [ "network-online.target" "time-sync.target" ];
partOf = [ "ceph-${daemonType}.target" ]; partOf = [ "ceph-${daemonType}.target" ];
wantedBy = [ "ceph-${daemonType}.target" ]; wantedBy = [ "ceph-${daemonType}.target" ];
@ -47,11 +44,11 @@ let
Group = if daemonType == "osd" then "disk" else "ceph"; Group = if daemonType == "osd" then "disk" else "ceph";
ExecStart = ''${ceph.out}/bin/${if daemonType == "rgw" then "radosgw" else "ceph-${daemonType}"} \ ExecStart = ''${ceph.out}/bin/${if daemonType == "rgw" then "radosgw" else "ceph-${daemonType}"} \
-f --cluster ${clusterName} --id ${daemonId}''; -f --cluster ${clusterName} --id ${daemonId}'';
} // optionalAttrs (daemonType == "osd") { } // lib.optionalAttrs (daemonType == "osd") {
ExecStartPre = "${ceph.lib}/libexec/ceph/ceph-osd-prestart.sh --id ${daemonId} --cluster ${clusterName}"; ExecStartPre = "${ceph.lib}/libexec/ceph/ceph-osd-prestart.sh --id ${daemonId} --cluster ${clusterName}";
RestartSec = "20s"; RestartSec = "20s";
PrivateDevices = "no"; # osd needs disk access PrivateDevices = "no"; # osd needs disk access
} // optionalAttrs ( daemonType == "mon") { } // lib.optionalAttrs ( daemonType == "mon") {
RestartSec = "10"; RestartSec = "10";
}; };
}; };
@ -71,11 +68,11 @@ in
options.services.ceph = { options.services.ceph = {
# Ceph has a monolithic configuration file but different sections for # Ceph has a monolithic configuration file but different sections for
# each daemon, a separate client section and a global section # each daemon, a separate client section and a global section
enable = mkEnableOption "Ceph global configuration"; enable = lib.mkEnableOption "Ceph global configuration";
global = { global = {
fsid = mkOption { fsid = lib.mkOption {
type = types.str; type = lib.types.str;
example = '' example = ''
433a2193-4f8a-47a0-95d2-209d7ca2cca5 433a2193-4f8a-47a0-95d2-209d7ca2cca5
''; '';
@ -85,25 +82,25 @@ in
''; '';
}; };
clusterName = mkOption { clusterName = lib.mkOption {
type = types.str; type = lib.types.str;
default = "ceph"; default = "ceph";
description = '' description = ''
Name of cluster Name of cluster
''; '';
}; };
mgrModulePath = mkOption { mgrModulePath = lib.mkOption {
type = types.path; type = lib.types.path;
default = "${pkgs.ceph.lib}/lib/ceph/mgr"; default = "${pkgs.ceph.lib}/lib/ceph/mgr";
defaultText = literalExpression ''"''${pkgs.ceph.lib}/lib/ceph/mgr"''; defaultText = lib.literalExpression ''"''${pkgs.ceph.lib}/lib/ceph/mgr"'';
description = '' description = ''
Path at which to find ceph-mgr modules. Path at which to find ceph-mgr modules.
''; '';
}; };
monInitialMembers = mkOption { monInitialMembers = lib.mkOption {
type = with types; nullOr commas; type = with lib.types; nullOr commas;
default = null; default = null;
example = '' example = ''
node0, node1, node2 node0, node1, node2
@ -113,8 +110,8 @@ in
''; '';
}; };
monHost = mkOption { monHost = lib.mkOption {
type = with types; nullOr commas; type = with lib.types; nullOr commas;
default = null; default = null;
example = '' example = ''
10.10.0.1, 10.10.0.2, 10.10.0.3 10.10.0.1, 10.10.0.2, 10.10.0.3
@ -124,40 +121,40 @@ in
''; '';
}; };
maxOpenFiles = mkOption { maxOpenFiles = lib.mkOption {
type = types.int; type = lib.types.int;
default = 131072; default = 131072;
description = '' description = ''
Max open files for each OSD daemon. Max open files for each OSD daemon.
''; '';
}; };
authClusterRequired = mkOption { authClusterRequired = lib.mkOption {
type = types.enum [ "cephx" "none" ]; type = lib.types.enum [ "cephx" "none" ];
default = "cephx"; default = "cephx";
description = '' description = ''
Enables requiring daemons to authenticate with eachother in the cluster. Enables requiring daemons to authenticate with eachother in the cluster.
''; '';
}; };
authServiceRequired = mkOption { authServiceRequired = lib.mkOption {
type = types.enum [ "cephx" "none" ]; type = lib.types.enum [ "cephx" "none" ];
default = "cephx"; default = "cephx";
description = '' description = ''
Enables requiring clients to authenticate with the cluster to access services in the cluster (e.g. radosgw, mds or osd). Enables requiring clients to authenticate with the cluster to access services in the cluster (e.g. radosgw, mds or osd).
''; '';
}; };
authClientRequired = mkOption { authClientRequired = lib.mkOption {
type = types.enum [ "cephx" "none" ]; type = lib.types.enum [ "cephx" "none" ];
default = "cephx"; default = "cephx";
description = '' description = ''
Enables requiring the cluster to authenticate itself to the client. Enables requiring the cluster to authenticate itself to the client.
''; '';
}; };
publicNetwork = mkOption { publicNetwork = lib.mkOption {
type = with types; nullOr commas; type = with lib.types; nullOr commas;
default = null; default = null;
example = '' example = ''
10.20.0.0/24, 192.168.1.0/24 10.20.0.0/24, 192.168.1.0/24
@ -167,8 +164,8 @@ in
''; '';
}; };
clusterNetwork = mkOption { clusterNetwork = lib.mkOption {
type = with types; nullOr commas; type = with lib.types; nullOr commas;
default = null; default = null;
example = '' example = ''
10.10.0.0/24, 192.168.0.0/24 10.10.0.0/24, 192.168.0.0/24
@ -178,18 +175,18 @@ in
''; '';
}; };
rgwMimeTypesFile = mkOption { rgwMimeTypesFile = lib.mkOption {
type = with types; nullOr path; type = with lib.types; nullOr path;
default = "${pkgs.mailcap}/etc/mime.types"; default = "${pkgs.mailcap}/etc/mime.types";
defaultText = literalExpression ''"''${pkgs.mailcap}/etc/mime.types"''; defaultText = lib.literalExpression ''"''${pkgs.mailcap}/etc/mime.types"'';
description = '' description = ''
Path to mime types used by radosgw. Path to mime types used by radosgw.
''; '';
}; };
}; };
extraConfig = mkOption { extraConfig = lib.mkOption {
type = with types; attrsOf str; type = with lib.types; attrsOf str;
default = {}; default = {};
example = { example = {
"ms bind ipv6" = "true"; "ms bind ipv6" = "true";
@ -200,9 +197,9 @@ in
}; };
mgr = { mgr = {
enable = mkEnableOption "Ceph MGR daemon"; enable = lib.mkEnableOption "Ceph MGR daemon";
daemons = mkOption { daemons = lib.mkOption {
type = with types; listOf str; type = with lib.types; listOf str;
default = []; default = [];
example = [ "name1" "name2" ]; example = [ "name1" "name2" ];
description = '' description = ''
@ -210,9 +207,9 @@ in
to the id part in ceph i.e. [ "name1" ] would result in mgr.name1 to the id part in ceph i.e. [ "name1" ] would result in mgr.name1
''; '';
}; };
package = mkPackageOption pkgs "ceph" { }; package = lib.mkPackageOption pkgs "ceph" { };
extraConfig = mkOption { extraConfig = lib.mkOption {
type = with types; attrsOf str; type = with lib.types; attrsOf str;
default = {}; default = {};
description = '' description = ''
Extra configuration to add to the global section for manager daemons. Extra configuration to add to the global section for manager daemons.
@ -221,9 +218,9 @@ in
}; };
mon = { mon = {
enable = mkEnableOption "Ceph MON daemon"; enable = lib.mkEnableOption "Ceph MON daemon";
daemons = mkOption { daemons = lib.mkOption {
type = with types; listOf str; type = with lib.types; listOf str;
default = []; default = [];
example = [ "name1" "name2" ]; example = [ "name1" "name2" ];
description = '' description = ''
@ -231,9 +228,9 @@ in
to the id part in ceph i.e. [ "name1" ] would result in mon.name1 to the id part in ceph i.e. [ "name1" ] would result in mon.name1
''; '';
}; };
package = mkPackageOption pkgs "ceph" { }; package = lib.mkPackageOption pkgs "ceph" { };
extraConfig = mkOption { extraConfig = lib.mkOption {
type = with types; attrsOf str; type = with lib.types; attrsOf str;
default = {}; default = {};
description = '' description = ''
Extra configuration to add to the monitor section. Extra configuration to add to the monitor section.
@ -242,9 +239,9 @@ in
}; };
osd = { osd = {
enable = mkEnableOption "Ceph OSD daemon"; enable = lib.mkEnableOption "Ceph OSD daemon";
daemons = mkOption { daemons = lib.mkOption {
type = with types; listOf str; type = with lib.types; listOf str;
default = []; default = [];
example = [ "name1" "name2" ]; example = [ "name1" "name2" ];
description = '' description = ''
@ -252,9 +249,9 @@ in
to the id part in ceph i.e. [ "name1" ] would result in osd.name1 to the id part in ceph i.e. [ "name1" ] would result in osd.name1
''; '';
}; };
package = mkPackageOption pkgs "ceph" { }; package = lib.mkPackageOption pkgs "ceph" { };
extraConfig = mkOption { extraConfig = lib.mkOption {
type = with types; attrsOf str; type = with lib.types; attrsOf str;
default = { default = {
"osd journal size" = "10000"; "osd journal size" = "10000";
"osd pool default size" = "3"; "osd pool default size" = "3";
@ -270,9 +267,9 @@ in
}; };
mds = { mds = {
enable = mkEnableOption "Ceph MDS daemon"; enable = lib.mkEnableOption "Ceph MDS daemon";
daemons = mkOption { daemons = lib.mkOption {
type = with types; listOf str; type = with lib.types; listOf str;
default = []; default = [];
example = [ "name1" "name2" ]; example = [ "name1" "name2" ];
description = '' description = ''
@ -280,9 +277,9 @@ in
to the id part in ceph i.e. [ "name1" ] would result in mds.name1 to the id part in ceph i.e. [ "name1" ] would result in mds.name1
''; '';
}; };
package = mkPackageOption pkgs "ceph" { }; package = lib.mkPackageOption pkgs "ceph" { };
extraConfig = mkOption { extraConfig = lib.mkOption {
type = with types; attrsOf str; type = with lib.types; attrsOf str;
default = {}; default = {};
description = '' description = ''
Extra configuration to add to the MDS section. Extra configuration to add to the MDS section.
@ -291,10 +288,10 @@ in
}; };
rgw = { rgw = {
enable = mkEnableOption "Ceph RadosGW daemon"; enable = lib.mkEnableOption "Ceph RadosGW daemon";
package = mkPackageOption pkgs "ceph" { }; package = lib.mkPackageOption pkgs "ceph" { };
daemons = mkOption { daemons = lib.mkOption {
type = with types; listOf str; type = with lib.types; listOf str;
default = []; default = [];
example = [ "name1" "name2" ]; example = [ "name1" "name2" ];
description = '' description = ''
@ -307,11 +304,11 @@ in
}; };
client = { client = {
enable = mkEnableOption "Ceph client configuration"; enable = lib.mkEnableOption "Ceph client configuration";
extraConfig = mkOption { extraConfig = lib.mkOption {
type = with types; attrsOf (attrsOf str); type = with lib.types; attrsOf (attrsOf str);
default = {}; default = {};
example = literalExpression '' example = lib.literalExpression ''
{ {
# This would create a section for a radosgw daemon named node0 and related # This would create a section for a radosgw daemon named node0 and related
# configuration for it # configuration for it
@ -326,7 +323,7 @@ in
}; };
}; };
config = mkIf config.services.ceph.enable { config = lib.mkIf config.services.ceph.enable {
assertions = [ assertions = [
{ assertion = cfg.global.fsid != ""; { assertion = cfg.global.fsid != "";
message = "fsid has to be set to a valid uuid for the cluster to function"; message = "fsid has to be set to a valid uuid for the cluster to function";
@ -345,22 +342,22 @@ in
} }
]; ];
warnings = optional (cfg.global.monInitialMembers == null) warnings = lib.optional (cfg.global.monInitialMembers == null)
"Not setting up a list of members in monInitialMembers requires that you set the host variable for each mon daemon or else the cluster won't function"; "Not setting up a list of members in monInitialMembers requires that you set the host variable for each mon daemon or else the cluster won't function";
environment.etc."ceph/ceph.conf".text = let environment.etc."ceph/ceph.conf".text = let
# Merge the extraConfig set for mgr daemons, as mgr don't have their own section # Merge the extraConfig set for mgr daemons, as mgr don't have their own section
globalSection = expandCamelCaseAttrs (cfg.global // cfg.extraConfig // optionalAttrs cfg.mgr.enable cfg.mgr.extraConfig); globalSection = expandCamelCaseAttrs (cfg.global // cfg.extraConfig // lib.optionalAttrs cfg.mgr.enable cfg.mgr.extraConfig);
# Remove all name-value pairs with null values from the attribute set to avoid making empty sections in the ceph.conf # Remove all name-value pairs with null values from the attribute set to avoid making empty sections in the ceph.conf
globalSection' = filterAttrs (name: value: value != null) globalSection; globalSection' = lib.filterAttrs (name: value: value != null) globalSection;
totalConfig = { totalConfig = {
global = globalSection'; global = globalSection';
} // optionalAttrs (cfg.mon.enable && cfg.mon.extraConfig != {}) { mon = cfg.mon.extraConfig; } } // lib.optionalAttrs (cfg.mon.enable && cfg.mon.extraConfig != {}) { mon = cfg.mon.extraConfig; }
// optionalAttrs (cfg.mds.enable && cfg.mds.extraConfig != {}) { mds = cfg.mds.extraConfig; } // lib.optionalAttrs (cfg.mds.enable && cfg.mds.extraConfig != {}) { mds = cfg.mds.extraConfig; }
// optionalAttrs (cfg.osd.enable && cfg.osd.extraConfig != {}) { osd = cfg.osd.extraConfig; } // lib.optionalAttrs (cfg.osd.enable && cfg.osd.extraConfig != {}) { osd = cfg.osd.extraConfig; }
// optionalAttrs (cfg.client.enable && cfg.client.extraConfig != {}) cfg.client.extraConfig; // lib.optionalAttrs (cfg.client.enable && cfg.client.extraConfig != {}) cfg.client.extraConfig;
in in
generators.toINI {} totalConfig; lib.generators.toINI {} totalConfig;
users.users.ceph = { users.users.ceph = {
uid = config.ids.uids.ceph; uid = config.ids.uids.ceph;
@ -375,13 +372,13 @@ in
systemd.services = let systemd.services = let
services = [] services = []
++ optional cfg.mon.enable (makeServices "mon" cfg.mon.daemons) ++ lib.optional cfg.mon.enable (makeServices "mon" cfg.mon.daemons)
++ optional cfg.mds.enable (makeServices "mds" cfg.mds.daemons) ++ lib.optional cfg.mds.enable (makeServices "mds" cfg.mds.daemons)
++ optional cfg.osd.enable (makeServices "osd" cfg.osd.daemons) ++ lib.optional cfg.osd.enable (makeServices "osd" cfg.osd.daemons)
++ optional cfg.rgw.enable (makeServices "rgw" cfg.rgw.daemons) ++ lib.optional cfg.rgw.enable (makeServices "rgw" cfg.rgw.daemons)
++ optional cfg.mgr.enable (makeServices "mgr" cfg.mgr.daemons); ++ lib.optional cfg.mgr.enable (makeServices "mgr" cfg.mgr.daemons);
in in
mkMerge services; lib.mkMerge services;
systemd.targets = let systemd.targets = let
targets = [ targets = [
@ -390,13 +387,13 @@ in
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
unitConfig.StopWhenUnneeded = true; unitConfig.StopWhenUnneeded = true;
}; } ] }; } ]
++ optional cfg.mon.enable (makeTarget "mon") ++ lib.optional cfg.mon.enable (makeTarget "mon")
++ optional cfg.mds.enable (makeTarget "mds") ++ lib.optional cfg.mds.enable (makeTarget "mds")
++ optional cfg.osd.enable (makeTarget "osd") ++ lib.optional cfg.osd.enable (makeTarget "osd")
++ optional cfg.rgw.enable (makeTarget "rgw") ++ lib.optional cfg.rgw.enable (makeTarget "rgw")
++ optional cfg.mgr.enable (makeTarget "mgr"); ++ lib.optional cfg.mgr.enable (makeTarget "mgr");
in in
mkMerge targets; lib.mkMerge targets;
systemd.tmpfiles.settings."10-ceph" = let systemd.tmpfiles.settings."10-ceph" = let
defaultConfig = { defaultConfig = {
@ -407,9 +404,9 @@ in
"/etc/ceph".d = defaultConfig; "/etc/ceph".d = defaultConfig;
"/run/ceph".d = defaultConfig // { mode = "0770"; }; "/run/ceph".d = defaultConfig // { mode = "0770"; };
"/var/lib/ceph".d = defaultConfig; "/var/lib/ceph".d = defaultConfig;
"/var/lib/ceph/mgr".d = mkIf (cfg.mgr.enable) defaultConfig; "/var/lib/ceph/mgr".d = lib.mkIf (cfg.mgr.enable) defaultConfig;
"/var/lib/ceph/mon".d = mkIf (cfg.mon.enable) defaultConfig; "/var/lib/ceph/mon".d = lib.mkIf (cfg.mon.enable) defaultConfig;
"/var/lib/ceph/osd".d = mkIf (cfg.osd.enable) defaultConfig; "/var/lib/ceph/osd".d = lib.mkIf (cfg.osd.enable) defaultConfig;
}; };
}; };
} }