Skip to content

Refactor host validation and save logic#785

Open
eduardomozart wants to merge 1 commit intoFOGProject:dev-branchfrom
eduardomozart:patch-2
Open

Refactor host validation and save logic#785
eduardomozart wants to merge 1 commit intoFOGProject:dev-branchfrom
eduardomozart:patch-2

Conversation

@eduardomozart
Copy link

This PR fixes an issue where FOG Client automatic host registration fails silently when creating a new pending host (Closes #779).

The failure happens because the database schema defines hosts.hostImage as NOT NULL, but the auto-registration code attempts to insert NULL when no image is assigned. As a result, the INSERT ... ON DUPLICATE KEY UPDATE statement is rejected by MySQL, causing Host::save() to return without throwing an exception.

Root Cause

  • During auto-registration, hosts are created in a pending state.
  • No image is assigned at this stage.
  • ORM generates:
hostImage = NULL
  • Database rejects the insert:
INSERT INTO `hosts` (`hostName`,`hostDesc`,`hostIP`,`hostImage`,`hostBuilding`,`hostCreateDate`,`hostLastDeploy`,`hostCreateBy`,`hostUseAD`,`hostADDomain`,`hostADOU`,`hostADUser`,`hostADPass`,`hostADPassLegacy`,`hostProductKey`,`hostPrinterLevel`,`hostKernelArgs`,`hostKernel`,`hostDevice`,`hostInit`,`hostPending`,`hostPubKey`,`hostSecToken`,`hostSecTime`,`hostPingCode`,`hostExitBios`,`hostExitEfi`,`hostEnforce`,`hostInfoKey`,`hostInfoLock`) VALUES ('EXAMPLE-18','Pending Registration created by FOG_CLIENT','',NULL,'','2026-02-12 22:36:53','','fog','','','','','','','','','','','','','1','','','','','','','1','','') ON DUPLICATE KEY UPDATE `hostName`=VALUES(`hostName`),`hostDesc`=VALUES(`hostDesc`),`hostIP`=VALUES(`hostIP`),`hostImage`=VALUES(`hostImage`),`hostBuilding`=VALUES(`hostBuilding`),`hostCreateDate`=VALUES(`hostCreateDate`),`hostLastDeploy`=VALUES(`hostLastDeploy`),`hostCreateBy`=VALUES(`hostCreateBy`),`hostUseAD`=VALUES(`hostUseAD`),`hostADDomain`=VALUES(`hostADDomain`),`hostADOU`=VALUES(`hostADOU`),`hostADUser`=VALUES(`hostADUser`),`hostADPass`=VALUES(`hostADPass`),`hostADPassLegacy`=VALUES(`hostADPassLegacy`),`hostProductKey`=VALUES(`hostProductKey`),`hostPrinterLevel`=VALUES(`hostPrinterLevel`),`hostKernelArgs`=VALUES(`hostKernelArgs`),`hostKernel`=VALUES(`hostKernel`),`hostDevice`=VALUES(`hostDevice`),`hostInit`=VALUES(`hostInit`),`hostPending`=VALUES(`hostPending`),`hostPubKey`=VALUES(`hostPubKey`),`hostSecToken`=VALUES(`hostSecToken`),`hostSecTime`=VALUES(`hostSecTime`),`hostPingCode`=VALUES(`hostPingCode`),`hostExitBios`=VALUES(`hostExitBios`),`hostExitEfi`=VALUES(`hostExitEfi`),`hostEnforce`=VALUES(`hostEnforce`),`hostInfoKey`=VALUES(`hostInfoKey`),`hostInfoLock`=VALUES(`hostInfoLock`);
ERROR 1048 (23000): Column 'hostImage' cannot be null
  • The failure is silent, and the host is never created, even though MACs may still be registered.

Solution

This PR explicitly sets the host image to 0 (No Image) during automatic host creation:

->set('imageID', 0)

This issue can be difficult to diagnose because it fails without throwing an exception. Adding the explicit imageID = 0 ensures predictable behavior and aligns auto-registration with existing FOG semantics, where 0 represents //no assigned image//, and fully satisfies the database constraint.

@darksidemilk
Copy link
Member

Firstly, thank you for contributing! It is greatly appreciated!
We haven't had the time to test and check this thoroughly yet.
My first thought though is that we don't want to set image Id to 0 as we've had historical issues with various ids being set to 0 and we're trying to avoid that as it can cause issues in later workflows if it doesn't get updated. We probably added a null check for creating a host and this entry point of creating a pending host probably slipped through the cracks. I don't know when I'll have time to dig in and really I would be asking @mastacontrola for help on this one. But we see it, we appreciate it, and we'll work it out.

@eduardomozart
Copy link
Author

@darksidemilk thank you for your comments and for reviewing this PR. Actually, when creating a new host manually through Web UI without selecting an image, it sets the hosts.hostImage column on DB to 0, so if this is not desired, I think that the DB Schema should be updated to allow NULL values on this column and be updated as such.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments