nixos/tsm-client: use freeformType for server config

`tsm-client` uses a global configuration
file that must contain coordinates for each
server that it is supposed to contact.
This configuration consists of text
lines with key-value pairs.

In the NixOS module, these servers may be declared
with an attribute set, where the attribute name
defines an alias for the server, and the value
is again an attribute set with the settings for
the respective server.
This is organized as an option of type `attrsOf submodule...`.

Before this commit:

Important settings have their own option within
the submodule.  For everything else, there is
the "catch-all" option `extraConfig` that may
be used to declare any key-value pairs.
There is also `text` that can be used to
add arbitrary text to each server's
section in the global config file.

After this commit:

`extraConfig` and `text` are gone,
the attribute names and values of each server's attribute
set are translated directly into key-value pairs,
with the following notable rules:

* Lists are translated into multiple lines
  with the same key, as such is permitted by
  the software for certain keys.
* `null` may be used to override/shadow a value that
  is defined elsewhere and hides the corresponding key.

Those "important settings" that have previously been
defined as dedicated options are still defined as such,
but they have been renamed to match their
corresponding key names in the configuration file.
There is a notable exception:
"Our" boolean option `genPasswd` influences the "real"
option `passwordaccess', but the latter one is
uncomfortable to use and might lead
to undesirable outcome if used the wrong way.
So it seems advisable to keep the boolean option
and the warning in its description.
To this end, the value of `getPasswd` itself is
later filtered out when the config file is generated.

The tsm-backup service module and the vm test are adapted.

Migration code will be added in a separate
commit to permit easy reversal later, when the
migration code is no longer deemed necessary.
This commit is contained in:
Yarny0 2023-08-23 16:00:32 +02:00
parent 8b918ed8ab
commit 3fb29fecd5
3 changed files with 98 additions and 118 deletions

View File

@ -2,163 +2,99 @@
let let
inherit (lib.attrsets) attrNames filterAttrs hasAttr mapAttrs mapAttrsToList optionalAttrs; inherit (lib.attrsets) attrValues mapAttrsToList removeAttrs;
inherit (lib.lists) allUnique map; inherit (lib.lists) allUnique concatLists elem isList map;
inherit (lib.modules) mkDefault mkIf; inherit (lib.modules) mkDefault mkIf;
inherit (lib.options) literalExpression mkEnableOption mkOption mkPackageOption; inherit (lib.options) mkEnableOption mkOption mkPackageOption;
inherit (lib.strings) concatLines optionalString toLower; inherit (lib.strings) concatLines optionalString toLower;
inherit (lib.types) addCheck attrsOf lines nonEmptyStr nullOr path port str strMatching submodule; inherit (lib.trivial) isInt;
inherit (lib.types) addCheck attrsOf coercedTo either enum int lines listOf nonEmptyStr nullOr oneOf path port singleLineStr strMatching submodule;
scalarType =
# see the option's description below for the
# handling/transformation of each possible type
oneOf [ (enum [ true null ]) int path singleLineStr ];
# TSM rejects servername strings longer than 64 chars. # TSM rejects servername strings longer than 64 chars.
servernameType = strMatching "[^[:space:]]{1,64}"; servernameType = strMatching "[^[:space:]]{1,64}";
serverOptions = { name, config, ... }: { serverOptions = { name, config, ... }: {
options.name = mkOption { freeformType = attrsOf (either scalarType (listOf scalarType));
# Client system-options file directives are explained here:
# https://www.ibm.com/docs/en/storage-protect/8.1.20?topic=commands-processing-options
options.servername = mkOption {
type = servernameType; type = servernameType;
default = name;
example = "mainTsmServer"; example = "mainTsmServer";
description = lib.mdDoc '' description = lib.mdDoc ''
Local name of the IBM TSM server, Local name of the IBM TSM server,
must be uncapitalized and no longer than 64 chars. must not contain space or more than 64 chars.
The value will be used for the
`server`
directive in {file}`dsm.sys`.
''; '';
}; };
options.server = mkOption { options.tcpserveraddress = mkOption {
type = nonEmptyStr; type = nonEmptyStr;
example = "tsmserver.company.com"; example = "tsmserver.company.com";
description = lib.mdDoc '' description = lib.mdDoc ''
Host/domain name or IP address of the IBM TSM server. Host/domain name or IP address of the IBM TSM server.
The value will be used for the
`tcpserveraddress`
directive in {file}`dsm.sys`.
''; '';
}; };
options.port = mkOption { options.tcpport = mkOption {
type = addCheck port (p: p<=32767); type = addCheck port (p: p<=32767);
default = 1500; # official default default = 1500; # official default
description = lib.mdDoc '' description = lib.mdDoc ''
TCP port of the IBM TSM server. TCP port of the IBM TSM server.
The value will be used for the
`tcpport`
directive in {file}`dsm.sys`.
TSM does not support ports above 32767. TSM does not support ports above 32767.
''; '';
}; };
options.node = mkOption { options.nodename = mkOption {
type = nonEmptyStr; type = nonEmptyStr;
example = "MY-TSM-NODE"; example = "MY-TSM-NODE";
description = lib.mdDoc '' description = lib.mdDoc ''
Target node name on the IBM TSM server. Target node name on the IBM TSM server.
The value will be used for the
`nodename`
directive in {file}`dsm.sys`.
''; '';
}; };
options.genPasswd = mkEnableOption (lib.mdDoc '' options.genPasswd = mkEnableOption (lib.mdDoc ''
automatic client password generation. automatic client password generation.
This option influences the This option does *not* cause a line in
`passwordaccess` {file}`dsm.sys` by itself, but generates a
directive in {file}`dsm.sys`. corresponding `passwordaccess` directive.
The password will be stored in the directory The password will be stored in the directory
given by the option {option}`passwdDir`. given by the option {option}`passworddir`.
*Caution*: *Caution*:
If this option is enabled and the server forces If this option is enabled and the server forces
to renew the password (e.g. on first connection), to renew the password (e.g. on first connection),
a random password will be generated and stored a random password will be generated and stored
''); '');
options.passwdDir = mkOption { options.passwordaccess = mkOption {
type = path; type = enum [ "generate" "prompt" ];
visible = false;
};
options.passworddir = mkOption {
type = nullOr path;
default = null;
example = "/home/alice/tsm-password"; example = "/home/alice/tsm-password";
description = lib.mdDoc '' description = lib.mdDoc ''
Directory that holds the TSM Directory that holds the TSM
node's password information. node's password information.
The value will be used for the
`passworddir`
directive in {file}`dsm.sys`.
''; '';
}; };
options.includeExclude = mkOption { options.inclexcl = mkOption {
type = lines; type = coercedTo lines
default = ""; (pkgs.writeText "inclexcl.dsm.sys")
(nullOr path);
default = null;
example = '' example = ''
exclude.dir /nix/store exclude.dir /nix/store
include.encrypt /home/.../* include.encrypt /home/.../*
''; '';
description = lib.mdDoc '' description = lib.mdDoc ''
`include.*` and Text lines with `include.*` and `exclude.*` directives
`exclude.*` directives to be to be used when sending files to the IBM TSM server,
used when sending files to the IBM TSM server. or an absolute path pointing to a file with such lines.
The lines will be written into a file that the
`inclexcl`
directive in {file}`dsm.sys` points to.
''; '';
}; };
options.extraConfig = mkOption { config.commmethod = mkDefault "v6tcpip"; # uses v4 or v6, based on dns lookup result
# TSM option keys are case insensitive; config.passwordaccess = if config.genPasswd then "generate" else "prompt";
# we have to ensure there are no keys that
# differ only by upper and lower case.
type = addCheck
(attrsOf (nullOr str))
(attrs: allUnique (map toLower (attrNames attrs)));
default = {};
example.compression = "yes";
example.passwordaccess = null;
description = lib.mdDoc ''
Additional key-value pairs for the server stanza.
Values must be strings, or `null`
for the key not to be used in the stanza
(e.g. to overrule values generated by other options).
'';
};
options.text = mkOption {
type = lines;
example = literalExpression
''lib.modules.mkAfter "compression no"'';
description = lib.mdDoc ''
Additional text lines for the server stanza.
This option can be used if certion configuration keys
must be used multiple times or ordered in a certain way
as the {option}`extraConfig` option can't
control the order of lines in the resulting stanza.
Note that the `server`
line at the beginning of the stanza is
not part of this option's value.
'';
};
options.stanza = mkOption {
type = str;
internal = true;
visible = false;
description = lib.mdDoc "Server stanza text generated from the options.";
};
config.name = mkDefault name;
# Client system-options file directives are explained here:
# https://www.ibm.com/docs/en/storage-protect/8.1.20?topic=commands-processing-options
config.extraConfig =
mapAttrs (lib.trivial.const mkDefault) (
{
commmethod = "v6tcpip"; # uses v4 or v6, based on dns lookup result
tcpserveraddress = config.server;
tcpport = builtins.toString config.port;
nodename = config.node;
passwordaccess = if config.genPasswd then "generate" else "prompt";
passworddir = ''"${config.passwdDir}"'';
} // optionalAttrs (config.includeExclude!="") {
inclexcl = ''"${pkgs.writeText "inclexcl.dsm.sys" config.includeExclude}"'';
}
);
config.text =
let
attrset = filterAttrs (k: v: v!=null) config.extraConfig;
mkLine = k: v: k + optionalString (v!="") " ${v}";
lines = mapAttrsToList mkLine attrset;
in
concatLines lines;
config.stanza = ''
server ${config.name}
${config.text}
'';
}; };
options.programs.tsmClient = { options.programs.tsmClient = {
@ -171,13 +107,24 @@ let
type = attrsOf (submodule serverOptions); type = attrsOf (submodule serverOptions);
default = {}; default = {};
example.mainTsmServer = { example.mainTsmServer = {
server = "tsmserver.company.com"; tcpserveraddress = "tsmserver.company.com";
node = "MY-TSM-NODE"; nodename = "MY-TSM-NODE";
extraConfig.compression = "yes"; compression = "yes";
}; };
description = lib.mdDoc '' description = lib.mdDoc ''
Server definitions ("stanzas") Server definitions ("stanzas")
for the client system-options file. for the client system-options file.
The name of each entry will be used for
the internal `servername` by default.
Each attribute will be transformed into a line
with a key-value pair within the server's stanza.
Integers as values will be
canonically turned into strings.
The boolean value `true` will be turned
into a line with just the attribute's name.
The value `null` will not generate a line.
A list as values generates an entry for
each value, according to the rules above.
''; '';
}; };
defaultServername = mkOption { defaultServername = mkOption {
@ -225,21 +172,54 @@ let
}; };
cfg = config.programs.tsmClient; cfg = config.programs.tsmClient;
servernames = map (s: s.servername) (attrValues cfg.servers);
assertions = [ assertions =
[
{ {
assertion = allUnique (mapAttrsToList (k: v: toLower v.name) cfg.servers); assertion = allUnique (map toLower servernames);
message = '' message = ''
TSM servernames contain duplicate name TSM server names
(note that case doesn't matter!) (option `programs.tsmClient.servers`)
contain duplicate name
(note that server names are case insensitive).
''; '';
} }
{ {
assertion = (cfg.defaultServername!=null)->(hasAttr cfg.defaultServername cfg.servers); assertion = (cfg.defaultServername!=null)->(elem cfg.defaultServername servernames);
message = "TSM defaultServername not found in list of servers"; message = ''
TSM default server name
`programs.tsmClient.defaultServername="${cfg.defaultServername}"`
not found in server names in
`programs.tsmClient.servers`.
'';
} }
]; ];
makeDsmSysLines = key: value:
# Turn a key-value pair from the server options attrset
# into zero (value==null), one (scalar value) or
# more (value is list) configuration stanza lines.
if isList value then map (makeDsmSysLines key) value else # recurse into list
if value == null then [ ] else # skip `null` value
[ (" ${key}${
if value == true then "" else # just output key if value is `true`
if isInt value then " ${builtins.toString value}" else
if path.check value then " \"${value}\"" else # enclose path in ".."
if singleLineStr.check value then " ${value}" else
throw "assertion failed: cannot convert type" # should never happen
}") ];
makeDsmSysStanza = {servername, ... }@serverCfg:
let
# drop special values that should not go into server config block
attrs = removeAttrs serverCfg [ "servername" "genPasswd" ];
in
''
servername ${servername}
${concatLines (concatLists (mapAttrsToList makeDsmSysLines attrs))}
'';
dsmSysText = '' dsmSysText = ''
**** IBM Storage Protect (Tivoli Storage Manager) **** IBM Storage Protect (Tivoli Storage Manager)
**** client system-options file "dsm.sys". **** client system-options file "dsm.sys".
@ -248,7 +228,7 @@ let
${optionalString (cfg.defaultServername!=null) "defaultserver ${cfg.defaultServername}"} ${optionalString (cfg.defaultServername!=null) "defaultserver ${cfg.defaultServername}"}
${concatLines (mapAttrsToList (k: v: v.stanza) cfg.servers)} ${concatLines (map makeDsmSysStanza (attrValues cfg.servers))}
''; '';
in in

View File

@ -79,7 +79,7 @@ in
config = mkIf cfg.enable { config = mkIf cfg.enable {
inherit assertions; inherit assertions;
programs.tsmClient.enable = true; programs.tsmClient.enable = true;
programs.tsmClient.servers.${cfg.servername}.passwdDir = programs.tsmClient.servers.${cfg.servername}.passworddir =
mkDefault "/var/lib/tsm-backup/password"; mkDefault "/var/lib/tsm-backup/password";
systemd.services.tsm-backup = { systemd.services.tsm-backup = {
description = "IBM Storage Protect (Tivoli Storage Manager) Backup"; description = "IBM Storage Protect (Tivoli Storage Manager) Backup";

View File

@ -18,9 +18,9 @@ import ./make-test-python.nix ({ lib, pkgs, ... }: {
defaultServername = "testserver"; defaultServername = "testserver";
servers.testserver = { servers.testserver = {
# 192.0.0.8 is a "dummy address" according to RFC 7600 # 192.0.0.8 is a "dummy address" according to RFC 7600
server = "192.0.0.8"; tcpserveraddress = "192.0.0.8";
node = "SOME-NODE"; nodename = "SOME-NODE";
passwdDir = "/tmp"; passworddir = "/tmp";
}; };
}; };
}; };