The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Adding pkgAcct Hook with varying exit codes

Discussion in 'cPanel Developers' started by KD0IGO, Sep 9, 2013.

  1. KD0IGO

    KD0IGO Member

    Joined:
    Jan 13, 2012
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    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.
     
  2. cPanelKenneth

    cPanelKenneth cPanel Development
    Staff Member

    Joined:
    Apr 7, 2006
    Messages:
    4,458
    Likes Received:
    22
    Trophy Points:
    38
    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.
     
  3. KD0IGO

    KD0IGO Member

    Joined:
    Jan 13, 2012
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    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:
    root@pstueck-passenger [/usr/local/<removed>bin/hooks]# ./returnZero 
    root@pstueck-passenger [/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.
     
  4. KostonConsulting

    KostonConsulting Well-Known Member

    Joined:
    Jun 17, 2010
    Messages:
    255
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    San Francisco, CA
    cPanel Access Level:
    Root Administrator
    This code should ensure the OS gets a 0 exit status:

    Code:
    Signal.trap('EXIT') { exit 0 }
    
    However, I'm not sure if cPanel is actually looking at an exit code as much as it's looking a STDOUT. Try just printing 0 to STDOUT at the end of the script.
     
  5. KD0IGO

    KD0IGO Member

    Joined:
    Jan 13, 2012
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    St. Charles, MO
    cPanel Access Level:
    DataCenter Provider
    Printing 0 to STDOUT worked great. Thanks!
     
  6. KD0IGO

    KD0IGO Member

    Joined:
    Jan 13, 2012
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    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.
     
  7. KostonConsulting

    KostonConsulting Well-Known Member

    Joined:
    Jun 17, 2010
    Messages:
    255
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    San Francisco, CA
    cPanel Access Level:
    Root Administrator
    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;
    }
    
     
Loading...

Share This Page