Adding pkgAcct Hook with varying exit codes

KD0IGO

Member
Jan 13, 2012
24
0
51
St. Charles, MO
cPanel Access Level
DataCenter Provider
Hello.

I have a script, written in Ruby, that is being ran during the "Pre" stage of PkgAccount.
Code:
/usr/local/cpanel/bin/manage_hooks add script /path/to/script --manual 1 --category PkgAcct --event Create --stage pre
I seem to be confused by the different actions that get taken, etc, during this run.

I'm wanting to add a check to this, so that if the script has an exit code of 0 then pkgacct runs as normal. Otherwise, any other error, the pkgAcct should quit/fail/whatever and exit with whatever text is sent from the script.

My "Error Text" is as follows:
Code:
abort("My Error Text")
This results in this script exiting with a exit code of 1. I need to figure out how to get PkgAcct to read this exit code, and fail while echoing out the abort message.

Any help here would be greatly appreciated.
 

cPanelKenneth

cPanel Development
Staff member
Apr 7, 2006
4,607
77
308
cPanel Access Level
Root Administrator
According to the documentation (Getting Started) the Hook system interprets return values as follows:

Code:
A value of 1 indicates that the hook action code performed successfully, while a value of 0 indicates that the hook action code failed.
Per that, your abort function needs to return 0 to prevent further execution.
 

KD0IGO

Member
Jan 13, 2012
24
0
51
St. Charles, MO
cPanel Access Level
DataCenter Provider
Kenneth,

I've tried to test this, but so far, this still is not succeeding.

My code:
Code:
#!/usr/bin/env ruby
#
def return_number(code)
  return code
end

return_number 0
exit 0;
No matter what, that script returns an exit code of 0.

Code:
[email protected] [/usr/local/<removed>bin/hooks]# ./returnZero 
[email protected] [/usr/local/<removed>/bin/hooks]# echo $?
0
I've added this hook to WHM for PkgAcct. However, I run PkgAcct <username> from the command line, and this is still able to run.
 

KD0IGO

Member
Jan 13, 2012
24
0
51
St. Charles, MO
cPanel Access Level
DataCenter Provider
Another question that I have regarding this.

I'm trying to add an option to "Override" the hook, so that even with the hook running and printing "0" to STDOUT, the pkgAcct function will still continue.

I have found:
--allow-override Causes the script to use instructions in /var/cpanel/lib/Whostmgr/Pkgacct/pkgacct, if any are present.

But, I'm unable to find any other documentation on how that needs to be formatted, what it needs to do, etc.

It appears that any other arguments passed to pkgacct do not get passed along within the hash. I'm wanting to add a "--force" option for example that will allow the pkgAcct call proceed.
 

KostonConsulting

Well-Known Member
Verifed Vendor
Jun 17, 2010
255
1
68
San Francisco, CA
cPanel Access Level
Root Administrator
Another question that I have regarding this.

I'm trying to add an option to "Override" the hook, so that even with the hook running and printing "0" to STDOUT, the pkgAcct function will still continue.

I have found:
--allow-override Causes the script to use instructions in /var/cpanel/lib/Whostmgr/Pkgacct/pkgacct, if any are present.

But, I'm unable to find any other documentation on how that needs to be formatted, what it needs to do, etc.

It appears that any other arguments passed to pkgacct do not get passed along within the hash. I'm wanting to add a "--force" option for example that will allow the pkgAcct call proceed.
I'm not sure if I follow. Are you looking to pass an additional flag to /scripts/pkgacct that would then be passed to your script to allow it to continue even if there are errors? If so, I don't believe this to be possible as cpsrvd determines which data gets passed to each hook and it would likely be a security issue if you could change this.

Perhaps you should instead build a development mode into your hook that checks for the existence of a debug file? For example, have the hook check for /var/cpanel/hook_debug_mode and if that file is present, it should always print 1. In addition, you could add some debug code to print additional information about the errors to a log file or /usr/local/cpanel/logs/error_log (be careful as that's a shared log file).

Psuedo-code:

Code:
if( -f '/var/cpanel/hook_debug_mode' ){
    #open error log file handle
    print ERRORHANDLE "Error: example error\n";
    print "1";
    exit;
}