FOGProject / fogproject

An open source computer cloning & management system
https://fogproject.org
GNU General Public License v3.0
1.12k stars 223 forks source link

Can't register new hosts via pxe or api in working-1.6 #548

Closed darksidemilk closed 3 months ago

darksidemilk commented 1 year ago

Describe the bug

When attempting to add a new host to fog in 1.6 either through the api or pxe full host registration it fails

To Reproduce

Steps to reproduce the behavior for pxe:

  1. Install or upgrade to fog 1.6
  2. pxe boot to it with an unregistered host (i.e a new vm)
  3. Attempt to do a full host registration and inventory and see the error that your hostname is invalid but this host name displayed isn't what you typed but rather a random string

Steps to reproduce the behavior for pxe:

  1. Install or upgrade to fog 1.6
  2. Submit an api call to create a new host
  3. Get a 500 error in return instead of a success message

Example using fogapi powershell module

install-module fogapi
import-module fogapi
set-fogserversettings -fogapitoken 'server-api-token-here' -fogusertoken 'your-fog-user-token-here' -fogserver 'your-fog-server-hostname-here';
Add-FogHost -name test-host -macs "01:02:03:04:05:06" 

Expected behavior

The host should have been added or registered successfully

Screenshots If applicable, add screenshots to help explain your problem.

Error on api call image

Error on full pxe registration

image

Software (please complete the following information):

Additional context Add any other context about the problem here.

It seems to be related to a call to the ishostnamesafe function in the host class.

public function isHostnameSafe($hostname = '')
    {
        if (empty($hostname)) {
            $hostname = $this->get('name');
        }
        $pattern = "/^[\w!@#$%^()\-'{}\.~]{1,15}$/";
        return (bool)preg_match($pattern, $hostname);
    }

if I change this function so it always returns true, then it gets past the error in the pxe host registration

public function isHostnameSafe($hostname = '')
    {
        return true;
        //if (empty($hostname)) {
           // $hostname = $this->get('name');
        //}
        //$pattern = "/^[\w!@#$%^()\-'{}\.~]{1,15}$/";
        //return (bool)preg_match($pattern, $hostname);
    }

But of course, that's not what we want, we want this check to exist. Also, it does something else weird, it gives it a different name than what I input. i.e. I input dr-wks-jjv5 as the hostname in the pxe regsitration with the ishostnamesafe set to always return true. It looks fine in the pxe menu but then the hostname that gets registered is ZHItd2tzLWpqdjUK

image

So something is happening with the input between the registration class fullreg call and when the host is registered.

Changing the return to always true doesn't get past the problem in the api call, so something else is happening with that.

darksidemilk commented 11 months ago

This commit https://github.com/FOGProject/fogproject/commit/b8e076d373e352131176a5c5f39d9b5b48c3666b appears to have fix pxe registration.

However, in for 1.5.X we can create a fog host via the api with a json like

{
  "name": "someName",
  "macs": [
    "1A:2B:3C:4D:5E:6F"
  ],
  "modules": [
    "4",
    "7",
    "1",
    "3",
    "5",
    "8",
    "9",
    "13",
    "10",
    "6",
    "11",
    "2",
    "12"
  ]
}

Which is the json format created by the fogApi powershell module function New-FogHost

In working-1.6 this json produces a 500 error

However, if you remove the macs array property from the json, it gives an { "error": "Already created" } in the response. But, it actually does create the host

image

If you hover over that host with no mac (it won't open on click) you can view the url that includes the host id, or using the fogApi ps module you can find the host id in the full list of hosts with get-foghosts | ? name -eq 'someName' | select id

Then you can use the api to add the mac to the host as a primary mac Add-FogHostMac -hostID (get-foghosts | ? name -eq 'someName').id -primary -macAddress "1A:2B:3C:4D:5E:6F" -vb

This function will given an error saying 404 not found at one point because it tries to get the host record before adding the mac, until the mac is added you can't get the record of the host, only view it in the full list of hosts, same as in the UI. But this does add the mac via the api and image

However, if you were to add another mac address to that host and mark it as primary too, i.e. Add-FogHostMac -hostID (get-foghosts | ? name -eq 'someName').id -primary -macAddress "1A:2B:3C:4D:5E:6H" it creates 2 entries for that host in the hosts view image

If you click into either it goes to the same place and lists the more recently set as primary mac as the primary mac image

If you click the 'Primary Mac' selection in the UI, it fixes the duplicated display. The duplicated display doesn't happen if you add a non-primary mac via the api

A bit hinky, but hopefully that workaround might help @mastacontrola with debugging what's going on.

Short version

Neustradamus commented 11 months ago

To follow

darksidemilk commented 3 months ago

This has been resolved. I don't recall exactly which commit, it may have been this one https://github.com/FOGProject/fogproject/commit/b8e076d373e352131176a5c5f39d9b5b48c3666b

But as of the current version for sure (https://github.com/FOGProject/fogproject/commit/e6a9f582e7180441338765ebab9e6b5edbca81e4) this is resolved.