Azure / nxtools

Azure Automanage Machine Configuration Linux DSC resources
Other
13 stars 11 forks source link

nxService - Incorrectly reporting that specific service is not a native service, crashes instead of handling error gracefully #31

Closed eehret closed 1 year ago

eehret commented 1 year ago

Details of the scenario you tried and the problem that is occurring

I have a RHEL virtual machine on which I'm attempting to ensure that the 'rhnsd' service is stopped. The service does exist on the target VM, but the nxService component is erroneously reporting that it does not and crashes. I believe this problem is similar but distinct from the one I reported in https://github.com/Azure/nxtools/issues/30 , which is why I've opened a separate issue.

Verbose logs showing the problem

Cannot bind parameter 'Exception'. Cannot convert the "rhnsd.service is not a native service, redirecting to /sbin/chkconfig." value of type "System.Management.Automation.ErrorRecord" to type "System.Exception".
The PowerShell DSC resource with className:nxService moduleName:nxtools moduleVersion:1.2.0 threw an exception: System.Management.Automation.ParameterBindingException: Cannot bind parameter 'Exception'. Cannot convert the "rhnsd.service is not a native service, redirecting to /sbin/chkconfig." value of type "System.Management.Automation.ErrorRecord" to type "System.Exception".
 ---> System.Management.Automation.PSInvalidCastException: Cannot convert the "rhnsd.service is not a native service, redirecting to /sbin/chkconfig." value of type "System.Management.Automation.ErrorRecord" to type "System.Exception".
   at System.Management.Automation.LanguagePrimitives.ThrowInvalidCastException(Object valueToConvert, Type resultType)
   at System.Management.Automation.LanguagePrimitives.ConvertNoConversion(Object valueToConvert, Type resultType, Boolean recurse, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable)
   at System.Management.Automation.LanguagePrimitives.ConversionData`1.Invoke(Object valueToConvert, Type resultType, Boolean recurse, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable)
   at System.Management.Automation.LanguagePrimitives.ConvertTo(Object valueToConvert, Type resultType, Boolean recursion, IFormatProvider formatProvider, TypeTable backupTypeTable)
   at System.Management.Automation.LanguagePrimitives.ConvertTo(Object valueToConvert, Type resultType, IFormatProvider formatProvider)
   at System.Management.Automation.ParameterBinderBase.CoerceTypeAsNeeded(CommandParameterInternal argument, String parameterName, Type toType, ParameterCollectionTypeInformation collectionTypeInfo, Object currentValue)
   --- End of inner exception stack trace ---
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
   at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(ScriptBlockClauseToInvoke clauseToInvoke, Boolean createLocalScope, Dictionary`2 functionsToDefine, List`1 variablesToDefine, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object[] args)
   at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(Boolean createLocalScope, Dictionary`2 functionsToDefine, List`1 variablesToDefine, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object[] args)
   at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Boolean propagateAllExceptionsToTop, List`1 variablesToDefine, Dictionary`2 functionsToDefine, Object[] args)
   at System.Management.Automation.ScriptBlock.InvokeUsingCmdlet(Cmdlet contextCmdlet, Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Object[] args)
   at Microsoft.PowerShell.Commands.ForEachObjectCommand.ProcessScriptBlockParameterSet()
   at Microsoft.PowerShell.Commands.ForEachObjectCommand.ProcessRecord()
   at System.Management.Automation.Cmdlet.DoProcessRecord()
   at System.Management.Automation.CommandProcessor.ProcessRecord(), while running the Get-DscResource functionality
The SendConfigurationApply function did not succeed. LCM failed to start desired state configuration manually.

Suggested solution to the issue

I can't suggest a specific solution other than to change the code that's trying to locate the service, since apparently it isn't functioning as it should. The below command executed on the target VM shows that the service in question does, in fact, exist:

[root@vm-azam-rhel7-b azops.eric.ehret]# systemctl status rhnsd.service
● rhnsd.service - LSB: Starts the Spacewalk Daemon
   Loaded: loaded (/etc/rc.d/init.d/rhnsd; bad; vendor preset: disabled)
   Active: active (running) since Sun 2023-09-10 14:48:46 UTC; 23h ago
     Docs: man:systemd-sysv-generator(8)
 Main PID: 1734 (rhnsd)
   CGroup: /system.slice/rhnsd.service
           └─1734 rhnsd

Sep 10 14:48:46 vm-azam-rhel7-b systemd[1]: Starting LSB: Starts the Spacewalk Daemon...
Sep 10 14:48:46 vm-azam-rhel7-b rhnsd[1707]: Starting Spacewalk Daemon: [  OK  ]
Sep 10 14:48:46 vm-azam-rhel7-b rhnsd[1734]: Spacewalk Services Daemon starting up, check in interval 240 minutes.
Sep 10 14:48:46 vm-azam-rhel7-b systemd[1]: Started LSB: Starts the Spacewalk Daemon.

The DSC configuration that is used to reproduce the issue (as detailed as possible)

    nxService rhnsd_disabled
    {
        Name = "rhnsd"
        Enabled = $false
        State = "Stopped"
    }

The operating system the target node is running

RHEL 7.9

Version and build of PowerShell the target node is running

7.3.6

Version of the DSC module that was used

1.2.0

gaelcolas commented 1 year ago

The command used is something like Systemctl list-units --type=service --no-legend --all --no-pager rhnsd.service

https://github.com/Azure/nxtools/blob/8741cbc6275df45ada0e80e6bf7b4084d1d331b2/source/Private/services/systemd/Get-nxSystemdService.ps1#L35

eehret commented 1 year ago

@gaelcolas Thanks! Interestingly if I manually stop the service manually and then run systemctl list-units --type=service --no-legend --all --no-pager rhnsd.service then no output is returned and exit code is zero. If I start the service manually and run same command then I get rhnsd.service loaded active running LSB: Starts the Spacewalk Daemon and exit code 0.

Not sure if this helps or not, just putting it out there :)

eehret commented 1 year ago

@jodi-boone I suspect this may have gotten fixed via https://github.com/Azure/nxtools/pull/35 Retesting now to confirm.

eehret commented 1 year ago

Confirmed, this seems to have been fixed via https://github.com/Azure/nxtools/pull/35 I am no longer able to reproduce this specific issue.