Repository: cobbler/cobbler

Issue:
Start cobblerd service error in cobbler 3.4.0-1
### Describe the bug

I was installed cobbler-3.4.0-1.fc37.noarch.rpm and start cobbled , start cobblerd service error.

### Steps to reproduce

1. `dnf localinstall cobbler-3.4.0-1.fc37.noarch.rpm -y`
2. `dnf install -y python3-hivex`
3. `systemctl status cobbler`

### Expected behavior
````
[root@fedora104 cobbler]# systemctl status cobblerd
× cobblerd.service - Cobbler Helper Daemon
     Loaded: loaded (/usr/lib/systemd/system/cobblerd.service; disabled; preset: disabled)
     Active: failed (Result: exit-code) since Thu 2023-06-01 08:22:20 EDT; 5s ago
   Duration: 1.167s
    Process: 6695 ExecStart=/usr/bin/cobblerd -F (code=exited, status=1/FAILURE)
   Main PID: 6695 (code=exited, status=1/FAILURE)
        CPU: 1.069s

Jun 01 08:22:20 fedora104 cobblerd[6695]:     api = cobbler_api.CobblerAPI(
Jun 01 08:22:20 fedora104 cobblerd[6695]:           ^^^^^^^^^^^^^^^^^^^^^^^
Jun 01 08:22:20 fedora104 cobblerd[6695]:   File "/usr/lib/python3.11/site-packages/cobbler/api.py", line 268, in __init__
Jun 01 08:22:20 fedora104 cobblerd[6695]:     self.__directory_startup_preparations()
Jun 01 08:22:20 fedora104 cobblerd[6695]:   File "/usr/lib/python3.11/site-packages/cobbler/api.py", line 288, in __directory_startup_preparations
Jun 01 08:22:20 fedora104 cobblerd[6695]:     raise FileNotFoundError(
Jun 01 08:22:20 fedora104 cobblerd[6695]: FileNotFoundError: Required directory "/var/www/cobbler" for operation is missing! Aborting startup of Cobbler!
Jun 01 08:22:20 fedora104 systemd[1]: cobblerd.service: Main process exited, code=exited, status=1/FAILURE
Jun 01 08:22:20 fedora104 systemd[1]: cobblerd.service: Failed with result 'exit-code'.
Jun 01 08:22:20 fedora104 systemd[1]: cobblerd.service: Consumed 1.069s CPU time.

````

### Cobbler version

<!--- Paste output from `cobbler version` -->
````paste below
````

### Operating system

Fedora Linux 37

### Cobbler log

<!--- Paste (partial) output from `/var/log/cobbler/cobbler.log` -->
````
[root@fedora104 cobbler]# cat /var/log/cobbler/cobbler.log
[Daemon] 2023-06-01T08:25:00 - INFO | running: ['/usr/sbin/selinuxenabled']
[Daemon] 2023-06-01T08:25:00 - INFO | received on stdout: 
[Daemon] 2023-06-01T08:25:00 - INFO | Automigration NOT executed
[Daemon] 2023-06-01T08:25:01 - INFO | python3-hivex not found. If you need Automatic Windows Installation support, please install.
[Daemon] 2023-06-01T08:25:01 - INFO | 11 breeds and 157 OS versions read from the signature file

````

### Screenshots



### Additional information





Discussion:
@xuezhixin This directory should exist. I don't see this error with the openSUSE-based test container. I see that you are running Fedora. Could you tell me how you built the RPM? In our CI we also build the RPM for Fedora and that works when starting the daemon.
I just use the 3.4.0 spec file to rpm-build in fedora37

````
[root@fedora104 rpm]# ls *
BUILD:

BUILDROOT:

RPMS:
noarch

SOURCES:
cobbler-3.4.0  cobbler-3.4.0.tar.gz

SPECS:
cobbler.spec

SRPMS:
cobbler-3.4.0-1.fc37.src.rpm

[root@fedora104 rpm]# rpmbuild -ba -D "_topdir `pwd`" SPECS/*.spec
````
@xuezhixin I will try to see if I can replicate the problem and if the linked PR is the correct fix for the problem.
Could SELinux restrict cobbler from creating the directory? Try it with SELinux off or execute `sudo /usr/bin/cobblerd -F` If this command executes properly, it is likely that SELinux is interfering.
This should not be caused by SELinux, because SELinux has always been a disabled state.
```
[root@fedora104 cobbler]# sudo /usr/bin/cobblerd -F
INFO | running: ['/usr/sbin/selinuxenabled']
INFO | received on stdout: 
INFO | Automigration NOT executed
INFO | python3-hivex not found. If you need Automatic Windows Installation support, please install.
INFO | 11 breeds and 157 OS versions read from the signature file
ERROR | Error while loading a collection: initrd not found: /var/www/cobbler/distro_mirror/ubuntu-22-04-x86_64/casper/initrd. Skipping collection distro!
ERROR | Error while loading a collection: distribution "ubuntu-22-04-casper-x86_64" not found. Skipping collection profile!
Traceback (most recent call last):
  File "/usr/bin/cobblerd", line 108, in main
    api = cobbler_api.CobblerAPI(
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/cobbler/api.py", line 268, in __init__
    self.__directory_startup_preparations()
  File "/usr/lib/python3.11/site-packages/cobbler/api.py", line 290, in __directory_startup_preparations
    raise FileNotFoundError(
FileNotFoundError: Required directory "/var/www/cobbler" for operation is missing! Aborting startup of Cobbler!
[root@fedora104 cobbler]# getenforce 
Disabled
```

Resolving patch:
diff --git a/cobbler/api.py b/cobbler/api.py
--- a/cobbler/api.py
+++ b/cobbler/api.py
@@ -278,16 +278,15 @@ def __directory_startup_preparations(self) -> None:
         """
         self.logger.debug("Creating necessary directories")
         required_directories = [
-            "/var/lib/cobbler",
-            "/etc/cobbler",
-            self.settings().webdir,
-            self.settings().tftpboot_location,
+            pathlib.Path("/var/lib/cobbler"),
+            pathlib.Path("/etc/cobbler"),
+            pathlib.Path(self.settings().webdir),
+            pathlib.Path(self.settings().tftpboot_location),
         ]
         for directory in required_directories:
-            if not pathlib.Path(directory).is_dir():
-                raise FileNotFoundError(
-                    f'Required directory "{directory}" for operation is missing! Aborting startup of Cobbler!'
-                )
+            if not directory.is_dir():
+                directory.mkdir()
+                self.logger.info('Created required directory: "%s"', str(directory))
         filesystem_helpers.create_tftpboot_dirs(self)
         filesystem_helpers.create_web_dirs(self)
         filesystem_helpers.create_trigger_dirs(self)


Test patch: