Facter ignoring variables at runtime - variables

I have a Facter custom fact for use in Puppet that should generate a list of specific information, but it doesn't always work correctly, even on servers running the exact same versions of ruby, facter, puppet, and mysql. The code is below:
#backends.rb
puts "Backends retrieval"
if Facter.value('demainversion') == '2'
dbasename = Facter.value('dbasename')
if dbasename == ''
dbasename = 'database'
end
mysqlcmd = "mysql -uUser -pPassword #{dbasename} --skip_column_names -e "
if Facter.value('clustered')
dbasename = Facter.value('dbasename')
mysqlcmd = "mysql -uUser -pPassword -hclustered-host #{dbasename} --skip_column_names -e "
end
puts mysqlcmd
clients = Facter.value('databases').split(', ')
if clients != ''
clients.each do |client|
count = 0
client_id =
res =
backends = ""
facter_name = "#{client.gsub(' ','_')}_backends"
Facter.add("#{facter_name}") do
setcode do
puts "#{client} listing"
client_id = %x[#{mysqlcmd}"SELECT id FROM UserGroup WHERE groupName='#{client.strip}'"].strip
puts "#{client_id} listing"
puts backends
res = %x[#{mysqlcmd}"SELECT sourceMachine,sourceUser FROM GrabberJob WHERE clientGroup_id=#{client_id.strip}"].strip
res.each do |row|
machine = row.split(' ')[0]
login = row.split(' ')[1]
val = login.gsub('admin','')
if count == 0
backends << machine.strip << "(" << val << ")"
else
backends << ", " << machine.strip << "(" << val << ")"
end
count += 1
end
backends.strip
end
end
end
end
end
When puppet (or irb) runs this it may (or may not) return ...
ERROR 1046 (3D000) at line 1: No database selected
... but if the server throws the error it will continue to exhibit the problem. I know that the dbasename variable is set correctly, courtesy of the puts statements used for debugging, but the variable is ignored when the mysql commands are actually run.
I have also tried adding a USE #{dbasename}; statement to the client_id and res commands to no avail. Any suggestions on what I could do to make it work consistently would be most appreciated.

Related

No matching records found (ODBC -2028) in SAP DI Service Call

When I try to make a new service call I get a No matching records found (ODBC -2028) here is my code:
Dim sC = company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oServiceCalls)
sC.CustomerCode = recordSet.Fields.Item(2).Value Logging(sC.CustomerCode)
sC.Subject = recordSet.Fields.Item(3).Value
sC.Description = recordSet.Fields.Item(5).Value
sC.InternalSerialNum = recordSet.Fields.Item(11).Value
sC.ItemCode = recordSet.Fields.Item(11).Value
Dim ret = sC.Add()
If ret <> 0 Then company.GetLastError(ErrCode, ErrMsg)
Logging("Error in service call: " + ErrCode.ToString() + " : " + ErrMsg)
End If
Every value is valid. When I remove the InternalSerialNum line. it is working. The InetrnalSerialNum is valid too. And on the other machine, this code is working.
How can I solve that problem?

sql SELECT returns a string in an array, the return_format is set to "string" tho. Sinatra

I'm currently working on a very basic login system, for a Web development school project. My Problem is, that both variables have the same values but their data-types are different, so the if block does not execute the code for the "true" condition. I set the return_format to "string" but i still get a string in an array. I also already tried to convert both variables into a string(.to_s), but that didn't helped me at all.
Here is the code:
pass = use_database "EMTippspiel", :return_format => "string" do |db|
sql = "SELECT Passwort FROM user WHERE Benutzername = '" + username + "';"
db.execute sql
end
if params[:password].to_s == pass
use_database "EMTippspiel" do |db|
sql = "UPDATE user SET Online = 'true', IP = '" + ip + "' WHERE Benutzername = '" + username + "';"
db.execute sql
end
redirect to("/hauptseite")
else
puts "="*80 #for debugging
p username
p params[:password]
p pass
puts "="*80
#success = false
redirect to("/anmeldung")
end
This is what the console window prints out:
======================================================
"Jemin"
"1965"
[["1965"]]
======================================================
found the solution, pass was actually an array, so the resolution is:
if params[:password].to_s == pass[0][0]

How to implement error handling?

My AutoIt script generates an error that I want to handle. A way that any error goes to a custom function will also do. In VBA I use OnErrorGoTo, but I am unable to find something similar in AutoIt.
My Code :
Func Start()
While 1
If ProcessExists ( "Photoshop.exe" ) <> 0 Then
Sleep(5000)
Else
Local $sFile ="C:\Auto\CodeToBe\Batch\Image Process-50-2D v.2-" & $n & ".jsxbin"
Local $iPID = ShellExecute($sFile)
Sleep(10000)
$n = $n+1
EndIf
WEnd
EndFunc
An error will occur when $n exceeds the number of files in that folder. I tried this but didn't work (from the "HELP SECTION" and a forum post):
Global $iEventError = 0 ; To be checked to know if COM error occurs. Must be reset after handling.
Local $oMyError = ObjEvent("AutoIt.Error", "ErrFunc") ; Install a custom error handler
Func Start()
While 1
If ProcessExists ( "Photoshop.exe" ) <> 0 Then
Sleep(5000)
Else
Local $sFile ="C:\Auto\CodeToBe\Batch\Image Process-50-2D v.2-" & $n & ".jsxbin"
Local $iPID = ShellExecute($sFile)
If $iEventError Then
MsgBox($MB_OK, "", "There was an error on the previous line.")
$iEventError = 0 ; Reset after displaying a COM Error occurred
EndIf
Sleep(10000)
$n = $n+1
EndIf
WEnd
EndFunc
; This is my custom error handler
Func MyErrFunc()
Msgbox(0,"","ERROR GENERATED ON " & $n)
Endfunc
I recommend the second example because it prevents an error in the first place. However, the first example can be used as a general error checker.
Example 1
Start()
Func Start()
Local $n = 1
While 1
If ProcessExists("Photoshop.exe") <> 0 Then
Sleep(5000)
Else
Local $sFile = "C:\Auto\CodeToBe\Batch\Image Process-50-2D v.2-" & $n & ".jsxbin"
Local $iPID = ShellExecute($sFile)
If #error Then MyErrFunc(#ScriptLineNumber, #error) ;check for error
Sleep(10000)
$n = $n + 1
EndIf
WEnd
EndFunc ;==>Start
; error handler
Func MyErrFunc($iLineNumer, $iError)
$iLineNumer = $iLineNumer - 1
MsgBox(0, "", "ERROR GENERATED ON SCRIPT LINE: " & $iLineNumer & #CRLF & "ERROR CODE: " & $iError)
EndFunc ;==>MyErrFunc
Example 2
Start2()
Func Start2()
Local $n = 1
While 1
If ProcessExists("Photoshop.exe") <> 0 Then
Sleep(5000)
Else
Local $sFile = "C:\Auto\CodeToBe\Batch\Image Process-50-2D v.2-" & $n & ".jsxbin"
If FileExists($sFile) Then
Local $iPID = ShellExecute($sFile)
Sleep(10000)
Else ;handle error (you could use a function here if you wanted)
ConsoleWrite("File not found: " & $sFile & #CRLF)
EndIf
$n = $n + 1
EndIf
WEnd
EndFunc ;==>Start2
Try to implement error checking.
If Not FileExists(your string with the $n) Then
... abort
Else
shellexecute ...
You could use _FileListToArray() instead.
An error will occur when $n exceeds the number of files in that folder.
As per Documentation - Function Reference - _FileListToArray() :
Lists files and\or folders in a specified folder (Similar to using Dir with the /B Switch)
Will loop over existing filenames only (avoiding error altogether). Example:
#include <File.au3>
Global Const $g_sPathFiles = 'C:\Auto\CodeToBe\Batch\'
Global Const $g_sMaskFile = '*.jsxbin'
Global Const $g_aFile = _FileListToArray($g_sPathFiles, $g_sMaskFile, $FLTA_FILES, True)
For $i1 = 1 To $g_aFile[0]
ShellExecuteWait($g_aFile[$i1])
Next
Alternatively _FileListToArrayRec() may be used.
Related.

VBS script return expected result, how to compare float value?

I am getting an interesting result when executing the following VB script.
Set StdOut = WScript.StdOut
Set wbemSvc = GetObject("winmgmts://" & "." & "/root/cimv2")
Set biosSet = wbemSvc.ExecQuery("Select * from Win32_BIOS")
For Each biosObj In biosSet
StdOut.WriteLine "SMBIOSMajorVersion=" & biosObj.SMBIOSMajorVersion
StdOut.WriteLine "SMBIOSMinorVersion=" & biosObj.SMBIOSMinorVersion
Next
StdOut.WriteLine "Return value is: " & IsNewBiosVersion
Function IsNewBiosVersion()
On Error Resume Next
Set biosSet = wbemSvc.ExecQuery("Select * from Win32_BIOS")
newBios = 0
For Each bios In biosSet
minorFloat = "." & bios.SMBIOSMinorVersion
If bios.SMBIOSMajorVersion > 2 OR (bios.SMBIOSMajorVersion = 2 AND minorFloat >= .6) Then
newBios = 1
End If
Next
IsNewBiosVersion = newBios
End Function
The result is as follow. This looks contradictory since the SMBIOSMinorVersion=4, according to the code logic in the script, the return value should be 0!!!
SMBIOSMajorVersion=2
SMBIOSMinorVersion=4
Return value is: 1
I ran this same script on another system and got the expected correct result.
SMBIOSMajorVersion=2
SMBIOSMinorVersion=4
Return value is: 0
So what is the problem here?
New update:
We execute the following script again on the system, and found that the CDbl() function does not convert the string "2.4" to double value correctly, instead it converts it to 24! Looks like the dot "." was lost when converting, what is wrong with this? An bug in CDbl or a violation when use it?
here is the script
Set StdOut = WScript.StdOut
StdOut.WriteLine ""
StdOut.WriteLine "Simple Function to Test BIOS Version"
StdOut.WriteLine ""
Set wbemSvc = GetObject("winmgmts://" & "." & "/root/cimv2")
Set biosSet = wbemSvc.ExecQuery("Select * from Win32_BIOS")
For Each bios In biosSet
newBios = 0
StdOut.WriteLine "SMBIOSMajorVersion=" & bios.SMBIOSMajorVersion
StdOut.WriteLine "SMBIOSMinorVersion=" & bios.SMBIOSMinorVersion
temp = bios.SMBIOSMajorVersion & "." & bios.SMBIOSMinorVersion
StdOut.WriteLine "major dot minor=" & temp
currentBios = CDbl(bios.SMBIOSMajorVersion & "." & bios.SMBIOSMinorVersion)
StdOut.WriteLine ""
StdOut.WriteLine "currentBios=" & currentBios
If currentBios >= 2.6 Then newBios = 1
StdOut.WriteLine "return value is: " & newBios
Next
here is the output
Simple Function to Test BIOS Version
SMBIOSMajorVersion=2
SMBIOSMinorVersion=4
major dot minor=2.4
currentBios=24
return value is: 1
Remove your error handling - it s probably suppressing an issue.
On Error Resume Next ' Not a good idea
For one thing you are comparing a string to a double in these lines:
minorFloat = "." & bios.SMBIOSMinorVersion
If bios.SMBIOSMajorVersion > 2 OR (bios.SMBIOSMajorVersion = 2 AND minorFloat >= .6) Then
Why not just convert the major.minor values to floating-point for your test? You're currently doing two separate tests, with one being a string vs float comparison, which is unusual.
Maybe try this instead?
currentBios = CDbl(bios.SMBIOSMajorVersion & "." & bios.SMBIOSMinorVersion)
If currentBios >= 2.6 Then newBios = 1
You have to be careful comparing floating-point values when math operations are involved but for literal values you'll be fine.
And, as already mentioned, remove On Error Resume Next or you may never know why it works on one PC but not another.

Way to Jump to Next i in For..Next Loop?

I'm reverse engineering in QuickBasic and I have code like this:
FOR i = star TO fin
IF a < 1 THEN
CALL clrbot
COLOR 15
PRINT force$(side); " army in "; city$(armyloc(i)); " is CUT OFF !";
TICK turbo!
GOTO alone
END IF
size = size + 1
max = 11: IF LEN(armyname$(i)) < 11 THEN max = LEN(armyname$(i))
mtx$(size) = LEFT$(armyname$(i), max)
array(size) = i
alone:
NEXT i
I'd like to get rid of the line label (alone) and instead do something like:
IF a < 1 THEN
CALL clrbot
COLOR 15
PRINT force$(side); " army in "; city$(armyloc(i)); " is CUT OFF !";
TICK turbo!
NEXT i
END IF
You could replace the GOTO with an Else:
For i = star To Fin
If a < 1 Then
' Do something
Else
' Do Something else
End If
Next
This would follow the same logic - the Else takes the place of the GOTO alone statement.
In the original code (QuickBASIC) if the If block is entered, everything after then GOTO alone statement is ignored.
If the If block is not entered (i.e., a >= 1) then everything after the If block is executed.
The Else statement in the VB.NET code will produce the same behavior. If a < 1, the first block will be executed and the Else block will be ignored, and the loop will advance to the next increment of i.
If a >= 1, then the Else block will be executed and the loop will then advance to the next increment of i.
The above assumes labels in QuickBASIC are like labels in DOS batch files.