Fixes #37602 - Fix broken host/hostgroup media association
Hostgroup with assigned Installation Media is used. A host will be registered using HostRegistartion. During this process, the Hostgroup was used and is assigned to the host. The host is using the subscription-manager to register. This will create a new OS in foreman if the OS doesn't currently exist. This new OS doesn’t have the installation media assigned (because its a new OS).
Results:
- The new created Operatingsystem like AlmaLinux9 doesn't have a Installation Media.
- The used Hostgroup has different Operatingsystems and is using a Installation Media which is valid for the Operatingsystem is used in this Hostgroup.
The POST request during the HostRegistration process which assigns the Hostgroup to the Host and should render the host_init_config Template will fail with:
The system has been registered with ID: 21312a8f-1b6e-4302-ae9e-1a64b643c6f8 The registered system name is: myra-bergh.foreman.local ERROR: Validation failed: Medium must belong to host's operating system Notice:
This commit makes sure that the installation medium and some other managed-host-related fields are only inherited for managed hosts.
Having said all of that, if you use global registration isn't it supposed to create an unmanaged host? I thought that's what this line is for:
Well, the host is unmanaged - even in a katello environment:
irb(main):002:0> Host.last.managed?
=> false
But, this check https://github.com/theforeman/foreman/blob/e1ed018cf40926f30decd0bf086b7b22220d356b/app/models/host/managed.rb#L325 doesn't check if managed or unmanaged. (this is what I tried to adapt in the previous PR)
The medium_id is inherited from hostgroup no matter if its managed or unmanaged and then it "crashes" because of the referenced medium_id check.
This PR does solve this issue so that medium_id is not used from hostgroup. If you want to convert the host from unmanaged to managed and you want to have medium, you need to a) add the medium to the operatingsystem which is assigned to the host and then select this medium for the host b) use a different operatingsystem which has a valid medium selection.
This issue happens always during HostRegistartion if a Hostgroup is used, which is configured with a medium but the Operatingsystem was created from the subscription-manager facts. Like, if you have configured a Hostgroup "AlmaLinux 8" and a Operatingsystem "AlmaLinux 8" with Medium "AlmaLinux8" but in the end, the subscription-manager auto-generates a Operatingsystem AlmaLinux 8.10 - boom. Just image, if a customer want to use one Hostgroup with all the details for "AlmaLinux 8" for host, which are newly provisioned and for host, which are attached (= HostRegistartion). This can not work currently.
@ekohl Any more changes required here?
Actually, this https://github.com/theforeman/foreman/pull/10246/files#diff-b7f82bd4fe3d5a5a8b246f502ab1e894197164cf7bf48fbfd5c2f3d76da2362eR564 already prevents that it reaches the section https://github.com/theforeman/foreman/pull/10246/files#diff-b7f82bd4fe3d5a5a8b246f502ab1e894197164cf7bf48fbfd5c2f3d76da2362eR570 which is necessary during host registration for a unmanaged host in the mentioned scenario.
@ekohl Would do you prefer? a) have it as it is right now which does maybe fix some other use-cases. b) Remove the managed? check https://github.com/theforeman/foreman/pull/10246/files#diff-b7f82bd4fe3d5a5a8b246f502ab1e894197164cf7bf48fbfd5c2f3d76da2362eR564 c) Remove the special treatment in https://github.com/theforeman/foreman/pull/10246/files#diff-b7f82bd4fe3d5a5a8b246f502ab1e894197164cf7bf48fbfd5c2f3d76da2362eR570
All 3 options would solve the issue.
How should we proceed with this?
Ping @nofaralfasi - how do we want to continue here?
Ping @nofaralfasi - how do we want to continue here?
Sorry, I didn't get to it this week, and I'm on PTO for the rest of the week. I'll handle it next week.
@nofaralfasi friendly reminder :)
Apologies for the delay. I’m having an issue with my local environment where the facts aren’t being reported back, so I’m unable to reproduce this problem. I’ll resume work on this as soon as I resolve the environment issue.
ping @nofaralfasi How can we proceed with this? (maybe merge just after branching and then see if something is broken?)
ping @nofaralfasi
Does nobody want to merge this? Just FYI: We are using this downstream since at least 2 version without user complains.
IMHO, if nobody objects, we can go ahead and merge it as there are two approvals already @sbernhard
A nice friday after the release of foreman 3.16 to get this merged @stejskalleos :-)
Thanks @sbernhard and everyone involved!