Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

Access data passed from cPanel in a custom hook?

Discussion in 'cPanel Developers' started by domeneas, Apr 12, 2018.

Tags:
  1. domeneas

    domeneas Active Member

    Joined:
    Sep 20, 2013
    Messages:
    37
    Likes Received:
    2
    Trophy Points:
    8
    cPanel Access Level:
    Root Administrator
    Hi,

    I am starting to write some hooks instead of running shell scripts to further automation, but am having some trouble starting out.

    I can make and register my hook, but I cannot run it using variables, I have to hard code everything which defeats the purpose.

    Specifically I am starting with one hook that should rename a file on account upgrade/downgrade.

    What is the syntax to access the data that you read into $data using

    Code:
    my ( $context, $data ) = @_;
    ?

    I can print the information I want to /usr/local/cpanel/logs/error_log using :

    Code:
    sub _more_logging {
        my ( $context, $data ) = @_;
        my $pretty_json = JSON->new->pretty;
        $logger->info( $pretty_json->encode($context) );
        $logger->info( $pretty_json->encode($data) );
    
    
    and
    Code:
    _more_logging($context, $data);
    That gives:
    Code:
      info [whostmgr2] {
       "category" : "Whostmgr",
       "blocking" : 1,
       "event" : "Accounts::change_package",
       "point" : "main",
       "stage" : "pre"
    }
    
       info [whostmgr2] {
       "cur_pkg" : "Level1pkg",
       "new_pkg" : "Level2pkg",
       "user" : "cpanel_username"
    
    The username, current package and new package all show up, but I cannot seem to access the information in any way.

    I need to be able to use the 3 variables cur_pkg, new_pkg and user for some comparisons and to identify the file to be edited.

    I've tried entering the "user" field in $data into my own variable $username using various methods found on forums and in guides and documentation, but have been unsuccessful. I've looked at ARGV and OPTS, but am coming up short as I don't quite grasp it. All the cpanel guides I've seen do not actually use any of the passed data to the hook from what i can tell...oddly enough.

    As mentioned, if I skip the variable, the hook runs fine and does it's thing.


    Thank you in advance for any pointers.
     
  2. cPanelLauren

    cPanelLauren Forums Analyst II
    Staff Member

    Joined:
    Nov 14, 2017
    Messages:
    3,190
    Likes Received:
    227
    Trophy Points:
    173
    Location:
    Houston
    cPanel Access Level:
    DataCenter Provider
  3. domeneas

    domeneas Active Member

    Joined:
    Sep 20, 2013
    Messages:
    37
    Likes Received:
    2
    Trophy Points:
    8
    cPanel Access Level:
    Root Administrator
    Hi,

    those are the guides I have been looking at for a couple days, but I cannot see ANY example in them on how to address a field read into the $data variable.

    Tutorial page: In the example the only things they manipulate is a hard coded backup file. They never use anything passed from cpanel to the hook through $data. From what I gather, there isn't even a point to reading:
    Code:
    my ( $context, $data ) = @_;
    in the example code at all.

    WHOSTMGR functions page: Lists the information and field names put into $data. As stated, I can output those fine into the log file, see my post above, I know they are there, but I cannot access them in the perl module I am writing.

    Developer Documentation: Really only links to the two guides above. It does however say
    So that what I'm doing.

    Specifically, I need to write a statement similar to:
    Code:
    if $data(cur_pkg) = "Level2pkg" and if $data(new_pkg) = "Level1pkg" {
    move ($old_name, $new_name) or die "Rename failed: $!";
    } else {
    die "No close needed: $!";
    }
    The $old_name and $new_name is based on $data's "user" field.

    How do I address the fields "cur_pkg", "new_pkg" and "user" passed into $data after running
    Code:
    my ( $context, $data ) = @_;
    ?

    Thanks again.
     
    #3 domeneas, Apr 13, 2018
    Last edited: Apr 13, 2018
  4. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    44,822
    Likes Received:
    1,898
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
Loading...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice