Merge pull request #246354 from minijackson/netbox-fixes

netbox: 3.5.6 -> 3.5.7 + migration fixes + upgrade NixOS test
This commit is contained in:
Benjamin Staffin 2023-08-21 16:22:38 -04:00 committed by GitHub
commit 63e9b2f8c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 142 additions and 53 deletions

View file

@ -1,7 +1,5 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
with lib;
let let
cfg = config.services.netbox; cfg = config.services.netbox;
pythonFmt = pkgs.formats.pythonVars {}; pythonFmt = pkgs.formats.pythonVars {};
@ -17,7 +15,7 @@ let
pkg = (cfg.package.overrideAttrs (old: { pkg = (cfg.package.overrideAttrs (old: {
installPhase = old.installPhase + '' installPhase = old.installPhase + ''
ln -s ${configFile} $out/opt/netbox/netbox/netbox/configuration.py ln -s ${configFile} $out/opt/netbox/netbox/netbox/configuration.py
'' + optionalString cfg.enableLdap '' '' + lib.optionalString cfg.enableLdap ''
ln -s ${cfg.ldapConfigPath} $out/opt/netbox/netbox/netbox/ldap_config.py ln -s ${cfg.ldapConfigPath} $out/opt/netbox/netbox/netbox/ldap_config.py
''; '';
})).override { })).override {
@ -31,7 +29,7 @@ let
in { in {
options.services.netbox = { options.services.netbox = {
enable = mkOption { enable = lib.mkOption {
type = lib.types.bool; type = lib.types.bool;
default = false; default = false;
description = lib.mdDoc '' description = lib.mdDoc ''
@ -66,18 +64,18 @@ in {
}; };
}; };
listenAddress = mkOption { listenAddress = lib.mkOption {
type = types.str; type = lib.types.str;
default = "[::1]"; default = "[::1]";
description = lib.mdDoc '' description = lib.mdDoc ''
Address the server will listen on. Address the server will listen on.
''; '';
}; };
package = mkOption { package = lib.mkOption {
type = types.package; type = lib.types.package;
default = if versionAtLeast config.system.stateVersion "23.05" then pkgs.netbox else pkgs.netbox_3_3; default = if lib.versionAtLeast config.system.stateVersion "23.05" then pkgs.netbox else pkgs.netbox_3_3;
defaultText = literalExpression '' defaultText = lib.literalExpression ''
if versionAtLeast config.system.stateVersion "23.05" then pkgs.netbox else pkgs.netbox_3_3; if versionAtLeast config.system.stateVersion "23.05" then pkgs.netbox else pkgs.netbox_3_3;
''; '';
description = lib.mdDoc '' description = lib.mdDoc ''
@ -85,18 +83,18 @@ in {
''; '';
}; };
port = mkOption { port = lib.mkOption {
type = types.port; type = lib.types.port;
default = 8001; default = 8001;
description = lib.mdDoc '' description = lib.mdDoc ''
Port the server will listen on. Port the server will listen on.
''; '';
}; };
plugins = mkOption { plugins = lib.mkOption {
type = types.functionTo (types.listOf types.package); type = with lib.types; functionTo (listOf package);
default = _: []; default = _: [];
defaultText = literalExpression '' defaultText = lib.literalExpression ''
python3Packages: with python3Packages; []; python3Packages: with python3Packages; [];
''; '';
description = lib.mdDoc '' description = lib.mdDoc ''
@ -104,23 +102,23 @@ in {
''; '';
}; };
dataDir = mkOption { dataDir = lib.mkOption {
type = types.str; type = lib.types.str;
default = "/var/lib/netbox"; default = "/var/lib/netbox";
description = lib.mdDoc '' description = lib.mdDoc ''
Storage path of netbox. Storage path of netbox.
''; '';
}; };
secretKeyFile = mkOption { secretKeyFile = lib.mkOption {
type = types.path; type = lib.types.path;
description = lib.mdDoc '' description = lib.mdDoc ''
Path to a file containing the secret key. Path to a file containing the secret key.
''; '';
}; };
extraConfig = mkOption { extraConfig = lib.mkOption {
type = types.lines; type = lib.types.lines;
default = ""; default = "";
description = lib.mdDoc '' description = lib.mdDoc ''
Additional lines of configuration appended to the `configuration.py`. Additional lines of configuration appended to the `configuration.py`.
@ -128,8 +126,8 @@ in {
''; '';
}; };
enableLdap = mkOption { enableLdap = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = false; default = false;
description = lib.mdDoc '' description = lib.mdDoc ''
Enable LDAP-Authentication for Netbox. Enable LDAP-Authentication for Netbox.
@ -138,8 +136,8 @@ in {
''; '';
}; };
ldapConfigPath = mkOption { ldapConfigPath = lib.mkOption {
type = types.path; type = lib.types.path;
default = ""; default = "";
description = lib.mdDoc '' description = lib.mdDoc ''
Path to the Configuration-File for LDAP-Authentication, will be loaded as `ldap_config.py`. Path to the Configuration-File for LDAP-Authentication, will be loaded as `ldap_config.py`.
@ -173,15 +171,17 @@ in {
}; };
}; };
config = mkIf cfg.enable { config = lib.mkIf cfg.enable {
services.netbox = { services.netbox = {
plugins = mkIf cfg.enableLdap (ps: [ ps.django-auth-ldap ]); plugins = lib.mkIf cfg.enableLdap (ps: [ ps.django-auth-ldap ]);
settings = { settings = {
STATIC_ROOT = staticDir; STATIC_ROOT = staticDir;
MEDIA_ROOT = "${cfg.dataDir}/media"; MEDIA_ROOT = "${cfg.dataDir}/media";
REPORTS_ROOT = "${cfg.dataDir}/reports"; REPORTS_ROOT = "${cfg.dataDir}/reports";
SCRIPTS_ROOT = "${cfg.dataDir}/scripts"; SCRIPTS_ROOT = "${cfg.dataDir}/scripts";
GIT_PATH = "${pkgs.gitMinimal}/bin/git";
DATABASE = { DATABASE = {
NAME = "netbox"; NAME = "netbox";
USER = "netbox"; USER = "netbox";
@ -264,39 +264,39 @@ in {
RestartSec = 30; RestartSec = 30;
}; };
in { in {
netbox-migration = {
description = "NetBox migrations";
wantedBy = [ "netbox.target" ];
environment = {
PYTHONPATH = pkg.pythonPath;
};
serviceConfig = defaultServiceConfig // {
Type = "oneshot";
ExecStart = ''
${pkg}/bin/netbox migrate
'';
PrivateTmp = true;
};
};
netbox = { netbox = {
description = "NetBox WSGI Service"; description = "NetBox WSGI Service";
documentation = [ "https://docs.netbox.dev/" ]; documentation = [ "https://docs.netbox.dev/" ];
wantedBy = [ "netbox.target" ]; wantedBy = [ "netbox.target" ];
after = [ "network-online.target" "netbox-migration.service" ]; after = [ "network-online.target" ];
wants = [ "network-online.target" ]; wants = [ "network-online.target" ];
environment.PYTHONPATH = pkg.pythonPath;
preStart = '' preStart = ''
# On the first run, or on upgrade / downgrade, run migrations and related.
# This mostly correspond to upstream NetBox's 'upgrade.sh' script.
versionFile="${cfg.dataDir}/version"
if [[ -e "$versionFile" && "$(cat "$versionFile")" == "${cfg.package.version}" ]]; then
exit 0
fi
${pkg}/bin/netbox migrate
${pkg}/bin/netbox trace_paths --no-input ${pkg}/bin/netbox trace_paths --no-input
${pkg}/bin/netbox collectstatic --no-input ${pkg}/bin/netbox collectstatic --no-input
${pkg}/bin/netbox remove_stale_contenttypes --no-input ${pkg}/bin/netbox remove_stale_contenttypes --no-input
''; # TODO: remove the condition when we remove netbox_3_3
${lib.optionalString
(lib.versionAtLeast cfg.package.version "3.5.0")
"${pkg}/bin/netbox reindex --lazy"}
${pkg}/bin/netbox clearsessions
${pkg}/bin/netbox clearcache
environment.PYTHONPATH = pkg.pythonPath; echo "${cfg.package.version}" > "$versionFile"
'';
serviceConfig = defaultServiceConfig // { serviceConfig = defaultServiceConfig // {
ExecStart = '' ExecStart = ''
@ -331,7 +331,7 @@ in {
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
after = [ "network-online.target" ]; after = [ "network-online.target" "netbox.service" ];
wants = [ "network-online.target" ]; wants = [ "network-online.target" ];
environment.PYTHONPATH = pkg.pythonPath; environment.PYTHONPATH = pkg.pythonPath;
@ -351,7 +351,7 @@ in {
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
after = [ "network-online.target" ]; after = [ "network-online.target" "netbox.service" ];
wants = [ "network-online.target" ]; wants = [ "network-online.target" ];
timerConfig = { timerConfig = {

View file

@ -525,6 +525,7 @@ in {
networking.scripted = handleTest ./networking.nix { networkd = false; }; networking.scripted = handleTest ./networking.nix { networkd = false; };
netbox = handleTest ./web-apps/netbox.nix { inherit (pkgs) netbox; }; netbox = handleTest ./web-apps/netbox.nix { inherit (pkgs) netbox; };
netbox_3_3 = handleTest ./web-apps/netbox.nix { netbox = pkgs.netbox_3_3; }; netbox_3_3 = handleTest ./web-apps/netbox.nix { netbox = pkgs.netbox_3_3; };
netbox-upgrade = handleTest ./web-apps/netbox-upgrade.nix {};
# TODO: put in networking.nix after the test becomes more complete # TODO: put in networking.nix after the test becomes more complete
networkingProxy = handleTest ./networking-proxy.nix {}; networkingProxy = handleTest ./networking-proxy.nix {};
nextcloud = handleTest ./nextcloud {}; nextcloud = handleTest ./nextcloud {};

View file

@ -0,0 +1,85 @@
import ../make-test-python.nix ({ lib, pkgs, ... }: let
oldNetbox = pkgs.netbox_3_3;
in {
name = "netbox-upgrade";
meta = with lib.maintainers; {
maintainers = [ minijackson ];
};
nodes.machine = { config, ... }: {
services.netbox = {
enable = true;
package = oldNetbox;
secretKeyFile = pkgs.writeText "secret" ''
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
'';
};
services.nginx = {
enable = true;
recommendedProxySettings = true;
virtualHosts.netbox = {
default = true;
locations."/".proxyPass = "http://localhost:${toString config.services.netbox.port}";
locations."/static/".alias = "/var/lib/netbox/static/";
};
};
users.users.nginx.extraGroups = [ "netbox" ];
networking.firewall.allowedTCPPorts = [ 80 ];
specialisation.upgrade.configuration.services.netbox.package = lib.mkForce pkgs.netbox;
};
testScript = { nodes, ... }:
let
apiVersion = version: lib.pipe version [
(lib.splitString ".")
(lib.take 2)
(lib.concatStringsSep ".")
];
oldApiVersion = apiVersion oldNetbox.version;
newApiVersion = apiVersion pkgs.netbox.version;
in
''
start_all()
machine.wait_for_unit("netbox.target")
machine.wait_for_unit("nginx.service")
machine.wait_until_succeeds("journalctl --since -1m --unit netbox --grep Listening")
def api_version(headers):
header = [header for header in headers.splitlines() if header.startswith("API-Version:")][0]
return header.split()[1]
def check_api_version(version):
headers = machine.succeed(
"curl -sSfL http://localhost/api/ --head -H 'Content-Type: application/json'"
)
assert api_version(headers) == version
with subtest("NetBox version is the old one"):
check_api_version("${oldApiVersion}")
# Somehow, even though netbox-housekeeping.service has After=netbox.service,
# netbox-housekeeping.service and netbox.service still get started at the
# same time, making netbox-housekeeping fail (can't really do some house
# keeping job if the database is not correctly formed).
#
# So we don't check that the upgrade went well, we just check that
# netbox.service is active, and that netbox-housekeeping can be run
# successfully afterwards.
#
# This is not good UX, but the system should be working nonetheless.
machine.execute("${nodes.machine.system.build.toplevel}/specialisation/upgrade/bin/switch-to-configuration test >&2")
machine.wait_for_unit("netbox.service")
machine.succeed("systemctl start netbox-housekeeping.service")
with subtest("NetBox version is the new one"):
check_api_version("${newApiVersion}")
'';
})

View file

@ -17,20 +17,23 @@ in
}) })
]; ];
tests.netbox = nixosTests.netbox_3_3; tests = {
netbox = nixosTests.netbox_3_3;
inherit (nixosTests) netbox-upgrade;
};
maintainers = with lib.maintainers; [ n0emis raitobezarius ]; maintainers = with lib.maintainers; [ n0emis raitobezarius ];
eol = true; eol = true;
}; };
netbox = callPackage generic { netbox = callPackage generic {
version = "3.5.6"; version = "3.5.7";
hash = "sha256-n5EJQcC5uVoL5KjGzF7bLF8c4Wke/YBJpx2V9KZz5Qo="; hash = "sha256-R5P4FOhn7rE6e9H9U3JlE3ms4Svv6ps4c3+ZjE1KwNM=";
extraPatches = [ extraPatches = [
# Allow setting the STATIC_ROOT from within the configuration and setting a custom redis URL # Allow setting the STATIC_ROOT from within the configuration and setting a custom redis URL
./config.patch ./config.patch
]; ];
tests = { tests = {
inherit (nixosTests) netbox; inherit (nixosTests) netbox netbox-upgrade;
}; };
maintainers = with lib.maintainers; [ minijackson n0emis raitobezarius ]; maintainers = with lib.maintainers; [ minijackson n0emis raitobezarius ];